File:  [gforth] / gforth / libcc.fs
Revision 1.14: download - view: text, annotated - select for diffs
Mon May 28 12:07:30 2007 UTC (12 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
minor libcc.fs bugfix

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

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