Annotation of gforth/libcc.fs, revision 1.9
1.1 anton 1: \ libcc.fs foreign function interface implemented using a C compiler
2:
3: \ Copyright (C) 2006 Free Software Foundation, Inc.
4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20:
21:
22: \ What this implementation does is this: if it sees a declaration like
23:
1.2 anton 24: \ \ something that tells it that the current library is libc
1.6 anton 25: \ \c #include <unistd.h>
1.2 anton 26: \ c-function dlseek lseek n d n -- d
1.1 anton 27:
28: \ it genererates C code similar to the following:
29:
30: \ #include <gforth.h>
1.2 anton 31: \ #include <unistd.h>
1.1 anton 32: \
1.2 anton 33: \ void gforth_c_lseek_ndn_d(void)
1.1 anton 34: \ {
35: \ Cell *sp = gforth_SP;
36: \ Float *fp = gforth_FP;
1.2 anton 37: \ long long result; /* longest type in C */
38: \ gforth_ll2d(lseek(sp[3],gforth_d2ll(sp[2],sp[1]),sp[0]),sp[3],sp[2]);
39: \ gforth_SP = sp+2;
1.1 anton 40: \ }
41:
42: \ Then it compiles this code and dynamically links it into the Gforth
43: \ system (batching and caching are future work). It also dynamically
44: \ links lseek. Performing DLSEEK then puts the function pointer of
1.2 anton 45: \ the function pointer of gforth_c_lseek_ndn_d on the stack and
46: \ calls CALL-C.
47:
1.7 anton 48: \ ToDo:
49:
50: \ Batching, caching and lazy evaluation:
51:
52: \ Batching:
53:
54: \ New words are deferred, and the corresponding C functions are
55: \ collected in one file, until the first word is EXECUTEd; then the
56: \ file is compiled and linked into the system, and the word is
57: \ resolved.
58:
59: \ Caching:
60:
61: \ Instead of compiling all this stuff anew for every execution, we
62: \ keep the files around and have an index file containing the function
63: \ names and their corresponding .so files. If the needed wrapper name
64: \ is already present, it is just linked instead of generating the
65: \ wrapper again. This is all done by loading the index file(s?),
66: \ which define words for the wrappers in a separate wordlist.
67:
68: \ The files are built in .../lib/gforth/$VERSION/libcc/ or
69: \ ~/.gforth/libcc/$HOST/.
70:
1.2 anton 71: \ other things to do:
72:
73: \ c-variable forth-name c-name
74: \ c-constant forth-name c-name
75:
76:
77: \ data structures
78:
79: \ c-function word body:
80: \ cell function pointer
81: \ char return type index
82: \ char parameter count n
83: \ char*n parameters (type indices)
84: \ counted string: c-name
85:
1.3 anton 86: : .nb ( n -- )
1.2 anton 87: 0 .r ;
88:
89: : const+ ( n1 "name" -- n2 )
90: dup constant 1+ ;
91:
1.6 anton 92: \ linked list stuff (should go elsewhere)
93:
94: hex
95:
96: require struct.fs
97:
98: struct
99: cell% field list-next
100: 1 0 field list-payload
101: end-struct list%
102:
103: : list-insert { node list -- }
104: list list-next @ node list-next !
105: node list list-next ! ;
106:
107: : list-append { node endlistp -- }
108: \ insert node at place pointed to by endlistp
109: node endlistp @ list-insert
110: node list-next endlistp ! ;
111:
112: : list-map ( ... list xt -- ... )
113: \ xt ( ... node -- ... )
114: { xt } begin { node }
115: node while
116: node xt execute
117: node list-next @
118: repeat ;
119:
120: \ C prefix lines
121:
122: \ linked list of longcstrings: [ link | count-cell | characters ]
123:
124: list%
125: cell% field c-prefix-count
126: 1 0 field c-prefix-chars
127: end-struct c-prefix%
128:
129: variable c-prefix-lines 0 c-prefix-lines !
130: variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
131:
132: : save-c-prefix-line ( c-addr u -- )
133: align here 0 , c-prefix-lines-end list-append ( c-addr u )
134: longstring, ;
135:
136: : \c ( "rest-of-line" -- )
137: -1 parse save-c-prefix-line ;
138:
139: : print-c-prefix-line ( node -- )
140: dup c-prefix-chars swap c-prefix-count @ type cr ;
141:
142: : print-c-prefix-lines ( -- )
143: c-prefix-lines @ ['] print-c-prefix-line list-map ;
144:
145: \c #include "engine/libcc.h"
1.5 anton 146:
1.6 anton 147: print-c-prefix-lines
1.5 anton 148:
1.6 anton 149: \ Types (for parsing)
1.5 anton 150:
1.2 anton 151: wordlist constant libcc-types
152:
153: get-current libcc-types set-current
154:
155: \ index values
156: -1
157: const+ -- \ end of arguments
158: const+ n \ integer cell
1.5 anton 159: const+ a \ address cell
1.2 anton 160: const+ d \ double
161: const+ r \ float
162: const+ func \ C function pointer
163: const+ void
164: drop
165:
166: set-current
167:
168: : parse-libcc-type ( "libcc-type" -- u )
169: parse-name libcc-types search-wordlist 0= -13 and throw execute ;
170:
171: : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
172: here 2 chars allot here begin
173: parse-libcc-type dup 0>= while
174: c,
175: repeat
1.3 anton 176: drop here swap - over char+ c!
177: parse-libcc-type dup 0< -32 and throw swap c! ;
1.2 anton 178:
179: : type-letter ( n -- c )
1.5 anton 180: chars s" nadrfv" drop + c@ ;
1.2 anton 181:
182: \ count-stacks
183:
184: : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
185: 1+ ;
186:
1.5 anton 187: : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
1.2 anton 188: 1+ ;
189:
190: : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
191: 2 + ;
192:
193: : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
194: swap 1+ swap ;
195:
196: : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
197: 1+ ;
198:
199: : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
200: ;
201:
202: create count-stacks-types
203: ' count-stacks-n ,
1.5 anton 204: ' count-stacks-a ,
1.2 anton 205: ' count-stacks-d ,
206: ' count-stacks-r ,
207: ' count-stacks-func ,
208: ' count-stacks-void ,
209:
210: : count-stacks ( pars -- fp-change sp-change )
211: \ pars is an addr u pair
212: 0 0 2swap over + swap u+do
1.3 anton 213: i c@ cells count-stacks-types + @ execute
1.2 anton 214: loop ;
215:
216: \ gen-pars
217:
218: : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.5 anton 219: ." sp[" 1- dup .nb ." ]" ;
1.2 anton 220:
1.5 anton 221: : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.2 anton 222: ." (void *)(" gen-par-n ." )" ;
223:
224: : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.4 anton 225: ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
1.2 anton 226:
227: : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.3 anton 228: swap 1- tuck ." fp[" .nb ." ]" ;
1.2 anton 229:
230: : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.5 anton 231: gen-par-a ;
1.2 anton 232:
233: : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
234: -32 throw ;
235:
236: create gen-par-types
237: ' gen-par-n ,
1.5 anton 238: ' gen-par-a ,
1.2 anton 239: ' gen-par-d ,
240: ' gen-par-r ,
241: ' gen-par-func ,
242: ' gen-par-void ,
243:
244: : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
1.3 anton 245: cells gen-par-types + @ execute ;
1.2 anton 246:
247: \ the call itself
248:
249: : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
250: c-name type ." ("
1.3 anton 251: fp-change1 sp-change1 pars over + swap u+do
1.2 anton 252: i c@ gen-par
253: i 1+ i' < if
254: ." ,"
255: endif
256: loop
257: 2drop ." )" ;
258:
259: \ calls for various kinds of return values
260:
261: : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
262: 2dup 2>r gen-wrapped-call 2r> ;
263:
1.3 anton 264: : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5 anton 265: 2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
1.3 anton 266:
1.5 anton 267: : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
268: 2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
1.3 anton 269:
270: : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
271: ." gforth_ll2d(" gen-wrapped-void
1.5 anton 272: ." ," gen-par-n ." ," gen-par-n ." )" ;
1.3 anton 273:
274: : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5 anton 275: 2dup gen-par-r 2>r ." =" gen-wrapped-void 2r> ;
1.3 anton 276:
277: : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5 anton 278: gen-wrapped-a ;
1.3 anton 279:
1.2 anton 280: create gen-wrapped-types
281: ' gen-wrapped-n ,
1.5 anton 282: ' gen-wrapped-a ,
1.2 anton 283: ' gen-wrapped-d ,
284: ' gen-wrapped-r ,
285: ' gen-wrapped-func ,
286: ' gen-wrapped-void ,
287:
288: : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
1.3 anton 289: cells gen-wrapped-types + @ execute ;
1.2 anton 290:
291: : gen-wrapper-function ( addr -- )
292: \ addr points to the return type index of a c-function descriptor
293: c@+ { ret } count 2dup { d: pars } chars + count { d: c-name }
1.6 anton 294: print-c-prefix-lines
1.2 anton 295: ." void gforth_c_" c-name type ." _"
1.5 anton 296: pars bounds u+do
297: i c@ type-letter emit
1.2 anton 298: loop
299: ." _" ret type-letter emit .\" (void)\n"
1.4 anton 300: .\" {\n Cell MAYBE_UNUSED *sp = gforth_SP;\n Float MAYBE_UNUSED *fp = gforth_FP;\n "
1.2 anton 301: pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
302: ?dup-if
1.3 anton 303: ." gforth_SP = sp+" .nb .\" ;\n"
1.2 anton 304: endif
305: ?dup-if
1.3 anton 306: ." gforth_FP = fp+" .nb .\" ;\n"
1.2 anton 307: endif
1.3 anton 308: .\" }\n" ;
1.2 anton 309:
1.5 anton 310: : compile-wrapper-function ( -- )
311: s" gcc -fPIC -shared -Wl,-soname,xxx.so.1 -Wl,-export_dynamic -o xxx.so.1 -O xxx.c" system
312: $? abort" compiler generated error" ;
313: \ s" ar rcs xxx.a xxx.o" system
314: \ $? abort" ar generated error" ;
315:
1.8 anton 316: : link-wrapper-function ( -- sym )
1.5 anton 317: s" /home/anton/gforth/xxx.so.1" open-lib ( lib-handle )
1.8 anton 318: s" gforth_c_strlen_a_n" rot lib-sym dup 0= -&32 and throw ;
1.5 anton 319:
1.8 anton 320:
321: : c-function-ft ( xt-defer xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
322: \ build time/first time action for c-function
323: noname create 2,
1.2 anton 324: parse-name { d: c-name }
1.8 anton 325: here parse-function-types c-name string,
1.9 ! anton 326: s" xxx.c" w/o create-file throw >r ( R:file-id )
! 327: ['] gen-wrapper-function r@ outfile-execute
1.5 anton 328: r> close-file throw
1.8 anton 329: does> ( ... -- ... )
330: 2@ { xt-defer xt-cfr }
1.5 anton 331: compile-wrapper-function
1.8 anton 332: link-wrapper-function xt-cfr >body !
333: xt-cfr xt-defer defer!
334: xt-cfr execute ;
335:
336: : c-function-rt ( -- )
337: \ run-time definition for c function; addr is the address where
338: \ the sym should be stored
339: noname create 0 ,
1.2 anton 340: does> ( ... -- ... )
341: @ call-c ;
342:
1.8 anton 343: : c-function ( "forth-name" "c-name" "{libcc-type}" "--" "libcc-type" -- )
344: defer lastxt dup c-function-rt lastxt c-function-ft
345: lastxt swap defer! ;
1.1 anton 346:
347: s" Library not found" exception constant err-nolib
348:
349: : library ( "name" "file" -- ) \ gforth
350: \G Dynamically links the library specified by @i{file}. Defines a
351: \G word @i{name} ( -- lib ) that starts the declaration of a
352: \G function from that library.
353: create parse-name open-lib dup 0= err-nolib and throw ,
354: does> ( -- lib )
355: @ ;
356:
1.3 anton 357: \ test
1.1 anton 358:
1.5 anton 359: \ test all parameter and return types
360:
361: \ cr .( #include "engine/libcc.h")
362: \ cr .( #include <unistd.h>)
363: \ cr ." typedef void (* func)(int);
364: \ cr ." int test1(int,char*,long,double,void (*)(int));"
365: \ cr ." Cell *test2(void);"
366: \ cr ." int test3(void);"
367: \ cr ." float test4(void);"
368: \ cr ." func test5(void);"
369: \ cr ." void test6(void);"
370: \ cr
371:
372: \ c-function dlseek lseek n d n -- d
373: \ c-function n test1 n a d r func -- n
374: \ c-function a test2 -- a
375: \ c-function d test3 -- d
376: \ c-function r test4 -- r
377: \ c-function func test5 -- func
378: \ c-function void test6 -- void
1.7 anton 379:
380: \c #include <string.h>
1.5 anton 381:
382: c-function strlen strlen a -- n
383:
384: cr s\" fooo\0" 2dup dump drop .s strlen cr .s cr
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>