Annotation of gforth/libffi.fs, revision 1.30
1.1 pazsan 1: \ libffi.fs shared library support package 14aug05py
2:
1.25 anton 3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005,2006,2007,2008 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:
1.22 anton 24: c-library libffi
25: s" ffi" add-lib
1.18 anton 26:
1.29 anton 27: \ The ffi.h of XCode needs the following line, and it should not hurt elsewhere
28: \c #define MACOSX
1.28 anton 29: include-ffi.h-string save-c-prefix-line \ #include <ffi.h>
1.30 ! pazsan 30: \c #include <stdio.h>
1.16 anton 31: \c static void **gforth_clist;
32: \c static void *gforth_ritem;
1.30 ! pazsan 33: \c #ifndef HAS_BACKLINK
! 34: \c static void **saved_gforth_pointers;
! 35: \c #endif
1.16 anton 36: \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 {
1.30 ! pazsan 40: \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);
1.16 anton 55: \c
1.30 ! pazsan 56: \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 }
1.16 anton 64: \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:
1.30 ! pazsan 86: \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
1.16 anton 91: \c #define ffi_call1(a_avalues, a_rvalue ,a_ip ,a_cif) \
1.30 ! pazsan 92: \c (saved_gforth_pointers = gforth_pointers), \
1.16 anton 93: \c ffi_call((ffi_cif *)a_cif, (void(*)())a_ip, \
94: \c (void *)a_rvalue, (void **)a_avalues)
1.30 ! pazsan 95: \c #endif
1.16 anton 96: c-function ffi-call ffi_call1 a a a a -- void
97:
1.17 anton 98: \c #define ffi_prep_closure1(a_ip, a_cif, a_closure) \
1.16 anton 99: \c ffi_prep_closure((ffi_closure *)a_closure, (ffi_cif *)a_cif, gforth_callback_ffi, (void *)a_ip)
1.21 dbane 100: c-function ffi-prep-closure ffi_prep_closure1 a a a -- n
1.16 anton 101:
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 ;
1.22 anton 159: end-c-library
1.16 anton 160:
1.1 pazsan 161: \ 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+ ;
1.8 pazsan 247: : >i+ ( n buf -- buf' ) tuck l! cell+ cell+ ;
1.1 pazsan 248: : >p+ ( addr buf -- buf' ) tuck ! cell+ cell+ ;
249: : >d+ ( d buf -- buf' ) dup >r ffi-2! r> cell+ cell+ ;
1.12 pazsan 250: : >dl+ ( d buf -- buf' ) nip dup >r ! r> cell+ cell+ ;
1.1 pazsan 251: : >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:
1.7 pazsan 256: : >i- ( n buf -- buf' ) 2 cells - tuck l! ;
1.1 pazsan 257: : >p- ( addr buf -- buf' ) 2 cells - tuck ! ;
258: : >d- ( d buf -- buf' ) 2 cells - dup >r ffi-2! r> ;
1.12 pazsan 259: : >dl- ( d buf -- buf' ) 2 cells - nip dup >r ! r> ;
1.1 pazsan 260: : >sf- ( r buf -- buf' ) 2 cells - dup sf! ;
261: : >df- ( r buf -- buf' ) 2 cells - dup df! ;
262:
263: \ return value
264:
1.7 pazsan 265: : i>x ( -- n ) retbuf l@ ;
266: : is>x ( -- n ) retbuf sl@ ;
1.1 pazsan 267: : p>x ( -- addr ) retbuf @ ;
1.12 pazsan 268: : dl>x ( -- d ) retbuf @ s>d ;
1.1 pazsan 269: : 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
1.2 pazsan 276: : cifreset cifbuf cell+ cifbuf ! ;
277: cifreset
1.1 pazsan 278: 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:
1.8 pazsan 294: Variable ind-call ind-call off
1.10 pazsan 295: : fptr ind-call on Create here thisproc !
1.8 pazsan 296: 0 , 0 , 0 , 0 also c-decl DOES> cell+ dup cell+ cell+ >r ! ;
297:
1.1 pazsan 298: : 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:
1.6 pazsan 310: : create-cif ( rtype -- addr ) cif,
1.1 pazsan 311: cif@ cifs search-wordlist
312: IF execute EXIT THEN
313: get-current >r cifs set-current
314: cif@ nextname Create here >r
1.6 pazsan 315: 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> ;
1.1 pazsan 321:
322: : decl, ( 0 arg1 .. argn call rtype start -- )
1.2 pazsan 323: start, { retxt rtype } cifreset
1.1 pazsan 324: 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,
1.8 pazsan 337: DOES> 2@ args @ decl, ind-call @ 0= IF symbol, THEN
338: previous revarg off args off ind-call off ;
1.1 pazsan 339:
1.12 pazsan 340: 6 1 cells 4 > 2* - Constant _long
341:
1.1 pazsan 342: also c-decl definitions
343:
344: : <rev> revarg on ;
345:
346: ' >i+ ' >i- 6 argtype int
1.12 pazsan 347: ' >p+ ' >p- _long argtype long
1.1 pazsan 348: ' >p+ ' >p- &12 argtype ptr
349: ' >d+ ' >d- 8 argtype llong
1.12 pazsan 350: ' >dl+ ' >dl- 6 argtype dlong
1.1 pazsan 351: ' >sf+ ' >sf- 9 argtype sf
352: ' >df+ ' >df- &10 argtype df
1.27 pazsan 353: : ints 0 ?DO int LOOP ;
1.1 pazsan 354:
355: ' noop 0 rettype (void)
1.6 pazsan 356: ' is>x 6 rettype (int)
1.11 pazsan 357: ' i>x 5 rettype (uint)
1.12 pazsan 358: ' p>x _long rettype (long)
1.1 pazsan 359: ' p>x &12 rettype (ptr)
360: ' d>x 8 rettype (llong)
1.12 pazsan 361: ' dl>x 6 rettype (dlong)
1.1 pazsan 362: ' sf>x 9 rettype (sf)
363: ' df>x &10 rettype (fp)
364:
1.3 pazsan 365: : (addr) thisproc @ cell+ postpone Literal postpone @ postpone EXIT
1.4 pazsan 366: drop symbol, previous revarg off args off ;
1.3 pazsan 367:
1.1 pazsan 368: 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:
1.2 pazsan 394: Variable rtype
395:
1.8 pazsan 396: : alloc-callback ( ip -- addr )
397: rtype @ make-cif here 1 ffi-size allot
398: dup >r ffi-prep-closure throw r> ;
1.2 pazsan 399:
1.1 pazsan 400: : callback ( -- )
1.2 pazsan 401: Create 0 ] postpone >r also cb-decl cifreset
1.1 pazsan 402: DOES>
1.8 pazsan 403: 0 Value -1 cells allot
404: here >r 0 , callbacks @ A, r@ callbacks !
1.1 pazsan 405: swap postpone Literal postpone call , postpone EXIT
1.8 pazsan 406: r@ cell+ cell+ alloc-callback r> ! ;
1.1 pazsan 407:
1.16 anton 408: \ !! is the stack effect right? or is it ( 0 ret arg1 .. argn -- ) ?
1.2 pazsan 409: : callback; ( 0 arg1 .. argn -- )
1.1 pazsan 410: BEGIN over WHILE compile, REPEAT
411: postpone r> postpone execute compile, drop
1.16 anton 412: \ !! should we put ]] 0 (bye) [[ here?
413: \ !! is the EXIT ever executed?
1.1 pazsan 414: postpone EXIT postpone [ previous ; immediate
415:
1.2 pazsan 416: : rettype' ( xt n -- )
417: Create , A, immediate
418: DOES> 2@ rtype ! ;
419: : argtype' ( xt n -- )
420: Create , A, immediate
421: DOES> 2@ cif, ;
1.1 pazsan 422:
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:
1.2 pazsan 434: ' ffi-arg-int 6 argtype' int
435: ' ffi-arg-float 9 argtype' sf
436: ' ffi-arg-double &10 argtype' df
1.12 pazsan 437: ' ffi-arg-long _long argtype' long
1.2 pazsan 438: ' ffi-arg-longlong 8 argtype' llong
1.12 pazsan 439: ' ffi-arg-dlong 6 argtype' dlong
1.2 pazsan 440: ' ffi-arg-ptr &12 argtype' ptr
1.26 pazsan 441: : ints ( n -- ) 0 ?DO postpone int LOOP ; immediate
1.2 pazsan 442:
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)
1.12 pazsan 448: ' ffi-ret-long _long rettype' (long)
449: ' ffi-ret-dlong _long rettype' (dlong)
1.2 pazsan 450: ' ffi-ret-ptr &12 rettype' (ptr)
1.1 pazsan 451:
452: previous definitions
453:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>