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