[gforth] / gforth / fflib.fs  

gforth: gforth/fflib.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help