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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help