File:  [gforth] / gforth / fflib.fs
Revision 1.17: download - view: text, annotated - select for diffs
Mon Dec 31 17:34:58 2007 UTC (16 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

    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 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: \ links between libraries
   23: Variable thisproc
   24: Variable thislib
   25: 
   26: Variable revdec  revdec off
   27: \ turn revdec on to compile bigFORTH libraries
   28: Variable revarg  revarg off
   29: \ turn revarg on to compile declarations with reverse arguments
   30: Variable legacy  legacy off
   31: \ turn legacy on to compile bigFORTH legacy libraries
   32: 
   33: Vocabulary c-decl
   34: Vocabulary cb-decl
   35: 
   36: : @lib ( lib -- )
   37:     \G obtains library handle
   38:     cell+ dup 2 cells + count open-lib
   39:     dup 0= abort" Library not found" swap ! ;
   40: 
   41: : @proc ( lib addr -- )
   42:     \G obtains symbol address
   43:     cell+ tuck cell+ @ count rot cell+ @
   44:     lib-sym  dup 0= abort" Proc not found!" swap ! ;
   45: 
   46: : proc, ( lib -- )
   47: \G allocates and initializes proc stub
   48: \G stub format:
   49: \G    linked list in library
   50: \G    address of proc
   51: \G    ptr to OS name of symbol as counted string
   52: \G    threaded code for invocation
   53:     here dup thisproc !
   54:     swap 2 cells + dup @ A, !
   55:     0 , 0 A, ;
   56: 
   57: Defer legacy-proc  ' noop IS legacy-proc
   58: 
   59: : proc:  ( lib "name" -- )
   60: \G Creates a named proc stub
   61:     Create proc, 0 also c-decl
   62:     legacy @ IF  legacy-proc  THEN
   63: DOES> ( x1 .. xn -- r )
   64:     dup cell+ @ swap 3 cells + >r ;
   65: 
   66: Variable ind-call ind-call off
   67: : fptr ( "name" -- )
   68:     Create here thisproc ! 0 , 0 , 0 ,  0 also c-decl  ind-call on
   69:     DOES>  3 cells + >r ;
   70: 
   71: : library ( "name" "file" -- )
   72: \G loads library "file" and creates a proc defining word "name"
   73: \G library format:
   74: \G    linked list of libraries
   75: \G    library handle
   76: \G    linked list of library's procs
   77: \G    OS name of library as counted string
   78:     Create  here libs @ A, dup libs !
   79:     0 , 0 A, parse-name string, @lib
   80: DOES> ( -- )  dup thislib ! proc: ;
   81: 
   82: : init-shared-libs ( -- )
   83:     defers 'cold
   84:     0  libs  BEGIN
   85: 	@ dup WHILE
   86: 	    dup  REPEAT
   87:     drop BEGIN
   88: 	dup  WHILE
   89: 	    >r
   90: 	    r@ @lib
   91: 	    r@ 2 cells +  BEGIN
   92: 		@ dup  WHILE
   93: 		    r@ over @proc  REPEAT
   94: 	    drop rdrop
   95:     REPEAT
   96:     drop ;
   97: 
   98: ' init-shared-libs IS 'cold
   99: 
  100: : argtype ( revxt pushxt fwxt "name" -- )
  101:     Create , , , ;
  102: 
  103: : arg@ ( arg -- argxt pushxt )
  104:     revarg @ IF  2 cells + @ ['] noop swap  ELSE  2@  THEN ;
  105: 
  106: : arg, ( xt -- )
  107:     dup ['] noop = IF  drop  EXIT  THEN  compile, ;
  108: 
  109: : decl, ( 0 arg1 .. argn call start -- )
  110:     2@ compile, >r
  111:     revdec @ IF  0 >r
  112: 	BEGIN  dup  WHILE  >r  REPEAT
  113: 	BEGIN  r> dup  WHILE  arg@ arg,  REPEAT  drop
  114: 	BEGIN  dup  WHILE  arg,  REPEAT drop
  115:     ELSE  0 >r
  116: 	BEGIN  dup  WHILE  arg@ arg, >r REPEAT drop
  117: 	BEGIN  r> dup  WHILE  arg,  REPEAT  drop
  118:     THEN
  119:     r> compile,  postpone EXIT ;
  120: 
  121: : symbol, ( "c-symbol" -- )
  122:     here thisproc @ 2 cells + ! parse-name s,
  123:     thislib @ thisproc @ @proc ;
  124: 
  125: : rettype ( endxt startxt "name" -- )
  126:     Create 2,
  127:   DOES>  decl, ind-call @ 0= IF  symbol,  THEN
  128:     previous revarg off ind-call off ;
  129: 
  130: also c-decl definitions
  131: 
  132: : <rev>  revarg on ;
  133: 
  134: ' av-int      ' av-int-r      ' >r  argtype int
  135: ' av-float    ' av-float-r    ' f>l argtype sf
  136: ' av-double   ' av-double-r   ' f>l argtype df
  137: ' av-longlong ' av-longlong-r ' 2>r argtype dlong
  138: ' av-ptr      ' av-ptr-r      ' >r  argtype ptr
  139: 
  140: ' av-call-void     ' av-start-void     rettype (void)
  141: ' av-call-int      ' av-start-int      rettype (int)
  142: ' av-call-float    ' av-start-float    rettype (sf)
  143: ' av-call-double   ' av-start-double   rettype (fp)
  144: ' av-call-longlong ' av-start-longlong rettype (dlong)
  145: ' av-call-ptr      ' av-start-ptr      rettype (ptr)
  146: 
  147: : (addr)  postpone EXIT drop symbol, previous revarg off ;
  148: 
  149: previous definitions
  150: 
  151: \ legacy support for old library interfaces
  152: \ interface to old vararg stuff not implemented yet
  153: 
  154: also c-decl
  155: 
  156: :noname ( n 0 -- 0 int1 .. intn )
  157:     legacy @ 0< revarg !
  158:     swap 0 ?DO  int  LOOP  (int)
  159: ; IS legacy-proc
  160: 
  161: : (int) ( n -- )
  162:     >r ' execute r> 0 ?DO  int  LOOP  (int) ;
  163: : (void) ( n -- )
  164:     >r ' execute r> 0 ?DO  int  LOOP  (void) ;
  165: : (float) ( n -- )
  166:     >r ' execute r> 0 ?DO  df   LOOP  (fp) ;
  167: 
  168: previous
  169: 
  170: \ callback stuff
  171: 
  172: Variable callbacks
  173: \G link between callbacks
  174: 
  175: : callback ( -- )
  176:     Create  0 ] postpone >r also cb-decl
  177:   DOES>
  178:     Create here >r 0 , callbacks @ A, r@ callbacks !
  179:     swap postpone Literal postpone call , postpone EXIT
  180:     r> dup cell+ cell+ alloc-callback swap !
  181:   DOES> @ ;
  182: 
  183: : callback; ( 0 xt1 .. xtn -- )
  184:     BEGIN  over  WHILE  compile,  REPEAT
  185:     postpone r> postpone execute compile, drop
  186:     postpone EXIT postpone [ previous ; immediate
  187: 
  188: : va-ret ( xt xt -- )
  189:     Create A, A, immediate
  190:   DOES> 2@ compile, ;
  191: 
  192: : init-callbacks ( -- )
  193:     defers 'cold  callbacks 1 cells -
  194:     BEGIN  cell+ @ dup  WHILE  dup cell+ cell+ alloc-callback over !
  195:     REPEAT  drop ;
  196: 
  197: ' init-callbacks IS 'cold
  198: 
  199: also cb-decl definitions
  200: 
  201: \ arguments
  202: 
  203: ' va-arg-int      Alias int
  204: ' va-arg-float    Alias sf
  205: ' va-arg-double   Alias df
  206: ' va-arg-longlong Alias dlong
  207: ' va-arg-ptr      Alias ptr
  208: 
  209: ' va-return-void     ' va-start-void     va-ret (void)
  210: ' va-return-int      ' va-start-int      va-ret (int)
  211: ' va-return-float    ' va-start-float    va-ret (sf)
  212: ' va-return-double   ' va-start-double   va-ret (fp)
  213: ' va-return-longlong ' va-start-longlong va-ret (dlong)
  214: ' va-return-ptr      ' va-start-ptr      va-ret (ptr)
  215: 
  216: previous definitions

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