File:  [gforth] / gforth / libffi.fs
Revision 1.38: download - view: text, annotated - select for diffs
Mon Dec 31 15:25:18 2012 UTC (6 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright year

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

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