[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.50 \ Copyright (C) 2006,2007,2008 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.29 [ifundef] parse-name
127 :     ' parse-word alias parse-name
128 :     [then]
129 :     [ifundef] defer!
130 :     : defer! ( xt xt-deferred -- ) \ gforth defer-store
131 :     \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
132 :     >body [ has? rom [IF] ] @ [ [THEN] ] ! ;
133 :     [then]
134 :    
135 :     \ : delete-file 2drop 0 ;
136 : anton 1.12
137 :     require struct.fs
138 :    
139 : anton 1.10 \ c-function-ft word body:
140 : anton 1.12 struct
141 :     cell% field cff-cfr \ xt of c-function-rt word
142 :     cell% field cff-deferred \ xt of c-function deferred word
143 :     cell% field cff-lha \ address of the lib-handle for the lib that
144 :     \ contains the wrapper function of the word
145 :     char% field cff-rtype \ return type
146 :     char% field cff-np \ number of parameters
147 :     1 0 field cff-ptypes \ #npar parameter types
148 :     \ counted string: c-name
149 :     end-struct cff%
150 :    
151 : anton 1.14 variable c-source-file-id \ contains the source file id of the current batch
152 :     0 c-source-file-id !
153 :     variable lib-handle-addr \ points to the library handle of the current batch.
154 :     \ the library handle is 0 if the current
155 :     \ batch is not yet compiled.
156 : anton 1.39 here 0 , lib-handle-addr ! \ just make sure LIB-HANDLE always works
157 : anton 1.23 2variable lib-filename \ filename without extension
158 :     2variable lib-modulename \ basename of the file without extension
159 : anton 1.44 2variable libcc-named-dir-v \ directory for named libcc wrapper libraries
160 :     0 value libcc-path \ pointer to path of library directories
161 : anton 1.2
162 : anton 1.49 defer replace-rpath ( c-addr1 u1 -- c-addr2 u2 )
163 :     ' noop is replace-rpath
164 : anton 1.35
165 : anton 1.3 : .nb ( n -- )
166 : anton 1.2 0 .r ;
167 :    
168 :     : const+ ( n1 "name" -- n2 )
169 :     dup constant 1+ ;
170 :    
171 : anton 1.10 : front-string { c-addr1 u1 c-addr2 u2 -- c-addr3 u3 }
172 :     \ insert string c-addr2 u2 in buffer c-addr1 u1; c-addr3 u3 is the
173 :     \ remainder of the buffer.
174 :     assert( u1 u2 u>= )
175 :     c-addr2 c-addr1 u2 move
176 :     c-addr1 u1 u2 /string ;
177 :    
178 :     : front-char { c-addr1 u1 c -- c-addr3 u2 }
179 :     \ insert c in buffer c-addr1 u1; c-addr3 u3 is the remainder of
180 :     \ the buffer.
181 :     assert( u1 0 u> )
182 :     c c-addr1 c!
183 :     c-addr1 u1 1 /string ;
184 :    
185 : anton 1.15 : s+ { addr1 u1 addr2 u2 -- addr u }
186 :     u1 u2 + allocate throw { addr }
187 :     addr1 addr u1 move
188 :     addr2 addr u1 + u2 move
189 :     addr u1 u2 +
190 :     ;
191 :    
192 :     : append { addr1 u1 addr2 u2 -- addr u }
193 :     addr1 u1 u2 + dup { u } resize throw { addr }
194 :     addr2 addr u1 + u2 move
195 :     addr u ;
196 :    
197 : anton 1.6 \ linked list stuff (should go elsewhere)
198 :    
199 :     struct
200 :     cell% field list-next
201 :     1 0 field list-payload
202 :     end-struct list%
203 :    
204 :     : list-insert { node list -- }
205 :     list list-next @ node list-next !
206 :     node list list-next ! ;
207 :    
208 :     : list-append { node endlistp -- }
209 :     \ insert node at place pointed to by endlistp
210 :     node endlistp @ list-insert
211 :     node list-next endlistp ! ;
212 :    
213 :     : list-map ( ... list xt -- ... )
214 :     \ xt ( ... node -- ... )
215 :     { xt } begin { node }
216 :     node while
217 :     node xt execute
218 :     node list-next @
219 :     repeat ;
220 :    
221 : anton 1.33 \ linked libraries
222 :    
223 :     list%
224 :     cell% 2* field c-lib-string
225 :     end-struct c-lib%
226 :    
227 :     variable c-libs \ linked list of library names (without "lib")
228 :    
229 :     : add-lib ( c-addr u -- )
230 :     \G Add library lib@i{string} to the list of libraries, where
231 :     \G @i{string} is represented by @i{c-addr u}.
232 :     c-lib% %size allocate throw dup >r
233 :     c-lib-string 2!
234 :     r> c-libs list-insert ;
235 :    
236 :     : append-l ( c-addr1 u1 node -- c-addr2 u2 )
237 :     \ append " -l<nodelib>" to string1
238 :     >r s" -l" append r> c-lib-string 2@ append ;
239 :    
240 : anton 1.6 \ C prefix lines
241 :    
242 :     \ linked list of longcstrings: [ link | count-cell | characters ]
243 :    
244 :     list%
245 :     cell% field c-prefix-count
246 :     1 0 field c-prefix-chars
247 :     end-struct c-prefix%
248 :    
249 :     variable c-prefix-lines 0 c-prefix-lines !
250 :     variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
251 :    
252 : anton 1.14 : print-c-prefix-line ( node -- )
253 :     dup c-prefix-chars swap c-prefix-count @ type cr ;
254 :    
255 :     : print-c-prefix-lines ( -- )
256 :     c-prefix-lines @ ['] print-c-prefix-line list-map ;
257 :    
258 : anton 1.6 : save-c-prefix-line ( c-addr u -- )
259 : anton 1.14 c-source-file-id @ ?dup-if
260 :     >r 2dup r> write-line throw
261 :     then
262 : anton 1.6 align here 0 , c-prefix-lines-end list-append ( c-addr u )
263 :     longstring, ;
264 :    
265 : anton 1.18 : \c ( "rest-of-line" -- ) \ gforth backslash-c
266 : anton 1.17 \G One line of C declarations for the C interface
267 : anton 1.6 -1 parse save-c-prefix-line ;
268 :    
269 : anton 1.19 s" #include <gforth/" version-string s+ s" /libcc.h>" append ( c-addr u )
270 :     2dup save-c-prefix-line drop free throw
271 : anton 1.5
272 : anton 1.6 \ Types (for parsing)
273 : anton 1.5
274 : anton 1.2 wordlist constant libcc-types
275 :    
276 :     get-current libcc-types set-current
277 :    
278 :     \ index values
279 :     -1
280 :     const+ -- \ end of arguments
281 :     const+ n \ integer cell
282 : anton 1.5 const+ a \ address cell
283 : anton 1.2 const+ d \ double
284 :     const+ r \ float
285 :     const+ func \ C function pointer
286 :     const+ void
287 :     drop
288 :    
289 :     set-current
290 :    
291 :     : parse-libcc-type ( "libcc-type" -- u )
292 :     parse-name libcc-types search-wordlist 0= -13 and throw execute ;
293 :    
294 :     : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
295 :     here 2 chars allot here begin
296 :     parse-libcc-type dup 0>= while
297 :     c,
298 :     repeat
299 : anton 1.3 drop here swap - over char+ c!
300 :     parse-libcc-type dup 0< -32 and throw swap c! ;
301 : anton 1.2
302 :     : type-letter ( n -- c )
303 : pazsan 1.52 chars s" nadrfv" drop + c@ ;
304 : anton 1.2
305 :     \ count-stacks
306 :    
307 :     : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
308 :     1+ ;
309 :    
310 : anton 1.5 : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
311 : anton 1.2 1+ ;
312 :    
313 :     : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
314 :     2 + ;
315 :    
316 :     : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
317 :     swap 1+ swap ;
318 :    
319 :     : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
320 :     1+ ;
321 :    
322 :     : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
323 :     ;
324 :    
325 :     create count-stacks-types
326 :     ' count-stacks-n ,
327 : anton 1.5 ' count-stacks-a ,
328 : anton 1.2 ' count-stacks-d ,
329 :     ' count-stacks-r ,
330 :     ' count-stacks-func ,
331 :     ' count-stacks-void ,
332 :    
333 :     : count-stacks ( pars -- fp-change sp-change )
334 :     \ pars is an addr u pair
335 :     0 0 2swap over + swap u+do
336 : anton 1.3 i c@ cells count-stacks-types + @ execute
337 : anton 1.2 loop ;
338 :    
339 :     \ gen-pars
340 :    
341 :     : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
342 : anton 1.5 ." sp[" 1- dup .nb ." ]" ;
343 : anton 1.2
344 : anton 1.5 : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
345 : anton 1.2 ." (void *)(" gen-par-n ." )" ;
346 :    
347 :     : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
348 : anton 1.4 ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
349 : anton 1.2
350 :     : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
351 : anton 1.3 swap 1- tuck ." fp[" .nb ." ]" ;
352 : anton 1.2
353 :     : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
354 : anton 1.5 gen-par-a ;
355 : anton 1.2
356 :     : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
357 :     -32 throw ;
358 :    
359 :     create gen-par-types
360 :     ' gen-par-n ,
361 : anton 1.5 ' gen-par-a ,
362 : anton 1.2 ' gen-par-d ,
363 :     ' gen-par-r ,
364 :     ' gen-par-func ,
365 :     ' gen-par-void ,
366 :    
367 :     : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
368 : anton 1.3 cells gen-par-types + @ execute ;
369 : anton 1.2
370 :     \ the call itself
371 :    
372 :     : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
373 :     c-name type ." ("
374 : anton 1.3 fp-change1 sp-change1 pars over + swap u+do
375 : anton 1.2 i c@ gen-par
376 :     i 1+ i' < if
377 :     ." ,"
378 :     endif
379 :     loop
380 :     2drop ." )" ;
381 :    
382 :     \ calls for various kinds of return values
383 :    
384 :     : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
385 :     2dup 2>r gen-wrapped-call 2r> ;
386 :    
387 : anton 1.3 : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
388 : anton 1.5 2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
389 : anton 1.3
390 : anton 1.5 : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
391 :     2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
392 : anton 1.3
393 :     : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
394 :     ." gforth_ll2d(" gen-wrapped-void
395 : anton 1.5 ." ," gen-par-n ." ," gen-par-n ." )" ;
396 : anton 1.3
397 :     : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
398 : anton 1.30 2dup gen-par-r 2>r ." =" gen-wrapped-call 2r> ;
399 : anton 1.3
400 :     : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
401 : anton 1.5 gen-wrapped-a ;
402 : anton 1.3
403 : anton 1.2 create gen-wrapped-types
404 :     ' gen-wrapped-n ,
405 : anton 1.5 ' gen-wrapped-a ,
406 : anton 1.2 ' gen-wrapped-d ,
407 :     ' gen-wrapped-r ,
408 :     ' gen-wrapped-func ,
409 :     ' gen-wrapped-void ,
410 :    
411 :     : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
412 : anton 1.3 cells gen-wrapped-types + @ execute ;
413 : anton 1.2
414 : anton 1.10 : wrapper-function-name ( addr -- c-addr u )
415 :     \ addr points to the return type index of a c-function descriptor
416 :     count { r-type } count { d: pars }
417 :     pars + count { d: c-name }
418 :     s" gforth_c_" { d: prefix }
419 :     prefix nip c-name nip + pars nip + 3 + { u }
420 :     u allocate throw { c-addr }
421 :     c-addr u
422 :     prefix front-string c-name front-string '_ front-char
423 :     pars bounds u+do
424 :     i c@ type-letter front-char
425 :     loop
426 :     '_ front-char r-type type-letter front-char assert( dup 0= )
427 :     2drop c-addr u ;
428 :    
429 : anton 1.2 : gen-wrapper-function ( addr -- )
430 :     \ addr points to the return type index of a c-function descriptor
431 : anton 1.10 dup { descriptor }
432 : anton 1.13 count { ret } count 2dup { d: pars } chars + count { d: c-name }
433 : anton 1.23 ." void " lib-modulename 2@ type ." _LTX_" descriptor wrapper-function-name 2dup type drop free throw
434 : anton 1.10 .\" (void)\n"
435 : anton 1.4 .\" {\n Cell MAYBE_UNUSED *sp = gforth_SP;\n Float MAYBE_UNUSED *fp = gforth_FP;\n "
436 : anton 1.2 pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
437 :     ?dup-if
438 : anton 1.3 ." gforth_SP = sp+" .nb .\" ;\n"
439 : anton 1.2 endif
440 :     ?dup-if
441 : anton 1.3 ." gforth_FP = fp+" .nb .\" ;\n"
442 : anton 1.2 endif
443 : anton 1.3 .\" }\n" ;
444 : anton 1.2
445 : anton 1.40 : scan-back { c-addr u1 c -- c-addr u2 }
446 :     \ the last occurence of c in c-addr u1 is at u2-1; if it does not
447 :     \ occur, u2=0.
448 :     c-addr 1- c-addr u1 + 1- u-do
449 :     i c@ c = if
450 :     c-addr i over - 1+ unloop exit endif
451 :     1 -loop
452 :     c-addr 0 ;
453 :    
454 :     : dirname ( c-addr1 u1 -- c-addr2 u2 )
455 :     \ directory name of the file name c-addr1 u1, including the final "/".
456 :     '/ scan-back ;
457 :    
458 :     : basename ( c-addr1 u1 -- c-addr2 u2 )
459 :     \ file name without directory component
460 :     2dup dirname nip /string ;
461 : anton 1.16
462 : anton 1.15 : gen-filename ( x -- c-addr u )
463 : anton 1.35 \ generates a file basename for lib-handle-addr X
464 : anton 1.16 0 <<# ['] #s $10 base-execute #>
465 : anton 1.35 s" gforth_c_" 2swap s+ #>> ;
466 :    
467 : anton 1.40 : libcc-named-dir ( -- c-addr u )
468 : anton 1.43 libcc-named-dir-v 2@ ;
469 : anton 1.40
470 :     : libcc-tmp-dir ( -- c-addr u )
471 : anton 1.41 s" ~/.gforth/libcc-tmp/" ;
472 : anton 1.40
473 :     : prepend-dirname ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
474 : anton 1.41 2over s+ 2swap drop free throw ;
475 : anton 1.35
476 : anton 1.42 : open-wrappers ( -- addr|0 )
477 :     lib-filename 2@ s" .la" s+
478 :     2dup libcc-named-dir string-prefix? if ( c-addr u )
479 :     \ see if we can open it in the path
480 :     libcc-named-dir nip /string
481 :     libcc-path open-path-file if
482 :     0 exit endif
483 :     ( wfile-id c-addr2 u2 ) rot close-file throw save-mem ( c-addr2 u2 )
484 :     endif
485 :     \ 2dup cr type
486 :     2dup open-lib >r
487 :     drop free throw r> ;
488 :    
489 : anton 1.39 : c-library-name-setup ( c-addr u -- )
490 : anton 1.35 assert( c-source-file-id @ 0= )
491 : anton 1.40 { d: filename }
492 : anton 1.37 here 0 , lib-handle-addr ! filename lib-filename 2!
493 : anton 1.40 filename basename lib-modulename 2! ;
494 : anton 1.39
495 :     : c-library-name-create ( -- )
496 :     lib-filename 2@ s" .c" s+ 2dup w/o create-file throw
497 :     dup c-source-file-id !
498 :     ['] print-c-prefix-lines swap outfile-execute
499 :     drop free throw ;
500 :    
501 : anton 1.40 : c-named-library-name ( c-addr u -- )
502 : anton 1.39 \ set up filenames for a (possibly new) library; c-addr u is the
503 :     \ basename of the library
504 : anton 1.40 libcc-named-dir prepend-dirname c-library-name-setup
505 : anton 1.37 open-wrappers dup if
506 :     lib-handle-addr @ !
507 :     else
508 : anton 1.39 drop c-library-name-create
509 : anton 1.37 endif ;
510 :    
511 : anton 1.40 : c-tmp-library-name ( c-addr u -- )
512 : anton 1.39 \ set up filenames for a new library; c-addr u is the basename of
513 :     \ the library
514 : anton 1.40 libcc-tmp-dir prepend-dirname c-library-name-setup c-library-name-create ;
515 : anton 1.39
516 : anton 1.37 : lib-handle ( -- addr )
517 :     lib-handle-addr @ @ ;
518 : anton 1.15
519 : anton 1.12 : init-c-source-file ( -- )
520 : anton 1.37 lib-handle 0= if
521 :     c-source-file-id @ 0= if
522 : anton 1.40 here gen-filename c-tmp-library-name
523 : anton 1.37 endif
524 : anton 1.12 endif ;
525 : anton 1.11
526 :     : c-source-file ( -- file-id )
527 : anton 1.12 c-source-file-id @ assert( dup ) ;
528 : anton 1.11
529 : anton 1.37 : notype-execute ( ... xt -- ... )
530 :     what's type { oldtype } try
531 :     ['] 2drop is type execute 0
532 :     restore
533 :     oldtype is type
534 :     endtry
535 :     throw ;
536 :    
537 :     : c-source-file-execute ( ... xt -- ... )
538 :     \ direct the output of xt to c-source-file, or nothing
539 :     lib-handle if
540 :     notype-execute
541 :     else
542 :     c-source-file outfile-execute
543 :     endif ;
544 :    
545 : anton 1.24 : .lib-error ( -- )
546 :     [ifdef] lib-error
547 :     ['] cr stderr outfile-execute
548 : anton 1.31 lib-error ['] type stderr outfile-execute
549 : anton 1.24 [then] ;
550 : anton 1.23
551 : anton 1.33 DEFER compile-wrapper-function ( -- )
552 : anton 1.35 : compile-wrapper-function1 ( -- )
553 : anton 1.37 lib-handle 0= if
554 :     c-source-file close-file throw
555 :     0 c-source-file-id !
556 : anton 1.48 [ libtool-command s" --silent --mode=compile --tag=CC " s+
557 :     libtool-cc append s" -I " append
558 :     s" includedir" getenv append ] sliteral
559 : anton 1.37 s" -O -c " s+ lib-filename 2@ append s" .c -o " append
560 :     lib-filename 2@ append s" .lo" append ( c-addr u )
561 : pazsan 1.47 \ cr 2dup type
562 : anton 1.37 2dup system drop free throw $? abort" libtool compile failed"
563 : anton 1.48 [ libtool-command s" --silent --mode=link --tag=CC " s+
564 :     libtool-cc append s" -module -rpath " s+ ] sliteral
565 : anton 1.49 lib-filename 2@ dirname replace-rpath s+ s" " append
566 : anton 1.37 lib-filename 2@ append s" .lo -o " append
567 :     lib-filename 2@ append s" .la" append ( c-addr u )
568 :     c-libs @ ['] append-l list-map
569 : pazsan 1.47 \ 2dup type cr
570 : anton 1.37 2dup system drop free throw $? abort" libtool link failed"
571 :     open-wrappers dup 0= if
572 :     .lib-error true abort" open-lib failed"
573 :     endif
574 :     ( lib-handle ) lib-handle-addr @ !
575 : anton 1.23 endif
576 : anton 1.35 lib-filename 2@ drop free throw 0 0 lib-filename 2! ;
577 :     ' compile-wrapper-function1 IS compile-wrapper-function
578 : anton 1.5 \ s" ar rcs xxx.a xxx.o" system
579 :     \ $? abort" ar generated error" ;
580 :    
581 : anton 1.12 : link-wrapper-function { cff -- sym }
582 :     cff cff-rtype wrapper-function-name { d: wrapper-name }
583 : anton 1.23 wrapper-name cff cff-lha @ @ assert( dup ) lib-sym dup 0= if
584 : anton 1.24 .lib-error -&32 throw
585 : anton 1.23 endif
586 : anton 1.10 wrapper-name drop free throw ;
587 : anton 1.8
588 : anton 1.12 : c-function-ft ( xt-defr xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
589 : anton 1.8 \ build time/first time action for c-function
590 : anton 1.12 init-c-source-file
591 :     noname create 2, lib-handle-addr @ ,
592 : anton 1.2 parse-name { d: c-name }
593 : anton 1.8 here parse-function-types c-name string,
594 : anton 1.37 ['] gen-wrapper-function c-source-file-execute
595 : anton 1.8 does> ( ... -- ... )
596 : anton 1.10 dup 2@ { xt-defer xt-cfr }
597 : anton 1.12 dup cff-lha @ @ 0= if
598 :     compile-wrapper-function
599 :     endif
600 :     link-wrapper-function xt-cfr >body !
601 : anton 1.8 xt-cfr xt-defer defer!
602 :     xt-cfr execute ;
603 :    
604 :     : c-function-rt ( -- )
605 :     \ run-time definition for c function; addr is the address where
606 :     \ the sym should be stored
607 :     noname create 0 ,
608 : anton 1.2 does> ( ... -- ... )
609 :     @ call-c ;
610 :    
611 : anton 1.18 : c-function ( "forth-name" "c-name" "@{type@}" "--" "type" -- ) \ gforth
612 : anton 1.17 \G Define a Forth word @i{forth-name}. @i{Forth-name} has the
613 :     \G specified stack effect and calls the C function @code{c-name}.
614 : anton 1.8 defer lastxt dup c-function-rt lastxt c-function-ft
615 :     lastxt swap defer! ;
616 : anton 1.33
617 :     : clear-libs ( -- )
618 :     \G Clear the list of libs
619 :     c-source-file-id @ if
620 :     compile-wrapper-function
621 :     endif
622 :     0 c-libs ! ;
623 :     clear-libs
624 : anton 1.35
625 :     : c-library-incomplete ( -- )
626 :     true abort" Called function of unfinished named C library" ;
627 :    
628 :     : c-library-name ( c-addr u -- ) \ gforth
629 : anton 1.38 \G Start a C library interface with name @i{c-addr u}.
630 : anton 1.35 clear-libs
631 :     ['] c-library-incomplete is compile-wrapper-function
632 : anton 1.40 c-named-library-name ;
633 : anton 1.35
634 :     : c-library ( "name" -- ) \ gforth
635 :     \G Parsing version of @code{c-library-name}
636 : anton 1.39 parse-name save-mem c-library-name ;
637 : anton 1.35
638 : anton 1.38 : end-c-library ( -- ) \ gforth
639 :     \G Finish and (if necessary) build the latest C library interface.
640 : anton 1.35 ['] compile-wrapper-function1 is compile-wrapper-function
641 :     compile-wrapper-function1 ;
642 : anton 1.43
643 :     : init-libcc ( -- )
644 :     s" ~/.gforth/libcc-named/" libcc-named-dir-v 2!
645 : pazsan 1.45 [IFDEF] make-path
646 : anton 1.43 make-path to libcc-path
647 :     libcc-named-dir libcc-path also-path
648 :     [ s" libccdir" getenv ] sliteral libcc-path also-path
649 : pazsan 1.45 [THEN]
650 : anton 1.43 ;
651 :    
652 :     init-libcc
653 :    
654 :     :noname ( -- )
655 :     defers 'cold
656 :     init-libcc ;
657 :     is 'cold

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help