Re: C Structures

From: Elizabeth D. Rather <erather_at_forth.com>
Date: Sun, 15 Apr 2001 12:12:24 -0700

At 12:01 PM 4/15/2001 -0700, you wrote:

>Hello all. I'm attempting to put together a document (which I will
>post for comment) describing how to interpret C structures for use in
>a SwiftForth program. Windows makes extensive use structures in the
>API and these structures are typically documented using C struct
>statements. Unless one is "C Literate", it can be difficult to
>translate the syntax. C compilers can, depending on compiler
>directives, pad variables to byte, word or dword boundaries adding
>another layer of complexity (confusion).
>
>Could anyone point me to a resource that details some of the fine
>points of C structures and how a C compiler interprets these
>structures?

I know very little about C, but have been working with Open Firmware
(the plug-and-play boot firmware developed at Sun and used in
many non-Intel PCI-bus systems by Sun, Apple, IBM, and others).

It contains the following Forth "equivalent" to C structs:

\ Structs
0 constant struct
: field ( n1 size -- n2 ) create over , +
    does> ( a1 -- a2 ) @ + ;

\ Example
struct ( scsi registers)
         0c field >cmd-adr \ Up to 12 command bytes
          4 field >cmd-len \ Length of command block
          4 field >data-adr \ Base address of DMA data area
          4 field >data-len \ Length of data area
          1 field >host-selectid \ Host's selection ID
          1 field >target-selectid \ Target's selection ID
          1 field >input? \ 1 for output, 0 for input
          1 field >message-out \ Outgoing message byte
         [ more fields ]
constant /scsi-regs \ Size of register structure

This is used as a template for accessing hardware registers;
you give the base address of the register bank followed by a
field name, which applies the appropriate offset.

Since this was developed by a C expert for use by others whose
first language is C, it's probably a pretty close equivalent.

Cheers,
Elizabeth

----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
For help with listar commands, send subject "help" to listar_at_forth.com
Archives are located at http://www.forth.com/sftalk -- check them out!
Received on Sun Apr 15 2001 - 12:16:09 PDT


Subscribe to our e-mail list service. It's free for all SwiftForth and SwiftX users!

This archive was generated 08-Feb-2012. Archive updated nightly.