[gforth] / gforth / libcc.fs  

gforth: gforth/libcc.fs


1 : anton 1.1 \ libcc.fs foreign function interface implemented using a C compiler
2 :    
3 :     \ Copyright (C) 2006 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 :    
22 :     \ What this implementation does is this: if it sees a declaration like
23 :    
24 : anton 1.2 \ \ something that tells it that the current library is libc
25 : anton 1.6 \ \c #include <unistd.h>
26 : anton 1.2 \ c-function dlseek lseek n d n -- d
27 : anton 1.1
28 :     \ it genererates C code similar to the following:
29 :    
30 :     \ #include <gforth.h>
31 : anton 1.2 \ #include <unistd.h>
32 : anton 1.1 \
33 : anton 1.2 \ void gforth_c_lseek_ndn_d(void)
34 : anton 1.1 \ {
35 :     \ Cell *sp = gforth_SP;
36 :     \ Float *fp = gforth_FP;
37 : anton 1.2 \ long long result; /* longest type in C */
38 :     \ gforth_ll2d(lseek(sp[3],gforth_d2ll(sp[2],sp[1]),sp[0]),sp[3],sp[2]);
39 :     \ gforth_SP = sp+2;
40 : anton 1.1 \ }
41 :    
42 :     \ Then it compiles this code and dynamically links it into the Gforth
43 :     \ system (batching and caching are future work). It also dynamically
44 :     \ links lseek. Performing DLSEEK then puts the function pointer of
45 : anton 1.2 \ the function pointer of gforth_c_lseek_ndn_d on the stack and
46 :     \ calls CALL-C.
47 :    
48 : anton 1.7 \ ToDo:
49 :    
50 :     \ Batching, caching and lazy evaluation:
51 :    
52 :     \ Batching:
53 :    
54 :     \ New words are deferred, and the corresponding C functions are
55 :     \ collected in one file, until the first word is EXECUTEd; then the
56 :     \ file is compiled and linked into the system, and the word is
57 :     \ resolved.
58 :    
59 :     \ Caching:
60 :    
61 :     \ Instead of compiling all this stuff anew for every execution, we
62 :     \ keep the files around and have an index file containing the function
63 :     \ names and their corresponding .so files. If the needed wrapper name
64 :     \ is already present, it is just linked instead of generating the
65 :     \ wrapper again. This is all done by loading the index file(s?),
66 :     \ which define words for the wrappers in a separate wordlist.
67 :    
68 :     \ The files are built in .../lib/gforth/$VERSION/libcc/ or
69 :     \ ~/.gforth/libcc/$HOST/.
70 :    
71 : anton 1.2 \ other things to do:
72 :    
73 :     \ c-variable forth-name c-name
74 :     \ c-constant forth-name c-name
75 :    
76 :    
77 :     \ data structures
78 :    
79 : anton 1.10 \ For every c-function, we have three words: two anonymous words
80 :     \ created by c-function-ft (first time) and c-function-rt (run-time),
81 :     \ and a named deferred word. The deferred word first points to the
82 :     \ first-time word, then to the run-time word; the run-time word calls
83 :     \ the c function.
84 :    
85 : anton 1.12
86 :     require struct.fs
87 :    
88 :     \ counted-string
89 :    
90 : anton 1.10 \ c-function-ft word body:
91 : anton 1.12 struct
92 :     cell% field cff-cfr \ xt of c-function-rt word
93 :     cell% field cff-deferred \ xt of c-function deferred word
94 :     cell% field cff-lha \ address of the lib-handle for the lib that
95 :     \ contains the wrapper function of the word
96 :     char% field cff-rtype \ return type
97 :     char% field cff-np \ number of parameters
98 :     1 0 field cff-ptypes \ #npar parameter types
99 :     \ counted string: c-name
100 :     end-struct cff%
101 :    
102 : anton 1.14 variable c-source-file-id \ contains the source file id of the current batch
103 :     0 c-source-file-id !
104 :     variable lib-handle-addr \ points to the library handle of the current batch.
105 :     \ the library handle is 0 if the current
106 :     \ batch is not yet compiled.
107 : anton 1.15 2variable lib-filename \ filename without extension
108 : anton 1.2
109 : anton 1.3 : .nb ( n -- )
110 : anton 1.2 0 .r ;
111 :    
112 :     : const+ ( n1 "name" -- n2 )
113 :     dup constant 1+ ;
114 :    
115 : anton 1.10 : front-string { c-addr1 u1 c-addr2 u2 -- c-addr3 u3 }
116 :     \ insert string c-addr2 u2 in buffer c-addr1 u1; c-addr3 u3 is the
117 :     \ remainder of the buffer.
118 :     assert( u1 u2 u>= )
119 :     c-addr2 c-addr1 u2 move
120 :     c-addr1 u1 u2 /string ;
121 :    
122 :     : front-char { c-addr1 u1 c -- c-addr3 u2 }
123 :     \ insert c in buffer c-addr1 u1; c-addr3 u3 is the remainder of
124 :     \ the buffer.
125 :     assert( u1 0 u> )
126 :     c c-addr1 c!
127 :     c-addr1 u1 1 /string ;
128 :    
129 : anton 1.15 : s+ { addr1 u1 addr2 u2 -- addr u }
130 :     u1 u2 + allocate throw { addr }
131 :     addr1 addr u1 move
132 :     addr2 addr u1 + u2 move
133 :     addr u1 u2 +
134 :     ;
135 :    
136 :     : append { addr1 u1 addr2 u2 -- addr u }
137 :     addr1 u1 u2 + dup { u } resize throw { addr }
138 :     addr2 addr u1 + u2 move
139 :     addr u ;
140 :    
141 : anton 1.6 \ linked list stuff (should go elsewhere)
142 :    
143 :     hex
144 :    
145 :     struct
146 :     cell% field list-next
147 :     1 0 field list-payload
148 :     end-struct list%
149 :    
150 :     : list-insert { node list -- }
151 :     list list-next @ node list-next !
152 :     node list list-next ! ;
153 :    
154 :     : list-append { node endlistp -- }
155 :     \ insert node at place pointed to by endlistp
156 :     node endlistp @ list-insert
157 :     node list-next endlistp ! ;
158 :    
159 :     : list-map ( ... list xt -- ... )
160 :     \ xt ( ... node -- ... )
161 :     { xt } begin { node }
162 :     node while
163 :     node xt execute
164 :     node list-next @
165 :     repeat ;
166 :    
167 :     \ C prefix lines
168 :    
169 :     \ linked list of longcstrings: [ link | count-cell | characters ]
170 :    
171 :     list%
172 :     cell% field c-prefix-count
173 :     1 0 field c-prefix-chars
174 :     end-struct c-prefix%
175 :    
176 :     variable c-prefix-lines 0 c-prefix-lines !
177 :     variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
178 :    
179 : anton 1.14 : print-c-prefix-line ( node -- )
180 :     dup c-prefix-chars swap c-prefix-count @ type cr ;
181 :    
182 :     : print-c-prefix-lines ( -- )
183 :     c-prefix-lines @ ['] print-c-prefix-line list-map ;
184 :    
185 : anton 1.6 : save-c-prefix-line ( c-addr u -- )
186 : anton 1.14 c-source-file-id @ ?dup-if
187 :     >r 2dup r> write-line throw
188 :     then
189 : anton 1.6 align here 0 , c-prefix-lines-end list-append ( c-addr u )
190 :     longstring, ;
191 :    
192 :     : \c ( "rest-of-line" -- )
193 :     -1 parse save-c-prefix-line ;
194 :    
195 :     \c #include "engine/libcc.h"
196 : anton 1.5
197 : anton 1.6 \ Types (for parsing)
198 : anton 1.5
199 : anton 1.2 wordlist constant libcc-types
200 :    
201 :     get-current libcc-types set-current
202 :    
203 :     \ index values
204 :     -1
205 :     const+ -- \ end of arguments
206 :     const+ n \ integer cell
207 : anton 1.5 const+ a \ address cell
208 : anton 1.2 const+ d \ double
209 :     const+ r \ float
210 :     const+ func \ C function pointer
211 :     const+ void
212 :     drop
213 :    
214 :     set-current
215 :    
216 :     : parse-libcc-type ( "libcc-type" -- u )
217 :     parse-name libcc-types search-wordlist 0= -13 and throw execute ;
218 :    
219 :     : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
220 :     here 2 chars allot here begin
221 :     parse-libcc-type dup 0>= while
222 :     c,
223 :     repeat
224 : anton 1.3 drop here swap - over char+ c!
225 :     parse-libcc-type dup 0< -32 and throw swap c! ;
226 : anton 1.2
227 :     : type-letter ( n -- c )
228 : anton 1.5 chars s" nadrfv" drop + c@ ;
229 : anton 1.2
230 :     \ count-stacks
231 :    
232 :     : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
233 :     1+ ;
234 :    
235 : anton 1.5 : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
236 : anton 1.2 1+ ;
237 :    
238 :     : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
239 :     2 + ;
240 :    
241 :     : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
242 :     swap 1+ swap ;
243 :    
244 :     : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
245 :     1+ ;
246 :    
247 :     : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
248 :     ;
249 :    
250 :     create count-stacks-types
251 :     ' count-stacks-n ,
252 : anton 1.5 ' count-stacks-a ,
253 : anton 1.2 ' count-stacks-d ,
254 :     ' count-stacks-r ,
255 :     ' count-stacks-func ,
256 :     ' count-stacks-void ,
257 :    
258 :     : count-stacks ( pars -- fp-change sp-change )
259 :     \ pars is an addr u pair
260 :     0 0 2swap over + swap u+do
261 : anton 1.3 i c@ cells count-stacks-types + @ execute
262 : anton 1.2 loop ;
263 :    
264 :     \ gen-pars
265 :    
266 :     : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
267 : anton 1.5 ." sp[" 1- dup .nb ." ]" ;
268 : anton 1.2
269 : anton 1.5 : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
270 : anton 1.2 ." (void *)(" gen-par-n ." )" ;
271 :    
272 :     : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
273 : anton 1.4 ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
274 : anton 1.2
275 :     : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
276 : anton 1.3 swap 1- tuck ." fp[" .nb ." ]" ;
277 : anton 1.2
278 :     : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
279 : anton 1.5 gen-par-a ;
280 : anton 1.2
281 :     : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
282 :     -32 throw ;
283 :    
284 :     create gen-par-types
285 :     ' gen-par-n ,
286 : anton 1.5 ' gen-par-a ,
287 : anton 1.2 ' gen-par-d ,
288 :     ' gen-par-r ,
289 :     ' gen-par-func ,
290 :     ' gen-par-void ,
291 :    
292 :     : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
293 : anton 1.3 cells gen-par-types + @ execute ;
294 : anton 1.2
295 :     \ the call itself
296 :    
297 :     : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
298 :     c-name type ." ("
299 : anton 1.3 fp-change1 sp-change1 pars over + swap u+do
300 : anton 1.2 i c@ gen-par
301 :     i 1+ i' < if
302 :     ." ,"
303 :     endif
304 :     loop
305 :     2drop ." )" ;
306 :    
307 :     \ calls for various kinds of return values
308 :    
309 :     : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
310 :     2dup 2>r gen-wrapped-call 2r> ;
311 :    
312 : anton 1.3 : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
313 : anton 1.5 2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
314 : anton 1.3
315 : anton 1.5 : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
316 :     2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
317 : anton 1.3
318 :     : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
319 :     ." gforth_ll2d(" gen-wrapped-void
320 : anton 1.5 ." ," gen-par-n ." ," gen-par-n ." )" ;
321 : anton 1.3
322 :     : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
323 : anton 1.5 2dup gen-par-r 2>r ." =" gen-wrapped-void 2r> ;
324 : anton 1.3
325 :     : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
326 : anton 1.5 gen-wrapped-a ;
327 : anton 1.3
328 : anton 1.2 create gen-wrapped-types
329 :     ' gen-wrapped-n ,
330 : anton 1.5 ' gen-wrapped-a ,
331 : anton 1.2 ' gen-wrapped-d ,
332 :     ' gen-wrapped-r ,
333 :     ' gen-wrapped-func ,
334 :     ' gen-wrapped-void ,
335 :    
336 :     : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
337 : anton 1.3 cells gen-wrapped-types + @ execute ;
338 : anton 1.2
339 : anton 1.10 : wrapper-function-name ( addr -- c-addr u )
340 :     \ addr points to the return type index of a c-function descriptor
341 :     count { r-type } count { d: pars }
342 :     pars + count { d: c-name }
343 :     s" gforth_c_" { d: prefix }
344 :     prefix nip c-name nip + pars nip + 3 + { u }
345 :     u allocate throw { c-addr }
346 :     c-addr u
347 :     prefix front-string c-name front-string '_ front-char
348 :     pars bounds u+do
349 :     i c@ type-letter front-char
350 :     loop
351 :     '_ front-char r-type type-letter front-char assert( dup 0= )
352 :     2drop c-addr u ;
353 :    
354 : anton 1.2 : gen-wrapper-function ( addr -- )
355 :     \ addr points to the return type index of a c-function descriptor
356 : anton 1.10 dup { descriptor }
357 : anton 1.13 count { ret } count 2dup { d: pars } chars + count { d: c-name }
358 : anton 1.10 ." void " descriptor wrapper-function-name 2dup type drop free throw
359 :     .\" (void)\n"
360 : anton 1.4 .\" {\n Cell MAYBE_UNUSED *sp = gforth_SP;\n Float MAYBE_UNUSED *fp = gforth_FP;\n "
361 : anton 1.2 pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
362 :     ?dup-if
363 : anton 1.3 ." gforth_SP = sp+" .nb .\" ;\n"
364 : anton 1.2 endif
365 :     ?dup-if
366 : anton 1.3 ." gforth_FP = fp+" .nb .\" ;\n"
367 : anton 1.2 endif
368 : anton 1.3 .\" }\n" ;
369 : anton 1.2
370 : anton 1.16 : tempdir ( -- c-addr u )
371 :     s" TMPDIR" getenv dup 0= if
372 :     2drop s" /tmp"
373 :     then ;
374 :    
375 : anton 1.15 : gen-filename ( x -- c-addr u )
376 :     \ generates a filename without extension for lib-handle-addr X
377 : anton 1.16 0 <<# ['] #s $10 base-execute #>
378 :     tempdir s" /gforth-c-" s+ 2swap append #>> ;
379 : anton 1.15
380 : anton 1.12 : init-c-source-file ( -- )
381 :     c-source-file-id @ 0= if
382 : anton 1.15 here 0 , dup lib-handle-addr ! gen-filename 2dup lib-filename 2!
383 :     s" .c" s+ 2dup w/o create-file throw dup c-source-file-id !
384 :     ['] print-c-prefix-lines swap outfile-execute
385 :     drop free throw
386 : anton 1.12 endif ;
387 : anton 1.11
388 :     : c-source-file ( -- file-id )
389 : anton 1.12 c-source-file-id @ assert( dup ) ;
390 : anton 1.11
391 : anton 1.5 : compile-wrapper-function ( -- )
392 : anton 1.12 c-source-file close-file throw
393 :     0 c-source-file-id !
394 : anton 1.16 s" gcc -I. -fPIC -shared -Wl,-soname," lib-filename 2@ s+
395 : anton 1.15 s" .so.1 -Wl,-export_dynamic -o " append lib-filename 2@ append
396 :     s" .so.1 -O " append lib-filename 2@ append s" .c" append ( c-addr u )
397 :     2dup system drop free throw
398 :     $? abort" compiler generated error" \ !! call dlerror
399 : anton 1.16 lib-filename 2@ s" .so.1" s+
400 : anton 1.15 2dup open-lib dup 0= abort" open-lib failed" \ !! call dlerror
401 :     ( lib-handle ) lib-handle-addr @ !
402 :     2dup delete-file throw drop free throw
403 :     lib-filename 2@ s" .c" s+ 2dup delete-file throw drop free throw
404 :     lib-filename 2@ drop free throw 0 0 lib-filename 2! ;
405 : anton 1.5 \ s" ar rcs xxx.a xxx.o" system
406 :     \ $? abort" ar generated error" ;
407 :    
408 : anton 1.12 : link-wrapper-function { cff -- sym }
409 :     cff cff-rtype wrapper-function-name { d: wrapper-name }
410 :     wrapper-name cff cff-lha @ @ assert( dup ) lib-sym dup 0= -&32 and throw
411 : anton 1.10 wrapper-name drop free throw ;
412 : anton 1.8
413 : anton 1.12 : c-function-ft ( xt-defr xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
414 : anton 1.8 \ build time/first time action for c-function
415 : anton 1.12 init-c-source-file
416 :     noname create 2, lib-handle-addr @ ,
417 : anton 1.2 parse-name { d: c-name }
418 : anton 1.8 here parse-function-types c-name string,
419 : anton 1.11 ['] gen-wrapper-function c-source-file outfile-execute
420 : anton 1.8 does> ( ... -- ... )
421 : anton 1.10 dup 2@ { xt-defer xt-cfr }
422 : anton 1.12 dup cff-lha @ @ 0= if
423 :     compile-wrapper-function
424 :     endif
425 :     link-wrapper-function xt-cfr >body !
426 : anton 1.8 xt-cfr xt-defer defer!
427 :     xt-cfr execute ;
428 :    
429 :     : c-function-rt ( -- )
430 :     \ run-time definition for c function; addr is the address where
431 :     \ the sym should be stored
432 :     noname create 0 ,
433 : anton 1.2 does> ( ... -- ... )
434 :     @ call-c ;
435 :    
436 : anton 1.8 : c-function ( "forth-name" "c-name" "{libcc-type}" "--" "libcc-type" -- )
437 :     defer lastxt dup c-function-rt lastxt c-function-ft
438 :     lastxt swap defer! ;
439 : anton 1.1
440 :     s" Library not found" exception constant err-nolib
441 :    
442 :     : library ( "name" "file" -- ) \ gforth
443 :     \G Dynamically links the library specified by @i{file}. Defines a
444 :     \G word @i{name} ( -- lib ) that starts the declaration of a
445 :     \G function from that library.
446 :     create parse-name open-lib dup 0= err-nolib and throw ,
447 :     does> ( -- lib )
448 :     @ ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help