File:  [gforth] / gforth / libffi.fs
Revision 1.29: download - view: text, annotated - select for diffs
Sat Aug 9 21:28:39 2008 UTC (12 years, 8 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Another fix for Xcode nonsense

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

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