Annotation of gforth/fflib.fs, revision 1.28

1.2       pazsan      1: \ lib.fs       shared library support package          16aug03py
1.1       pazsan      2: 
1.23      anton       3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005,2006,2007,2008 Free Software Foundation, Inc.
1.1       pazsan      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
1.18      anton       9: \ as published by the Free Software Foundation, either version 3
1.1       pazsan     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
1.18      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       pazsan     19: 
1.19      anton      20: \ replacements for former primitives
1.21      anton      21: c-library fflib
1.20      anton      22: s" avcall" add-lib
                     23: s" callback" add-lib
                     24: 
1.19      anton      25: \c #include <avcall.h>
                     26: \c #include <callback.h>
                     27: \c static av_alist alist;
                     28: \c static va_alist gforth_clist;
1.26      pazsan     29: \c #ifndef HAS_BACKLINK
                     30: \c static void **saved_gforth_pointers;
                     31: \c #endif
1.19      anton      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 {
1.26      pazsan     42: \c #ifndef HAS_BACKLINK
                     43: \c   void **gforth_pointers = saved_gforth_pointers;
                     44: \c #endif
                     45: \c   {
1.28    ! anton      46: \c     /* save global variables */
1.26      pazsan     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;
1.19      anton      52: \c 
1.26      pazsan     53: \c     gforth_clist = (va_alist)alist;
1.19      anton      54: \c 
1.26      pazsan     55: \c     gforth_engine(fcall, sp, rp, fp, lp, gforth_UP);
1.19      anton      56: \c 
1.26      pazsan     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   }
1.19      anton      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
1.21      anton     136: end-c-library
1.19      anton     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: 
1.1       pazsan    152: Variable libs 0 libs !
1.4       pazsan    153: \ links between libraries
1.1       pazsan    154: Variable thisproc
                    155: Variable thislib
1.4       pazsan    156: 
1.2       pazsan    157: Variable revdec  revdec off
                    158: \ turn revdec on to compile bigFORTH libraries
1.4       pazsan    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
1.2       pazsan    163: 
                    164: Vocabulary c-decl
                    165: Vocabulary cb-decl
1.1       pazsan    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: 
1.4       pazsan    188: Defer legacy-proc  ' noop IS legacy-proc
                    189: 
1.1       pazsan    190: : proc:  ( lib "name" -- )
1.4       pazsan    191: \G Creates a named proc stub
1.2       pazsan    192:     Create proc, 0 also c-decl
1.4       pazsan    193:     legacy @ IF  legacy-proc  THEN
1.1       pazsan    194: DOES> ( x1 .. xn -- r )
                    195:     dup cell+ @ swap 3 cells + >r ;
                    196: 
1.11      pazsan    197: Variable ind-call ind-call off
1.12      pazsan    198: : fptr ( "name" -- )
1.11      pazsan    199:     Create here thisproc ! 0 , 0 , 0 ,  0 also c-decl  ind-call on
                    200:     DOES>  3 cells + >r ;
                    201: 
1.1       pazsan    202: : library ( "name" "file" -- )
1.4       pazsan    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
1.1       pazsan    209:     Create  here libs @ A, dup libs !
1.6       pazsan    210:     0 , 0 A, parse-name string, @lib
1.1       pazsan    211: DOES> ( -- )  dup thislib ! proc: ;
                    212: 
                    213: : init-shared-libs ( -- )
1.16      anton     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 ;
1.1       pazsan    228: 
                    229: ' init-shared-libs IS 'cold
                    230: 
1.4       pazsan    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 -- )
1.1       pazsan    241:     2@ compile, >r
1.2       pazsan    242:     revdec @ IF  0 >r
1.4       pazsan    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
1.1       pazsan    249:     THEN
1.4       pazsan    250:     r> compile,  postpone EXIT ;
                    251: 
                    252: : symbol, ( "c-symbol" -- )
1.6       pazsan    253:     here thisproc @ 2 cells + ! parse-name s,
1.4       pazsan    254:     thislib @ thisproc @ @proc ;
                    255: 
                    256: : rettype ( endxt startxt "name" -- )
                    257:     Create 2,
1.11      pazsan    258:   DOES>  decl, ind-call @ 0= IF  symbol,  THEN
                    259:     previous revarg off ind-call off ;
1.2       pazsan    260: 
1.27      pazsan    261: : func@ >body cell+ @ ;
                    262: : func' ' func@ ;
                    263: : [func'] postpone ['] postpone func@ ; immediate restrict
                    264: 
