[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 : anton 1.20 \ Todo: conversion between function pointers and xts (both directions)
77 :    
78 :     \ taking an xt and turning it into a function pointer:
79 :    
80 :     \ e.g., assume we have the xt of + and want to create a C function int
81 :     \ gforth_callback_plus(int, int), and then pass the pointer to that
82 :     \ function:
83 :    
84 :     \ There should be Forth code like this:
85 :     \ ] + 0 (bye)
86 :     \ Assume that the start of this code is START
87 :    
88 :     \ Now, there should be a C function:
89 :    
90 :     \ int gforth_callback_plus(int p1, int p2)
91 :     \ {
92 :     \ Cell *sp = gforth_SP;
93 :     \ Float *fp = gforth_FP;
94 :     \ Float *fp = gforth_FP;
95 :     \ Address lp = gforth_LP;
96 :     \ sp -= 2;
97 :     \ sp[0] = p1;
98 :     \ sp[1] = p2;
99 :     \ gforth_engine(START, sp, rp, fp, lp);
100 :     \ sp += 1;
101 :     \ gforth_RP = rp;
102 :     \ gforth_SP = sp;
103 :     \ gforth_FP = fp;
104 :     \ gforth_LP = lp;
105 :     \ return sp[0];
106 :     \ }
107 :    
108 :     \ and the pointer to that function is the C function pointer for the XT of +.
109 :    
110 :     \ Future problems:
111 :     \ how to combine the Forth code generation with inlining
112 :     \ START is not a constant across executions (when caching the C files)
113 :     \ Solution: make START a variable, and store into it on startup with dlsym
114 :    
115 :     \ Syntax:
116 :     \ callback <rettype> <params> <paramtypes> -- <rettype>
117 :    
118 : anton 1.2
119 :     \ data structures
120 :    
121 : anton 1.10 \ For every c-function, we have three words: two anonymous words
122 :     \ created by c-function-ft (first time) and c-function-rt (run-time),
123 :     \ and a named deferred word. The deferred word first points to the
124 :     \ first-time word, then to the run-time word; the run-time word calls
125 :     \ the c function.
126 :    
127 : anton 1.12
128 :     require struct.fs
129 :    
130 :     \ counted-string
131 :    
132 : anton 1.10 \ c-function-ft word body:
133 : anton 1.12 struct
134 :     cell% field cff-cfr \ xt of c-function-rt word
135 :     cell% field cff-deferred \ xt of c-function deferred word
136 :     cell% field cff-lha \ address of the lib-handle for the lib that
137 :     \ contains the wrapper function of the word
138 :     char% field cff-rtype \ return type
139 :     char% field cff-np \ number of parameters
140 :     1 0 field cff-ptypes \ #npar parameter types
141 :     \ counted string: c-name
142 :     end-struct cff%
143 :    
144 : anton 1.14 variable c-source-file-id \ contains the source file id of the current batch
145 :     0 c-source-file-id !
146 :     variable lib-handle-addr \ points to the library handle of the current batch.
147 :     \ the library handle is 0 if the current
148 :     \ batch is not yet compiled.
149 : anton 1.15 2variable lib-filename \ filename without extension
150 : anton 1.2
151 : anton 1.3 : .nb ( n -- )
152 : anton 1.2 0 .r ;
153 :    
154 :     : const+ ( n1 "name" -- n2 )
155 :     dup constant 1+ ;
156 :    
157 : anton 1.10 : front-string { c-addr1 u1 c-addr2 u2 -- c-addr3 u3 }
158 :     \ insert string c-addr2 u2 in buffer c-addr1 u1; c-addr3 u3 is the
159 :     \ remainder of the buffer.
160 :     assert( u1 u2 u>= )
161 :     c-addr2 c-addr1 u2 move
162 :     c-addr1 u1 u2 /string ;
163 :    
164 :     : front-char { c-addr1 u1 c -- c-addr3 u2 }
165 :     \ insert c in buffer c-addr1 u1; c-addr3 u3 is the remainder of
166 :     \ the buffer.
167 :     assert( u1 0 u> )
168 :     c c-addr1 c!
169 :     c-addr1 u1 1 /string ;
170 :    
171 : anton 1.15 : s+ { addr1 u1 addr2 u2 -- addr u }
172 :     u1 u2 + allocate throw { addr }
173 :     addr1 addr u1 move
174 :     addr2 addr u1 + u2 move
175 :     addr u1 u2 +
176 :     ;
177 :    
178 :     : append { addr1 u1 addr2 u2 -- addr u }
179 :     addr1 u1 u2 + dup { u } resize throw { addr }
180 :     addr2 addr u1 + u2 move
181 :     addr u ;
182 :    
183 : anton 1.6 \ linked list stuff (should go elsewhere)
184 :    
185 :     struct
186 :     cell% field list-next
187 :     1 0 field list-payload
188 :     end-struct list%
189 :    
190 :     : list-insert { node list -- }
191 :     list list-next @ node list-next !
192 :     node list list-next ! ;
193 :    
194 :     : list-append { node endlistp -- }
195 :     \ insert node at place pointed to by endlistp
196 :     node endlistp @ list-insert
197 :     node list-next endlistp ! ;
198 :    
199 :     : list-map ( ... list xt -- ... )
200 :     \ xt ( ... node -- ... )
201 :     { xt } begin { node }
202 :     node while
203 :     node xt execute
204 :     node list-next @
205 :     repeat ;
206 :    
207 :     \ C prefix lines
208 :    
209 :     \ linked list of longcstrings: [ link | count-cell | characters ]
210 :    
211 :     list%
212 :     cell% field c-prefix-count
213 :     1 0 field c-prefix-chars
214 :     end-struct c-prefix%
215 :    
216 :     variable c-prefix-lines 0 c-prefix-lines !
217 :     variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
218 :    
219 : anton 1.14 : print-c-prefix-line ( node -- )
220 :     dup c-prefix-chars swap c-prefix-count @ type cr ;
221 :    
222 :     : print-c-prefix-lines ( -- )
223 :     c-prefix-lines @ ['] print-c-prefix-line list-map ;
224 :    
225 : anton 1.6 : save-c-prefix-line ( c-addr u -- )
226 : anton 1.14 c-source-file-id @ ?dup-if
227 :     >r 2dup r> write-line throw
228 :     then
229 : anton 1.6 align here 0 , c-prefix-lines-end list-append ( c-addr u )
230 :     longstring, ;
231 :    
232 : anton 1.18 : \c ( "rest-of-line" -- ) \ gforth backslash-c
233 : anton 1.17 \G One line of C declarations for the C interface
234 : anton 1.6 -1 parse save-c-prefix-line ;
235 :    
236 : anton 1.19 s" #include <gforth/" version-string s+ s" /libcc.h>" append ( c-addr u )
237 :     2dup save-c-prefix-line drop free throw
238 : anton 1.5
239 : anton 1.6 \ Types (for parsing)
240 : anton 1.5
241 : anton 1.2 wordlist constant libcc-types
242 :    
243 :     get-current libcc-types set-current
244 :    
245 :     \ index values
246 :     -1
247 :     const+ -- \ end of arguments
248 :     const+ n \ integer cell
249 : anton 1.5 const+ a \ address cell
250 : anton 1.2 const+ d \ double
251 :     const+ r \ float
252 :     const+ func \ C function pointer
253 :     const+ void
254 :     drop
255 :    
256 :     set-current
257 :    
258 :     : parse-libcc-type ( "libcc-type" -- u )
259 :     parse-name libcc-types search-wordlist 0= -13 and throw execute ;
260 :    
261 :     : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
262 :     here 2 chars allot here begin
263 :     parse-libcc-type dup 0>= while
264 :     c,
265 :     repeat
266 : anton 1.3 drop here swap - over char+ c!
267 :     parse-libcc-type dup 0< -32 and throw swap c! ;
268 : anton 1.2
269 :     : type-letter ( n -- c )
270 : anton 1.5 chars s" nadrfv" drop + c@ ;
271 : anton 1.2
272 :     \ count-stacks
273 :    
274 :     : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
275 :     1+ ;
276 :    
277 : anton 1.5 : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
278 : anton 1.2 1+ ;
279 :    
280 :     : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
281 :     2 + ;
282 :    
283 :     : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
284 :     swap 1+ swap ;
285 :    
286 :     : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
287 :     1+ ;
288 :    
289 :     : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
290 :     ;
291 :    
292 :     create count-stacks-types
293 :     ' count-stacks-n ,
294 : anton 1.5 ' count-stacks-a ,
295 : anton 1.2 ' count-stacks-d ,
296 :     ' count-stacks-r ,
297 :     ' count-stacks-func ,
298 :     ' count-stacks-void ,
299 :    
300 :     : count-stacks ( pars -- fp-change sp-change )
301 :     \ pars is an addr u pair
302 :     0 0 2swap over + swap u+do
303 : anton 1.3 i c@ cells count-stacks-types + @ execute
304 : anton 1.2 loop ;
305 :    
306 :     \ gen-pars
307 :    
308 :     : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
309 : anton 1.5 ." sp[" 1- dup .nb ." ]" ;
310 : anton 1.2
311 : anton 1.5 : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
312 : anton 1.2 ." (void *)(" gen-par-n ." )" ;
313 :    
314 :     : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
315 : anton 1.4 ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
316 : anton 1.2
317 :     : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
318 : anton 1.3 swap 1- tuck ." fp[" .nb ." ]" ;
319 : anton 1.2
320 :     : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
321 : anton 1.5 gen-par-a ;
322 : anton 1.2
323 :     : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
324 :     -32 throw ;
325 :    
326 :     create gen-par-types
327 :     ' gen-par-n ,
328 : anton 1.5 ' gen-par-a ,
329 : anton 1.2 ' gen-par-d ,
330 :     ' gen-par-r ,
331 :     ' gen-par-func ,
332 :     ' gen-par-void ,
333 :    
334 :     : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
335 : anton 1.3 cells gen-par-types + @ execute ;
336 : anton 1.2
337 :     \ the call itself
338 :    
339 :     : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
340 :     c-name type ." ("
341 : anton 1.3 fp-change1 sp-change1 pars over + swap u+do
342 : anton 1.2 i c@ gen-par
343 :     i 1+ i' < if
344 :     ." ,"
345 :     endif
346 :     loop
347 :     2drop ." )" ;
348 :    
349 :     \ calls for various kinds of return values
350 :    
351 :     : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
352 :     2dup 2>r gen-wrapped-call 2r> ;
353 :    
354 : anton 1.3 : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
355 : anton 1.5 2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
356 : anton 1.3
357 : anton 1.5 : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
358 :     2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
359 : anton 1.3
360 :     : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
361 :     ." gforth_ll2d(" gen-wrapped-void
362 : anton 1.5 ." ," gen-par-n ." ," gen-par-n ." )" ;
363 : anton 1.3
364 :     : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
365 : anton 1.5 2dup gen-par-r 2>r ." =" gen-wrapped-void 2r> ;
366 : anton 1.3
367 :     : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
368 : anton 1.5 gen-wrapped-a ;
369 : anton 1.3
370 : anton 1.2 create gen-wrapped-types
371 :     ' gen-wrapped-n ,
372 : anton 1.5 ' gen-wrapped-a ,
373 : anton 1.2 ' gen-wrapped-d ,
374 :     ' gen-wrapped-r ,
375 :     ' gen-wrapped-func ,
376 :     ' gen-wrapped-void ,
377 :    
378 :     : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
379 : anton 1.3 cells gen-wrapped-types + @ execute ;
380 : anton 1.2
381 : anton 1.10 : wrapper-function-name ( addr -- c-addr u )
382 :     \ addr points to the return type index of a c-function descriptor
383 :     count { r-type } count { d: pars }
384 :     pars + count { d: c-name }
385 :     s" gforth_c_" { d: prefix }
386 :     prefix nip c-name nip + pars nip + 3 + { u }
387 :     u allocate throw { c-addr }
388 :     c-addr u
389 :     prefix front-string c-name front-string '_ front-char
390 :     pars bounds u+do
391 :     i c@ type-letter front-char
392 :     loop
393 :     '_ front-char r-type type-letter front-char assert( dup 0= )
394 :     2drop c-addr u ;
395 :    
396 : anton 1.2 : gen-wrapper-function ( addr -- )
397 :     \ addr points to the return type index of a c-function descriptor
398 : anton 1.10 dup { descriptor }
399 : anton 1.13 count { ret } count 2dup { d: pars } chars + count { d: c-name }
400 : anton 1.10 ." void " descriptor wrapper-function-name 2dup type drop free throw
401 :     .\" (void)\n"
402 : anton 1.4 .\" {\n Cell MAYBE_UNUSED *sp = gforth_SP;\n Float MAYBE_UNUSED *fp = gforth_FP;\n "
403 : anton 1.2 pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
404 :     ?dup-if
405 : anton 1.3 ." gforth_SP = sp+" .nb .\" ;\n"
406 : anton 1.2 endif
407 :     ?dup-if
408 : anton 1.3 ." gforth_FP = fp+" .nb .\" ;\n"
409 : anton 1.2 endif
410 : anton 1.3 .\" }\n" ;
411 : anton 1.2
412 : anton 1.16 : tempdir ( -- c-addr u )
413 :     s" TMPDIR" getenv dup 0= if
414 :     2drop s" /tmp"
415 :     then ;
416 :    
417 : anton 1.15 : gen-filename ( x -- c-addr u )
418 :     \ generates a filename without extension for lib-handle-addr X
419 : anton 1.16 0 <<# ['] #s $10 base-execute #>
420 :     tempdir s" /gforth-c-" s+ 2swap append #>> ;
421 : anton 1.15
422 : anton 1.12 : init-c-source-file ( -- )
423 :     c-source-file-id @ 0= if
424 : anton 1.15 here 0 , dup lib-handle-addr ! gen-filename 2dup lib-filename 2!
425 :     s" .c" s+ 2dup w/o create-file throw dup c-source-file-id !
426 :     ['] print-c-prefix-lines swap outfile-execute
427 :     drop free throw
428 : anton 1.12 endif ;
429 : anton 1.11
430 :     : c-source-file ( -- file-id )
431 : anton 1.12 c-source-file-id @ assert( dup ) ;
432 : anton 1.11
433 : anton 1.5 : compile-wrapper-function ( -- )
434 : anton 1.12 c-source-file close-file throw
435 :     0 c-source-file-id !
436 : anton 1.19 s" gcc -fPIC -shared -Wl,-soname," lib-filename 2@ s+
437 : anton 1.15 s" .so.1 -Wl,-export_dynamic -o " append lib-filename 2@ append
438 : anton 1.19 [ s" .so.1 -O -I " s" includedir" getenv append s" " append ] sliteral
439 :     append lib-filename 2@ append s" .c" append ( c-addr u )
440 : anton 1.15 2dup system drop free throw
441 :     $? abort" compiler generated error" \ !! call dlerror
442 : anton 1.16 lib-filename 2@ s" .so.1" s+
443 : anton 1.15 2dup open-lib dup 0= abort" open-lib failed" \ !! call dlerror
444 :     ( lib-handle ) lib-handle-addr @ !
445 :     2dup delete-file throw drop free throw
446 :     lib-filename 2@ s" .c" s+ 2dup delete-file throw drop free throw
447 :     lib-filename 2@ drop free throw 0 0 lib-filename 2! ;
448 : anton 1.5 \ s" ar rcs xxx.a xxx.o" system
449 :     \ $? abort" ar generated error" ;
450 :    
451 : anton 1.12 : link-wrapper-function { cff -- sym }
452 :     cff cff-rtype wrapper-function-name { d: wrapper-name }
453 :     wrapper-name cff cff-lha @ @ assert( dup ) lib-sym dup 0= -&32 and throw
454 : anton 1.10 wrapper-name drop free throw ;
455 : anton 1.8
456 : anton 1.12 : c-function-ft ( xt-defr xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
457 : anton 1.8 \ build time/first time action for c-function
458 : anton 1.12 init-c-source-file
459 :     noname create 2, lib-handle-addr @ ,
460 : anton 1.2 parse-name { d: c-name }
461 : anton 1.8 here parse-function-types c-name string,
462 : anton 1.11 ['] gen-wrapper-function c-source-file outfile-execute
463 : anton 1.8 does> ( ... -- ... )
464 : anton 1.10 dup 2@ { xt-defer xt-cfr }
465 : anton 1.12 dup cff-lha @ @ 0= if
466 :     compile-wrapper-function
467 :     endif
468 :     link-wrapper-function xt-cfr >body !
469 : anton 1.8 xt-cfr xt-defer defer!
470 :     xt-cfr execute ;
471 :    
472 :     : c-function-rt ( -- )
473 :     \ run-time definition for c function; addr is the address where
474 :     \ the sym should be stored
475 :     noname create 0 ,
476 : anton 1.2 does> ( ... -- ... )
477 :     @ call-c ;
478 :    
479 : anton 1.18 : c-function ( "forth-name" "c-name" "@{type@}" "--" "type" -- ) \ gforth
480 : anton 1.17 \G Define a Forth word @i{forth-name}. @i{Forth-name} has the
481 :     \G specified stack effect and calls the C function @code{c-name}.
482 : anton 1.8 defer lastxt dup c-function-rt lastxt c-function-ft
483 :     lastxt swap defer! ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help