| \ the function pointer of gforth_c_lseek_ndn_d on the stack and |
\ the function pointer of gforth_c_lseek_ndn_d on the stack and |
| \ calls CALL-C. |
\ calls CALL-C. |
| |
|
| |
\ ToDo: |
| |
|
| |
\ Batching, caching and lazy evaluation: |
| |
|
| |
\ Batching: |
| |
|
| |
\ New words are deferred, and the corresponding C functions are |
| |
\ collected in one file, until the first word is EXECUTEd; then the |
| |
\ file is compiled and linked into the system, and the word is |
| |
\ resolved. |
| |
|
| |
\ Caching: |
| |
|
| |
\ Instead of compiling all this stuff anew for every execution, we |
| |
\ keep the files around and have an index file containing the function |
| |
\ names and their corresponding .so files. If the needed wrapper name |
| |
\ is already present, it is just linked instead of generating the |
| |
\ wrapper again. This is all done by loading the index file(s?), |
| |
\ which define words for the wrappers in a separate wordlist. |
| |
|
| |
\ The files are built in .../lib/gforth/$VERSION/libcc/ or |
| |
\ ~/.gforth/libcc/$HOST/. |
| |
|
| \ other things to do: |
\ other things to do: |
| |
|
| \ c-variable forth-name c-name |
\ c-variable forth-name c-name |
| \ s" ar rcs xxx.a xxx.o" system |
\ s" ar rcs xxx.a xxx.o" system |
| \ $? abort" ar generated error" ; |
\ $? abort" ar generated error" ; |
| |
|
| : link-wrapper-function ( -- ) |
: link-wrapper-function ( -- sym ) |
| s" /home/anton/gforth/xxx.so.1" open-lib ( lib-handle ) |
s" /home/anton/gforth/xxx.so.1" open-lib ( lib-handle ) |
| s" gforth_c_strlen_a_n" rot lib-sym dup 0= -32 and throw ; |
s" gforth_c_strlen_a_n" rot lib-sym dup 0= -&32 and throw ; |
| |
|
| : c-function ( "forth-name" "c-name" "{libcc-type}" "--" "libcc-type" -- ) |
|
| create here >r 0 , \ place for the wrapper function pointer |
: c-function-ft ( xt-defer xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- ) |
| |
\ build time/first time action for c-function |
| |
noname create 2, |
| parse-name { d: c-name } |
parse-name { d: c-name } |
| parse-function-types c-name string, |
here parse-function-types c-name string, |
| r@ cell+ |
s" xxx.c" w/o create-file throw >r ( R:file-id ) |
| s" xxx.c" w/o create-file throw ( file-id ) |
['] gen-wrapper-function r@ outfile-execute |
| dup >r >outfile gen-wrapper-function outfile< |
|
| r> close-file throw |
r> close-file throw |
| |
does> ( ... -- ... ) |
| |
2@ { xt-defer xt-cfr } |
| compile-wrapper-function |
compile-wrapper-function |
| link-wrapper-function |
link-wrapper-function xt-cfr >body ! |
| r> ! |
xt-cfr xt-defer defer! |
| |
xt-cfr execute ; |
| |
|
| |
: c-function-rt ( -- ) |
| |
\ run-time definition for c function; addr is the address where |
| |
\ the sym should be stored |
| |
noname create 0 , |
| does> ( ... -- ... ) |
does> ( ... -- ... ) |
| @ call-c ; |
@ call-c ; |
| |
|
| |
: c-function ( "forth-name" "c-name" "{libcc-type}" "--" "libcc-type" -- ) |
| |
defer lastxt dup c-function-rt lastxt c-function-ft |
| |
lastxt swap defer! ; |
| |
|
| s" Library not found" exception constant err-nolib |
s" Library not found" exception constant err-nolib |
| |
|
| \ c-function func test5 -- func |
\ c-function func test5 -- func |
| \ c-function void test6 -- void |
\ c-function void test6 -- void |
| |
|
| |
\c #include <string.h> |
| |
|
| c-function strlen strlen a -- n |
c-function strlen strlen a -- n |
| |
|
| cr s\" fooo\0" 2dup dump drop .s strlen cr .s cr |
cr s\" fooo\0" 2dup dump drop .s strlen cr .s cr |