Annotation of gforth/fflib.fs, revision 1.4
1.2 pazsan 1: \ lib.fs shared library support package 16aug03py
1.1 pazsan 2:
1.2 pazsan 3: \ Copyright (C) 1995,1996,1997,1998,2000,2003 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
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 !
1.4 ! pazsan 22: \ links between libraries
1.1 pazsan 23: Variable thisproc
24: Variable thislib
1.4 ! pazsan 25:
1.2 pazsan 26: Variable revdec revdec off
27: \ turn revdec on to compile bigFORTH libraries
1.4 ! pazsan 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
1.2 pazsan 32:
33: Vocabulary c-decl
34: Vocabulary cb-decl
1.1 pazsan 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:
1.4 ! pazsan 57: Defer legacy-proc ' noop IS legacy-proc
! 58:
1.1 pazsan 59: : proc: ( lib "name" -- )
1.4 ! pazsan 60: \G Creates a named proc stub
1.2 pazsan 61: Create proc, 0 also c-decl
1.4 ! pazsan 62: legacy @ IF legacy-proc THEN
1.1 pazsan 63: DOES> ( x1 .. xn -- r )
64: dup cell+ @ swap 3 cells + >r ;
65:
66: : library ( "name" "file" -- )
1.4 ! pazsan 67: \G loads library "file" and creates a proc defining word "name"
! 68: \G library format:
! 69: \G linked list of libraries
! 70: \G library handle
! 71: \G linked list of library's procs
! 72: \G OS name of library as counted string
1.1 pazsan 73: Create here libs @ A, dup libs !
74: 0 , 0 A, bl sword string, @lib
75: DOES> ( -- ) dup thislib ! proc: ;
76:
77: : init-shared-libs ( -- )
78: defers 'cold libs
79: 0 libs BEGIN @ dup WHILE dup REPEAT drop
80: BEGIN dup WHILE >r
81: r@ @lib
82: r@ 2 cells + BEGIN @ dup WHILE r@ over @proc REPEAT
83: drop rdrop
84: REPEAT drop ;
85:
86: ' init-shared-libs IS 'cold
87:
1.4 ! pazsan 88: : argtype ( revxt pushxt fwxt "name" -- )
! 89: Create , , , ;
! 90:
! 91: : arg@ ( arg -- argxt pushxt )
! 92: revarg @ IF 2 cells + @ ['] noop swap ELSE 2@ THEN ;
! 93:
! 94: : arg, ( xt -- )
! 95: dup ['] noop = IF drop EXIT THEN compile, ;
! 96:
! 97: : decl, ( 0 arg1 .. argn call start -- )
1.1 pazsan 98: 2@ compile, >r
1.2 pazsan 99: revdec @ IF 0 >r
1.4 ! pazsan 100: BEGIN dup WHILE >r REPEAT
! 101: BEGIN r> dup WHILE arg@ arg, REPEAT drop
! 102: BEGIN dup WHILE arg, REPEAT drop
! 103: ELSE 0 >r
! 104: BEGIN dup WHILE arg@ arg, >r REPEAT drop
! 105: BEGIN r> dup WHILE arg, REPEAT drop
1.1 pazsan 106: THEN
1.4 ! pazsan 107: r> compile, postpone EXIT ;
! 108:
! 109: : symbol, ( "c-symbol" -- )
1.1 pazsan 110: here thisproc @ 2 cells + ! bl sword s,
1.4 ! pazsan 111: thislib @ thisproc @ @proc ;
! 112:
! 113: : rettype ( endxt startxt "name" -- )
! 114: Create 2,
! 115: DOES> decl, symbol, previous revarg off ;
1.2 pazsan 116:
117: also c-decl definitions
118:
1.4 ! pazsan 119: : <rev> revarg on ;
! 120:
! 121: ' av-int ' av-int-r ' >r argtype int
! 122: ' av-float ' av-float-r ' f>l argtype sf
! 123: ' av-double ' av-double-r ' f>l argtype df
! 124: ' av-longlong ' av-longlong-r ' 2>r argtype llong
! 125: ' av-ptr ' av-ptr-r ' >r argtype ptr
! 126:
! 127: ' av-call-void ' av-start-void rettype (void)
! 128: ' av-call-int ' av-start-int rettype (int)
! 129: ' av-call-float ' av-start-float rettype (sf)
! 130: ' av-call-double ' av-start-double rettype (fp)
1.1 pazsan 131: ' av-call-longlong ' av-start-longlong rettype (llong)
1.4 ! pazsan 132: ' av-call-ptr ' av-start-ptr rettype (ptr)
1.1 pazsan 133:
1.2 pazsan 134: previous definitions
1.1 pazsan 135:
1.4 ! pazsan 136: \ legacy support for old library interfaces
! 137: \ interface to old vararg stuff not implemented yet
1.1 pazsan 138:
1.2 pazsan 139: also c-decl
1.1 pazsan 140:
1.4 ! pazsan 141: :noname ( n 0 -- 0 int1 .. intn )
! 142: legacy @ 0< revarg !
! 143: swap 0 ?DO int LOOP (int)
! 144: ; IS legacy-proc
! 145:
1.1 pazsan 146: : (int) ( n -- )
1.4 ! pazsan 147: >r ' execute r> 0 ?DO int LOOP (int) ;
1.1 pazsan 148: : (void) ( n -- )
1.4 ! pazsan 149: >r ' execute r> 0 ?DO int LOOP (void) ;
1.1 pazsan 150: : (float) ( n -- )
1.4 ! pazsan 151: >r ' execute r> 0 ?DO df LOOP (fp) ;
1.2 pazsan 152:
153: previous
154:
155: \ callback stuff
156:
157: Variable callbacks
158: \G link between callbacks
159:
160: : callback ( -- )
161: Create 0 ] postpone >r also cb-decl
162: DOES>
163: Create here >r 0 , callbacks @ A, r@ callbacks !
1.3 pazsan 164: swap postpone Literal postpone call , postpone EXIT
1.2 pazsan 165: r> dup cell+ cell+ alloc-callback swap !
166: DOES> @ ;
167:
168: : callback; ( 0 xt1 .. xtn -- )
169: BEGIN over WHILE compile, REPEAT
170: postpone r> postpone execute compile, drop
171: postpone EXIT postpone [ previous ; immediate
172:
173: : va-ret ( xt xt -- )
174: Create A, A, immediate
175: DOES> 2@ compile, ;
176:
177: : init-callbacks ( -- )
178: defers 'cold callbacks 1 cells -
179: BEGIN cell+ @ dup WHILE dup cell+ cell+ alloc-callback over !
180: REPEAT drop ;
181:
182: ' init-callbacks IS 'cold
183:
184: also cb-decl definitions
185:
186: \ arguments
1.1 pazsan 187:
1.2 pazsan 188: ' va-arg-int Alias int
189: ' va-arg-float Alias sf
190: ' va-arg-double Alias df
191: ' va-arg-longlong Alias llong
192: ' va-arg-ptr Alias ptr
193:
194: ' va-return-void ' va-start-void va-ret (void)
195: ' va-return-int ' va-start-int va-ret (int)
196: ' va-return-float ' va-start-float va-ret (sf)
197: ' va-return-double ' va-start-double va-ret (fp)
198: ' va-return-longlong ' va-start-longlong va-ret (llong)
199: ' va-return-ptr ' va-start-ptr va-ret (ptr)
200:
201: previous definitions
202:
203: \ testing stuff
1.1 pazsan 204:
205: [ifdef] testing
206:
1.4 ! pazsan 207: library libc libc.so.6
1.1 pazsan 208:
209: libc sleep int (int) sleep
210: libc open int int ptr (int) open
211: libc lseek int llong int (llong) lseek
212: libc read int ptr int (int) read
213: libc close int (int) close
214:
1.4 ! pazsan 215: library libm libm.so.6
1.1 pazsan 216:
217: libm fmodf sf sf (sf) fmodf
218: libm fmod df df (fp) fmod
219:
1.3 pazsan 220: \ example for a windows callback
221:
1.2 pazsan 222: callback wincall (int) int int int int callback;
223:
224: :noname ( a b c d -- e ) 2drop 2drop 0 ; wincall do_timer
1.3 pazsan 225:
226: \ test a callback
227:
228: callback 2:1 (int) int int callback;
229:
230: : cb-test ( a b -- c )
231: cr ." Testing callback"
232: cr ." arguments: " .s
233: cr ." result " + .s cr ;
234: ' cb-test 2:1 c_plus
235:
1.4 ! pazsan 236: : test c_plus av-start-int >r >r av-int-r av-int-r av-call-int ;
1.3 pazsan 237:
238: \ 3 4 test
1.4 ! pazsan 239:
! 240: \ bigFORTH legacy library test
! 241:
! 242: library libX11 libX11.so.6
! 243:
! 244: legacy on
! 245:
! 246: 1 libX11 XOpenDisplay XOpenDisplay ( name -- dpy )
! 247: 5 libX11 XInternAtoms XInternAtoms ( atoms flag count names dpy -- status )
! 248:
! 249: legacy off
1.3 pazsan 250:
1.1 pazsan 251: [then]
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>