Diff for /gforth/struct.fs between versions 1.11 and 1.21

version 1.11, 1997/06/06 17:27:58 version 1.21, 2003/03/09 15:16:56
Line 1 Line 1
 \ data structures (like C structs)  \ data structures (like C structs)
   
 \ Copyright (C) 1995, 1997 Free Software Foundation, Inc.  \ Copyright (C) 1995,1997,2000,2003 Free Software Foundation, Inc.
   
 \ This file is part of Gforth.  \ This file is part of Gforth.
   
Line 16 Line 16
   
 \ You should have received a copy of the GNU General Public License  \ You should have received a copy of the GNU General Public License
 \ along with this program; if not, write to the Free Software  \ along with this program; if not, write to the Free Software
 \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   
   : naligned ( addr1 n -- addr2 ) \ gforth
 \ Usage example:  \g @var{addr2} is the aligned version of @var{addr1} with respect to the
 \  \g alignment @var{n}.
 \ struct  
 \     1 cells: field search-method  
 \     1 cells: field reveal-method  
 \ end-struct wordlist-map  
 \  
 \ The structure can then be extended in the following way  
 \ wordlist-map  
 \     1 cells: field enum-method  
 \ end-struct ext-wordlist-map \ with the fields search-method,...,enum-method  
   
 : nalign ( addr1 n -- addr2 )  
 \ addr2 is the aligned version of addr1 wrt the alignment size n  
  1- tuck +  swap invert and ;   1- tuck +  swap invert and ;
   
   ' naligned alias nalign \ old name, obsolete
   
 : dozerofield ( -- )  : dozerofield ( -- )
     \ a field that makes no change      \ a field that makes no change
     \ to enable accessing the offset with "['] <field> >body @" this      \ to enable accessing the offset with "['] <field> >body @" this
Line 43 Line 33
     if      if
         immediate          immediate
     then      then
 does> ( -- )  does> ( name execution: -- )
     drop ;      drop ;
   
 : field ( offset1 align1 size align "name" -- offset2 align2 ) \ gforth  : field, ( align1 offset1 align size --  align2 offset2 )
     \G name execution: ( addr1 -- addr2 )      swap rot over nalign dup , ( align1 size align offset )
     >r rot r@ nalign dup      rot + >r nalign r> ;
   
   : create-field ( align1 offset1 align size --  align2 offset2 )
       create field, ;
   
   : field ( align1 offset1 align size "name" --  align2 offset2 ) \ gforth
       \g Create a field @var{name} with offset @var{offset1}, and the type
       \g given by @var{align size}. @var{offset2} is the offset of the
       \g next field, and @var{align2} is the alignment of all fields.@*
       \g @code{name} execution: @var{addr1} -- @var{addr2}.@*
       \g @var{addr2}=@var{addr1}+@var{offset1}
       2 pick 
     if \ field offset <> 0      if \ field offset <> 0
         [IFDEF]  (Field)          [IFDEF]  (Field)
             (Field)              (Field)
Line 57  does> ( -- ) Line 58  does> ( -- )
         [THEN]          [THEN]
     else      else
         create dozerofield          create dozerofield
     then ( align1 size offset )      then
     dup , + swap r> nalign ;      field, ;
   
 : end-struct ( size align -- )  
  2constant ;  
   
 0 1 chars end-struct struct  
   
 \ : field  ( offset1 align1 size align -- offset2 align2 )  
 \    create-field  
 \    does> ( addr1 -- addr2 )  
 \       @ + ;  
   
 \ I don't really like the "type:" syntax. Any other ideas? - anton  
 \ Also, this seems to be somewhat general. It probably belongs to some  
 \ other place  
 : cells: ( n -- size align )  
     cells cell ;  
   
 : doubles: ( n -- size align )  
     2* cells cell ;  
   
 : chars: ( n -- size align )  
     chars 1 chars ;  
   
 : floats: ( n -- size align )  
     floats 1 floats ;  
   
 : dfloats: ( n -- size align )  
     dfloats 1 dfloats ;  
   
 : sfloats: ( n -- size align )  
     sfloats 1 sfloats ;  
   
 : struct-align ( size align -- )  
     dp @ swap nalign dp !  
     drop ;  
   
 : struct-allot ( size align -- addr )  : end-struct ( align size "name" -- ) \ gforth
     over swap struct-align  \g Define a structure/type descriptor @var{name} with alignment
   \g @var{align} and size @var{size1} (@var{size} rounded up to be a
   \g multiple of @var{align}).@*
   \g @code{name} execution: -- @var{align size1}@*
       over nalign \ pad size to full alignment
       2constant ;
   
   1 chars 0 end-struct struct ( -- align size ) \ gforth
   \g An empty structure, used to start a structure definition.
   
   \ type descriptors
   1 aligned   1 cells   2constant cell% ( -- align size ) \ gforth
   1 chars     1 chars   2constant char% ( -- align size ) \ gforth
   1 faligned  1 floats  2constant float% ( -- align size ) \ gforth
   1 dfaligned 1 dfloats 2constant dfloat% ( -- align size ) \ gforth
   1 sfaligned 1 sfloats 2constant sfloat% ( -- align size ) \ gforth
   cell% 2*              2constant double% ( -- align size ) \ gforth
   
   \ memory allocation words
   ' drop alias %alignment ( align size -- align ) \ gforth
   \g The alignment of the structure.
   ' nip alias %size ( align size -- size ) \ gforth
   \g The size of the structure.
   
   : %align ( align size -- ) \ gforth
       \G Align the data space pointer to the alignment @var{align}. 
       drop here swap nalign here - allot ;
   
   : %allot ( align size -- addr ) \ gforth
       \g Allot @var{size} address units of data space with alignment
       \g @var{align}; the resulting block of data is found at
       \g @var{addr}.
       tuck %align
     here swap allot ;      here swap allot ;
   
 : struct-allocate ( size align -- addr ior )  : %allocate ( align size -- addr ior ) \ gforth
     drop allocate ;      \g Allocate @var{size} address units with alignment @var{align},
       \g similar to @code{allocate}.
 : struct-alloc ( size align -- addr )      nip allocate ;
     struct-allocate throw ;  
   : %alloc ( size align -- addr ) \ gforth
       \g Allocate @var{size} address units with alignment @var{align},
       \g giving a data block at @var{addr}; @code{throw} an ior code
       \g if not successful.
       %allocate throw ;

Removed from v.1.11  
changed lines
  Added in v.1.21


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