Annotation of gforth/libcc.fs, revision 1.10

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: 
1.10    ! anton      79: \ For every c-function, we have three words: two anonymous words
        !            80: \ created by c-function-ft (first time) and c-function-rt (run-time),
        !            81: \ and a named deferred word.  The deferred word first points to the
        !            82: \ first-time word, then to the run-time word; the run-time word calls
        !            83: \ the c function.
        !            84: 
        !            85: \ c-function-ft word body:
        !            86: \  cell xt of c-function-rt word
        !            87: \  cell xt of c-function deferred word 
1.2       anton      88: \  char return type index
                     89: \  char parameter count n
                     90: \  char*n parameters (type indices)
                     91: \  counted string: c-name
                     92: 
1.3       anton      93: : .nb ( n -- )
1.2       anton      94:     0 .r ;
                     95: 
                     96: : const+ ( n1 "name" -- n2 )
                     97:     dup constant 1+ ;
                     98: 
1.10    ! anton      99: : front-string { c-addr1 u1 c-addr2 u2 -- c-addr3 u3 }
        !           100:     \ insert string c-addr2 u2 in buffer c-addr1 u1; c-addr3 u3 is the
        !           101:     \ remainder of the buffer.
        !           102:     assert( u1 u2 u>= )
        !           103:     c-addr2 c-addr1 u2 move
        !           104:     c-addr1 u1 u2 /string ;
        !           105: 
        !           106: : front-char { c-addr1 u1 c -- c-addr3 u2 }
        !           107:     \ insert c in buffer c-addr1 u1; c-addr3 u3 is the remainder of
        !           108:     \ the buffer.
        !           109:     assert( u1 0 u> )
        !           110:     c c-addr1 c!
        !           111:     c-addr1 u1 1 /string ;
        !           112: 
