Annotation of gforth/libcc.fs, revision 1.12

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

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