File:  [gforth] / gforth / struct.fs
Revision 1.25: download - view: text, annotated - select for diffs
Mon Dec 31 18:40:24 2007 UTC (11 years, 9 months ago) by anton
Branches: MAIN
CVS tags: v0-7-0, HEAD
updated copyright notices for GPL v3

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

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