1.6       anton     113: \ linked list stuff (should go elsewhere)
                    114: 
                    115: hex
                    116: 
                    117: require struct.fs
                    118: 
                    119: struct
                    120:     cell% field list-next
                    121:     1 0   field list-payload
                    122: end-struct list%
                    123: 
                    124: : list-insert { node list -- }
                    125:     list list-next @ node list-next !
                    126:     node list list-next ! ;
                    127: 
                    128: : list-append { node endlistp -- }
                    129:     \ insert node at place pointed to by endlistp
                    130:     node endlistp @ list-insert
                    131:     node list-next endlistp ! ;
                    132: 
                    133: : list-map ( ... list xt -- ... )
                    134:     \ xt ( ... node -- ... )
                    135:     { xt } begin { node }
                    136:        node while
                    137:            node xt execute
                    138:            node list-next @
                    139:     repeat ;
                    140: 
                    141: \ C prefix lines
                    142: 
                    143: \ linked list of longcstrings: [ link | count-cell | characters ]
                    144: 
                    145: list%
                    146:     cell% field c-prefix-count
                    147:     1 0   field c-prefix-chars
                    148: end-struct c-prefix%
                    149: 
                    150: variable c-prefix-lines 0 c-prefix-lines !
                    151: variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
                    152: 
                    153: : save-c-prefix-line ( c-addr u -- )
                    154:     align here 0 , c-prefix-lines-end list-append ( c-addr u )
                    155:     longstring, ;
                    156: 
                    157: : \c ( "rest-of-line" -- )
                    158:     -1 parse save-c-prefix-line ;
                    159: 
                    160: : print-c-prefix-line ( node -- )
                    161:     dup c-prefix-chars swap c-prefix-count @ type cr ;
                    162: 
                    163: : print-c-prefix-lines ( -- )
                    164:     c-prefix-lines @ ['] print-c-prefix-line list-map ;
                    165: 
                    166: \c #include "engine/libcc.h"
1.5       anton     167: 
1.6       anton     168: print-c-prefix-lines
1.5       anton     169: 
1.6       anton     170: \ Types (for parsing)
1.5       anton     171: 
1.2       anton     172: wordlist constant libcc-types
                    173: 
                    174: get-current libcc-types set-current
                    175: 
                    176: \ index values
                    177: -1
                    178: const+ -- \ end of arguments
                    179: const+ n \ integer cell
1.5       anton     180: const+ a \ address cell
1.2       anton     181: const+ d \ double
                    182: const+ r \ float
                    183: const+ func \ C function pointer
                    184: const+ void
                    185: drop
                    186: 
                    187: set-current
                    188: 
                    189: : parse-libcc-type ( "libcc-type" -- u )
                    190:     parse-name libcc-types search-wordlist 0= -13 and throw execute ;
                    191: 
                    192: : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
                    193:     here 2 chars allot here begin
                    194:        parse-libcc-type dup 0>= while
                    195:            c,
                    196:     repeat
1.3       anton     197:     drop here swap - over char+ c!
                    198:     parse-libcc-type dup 0< -32 and throw swap c! ;
1.2       anton     199: 
                    200: : type-letter ( n -- c )
1.5       anton     201:     chars s" nadrfv" drop + c@ ;
1.2       anton     202: 
                    203: \ count-stacks
                    204: 
                    205: : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    206:     1+ ;
                    207: 
1.5       anton     208: : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
1.2       anton     209:     1+ ;
                    210: 
                    211: : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    212:     2 + ;
                    213: 
                    214: : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    215:     swap 1+ swap ;
                    216: 
                    217: : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    218:     1+ ;
                    219: 
                    220: : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    221: ;
                    222: 
                    223: create count-stacks-types
                    224: ' count-stacks-n ,
1.5       anton     225: ' count-stacks-a ,
1.2       anton     226: ' count-stacks-d ,
                    227: ' count-stacks-r ,
                    228: ' count-stacks-func ,
                    229: ' count-stacks-void ,
                    230: 
                    231: : count-stacks ( pars -- fp-change sp-change )
                    232:     \ pars is an addr u pair
                    233:     0 0 2swap over + swap u+do
1.3       anton     234:        i c@ cells count-stacks-types + @ execute
1.2       anton     235:     loop ;
                    236: 
                    237: \ gen-pars
                    238: 
                    239: : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.5       anton     240:     ." sp[" 1- dup .nb ." ]" ;
1.2       anton     241: 
1.5       anton     242: : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.2       anton     243:     ." (void *)(" gen-par-n ." )" ;
                    244: 
                    245: : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.4       anton     246:     ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
1.2       anton     247: 
                    248: : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.3       anton     249:     swap 1- tuck ." fp[" .nb ." ]" ;
1.2       anton     250: 
                    251: : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.5       anton     252:     gen-par-a ;
1.2       anton     253: 
                    254: : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
                    255:     -32 throw ;
                    256: 
                    257: create gen-par-types
                    258: ' gen-par-n ,
1.5       anton     259: ' gen-par-a ,
1.2       anton     260: ' gen-par-d ,
                    261: ' gen-par-r ,
                    262: ' gen-par-func ,
                    263: ' gen-par-void ,
                    264: 
                    265: : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
1.3       anton     266:     cells gen-par-types + @ execute ;
1.2       anton     267: 
                    268: \ the call itself
                    269: 
                    270: : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
                    271:     c-name type ." ("
1.3       anton     272:     fp-change1 sp-change1 pars over + swap u+do 
1.2       anton     273:        i c@ gen-par
                    274:        i 1+ i' < if
                    275:            ." ,"
                    276:        endif
                    277:     loop
                    278:     2drop ." )" ;
                    279: 
                    280: \ calls for various kinds of return values
                    281: 
                    282: : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
                    283:     2dup 2>r gen-wrapped-call 2r> ;
                    284: 
1.3       anton     285: : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5       anton     286:     2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
1.3       anton     287: 
1.5       anton     288: : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
                    289:     2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
1.3       anton     290: 
                    291: : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
                    292:     ." gforth_ll2d(" gen-wrapped-void
1.5       anton     293:     ." ," gen-par-n ." ," gen-par-n ." )" ;
1.3       anton     294: 
                    295: : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5       anton     296:     2dup gen-par-r 2>r ." =" gen-wrapped-void 2r> ;
1.3       anton     297: 
                    298: : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5       anton     299:     gen-wrapped-a ;
1.3       anton     300: 
1.2       anton     301: create gen-wrapped-types
                    302: ' gen-wrapped-n ,
1.5       anton     303: ' gen-wrapped-a ,
1.2       anton     304: ' gen-wrapped-d ,
                    305: ' gen-wrapped-r ,
                    306: ' gen-wrapped-func ,
                    307: ' gen-wrapped-void ,
                    308: 
                    309: : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
1.3       anton     310:     cells gen-wrapped-types + @ execute ;
1.2       anton     311: 
1.10    ! anton     312: : wrapper-function-name ( addr -- c-addr u )
        !           313:     \ addr points to the return type index of a c-function descriptor
        !           314:     count { r-type } count { d: pars }
        !           315:     pars + count { d: c-name }
        !           316:     s" gforth_c_" { d: prefix }
        !           317:     prefix nip c-name nip + pars nip + 3 + { u }
        !           318:     u allocate throw { c-addr }
        !           319:     c-addr u
        !           320:     prefix front-string c-name front-string '_ front-char
        !           321:     pars bounds u+do
        !           322:        i c@ type-letter front-char
        !           323:     loop
        !           324:     '_ front-char r-type type-letter front-char assert( dup 0= )
        !           325:     2drop c-addr u ;
        !           326: 
1.2       anton     327: : gen-wrapper-function ( addr -- )
                    328:     \ addr points to the return type index of a c-function descriptor
1.10    ! anton     329:     dup { descriptor }
1.2       anton     330:     c@+ { ret } count 2dup { d: pars } chars + count { d: c-name }
1.6       anton     331:     print-c-prefix-lines
1.10    ! anton     332:     ." void " descriptor wrapper-function-name 2dup type drop free throw
        !           333:     .\" (void)\n"
1.4       anton     334:     .\" {\n  Cell MAYBE_UNUSED *sp = gforth_SP;\n  Float MAYBE_UNUSED *fp = gforth_FP;\n  "
1.2       anton     335:     pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
                    336:     ?dup-if
1.3       anton     337:        ."   gforth_SP = sp+" .nb .\" ;\n"
1.2       anton     338:     endif
                    339:     ?dup-if
1.3       anton     340:        ."   gforth_FP = fp+" .nb .\" ;\n"
1.2       anton     341:     endif
1.3       anton     342:     .\" }\n" ;
1.2       anton     343: 
1.5       anton     344: : compile-wrapper-function ( -- )
                    345:     s" gcc -fPIC -shared -Wl,-soname,xxx.so.1 -Wl,-export_dynamic -o xxx.so.1 -O xxx.c" system
                    346:     $? abort" compiler generated error" ;
                    347: \    s" ar rcs xxx.a xxx.o" system
                    348: \    $? abort" ar generated error" ;
                    349: 
1.10    ! anton     350: : link-wrapper-function ( addr -- sym )
        !           351:     wrapper-function-name { d: wrapper-name }
1.5       anton     352:     s" /home/anton/gforth/xxx.so.1" open-lib ( lib-handle )
1.10    ! anton     353:     wrapper-name rot lib-sym dup 0= -&32 and throw
        !           354:     wrapper-name drop free throw ;
1.8       anton     355: 
                    356: : c-function-ft ( xt-defer xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
                    357:     \ build time/first time action for c-function
                    358:     noname create 2,
1.2       anton     359:     parse-name { d: c-name }
1.8       anton     360:     here parse-function-types c-name string,
1.9       anton     361:     s" xxx.c" w/o create-file throw >r ( R:file-id )
                    362:     ['] gen-wrapper-function r@ outfile-execute
1.5       anton     363:     r> close-file throw
1.8       anton     364:   does> ( ... -- ... )
1.10    ! anton     365:     dup 2@ { xt-defer xt-cfr }
1.5       anton     366:     compile-wrapper-function
1.10    ! anton     367:     2 cells + link-wrapper-function xt-cfr >body !
1.8       anton     368:     xt-cfr xt-defer defer!
                    369:     xt-cfr execute ;
                    370: 
                    371: : c-function-rt ( -- )
                    372:     \ run-time definition for c function; addr is the address where
                    373:     \ the sym should be stored
                    374:     noname create 0 ,
1.2       anton     375:   does> ( ... -- ... )
                    376:     @ call-c ;
                    377: 
1.8       anton     378: : c-function ( "forth-name" "c-name" "{libcc-type}" "--" "libcc-type" -- )
                    379:     defer lastxt dup c-function-rt lastxt c-function-ft
                    380:     lastxt swap defer! ;
1.1       anton     381: 
                    382: s" Library not found" exception constant err-nolib
                    383: 
                    384: : library ( "name" "file" -- ) \ gforth
                    385: \G Dynamically links the library specified by @i{file}.  Defines a
                    386: \G word @i{name} ( -- lib ) that starts the declaration of a
                    387: \G function from that library.
                    388:     create parse-name open-lib dup 0= err-nolib and throw ,
                    389:   does> ( -- lib )
                    390:     @ ;
                    391: 
1.3       anton     392: \ test
1.1       anton     393: 
1.5       anton     394: \ test all parameter and return types
                    395: 
                    396: \ cr .( #include "engine/libcc.h")
                    397: \ cr .( #include <unistd.h>)
                    398: \ cr ." typedef void (* func)(int);
                    399: \ cr ." int test1(int,char*,long,double,void (*)(int));"
                    400: \ cr ." Cell *test2(void);"
                    401: \ cr ." int test3(void);"
                    402: \ cr ." float test4(void);"
                    403: \ cr ." func test5(void);"
                    404: \ cr ." void test6(void);"
                    405: \ cr
                    406: 
                    407: \ c-function dlseek lseek n d n -- d
                    408: \ c-function n test1 n a d r func -- n
                    409: \ c-function a test2 -- a
                    410: \ c-function d test3 -- d
                    411: \ c-function r test4 -- r
                    412: \ c-function func test5 -- func
                    413: \ c-function void test6 -- void
1.7       anton     414: 
                    415: \c #include <string.h>
1.5       anton     416: 
                    417: c-function strlen strlen a -- n
                    418: 
                    419: cr s\" fooo\0" 2dup dump drop .s strlen cr .s cr

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>