File:  [gforth] / gforth / fflib.fs
Revision 1.24: download - view: text, annotated - select for diffs
Tue Jul 29 09:01:44 2008 UTC (12 years, 8 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed fflib.fs
Real fix for the macro problem (define wrapper macro instead)

    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 static float frv;
   30: \c static int irv;
   31: \c static double drv;
   32: \c static long long llrv;
   33: \c static void * prv;
   34: \c typedef void *Label;
   35: \c typedef Label *Xt;
   36: \c Label *gforth_engine(Xt *ip, Cell *sp, Cell *rp0, Float *fp, char *lp);
   37: \c 
   38: \c void gforth_callback_ffcall(Xt* fcall, void * alist)
   39: \c {
   40: \c   /* save global valiables */
   41: \c   Cell *rp = gforth_RP;
   42: \c   Cell *sp = gforth_SP;
   43: \c   Float *fp = gforth_FP;
   44: \c   char *lp = gforth_LP;
   45: \c   va_alist clist = gforth_clist;
   46: \c 
   47: \c   gforth_clist = (va_alist)alist;
   48: \c 
   49: \c   gforth_engine(fcall, sp, rp, fp, lp);
   50: \c 
   51: \c   /* restore global variables */
   52: \c   gforth_RP = rp;
   53: \c   gforth_SP = sp;
   54: \c   gforth_FP = fp;
   55: \c   gforth_LP = lp;
   56: \c   gforth_clist = clist;
   57: \c }
   58: 
   59: \c #define av_start_void1(c_addr) av_start_void(alist, c_addr)
   60: c-function av-start-void av_start_void1 a -- void
   61: \c #define av_start_int1(c_addr) av_start_int(alist, c_addr, &irv)
   62: c-function av-start-int av_start_int1 a -- void
   63: \c #define av_start_float1(c_addr) av_start_float(alist, c_addr, &frv)
   64: c-function av-start-float av_start_float1 a -- void
   65: \c #define av_start_double1(c_addr) av_start_double(alist, c_addr, &drv)
   66: c-function av-start-double av_start_double1 a -- void
   67: \c #define av_start_longlong1(c_addr) av_start_longlong(alist, c_addr, &llrv)
   68: c-function av-start-longlong av_start_longlong1 a -- void
   69: \c #define av_start_ptr1(c_addr) av_start_ptr(alist, c_addr, void *, &prv)
   70: c-function av-start-ptr av_start_ptr1 a -- void
   71: \c #define av_int1(w) av_int(alist,w)
   72: c-function av-int av_int1 n -- void
   73: \c #define av_float1(r) av_float(alist,r)
   74: c-function av-float av_float1 r -- void
   75: \c #define av_double1(r) av_double(alist,r)
   76: c-function av-double av_double1 r -- void
   77: \c #define av_longlong1(d) av_longlong(alist,d)
   78: c-function av-longlong av_longlong1 d -- void
   79: \c #define av_ptr1(a) av_ptr(alist, void *, a)
   80: c-function av-ptr av_ptr1 a -- void
   81: \c #define av_call_void() av_call(alist)
   82: c-function av-call-void av_call_void -- void
   83: \c #define av_call_int() (av_call(alist), irv)
   84: c-function av-call-int av_call_int -- n
   85: \c #define av_call_float() (av_call(alist), frv)
   86: c-function av-call-float av_call_float -- r
   87: \c #define av_call_double() (av_call(alist), drv)
   88: c-function av-call-double av_call_double -- r
   89: \c #define av_call_longlong() (av_call(alist), llrv)
   90: c-function av-call-longlong av_call_longlong -- d
   91: \c #define av_call_ptr() (av_call(alist), prv)
   92: c-function av-call-ptr av_call_ptr -- a
   93: \c #define alloc_callback1(a_ip) alloc_callback(gforth_callback_ffcall, (Xt *)a_ip)
   94: c-function alloc-callback alloc_callback1 a -- a
   95: \c #define va_start_void1() va_start_void(gforth_clist)
   96: c-function va-start-void va_start_void1 -- void
   97: \c #define va_start_int1() va_start_int(gforth_clist)
   98: c-function va-start-int va_start_int1 -- void
   99: \c #define va_start_longlong1() va_start_longlong(gforth_clist)
  100: c-function va-start-longlong va_start_longlong1 -- void
  101: \c #define va_start_ptr1() va_start_ptr(gforth_clist, (char *))
  102: c-function va-start-ptr va_start_ptr1 -- void
  103: \c #define va_start_float1() va_start_float(gforth_clist)
  104: c-function va-start-float va_start_float1 -- void
  105: \c #define va_start_double1() va_start_double(gforth_clist)
  106: c-function va-start-double va_start_double1 -- void
  107: \c #define va_arg_int1() va_arg_int(gforth_clist)
  108: c-function va-arg-int va_arg_int1 -- n
  109: \c #define va_arg_longlong1() va_arg_longlong(gforth_clist)
  110: c-function va-arg-longlong va_arg_longlong1 -- d
  111: \c #define va_arg_ptr1() va_arg_ptr(gforth_clist, char *)
  112: c-function va-arg-ptr va_arg_ptr1 -- a
  113: \c #define va_arg_float1() va_arg_float(gforth_clist)
  114: c-function va-arg-float va_arg_float1 -- r
  115: \c #define va_arg_double1() va_arg_double(gforth_clist)
  116: c-function va-arg-double va_arg_double1 -- r
  117: \c #define va_return_void1() va_return_void(gforth_clist)
  118: c-function va-return-void1 va_return_void1 -- void
  119: \c #define va_return_int1(w) va_return_int(gforth_clist,w)
  120: c-function va-return-int1 va_return_int1 n -- void
  121: \c #define va_return_ptr1(w) va_return_ptr(gforth_clist, void *, w)
  122: c-function va-return-ptr1 va_return_ptr1 a -- void
  123: \c #define va_return_longlong1(d) va_return_longlong(gforth_clist,d)
  124: c-function va-return-longlong1 va_return_longlong1 d -- void
  125: \c #define va_return_float1(r) va_return_float(gforth_clist,r)
  126: c-function va-return-float1 va_return_float1 r -- void
  127: \c #define va_return_double1(r) va_return_double(gforth_clist,r)
  128: c-function va-return-double1 va_return_double1 r -- void
  129: end-c-library
  130: 
  131: : av-int-r      2r> >r av-int ;
  132: : av-float-r    f@local0 lp+ av-float ;
  133: : av-double-r   f@local0 lp+ av-double ;
  134: : av-longlong-r r> 2r> rot >r av-longlong ;
  135: : av-ptr-r      2r> >r av-ptr ;
  136: : va-return-void      va-return-void1     0 (bye) ;
  137: : va-return-int       va-return-int1      0 (bye) ;
  138: : va-return-ptr       va-return-ptr1      0 (bye) ;
  139: : va-return-longlong  va-return-longlong1 0 (bye) ;
  140: : va-return-float     va-return-float1    0 (bye) ;
  141: : va-return-double    va-return-double1   0 (bye) ;
  142: 
  143: \ start of fflib proper
  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:     dup cell+ @ swap 3 cells + >r ;
  189: 
  190: Variable ind-call ind-call off
  191: : fptr ( "name" -- )
  192:     Create here thisproc ! 0 , 0 , 0 ,  0 also c-decl  ind-call on
  193:     DOES>  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
  208:     0  libs  BEGIN
  209: 	@ dup WHILE
  210: 	    dup  REPEAT
  211:     drop BEGIN
  212: 	dup  WHILE
  213: 	    >r
  214: 	    r@ @lib
  215: 	    r@ 2 cells +  BEGIN
  216: 		@ dup  WHILE
  217: 		    r@ over @proc  REPEAT
  218: 	    drop rdrop
  219:     REPEAT
  220:     drop ;
  221: 
  222: ' init-shared-libs IS 'cold
  223: 
  224: : argtype ( revxt pushxt fwxt "name" -- )
  225:     Create , , , ;
  226: 
  227: : arg@ ( arg -- argxt pushxt )
  228:     revarg @ IF  2 cells + @ ['] noop swap  ELSE  2@  THEN ;
  229: 
  230: : arg, ( xt -- )
  231:     dup ['] noop = IF  drop  EXIT  THEN  compile, ;
  232: 
  233: : decl, ( 0 arg1 .. argn call start -- )
  234:     2@ compile, >r
  235:     revdec @ IF  0 >r
  236: 	BEGIN  dup  WHILE  >r  REPEAT
  237: 	BEGIN  r> dup  WHILE  arg@ arg,  REPEAT  drop
  238: 	BEGIN  dup  WHILE  arg,  REPEAT drop
  239:     ELSE  0 >r
  240: 	BEGIN  dup  WHILE  arg@ arg, >r REPEAT drop
  241: 	BEGIN  r> dup  WHILE  arg,  REPEAT  drop
  242:     THEN
  243:     r> compile,  postpone EXIT ;
  244: 
  245: : symbol, ( "c-symbol" -- )
  246:     here thisproc @ 2 cells + ! parse-name s,
  247:     thislib @ thisproc @ @proc ;
  248: 
  249: : rettype ( endxt startxt "name" -- )
  250:     Create 2,
  251:   DOES>  decl, ind-call @ 0= IF  symbol,  THEN
  252:     previous revarg off ind-call off ;
  253: 
  254: also c-decl definitions
  255: 
  256: : <rev>  revarg on ;
  257: 
  258: ' av-int      ' av-int-r      ' >r  argtype int
  259: ' av-float    ' av-float-r    ' f>l argtype sf
  260: ' av-double   ' av-double-r   ' f>l argtype df
  261: ' av-longlong ' av-longlong-r ' 2>r argtype dlong
  262: ' av-ptr      ' av-ptr-r      ' >r  argtype ptr
  263: 
  264: ' av-call-void     ' av-start-void     rettype (void)
  265: ' av-call-int      ' av-start-int      rettype (int)
  266: ' av-call-float    ' av-start-float    rettype (sf)
  267: ' av-call-double   ' av-start-double   rettype (fp)
  268: ' av-call-longlong ' av-start-longlong rettype (dlong)
  269: ' av-call-ptr      ' av-start-ptr      rettype (ptr)
  270: 
  271: : (addr)  postpone EXIT drop symbol, previous revarg off ;
  272: 
  273: previous definitions
  274: 
  275: \ legacy support for old library interfaces
  276: \ interface to old vararg stuff not implemented yet
  277: 
  278: also c-decl
  279: 
  280: :noname ( n 0 -- 0 int1 .. intn )
  281:     legacy @ 0< revarg !
  282:     swap 0 ?DO  int  LOOP  (int)
  283: ; IS legacy-proc
  284: 
  285: : (int) ( n -- )
  286:     >r ' execute r> 0 ?DO  int  LOOP  (int) ;
  287: : (void) ( n -- )
  288:     >r ' execute r> 0 ?DO  int  LOOP  (void) ;
  289: : (float) ( n -- )
  290:     >r ' execute r> 0 ?DO  df   LOOP  (fp) ;
  291: 
  292: previous
  293: 
  294: \ callback stuff
  295: 
  296: Variable callbacks
  297: \G link between callbacks
  298: 
  299: : callback ( -- )
  300:     Create  0 ] postpone >r also cb-decl
  301:   DOES>
  302:     Create here >r 0 , callbacks @ A, r@ callbacks !
  303:     swap postpone Literal postpone call , postpone EXIT
  304:     r> dup cell+ cell+ alloc-callback swap !
  305:   DOES> @ ;
  306: 
  307: : callback; ( 0 xt1 .. xtn -- )
  308:     BEGIN  over  WHILE  compile,  REPEAT
  309:     postpone r> postpone execute compile, drop
  310:     postpone EXIT postpone [ previous ; immediate
  311: 
  312: : va-ret ( xt xt -- )
  313:     Create A, A, immediate
  314:   DOES> 2@ compile, ;
  315: 
  316: : init-callbacks ( -- )
  317:     defers 'cold  callbacks 1 cells -
  318:     BEGIN  cell+ @ dup  WHILE  dup cell+ cell+ alloc-callback over !
  319:     REPEAT  drop ;
  320: 
  321: ' init-callbacks IS 'cold
  322: 
  323: also cb-decl definitions
  324: 
  325: \ arguments
  326: 
  327: ' va-arg-int      Alias int
  328: ' va-arg-float    Alias sf
  329: ' va-arg-double   Alias df
  330: ' va-arg-longlong Alias dlong
  331: ' va-arg-ptr      Alias ptr
  332: 
  333: ' va-return-void     ' va-start-void     va-ret (void)
  334: ' va-return-int      ' va-start-int      va-ret (int)
  335: ' va-return-float    ' va-start-float    va-ret (sf)
  336: ' va-return-double   ' va-start-double   va-ret (fp)
  337: ' va-return-longlong ' va-start-longlong va-ret (dlong)
  338: ' va-return-ptr      ' va-start-ptr      va-ret (ptr)
  339: 
  340: previous definitions

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