File:  [gforth] / gforth / fflib.fs
Revision 1.2: download - view: text, annotated - select for diffs
Sat Aug 16 19:46:11 2003 UTC (17 years, 8 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
added callback stuff to fflib.fs
removed legacy flag (different usage is sufficient)

    1: \ lib.fs	shared library support package 		16aug03py
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003 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 2
   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, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: Variable libs 0 libs !
   22: Variable thisproc
   23: Variable thislib
   24: \G links between libraries
   25: Variable revdec  revdec off
   26: \ turn revdec on to compile bigFORTH libraries
   27: 
   28: Vocabulary c-decl
   29: Vocabulary cb-decl
   30: 
   31: : @lib ( lib -- )
   32:     \G obtains library handle
   33:     cell+ dup 2 cells + count open-lib
   34:     dup 0= abort" Library not found" swap ! ;
   35: 
   36: : @proc ( lib addr -- )
   37:     \G obtains symbol address
   38:     cell+ tuck cell+ @ count rot cell+ @
   39:     lib-sym  dup 0= abort" Proc not found!" swap ! ;
   40: 
   41: : proc, ( lib -- )
   42: \G allocates and initializes proc stub
   43: \G stub format:
   44: \G    linked list in library
   45: \G    address of proc
   46: \G    ptr to OS name of symbol as counted string
   47: \G    threaded code for invocation
   48:     here dup thisproc !
   49:     swap 2 cells + dup @ A, !
   50:     0 , 0 A, ;
   51: 
   52: : proc:  ( lib "name" -- )
   53:     \G Creates a named proc stub
   54:     Create proc, 0 also c-decl
   55: DOES> ( x1 .. xn -- r )
   56:     dup cell+ @ swap 3 cells + >r ;
   57: 
   58: : library ( "name" "file" -- )
   59:     \G loads library "file" and creates a proc defining word "name"
   60:     \G library format:
   61:     \G    linked list of libraries
   62:     \G    library handle
   63:     \G    linked list of library's procs
   64:     \G    OS name of library as counted string
   65:     Create  here libs @ A, dup libs !
   66:     0 , 0 A, bl sword string, @lib
   67: DOES> ( -- )  dup thislib ! proc: ;
   68: 
   69: : init-shared-libs ( -- )
   70:     defers 'cold  libs
   71:     0  libs  BEGIN  @ dup  WHILE  dup  REPEAT  drop
   72:     BEGIN  dup  WHILE  >r
   73: 	r@ @lib
   74: 	r@ 2 cells +  BEGIN  @ dup  WHILE  r@ over @proc  REPEAT
   75: 	drop rdrop
   76:     REPEAT  drop ;
   77: 
   78: ' init-shared-libs IS 'cold
   79: 
   80: : rettype ( endxt startxt "name" -- )
   81:     create immediate 2,
   82:   DOES>
   83:     2@ compile, >r
   84:     revdec @ IF  0 >r
   85: 	BEGIN  dup  WHILE  >r  REPEAT drop
   86: 	BEGIN  r> dup  WHILE  compile,  REPEAT  drop
   87:     ELSE
   88: 	BEGIN  dup  WHILE  compile,  REPEAT drop
   89:     THEN
   90:     r> compile,  postpone EXIT
   91:     here thisproc @ 2 cells + ! bl sword s,
   92:     thislib @ thisproc @ @proc previous ;
   93: 
   94: also c-decl definitions
   95: 
   96: ' av-int AConstant int
   97: ' av-float AConstant sf
   98: ' av-double AConstant df
   99: ' av-longlong AConstant llong
  100: ' av-ptr AConstant ptr
  101: 
  102: ' av-call-void ' av-start-void rettype (void)
  103: ' av-call-int ' av-start-int rettype (int)
  104: ' av-call-float ' av-start-float rettype (sf)
  105: ' av-call-double ' av-start-double rettype (fp)
  106: ' av-call-longlong ' av-start-longlong rettype (llong)
  107: ' av-call-ptr ' av-start-ptr rettype (ptr)
  108: 
  109: previous definitions
  110: 
  111: \ legacy interface for old library interface
  112: 
  113: also c-decl
  114: 
  115: : (int...) ( n -- )
  116:     >r ' execute r> 0 ?DO  int  LOOP
  117:     0 postpone Literal postpone ?DO postpone int postpone LOOP
  118:     postpone (int) ;
  119: : (void...) ( n -- )
  120:     >r ' execute r> 0 ?DO  int  LOOP
  121:     0 postpone Literal postpone ?DO postpone int postpone LOOP
  122:     postpone (void) ;
  123: : (float...) ( n -- )
  124:     >r ' execute r> 0 ?DO  df  LOOP
  125:     0 postpone Literal postpone ?DO postpone df postpone LOOP
  126:     postpone (fp) ;
  127: : (int) ( n -- )
  128:     >r ' execute r> 0 ?DO  int  LOOP  postpone (int) ;
  129: : (void) ( n -- )
  130:     >r ' execute r> 0 ?DO  int  LOOP  postpone (void) ;
  131: : (float) ( n -- )
  132:     >r ' execute r> 0 ?DO  df   LOOP  postpone (fp) ;
  133: 
  134: previous
  135: 
  136: \ callback stuff
  137: 
  138: Variable callbacks
  139: \G link between callbacks
  140: 
  141: : callback ( -- )
  142:     Create  0 ] postpone >r also cb-decl
  143:   DOES>
  144:     Create here >r 0 , callbacks @ A, r@ callbacks !
  145:     swap postpone Literal compile, postpone EXIT
  146:     r> dup cell+ cell+ alloc-callback swap !
  147:   DOES> @ ;
  148: 
  149: : callback; ( 0 xt1 .. xtn -- )
  150:     BEGIN  over  WHILE  compile,  REPEAT
  151:     postpone r> postpone execute compile, drop
  152:     postpone EXIT postpone [ previous ; immediate
  153: 
  154: : va-ret ( xt xt -- )
  155:     Create A, A, immediate
  156:   DOES> 2@ compile, ;
  157: 
  158: : init-callbacks ( -- )
  159:     defers 'cold  callbacks 1 cells -
  160:     BEGIN  cell+ @ dup  WHILE  dup cell+ cell+ alloc-callback over !
  161:     REPEAT  drop ;
  162: 
  163: ' init-callbacks IS 'cold
  164: 
  165: also cb-decl definitions
  166: 
  167: \ arguments
  168: 
  169: ' va-arg-int      Alias int
  170: ' va-arg-float    Alias sf
  171: ' va-arg-double   Alias df
  172: ' va-arg-longlong Alias llong
  173: ' va-arg-ptr      Alias ptr
  174: 
  175: ' va-return-void     ' va-start-void     va-ret (void)
  176: ' va-return-int      ' va-start-int      va-ret (int)
  177: ' va-return-float    ' va-start-float    va-ret (sf)
  178: ' va-return-double   ' va-start-double   va-ret (fp)
  179: ' va-return-longlong ' va-start-longlong va-ret (llong)
  180: ' va-return-ptr      ' va-start-ptr      va-ret (ptr)
  181: 
  182: previous definitions
  183: 
  184: \ testing stuff
  185: 
  186: [ifdef] testing
  187: 
  188: library libc /lib/libc.so.6
  189:                 
  190: libc sleep int (int) sleep
  191: libc open  int int ptr (int) open
  192: libc lseek int llong int (llong) lseek
  193: libc read  int ptr int (int) read
  194: libc close int (int) close
  195: 
  196: library libm /lib/libm.so.6
  197: 
  198: libm fmodf sf sf (sf) fmodf
  199: libm fmod  df df (fp) fmod
  200: 
  201: callback wincall (int) int int int int callback;
  202: 
  203: :noname ( a b c d -- e )  2drop 2drop 0 ; wincall do_timer
  204:     
  205: [then]    

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