File:  [gforth] / gforth / libcc.fs
Revision 1.12: download - view: text, annotated - select for diffs
Wed Apr 25 22:11:29 2007 UTC (12 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
more libcc work: now can handle multiple functions (one batch)

    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: 
  103: : .nb ( n -- )
  104:     0 .r ;
  105: 
  106: : const+ ( n1 "name" -- n2 )
  107:     dup constant 1+ ;
  108: 
  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: 
  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"
  175: 
  176: \ Types (for parsing)
  177: 
  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
  186: const+ a \ address cell
  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
  203:     drop here swap - over char+ c!
  204:     parse-libcc-type dup 0< -32 and throw swap c! ;
  205: 
  206: : type-letter ( n -- c )
  207:     chars s" nadrfv" drop + c@ ;
  208: 
  209: \ count-stacks
  210: 
  211: : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  212:     1+ ;
  213: 
  214: : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  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 ,
  231: ' count-stacks-a ,
  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
  240: 	i c@ cells count-stacks-types + @ execute
  241:     loop ;
  242: 
  243: \ gen-pars
  244: 
  245: : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  246:     ." sp[" 1- dup .nb ." ]" ;
  247: 
  248: : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  249:     ." (void *)(" gen-par-n ." )" ;
  250: 
  251: : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  252:     ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
  253: 
  254: : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  255:     swap 1- tuck ." fp[" .nb ." ]" ;
  256: 
  257: : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  258:     gen-par-a ;
  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 ,
  265: ' gen-par-a ,
  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 )
  272:     cells gen-par-types + @ execute ;
  273: 
  274: \ the call itself
  275: 
  276: : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
  277:     c-name type ." ("
  278:     fp-change1 sp-change1 pars over + swap u+do 
  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: 
  291: : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  292:     2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
  293: 
  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> ;
  296: 
  297: : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  298:     ." gforth_ll2d(" gen-wrapped-void
  299:     ." ," gen-par-n ." ," gen-par-n ." )" ;
  300: 
  301: : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  302:     2dup gen-par-r 2>r ." =" gen-wrapped-void 2r> ;
  303: 
  304: : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  305:     gen-wrapped-a ;
  306: 
  307: create gen-wrapped-types
  308: ' gen-wrapped-n ,
  309: ' gen-wrapped-a ,
  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 )
  316:     cells gen-wrapped-types + @ execute ;
  317: 
  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: 
  333: : gen-wrapper-function ( addr -- )
  334:     \ addr points to the return type index of a c-function descriptor
  335:     dup { descriptor }
  336:     c@+ { ret } count 2dup { d: pars } chars + count { d: c-name }
  337:     ." void " descriptor wrapper-function-name 2dup type drop free throw
  338:     .\" (void)\n"
  339:     .\" {\n  Cell MAYBE_UNUSED *sp = gforth_SP;\n  Float MAYBE_UNUSED *fp = gforth_FP;\n  "
  340:     pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
  341:     ?dup-if
  342: 	."   gforth_SP = sp+" .nb .\" ;\n"
  343:     endif
  344:     ?dup-if
  345: 	."   gforth_FP = fp+" .nb .\" ;\n"
  346:     endif
  347:     .\" }\n" ;
  348: 
  349: variable c-source-file-id \ contains the source file id of the current batch
  350: 0 c-source-file-id !
  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 ;
  361: 
  362: : c-source-file ( -- file-id )
  363:     c-source-file-id @ assert( dup ) ;
  364: 
  365: : compile-wrapper-function ( -- )
  366:     c-source-file close-file throw
  367:     0 c-source-file-id !
  368:     s" gcc -fPIC -shared -Wl,-soname,xxx.so.1 -Wl,-export_dynamic -o xxx.so.1 -O xxx.c" system
  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 @ ! ;
  372: \    s" ar rcs xxx.a xxx.o" system
  373: \    $? abort" ar generated error" ;
  374: 
  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
  378:     wrapper-name drop free throw ;
  379: 
  380: : c-function-ft ( xt-defr xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
  381:     \ build time/first time action for c-function
  382:     init-c-source-file
  383:     noname create 2, lib-handle-addr @ ,
  384:     parse-name { d: c-name }
  385:     here parse-function-types c-name string,
  386:     ['] gen-wrapper-function c-source-file outfile-execute
  387:   does> ( ... -- ... )
  388:     dup 2@ { xt-defer xt-cfr }
  389:     dup cff-lha @ @ 0= if
  390: 	compile-wrapper-function
  391:     endif
  392:     link-wrapper-function xt-cfr >body !
  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 ,
  400:   does> ( ... -- ... )
  401:     @ call-c ;
  402: 
  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! ;
  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: 
  417: \ test
  418: 
  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
  439: 
  440: \c #include <string.h>
  441: \c #include <stdlib.h>
  442: 
  443: c-function strlen strlen a -- n
  444: c-function labs labs n -- n
  445: 
  446: cr s\" fooo\0" 2dup dump drop .s strlen cr .s drop cr 
  447: -5 labs .s drop cr

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