[gforth] / gforth / fflib.fs  

gforth: gforth/fflib.fs


1 : pazsan 1.2 \ lib.fs shared library support package 16aug03py
2 : pazsan 1.1
3 : anton 1.17 \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005,2006,2007 Free Software Foundation, Inc.
4 : pazsan 1.1
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 : anton 1.18 \ as published by the Free Software Foundation, either version 3
10 : pazsan 1.1 \ 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 : anton 1.18 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : pazsan 1.1
20 : anton 1.19 \ replacements for former primitives
21 :     require libcc.fs
22 :    
23 : anton 1.20 clear-libs
24 :     s" avcall" add-lib
25 :     s" callback" add-lib
26 :    
27 : anton 1.19 \c #include <avcall.h>
28 :     \c #include <callback.h>
29 :     \c static av_alist alist;
30 :     \c static va_alist gforth_clist;
31 :     \c static float frv;
32 :     \c static int irv;
33 :     \c static double drv;
34 :     \c static long long llrv;
35 :     \c static void * prv;
36 :     \c static Cell *gforth_RP;
37 :     \c static char *gforth_LP;
38 :     \c typedef void *Label;
39 :     \c typedef Label *Xt;
40 :     \c Label *gforth_engine(Xt *ip, Cell *sp, Cell *rp0, Float *fp, char *lp);
41 :     \c
42 :     \c void gforth_callback_ffcall(Xt* fcall, void * alist)
43 :     \c {
44 :     \c /* save global valiables */
45 :     \c Cell *rp = gforth_RP;
46 :     \c Cell *sp = gforth_SP;
47 :     \c Float *fp = gforth_FP;
48 :     \c char *lp = gforth_LP;
49 :     \c va_alist clist = gforth_clist;
50 :     \c
51 :     \c gforth_clist = (va_alist)alist;
52 :     \c
53 :     \c gforth_engine(fcall, sp, rp, fp, lp);
54 :     \c
55 :     \c /* restore global variables */
56 :     \c gforth_RP = rp;
57 :     \c gforth_SP = sp;
58 :     \c gforth_FP = fp;
59 :     \c gforth_LP = lp;
60 :     \c gforth_clist = clist;
61 :     \c }
62 :    
63 :     \c #define av_start_void1(c_addr) av_start_void(alist, c_addr)
64 :     c-function av-start-void av_start_void1 a -- void
65 :     \c #define av_start_int1(c_addr) av_start_int(alist, c_addr, &irv)
66 :     c-function av-start-int av_start_int1 a -- void
67 :     \c #define av_start_float1(c_addr) av_start_float(alist, c_addr, &frv)
68 :     c-function av-start-float av_start_float1 a -- void
69 :     \c #define av_start_double1(c_addr) av_start_double(alist, c_addr, &drv)
70 :     c-function av-start-double av_start_double1 a -- void
71 :     \c #define av_start_longlong1(c_addr) av_start_longlong(alist, c_addr, &llrv)
72 :     c-function av-start-longlong av_start_longlong1 a -- void
73 :     \c #define av_start_ptr1(c_addr) av_start_ptr(alist, c_addr, void *, &prv)
74 :     c-function av-start-ptr av_start_ptr1 a -- void
75 :     \c #define av_int1(w) av_int(alist,w)
76 :     c-function av-int av_int1 n -- void
77 :     \c #define av_float1(r) av_float(alist,r)
78 :     c-function av-float av_float1 r -- void
79 :     \c #define av_double1(r) av_double(alist,r)
80 :     c-function av-double av_double1 r -- void
81 :     \c #define av_longlong1(d) av_longlong(alist,d)
82 :     c-function av-longlong av_longlong1 d -- void
83 :     \c #define av_ptr1(a) av_ptr(alist, void *, a)
84 :     c-function av-ptr av_ptr1 a -- void
85 :     \c #define av_call_void() av_call(alist)
86 :     c-function av-call-void av_call_void -- void
87 :     \c #define av_call_int() (av_call(alist), irv)
88 :     c-function av-call-int av_call_int -- n
89 :     \c #define av_call_float() (av_call(alist), frv)
90 :     c-function av-call-float av_call_float -- r
91 :     \c #define av_call_double() (av_call(alist), drv)
92 :     c-function av-call-double av_call_double -- r
93 :     \c #define av_call_longlong() (av_call(alist), llrv)
94 :     c-function av-call-longlong av_call_longlong -- d
95 :     \c #define av_call_ptr() (av_call(alist), prv)
96 :     c-function av-call-ptr av_call_ptr -- a
97 :     \c #define alloc_callback1(a_ip) alloc_callback(gforth_callback_ffcall, (Xt *)a_ip)
98 :     c-function alloc-callback alloc_callback1 a -- a
99 :     \c #define va_start_void1() va_start_void(gforth_clist)
100 :     c-function va-start-void va_start_void1 -- void
101 :     \c #define va_start_int1() va_start_int(gforth_clist)
102 :     c-function va-start-int va_start_int1 -- void
103 :     \c #define va_start_longlong1() va_start_longlong(gforth_clist)
104 :     c-function va-start-longlong va_start_longlong1 -- void
105 :     \c #define va_start_ptr1() va_start_ptr(gforth_clist, (char *))
106 :     c-function va-start-ptr va_start_ptr1 -- void
107 :     \c #define va_start_float1() va_start_float(gforth_clist)
108 :     c-function va-start-float va_start_float1 -- void
109 :     \c #define va_start_double1() va_start_double(gforth_clist)
110 :     c-function va-start-double va_start_double1 -- void
111 :     \c #define va_arg_int1() va_arg_int(gforth_clist)
112 :     c-function va-arg-int va_arg_int1 -- n
113 :     \c #define va_arg_longlong1() va_arg_longlong(gforth_clist)
114 :     c-function va-arg-longlong va_arg_longlong1 -- d
115 :     \c #define va_arg_ptr1() va_arg_ptr(gforth_clist, char *)
116 :     c-function va-arg-ptr va_arg_ptr1 -- a
117 :     \c #define va_arg_float1() va_arg_float(gforth_clist)
118 :     c-function va-arg-float va_arg_float1 -- r
119 :     \c #define va_arg_double1() va_arg_double(gforth_clist)
120 :     c-function va-arg-double va_arg_double1 -- r
121 :     \c #define va_return_void1() va_return_void(gforth_clist)
122 :     c-function va-return-void1 va_return_void1 -- void
123 :     \c #define va_return_int1(w) va_return_int(gforth_clist,w)
124 :     c-function va-return-int1 va_return_int1 n -- void
125 :     \c #define va_return_ptr1(w) va_return_ptr(gforth_clist, void *, w)
126 :     c-function va-return-ptr1 va_return_ptr1 a -- void
127 :     \c #define va_return_longlong1(d) va_return_longlong(gforth_clist,d)
128 :     c-function va-return-longlong1 va_return_longlong1 d -- void
129 :     \c #define va_return_float1(r) va_return_float(gforth_clist,r)
130 :     c-function va-return-float1 va_return_float1 r -- void
131 :     \c #define va_return_double1(r) va_return_double(gforth_clist,r)
132 :     c-function va-return-double1 va_return_double1 r -- void
133 :    
134 :     : av-int-r 2r> >r av-int ;
135 :     : av-float-r f@local0 lp+ av-float ;
136 :     : av-double-r f@local0 lp+ av-double ;
137 :     : av-longlong-r r> 2r> rot >r av-longlong ;
138 :     : av-ptr-r 2r> >r av-ptr ;
139 :     : va-return-void va-return-void1 0 (bye) ;
140 :     : va-return-int va-return-int1 0 (bye) ;
141 :     : va-return-ptr va-return-ptr1 0 (bye) ;
142 :     : va-return-longlong va-return-longlong1 0 (bye) ;
143 :     : va-return-float va-return-float1 0 (bye) ;
144 :     : va-return-double va-return-double1 0 (bye) ;
145 :    
146 :     \ start of fflib proper
147 :    
148 : pazsan 1.1 Variable libs 0 libs !
149 : pazsan 1.4 \ links between libraries
150 : pazsan 1.1 Variable thisproc
151 :     Variable thislib
152 : pazsan 1.4
153 : pazsan 1.2 Variable revdec revdec off
154 :     \ turn revdec on to compile bigFORTH libraries
155 : pazsan 1.4 Variable revarg revarg off
156 :     \ turn revarg on to compile declarations with reverse arguments
157 :     Variable legacy legacy off
158 :     \ turn legacy on to compile bigFORTH legacy libraries
159 : pazsan 1.2
160 :     Vocabulary c-decl
161 :     Vocabulary cb-decl
162 : pazsan 1.1
163 :     : @lib ( lib -- )
164 :     \G obtains library handle
165 :     cell+ dup 2 cells + count open-lib
166 :     dup 0= abort" Library not found" swap ! ;
167 :    
168 :     : @proc ( lib addr -- )
169 :     \G obtains symbol address
170 :     cell+ tuck cell+ @ count rot cell+ @
171 :     lib-sym dup 0= abort" Proc not found!" swap ! ;
172 :    
173 :     : proc, ( lib -- )
174 :     \G allocates and initializes proc stub
175 :     \G stub format:
176 :     \G linked list in library
177 :     \G address of proc
178 :     \G ptr to OS name of symbol as counted string
179 :     \G threaded code for invocation
180 :     here dup thisproc !
181 :     swap 2 cells + dup @ A, !
182 :     0 , 0 A, ;
183 :    
184 : pazsan 1.4 Defer legacy-proc ' noop IS legacy-proc
185 :    
186 : pazsan 1.1 : proc: ( lib "name" -- )
187 : pazsan 1.4 \G Creates a named proc stub
188 : pazsan 1.2 Create proc, 0 also c-decl
189 : pazsan 1.4 legacy @ IF legacy-proc THEN
190 : pazsan 1.1 DOES> ( x1 .. xn -- r )
191 :     dup cell+ @ swap 3 cells + >r ;
192 :    
193 : pazsan 1.11 Variable ind-call ind-call off
194 : pazsan 1.12 : fptr ( "name" -- )
195 : pazsan 1.11 Create here thisproc ! 0 , 0 , 0 , 0 also c-decl ind-call on
196 :     DOES> 3 cells + >r ;
197 :    
198 : pazsan 1.1 : library ( "name" "file" -- )
199 : pazsan 1.4 \G loads library "file" and creates a proc defining word "name"
200 :     \G library format:
201 :     \G linked list of libraries
202 :     \G library handle
203 :     \G linked list of library's procs
204 :     \G OS name of library as counted string
205 : pazsan 1.1 Create here libs @ A, dup libs !
206 : pazsan 1.6 0 , 0 A, parse-name string, @lib
207 : pazsan 1.1 DOES> ( -- ) dup thislib ! proc: ;
208 :    
209 :     : init-shared-libs ( -- )
210 : anton 1.16 defers 'cold
211 :     0 libs BEGIN
212 :     @ dup WHILE
213 :     dup REPEAT
214 :     drop BEGIN
215 :     dup WHILE
216 :     >r
217 :     r@ @lib
218 :     r@ 2 cells + BEGIN
219 :     @ dup WHILE
220 :     r@ over @proc REPEAT
221 :     drop rdrop
222 :     REPEAT
223 :     drop ;
224 : pazsan 1.1
225 :     ' init-shared-libs IS 'cold
226 :    
227 : pazsan 1.4 : argtype ( revxt pushxt fwxt "name" -- )
228 :     Create , , , ;
229 :    
230 :     : arg@ ( arg -- argxt pushxt )
231 :     revarg @ IF 2 cells + @ ['] noop swap ELSE 2@ THEN ;
232 :    
233 :     : arg, ( xt -- )
234 :     dup ['] noop = IF drop EXIT THEN compile, ;
235 :    
236 :     : decl, ( 0 arg1 .. argn call start -- )
237 : pazsan 1.1 2@ compile, >r
238 : pazsan 1.2 revdec @ IF 0 >r
239 : pazsan 1.4 BEGIN dup WHILE >r REPEAT
240 :     BEGIN r> dup WHILE arg@ arg, REPEAT drop
241 :     BEGIN dup WHILE arg, REPEAT drop
242 :     ELSE 0 >r
243 :     BEGIN dup WHILE arg@ arg, >r REPEAT drop
244 :     BEGIN r> dup WHILE arg, REPEAT drop
245 : pazsan 1.1 THEN
246 : pazsan 1.4 r> compile, postpone EXIT ;
247 :    
248 :     : symbol, ( "c-symbol" -- )
249 : pazsan 1.6 here thisproc @ 2 cells + ! parse-name s,
250 : pazsan 1.4 thislib @ thisproc @ @proc ;
251 :    
252 :     : rettype ( endxt startxt "name" -- )
253 :     Create 2,
254 : pazsan 1.11 DOES> decl, ind-call @ 0= IF symbol, THEN
255 :     previous revarg off ind-call off ;
256 : pazsan 1.2
257 :     also c-decl definitions
258 :    
259 : pazsan 1.4 : <rev> revarg on ;
260 :    
261 :     ' av-int ' av-int-r ' >r argtype int
262 :     ' av-float ' av-float-r ' f>l argtype sf
263 :     ' av-double ' av-double-r ' f>l argtype df
264 : pazsan 1.14 ' av-longlong ' av-longlong-r ' 2>r argtype dlong
265 : pazsan 1.4 ' av-ptr ' av-ptr-r ' >r argtype ptr
266 :    
267 :     ' av-call-void ' av-start-void rettype (void)
268 :     ' av-call-int ' av-start-int rettype (int)
269 :     ' av-call-float ' av-start-float rettype (sf)
270 :     ' av-call-double ' av-start-double rettype (fp)
271 : pazsan 1.14 ' av-call-longlong ' av-start-longlong rettype (dlong)
272 : pazsan 1.4 ' av-call-ptr ' av-start-ptr rettype (ptr)
273 : pazsan 1.1
274 : pazsan 1.10 : (addr) postpone EXIT drop symbol, previous revarg off ;
275 : pazsan 1.8
276 : pazsan 1.2 previous definitions
277 : pazsan 1.1
278 : pazsan 1.4 \ legacy support for old library interfaces
279 :     \ interface to old vararg stuff not implemented yet
280 : pazsan 1.1
281 : pazsan 1.2 also c-decl
282 : pazsan 1.1
283 : pazsan 1.4 :noname ( n 0 -- 0 int1 .. intn )
284 :     legacy @ 0< revarg !
285 :     swap 0 ?DO int LOOP (int)
286 :     ; IS legacy-proc
287 :    
288 : pazsan 1.1 : (int) ( n -- )
289 : pazsan 1.4 >r ' execute r> 0 ?DO int LOOP (int) ;
290 : pazsan 1.1 : (void) ( n -- )
291 : pazsan 1.4 >r ' execute r> 0 ?DO int LOOP (void) ;
292 : pazsan 1.1 : (float) ( n -- )
293 : pazsan 1.4 >r ' execute r> 0 ?DO df LOOP (fp) ;
294 : pazsan 1.2
295 :     previous
296 :    
297 :     \ callback stuff
298 :    
299 :     Variable callbacks
300 :     \G link between callbacks
301 :    
302 :     : callback ( -- )
303 :     Create 0 ] postpone >r also cb-decl
304 :     DOES>
305 :     Create here >r 0 , callbacks @ A, r@ callbacks !
306 : pazsan 1.3 swap postpone Literal postpone call , postpone EXIT
307 : pazsan 1.2 r> dup cell+ cell+ alloc-callback swap !
308 :     DOES> @ ;
309 :    
310 :     : callback; ( 0 xt1 .. xtn -- )
311 :     BEGIN over WHILE compile, REPEAT
312 :     postpone r> postpone execute compile, drop
313 :     postpone EXIT postpone [ previous ; immediate
314 :    
315 :     : va-ret ( xt xt -- )
316 :     Create A, A, immediate
317 :     DOES> 2@ compile, ;
318 :    
319 :     : init-callbacks ( -- )
320 :     defers 'cold callbacks 1 cells -
321 :     BEGIN cell+ @ dup WHILE dup cell+ cell+ alloc-callback over !
322 :     REPEAT drop ;
323 :    
324 :     ' init-callbacks IS 'cold
325 :    
326 :     also cb-decl definitions
327 :    
328 :     \ arguments
329 : pazsan 1.1
330 : pazsan 1.2 ' va-arg-int Alias int
331 :     ' va-arg-float Alias sf
332 :     ' va-arg-double Alias df
333 : pazsan 1.14 ' va-arg-longlong Alias dlong
334 : pazsan 1.2 ' va-arg-ptr Alias ptr
335 :    
336 :     ' va-return-void ' va-start-void va-ret (void)
337 :     ' va-return-int ' va-start-int va-ret (int)
338 :     ' va-return-float ' va-start-float va-ret (sf)
339 :     ' va-return-double ' va-start-double va-ret (fp)
340 : pazsan 1.14 ' va-return-longlong ' va-start-longlong va-ret (dlong)
341 : pazsan 1.2 ' va-return-ptr ' va-start-ptr va-ret (ptr)
342 :    
343 :     previous definitions
344 : anton 1.20 clear-libs

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help