[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.13 \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005 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 :     \ 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 : pazsan 1.4 \ links between libraries
23 : pazsan 1.1 Variable thisproc
24 :     Variable thislib
25 : pazsan 1.4
26 : pazsan 1.2 Variable revdec revdec off
27 :     \ turn revdec on to compile bigFORTH libraries
28 : pazsan 1.4 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 : pazsan 1.2
33 :     Vocabulary c-decl
34 :     Vocabulary cb-decl
35 : pazsan 1.1
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 : pazsan 1.4 Defer legacy-proc ' noop IS legacy-proc
58 :    
59 : pazsan 1.1 : proc: ( lib "name" -- )
60 : pazsan 1.4 \G Creates a named proc stub
61 : pazsan 1.2 Create proc, 0 also c-decl
62 : pazsan 1.4 legacy @ IF legacy-proc THEN
63 : pazsan 1.1 DOES> ( x1 .. xn -- r )
64 :     dup cell+ @ swap 3 cells + >r ;
65 :    
66 : pazsan 1.11 Variable ind-call ind-call off
67 : pazsan 1.12 : fptr ( "name" -- )
68 : pazsan 1.11 Create here thisproc ! 0 , 0 , 0 , 0 also c-decl ind-call on
69 :     DOES> 3 cells + >r ;
70 :    
71 : pazsan 1.1 : library ( "name" "file" -- )
72 : pazsan 1.4 \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 : pazsan 1.1 Create here libs @ A, dup libs !
79 : pazsan 1.6 0 , 0 A, parse-name string, @lib
80 : pazsan 1.1 DOES> ( -- ) dup thislib ! proc: ;
81 :    
82 :     : init-shared-libs ( -- )
83 :     defers 'cold libs
84 :     0 libs BEGIN @ dup WHILE dup REPEAT drop
85 :     BEGIN dup WHILE >r
86 :     r@ @lib
87 :     r@ 2 cells + BEGIN @ dup WHILE r@ over @proc REPEAT
88 :     drop rdrop
89 :     REPEAT drop ;
90 :    
91 :     ' init-shared-libs IS 'cold
92 :    
93 : pazsan 1.4 : argtype ( revxt pushxt fwxt "name" -- )
94 :     Create , , , ;
95 :    
96 :     : arg@ ( arg -- argxt pushxt )
97 :     revarg @ IF 2 cells + @ ['] noop swap ELSE 2@ THEN ;
98 :    
99 :     : arg, ( xt -- )
100 :     dup ['] noop = IF drop EXIT THEN compile, ;
101 :    
102 :     : decl, ( 0 arg1 .. argn call start -- )
103 : pazsan 1.1 2@ compile, >r
104 : pazsan 1.2 revdec @ IF 0 >r
105 : pazsan 1.4 BEGIN dup WHILE >r REPEAT
106 :     BEGIN r> dup WHILE arg@ arg, REPEAT drop
107 :     BEGIN dup WHILE arg, REPEAT drop
108 :     ELSE 0 >r
109 :     BEGIN dup WHILE arg@ arg, >r REPEAT drop
110 :     BEGIN r> dup WHILE arg, REPEAT drop
111 : pazsan 1.1 THEN
112 : pazsan 1.4 r> compile, postpone EXIT ;
113 :    
114 :     : symbol, ( "c-symbol" -- )
115 : pazsan 1.6 here thisproc @ 2 cells + ! parse-name s,
116 : pazsan 1.4 thislib @ thisproc @ @proc ;
117 :    
118 :     : rettype ( endxt startxt "name" -- )
119 :     Create 2,
120 : pazsan 1.11 DOES> decl, ind-call @ 0= IF symbol, THEN
121 :     previous revarg off ind-call off ;
122 : pazsan 1.2
123 :     also c-decl definitions
124 :    
125 : pazsan 1.4 : <rev> revarg on ;
126 :    
127 :     ' av-int ' av-int-r ' >r argtype int
128 :     ' av-float ' av-float-r ' f>l argtype sf
129 :     ' av-double ' av-double-r ' f>l argtype df
130 : pazsan 1.14 ' av-longlong ' av-longlong-r ' 2>r argtype dlong
131 : pazsan 1.4 ' av-ptr ' av-ptr-r ' >r argtype ptr
132 :    
133 :     ' av-call-void ' av-start-void rettype (void)
134 :     ' av-call-int ' av-start-int rettype (int)
135 :     ' av-call-float ' av-start-float rettype (sf)
136 :     ' av-call-double ' av-start-double rettype (fp)
137 : pazsan 1.14 ' av-call-longlong ' av-start-longlong rettype (dlong)
138 : pazsan 1.4 ' av-call-ptr ' av-start-ptr rettype (ptr)
139 : pazsan 1.1
140 : pazsan 1.10 : (addr) postpone EXIT drop symbol, previous revarg off ;
141 : pazsan 1.8
142 : pazsan 1.2 previous definitions
143 : pazsan 1.1
144 : pazsan 1.4 \ legacy support for old library interfaces
145 :     \ interface to old vararg stuff not implemented yet
146 : pazsan 1.1
147 : pazsan 1.2 also c-decl
148 : pazsan 1.1
149 : pazsan 1.4 :noname ( n 0 -- 0 int1 .. intn )
150 :     legacy @ 0< revarg !
151 :     swap 0 ?DO int LOOP (int)
152 :     ; IS legacy-proc
153 :    
154 : pazsan 1.1 : (int) ( n -- )
155 : pazsan 1.4 >r ' execute r> 0 ?DO int LOOP (int) ;
156 : pazsan 1.1 : (void) ( n -- )
157 : pazsan 1.4 >r ' execute r> 0 ?DO int LOOP (void) ;
158 : pazsan 1.1 : (float) ( n -- )
159 : pazsan 1.4 >r ' execute r> 0 ?DO df LOOP (fp) ;
160 : pazsan 1.2
161 :     previous
162 :    
163 :     \ callback stuff
164 :    
165 :     Variable callbacks
166 :     \G link between callbacks
167 :    
168 :     : callback ( -- )
169 :     Create 0 ] postpone >r also cb-decl
170 :     DOES>
171 :     Create here >r 0 , callbacks @ A, r@ callbacks !
172 : pazsan 1.3 swap postpone Literal postpone call , postpone EXIT
173 : pazsan 1.2 r> dup cell+ cell+ alloc-callback swap !
174 :     DOES> @ ;
175 :    
176 :     : callback; ( 0 xt1 .. xtn -- )
177 :     BEGIN over WHILE compile, REPEAT
178 :     postpone r> postpone execute compile, drop
179 :     postpone EXIT postpone [ previous ; immediate
180 :    
181 :     : va-ret ( xt xt -- )
182 :     Create A, A, immediate
183 :     DOES> 2@ compile, ;
184 :    
185 :     : init-callbacks ( -- )
186 :     defers 'cold callbacks 1 cells -
187 :     BEGIN cell+ @ dup WHILE dup cell+ cell+ alloc-callback over !
188 :     REPEAT drop ;
189 :    
190 :     ' init-callbacks IS 'cold
191 :    
192 :     also cb-decl definitions
193 :    
194 :     \ arguments
195 : pazsan 1.1
196 : pazsan 1.2 ' va-arg-int Alias int
197 :     ' va-arg-float Alias sf
198 :     ' va-arg-double Alias df
199 : pazsan 1.14 ' va-arg-longlong Alias dlong
200 : pazsan 1.2 ' va-arg-ptr Alias ptr
201 :    
202 :     ' va-return-void ' va-start-void va-ret (void)
203 :     ' va-return-int ' va-start-int va-ret (int)
204 :     ' va-return-float ' va-start-float va-ret (sf)
205 :     ' va-return-double ' va-start-double va-ret (fp)
206 : pazsan 1.14 ' va-return-longlong ' va-start-longlong va-ret (dlong)
207 : pazsan 1.2 ' va-return-ptr ' va-start-ptr va-ret (ptr)
208 :    
209 :     previous definitions

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help