File:  [gforth] / gforth / libffi.fs
Revision 1.12: download - view: text, annotated - select for diffs
Thu May 25 22:10:16 2006 UTC (17 years, 10 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added long/dlong type
ADC-controlled running text
Interrupt table space

    1: \ libffi.fs	shared library support package 		14aug05py
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005 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 2
   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, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: \ common stuff, same as fflib.fs
   22: 
   23: Variable libs 0 libs !
   24: \ links between libraries
   25: Variable thisproc
   26: Variable thislib
   27: 
   28: Variable revdec  revdec off
   29: \ turn revdec on to compile bigFORTH libraries
   30: Variable revarg  revarg off
   31: \ turn revarg on to compile declarations with reverse arguments
   32: Variable legacy  legacy off
   33: \ turn legacy on to compile bigFORTH legacy libraries
   34: 
   35: Vocabulary c-decl
   36: Vocabulary cb-decl
   37: 
   38: : @lib ( lib -- )
   39:     \G obtains library handle
   40:     cell+ dup 2 cells + count open-lib
   41:     dup 0= abort" Library not found" swap ! ;
   42: 
   43: : @proc ( lib addr -- )
   44:     \G obtains symbol address
   45:     cell+ tuck cell+ @ count rot cell+ @
   46:     lib-sym  dup 0= abort" Proc not found!" swap ! ;
   47: 
   48: : proc, ( lib -- )
   49: \G allocates and initializes proc stub
   50: \G stub format:
   51: \G    linked list in library
   52: \G    address of proc
   53: \G    ptr to OS name of symbol as counted string
   54: \G    threaded code for invocation
   55:     here dup thisproc !
   56:     swap 2 cells + dup @ A, !
   57:     0 , 0 A, ;
   58: 
   59: Defer legacy-proc  ' noop IS legacy-proc
   60: 
   61: : proc:  ( lib "name" -- )
   62: \G Creates a named proc stub
   63:     Create proc, 0 also c-decl
   64:     legacy @ IF  legacy-proc  THEN
   65: DOES> ( x1 .. xn -- r )
   66:     3 cells + >r ;
   67: 
   68: : library ( "name" "file" -- )
   69: \G loads library "file" and creates a proc defining word "name"
   70: \G library format:
   71: \G    linked list of libraries
   72: \G    library handle
   73: \G    linked list of library's procs
   74: \G    OS name of library as counted string
   75:     Create  here libs @ A, dup libs !
   76:     0 , 0 A, parse-name string, @lib
   77: DOES> ( -- )  dup thislib ! proc: ;
   78: 
   79: : init-shared-libs ( -- )
   80:     defers 'cold  libs
   81:     0  libs  BEGIN  @ dup  WHILE  dup  REPEAT  drop
   82:     BEGIN  dup  WHILE  >r
   83: 	r@ @lib
   84: 	r@ 2 cells +  BEGIN  @ dup  WHILE  r@ over @proc  REPEAT
   85: 	drop rdrop
   86:     REPEAT  drop ;
   87: 
   88: ' init-shared-libs IS 'cold
   89: 
   90: : symbol, ( "c-symbol" -- )
   91:     here thisproc @ 2 cells + ! parse-name s,
   92:     thislib @ thisproc @ @proc ;
   93: 
   94: \ stuff for libffi
   95: 
   96: \ libffi uses a parameter array for the input
   97: 
   98: $20 Value maxargs
   99: 
  100: Create retbuf 2 cells allot
  101: Create argbuf maxargs 2* cells allot
  102: Create argptr maxargs 0 [DO]  argbuf [I] 2* cells + A, [LOOP]
  103: 
  104: \ "forward" when revarg is on
  105: 
  106: \ : >c+  ( char buf -- buf' )  tuck   c!    cell+ cell+ ;
  107: : >i+  ( n buf -- buf' )     tuck   l!    cell+ cell+ ;
  108: : >p+  ( addr buf -- buf' )  tuck    !    cell+ cell+ ;
  109: : >d+  ( d buf -- buf' )     dup >r ffi-2! r> cell+ cell+ ;
  110: : >dl+ ( d buf -- buf' )     nip dup >r  ! r> cell+ cell+ ;
  111: : >sf+ ( r buf -- buf' )     dup   sf!    cell+ cell+ ;
  112: : >df+ ( r buf -- buf' )     dup   df!    cell+ cell+ ;
  113: 
  114: \ "backward" when revarg is off
  115: 
  116: : >i-  ( n buf -- buf' )     2 cells - tuck   l! ;
  117: : >p-  ( addr buf -- buf' )  2 cells - tuck    ! ;
  118: : >d-  ( d buf -- buf' )     2 cells - dup >r ffi-2! r> ;
  119: : >dl- ( d buf -- buf' )     2 cells - nip dup >r ! r> ;
  120: : >sf- ( r buf -- buf' )     2 cells - dup   sf! ;
  121: : >df- ( r buf -- buf' )     2 cells - dup   df! ;
  122: 
  123: \ return value
  124: 
  125: : i>x   ( -- n )  retbuf l@ ;
  126: : is>x   ( -- n )  retbuf sl@ ;
  127: : p>x   ( -- addr ) retbuf @ ;
  128: : dl>x   ( -- d ) retbuf @ s>d ;
  129: : d>x   ( -- d )  retbuf ffi-2@ ;
  130: : sf>x  ( -- r )  retbuf sf@ ;
  131: : df>x  ( -- r )  retbuf df@ ;
  132: 
  133: wordlist constant cifs
  134: 
  135: Variable cifbuf $40 allot \ maximum: 64 parameters
  136: : cifreset  cifbuf cell+ cifbuf ! ;
  137: cifreset
  138: Variable args args off
  139: 
  140: : argtype ( bkxt fwxt type "name" -- )
  141:     Create , , , DOES>  1 args +! ;
  142: 
  143: : arg@ ( arg -- type pushxt )
  144:     dup @ swap cell+
  145:     revarg @ IF  cell+  THEN  @    ;
  146: 
  147: : arg, ( xt -- )
  148:     dup ['] noop = IF  drop  EXIT  THEN  compile, ;
  149: 
  150: : start, ( n -- )  cifbuf cell+ cifbuf !
  151:     revarg @ IF  drop 0  ELSE  2* cells  THEN  argbuf +
  152:     postpone Literal ;
  153: 
  154: Variable ind-call  ind-call off
  155: : fptr  ind-call on  Create  here thisproc !
  156:     0 , 0 , 0 , 0 also c-decl  DOES>  cell+ dup cell+ cell+ >r ! ;
  157: 
  158: : ffi-call, ( -- lit-cif )
  159:     postpone drop postpone argptr postpone retbuf
  160:     thisproc @ cell+ postpone literal postpone @
  161:     0 postpone literal here cell -
  162:     postpone ffi-call ;
  163: 
  164: : cif, ( n -- )
  165:     cifbuf @ c! 1 cifbuf +! ;
  166: 
  167: : cif@ ( -- addr u )
  168:     cifbuf cell+ cifbuf @ over - ;
  169: 
  170: : create-cif ( rtype -- addr ) cif,
  171:     cif@ cifs search-wordlist
  172:     IF  execute  EXIT  THEN
  173:     get-current >r cifs set-current
  174:     cif@ nextname Create  here >r
  175:     cif@ 1- bounds ?DO  I c@ ffi-type ,  LOOP  r>
  176:     r> set-current ;
  177: 
  178: : make-cif ( rtype -- addr )  create-cif
  179:     cif@ 1- tuck + c@ ffi-type here 0 ffi-size allot
  180:     dup >r ffi-prep-cif throw r> ;
  181: 
  182: : decl, ( 0 arg1 .. argn call rtype start -- )
  183:     start, { retxt rtype } cifreset
  184:     revdec @ IF  0 >r
  185: 	BEGIN  dup  WHILE  >r  REPEAT
  186: 	BEGIN  r> dup  WHILE  arg@ arg,  REPEAT
  187: 	ffi-call, retxt compile,  postpone  EXIT
  188: 	BEGIN  dup  WHILE  cif,  REPEAT drop
  189:     ELSE  0 >r
  190: 	BEGIN  dup  WHILE  arg@ arg, >r REPEAT drop
  191: 	ffi-call, retxt compile,  postpone  EXIT
  192: 	BEGIN  r> dup  WHILE  cif,  REPEAT  drop
  193:     THEN  rtype make-cif swap ! here thisproc @ 2 cells + ! ;
  194: 
  195: : rettype ( endxt n "name" -- )
  196:     Create 2,
  197:   DOES>  2@ args @ decl, ind-call @ 0= IF  symbol,  THEN
  198:     previous revarg off args off ind-call off ;
  199: 
  200: 6 1 cells 4 > 2* - Constant _long
  201: 
  202: also c-decl definitions
  203: 
  204: : <rev>  revarg on ;
  205: 
  206: ' >i+  ' >i-    6 argtype int
  207: ' >p+  ' >p-    _long argtype long
  208: ' >p+  ' >p-  &12 argtype ptr
  209: ' >d+  ' >d-    8 argtype llong
  210: ' >dl+ ' >dl-   6 argtype dlong
  211: ' >sf+ ' >sf-   9 argtype sf
  212: ' >df+ ' >df- &10 argtype df
  213: 
  214: ' noop   0 rettype (void)
  215: ' is>x   6 rettype (int)
  216: ' i>x    5 rettype (uint)
  217: ' p>x    _long rettype (long)
  218: ' p>x  &12 rettype (ptr)
  219: ' d>x    8 rettype (llong)
  220: ' dl>x   6 rettype (dlong)
  221: ' sf>x   9 rettype (sf)
  222: ' df>x &10 rettype (fp)
  223: 
  224: : (addr) thisproc @ cell+ postpone Literal postpone @ postpone EXIT
  225:     drop symbol, previous revarg off args off ;
  226: 
  227: previous definitions
  228: 
  229: \ legacy support for old library interfaces
  230: \ interface to old vararg stuff not implemented yet
  231: 
  232: also c-decl
  233: 
  234: :noname ( n 0 -- 0 int1 .. intn )
  235:     legacy @ 0< revarg !
  236:     swap 0 ?DO  int  LOOP  (int)
  237: ; IS legacy-proc
  238: 
  239: : (int) ( n -- )
  240:     >r ' execute r> 0 ?DO  int  LOOP  (int) ;
  241: : (void) ( n -- )
  242:     >r ' execute r> 0 ?DO  int  LOOP  (void) ;
  243: : (float) ( n -- )
  244:     >r ' execute r> 0 ?DO  df   LOOP  (fp) ;
  245: 
  246: previous
  247: 
  248: \ callback stuff
  249: 
  250: Variable callbacks
  251: \G link between callbacks
  252: 
  253: Variable rtype
  254: 
  255: : alloc-callback ( ip -- addr )
  256:     rtype @ make-cif here 1 ffi-size allot
  257:     dup >r ffi-prep-closure throw r> ;
  258: 
  259: : callback ( -- )
  260:     Create  0 ] postpone >r also cb-decl cifreset
  261:   DOES>
  262:     0 Value  -1 cells allot
  263:     here >r 0 , callbacks @ A, r@ callbacks !
  264:     swap postpone Literal postpone call , postpone EXIT
  265:     r@ cell+ cell+ alloc-callback r> ! ;
  266: 
  267: : callback; ( 0 arg1 .. argn -- )
  268:     BEGIN  over  WHILE  compile,  REPEAT
  269:     postpone r> postpone execute compile, drop
  270:     postpone EXIT postpone [ previous ; immediate
  271: 
  272: : rettype' ( xt n -- )
  273:     Create , A, immediate
  274:   DOES> 2@ rtype ! ;
  275: : argtype' ( xt n -- )
  276:     Create , A, immediate
  277:   DOES> 2@ cif, ;
  278: 
  279: : init-callbacks ( -- )
  280:     defers 'cold  callbacks cell -
  281:     BEGIN  cell+ @ dup  WHILE  dup cell+ cell+ alloc-callback over !
  282:     REPEAT  drop ;
  283: 
  284: ' init-callbacks IS 'cold
  285: 
  286: also cb-decl definitions
  287: 
  288: \ arguments
  289: 
  290: ' ffi-arg-int        6 argtype' int
  291: ' ffi-arg-float      9 argtype' sf
  292: ' ffi-arg-double   &10 argtype' df
  293: ' ffi-arg-long       _long argtype' long
  294: ' ffi-arg-longlong   8 argtype' llong
  295: ' ffi-arg-dlong      6 argtype' dlong
  296: ' ffi-arg-ptr      &12 argtype' ptr
  297: 
  298: ' ffi-ret-void       0 rettype' (void)
  299: ' ffi-ret-int        6 rettype' (int)
  300: ' ffi-ret-float      9 rettype' (sf)
  301: ' ffi-ret-double   &10 rettype' (fp)
  302: ' ffi-ret-longlong   8 rettype' (llong)
  303: ' ffi-ret-long       _long rettype' (long)
  304: ' ffi-ret-dlong      _long rettype' (dlong)
  305: ' ffi-ret-ptr      &12 rettype' (ptr)
  306: 
  307: previous definitions
  308:     

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