Annotation of gforth/libffi.fs, revision 1.27

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

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