[gforth] / gforth / libffi.fs  

gforth: gforth/libffi.fs


1 : pazsan 1.1 \ libffi.fs shared library support package 14aug05py
2 :    
3 : anton 1.33 \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005,2006,2007,2008,2009 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 : pazsan 1.31 : func@ >body cell+ @ ;
235 :     : func' ' func@ ;
236 :     : [func'] postpone ['] postpone func@ ; immediate restrict
237 :    
238 : pazsan 1.1 \ stuff for libffi
239 :    
240 :     \ libffi uses a parameter array for the input
241 :    
242 :     $20 Value maxargs
243 :    
244 :     Create retbuf 2 cells allot
245 :     Create argbuf maxargs 2* cells allot
246 :     Create argptr maxargs 0 [DO] argbuf [I] 2* cells + A, [LOOP]
247 :    
248 :     \ "forward" when revarg is on
249 :    
250 :     \ : >c+ ( char buf -- buf' ) tuck c! cell+ cell+ ;
251 : pazsan 1.8 : >i+ ( n buf -- buf' ) tuck l! cell+ cell+ ;
252 : pazsan 1.1 : >p+ ( addr buf -- buf' ) tuck ! cell+ cell+ ;
253 :     : >d+ ( d buf -- buf' ) dup >r ffi-2! r> cell+ cell+ ;
254 : pazsan 1.12 : >dl+ ( d buf -- buf' ) nip dup >r ! r> cell+ cell+ ;
255 : pazsan 1.1 : >sf+ ( r buf -- buf' ) dup sf! cell+ cell+ ;
256 :     : >df+ ( r buf -- buf' ) dup df! cell+ cell+ ;
257 :    
258 :     \ "backward" when revarg is off
259 :    
260 : pazsan 1.7 : >i- ( n buf -- buf' ) 2 cells - tuck l! ;
261 : pazsan 1.1 : >p- ( addr buf -- buf' ) 2 cells - tuck ! ;
262 :     : >d- ( d buf -- buf' ) 2 cells - dup >r ffi-2! r> ;
263 : pazsan 1.12 : >dl- ( d buf -- buf' ) 2 cells - nip dup >r ! r> ;
264 : pazsan 1.1 : >sf- ( r buf -- buf' ) 2 cells - dup sf! ;
265 :     : >df- ( r buf -- buf' ) 2 cells - dup df! ;
266 :    
267 :     \ return value
268 :    
269 : pazsan 1.7 : i>x ( -- n ) retbuf l@ ;
270 :     : is>x ( -- n ) retbuf sl@ ;
271 : pazsan 1.1 : p>x ( -- addr ) retbuf @ ;
272 : pazsan 1.12 : dl>x ( -- d ) retbuf @ s>d ;
273 : pazsan 1.1 : d>x ( -- d ) retbuf ffi-2@ ;
274 :     : sf>x ( -- r ) retbuf sf@ ;
275 :     : df>x ( -- r ) retbuf df@ ;
276 :    
277 : pazsan 1.32 table constant cifs
278 : pazsan 1.1
279 :     Variable cifbuf $40 allot \ maximum: 64 parameters
280 : pazsan 1.2 : cifreset cifbuf cell+ cifbuf ! ;
281 :     cifreset
282 : pazsan 1.1 Variable args args off
283 :    
284 :     : argtype ( bkxt fwxt type "name" -- )
285 :     Create , , , DOES> 1 args +! ;
286 :    
287 :     : arg@ ( arg -- type pushxt )
288 :     dup @ swap cell+
289 :     revarg @ IF cell+ THEN @ ;
290 :    
291 :     : arg, ( xt -- )
292 :     dup ['] noop = IF drop EXIT THEN compile, ;
293 :    
294 :     : start, ( n -- ) cifbuf cell+ cifbuf !
295 :     revarg @ IF drop 0 ELSE 2* cells THEN argbuf +
296 :     postpone Literal ;
297 :    
298 : pazsan 1.8 Variable ind-call ind-call off
299 : pazsan 1.10 : fptr ind-call on Create here thisproc !
300 : pazsan 1.8 0 , 0 , 0 , 0 also c-decl DOES> cell+ dup cell+ cell+ >r ! ;
301 :    
302 : pazsan 1.1 : ffi-call, ( -- lit-cif )
303 :     postpone drop postpone argptr postpone retbuf
304 :     thisproc @ cell+ postpone literal postpone @
305 :     0 postpone literal here cell -
306 :     postpone ffi-call ;
307 :    
308 :     : cif, ( n -- )
309 :     cifbuf @ c! 1 cifbuf +! ;
310 :    
311 :     : cif@ ( -- addr u )
312 :     cifbuf cell+ cifbuf @ over - ;
313 :    
314 : pazsan 1.6 : create-cif ( rtype -- addr ) cif,
315 : pazsan 1.1 cif@ cifs search-wordlist
316 :     IF execute EXIT THEN
317 :     get-current >r cifs set-current
318 :     cif@ nextname Create here >r
319 : pazsan 1.6 cif@ 1- bounds ?DO I c@ ffi-type , LOOP r>
320 :     r> set-current ;
321 :    
322 :     : make-cif ( rtype -- addr ) create-cif
323 :     cif@ 1- tuck + c@ ffi-type here 0 ffi-size allot
324 :     dup >r ffi-prep-cif throw r> ;
325 : pazsan 1.1
326 :     : decl, ( 0 arg1 .. argn call rtype start -- )
327 : pazsan 1.2 start, { retxt rtype } cifreset
328 : pazsan 1.1 revdec @ IF 0 >r
329 :     BEGIN dup WHILE >r REPEAT
330 :     BEGIN r> dup WHILE arg@ arg, REPEAT
331 :     ffi-call, retxt compile, postpone EXIT
332 :     BEGIN dup WHILE cif, REPEAT drop
333 :     ELSE 0 >r
334 :     BEGIN dup WHILE arg@ arg, >r REPEAT drop
335 :     ffi-call, retxt compile, postpone EXIT
336 :     BEGIN r> dup WHILE cif, REPEAT drop
337 :     THEN rtype make-cif swap ! here thisproc @ 2 cells + ! ;
338 :    
339 :     : rettype ( endxt n "name" -- )
340 :     Create 2,
341 : pazsan 1.8 DOES> 2@ args @ decl, ind-call @ 0= IF symbol, THEN
342 :     previous revarg off args off ind-call off ;
343 : pazsan 1.1
344 : pazsan 1.12 6 1 cells 4 > 2* - Constant _long
345 :    
346 : pazsan 1.1 also c-decl definitions
347 :    
348 :     : <rev> revarg on ;
349 :    
350 :     ' >i+ ' >i- 6 argtype int
351 : pazsan 1.12 ' >p+ ' >p- _long argtype long
352 : pazsan 1.1 ' >p+ ' >p- &12 argtype ptr
353 :     ' >d+ ' >d- 8 argtype llong
354 : pazsan 1.12 ' >dl+ ' >dl- 6 argtype dlong
355 : pazsan 1.1 ' >sf+ ' >sf- 9 argtype sf
356 :     ' >df+ ' >df- &10 argtype df
357 : pazsan 1.27 : ints 0 ?DO int LOOP ;
358 : pazsan 1.1
359 :     ' noop 0 rettype (void)
360 : pazsan 1.6 ' is>x 6 rettype (int)
361 : pazsan 1.11 ' i>x 5 rettype (uint)
362 : pazsan 1.12 ' p>x _long rettype (long)
363 : pazsan 1.1 ' p>x &12 rettype (ptr)
364 :     ' d>x 8 rettype (llong)
365 : pazsan 1.12 ' dl>x 6 rettype (dlong)
366 : pazsan 1.1 ' sf>x 9 rettype (sf)
367 :     ' df>x &10 rettype (fp)
368 :    
369 : pazsan 1.3 : (addr) thisproc @ cell+ postpone Literal postpone @ postpone EXIT
370 : pazsan 1.4 drop symbol, previous revarg off args off ;
371 : pazsan 1.3
372 : pazsan 1.1 previous definitions
373 :    
374 :     \ legacy support for old library interfaces
375 :     \ interface to old vararg stuff not implemented yet
376 :    
377 :     also c-decl
378 :    
379 :     :noname ( n 0 -- 0 int1 .. intn )
380 :     legacy @ 0< revarg !
381 :     swap 0 ?DO int LOOP (int)
382 :     ; IS legacy-proc
383 :    
384 :     : (int) ( n -- )
385 :     >r ' execute r> 0 ?DO int LOOP (int) ;
386 :     : (void) ( n -- )
387 :     >r ' execute r> 0 ?DO int LOOP (void) ;
388 :     : (float) ( n -- )
389 :     >r ' execute r> 0 ?DO df LOOP (fp) ;
390 :    
391 :     previous
392 :    
393 :     \ callback stuff
394 :    
395 :     Variable callbacks
396 :     \G link between callbacks
397 :    
398 : pazsan 1.2 Variable rtype
399 :    
400 : pazsan 1.8 : alloc-callback ( ip -- addr )
401 :     rtype @ make-cif here 1 ffi-size allot
402 :     dup >r ffi-prep-closure throw r> ;
403 : pazsan 1.2
404 : pazsan 1.1 : callback ( -- )
405 : pazsan 1.2 Create 0 ] postpone >r also cb-decl cifreset
406 : pazsan 1.1 DOES>
407 : pazsan 1.8 0 Value -1 cells allot
408 :     here >r 0 , callbacks @ A, r@ callbacks !
409 : pazsan 1.1 swap postpone Literal postpone call , postpone EXIT
410 : pazsan 1.8 r@ cell+ cell+ alloc-callback r> ! ;
411 : pazsan 1.1
412 : anton 1.16 \ !! is the stack effect right? or is it ( 0 ret arg1 .. argn -- ) ?
413 : pazsan 1.2 : callback; ( 0 arg1 .. argn -- )
414 : pazsan 1.1 BEGIN over WHILE compile, REPEAT
415 :     postpone r> postpone execute compile, drop
416 : anton 1.16 \ !! should we put ]] 0 (bye) [[ here?
417 :     \ !! is the EXIT ever executed?
418 : pazsan 1.1 postpone EXIT postpone [ previous ; immediate
419 :    
420 : pazsan 1.2 : rettype' ( xt n -- )
421 :     Create , A, immediate
422 :     DOES> 2@ rtype ! ;
423 :     : argtype' ( xt n -- )
424 :     Create , A, immediate
425 :     DOES> 2@ cif, ;
426 : pazsan 1.1
427 :     : init-callbacks ( -- )
428 :     defers 'cold callbacks cell -
429 :     BEGIN cell+ @ dup WHILE dup cell+ cell+ alloc-callback over !
430 :     REPEAT drop ;
431 :    
432 :     ' init-callbacks IS 'cold
433 :    
434 :     also cb-decl definitions
435 :    
436 :     \ arguments
437 :    
438 : pazsan 1.2 ' ffi-arg-int 6 argtype' int
439 :     ' ffi-arg-float 9 argtype' sf
440 :     ' ffi-arg-double &10 argtype' df
441 : pazsan 1.12 ' ffi-arg-long _long argtype' long
442 : pazsan 1.2 ' ffi-arg-longlong 8 argtype' llong
443 : pazsan 1.12 ' ffi-arg-dlong 6 argtype' dlong
444 : pazsan 1.2 ' ffi-arg-ptr &12 argtype' ptr
445 : pazsan 1.26 : ints ( n -- ) 0 ?DO postpone int LOOP ; immediate
446 : pazsan 1.2
447 :     ' ffi-ret-void 0 rettype' (void)
448 :     ' ffi-ret-int 6 rettype' (int)
449 :     ' ffi-ret-float 9 rettype' (sf)
450 :     ' ffi-ret-double &10 rettype' (fp)
451 :     ' ffi-ret-longlong 8 rettype' (llong)
452 : pazsan 1.12 ' ffi-ret-long _long rettype' (long)
453 :     ' ffi-ret-dlong _long rettype' (dlong)
454 : pazsan 1.2 ' ffi-ret-ptr &12 rettype' (ptr)
455 : pazsan 1.1
456 :     previous definitions
457 :    

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help