File:  [gforth] / gforth / fflib.fs
Revision 1.21: download - view: text, annotated - select for diffs
Sat Jul 5 20:09:42 2008 UTC (12 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
fflib.fs now is a named C interface library
fixed bug in libcc.fs (unnamed libraries are now always rebuilt)

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

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