| 1 : |
anton
|
1.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 : |
|
|
|
| 24 : |
|
|
\ libc dlseek int dlong int (dlong) lseek ( fd doffset whence -- doffset2 ) |
| 25 : |
|
|
|
| 26 : |
|
|
\ it genererates C code similar to the following: |
| 27 : |
|
|
|
| 28 : |
|
|
\ #include <gforth.h> |
| 29 : |
|
|
\ |
| 30 : |
|
|
\ void gforth_call_dl_i_dl_i(void) |
| 31 : |
|
|
\ { |
| 32 : |
|
|
\ Cell *sp = gforth_SP; |
| 33 : |
|
|
\ Float *fp = gforth_FP; |
| 34 : |
|
|
\ long (*func)(int, long, int); |
| 35 : |
|
|
\ int arg1; |
| 36 : |
|
|
\ long arg2; |
| 37 : |
|
|
\ int arg3; |
| 38 : |
|
|
\ long result; |
| 39 : |
|
|
\ func = (char *)((Cell *)sp)[0]; |
| 40 : |
|
|
\ arg3 = ((Cell *)sp)[1]; |
| 41 : |
|
|
\ arg2 = gforth_d2ll(sp[3],sp[2]); |
| 42 : |
|
|
\ arg1 = ((Cell *)sp)[4]; |
| 43 : |
|
|
\ result = func(arg1, arg2, arg3); |
| 44 : |
|
|
\ gforth_ll2d(result, sp[4], sp[3]); |
| 45 : |
|
|
\ gforth_SP += 3; |
| 46 : |
|
|
\ } |
| 47 : |
|
|
|
| 48 : |
|
|
\ Then it compiles this code and dynamically links it into the Gforth |
| 49 : |
|
|
\ system (batching and caching are future work). It also dynamically |
| 50 : |
|
|
\ links lseek. Performing DLSEEK then puts the function pointer of |
| 51 : |
|
|
\ lseek() on the stack, the function pointer of |
| 52 : |
|
|
\ gforth_call_del_i_dl_i, and calls CALL-C. |
| 53 : |
|
|
|
| 54 : |
|
|
|
| 55 : |
|
|
s" Library not found" exception constant err-nolib |
| 56 : |
|
|
|
| 57 : |
|
|
: library ( "name" "file" -- ) \ gforth |
| 58 : |
|
|
\G Dynamically links the library specified by @i{file}. Defines a |
| 59 : |
|
|
\G word @i{name} ( -- lib ) that starts the declaration of a |
| 60 : |
|
|
\G function from that library. |
| 61 : |
|
|
create parse-name open-lib dup 0= err-nolib and throw , |
| 62 : |
|
|
does> ( -- lib ) |
| 63 : |
|
|
@ ; |
| 64 : |
|
|
|
| 65 : |
|
|
|