File:  [gforth] / gforth / struct.fs
Revision 1.13: download - view: text, annotated - select for diffs
Tue Apr 14 16:43:07 1998 UTC (25 years, 11 months ago) by anton
Branches: MAIN
CVS tags: v0-4-0, HEAD
emacs update bug fix in gforth.el
bug fix in struct.fs, compat/struct.fs
main.c: stdout is now unbuffered, if it is a tty

    1: \ data structures (like C structs)
    2: 
    3: \ Copyright (C) 1995, 1997 Free Software Foundation, Inc.
    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
   19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   20: 
   21: : nalign ( addr1 n -- addr2 ) \ gforth
   22: \g @code{addr2} is the aligned version of @code{addr1} wrt the
   23: \g alignment @code{n}.
   24:  1- tuck +  swap invert and ;
   25: 
   26: : dozerofield ( -- )
   27:     \ a field that makes no change
   28:     \ to enable accessing the offset with "['] <field> >body @" this
   29:     \ is not implemented with "['] noop alias"
   30:     last @
   31:     if
   32: 	immediate
   33:     then
   34: does> ( name execution: -- )
   35:     drop ;
   36: 
   37: : field, ( align1 offset1 align size --  align2 offset2 )
   38:     swap rot over nalign dup , ( align1 size align offset )
   39:     rot + >r nalign r> ;
   40: 
   41: : create-field ( align1 offset1 align size --  align2 offset2 )
   42:     create field, ;
   43: 
   44: : field ( align1 offset1 align size "name" --  align2 offset2 ) \ gforth
   45:     \G name execution: ( addr1 -- addr2 )
   46:     2 pick 
   47:     if \ field offset <> 0
   48: 	[IFDEF]  (Field)
   49: 	    (Field)
   50: 	[ELSE]
   51: 	    Header reveal dofield: cfa,
   52: 	[THEN]
   53:     else
   54: 	create dozerofield
   55:     then
   56:     field, ;
   57: 
   58: : end-struct ( align size "name" -- ) \ gforth
   59: \g @code{name} execution: @code{addr1 -- addr1+offset1}@*
   60: \g create a field @code{name} with offset @code{offset1}, and the type
   61: \g given by @code{size align}. @code{offset2} is the offset of the
   62: \g next field, and @code{align2} is the alignment of all fields.
   63:     over nalign \ pad size to full alignment
   64:     2constant ;
   65: 
   66: 1 chars 0 end-struct struct ( -- align size ) \ gforth
   67: \g an empty structure, used to start a structure definition.
   68: 
   69: \ type descriptors
   70: 1 aligned   1 cells   2constant cell% ( -- align size ) \ gforth
   71: 1 chars     1 chars   2constant char% ( -- align size ) \ gforth
   72: 1 faligned  1 floats  2constant float% ( -- align size ) \ gforth
   73: 1 dfaligned 1 dfloats 2constant dfloat% ( -- align size ) \ gforth
   74: 1 sfaligned 1 sfloats 2constant sfloat% ( -- align size ) \ gforth
   75: cell% 2*              2constant double% ( -- align size ) \ gforth
   76: 
   77: \ memory allocation words
   78: ' drop alias %alignment ( align size -- align ) \ gforth
   79: \g the alignment of the structure
   80: ' nip alias %size ( align size -- size ) \ gforth
   81: \g the size of the structure
   82: 
   83: : %align ( align size -- ) \ gforth
   84:     \G align the data space pointer to the alignment @code{align}. 
   85:     drop here swap nalign here - allot ;
   86: 
   87: : %allot ( align size -- addr ) \ gforth
   88:     \g allot @code{size} address units of data space with alignment
   89:     \g @code{align}; the resulting block of data is found at
   90:     \g @code{addr}.
   91:     tuck %align
   92:     here swap allot ;
   93: 
   94: : %allocate ( align size -- addr ior ) \ gforth
   95:     \g allocate @code{size} address units with alignment @code{align},
   96:     \g similar to @code{allocate}.
   97:     nip allocate ;
   98: 
   99: : %alloc ( size align -- addr ) \ gforth
  100:     \g allocate @code{size} address units with alignment @code{align},
  101:     \g giving a data block at @code{addr}; @code{throw}s an ior code
  102:     \g if not successful.
  103:     %allocate throw ;

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