File:  [gforth] / gforth / libffi.fs
Revision 1.23: download - view: text, annotated - select for diffs
Thu Jul 3 12:29:04 2008 UTC (15 years, 9 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Removed remainders of FFI in C code

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

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