Annotation of gforth/libffi.fs, revision 1.16

1.1       pazsan      1: \ libffi.fs    shared library support package          14aug05py
                      2: 
1.15      anton       3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005,2006,2007 Free Software Foundation, Inc.
1.1       pazsan      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
1.14      anton       9: \ as published by the Free Software Foundation, either version 3
1.1       pazsan     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
1.14      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       pazsan     19: 
1.16    ! anton      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: 
1.1       pazsan    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+ ;
1.8       pazsan    229: : >i+  ( n buf -- buf' )     tuck   l!    cell+ cell+ ;
1.1       pazsan    230: : >p+  ( addr buf -- buf' )  tuck    !    cell+ cell+ ;
                    231: : >d+  ( d buf -- buf' )     dup >r ffi-2! r> cell+ cell+ ;
1.12      pazsan    232: : >dl+ ( d buf -- buf' )     nip dup >r  ! r> cell+ cell+ ;
1.1       pazsan    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: 
1.7       pazsan    238: : >i-  ( n buf -- buf' )     2 cells - tuck   l! ;
1.1       pazsan    239: : >p-  ( addr buf -- buf' )  2 cells - tuck    ! ;
                    240: : >d-  ( d buf -- buf' )     2 cells - dup >r ffi-2! r> ;
1.12      pazsan    241: : >dl- ( d buf -- buf' )     2 cells - nip dup >r ! r> ;
1.1       pazsan    242: : >sf- ( r buf -- buf' )     2 cells - dup   sf! ;
                    243: : >df- ( r buf -- buf' )     2 cells - dup   df! ;
                    244: 
                    245: \ return value
                    246: 
1.7       pazsan    247: : i>x   ( -- n )  retbuf l@ ;
                    248: : is>x   ( -- n )  retbuf sl@ ;
1.1       pazsan    249: : p>x   ( -- addr ) retbuf @ ;
1.12      pazsan    250: : dl>x   ( -- d ) retbuf @ s>d ;
1.1       pazsan    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
1.2       pazsan    258: : cifreset  cifbuf cell+ cifbuf ! ;
                    259: cifreset
1.1       pazsan    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: 
1.8       pazsan    276: Variable ind-call  ind-call off
1.10      pazsan    277: : fptr  ind-call on  Create  here thisproc !
1.8       pazsan    278:     0 , 0 , 0 , 0 also c-decl  DOES>  cell+ dup cell+ cell+ >r ! ;
                    279: 
1.1       pazsan    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: 
1.6       pazsan    292: : create-cif ( rtype -- addr ) cif,
1.1       pazsan    293:     cif@ cifs search-wordlist
                    294:     IF  execute  EXIT  THEN
                    295:     get-current >r cifs set-current
                    296:     cif@ nextname Create  here >r
1.6       pazsan    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> ;
1.1       pazsan    303: 
                    304: : decl, ( 0 arg1 .. argn call rtype start -- )
1.2       pazsan    305:     start, { retxt rtype } cifreset
1.1       pazsan    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,
1.8       pazsan    319:   DOES>  2@ args @ decl, ind-call @ 0= IF  symbol,  THEN
                    320:     previous revarg off args off ind-call off ;
1.1       pazsan    321: 
1.12      pazsan    322: 6 1 cells 4 > 2* - Constant _long
                    323: 
1.1       pazsan    324: also c-decl definitions
                    325: 
                    326: : <rev>  revarg on ;
                    327: 
                    328: ' >i+  ' >i-    6 argtype int
1.12      pazsan    329: ' >p+  ' >p-    _long argtype long
1.1       pazsan    330: ' >p+  ' >p-  &12 argtype ptr
                    331: ' >d+  ' >d-    8 argtype llong
1.12      pazsan    332: ' >dl+ ' >dl-   6 argtype dlong
1.1       pazsan    333: ' >sf+ ' >sf-   9 argtype sf
                    334: ' >df+ ' >df- &10 argtype df
                    335: 
                    336: ' noop   0 rettype (void)
1.6       pazsan    337: ' is>x   6 rettype (int)
1.11      pazsan    338: ' i>x    5 rettype (uint)
1.12      pazsan    339: ' p>x    _long rettype (long)
1.1       pazsan    340: ' p>x  &12 rettype (ptr)
                    341: ' d>x    8 rettype (llong)
1.12      pazsan    342: ' dl>x   6 rettype (dlong)
1.1       pazsan    343: ' sf>x   9 rettype (sf)
                    344: ' df>x &10 rettype (fp)
                    345: 
1.3       pazsan    346: : (addr) thisproc @ cell+ postpone Literal postpone @ postpone EXIT
1.4       pazsan    347:     drop symbol, previous revarg off args off ;
1.3       pazsan    348: 
1.1       pazsan    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: 
1.2       pazsan    375: Variable rtype
                    376: 
1.8       pazsan    377: : alloc-callback ( ip -- addr )
                    378:     rtype @ make-cif here 1 ffi-size allot
                    379:     dup >r ffi-prep-closure throw r> ;
1.2       pazsan    380: 
1.1       pazsan    381: : callback ( -- )
1.2       pazsan    382:     Create  0 ] postpone >r also cb-decl cifreset
1.1       pazsan    383:   DOES>
1.8       pazsan    384:     0 Value  -1 cells allot
                    385:     here >r 0 , callbacks @ A, r@ callbacks !
1.1       pazsan    386:     swap postpone Literal postpone call , postpone EXIT
1.8       pazsan    387:     r@ cell+ cell+ alloc-callback r> ! ;
1.1       pazsan    388: 
1.16    ! anton     389: \ !! is the stack effect right?  or is it ( 0 ret arg1 .. argn -- ) ?
1.2       pazsan    390: : callback; ( 0 arg1 .. argn -- )
1.1       pazsan    391:     BEGIN  over  WHILE  compile,  REPEAT
                    392:     postpone r> postpone execute compile, drop
1.16    ! anton     393:     \ !! should we put ]] 0 (bye) [[ here?
        !           394:     \ !! is the EXIT ever executed?
1.1       pazsan    395:     postpone EXIT postpone [ previous ; immediate
                    396: 
1.2       pazsan    397: : rettype' ( xt n -- )
                    398:     Create , A, immediate
                    399:   DOES> 2@ rtype ! ;
                    400: : argtype' ( xt n -- )
                    401:     Create , A, immediate
                    402:   DOES> 2@ cif, ;
1.1       pazsan    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: 
1.2       pazsan    415: ' ffi-arg-int        6 argtype' int
                    416: ' ffi-arg-float      9 argtype' sf
                    417: ' ffi-arg-double   &10 argtype' df
1.12      pazsan    418: ' ffi-arg-long       _long argtype' long
1.2       pazsan    419: ' ffi-arg-longlong   8 argtype' llong
1.12      pazsan    420: ' ffi-arg-dlong      6 argtype' dlong
1.2       pazsan    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)
1.12      pazsan    428: ' ffi-ret-long       _long rettype' (long)
                    429: ' ffi-ret-dlong      _long rettype' (dlong)
1.2       pazsan    430: ' ffi-ret-ptr      &12 rettype' (ptr)
1.1       pazsan    431: 
                    432: previous definitions
                    433:     

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