File:  [gforth] / gforth / fflib.fs
Revision 1.26: download - view: text, annotated - select for diffs
Thu Sep 18 02:42:24 2008 UTC (15 years, 6 months ago) by pazsan
Branches: MAIN
CVS tags: v0-7-0, HEAD
Windows version of libcc interface (first attempt)

    1: \ lib.fs	shared library support package 		16aug03py
    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: c-library fflib
   22: s" avcall" add-lib
   23: s" callback" add-lib
   24: 
   25: \c #include <avcall.h>
   26: \c #include <callback.h>
   27: \c static av_alist alist;
   28: \c static va_alist gforth_clist;
   29: \c #ifndef HAS_BACKLINK
   30: \c static void **saved_gforth_pointers;
   31: \c #endif
   32: \c static float frv;
   33: \c static int irv;
   34: \c static double drv;
   35: \c static long long llrv;
   36: \c static void * prv;
   37: \c typedef void *Label;
   38: \c typedef Label *Xt;
   39: \c 
   40: \c void gforth_callback_ffcall(Xt* fcall, void * alist)
   41: \c {
   42: \c #ifndef HAS_BACKLINK
   43: \c   void **gforth_pointers = saved_gforth_pointers;
   44: \c #endif
   45: \c   {
   46: \c     /* save global valiables */
   47: \c     Cell *rp = gforth_RP;
   48: \c     Cell *sp = gforth_SP;
   49: \c     Float *fp = gforth_FP;
   50: \c     char *lp = gforth_LP;
   51: \c     va_alist clist = gforth_clist;
   52: \c 
   53: \c     gforth_clist = (va_alist)alist;
   54: \c 
   55: \c     gforth_engine(fcall, sp, rp, fp, lp, gforth_UP);
   56: \c 
   57: \c     /* restore global variables */
   58: \c     gforth_RP = rp;
   59: \c     gforth_SP = sp;
   60: \c     gforth_FP = fp;
   61: \c     gforth_LP = lp;
   62: \c     gforth_clist = clist;
   63: \c   }
   64: \c }
   65: 
   66: \c #define av_start_void1(c_addr) av_start_void(alist, c_addr)
   67: c-function av-start-void av_start_void1 a -- void
   68: \c #define av_start_int1(c_addr) av_start_int(alist, c_addr, &irv)
   69: c-function av-start-int av_start_int1 a -- void
   70: \c #define av_start_float1(c_addr) av_start_float(alist, c_addr, &frv)
   71: c-function av-start-float av_start_float1 a -- void
   72: \c #define av_start_double1(c_addr) av_start_double(alist, c_addr, &drv)
   73: c-function av-start-double av_start_double1 a -- void
   74: \c #define av_start_longlong1(c_addr) av_start_longlong(alist, c_addr, &llrv)
   75: c-function av-start-longlong av_start_longlong1 a -- void
   76: \c #define av_start_ptr1(c_addr) av_start_ptr(alist, c_addr, void *, &prv)
   77: c-function av-start-ptr av_start_ptr1 a -- void
   78: \c #define av_int1(w) av_int(alist,w)
   79: c-function av-int av_int1 n -- void
   80: \c #define av_float1(r) av_float(alist,r)
   81: c-function av-float av_float1 r -- void
   82: \c #define av_double1(r) av_double(alist,r)
   83: c-function av-double av_double1 r -- void
   84: \c #define av_longlong1(d) av_longlong(alist,d)
   85: c-function av-longlong av_longlong1 d -- void
   86: \c #define av_ptr1(a) av_ptr(alist, void *, a)
   87: c-function av-ptr av_ptr1 a -- void
   88: \c #define av_call_void() av_call(alist)
   89: c-function av-call-void av_call_void -- void
   90: \c #define av_call_int() (av_call(alist), irv)
   91: c-function av-call-int av_call_int -- n
   92: \c #define av_call_float() (av_call(alist), frv)
   93: c-function av-call-float av_call_float -- r
   94: \c #define av_call_double() (av_call(alist), drv)
   95: c-function av-call-double av_call_double -- r
   96: \c #define av_call_longlong() (av_call(alist), llrv)
   97: c-function av-call-longlong av_call_longlong -- d
   98: \c #define av_call_ptr() (av_call(alist), prv)
   99: c-function av-call-ptr av_call_ptr -- a
  100: \c #define alloc_callback1(a_ip) alloc_callback(gforth_callback_ffcall, (Xt *)a_ip)
  101: c-function alloc-callback alloc_callback1 a -- a
  102: \c #define va_start_void1() va_start_void(gforth_clist)
  103: c-function va-start-void va_start_void1 -- void
  104: \c #define va_start_int1() va_start_int(gforth_clist)
  105: c-function va-start-int va_start_int1 -- void
  106: \c #define va_start_longlong1() va_start_longlong(gforth_clist)
  107: c-function va-start-longlong va_start_longlong1 -- void
  108: \c #define va_start_ptr1() va_start_ptr(gforth_clist, (char *))
  109: c-function va-start-ptr va_start_ptr1 -- void
  110: \c #define va_start_float1() va_start_float(gforth_clist)
  111: c-function va-start-float va_start_float1 -- void
  112: \c #define va_start_double1() va_start_double(gforth_clist)
  113: c-function va-start-double va_start_double1 -- void
  114: \c #define va_arg_int1() va_arg_int(gforth_clist)
  115: c-function va-arg-int va_arg_int1 -- n
  116: \c #define va_arg_longlong1() va_arg_longlong(gforth_clist)
  117: c-function va-arg-longlong va_arg_longlong1 -- d
  118: \c #define va_arg_ptr1() va_arg_ptr(gforth_clist, char *)
  119: c-function va-arg-ptr va_arg_ptr1 -- a
  120: \c #define va_arg_float1() va_arg_float(gforth_clist)
  121: c-function va-arg-float va_arg_float1 -- r
  122: \c #define va_arg_double1() va_arg_double(gforth_clist)
  123: c-function va-arg-double va_arg_double1 -- r
  124: \c #define va_return_void1() va_return_void(gforth_clist)
  125: c-function va-return-void1 va_return_void1 -- void
  126: \c #define va_return_int1(w) va_return_int(gforth_clist,w)
  127: c-function va-return-int1 va_return_int1 n -- void
  128: \c #define va_return_ptr1(w) va_return_ptr(gforth_clist, void *, w)
  129: c-function va-return-ptr1 va_return_ptr1 a -- void
  130: \c #define va_return_longlong1(d) va_return_longlong(gforth_clist,d)
  131: c-function va-return-longlong1 va_return_longlong1 d -- void
  132: \c #define va_return_float1(r) va_return_float(gforth_clist,r)
  133: c-function va-return-float1 va_return_float1 r -- void
  134: \c #define va_return_double1(r) va_return_double(gforth_clist,r)
  135: c-function va-return-double1 va_return_double1 r -- void
  136: end-c-library
  137: 
  138: : av-int-r      2r> >r av-int ;
  139: : av-float-r    f@local0 lp+ av-float ;
  140: : av-double-r   f@local0 lp+ av-double ;
  141: : av-longlong-r r> 2r> rot >r av-longlong ;
  142: : av-ptr-r      2r> >r av-ptr ;
  143: : va-return-void      va-return-void1     0 (bye) ;
  144: : va-return-int       va-return-int1      0 (bye) ;
  145: : va-return-ptr       va-return-ptr1      0 (bye) ;
  146: : va-return-longlong  va-return-longlong1 0 (bye) ;
  147: : va-return-float     va-return-float1    0 (bye) ;
  148: : va-return-double    va-return-double1   0 (bye) ;
  149: 
  150: \ start of fflib proper
  151: 
  152: Variable libs 0 libs !
  153: \ links between libraries
  154: Variable thisproc
  155: Variable thislib
  156: 
  157: Variable revdec  revdec off
  158: \ turn revdec on to compile bigFORTH libraries
  159: Variable revarg  revarg off
  160: \ turn revarg on to compile declarations with reverse arguments
  161: Variable legacy  legacy off
  162: \ turn legacy on to compile bigFORTH legacy libraries
  163: 
  164: Vocabulary c-decl
  165: Vocabulary cb-decl
  166: 
  167: : @lib ( lib -- )
  168:     \G obtains library handle
  169:     cell+ dup 2 cells + count open-lib
  170:     dup 0= abort" Library not found" swap ! ;
  171: 
  172: : @proc ( lib addr -- )
  173:     \G obtains symbol address
  174:     cell+ tuck cell+ @ count rot cell+ @
  175:     lib-sym  dup 0= abort" Proc not found!" swap ! ;
  176: 
  177: : proc, ( lib -- )
  178: \G allocates and initializes proc stub
  179: \G stub format:
  180: \G    linked list in library
  181: \G    address of proc
  182: \G    ptr to OS name of symbol as counted string
  183: \G    threaded code for invocation
  184:     here dup thisproc !
  185:     swap 2 cells + dup @ A, !
  186:     0 , 0 A, ;
  187: 
  188: Defer legacy-proc  ' noop IS legacy-proc
  189: 
  190: : proc:  ( lib "name" -- )
  191: \G Creates a named proc stub
  192:     Create proc, 0 also c-decl
  193:     legacy @ IF  legacy-proc  THEN
  194: DOES> ( x1 .. xn -- r )
  195:     dup cell+ @ swap 3 cells + >r ;
  196: 
  197: Variable ind-call ind-call off
  198: : fptr ( "name" -- )
  199:     Create here thisproc ! 0 , 0 , 0 ,  0 also c-decl  ind-call on
  200:     DOES>  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
  215:     0  libs  BEGIN
  216: 	@ dup WHILE
  217: 	    dup  REPEAT
  218:     drop BEGIN
  219: 	dup  WHILE
  220: 	    >r
  221: 	    r@ @lib
  222: 	    r@ 2 cells +  BEGIN
  223: 		@ dup  WHILE
  224: 		    r@ over @proc  REPEAT
  225: 	    drop rdrop
  226:     REPEAT
  227:     drop ;
  228: 
  229: ' init-shared-libs IS 'cold
  230: 
  231: : argtype ( revxt pushxt fwxt "name" -- )
  232:     Create , , , ;
  233: 
  234: : arg@ ( arg -- argxt pushxt )
  235:     revarg @ IF  2 cells + @ ['] noop swap  ELSE  2@  THEN ;
  236: 
  237: : arg, ( xt -- )
  238:     dup ['] noop = IF  drop  EXIT  THEN  compile, ;
  239: 
  240: : decl, ( 0 arg1 .. argn call start -- )
  241:     2@ compile, >r
  242:     revdec @ IF  0 >r
  243: 	BEGIN  dup  WHILE  >r  REPEAT
  244: 	BEGIN  r> dup  WHILE  arg@ arg,  REPEAT  drop
  245: 	BEGIN  dup  WHILE  arg,  REPEAT drop
  246:     ELSE  0 >r
  247: 	BEGIN  dup  WHILE  arg@ arg, >r REPEAT drop
  248: 	BEGIN  r> dup  WHILE  arg,  REPEAT  drop
  249:     THEN
  250:     r> compile,  postpone EXIT ;
  251: 
  252: : symbol, ( "c-symbol" -- )
  253:     here thisproc @ 2 cells + ! parse-name s,
  254:     thislib @ thisproc @ @proc ;
  255: 
  256: : rettype ( endxt startxt "name" -- )
  257:     Create 2,
  258:   DOES>  decl, ind-call @ 0= IF  symbol,  THEN
  259:     previous revarg off ind-call off ;
  260: 
  261: also c-decl definitions
  262: 
  263: : <rev>  revarg on ;
  264: 
  265: ' av-int      ' av-int-r      ' >r  argtype int
  266: ' av-float    ' av-float-r    ' f>l argtype sf
  267: ' av-double   ' av-double-r   ' f>l argtype df
  268: ' av-longlong ' av-longlong-r ' 2>r argtype dlong
  269: ' av-ptr      ' av-ptr-r      ' >r  argtype ptr
  270: 
  271: ' av-call-void     ' av-start-void     rettype (void)
  272: ' av-call-int      ' av-start-int      rettype (int)
  273: ' av-call-float    ' av-start-float    rettype (sf)
  274: ' av-call-double   ' av-start-double   rettype (fp)
  275: ' av-call-longlong ' av-start-longlong rettype (dlong)
  276: ' av-call-ptr      ' av-start-ptr      rettype (ptr)
  277: 
  278: : (addr)  postpone EXIT drop symbol, previous revarg off ;
  279: 
  280: previous definitions
  281: 
  282: \ legacy support for old library interfaces
  283: \ interface to old vararg stuff not implemented yet
  284: 
  285: also c-decl
  286: 
  287: :noname ( n 0 -- 0 int1 .. intn )
  288:     legacy @ 0< revarg !
  289:     swap 0 ?DO  int  LOOP  (int)
  290: ; IS legacy-proc
  291: 
  292: : (int) ( n -- )
  293:     >r ' execute r> 0 ?DO  int  LOOP  (int) ;
  294: : (void) ( n -- )
  295:     >r ' execute r> 0 ?DO  int  LOOP  (void) ;
  296: : (float) ( n -- )
  297:     >r ' execute r> 0 ?DO  df   LOOP  (fp) ;
  298: 
  299: previous
  300: 
  301: \ callback stuff
  302: 
  303: Variable callbacks
  304: \G link between callbacks
  305: 
  306: : callback ( -- )
  307:     Create  0 ] postpone >r also cb-decl
  308:   DOES>
  309:     Create here >r 0 , callbacks @ A, r@ callbacks !
  310:     swap postpone Literal postpone call , postpone EXIT
  311:     r> dup cell+ cell+ alloc-callback swap !
  312:   DOES> @ ;
  313: 
  314: : callback; ( 0 xt1 .. xtn -- )
  315:     BEGIN  over  WHILE  compile,  REPEAT
  316:     postpone r> postpone execute compile, drop
  317:     postpone EXIT postpone [ previous ; immediate
  318: 
  319: : va-ret ( xt xt -- )
  320:     Create A, A, immediate
  321:   DOES> 2@ compile, ;
  322: 
  323: : init-callbacks ( -- )
  324:     defers 'cold  callbacks 1 cells -
  325:     BEGIN  cell+ @ dup  WHILE  dup cell+ cell+ alloc-callback over !
  326:     REPEAT  drop ;
  327: 
  328: ' init-callbacks IS 'cold
  329: 
  330: also cb-decl definitions
  331: 
  332: \ arguments
  333: 
  334: ' va-arg-int      Alias int
  335: ' va-arg-float    Alias sf
  336: ' va-arg-double   Alias df
  337: ' va-arg-longlong Alias dlong
  338: ' va-arg-ptr      Alias ptr
  339: 
  340: ' va-return-void     ' va-start-void     va-ret (void)
  341: ' va-return-int      ' va-start-int      va-ret (int)
  342: ' va-return-float    ' va-start-float    va-ret (sf)
  343: ' va-return-double   ' va-start-double   va-ret (fp)
  344: ' va-return-longlong ' va-start-longlong va-ret (dlong)
  345: ' va-return-ptr      ' va-start-ptr      va-ret (ptr)
  346: 
  347: previous definitions

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