File:  [gforth] / gforth / fflib.fs
Revision 1.25: download - view: text, annotated - select for diffs
Tue Jul 29 09:24:31 2008 UTC (15 years, 8 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Correct declaration of gforth_RP and gforth_LP

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

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