[gforth] / gforth / libffi.fs  

gforth: gforth/libffi.fs


1 : pazsan 1.1 \ libffi.fs shared library support package 14aug05py
2 :    
3 : anton 1.15 \ 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.14 \ 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.14 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : pazsan 1.1
20 : anton 1.16 \ replacements for former primitives
21 :     \ note that the API functions have their arguments reversed and other
22 :     \ deviations.
23 :    
24 :     require libcc.fs
25 :    
26 : anton 1.20 clear-libs s" ffi" add-lib
27 : anton 1.18
28 : anton 1.16 \c #include <ffi.h>
29 :     \c static Cell *gforth_RP;
30 :     \c static char *gforth_LP;
31 :     \c static void **gforth_clist;
32 :     \c static void *gforth_ritem;
33 :     \c typedef void *Label;
34 :     \c typedef Label *Xt;
35 :     \c static void gforth_callback_ffi(ffi_cif * cif, void * resp, void ** args, void * ip)
36 :     \c {
37 :     \c Cell *rp1 = gforth_RP;
38 :     \c Cell *sp = gforth_SP;
39 :     \c Float *fp = gforth_FP;
40 :     \c char *lp = gforth_LP;
41 :     \c void ** clist = gforth_clist;
42 :     \c void * ritem = gforth_ritem;
43 :     \c
44 :     \c gforth_clist = args;
45 :     \c gforth_ritem = resp;
46 :     \c
47 :     \c gforth_engine((Xt *)ip, sp, rp1, fp, lp);
48 :     \c
49 :     \c /* restore global variables */
50 :     \c gforth_RP = rp1;
51 :     \c gforth_SP = sp;
52 :     \c gforth_FP = fp;
53 :     \c gforth_LP = lp;
54 :     \c gforth_clist = clist;
55 :     \c gforth_ritem = ritem;
56 :     \c }
57 :    
58 :     \c static void* ffi_types[] =
59 :     \c { &ffi_type_void,
60 :     \c &ffi_type_uint8, &ffi_type_sint8,
61 :     \c &ffi_type_uint16, &ffi_type_sint16,
62 :     \c &ffi_type_uint32, &ffi_type_sint32,
63 :     \c &ffi_type_uint64, &ffi_type_sint64,
64 :     \c &ffi_type_float, &ffi_type_double, &ffi_type_longdouble,
65 :     \c &ffi_type_pointer };
66 :     \c #define ffi_type(n) (ffi_types[n])
67 :     c-function ffi-type ffi_type n -- a
68 :    
69 :     \c static int ffi_sizes[] = { sizeof(ffi_cif), sizeof(ffi_closure) };
70 :     \c #define ffi_size(n1) (ffi_sizes[n1])
71 :     c-function ffi-size ffi_size n -- n
72 :    
73 :     \c #define ffi_prep_cif1(atypes, n, rtype, cif) \
74 :     \c ffi_prep_cif((ffi_cif *)cif, FFI_DEFAULT_ABI, n, \
75 :     \c (ffi_type *)rtype, (ffi_type **)atypes)
76 :     c-function ffi-prep-cif ffi_prep_cif1 a n a a -- n
77 :    
78 :     \c #define ffi_call1(a_avalues, a_rvalue ,a_ip ,a_cif) \
79 :     \c ffi_call((ffi_cif *)a_cif, (void(*)())a_ip, \
80 :     \c (void *)a_rvalue, (void **)a_avalues)
81 :     c-function ffi-call ffi_call1 a a a a -- void
82 :    
83 : anton 1.17 \c #define ffi_prep_closure1(a_ip, a_cif, a_closure) \
84 : anton 1.16 \c ffi_prep_closure((ffi_closure *)a_closure, (ffi_cif *)a_cif, gforth_callback_ffi, (void *)a_ip)
85 :     c-function ffi-prep-closure a a a -- n
86 :    
87 :     \ !! use ud?
88 :     \c #define ffi_2fetch(a_addr) (*(long long *)a_addr)
89 :     c-function ffi-2@ ffi_2fetch a -- d
90 :    
91 :     \c #define ffi_2store(d,a_addr) ((*(long long *)a_addr) = (long long)d)
92 :     c-function ffi-2! ffi_2store d a -- void
93 :    
94 :     \c #define ffi_arg_int() (*(int *)(*gforth_clist++))
95 :     c-function ffi-arg-int ffi_arg_int -- n
96 :    
97 :     \c #define ffi_arg_long() (*(long *)(*gforth_clist++))
98 :     c-function ffi-arg-long ffi_arg_long -- n
99 :    
100 :     \c #define ffi_arg_longlong() (*(long long *)(*gforth_clist++))
101 :     c-function ffi-arg-longlong ffi_arg_longlong -- d
102 :    
103 :     \ !! correct? The primitive is different, but looks funny
104 :     c-function ffi-arg-dlong ffi_arg_long -- d
105 :    
106 :     \c #define ffi_arg_ptr() (*(char **)(*gforth_clist++))
107 :     c-function ffi-arg-ptr ffi_arg_ptr -- a
108 :    
109 :     \c #define ffi_arg_float() (*(float *)(*gforth_clist++))
110 :     c-function ffi-arg-float ffi_arg_float -- r
111 :    
112 :     \c #define ffi_arg_double() (*(double *)(*gforth_clist++))
113 :     c-function ffi-arg-double ffi_arg_double -- r
114 :    
115 :     : ffi-ret-void ( -- )
116 :     0 (bye) ;
117 :    
118 :     \c #define ffi_ret_int1(w) (*(int*)(gforth_ritem) = w)
119 :     c-function ffi-ret-int1 ffi_ret_int1 n -- void
120 :     : ffi-ret-int ( w -- ) ffi-ret-int1 ffi-ret-void ;
121 :    
122 :     \c #define ffi_ret_longlong1(d) (*(long long *)(gforth_ritem) = d)
123 :     c-function ffi-ret-longlong1 ffi_ret_longlong1 d -- void
124 :     : ffi-ret-longlong ( d -- ) ffi-ret-longlong1 ffi-ret-void ;
125 :    
126 :     \c #define ffi_ret_dlong1(d) (*(long *)(gforth_ritem) = d)
127 :     c-function ffi-ret-dlong1 ffi_ret_dlong1 d -- void
128 :     : ffi-ret-dlong ( d -- ) ffi-ret-dlong1 ffi-ret-void ;
129 :    
130 :     c-function ffi-ret-long1 ffi_ret_dlong1 n -- void
131 :     : ffi-ret-long ( n -- ) ffi-ret-long1 ffi-ret-void ;
132 :    
133 :     \c #define ffi_ret_ptr1(w) (*(char **)(gforth_ritem) = w)
134 :     c-function ffi-ret-ptr1 ffi_ret_ptr1 a -- void
135 :     : ffi-ret-ptr ( a -- ) ffi-ret-ptr1 ffi-ret-void ;
136 :    
137 :     \c #define ffi_ret_float1(r) (*(float *)(gforth_ritem) = r)
138 :     c-function ffi-ret-float1 ffi_ret_float1 r -- void
139 :     : ffi-ret-float ( r -- ) ffi-ret-float1 ffi-ret-void ;
140 :    
141 :     \c #define ffi_ret_double1(r) (*(double *)(gforth_ritem) = r)
142 :     c-function ffi-ret-double1 ffi_ret_double1 r -- void
143 :     : ffi-ret-double ( r -- ) ffi-ret-double1 ffi-ret-void ;
144 :    
145 : pazsan 1.1 \ common stuff, same as fflib.fs
146 :    
147 :     Variable libs 0 libs !
148 :     \ links between libraries
149 :     Variable thisproc
150 :     Variable thislib
151 :    
152 :     Variable revdec revdec off
153 :     \ turn revdec on to compile bigFORTH libraries
154 :     Variable revarg revarg off
155 :     \ turn revarg on to compile declarations with reverse arguments
156 :     Variable legacy legacy off
157 :     \ turn legacy on to compile bigFORTH legacy libraries
158 :    
159 :     Vocabulary c-decl
160 :     Vocabulary cb-decl
161 :    
162 :     : @lib ( lib -- )
163 :     \G obtains library handle
164 :     cell+ dup 2 cells + count open-lib
165 :     dup 0= abort" Library not found" swap ! ;
166 :    
167 :     : @proc ( lib addr -- )
168 :     \G obtains symbol address
169 :     cell+ tuck cell+ @ count rot cell+ @
170 :     lib-sym dup 0= abort" Proc not found!" swap ! ;
171 :    
172 :     : proc, ( lib -- )
173 :     \G allocates and initializes proc stub
174 :     \G stub format:
175 :     \G linked list in library
176 :     \G address of proc
177 :     \G ptr to OS name of symbol as counted string
178 :     \G threaded code for invocation
179 :     here dup thisproc !
180 :     swap 2 cells + dup @ A, !
181 :     0 , 0 A, ;
182 :    
183 :     Defer legacy-proc ' noop IS legacy-proc
184 :    
185 :     : proc: ( lib "name" -- )
186 :     \G Creates a named proc stub
187 :     Create proc, 0 also c-decl
188 :     legacy @ IF legacy-proc THEN
189 :     DOES> ( x1 .. xn -- r )
190 :     3 cells + >r ;
191 :    
192 :     : library ( "name" "file" -- )
193 :     \G loads library "file" and creates a proc defining word "name"
194 :     \G library format:
195 :     \G linked list of libraries
196 :     \G library handle
197 :     \G linked list of library's procs
198 :     \G OS name of library as counted string
199 :     Create here libs @ A, dup libs !
200 :     0 , 0 A, parse-name string, @lib
201 :     DOES> ( -- ) dup thislib ! proc: ;
202 :    
203 :     : init-shared-libs ( -- )
204 :     defers 'cold libs
205 :     0 libs BEGIN @ dup WHILE dup REPEAT drop
206 :     BEGIN dup WHILE >r
207 :     r@ @lib
208 :     r@ 2 cells + BEGIN @ dup WHILE r@ over @proc REPEAT
209 :     drop rdrop
210 :     REPEAT drop ;
211 :    
212 :     ' init-shared-libs IS 'cold
213 :    
214 :     : symbol, ( "c-symbol" -- )
215 :     here thisproc @ 2 cells + ! parse-name s,
216 :     thislib @ thisproc @ @proc ;
217 :    
218 :     \ stuff for libffi
219 :    
220 :     \ libffi uses a parameter array for the input
221 :    
222 :     $20 Value maxargs
223 :    
224 :     Create retbuf 2 cells allot
225 :     Create argbuf maxargs 2* cells allot
226 :     Create argptr maxargs 0 [DO] argbuf [I] 2* cells + A, [LOOP]
227 :    
228 :     \ "forward" when revarg is on
229 :    
230 :     \ : >c+ ( char buf -- buf' ) tuck c! cell+ cell+ ;
231 : pazsan 1.8 : >i+ ( n buf -- buf' ) tuck l! cell+ cell+ ;
232 : pazsan 1.1 : >p+ ( addr buf -- buf' ) tuck ! cell+ cell+ ;
233 :     : >d+ ( d buf -- buf' ) dup >r ffi-2! r> cell+ cell+ ;
234 : pazsan 1.12 : >dl+ ( d buf -- buf' ) nip dup >r ! r> cell+ cell+ ;
235 : pazsan 1.1 : >sf+ ( r buf -- buf' ) dup sf! cell+ cell+ ;
236 :     : >df+ ( r buf -- buf' ) dup df! cell+ cell+ ;
237 :    
238 :     \ "backward" when revarg is off
239 :    
240 : pazsan 1.7 : >i- ( n buf -- buf' ) 2 cells - tuck l! ;
241 : pazsan 1.1 : >p- ( addr buf -- buf' ) 2 cells - tuck ! ;
242 :     : >d- ( d buf -- buf' ) 2 cells - dup >r ffi-2! r> ;
243 : pazsan 1.12 : >dl- ( d buf -- buf' ) 2 cells - nip dup >r ! r> ;
244 : pazsan 1.1 : >sf- ( r buf -- buf' ) 2 cells - dup sf! ;
245 :     : >df- ( r buf -- buf' ) 2 cells - dup df! ;
246 :    
247 :     \ return value
248 :    
249 : pazsan 1.7 : i>x ( -- n ) retbuf l@ ;
250 :     : is>x ( -- n ) retbuf sl@ ;
251 : pazsan 1.1 : p>x ( -- addr ) retbuf @ ;
252 : pazsan 1.12 : dl>x ( -- d ) retbuf @ s>d ;
253 : pazsan 1.1 : d>x ( -- d ) retbuf ffi-2@ ;
254 :     : sf>x ( -- r ) retbuf sf@ ;
255 :     : df>x ( -- r ) retbuf df@ ;
256 :    
257 :     wordlist constant cifs
258 :    
259 :     Variable cifbuf $40 allot \ maximum: 64 parameters
260 : pazsan 1.2 : cifreset cifbuf cell+ cifbuf ! ;
261 :     cifreset
262 : pazsan 1.1 Variable args args off
263 :    
264 :     : argtype ( bkxt fwxt type "name" -- )
265 :     Create , , , DOES> 1 args +! ;
266 :    
267 :     : arg@ ( arg -- type pushxt )
268 :     dup @ swap cell+
269 :     revarg @ IF cell+ THEN @ ;
270 :    
271 :     : arg, ( xt -- )
272 :     dup ['] noop = IF drop EXIT THEN compile, ;
273 :    
274 :     : start, ( n -- ) cifbuf cell+ cifbuf !
275 :     revarg @ IF drop 0 ELSE 2* cells THEN argbuf +
276 :     postpone Literal ;
277 :    
278 : pazsan 1.8 Variable ind-call ind-call off
279 : pazsan 1.10 : fptr ind-call on Create here thisproc !
280 : pazsan 1.8 0 , 0 , 0 , 0 also c-decl DOES> cell+ dup cell+ cell+ >r ! ;
281 :    
282 : pazsan 1.1 : ffi-call, ( -- lit-cif )
283 :     postpone drop postpone argptr postpone retbuf
284 :     thisproc @ cell+ postpone literal postpone @
285 :     0 postpone literal here cell -
286 :     postpone ffi-call ;
287 :    
288 :     : cif, ( n -- )
289 :     cifbuf @ c! 1 cifbuf +! ;
290 :    
291 :     : cif@ ( -- addr u )
292 :     cifbuf cell+ cifbuf @ over - ;
293 :    
294 : pazsan 1.6 : create-cif ( rtype -- addr ) cif,
295 : pazsan 1.1 cif@ cifs search-wordlist
296 :     IF execute EXIT THEN
297 :     get-current >r cifs set-current
298 :     cif@ nextname Create here >r
299 : pazsan 1.6 cif@ 1- bounds ?DO I c@ ffi-type , LOOP r>
300 :     r> set-current ;
301 :    
302 :     : make-cif ( rtype -- addr ) create-cif
303 :     cif@ 1- tuck + c@ ffi-type here 0 ffi-size allot
304 :     dup >r ffi-prep-cif throw r> ;
305 : pazsan 1.1
306 :     : decl, ( 0 arg1 .. argn call rtype start -- )
307 : pazsan 1.2 start, { retxt rtype } cifreset
308 : pazsan 1.1 revdec @ IF 0 >r
309 :     BEGIN dup WHILE >r REPEAT
310 :     BEGIN r> dup WHILE arg@ arg, REPEAT
311 :     ffi-call, retxt compile, postpone EXIT
312 :     BEGIN dup WHILE cif, REPEAT drop
313 :     ELSE 0 >r
314 :     BEGIN dup WHILE arg@ arg, >r REPEAT drop
315 :     ffi-call, retxt compile, postpone EXIT
316 :     BEGIN r> dup WHILE cif, REPEAT drop
317 :     THEN rtype make-cif swap ! here thisproc @ 2 cells + ! ;
318 :    
319 :     : rettype ( endxt n "name" -- )
320 :     Create 2,
321 : pazsan 1.8 DOES> 2@ args @ decl, ind-call @ 0= IF symbol, THEN
322 :     previous revarg off args off ind-call off ;
323 : pazsan 1.1
324 : pazsan 1.12 6 1 cells 4 > 2* - Constant _long
325 :    
326 : pazsan 1.1 also c-decl definitions
327 :    
328 :     : <rev> revarg on ;
329 :    
330 :     ' >i+ ' >i- 6 argtype int
331 : pazsan 1.12 ' >p+ ' >p- _long argtype long
332 : pazsan 1.1 ' >p+ ' >p- &12 argtype ptr
333 :     ' >d+ ' >d- 8 argtype llong
334 : pazsan 1.12 ' >dl+ ' >dl- 6 argtype dlong
335 : pazsan 1.1 ' >sf+ ' >sf- 9 argtype sf
336 :     ' >df+ ' >df- &10 argtype df
337 :    
338 :     ' noop 0 rettype (void)
339 : pazsan 1.6 ' is>x 6 rettype (int)
340 : pazsan 1.11 ' i>x 5 rettype (uint)
341 : pazsan 1.12 ' p>x _long rettype (long)
342 : pazsan 1.1 ' p>x &12 rettype (ptr)
343 :     ' d>x 8 rettype (llong)
344 : pazsan 1.12 ' dl>x 6 rettype (dlong)
345 : pazsan 1.1 ' sf>x 9 rettype (sf)
346 :     ' df>x &10 rettype (fp)
347 :    
348 : pazsan 1.3 : (addr) thisproc @ cell+ postpone Literal postpone @ postpone EXIT
349 : pazsan 1.4 drop symbol, previous revarg off args off ;
350 : pazsan 1.3
351 : pazsan 1.1 previous definitions
352 :    
353 :     \ legacy support for old library interfaces
354 :     \ interface to old vararg stuff not implemented yet
355 :    
356 :     also c-decl
357 :    
358 :     :noname ( n 0 -- 0 int1 .. intn )
359 :     legacy @ 0< revarg !
360 :     swap 0 ?DO int LOOP (int)
361 :     ; IS legacy-proc
362 :    
363 :     : (int) ( n -- )
364 :     >r ' execute r> 0 ?DO int LOOP (int) ;
365 :     : (void) ( n -- )
366 :     >r ' execute r> 0 ?DO int LOOP (void) ;
367 :     : (float) ( n -- )
368 :     >r ' execute r> 0 ?DO df LOOP (fp) ;
369 :    
370 :     previous
371 :    
372 :     \ callback stuff
373 :    
374 :     Variable callbacks
375 :     \G link between callbacks
376 :    
377 : pazsan 1.2 Variable rtype
378 :    
379 : pazsan 1.8 : alloc-callback ( ip -- addr )
380 :     rtype @ make-cif here 1 ffi-size allot
381 :     dup >r ffi-prep-closure throw r> ;
382 : pazsan 1.2
383 : pazsan 1.1 : callback ( -- )
384 : pazsan 1.2 Create 0 ] postpone >r also cb-decl cifreset
385 : pazsan 1.1 DOES>
386 : pazsan 1.8 0 Value -1 cells allot
387 :     here >r 0 , callbacks @ A, r@ callbacks !
388 : pazsan 1.1 swap postpone Literal postpone call , postpone EXIT
389 : pazsan 1.8 r@ cell+ cell+ alloc-callback r> ! ;
390 : pazsan 1.1
391 : anton 1.16 \ !! is the stack effect right? or is it ( 0 ret arg1 .. argn -- ) ?
392 : pazsan 1.2 : callback; ( 0 arg1 .. argn -- )
393 : pazsan 1.1 BEGIN over WHILE compile, REPEAT
394 :     postpone r> postpone execute compile, drop
395 : anton 1.16 \ !! should we put ]] 0 (bye) [[ here?
396 :     \ !! is the EXIT ever executed?
397 : pazsan 1.1 postpone EXIT postpone [ previous ; immediate
398 :    
399 : pazsan 1.2 : rettype' ( xt n -- )
400 :     Create , A, immediate
401 :     DOES> 2@ rtype ! ;
402 :     : argtype' ( xt n -- )
403 :     Create , A, immediate
404 :     DOES> 2@ cif, ;
405 : pazsan 1.1
406 :     : init-callbacks ( -- )
407 :     defers 'cold callbacks cell -
408 :     BEGIN cell+ @ dup WHILE dup cell+ cell+ alloc-callback over !
409 :     REPEAT drop ;
410 :    
411 :     ' init-callbacks IS 'cold
412 :    
413 :     also cb-decl definitions
414 :    
415 :     \ arguments
416 :    
417 : pazsan 1.2 ' ffi-arg-int 6 argtype' int
418 :     ' ffi-arg-float 9 argtype' sf
419 :     ' ffi-arg-double &10 argtype' df
420 : pazsan 1.12 ' ffi-arg-long _long argtype' long
421 : pazsan 1.2 ' ffi-arg-longlong 8 argtype' llong
422 : pazsan 1.12 ' ffi-arg-dlong 6 argtype' dlong
423 : pazsan 1.2 ' ffi-arg-ptr &12 argtype' ptr
424 :    
425 :     ' ffi-ret-void 0 rettype' (void)
426 :     ' ffi-ret-int 6 rettype' (int)
427 :     ' ffi-ret-float 9 rettype' (sf)
428 :     ' ffi-ret-double &10 rettype' (fp)
429 :     ' ffi-ret-longlong 8 rettype' (llong)
430 : pazsan 1.12 ' ffi-ret-long _long rettype' (long)
431 :     ' ffi-ret-dlong _long rettype' (dlong)
432 : pazsan 1.2 ' ffi-ret-ptr &12 rettype' (ptr)
433 : pazsan 1.1
434 :     previous definitions
435 :    

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help