Annotation of gforth/fflib.fs, revision 1.18
1.2 pazsan 1: \ lib.fs shared library support package 16aug03py
1.1 pazsan 2:
1.17 anton 3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005,2006,2007 Free Software Foundation, Inc.
1.1 pazsan 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
1.18 ! anton 9: \ as published by the Free Software Foundation, either version 3
1.1 pazsan 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
1.18 ! anton 18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1 pazsan 19:
20: Variable libs 0 libs !
1.4 pazsan 21: \ links between libraries
1.1 pazsan 22: Variable thisproc
23: Variable thislib
1.4 pazsan 24:
1.2 pazsan 25: Variable revdec revdec off
26: \ turn revdec on to compile bigFORTH libraries
1.4 pazsan 27: Variable revarg revarg off
28: \ turn revarg on to compile declarations with reverse arguments
29: Variable legacy legacy off
30: \ turn legacy on to compile bigFORTH legacy libraries
1.2 pazsan 31:
32: Vocabulary c-decl
33: Vocabulary cb-decl
1.1 pazsan 34:
35: : @lib ( lib -- )
36: \G obtains library handle
37: cell+ dup 2 cells + count open-lib
38: dup 0= abort" Library not found" swap ! ;
39:
40: : @proc ( lib addr -- )
41: \G obtains symbol address
42: cell+ tuck cell+ @ count rot cell+ @
43: lib-sym dup 0= abort" Proc not found!" swap ! ;
44:
45: : proc, ( lib -- )
46: \G allocates and initializes proc stub
47: \G stub format:
48: \G linked list in library
49: \G address of proc
50: \G ptr to OS name of symbol as counted string
51: \G threaded code for invocation
52: here dup thisproc !
53: swap 2 cells + dup @ A, !
54: 0 , 0 A, ;
55:
1.4 pazsan 56: Defer legacy-proc ' noop IS legacy-proc
57:
1.1 pazsan 58: : proc: ( lib "name" -- )
1.4 pazsan 59: \G Creates a named proc stub
1.2 pazsan 60: Create proc, 0 also c-decl
1.4 pazsan 61: legacy @ IF legacy-proc THEN
1.1 pazsan 62: DOES> ( x1 .. xn -- r )
63: dup cell+ @ swap 3 cells + >r ;
64:
1.11 pazsan 65: Variable ind-call ind-call off
1.12 pazsan 66: : fptr ( "name" -- )
1.11 pazsan 67: Create here thisproc ! 0 , 0 , 0 , 0 also c-decl ind-call on
68: DOES> 3 cells + >r ;
69:
1.1 pazsan 70: : library ( "name" "file" -- )
1.4 pazsan 71: \G loads library "file" and creates a proc defining word "name"
72: \G library format:
73: \G linked list of libraries
74: \G library handle
75: \G linked list of library's procs
76: \G OS name of library as counted string
1.1 pazsan 77: Create here libs @ A, dup libs !
1.6 pazsan 78: 0 , 0 A, parse-name string, @lib
1.1 pazsan 79: DOES> ( -- ) dup thislib ! proc: ;
80:
81: : init-shared-libs ( -- )
1.16 anton 82: defers 'cold
83: 0 libs BEGIN
84: @ dup WHILE
85: dup REPEAT
86: drop BEGIN
87: dup WHILE
88: >r
89: r@ @lib
90: r@ 2 cells + BEGIN
91: @ dup WHILE
92: r@ over @proc REPEAT
93: drop rdrop
94: REPEAT
95: drop ;
1.1 pazsan 96:
97: ' init-shared-libs IS 'cold
98:
1.4 pazsan 99: : argtype ( revxt pushxt fwxt "name" -- )
100: Create , , , ;
101:
102: : arg@ ( arg -- argxt pushxt )
103: revarg @ IF 2 cells + @ ['] noop swap ELSE 2@ THEN ;
104:
105: : arg, ( xt -- )
106: dup ['] noop = IF drop EXIT THEN compile, ;
107:
108: : decl, ( 0 arg1 .. argn call start -- )
1.1 pazsan 109: 2@ compile, >r
1.2 pazsan 110: revdec @ IF 0 >r
1.4 pazsan 111: BEGIN dup WHILE >r REPEAT
112: BEGIN r> dup WHILE arg@ arg, REPEAT drop
113: BEGIN dup WHILE arg, REPEAT drop
114: ELSE 0 >r
115: BEGIN dup WHILE arg@ arg, >r REPEAT drop
116: BEGIN r> dup WHILE arg, REPEAT drop
1.1 pazsan 117: THEN
1.4 pazsan 118: r> compile, postpone EXIT ;
119:
120: : symbol, ( "c-symbol" -- )
1.6 pazsan 121: here thisproc @ 2 cells + ! parse-name s,
1.4 pazsan 122: thislib @ thisproc @ @proc ;
123:
124: : rettype ( endxt startxt "name" -- )
125: Create 2,
1.11 pazsan 126: DOES> decl, ind-call @ 0= IF symbol, THEN
127: previous revarg off ind-call off ;
1.2 pazsan 128:
129: also c-decl definitions
130:
1.4 pazsan 131: : <rev> revarg on ;
132:
133: ' av-int ' av-int-r ' >r argtype int
134: ' av-float ' av-float-r ' f>l argtype sf
135: ' av-double ' av-double-r ' f>l argtype df
1.14 pazsan 136: ' av-longlong ' av-longlong-r ' 2>r argtype dlong
1.4 pazsan 137: ' av-ptr ' av-ptr-r ' >r argtype ptr
138:
139: ' av-call-void ' av-start-void rettype (void)
140: ' av-call-int ' av-start-int rettype (int)
141: ' av-call-float ' av-start-float rettype (sf)
142: ' av-call-double ' av-start-double rettype (fp)
1.14 pazsan 143: ' av-call-longlong ' av-start-longlong rettype (dlong)
1.4 pazsan 144: ' av-call-ptr ' av-start-ptr rettype (ptr)
1.1 pazsan 145:
1.10 pazsan 146: : (addr) postpone EXIT drop symbol, previous revarg off ;
1.8 pazsan 147:
1.2 pazsan 148: previous definitions
1.1 pazsan 149:
1.4 pazsan 150: \ legacy support for old library interfaces
151: \ interface to old vararg stuff not implemented yet
1.1 pazsan 152:
1.2 pazsan 153: also c-decl
1.1 pazsan 154:
1.4 pazsan 155: :noname ( n 0 -- 0 int1 .. intn )
156: legacy @ 0< revarg !
157: swap 0 ?DO int LOOP (int)
158: ; IS legacy-proc
159:
1.1 pazsan 160: : (int) ( n -- )
1.4 pazsan 161: >r ' execute r> 0 ?DO int LOOP (int) ;
1.1 pazsan 162: : (void) ( n -- )
1.4 pazsan 163: >r ' execute r> 0 ?DO int LOOP (void) ;
1.1 pazsan 164: : (float) ( n -- )
1.4 pazsan 165: >r ' execute r> 0 ?DO df LOOP (fp) ;
1.2 pazsan 166:
167: previous
168:
169: \ callback stuff
170:
171: Variable callbacks
172: \G link between callbacks
173:
174: : callback ( -- )
175: Create 0 ] postpone >r also cb-decl
176: DOES>
177: Create here >r 0 , callbacks @ A, r@ callbacks !
1.3 pazsan 178: swap postpone Literal postpone call , postpone EXIT
1.2 pazsan 179: r> dup cell+ cell+ alloc-callback swap !
180: DOES> @ ;
181:
182: : callback; ( 0 xt1 .. xtn -- )
183: BEGIN over WHILE compile, REPEAT
184: postpone r> postpone execute compile, drop
185: postpone EXIT postpone [ previous ; immediate
186:
187: : va-ret ( xt xt -- )
188: Create A, A, immediate
189: DOES> 2@ compile, ;
190:
191: : init-callbacks ( -- )
192: defers 'cold callbacks 1 cells -
193: BEGIN cell+ @ dup WHILE dup cell+ cell+ alloc-callback over !
194: REPEAT drop ;
195:
196: ' init-callbacks IS 'cold
197:
198: also cb-decl definitions
199:
200: \ arguments
1.1 pazsan 201:
1.2 pazsan 202: ' va-arg-int Alias int
203: ' va-arg-float Alias sf
204: ' va-arg-double Alias df
1.14 pazsan 205: ' va-arg-longlong Alias dlong
1.2 pazsan 206: ' va-arg-ptr Alias ptr
207:
208: ' va-return-void ' va-start-void va-ret (void)
209: ' va-return-int ' va-start-int va-ret (int)
210: ' va-return-float ' va-start-float va-ret (sf)
211: ' va-return-double ' va-start-double va-ret (fp)
1.14 pazsan 212: ' va-return-longlong ' va-start-longlong va-ret (dlong)
1.2 pazsan 213: ' va-return-ptr ' va-start-ptr va-ret (ptr)
214:
215: previous definitions
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>