File:  [gforth] / gforth / libffi.fs
Revision 1.17: download - view: text, annotated - select for diffs
Tue Mar 25 10:41:30 2008 UTC (12 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
typo fixed <http://savannah.gnu.org/bugs/?22722>

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

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