Annotation of gforth/libcc.fs, revision 1.51
1.1 anton 1: \ libcc.fs foreign function interface implemented using a C compiler
2:
1.50 anton 3: \ Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
1.1 anton 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
1.28 anton 9: \ as published by the Free Software Foundation, either version 3
1.1 anton 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
1.28 anton 18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1 anton 19:
20:
21: \ What this implementation does is this: if it sees a declaration like
22:
1.2 anton 23: \ \ something that tells it that the current library is libc
1.6 anton 24: \ \c #include <unistd.h>
1.2 anton 25: \ c-function dlseek lseek n d n -- d
1.1 anton 26:
27: \ it genererates C code similar to the following:
28:
29: \ #include <gforth.h>
1.2 anton 30: \ #include <unistd.h>
1.1 anton 31: \
1.2 anton 32: \ void gforth_c_lseek_ndn_d(void)
1.1 anton 33: \ {
34: \ Cell *sp = gforth_SP;
35: \ Float *fp = gforth_FP;
1.2 anton 36: \ 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;
1.1 anton 39: \ }
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
1.2 anton 44: \ the function pointer of gforth_c_lseek_ndn_d on the stack and
45: \ calls CALL-C.
46:
1.7 anton 47: \ 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:
1.2 anton 70: \ other things to do:
71:
72: \ c-variable forth-name c-name
73: \ c-constant forth-name c-name
74:
1.20 anton 75: \ 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:
1.2 anton 117:
118: \ data structures
119:
1.10 anton 120: \ 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:
1.29 anton 126: [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 ;
1.12 anton 136:
137: require struct.fs
138:
1.10 anton 139: \ c-function-ft word body:
1.12 anton 140: 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:
1.14 anton 151: 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.
1.39 anton 156: here 0 , lib-handle-addr ! \ just make sure LIB-HANDLE always works
1.23 anton 157: 2variable lib-filename \ filename without extension
158: 2variable lib-modulename \ basename of the file without extension
1.44 anton 159: 2variable libcc-named-dir-v \ directory for named libcc wrapper libraries
160: 0 value libcc-path \ pointer to path of library directories
1.2 anton 161:
1.49 anton 162: defer replace-rpath ( c-addr1 u1 -- c-addr2 u2 )
163: ' noop is replace-rpath
1.35 anton 164:
1.3 anton 165: : .nb ( n -- )
1.2 anton 166: 0 .r ;
167:
168: : const+ ( n1 "name" -- n2 )
169: dup constant 1+ ;
170:
1.10 anton 171: : 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:
1.15 anton 185: : 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:
1.6 anton 197: \ 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:
1.33 anton 221: \ 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:
1.6 anton 240: \ 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:
1.14 anton 252: : 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:
1.6 anton 258: : save-c-prefix-line ( c-addr u -- )
1.14 anton 259: c-source-file-id @ ?dup-if
260: >r 2dup r> write-line throw
261: then
1.6 anton 262: align here 0 , c-prefix-lines-end list-append ( c-addr u )
263: longstring, ;
264:
1.18 anton 265: : \c ( "rest-of-line" -- ) \ gforth backslash-c
1.17 anton 266: \G One line of C declarations for the C interface
1.6 anton 267: -1 parse save-c-prefix-line ;
268:
1.19 anton 269: s" #include <gforth/" version-string s+ s" /libcc.h>" append ( c-addr u )
270: 2dup save-c-prefix-line drop free throw
1.5 anton 271:
1.6 anton 272: \ Types (for parsing)
1.5 anton 273:
1.2 anton 274: 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
1.5 anton 282: const+ a \ address cell
1.2 anton 283: const+ d \ double
284: const+ r \ float
285: const+ func \ C function pointer
286: const+ void
1.51 ! pazsan 287: const+ file \ C file pointer
1.2 anton 288: drop
289:
290: set-current
291:
292: : parse-libcc-type ( "libcc-type" -- u )
293: parse-name libcc-types search-wordlist 0= -13 and throw execute ;
294:
295: : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
296: here 2 chars allot here begin
297: parse-libcc-type dup 0>= while
298: c,
299: repeat
1.3 anton 300: drop here swap - over char+ c!
301: parse-libcc-type dup 0< -32 and throw swap c! ;
1.2 anton 302:
303: : type-letter ( n -- c )
1.51 ! pazsan 304: chars s" nadrfvF" drop + c@ ;
1.2 anton 305:
306: \ count-stacks
307:
308: : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
309: 1+ ;
310:
1.5 anton 311: : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
1.2 anton 312: 1+ ;
313:
314: : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
315: 2 + ;
316:
317: : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
318: swap 1+ swap ;
319:
320: : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
321: 1+ ;
322:
323: : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
324: ;
325:
326: create count-stacks-types
327: ' count-stacks-n ,
1.5 anton 328: ' count-stacks-a ,
1.2 anton 329: ' count-stacks-d ,
330: ' count-stacks-r ,
331: ' count-stacks-func ,
332: ' count-stacks-void ,
1.51 ! pazsan 333: ' count-stacks-a ,
1.2 anton 334:
335: : count-stacks ( pars -- fp-change sp-change )
336: \ pars is an addr u pair
337: 0 0 2swap over + swap u+do
1.3 anton 338: i c@ cells count-stacks-types + @ execute
1.2 anton 339: loop ;
340:
341: \ gen-pars
342:
343: : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.5 anton 344: ." sp[" 1- dup .nb ." ]" ;
1.2 anton 345:
1.5 anton 346: : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.2 anton 347: ." (void *)(" gen-par-n ." )" ;
348:
349: : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.4 anton 350: ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
1.2 anton 351:
352: : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.3 anton 353: swap 1- tuck ." fp[" .nb ." ]" ;
1.2 anton 354:
355: : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.5 anton 356: gen-par-a ;
1.2 anton 357:
358: : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
359: -32 throw ;
360:
1.51 ! pazsan 361: : gen-par-file ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
! 362: ." (FILE *)(" gen-par-n ." )" ;
! 363:
1.2 anton 364: create gen-par-types
365: ' gen-par-n ,
1.5 anton 366: ' gen-par-a ,
1.2 anton 367: ' gen-par-d ,
368: ' gen-par-r ,
369: ' gen-par-func ,
370: ' gen-par-void ,
1.51 ! pazsan 371: ' gen-par-file ,
1.2 anton 372:
373: : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
1.3 anton 374: cells gen-par-types + @ execute ;
1.2 anton 375:
376: \ the call itself
377:
378: : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
379: c-name type ." ("
1.3 anton 380: fp-change1 sp-change1 pars over + swap u+do
1.2 anton 381: i c@ gen-par
382: i 1+ i' < if
383: ." ,"
384: endif
385: loop
386: 2drop ." )" ;
387:
388: \ calls for various kinds of return values
389:
390: : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
391: 2dup 2>r gen-wrapped-call 2r> ;
392:
1.3 anton 393: : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5 anton 394: 2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
1.3 anton 395:
1.5 anton 396: : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
397: 2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
1.3 anton 398:
399: : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
400: ." gforth_ll2d(" gen-wrapped-void
1.5 anton 401: ." ," gen-par-n ." ," gen-par-n ." )" ;
1.3 anton 402:
403: : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.30 anton 404: 2dup gen-par-r 2>r ." =" gen-wrapped-call 2r> ;
1.3 anton 405:
406: : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5 anton 407: gen-wrapped-a ;
1.3 anton 408:
1.2 anton 409: create gen-wrapped-types
410: ' gen-wrapped-n ,
1.5 anton 411: ' gen-wrapped-a ,
1.2 anton 412: ' gen-wrapped-d ,
413: ' gen-wrapped-r ,
414: ' gen-wrapped-func ,
415: ' gen-wrapped-void ,
1.51 ! pazsan 416: ' gen-wrapped-a ,
1.2 anton 417:
418: : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
1.3 anton 419: cells gen-wrapped-types + @ execute ;
1.2 anton 420:
1.10 anton 421: : wrapper-function-name ( addr -- c-addr u )
422: \ addr points to the return type index of a c-function descriptor
423: count { r-type } count { d: pars }
424: pars + count { d: c-name }
425: s" gforth_c_" { d: prefix }
426: prefix nip c-name nip + pars nip + 3 + { u }
427: u allocate throw { c-addr }
428: c-addr u
429: prefix front-string c-name front-string '_ front-char
430: pars bounds u+do
431: i c@ type-letter front-char
432: loop
433: '_ front-char r-type type-letter front-char assert( dup 0= )
434: 2drop c-addr u ;
435:
1.2 anton 436: : gen-wrapper-function ( addr -- )
437: \ addr points to the return type index of a c-function descriptor
1.10 anton 438: dup { descriptor }
1.13 anton 439: count { ret } count 2dup { d: pars } chars + count { d: c-name }
1.23 anton 440: ." void " lib-modulename 2@ type ." _LTX_" descriptor wrapper-function-name 2dup type drop free throw
1.10 anton 441: .\" (void)\n"
1.4 anton 442: .\" {\n Cell MAYBE_UNUSED *sp = gforth_SP;\n Float MAYBE_UNUSED *fp = gforth_FP;\n "
1.2 anton 443: pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
444: ?dup-if
1.3 anton 445: ." gforth_SP = sp+" .nb .\" ;\n"
1.2 anton 446: endif
447: ?dup-if
1.3 anton 448: ." gforth_FP = fp+" .nb .\" ;\n"
1.2 anton 449: endif
1.3 anton 450: .\" }\n" ;
1.2 anton 451:
1.40 anton 452: : scan-back { c-addr u1 c -- c-addr u2 }
453: \ the last occurence of c in c-addr u1 is at u2-1; if it does not
454: \ occur, u2=0.
455: c-addr 1- c-addr u1 + 1- u-do
456: i c@ c = if
457: c-addr i over - 1+ unloop exit endif
458: 1 -loop
459: c-addr 0 ;
460:
461: : dirname ( c-addr1 u1 -- c-addr2 u2 )
462: \ directory name of the file name c-addr1 u1, including the final "/".
463: '/ scan-back ;
464:
465: : basename ( c-addr1 u1 -- c-addr2 u2 )
466: \ file name without directory component
467: 2dup dirname nip /string ;
1.16 anton 468:
1.15 anton 469: : gen-filename ( x -- c-addr u )
1.35 anton 470: \ generates a file basename for lib-handle-addr X
1.16 anton 471: 0 <<# ['] #s $10 base-execute #>
1.35 anton 472: s" gforth_c_" 2swap s+ #>> ;
473:
1.40 anton 474: : libcc-named-dir ( -- c-addr u )
1.43 anton 475: libcc-named-dir-v 2@ ;
1.40 anton 476:
477: : libcc-tmp-dir ( -- c-addr u )
1.41 anton 478: s" ~/.gforth/libcc-tmp/" ;
1.40 anton 479:
480: : prepend-dirname ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
1.41 anton 481: 2over s+ 2swap drop free throw ;
1.35 anton 482:
1.42 anton 483: : open-wrappers ( -- addr|0 )
484: lib-filename 2@ s" .la" s+
485: 2dup libcc-named-dir string-prefix? if ( c-addr u )
486: \ see if we can open it in the path
487: libcc-named-dir nip /string
488: libcc-path open-path-file if
489: 0 exit endif
490: ( wfile-id c-addr2 u2 ) rot close-file throw save-mem ( c-addr2 u2 )
491: endif
492: \ 2dup cr type
493: 2dup open-lib >r
494: drop free throw r> ;
495:
1.39 anton 496: : c-library-name-setup ( c-addr u -- )
1.35 anton 497: assert( c-source-file-id @ 0= )
1.40 anton 498: { d: filename }
1.37 anton 499: here 0 , lib-handle-addr ! filename lib-filename 2!
1.40 anton 500: filename basename lib-modulename 2! ;
1.39 anton 501:
502: : c-library-name-create ( -- )
503: lib-filename 2@ s" .c" s+ 2dup w/o create-file throw
504: dup c-source-file-id !
505: ['] print-c-prefix-lines swap outfile-execute
506: drop free throw ;
507:
1.40 anton 508: : c-named-library-name ( c-addr u -- )
1.39 anton 509: \ set up filenames for a (possibly new) library; c-addr u is the
510: \ basename of the library
1.40 anton 511: libcc-named-dir prepend-dirname c-library-name-setup
1.37 anton 512: open-wrappers dup if
513: lib-handle-addr @ !
514: else
1.39 anton 515: drop c-library-name-create
1.37 anton 516: endif ;
517:
1.40 anton 518: : c-tmp-library-name ( c-addr u -- )
1.39 anton 519: \ set up filenames for a new library; c-addr u is the basename of
520: \ the library
1.40 anton 521: libcc-tmp-dir prepend-dirname c-library-name-setup c-library-name-create ;
1.39 anton 522:
1.37 anton 523: : lib-handle ( -- addr )
524: lib-handle-addr @ @ ;
1.15 anton 525:
1.12 anton 526: : init-c-source-file ( -- )
1.37 anton 527: lib-handle 0= if
528: c-source-file-id @ 0= if
1.40 anton 529: here gen-filename c-tmp-library-name
1.37 anton 530: endif
1.12 anton 531: endif ;
1.11 anton 532:
533: : c-source-file ( -- file-id )
1.12 anton 534: c-source-file-id @ assert( dup ) ;
1.11 anton 535:
1.37 anton 536: : notype-execute ( ... xt -- ... )
537: what's type { oldtype } try
538: ['] 2drop is type execute 0
539: restore
540: oldtype is type
541: endtry
542: throw ;
543:
544: : c-source-file-execute ( ... xt -- ... )
545: \ direct the output of xt to c-source-file, or nothing
546: lib-handle if
547: notype-execute
548: else
549: c-source-file outfile-execute
550: endif ;
551:
1.24 anton 552: : .lib-error ( -- )
553: [ifdef] lib-error
554: ['] cr stderr outfile-execute
1.31 anton 555: lib-error ['] type stderr outfile-execute
1.24 anton 556: [then] ;
1.23 anton 557:
1.33 anton 558: DEFER compile-wrapper-function ( -- )
1.35 anton 559: : compile-wrapper-function1 ( -- )
1.37 anton 560: lib-handle 0= if
561: c-source-file close-file throw
562: 0 c-source-file-id !
1.48 anton 563: [ libtool-command s" --silent --mode=compile --tag=CC " s+
564: libtool-cc append s" -I " append
565: s" includedir" getenv append ] sliteral
1.37 anton 566: s" -O -c " s+ lib-filename 2@ append s" .c -o " append
567: lib-filename 2@ append s" .lo" append ( c-addr u )
1.47 pazsan 568: \ cr 2dup type
1.37 anton 569: 2dup system drop free throw $? abort" libtool compile failed"
1.48 anton 570: [ libtool-command s" --silent --mode=link --tag=CC " s+
571: libtool-cc append s" -module -rpath " s+ ] sliteral
1.49 anton 572: lib-filename 2@ dirname replace-rpath s+ s" " append
1.37 anton 573: lib-filename 2@ append s" .lo -o " append
574: lib-filename 2@ append s" .la" append ( c-addr u )
575: c-libs @ ['] append-l list-map
1.47 pazsan 576: \ 2dup type cr
1.37 anton 577: 2dup system drop free throw $? abort" libtool link failed"
578: open-wrappers dup 0= if
579: .lib-error true abort" open-lib failed"
580: endif
581: ( lib-handle ) lib-handle-addr @ !
1.23 anton 582: endif
1.35 anton 583: lib-filename 2@ drop free throw 0 0 lib-filename 2! ;
584: ' compile-wrapper-function1 IS compile-wrapper-function
1.5 anton 585: \ s" ar rcs xxx.a xxx.o" system
586: \ $? abort" ar generated error" ;
587:
1.12 anton 588: : link-wrapper-function { cff -- sym }
589: cff cff-rtype wrapper-function-name { d: wrapper-name }
1.23 anton 590: wrapper-name cff cff-lha @ @ assert( dup ) lib-sym dup 0= if
1.24 anton 591: .lib-error -&32 throw
1.23 anton 592: endif
1.10 anton 593: wrapper-name drop free throw ;
1.8 anton 594:
1.12 anton 595: : c-function-ft ( xt-defr xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
1.8 anton 596: \ build time/first time action for c-function
1.12 anton 597: init-c-source-file
598: noname create 2, lib-handle-addr @ ,
1.2 anton 599: parse-name { d: c-name }
1.8 anton 600: here parse-function-types c-name string,
1.37 anton 601: ['] gen-wrapper-function c-source-file-execute
1.8 anton 602: does> ( ... -- ... )
1.10 anton 603: dup 2@ { xt-defer xt-cfr }
1.12 anton 604: dup cff-lha @ @ 0= if
605: compile-wrapper-function
606: endif
607: link-wrapper-function xt-cfr >body !
1.8 anton 608: xt-cfr xt-defer defer!
609: xt-cfr execute ;
610:
611: : c-function-rt ( -- )
612: \ run-time definition for c function; addr is the address where
613: \ the sym should be stored
614: noname create 0 ,
1.2 anton 615: does> ( ... -- ... )
616: @ call-c ;
617:
1.18 anton 618: : c-function ( "forth-name" "c-name" "@{type@}" "--" "type" -- ) \ gforth
1.17 anton 619: \G Define a Forth word @i{forth-name}. @i{Forth-name} has the
620: \G specified stack effect and calls the C function @code{c-name}.
1.8 anton 621: defer lastxt dup c-function-rt lastxt c-function-ft
622: lastxt swap defer! ;
1.33 anton 623:
624: : clear-libs ( -- )
625: \G Clear the list of libs
626: c-source-file-id @ if
627: compile-wrapper-function
628: endif
629: 0 c-libs ! ;
630: clear-libs
1.35 anton 631:
632: : c-library-incomplete ( -- )
633: true abort" Called function of unfinished named C library" ;
634:
635: : c-library-name ( c-addr u -- ) \ gforth
1.38 anton 636: \G Start a C library interface with name @i{c-addr u}.
1.35 anton 637: clear-libs
638: ['] c-library-incomplete is compile-wrapper-function
1.40 anton 639: c-named-library-name ;
1.35 anton 640:
641: : c-library ( "name" -- ) \ gforth
642: \G Parsing version of @code{c-library-name}
1.39 anton 643: parse-name save-mem c-library-name ;
1.35 anton 644:
1.38 anton 645: : end-c-library ( -- ) \ gforth
646: \G Finish and (if necessary) build the latest C library interface.
1.35 anton 647: ['] compile-wrapper-function1 is compile-wrapper-function
648: compile-wrapper-function1 ;
1.43 anton 649:
650: : init-libcc ( -- )
651: s" ~/.gforth/libcc-named/" libcc-named-dir-v 2!
1.45 pazsan 652: [IFDEF] make-path
1.43 anton 653: make-path to libcc-path
654: libcc-named-dir libcc-path also-path
655: [ s" libccdir" getenv ] sliteral libcc-path also-path
1.45 pazsan 656: [THEN]
1.43 anton 657: ;
658:
659: init-libcc
660:
661: :noname ( -- )
662: defers 'cold
663: init-libcc ;
664: is 'cold
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>