File:  [gforth] / gforth / fflib.fs
Revision 1.19: download - view: text, annotated - select for diffs
Tue Feb 12 20:28:51 2008 UTC (16 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Added replacements for primitives to fflib.fs
  current status: the libcc-generated code compiles, but does not link
Bugfix in libcc.fs

    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 #include <avcall.h>
   24: \c #include <callback.h>
   25: \c static av_alist alist;
   26: \c static va_alist gforth_clist;
   27: \c static float frv;
   28: \c static int irv;
   29: \c static double drv;
   30: \c static long long llrv;
   31: \c static void * prv;
   32: \c static Cell *gforth_RP;
   33: \c static char *gforth_LP;
   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: 
  130: : av-int-r      2r> >r av-int ;
  131: : av-float-r    f@local0 lp+ av-float ;
  132: : av-double-r   f@local0 lp+ av-double ;
  133: : av-longlong-r r> 2r> rot >r av-longlong ;
  134: : av-ptr-r      2r> >r av-ptr ;
  135: : va-return-void      va-return-void1     0 (bye) ;
  136: : va-return-int       va-return-int1      0 (bye) ;
  137: : va-return-ptr       va-return-ptr1      0 (bye) ;
  138: : va-return-longlong  va-return-longlong1 0 (bye) ;
  139: : va-return-float     va-return-float1    0 (bye) ;
  140: : va-return-double    va-return-double1   0 (bye) ;
  141: 
  142: \ start of fflib proper
  143: 
  144: Variable libs 0 libs !
  145: \ links between libraries
  146: Variable thisproc
  147: Variable thislib
  148: 
  149: Variable revdec  revdec off
  150: \ turn revdec on to compile bigFORTH libraries
  151: Variable revarg  revarg off
  152: \ turn revarg on to compile declarations with reverse arguments
  153: Variable legacy  legacy off
  154: \ turn legacy on to compile bigFORTH legacy libraries
  155: 
  156: Vocabulary c-decl
  157: Vocabulary cb-decl
  158: 
  159: : @lib ( lib -- )
  160:     \G obtains library handle
  161:     cell+ dup 2 cells + count open-lib
  162:     dup 0= abort" Library not found" swap ! ;
  163: 
  164: : @proc ( lib addr -- )
  165:     \G obtains symbol address
  166:     cell+ tuck cell+ @ count rot cell+ @
  167:     lib-sym  dup 0= abort" Proc not found!" swap ! ;
  168: 
  169: : proc, ( lib -- )
  170: \G allocates and initializes proc stub
  171: \G stub format:
  172: \G    linked list in library
  173: \G    address of proc
  174: \G    ptr to OS name of symbol as counted string
  175: \G    threaded code for invocation
  176:     here dup thisproc !
  177:     swap 2 cells + dup @ A, !
  178:     0 , 0 A, ;
  179: 
  180: Defer legacy-proc  ' noop IS legacy-proc
  181: 
  182: : proc:  ( lib "name" -- )
  183: \G Creates a named proc stub
  184:     Create proc, 0 also c-decl
  185:     legacy @ IF  legacy-proc  THEN
  186: DOES> ( x1 .. xn -- r )
  187:     dup cell+ @ swap 3 cells + >r ;
  188: 
  189: Variable ind-call ind-call off
  190: : fptr ( "name" -- )
  191:     Create here thisproc ! 0 , 0 , 0 ,  0 also c-decl  ind-call on
  192:     DOES>  3 cells + >r ;
  193: 
  194: : library ( "name" "file" -- )
  195: \G loads library "file" and creates a proc defining word "name"
  196: \G library format:
  197: \G    linked list of libraries
  198: \G    library handle
  199: \G    linked list of library's procs
  200: \G    OS name of library as counted string
  201:     Create  here libs @ A, dup libs !
  202:     0 , 0 A, parse-name string, @lib
  203: DOES> ( -- )  dup thislib ! proc: ;
  204: 
  205: : init-shared-libs ( -- )
  206:     defers 'cold
  207:     0  libs  BEGIN
  208: 	@ dup WHILE
  209: 	    dup  REPEAT
  210:     drop BEGIN
  211: 	dup  WHILE
  212: 	    >r
  213: 	    r@ @lib
  214: 	    r@ 2 cells +  BEGIN
  215: 		@ dup  WHILE
  216: 		    r@ over @proc  REPEAT
  217: 	    drop rdrop
  218:     REPEAT
  219:     drop ;
  220: 
  221: ' init-shared-libs IS 'cold
  222: 
  223: : argtype ( revxt pushxt fwxt "name" -- )
  224:     Create , , , ;
  225: 
  226: : arg@ ( arg -- argxt pushxt )
  227:     revarg @ IF  2 cells + @ ['] noop swap  ELSE  2@  THEN ;
  228: 
  229: : arg, ( xt -- )
  230:     dup ['] noop = IF  drop  EXIT  THEN  compile, ;
  231: 
  232: : decl, ( 0 arg1 .. argn call start -- )
  233:     2@ compile, >r
  234:     revdec @ IF  0 >r
  235: 	BEGIN  dup  WHILE  >r  REPEAT
  236: 	BEGIN  r> dup  WHILE  arg@ arg,  REPEAT  drop
  237: 	BEGIN  dup  WHILE  arg,  REPEAT drop
  238:     ELSE  0 >r
  239: 	BEGIN  dup  WHILE  arg@ arg, >r REPEAT drop
  240: 	BEGIN  r> dup  WHILE  arg,  REPEAT  drop
  241:     THEN
  242:     r> compile,  postpone EXIT ;
  243: 
  244: : symbol, ( "c-symbol" -- )
  245:     here thisproc @ 2 cells + ! parse-name s,
  246:     thislib @ thisproc @ @proc ;
  247: 
  248: : rettype ( endxt startxt "name" -- )
  249:     Create 2,
  250:   DOES>  decl, ind-call @ 0= IF  symbol,  THEN
  251:     previous revarg off ind-call off ;
  252: 
  253: also c-decl definitions
  254: 
  255: : <rev>  revarg on ;
  256: 
  257: ' av-int      ' av-int-r      ' >r  argtype int
  258: ' av-float    ' av-float-r    ' f>l argtype sf
  259: ' av-double   ' av-double-r   ' f>l argtype df
  260: ' av-longlong ' av-longlong-r ' 2>r argtype dlong
  261: ' av-ptr      ' av-ptr-r      ' >r  argtype ptr
  262: 
  263: ' av-call-void     ' av-start-void     rettype (void)
  264: ' av-call-int      ' av-start-int      rettype (int)
  265: ' av-call-float    ' av-start-float    rettype (sf)
  266: ' av-call-double   ' av-start-double   rettype (fp)
  267: ' av-call-longlong ' av-start-longlong rettype (dlong)
  268: ' av-call-ptr      ' av-start-ptr      rettype (ptr)
  269: 
  270: : (addr)  postpone EXIT drop symbol, previous revarg off ;
  271: 
  272: previous definitions
  273: 
  274: \ legacy support for old library interfaces
  275: \ interface to old vararg stuff not implemented yet
  276: 
  277: also c-decl
  278: 
  279: :noname ( n 0 -- 0 int1 .. intn )
  280:     legacy @ 0< revarg !
  281:     swap 0 ?DO  int  LOOP  (int)
  282: ; IS legacy-proc
  283: 
  284: : (int) ( n -- )
  285:     >r ' execute r> 0 ?DO  int  LOOP  (int) ;
  286: : (void) ( n -- )
  287:     >r ' execute r> 0 ?DO  int  LOOP  (void) ;
  288: : (float) ( n -- )
  289:     >r ' execute r> 0 ?DO  df   LOOP  (fp) ;
  290: 
  291: previous
  292: 
  293: \ callback stuff
  294: 
  295: Variable callbacks
  296: \G link between callbacks
  297: 
  298: : callback ( -- )
  299:     Create  0 ] postpone >r also cb-decl
  300:   DOES>
  301:     Create here >r 0 , callbacks @ A, r@ callbacks !
  302:     swap postpone Literal postpone call , postpone EXIT
  303:     r> dup cell+ cell+ alloc-callback swap !
  304:   DOES> @ ;
  305: 
  306: : callback; ( 0 xt1 .. xtn -- )
  307:     BEGIN  over  WHILE  compile,  REPEAT
  308:     postpone r> postpone execute compile, drop
  309:     postpone EXIT postpone [ previous ; immediate
  310: 
  311: : va-ret ( xt xt -- )
  312:     Create A, A, immediate
  313:   DOES> 2@ compile, ;
  314: 
  315: : init-callbacks ( -- )
  316:     defers 'cold  callbacks 1 cells -
  317:     BEGIN  cell+ @ dup  WHILE  dup cell+ cell+ alloc-callback over !
  318:     REPEAT  drop ;
  319: 
  320: ' init-callbacks IS 'cold
  321: 
  322: also cb-decl definitions
  323: 
  324: \ arguments
  325: 
  326: ' va-arg-int      Alias int
  327: ' va-arg-float    Alias sf
  328: ' va-arg-double   Alias df
  329: ' va-arg-longlong Alias dlong
  330: ' va-arg-ptr      Alias ptr
  331: 
  332: ' va-return-void     ' va-start-void     va-ret (void)
  333: ' va-return-int      ' va-start-int      va-ret (int)
  334: ' va-return-float    ' va-start-float    va-ret (sf)
  335: ' va-return-double   ' va-start-double   va-ret (fp)
  336: ' va-return-longlong ' va-start-longlong va-ret (dlong)
  337: ' va-return-ptr      ' va-start-ptr      va-ret (ptr)
  338: 
  339: previous definitions

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