[gforth] / gforth / libcc.fs  

gforth: gforth/libcc.fs


1 : anton 1.1 \ libcc.fs foreign function interface implemented using a C compiler
2 :    
3 : anton 1.27 \ Copyright (C) 2006,2007 Free Software Foundation, Inc.
4 : anton 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.28 \ as published by the Free Software Foundation, either version 3
10 : anton 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.28 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : anton 1.1
20 :    
21 :     \ What this implementation does is this: if it sees a declaration like
22 :    
23 : anton 1.2 \ \ something that tells it that the current library is libc
24 : anton 1.6 \ \c #include <unistd.h>
25 : anton 1.2 \ c-function dlseek lseek n d n -- d
26 : anton 1.1
27 :     \ it genererates C code similar to the following:
28 :    
29 :     \ #include <gforth.h>
30 : anton 1.2 \ #include <unistd.h>
31 : anton 1.1 \
32 : anton 1.2 \ void gforth_c_lseek_ndn_d(void)
33 : anton 1.1 \ {
34 :     \ Cell *sp = gforth_SP;
35 :     \ Float *fp = gforth_FP;
36 : anton 1.2 \ long long result; /* longest type in C */
37 :     \ gforth_ll2d(lseek(sp[3],gforth_d2ll(sp[2],sp[1]),sp[0]),sp[3],sp[2]);
38 :     \ gforth_SP = sp+2;
39 : anton 1.1 \ }
40 :    
41 :     \ Then it compiles this code and dynamically links it into the Gforth
42 :     \ system (batching and caching are future work). It also dynamically
43 :     \ links lseek. Performing DLSEEK then puts the function pointer of
44 : anton 1.2 \ the function pointer of gforth_c_lseek_ndn_d on the stack and
45 :     \ calls CALL-C.
46 :    
47 : anton 1.7 \ ToDo:
48 :    
49 :     \ Batching, caching and lazy evaluation:
50 :    
51 :     \ Batching:
52 :    
53 :     \ New words are deferred, and the corresponding C functions are
54 :     \ collected in one file, until the first word is EXECUTEd; then the
55 :     \ file is compiled and linked into the system, and the word is
56 :     \ resolved.
57 :    
58 :     \ Caching:
59 :    
60 :     \ Instead of compiling all this stuff anew for every execution, we
61 :     \ keep the files around and have an index file containing the function
62 :     \ names and their corresponding .so files. If the needed wrapper name
63 :     \ is already present, it is just linked instead of generating the
64 :     \ wrapper again. This is all done by loading the index file(s?),
65 :     \ which define words for the wrappers in a separate wordlist.
66 :    
67 :     \ The files are built in .../lib/gforth/$VERSION/libcc/ or
68 :     \ ~/.gforth/libcc/$HOST/.
69 :    
70 : anton 1.2 \ other things to do:
71 :    
72 :     \ c-variable forth-name c-name
73 :     \ c-constant forth-name c-name
74 :    
75 : anton 1.20 \ Todo: conversion between function pointers and xts (both directions)
76 :    
77 :     \ taking an xt and turning it into a function pointer:
78 :    
79 :     \ e.g., assume we have the xt of + and want to create a C function int
80 :     \ gforth_callback_plus(int, int), and then pass the pointer to that
81 :     \ function:
82 :    
83 :     \ There should be Forth code like this:
84 :     \ ] + 0 (bye)
85 :     \ Assume that the start of this code is START
86 :    
87 :     \ Now, there should be a C function:
88 :    
89 :     \ int gforth_callback_plus(int p1, int p2)
90 :     \ {
91 :     \ Cell *sp = gforth_SP;
92 :     \ Float *fp = gforth_FP;
93 :     \ Float *fp = gforth_FP;
94 :     \ Address lp = gforth_LP;
95 :     \ sp -= 2;
96 :     \ sp[0] = p1;
97 :     \ sp[1] = p2;
98 :     \ gforth_engine(START, sp, rp, fp, lp);
99 :     \ sp += 1;
100 :     \ gforth_RP = rp;
101 :     \ gforth_SP = sp;
102 :     \ gforth_FP = fp;
103 :     \ gforth_LP = lp;
104 :     \ return sp[0];
105 :     \ }
106 :    
107 :     \ and the pointer to that function is the C function pointer for the XT of +.
108 :    
109 :     \ Future problems:
110 :     \ how to combine the Forth code generation with inlining
111 :     \ START is not a constant across executions (when caching the C files)
112 :     \ Solution: make START a variable, and store into it on startup with dlsym
113 :    
114 :     \ Syntax:
115 :     \ callback <rettype> <params> <paramtypes> -- <rettype>
116 :    
117 : anton 1.2
118 :     \ data structures
119 :    
120 : anton 1.10 \ For every c-function, we have three words: two anonymous words
121 :     \ created by c-function-ft (first time) and c-function-rt (run-time),
122 :     \ and a named deferred word. The deferred word first points to the
123 :     \ first-time word, then to the run-time word; the run-time word calls
124 :     \ the c function.
125 :    
126 : anton 1.22 : delete-file 2drop 0 ;
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.23 2variable lib-filename \ filename without extension
150 :     2variable lib-modulename \ basename of the file without extension
151 : anton 1.2
152 : anton 1.3 : .nb ( n -- )
153 : anton 1.2 0 .r ;
154 :    
155 :     : const+ ( n1 "name" -- n2 )
156 :     dup constant 1+ ;
157 :    
158 : anton 1.10 : front-string { c-addr1 u1 c-addr2 u2 -- c-addr3 u3 }
159 :     \ insert string c-addr2 u2 in buffer c-addr1 u1; c-addr3 u3 is the
160 :     \ remainder of the buffer.
161 :     assert( u1 u2 u>= )
162 :     c-addr2 c-addr1 u2 move
163 :     c-addr1 u1 u2 /string ;
164 :    
165 :     : front-char { c-addr1 u1 c -- c-addr3 u2 }
166 :     \ insert c in buffer c-addr1 u1; c-addr3 u3 is the remainder of
167 :     \ the buffer.
168 :     assert( u1 0 u> )
169 :     c c-addr1 c!
170 :     c-addr1 u1 1 /string ;
171 :    
172 : anton 1.15 : s+ { addr1 u1 addr2 u2 -- addr u }
173 :     u1 u2 + allocate throw { addr }
174 :     addr1 addr u1 move
175 :     addr2 addr u1 + u2 move
176 :     addr u1 u2 +
177 :     ;
178 :    
179 :     : append { addr1 u1 addr2 u2 -- addr u }
180 :     addr1 u1 u2 + dup { u } resize throw { addr }
181 :     addr2 addr u1 + u2 move
182 :     addr u ;
183 :    
184 : anton 1.6 \ linked list stuff (should go elsewhere)
185 :    
186 :     struct
187 :     cell% field list-next
188 :     1 0 field list-payload
189 :     end-struct list%
190 :    
191 :     : list-insert { node list -- }
192 :     list list-next @ node list-next !
193 :     node list list-next ! ;
194 :    
195 :     : list-append { node endlistp -- }
196 :     \ insert node at place pointed to by endlistp
197 :     node endlistp @ list-insert
198 :     node list-next endlistp ! ;
199 :    
200 :     : list-map ( ... list xt -- ... )
201 :     \ xt ( ... node -- ... )
202 :     { xt } begin { node }
203 :     node while
204 :     node xt execute
205 :     node list-next @
206 :     repeat ;
207 :    
208 :     \ C prefix lines
209 :    
210 :     \ linked list of longcstrings: [ link | count-cell | characters ]
211 :    
212 :     list%
213 :     cell% field c-prefix-count
214 :     1 0 field c-prefix-chars
215 :     end-struct c-prefix%
216 :    
217 :     variable c-prefix-lines 0 c-prefix-lines !
218 :     variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
219 :    
220 : anton 1.14 : print-c-prefix-line ( node -- )
221 :     dup c-prefix-chars swap c-prefix-count @ type cr ;
222 :    
223 :     : print-c-prefix-lines ( -- )
224 :     c-prefix-lines @ ['] print-c-prefix-line list-map ;
225 :    
226 : anton 1.6 : save-c-prefix-line ( c-addr u -- )
227 : anton 1.14 c-source-file-id @ ?dup-if
228 :     >r 2dup r> write-line throw
229 :     then
230 : anton 1.6 align here 0 , c-prefix-lines-end list-append ( c-addr u )
231 :     longstring, ;
232 :    
233 : anton 1.18 : \c ( "rest-of-line" -- ) \ gforth backslash-c
234 : anton 1.17 \G One line of C declarations for the C interface
235 : anton 1.6 -1 parse save-c-prefix-line ;
236 :    
237 : anton 1.19 s" #include <gforth/" version-string s+ s" /libcc.h>" append ( c-addr u )
238 :     2dup save-c-prefix-line drop free throw
239 : anton 1.5
240 : anton 1.6 \ Types (for parsing)
241 : anton 1.5
242 : anton 1.2 wordlist constant libcc-types
243 :    
244 :     get-current libcc-types set-current
245 :    
246 :     \ index values
247 :     -1
248 :     const+ -- \ end of arguments
249 :     const+ n \ integer cell
250 : anton 1.5 const+ a \ address cell
251 : anton 1.2 const+ d \ double
252 :     const+ r \ float
253 :     const+ func \ C function pointer
254 :     const+ void
255 :     drop
256 :    
257 :     set-current
258 :    
259 :     : parse-libcc-type ( "libcc-type" -- u )
260 :     parse-name libcc-types search-wordlist 0= -13 and throw execute ;
261 :    
262 :     : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
263 :     here 2 chars allot here begin
264 :     parse-libcc-type dup 0>= while
265 :     c,
266 :     repeat
267 : anton 1.3 drop here swap - over char+ c!
268 :     parse-libcc-type dup 0< -32 and throw swap c! ;
269 : anton 1.2
270 :     : type-letter ( n -- c )
271 : anton 1.5 chars s" nadrfv" drop + c@ ;
272 : anton 1.2
273 :     \ count-stacks
274 :    
275 :     : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
276 :     1+ ;
277 :    
278 : anton 1.5 : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
279 : anton 1.2 1+ ;
280 :    
281 :     : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
282 :     2 + ;
283 :    
284 :     : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
285 :     swap 1+ swap ;
286 :    
287 :     : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
288 :     1+ ;
289 :    
290 :     : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
291 :     ;
292 :    
293 :     create count-stacks-types
294 :     ' count-stacks-n ,
295 : anton 1.5 ' count-stacks-a ,
296 : anton 1.2 ' count-stacks-d ,
297 :     ' count-stacks-r ,
298 :     ' count-stacks-func ,
299 :     ' count-stacks-void ,
300 :    
301 :     : count-stacks ( pars -- fp-change sp-change )
302 :     \ pars is an addr u pair
303 :     0 0 2swap over + swap u+do
304 : anton 1.3 i c@ cells count-stacks-types + @ execute
305 : anton 1.2 loop ;
306 :    
307 :     \ gen-pars
308 :    
309 :     : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
310 : anton 1.5 ." sp[" 1- dup .nb ." ]" ;
311 : anton 1.2
312 : anton 1.5 : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
313 : anton 1.2 ." (void *)(" gen-par-n ." )" ;
314 :    
315 :     : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
316 : anton 1.4 ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
317 : anton 1.2
318 :     : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
319 : anton 1.3 swap 1- tuck ." fp[" .nb ." ]" ;
320 : anton 1.2
321 :     : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
322 : anton 1.5 gen-par-a ;
323 : anton 1.2
324 :     : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
325 :     -32 throw ;
326 :    
327 :     create gen-par-types
328 :     ' gen-par-n ,
329 : anton 1.5 ' gen-par-a ,
330 : anton 1.2 ' gen-par-d ,
331 :     ' gen-par-r ,
332 :     ' gen-par-func ,
333 :     ' gen-par-void ,
334 :    
335 :     : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
336 : anton 1.3 cells gen-par-types + @ execute ;
337 : anton 1.2
338 :     \ the call itself
339 :    
340 :     : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
341 :     c-name type ." ("
342 : anton 1.3 fp-change1 sp-change1 pars over + swap u+do
343 : anton 1.2 i c@ gen-par
344 :     i 1+ i' < if
345 :     ." ,"
346 :     endif
347 :     loop
348 :     2drop ." )" ;
349 :    
350 :     \ calls for various kinds of return values
351 :    
352 :     : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
353 :     2dup 2>r gen-wrapped-call 2r> ;
354 :    
355 : anton 1.3 : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
356 : anton 1.5 2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
357 : anton 1.3
358 : anton 1.5 : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
359 :     2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
360 : anton 1.3
361 :     : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
362 :     ." gforth_ll2d(" gen-wrapped-void
363 : anton 1.5 ." ," gen-par-n ." ," gen-par-n ." )" ;
364 : anton 1.3
365 :     : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
366 : anton 1.5 2dup gen-par-r 2>r ." =" gen-wrapped-void 2r> ;
367 : anton 1.3
368 :     : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
369 : anton 1.5 gen-wrapped-a ;
370 : anton 1.3
371 : anton 1.2 create gen-wrapped-types
372 :     ' gen-wrapped-n ,
373 : anton 1.5 ' gen-wrapped-a ,
374 : anton 1.2 ' gen-wrapped-d ,
375 :     ' gen-wrapped-r ,
376 :     ' gen-wrapped-func ,
377 :     ' gen-wrapped-void ,
378 :    
379 :     : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
380 : anton 1.3 cells gen-wrapped-types + @ execute ;
381 : anton 1.2
382 : anton 1.10 : wrapper-function-name ( addr -- c-addr u )
383 :     \ addr points to the return type index of a c-function descriptor
384 :     count { r-type } count { d: pars }
385 :     pars + count { d: c-name }
386 :     s" gforth_c_" { d: prefix }
387 :     prefix nip c-name nip + pars nip + 3 + { u }
388 :     u allocate throw { c-addr }
389 :     c-addr u
390 :     prefix front-string c-name front-string '_ front-char
391 :     pars bounds u+do
392 :     i c@ type-letter front-char
393 :     loop
394 :     '_ front-char r-type type-letter front-char assert( dup 0= )
395 :     2drop c-addr u ;
396 :    
397 : anton 1.2 : gen-wrapper-function ( addr -- )
398 :     \ addr points to the return type index of a c-function descriptor
399 : anton 1.10 dup { descriptor }
400 : anton 1.13 count { ret } count 2dup { d: pars } chars + count { d: c-name }
401 : anton 1.23 ." void " lib-modulename 2@ type ." _LTX_" descriptor wrapper-function-name 2dup type drop free throw
402 : anton 1.10 .\" (void)\n"
403 : anton 1.4 .\" {\n Cell MAYBE_UNUSED *sp = gforth_SP;\n Float MAYBE_UNUSED *fp = gforth_FP;\n "
404 : anton 1.2 pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
405 :     ?dup-if
406 : anton 1.3 ." gforth_SP = sp+" .nb .\" ;\n"
407 : anton 1.2 endif
408 :     ?dup-if
409 : anton 1.3 ." gforth_FP = fp+" .nb .\" ;\n"
410 : anton 1.2 endif
411 : anton 1.3 .\" }\n" ;
412 : anton 1.2
413 : anton 1.16 : tempdir ( -- c-addr u )
414 :     s" TMPDIR" getenv dup 0= if
415 :     2drop s" /tmp"
416 :     then ;
417 :    
418 : anton 1.15 : gen-filename ( x -- c-addr u )
419 :     \ generates a filename without extension for lib-handle-addr X
420 : anton 1.16 0 <<# ['] #s $10 base-execute #>
421 : anton 1.23 tempdir s" /gforth_c_" s+ 2swap append #>> ;
422 : anton 1.15
423 : anton 1.12 : init-c-source-file ( -- )
424 :     c-source-file-id @ 0= if
425 : anton 1.23 here 0 , dup lib-handle-addr ! gen-filename 2dup lib-filename 2!
426 :     2dup tempdir nip 1+ /string lib-modulename 2!
427 : anton 1.15 s" .c" s+ 2dup w/o create-file throw dup c-source-file-id !
428 :     ['] print-c-prefix-lines swap outfile-execute
429 :     drop free throw
430 : anton 1.12 endif ;
431 : anton 1.11
432 :     : c-source-file ( -- file-id )
433 : anton 1.12 c-source-file-id @ assert( dup ) ;
434 : anton 1.11
435 : anton 1.24 : .lib-error ( -- )
436 :     [ifdef] lib-error
437 :     ['] cr stderr outfile-execute
438 :     lib-error ['] type outfile-execute
439 :     [then] ;
440 : anton 1.23
441 : anton 1.21 DEFER compile-wrapper-function
442 :     :NONAME ( -- )
443 : anton 1.12 c-source-file close-file throw
444 :     0 c-source-file-id !
445 : anton 1.25 [ libtool-command s" --silent --mode=compile gcc -I " s+
446 :     s" includedir" getenv append ] sliteral
447 : anton 1.23 s" -O -c " s+ lib-filename 2@ append s" .c -o " append
448 :     lib-filename 2@ append s" .lo" append ( c-addr u )
449 :     2dup system drop free throw $? abort" libtool compile failed"
450 : anton 1.25 [ libtool-command s" --silent --mode=link gcc -module -rpath " s+ ] sliteral
451 :     tempdir s+ s" " append
452 : anton 1.23 lib-filename 2@ append s" .lo -o " append
453 : anton 1.22 lib-filename 2@ append s" .la" append ( c-addr u )
454 : anton 1.23 2dup system drop free throw $? abort" libtool link failed"
455 :     lib-filename 2@ s" .la" s+
456 :     2dup open-lib dup 0= if
457 : anton 1.24 .lib-error true abort" open-lib failed"
458 : anton 1.23 endif
459 : anton 1.15 ( lib-handle ) lib-handle-addr @ !
460 :     2dup delete-file throw drop free throw
461 :     lib-filename 2@ s" .c" s+ 2dup delete-file throw drop free throw
462 : anton 1.21 lib-filename 2@ drop free throw 0 0 lib-filename 2! ; IS compile-wrapper-function
463 : anton 1.5 \ s" ar rcs xxx.a xxx.o" system
464 :     \ $? abort" ar generated error" ;
465 :    
466 : anton 1.12 : link-wrapper-function { cff -- sym }
467 :     cff cff-rtype wrapper-function-name { d: wrapper-name }
468 : anton 1.23 wrapper-name cff cff-lha @ @ assert( dup ) lib-sym dup 0= if
469 : anton 1.24 .lib-error -&32 throw
470 : anton 1.23 endif
471 : anton 1.10 wrapper-name drop free throw ;
472 : anton 1.8
473 : anton 1.12 : c-function-ft ( xt-defr xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
474 : anton 1.8 \ build time/first time action for c-function
475 : anton 1.12 init-c-source-file
476 :     noname create 2, lib-handle-addr @ ,
477 : anton 1.2 parse-name { d: c-name }
478 : anton 1.8 here parse-function-types c-name string,
479 : anton 1.11 ['] gen-wrapper-function c-source-file outfile-execute
480 : anton 1.8 does> ( ... -- ... )
481 : anton 1.10 dup 2@ { xt-defer xt-cfr }
482 : anton 1.12 dup cff-lha @ @ 0= if
483 :     compile-wrapper-function
484 :     endif
485 :     link-wrapper-function xt-cfr >body !
486 : anton 1.8 xt-cfr xt-defer defer!
487 :     xt-cfr execute ;
488 :    
489 :     : c-function-rt ( -- )
490 :     \ run-time definition for c function; addr is the address where
491 :     \ the sym should be stored
492 :     noname create 0 ,
493 : anton 1.2 does> ( ... -- ... )
494 :     @ call-c ;
495 :    
496 : anton 1.18 : c-function ( "forth-name" "c-name" "@{type@}" "--" "type" -- ) \ gforth
497 : anton 1.17 \G Define a Forth word @i{forth-name}. @i{Forth-name} has the
498 :     \G specified stack effect and calls the C function @code{c-name}.
499 : anton 1.8 defer lastxt dup c-function-rt lastxt c-function-ft
500 :     lastxt swap defer! ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help