[gforth] / gforth / compat / struct.fs  

gforth: gforth/compat/struct.fs


1 : anton 1.1 \ data structures (like C structs)
2 :    
3 : anton 1.3 \ This file is in the public domain. NO WARRANTY.
4 : anton 1.1
5 : anton 1.3 \ This program uses the following words
6 :     \ from CORE :
7 :     \ : 1- + swap invert and ; DOES> @ immediate drop Create rot dup , >r
8 :     \ r> IF ELSE THEN over chars aligned cells 2* here - allot
9 :     \ from CORE-EXT :
10 :     \ tuck pick nip
11 :     \ from BLOCK-EXT :
12 :     \ \
13 :     \ from DOUBLE :
14 :     \ 2Constant
15 :     \ from EXCEPTION :
16 :     \ throw
17 :     \ from FILE :
18 :     \ (
19 :     \ from FLOAT :
20 :     \ faligned floats
21 :     \ from FLOAT-EXT :
22 :     \ dfaligned dfloats sfaligned sfloats
23 :     \ from MEMORY :
24 :     \ allocate
25 : anton 1.2
26 : anton 1.6 : naligned ( addr1 n -- addr2 )
27 :     \ addr2 is the aligned version of addr1 wrt the alignment size n
28 :     1- tuck + swap invert and ;
29 :    
30 :     : nalign naligned ; \ old name, obsolete
31 : anton 1.1
32 : anton 1.2 : dofield ( -- )
33 :     does> ( name execution: addr1 -- addr2 )
34 :     @ + ;
35 :    
36 :     : dozerofield ( -- )
37 :     immediate
38 :     does> ( name execution: -- )
39 :     drop ;
40 :    
41 : anton 1.3 : create-field ( align1 offset1 align size "name" -- align2 offset2 )
42 : anton 1.5 create swap rot over nalign dup , ( align1 size align offset )
43 :     rot + >r nalign r> ;
44 : anton 1.2
45 : anton 1.3 : field ( align1 offset1 align size "name" -- align2 offset2 )
46 : anton 1.2 \ name execution: addr1 -- addr2
47 : anton 1.3 2 pick >r \ this uglyness is just for optimizing with dozerofield
48 : anton 1.2 create-field
49 : anton 1.3 r> if \ offset<>0
50 : anton 1.2 dofield
51 :     else
52 :     dozerofield
53 :     then ;
54 : anton 1.1
55 : anton 1.3 : end-struct ( align size "name" -- )
56 :     over nalign \ pad size to full alignment
57 : anton 1.2 2constant ;
58 : anton 1.1
59 : anton 1.3 \ an empty struct
60 :     1 chars 0 end-struct struct
61 : anton 1.1
62 : anton 1.3 \ type descriptors, all ( -- align size )
63 :     1 aligned 1 cells 2constant cell%
64 :     1 chars 1 chars 2constant char%
65 :     1 faligned 1 floats 2constant float%
66 :     1 dfaligned 1 dfloats 2constant dfloat%
67 :     1 sfaligned 1 sfloats 2constant sfloat%
68 :     cell% 2* 2constant double%
69 :    
70 :     \ memory allocation words
71 : anton 1.4 : %alignment ( align size -- align )
72 :     drop ;
73 :    
74 :     : %size ( align size -- size )
75 :     nip ;
76 :    
77 : anton 1.3 : %align ( align size -- )
78 :     drop here swap nalign here - allot ;
79 : anton 1.1
80 : anton 1.3 : %allot ( align size -- addr )
81 :     tuck %align
82 : anton 1.1 here swap allot ;
83 :    
84 : anton 1.3 : %allocate ( align size -- addr ior )
85 :     nip allocate ;
86 : anton 1.2
87 : anton 1.7 : %alloc ( align size -- addr )
88 : anton 1.3 %allocate throw ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help