[gforth] / gforth / libffi.fs  

gforth: gforth/libffi.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help