File: struct.fs

package info (click to toggle)
gforth 0.7.3%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 5,992 kB
  • sloc: ansic: 8,535; sh: 3,666; lisp: 1,778; makefile: 1,019; yacc: 186; sed: 141; lex: 102; awk: 21
file content (108 lines) | stat: -rw-r--r-- 3,840 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
\ data structures (like C structs)

\ Copyright (C) 1995,1997,2000,2003,2007 Free Software Foundation, Inc.

\ This file is part of Gforth.

\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation, either version 3
\ of the License, or (at your option) any later version.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
\ GNU General Public License for more details.

\ You should have received a copy of the GNU General Public License
\ along with this program. If not, see http://www.gnu.org/licenses/.

: naligned ( addr1 n -- addr2 ) \ gforth
\g @var{addr2} is the aligned version of @var{addr1} with respect to the
\g alignment @var{n}.
 1- tuck +  swap invert and ;

' naligned alias nalign \ old name, obsolete

: dozerofield ( -- )
    \ a field that makes no change
    \ to enable accessing the offset with "['] <field> >body @" this
    \ is not implemented with "['] noop alias"
    latest
    if
	immediate
    then
does> ( name execution: -- )
    drop ;

: field, ( align1 offset1 align size --  align2 offset2 )
    swap rot over nalign dup , ( align1 size align offset )
    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
	[IFDEF]  (Field)
	    (Field)
	[ELSE]
	    Header reveal dofield: cfa,
	[THEN]
    else
	create dozerofield
    then
    field, ;

: end-struct ( align size "name" -- ) \ gforth
\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 ;

: %allocate ( align size -- addr ior ) \ gforth
    \g Allocate @var{size} address units with alignment @var{align},
    \g similar to @code{allocate}.
    nip allocate ;

: %alloc ( align size -- 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 ;