Annotation of gforth/libcc.fs, revision 1.18

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

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