Annotation of gforth/struct.fs, revision 1.20

1.8       anton       1: \ data structures (like C structs)
                      2: 
1.20    ! anton       3: \ Copyright (C) 1995,1997,2000 Free Software Foundation, Inc.
1.8       anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
1.18      anton      19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.8       anton      20: 
1.14      anton      21: : naligned ( addr1 n -- addr2 ) \ gforth
1.15      crook      22: \g @var{addr2} is the aligned version of @var{addr1} with respect to the
                     23: \g alignment @var{n}.
1.1       anton      24:  1- tuck +  swap invert and ;
1.14      anton      25: 
                     26: ' naligned alias nalign \ old name, obsolete
1.1       anton      27: 
1.11      anton      28: : dozerofield ( -- )
                     29:     \ a field that makes no change
                     30:     \ to enable accessing the offset with "['] <field> >body @" this
                     31:     \ is not implemented with "['] noop alias"
                     32:     last @
                     33:     if
                     34:        immediate
                     35:     then
1.12      anton      36: does> ( name execution: -- )
1.11      anton      37:     drop ;
                     38: 
1.12      anton      39: : field, ( align1 offset1 align size --  align2 offset2 )
1.13      anton      40:     swap rot over nalign dup , ( align1 size align offset )
                     41:     rot + >r nalign r> ;
1.12      anton      42: 
                     43: : create-field ( align1 offset1 align size --  align2 offset2 )
                     44:     create field, ;
                     45: 
                     46: : field ( align1 offset1 align size "name" --  align2 offset2 ) \ gforth
1.16      anton      47:     \g Create a field @var{name} with offset @var{offset1}, and the type
                     48:     \g given by @var{align size}. @var{offset2} is the offset of the
                     49:     \g next field, and @var{align2} is the alignment of all fields.@*
                     50:     \g @code{name} execution: @var{addr1} -- @var{addr2}.@*
                     51:     \g @var{addr2}=@var{addr1}+@var{offset1}
1.12      anton      52:     2 pick 
1.11      anton      53:     if \ field offset <> 0
                     54:        [IFDEF]  (Field)
                     55:            (Field)
                     56:        [ELSE]
                     57:            Header reveal dofield: cfa,
                     58:        [THEN]
                     59:     else
                     60:        create dozerofield
1.12      anton      61:     then
                     62:     field, ;
1.1       anton      63: 
1.12      anton      64: : end-struct ( align size "name" -- ) \ gforth
1.16      anton      65: \g Define a structure/type descriptor @var{name} with alignment
                     66: \g @var{align} and size @var{size1} (@var{size} rounded up to be a
                     67: \g multiple of @var{align}).@*
                     68: \g @code{name} execution: -- @var{align size1}@*
1.12      anton      69:     over nalign \ pad size to full alignment
                     70:     2constant ;
                     71: 
                     72: 1 chars 0 end-struct struct ( -- align size ) \ gforth
1.15      crook      73: \g An empty structure, used to start a structure definition.
1.12      anton      74: 
                     75: \ type descriptors
                     76: 1 aligned   1 cells   2constant cell% ( -- align size ) \ gforth
                     77: 1 chars     1 chars   2constant char% ( -- align size ) \ gforth
                     78: 1 faligned  1 floats  2constant float% ( -- align size ) \ gforth
                     79: 1 dfaligned 1 dfloats 2constant dfloat% ( -- align size ) \ gforth
                     80: 1 sfaligned 1 sfloats 2constant sfloat% ( -- align size ) \ gforth
                     81: cell% 2*              2constant double% ( -- align size ) \ gforth
                     82: 
                     83: \ memory allocation words
                     84: ' drop alias %alignment ( align size -- align ) \ gforth
1.15      crook      85: \g The alignment of the structure.
1.12      anton      86: ' nip alias %size ( align size -- size ) \ gforth
1.15      crook      87: \g The size of the structure.
1.12      anton      88: 
                     89: : %align ( align size -- ) \ gforth
1.15      crook      90:     \G Align the data space pointer to the alignment @var{align}. 
1.12      anton      91:     drop here swap nalign here - allot ;
                     92: 
                     93: : %allot ( align size -- addr ) \ gforth
1.15      crook      94:     \g Allot @var{size} address units of data space with alignment
                     95:     \g @var{align}; the resulting block of data is found at
                     96:     \g @var{addr}.
1.12      anton      97:     tuck %align
1.2       anton      98:     here swap allot ;
                     99: 
1.12      anton     100: : %allocate ( align size -- addr ior ) \ gforth
1.15      crook     101:     \g Allocate @var{size} address units with alignment @var{align},
1.12      anton     102:     \g similar to @code{allocate}.
                    103:     nip allocate ;
                    104: 
                    105: : %alloc ( size align -- addr ) \ gforth
1.15      crook     106:     \g Allocate @var{size} address units with alignment @var{align},
                    107:     \g giving a data block at @var{addr}; @code{throw} an ior code
1.12      anton     108:     \g if not successful.
                    109:     %allocate throw ;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>