1.2       pazsan    265: also c-decl definitions
                    266: 
1.4       pazsan    267: : <rev>  revarg on ;
                    268: 
                    269: ' av-int      ' av-int-r      ' >r  argtype int
                    270: ' av-float    ' av-float-r    ' f>l argtype sf
                    271: ' av-double   ' av-double-r   ' f>l argtype df
1.14      pazsan    272: ' av-longlong ' av-longlong-r ' 2>r argtype dlong
1.4       pazsan    273: ' av-ptr      ' av-ptr-r      ' >r  argtype ptr
                    274: 
                    275: ' av-call-void     ' av-start-void     rettype (void)
                    276: ' av-call-int      ' av-start-int      rettype (int)
                    277: ' av-call-float    ' av-start-float    rettype (sf)
                    278: ' av-call-double   ' av-start-double   rettype (fp)
1.14      pazsan    279: ' av-call-longlong ' av-start-longlong rettype (dlong)
1.4       pazsan    280: ' av-call-ptr      ' av-start-ptr      rettype (ptr)
1.1       pazsan    281: 
1.10      pazsan    282: : (addr)  postpone EXIT drop symbol, previous revarg off ;
1.8       pazsan    283: 
1.2       pazsan    284: previous definitions
1.1       pazsan    285: 
1.4       pazsan    286: \ legacy support for old library interfaces
                    287: \ interface to old vararg stuff not implemented yet
1.1       pazsan    288: 
1.2       pazsan    289: also c-decl
1.1       pazsan    290: 
1.4       pazsan    291: :noname ( n 0 -- 0 int1 .. intn )
                    292:     legacy @ 0< revarg !
                    293:     swap 0 ?DO  int  LOOP  (int)
                    294: ; IS legacy-proc
                    295: 
1.1       pazsan    296: : (int) ( n -- )
1.4       pazsan    297:     >r ' execute r> 0 ?DO  int  LOOP  (int) ;
1.1       pazsan    298: : (void) ( n -- )
1.4       pazsan    299:     >r ' execute r> 0 ?DO  int  LOOP  (void) ;
1.1       pazsan    300: : (float) ( n -- )
1.4       pazsan    301:     >r ' execute r> 0 ?DO  df   LOOP  (fp) ;
1.2       pazsan    302: 
                    303: previous
                    304: 
                    305: \ callback stuff
                    306: 
                    307: Variable callbacks
                    308: \G link between callbacks
                    309: 
                    310: : callback ( -- )
                    311:     Create  0 ] postpone >r also cb-decl
                    312:   DOES>
                    313:     Create here >r 0 , callbacks @ A, r@ callbacks !
1.3       pazsan    314:     swap postpone Literal postpone call , postpone EXIT
1.2       pazsan    315:     r> dup cell+ cell+ alloc-callback swap !
                    316:   DOES> @ ;
                    317: 
                    318: : callback; ( 0 xt1 .. xtn -- )
                    319:     BEGIN  over  WHILE  compile,  REPEAT
                    320:     postpone r> postpone execute compile, drop
                    321:     postpone EXIT postpone [ previous ; immediate
                    322: 
                    323: : va-ret ( xt xt -- )
                    324:     Create A, A, immediate
                    325:   DOES> 2@ compile, ;
                    326: 
                    327: : init-callbacks ( -- )
                    328:     defers 'cold  callbacks 1 cells -
                    329:     BEGIN  cell+ @ dup  WHILE  dup cell+ cell+ alloc-callback over !
                    330:     REPEAT  drop ;
                    331: 
                    332: ' init-callbacks IS 'cold
                    333: 
                    334: also cb-decl definitions
                    335: 
                    336: \ arguments
1.1       pazsan    337: 
1.2       pazsan    338: ' va-arg-int      Alias int
                    339: ' va-arg-float    Alias sf
                    340: ' va-arg-double   Alias df
1.14      pazsan    341: ' va-arg-longlong Alias dlong
1.2       pazsan    342: ' va-arg-ptr      Alias ptr
                    343: 
                    344: ' va-return-void     ' va-start-void     va-ret (void)
                    345: ' va-return-int      ' va-start-int      va-ret (int)
                    346: ' va-return-float    ' va-start-float    va-ret (sf)
                    347: ' va-return-double   ' va-start-double   va-ret (fp)
1.14      pazsan    348: ' va-return-longlong ' va-start-longlong va-ret (dlong)
1.2       pazsan    349: ' va-return-ptr      ' va-start-ptr      va-ret (ptr)
                    350: 
                    351: previous definitions

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