File:  [gforth] / gforth / libffi.fs
Revision 1.25: download - view: text, annotated - select for diffs
Tue Jul 15 16:11:49 2008 UTC (15 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years
updated copyright-blacklist (added libltdl)
updated distributed files (don't distribute files without distribution terms)
added copyright to preforth.in and build-ec.in

    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: 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;
   39: \c static void **gforth_clist;
   40: \c static void *gforth_ritem;
   41: \c typedef void *Label;
   42: \c typedef Label *Xt;
   43: \c Label *gforth_engine(Xt *ip, Cell *sp, Cell *rp0, Float *fp, unsigned char *lp);
   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;
   49: \c   unsigned char *lp = gforth_LP;
   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: 
   92: \c #define ffi_prep_closure1(a_ip, a_cif, a_closure) \
   93: \c              ffi_prep_closure((ffi_closure *)a_closure, (ffi_cif *)a_cif, gforth_callback_ffi, (void *)a_ip)
   94: c-function ffi-prep-closure ffi_prep_closure1 a a a -- n
   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 ;
  153: end-c-library
  154: 
  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+ ;
  241: : >i+  ( n buf -- buf' )     tuck   l!    cell+ cell+ ;
  242: : >p+  ( addr buf -- buf' )  tuck    !    cell+ cell+ ;
  243: : >d+  ( d buf -- buf' )     dup >r ffi-2! r> cell+ cell+ ;
  244: : >dl+ ( d buf -- buf' )     nip dup >r  ! r> cell+ cell+ ;
  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: 
  250: : >i-  ( n buf -- buf' )     2 cells - tuck   l! ;
  251: : >p-  ( addr buf -- buf' )  2 cells - tuck    ! ;
  252: : >d-  ( d buf -- buf' )     2 cells - dup >r ffi-2! r> ;
  253: : >dl- ( d buf -- buf' )     2 cells - nip dup >r ! r> ;
  254: : >sf- ( r buf -- buf' )     2 cells - dup   sf! ;
  255: : >df- ( r buf -- buf' )     2 cells - dup   df! ;
  256: 
  257: \ return value
  258: 
  259: : i>x   ( -- n )  retbuf l@ ;
  260: : is>x   ( -- n )  retbuf sl@ ;
  261: : p>x   ( -- addr ) retbuf @ ;
  262: : dl>x   ( -- d ) retbuf @ s>d ;
  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
  270: : cifreset  cifbuf cell+ cifbuf ! ;
  271: cifreset
  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: 
  288: Variable ind-call  ind-call off
  289: : fptr  ind-call on  Create  here thisproc !
  290:     0 , 0 , 0 , 0 also c-decl  DOES>  cell+ dup cell+ cell+ >r ! ;
  291: 
  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: 
  304: : create-cif ( rtype -- addr ) cif,
  305:     cif@ cifs search-wordlist
  306:     IF  execute  EXIT  THEN
  307:     get-current >r cifs set-current
  308:     cif@ nextname Create  here >r
  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> ;
  315: 
  316: : decl, ( 0 arg1 .. argn call rtype start -- )
  317:     start, { retxt rtype } cifreset
  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,
  331:   DOES>  2@ args @ decl, ind-call @ 0= IF  symbol,  THEN
  332:     previous revarg off args off ind-call off ;
  333: 
  334: 6 1 cells 4 > 2* - Constant _long
  335: 
  336: also c-decl definitions
  337: 
  338: : <rev>  revarg on ;
  339: 
  340: ' >i+  ' >i-    6 argtype int
  341: ' >p+  ' >p-    _long argtype long
  342: ' >p+  ' >p-  &12 argtype ptr
  343: ' >d+  ' >d-    8 argtype llong
  344: ' >dl+ ' >dl-   6 argtype dlong
  345: ' >sf+ ' >sf-   9 argtype sf
  346: ' >df+ ' >df- &10 argtype df
  347: 
  348: ' noop   0 rettype (void)
  349: ' is>x   6 rettype (int)
  350: ' i>x    5 rettype (uint)
  351: ' p>x    _long rettype (long)
  352: ' p>x  &12 rettype (ptr)
  353: ' d>x    8 rettype (llong)
  354: ' dl>x   6 rettype (dlong)
  355: ' sf>x   9 rettype (sf)
  356: ' df>x &10 rettype (fp)
  357: 
  358: : (addr) thisproc @ cell+ postpone Literal postpone @ postpone EXIT
  359:     drop symbol, previous revarg off args off ;
  360: 
  361: previous definitions
  362: 
  363: \ legacy support for old library interfaces
  364: \ interface to old vararg stuff not implemented yet
  365: 
  366: also c-decl
  367: 
  368: :noname ( n 0 -- 0 int1 .. intn )
  369:     legacy @ 0< revarg !
  370:     swap 0 ?DO  int  LOOP  (int)
  371: ; IS legacy-proc
  372: 
  373: : (int) ( n -- )
  374:     >r ' execute r> 0 ?DO  int  LOOP  (int) ;
  375: : (void) ( n -- )
  376:     >r ' execute r> 0 ?DO  int  LOOP  (void) ;
  377: : (float) ( n -- )
  378:     >r ' execute r> 0 ?DO  df   LOOP  (fp) ;
  379: 
  380: previous
  381: 
  382: \ callback stuff
  383: 
  384: Variable callbacks
  385: \G link between callbacks
  386: 
  387: Variable rtype
  388: 
  389: : alloc-callback ( ip -- addr )
  390:     rtype @ make-cif here 1 ffi-size allot
  391:     dup >r ffi-prep-closure throw r> ;
  392: 
  393: : callback ( -- )
  394:     Create  0 ] postpone >r also cb-decl cifreset
  395:   DOES>
  396:     0 Value  -1 cells allot
  397:     here >r 0 , callbacks @ A, r@ callbacks !
  398:     swap postpone Literal postpone call , postpone EXIT
  399:     r@ cell+ cell+ alloc-callback r> ! ;
  400: 
  401: \ !! is the stack effect right?  or is it ( 0 ret arg1 .. argn -- ) ?
  402: : callback; ( 0 arg1 .. argn -- )
  403:     BEGIN  over  WHILE  compile,  REPEAT
  404:     postpone r> postpone execute compile, drop
  405:     \ !! should we put ]] 0 (bye) [[ here?
  406:     \ !! is the EXIT ever executed?
  407:     postpone EXIT postpone [ previous ; immediate
  408: 
  409: : rettype' ( xt n -- )
  410:     Create , A, immediate
  411:   DOES> 2@ rtype ! ;
  412: : argtype' ( xt n -- )
  413:     Create , A, immediate
  414:   DOES> 2@ cif, ;
  415: 
  416: : init-callbacks ( -- )
  417:     defers 'cold  callbacks cell -
  418:     BEGIN  cell+ @ dup  WHILE  dup cell+ cell+ alloc-callback over !
  419:     REPEAT  drop ;
  420: 
  421: ' init-callbacks IS 'cold
  422: 
  423: also cb-decl definitions
  424: 
  425: \ arguments
  426: 
  427: ' ffi-arg-int        6 argtype' int
  428: ' ffi-arg-float      9 argtype' sf
  429: ' ffi-arg-double   &10 argtype' df
  430: ' ffi-arg-long       _long argtype' long
  431: ' ffi-arg-longlong   8 argtype' llong
  432: ' ffi-arg-dlong      6 argtype' dlong
  433: ' ffi-arg-ptr      &12 argtype' ptr
  434: 
  435: ' ffi-ret-void       0 rettype' (void)
  436: ' ffi-ret-int        6 rettype' (int)
  437: ' ffi-ret-float      9 rettype' (sf)
  438: ' ffi-ret-double   &10 rettype' (fp)
  439: ' ffi-ret-longlong   8 rettype' (llong)
  440: ' ffi-ret-long       _long rettype' (long)
  441: ' ffi-ret-dlong      _long rettype' (dlong)
  442: ' ffi-ret-ptr      &12 rettype' (ptr)
  443: 
  444: previous definitions
  445:     

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