Annotation of gforth/libffi.fs, revision 1.36

1.1       pazsan      1: \ libffi.fs    shared library support package          14aug05py
                      2: 
1.33      anton       3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005,2006,2007,2008,2009 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
1.35      pazsan     34: \c static void *(*saved_gforth_pointers)(Cell);
1.30      pazsan     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
1.35      pazsan     41: \c   void *(*gforth_pointers)(Cell) = saved_gforth_pointers;
1.30      pazsan     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
1.36    ! pazsan     54: \c     gforth_engine((Xt *)ip);
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: 
1.31      pazsan    234: : func@ >body cell+ @ ;
                    235: : func' ' func@ ;
                    236: : [func'] postpone ['] postpone func@ ; immediate restrict
                    237: 
1.1       pazsan    238: \ 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+ ;
1.8       pazsan    251: : >i+  ( n buf -- buf' )     tuck   l!    cell+ cell+ ;
1.1       pazsan    252: : >p+  ( addr buf -- buf' )  tuck    !    cell+ cell+ ;
                    253: : >d+  ( d buf -- buf' )     dup >r ffi-2! r> cell+ cell+ ;
1.12      pazsan    254: : >dl+ ( d buf -- buf' )     nip dup >r  ! r> cell+ cell+ ;
1.1       pazsan    255: : >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: 
1.7       pazsan    260: : >i-  ( n buf -- buf' )     2 cells - tuck   l! ;
1.1       pazsan    261: : >p-  ( addr buf -- buf' )  2 cells - tuck    ! ;
                    262: : >d-  ( d buf -- buf' )     2 cells - dup >r ffi-2! r> ;
1.12      pazsan    263: : >dl- ( d buf -- buf' )     2 cells - nip dup >r ! r> ;
1.1       pazsan    264: : >sf- ( r buf -- buf' )     2 cells - dup   sf! ;
                    265: : >df- ( r buf -- buf' )     2 cells - dup   df! ;
                    266: 
                    267: \ return value
                    268: 
1.7       pazsan    269: : i>x   ( -- n )  retbuf l@ ;
                    270: : is>x   ( -- n )  retbuf sl@ ;
1.1       pazsan    271: : p>x   ( -- addr ) retbuf @ ;
1.12      pazsan    272: : dl>x   ( -- d ) retbuf @ s>d ;
1.1       pazsan    273: : d>x   ( -- d )  retbuf ffi-2@ ;
                    274: : sf>x  ( -- r )  retbuf sf@ ;
                    275: : df>x  ( -- r )  retbuf df@ ;
                    276: 
1.32      pazsan    277: table constant cifs
1.1       pazsan    278: 
                    279: Variable cifbuf $40 allot \ maximum: 64 parameters
1.2       pazsan    280: : cifreset  cifbuf cell+ cifbuf ! ;
                    281: cifreset
1.1       pazsan    282: 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: 
1.8       pazsan    298: Variable ind-call  ind-call off
1.10      pazsan    299: : fptr  ind-call on  Create  here thisproc !
1.8       pazsan    300:     0 , 0 , 0 , 0 also c-decl  DOES>  cell+ dup cell+ cell+ >r ! ;
                    301: 
1.1       pazsan    302: : 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: 
1.6       pazsan    314: : create-cif ( rtype -- addr ) cif,
1.1       pazsan    315:     cif@ cifs search-wordlist
                    316:     IF  execute  EXIT  THEN
                    317:     get-current >r cifs set-current
                    318:     cif@ nextname Create  here >r
1.6       pazsan    319:     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> ;
1.1       pazsan    325: 
                    326: : decl, ( 0 arg1 .. argn call rtype start -- )
1.2       pazsan    327:     start, { retxt rtype } cifreset
1.1       pazsan    328:     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,
1.8       pazsan    341:   DOES>  2@ args @ decl, ind-call @ 0= IF  symbol,  THEN
                    342:     previous revarg off args off ind-call off ;
1.1       pazsan    343: 
1.12      pazsan    344: 6 1 cells 4 > 2* - Constant _long
                    345: 
1.1       pazsan    346: also c-decl definitions
                    347: 
                    348: : <rev>  revarg on ;
                    349: 
                    350: ' >i+  ' >i-    6 argtype int
1.12      pazsan    351: ' >p+  ' >p-    _long argtype long
1.1       pazsan    352: ' >p+  ' >p-  &12 argtype ptr
                    353: ' >d+  ' >d-    8 argtype llong
1.12      pazsan    354: ' >dl+ ' >dl-   6 argtype dlong
1.1       pazsan    355: ' >sf+ ' >sf-   9 argtype sf
                    356: ' >df+ ' >df- &10 argtype df
1.27      pazsan    357: : ints 0 ?DO int LOOP ;
1.1       pazsan    358: 
                    359: ' noop   0 rettype (void)
1.6       pazsan    360: ' is>x   6 rettype (int)
1.11      pazsan    361: ' i>x    5 rettype (uint)
1.12      pazsan    362: ' p>x    _long rettype (long)
1.1       pazsan    363: ' p>x  &12 rettype (ptr)
                    364: ' d>x    8 rettype (llong)
1.12      pazsan    365: ' dl>x   6 rettype (dlong)
1.1       pazsan    366: ' sf>x   9 rettype (sf)
                    367: ' df>x &10 rettype (fp)
                    368: 
1.3       pazsan    369: : (addr) thisproc @ cell+ postpone Literal postpone @ postpone EXIT
1.4       pazsan    370:     drop symbol, previous revarg off args off ;
1.3       pazsan    371: 
1.1       pazsan    372: 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: 
1.2       pazsan    398: Variable rtype
                    399: 
1.8       pazsan    400: : alloc-callback ( ip -- addr )
                    401:     rtype @ make-cif here 1 ffi-size allot
                    402:     dup >r ffi-prep-closure throw r> ;
1.2       pazsan    403: 
1.1       pazsan    404: : callback ( -- )
1.2       pazsan    405:     Create  0 ] postpone >r also cb-decl cifreset
1.1       pazsan    406:   DOES>
1.8       pazsan    407:     0 Value  -1 cells allot
                    408:     here >r 0 , callbacks @ A, r@ callbacks !
1.1       pazsan    409:     swap postpone Literal postpone call , postpone EXIT
1.8       pazsan    410:     r@ cell+ cell+ alloc-callback r> ! ;
1.1       pazsan    411: 
1.16      anton     412: \ !! is the stack effect right?  or is it ( 0 ret arg1 .. argn -- ) ?
1.2       pazsan    413: : callback; ( 0 arg1 .. argn -- )
1.1       pazsan    414:     BEGIN  over  WHILE  compile,  REPEAT
                    415:     postpone r> postpone execute compile, drop
1.16      anton     416:     \ !! should we put ]] 0 (bye) [[ here?
                    417:     \ !! is the EXIT ever executed?
1.1       pazsan    418:     postpone EXIT postpone [ previous ; immediate
                    419: 
1.2       pazsan    420: : rettype' ( xt n -- )
                    421:     Create , A, immediate
                    422:   DOES> 2@ rtype ! ;
                    423: : argtype' ( xt n -- )
                    424:     Create , A, immediate
                    425:   DOES> 2@ cif, ;
1.1       pazsan    426: 
                    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: 
1.2       pazsan    438: ' ffi-arg-int        6 argtype' int
                    439: ' ffi-arg-float      9 argtype' sf
                    440: ' ffi-arg-double   &10 argtype' df
1.12      pazsan    441: ' ffi-arg-long       _long argtype' long
1.2       pazsan    442: ' ffi-arg-longlong   8 argtype' llong
1.12      pazsan    443: ' ffi-arg-dlong      6 argtype' dlong
1.2       pazsan    444: ' ffi-arg-ptr      &12 argtype' ptr
1.26      pazsan    445: : ints ( n -- ) 0 ?DO postpone int LOOP ; immediate
1.2       pazsan    446: 
                    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)
1.12      pazsan    452: ' ffi-ret-long       _long rettype' (long)
                    453: ' ffi-ret-dlong      _long rettype' (dlong)
1.2       pazsan    454: ' ffi-ret-ptr      &12 rettype' (ptr)
1.1       pazsan    455: 
                    456: previous definitions
                    457:     

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>