File:  [gforth] / gforth / libcc.fs
Revision 1.9: download - view: text, annotated - select for diffs
Fri Feb 23 22:33:21 2007 UTC (12 years, 5 months ago) by anton
Branches: MAIN
CVS tags: HEAD
changed >OUTFILE ... OUTFILE< to OUTFILE-EXECUTE
changed >INFILE ... INFILE< to INFILE-EXECUTE
added BASE-EXECUTE
related documentation changes

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

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