File:  [gforth] / gforth / libcc.fs
Revision 1.31: download - view: text, annotated - select for diffs
Sat Apr 19 19:15:14 2008 UTC (11 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
CPPFLAGS are now used by the Makefiles
bugfix in libcc.fs
OPEN-LIB now can open libraries without extension and the library's
   symbols become global
libltdl from libtool-2.2 or higher required (will be included soon)

    1: \ libcc.fs	foreign function interface implemented using a C compiler
    2: 
    3: \ Copyright (C) 2006,2007 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 3
   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, see http://www.gnu.org/licenses/.
   19: 
   20: 
   21: \ What this implementation does is this: if it sees a declaration like
   22: 
   23: \ \ something that tells it that the current library is libc
   24: \ \c #include <unistd.h>
   25: \ c-function dlseek lseek n d n -- d
   26: 
   27: \ it genererates C code similar to the following:
   28: 
   29: \ #include <gforth.h>
   30: \ #include <unistd.h>
   31: \ 
   32: \ void gforth_c_lseek_ndn_d(void)
   33: \ {
   34: \   Cell *sp = gforth_SP;
   35: \   Float *fp = gforth_FP;
   36: \   long long result;  /* longest type in C */
   37: \   gforth_ll2d(lseek(sp[3],gforth_d2ll(sp[2],sp[1]),sp[0]),sp[3],sp[2]);
   38: \   gforth_SP = sp+2;
   39: \ }
   40: 
   41: \ Then it compiles this code and dynamically links it into the Gforth
   42: \ system (batching and caching are future work).  It also dynamically
   43: \ links lseek.  Performing DLSEEK then puts the function pointer of
   44: \ the function pointer of gforth_c_lseek_ndn_d on the stack and
   45: \ calls CALL-C.
   46: 
   47: \ ToDo:
   48: 
   49: \ Batching, caching and lazy evaluation:
   50: 
   51: \ Batching:
   52: 
   53: \ New words are deferred, and the corresponding C functions are
   54: \ collected in one file, until the first word is EXECUTEd; then the
   55: \ file is compiled and linked into the system, and the word is
   56: \ resolved.
   57: 
   58: \ Caching:
   59: 
   60: \ Instead of compiling all this stuff anew for every execution, we
   61: \ keep the files around and have an index file containing the function
   62: \ names and their corresponding .so files.  If the needed wrapper name
   63: \ is already present, it is just linked instead of generating the
   64: \ wrapper again.  This is all done by loading the index file(s?),
   65: \ which define words for the wrappers in a separate wordlist.
   66: 
   67: \ The files are built in .../lib/gforth/$VERSION/libcc/ or
   68: \ ~/.gforth/libcc/$HOST/.
   69: 
   70: \ other things to do:
   71: 
   72: \ c-variable forth-name c-name
   73: \ c-constant forth-name c-name
   74: 
   75: \ Todo: conversion between function pointers and xts (both directions)
   76: 
   77: \ taking an xt and turning it into a function pointer:
   78: 
   79: \ e.g., assume we have the xt of + and want to create a C function int
   80: \ gforth_callback_plus(int, int), and then pass the pointer to that
   81: \ function:
   82: 
   83: \ There should be Forth code like this:
   84: \   ] + 0 (bye)
   85: \ Assume that the start of this code is START
   86:         
   87: \ Now, there should be a C function:
   88: 
   89: \ int gforth_callback_plus(int p1, int p2)
   90: \ {
   91: \   Cell   *sp = gforth_SP;
   92: \   Float  *fp = gforth_FP;
   93: \   Float  *fp = gforth_FP;
   94: \   Address lp = gforth_LP;
   95: \   sp -= 2;
   96: \   sp[0] = p1;
   97: \   sp[1] = p2;
   98: \   gforth_engine(START, sp, rp, fp, lp);
   99: \   sp += 1;
  100: \   gforth_RP = rp;
  101: \   gforth_SP = sp;
  102: \   gforth_FP = fp;
  103: \   gforth_LP = lp;
  104: \   return sp[0];
  105: \ }
  106: 
  107: \ and the pointer to that function is the C function pointer for the XT of +.
  108: 
  109: \ Future problems:
  110: \   how to combine the Forth code generation with inlining
  111: \   START is not a constant across executions (when caching the C files)
  112: \      Solution: make START a variable, and store into it on startup with dlsym
  113: 
  114: \ Syntax:
  115: \  callback <rettype> <params> <paramtypes> -- <rettype>
  116: 
  117: 
  118: \ data structures
  119: 
  120: \ For every c-function, we have three words: two anonymous words
  121: \ created by c-function-ft (first time) and c-function-rt (run-time),
  122: \ and a named deferred word.  The deferred word first points to the
  123: \ first-time word, then to the run-time word; the run-time word calls
  124: \ the c function.
  125: 
  126: [ifundef] parse-name
  127:     ' parse-word alias parse-name
  128: [then]
  129: [ifundef] defer!
  130: : defer! ( xt xt-deferred -- ) \ gforth  defer-store
  131: \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
  132:     >body [ has? rom [IF] ] @ [ [THEN] ] ! ;
  133: [then]
  134: 
  135: \ : delete-file 2drop 0 ;
  136: 
  137: require struct.fs
  138: 
  139:     \ counted-string
  140:     
  141: \ c-function-ft word body:
  142: struct
  143:     cell% field cff-cfr \ xt of c-function-rt word
  144:     cell% field cff-deferred \ xt of c-function deferred word
  145:     cell% field cff-lha \ address of the lib-handle for the lib that
  146:                         \ contains the wrapper function of the word
  147:     char% field cff-rtype  \ return type
  148:     char% field cff-np     \ number of parameters
  149:     1 0   field cff-ptypes \ #npar parameter types
  150:     \  counted string: c-name
  151: end-struct cff%
  152: 
  153: variable c-source-file-id \ contains the source file id of the current batch
  154: 0 c-source-file-id !
  155: variable lib-handle-addr \ points to the library handle of the current batch.
  156:                          \ the library handle is 0 if the current
  157:                          \ batch is not yet compiled.
  158: 2variable lib-filename   \ filename without extension
  159: 2variable lib-modulename \ basename of the file without extension
  160: 
  161: : .nb ( n -- )
  162:     0 .r ;
  163: 
  164: : const+ ( n1 "name" -- n2 )
  165:     dup constant 1+ ;
  166: 
  167: : front-string { c-addr1 u1 c-addr2 u2 -- c-addr3 u3 }
  168:     \ insert string c-addr2 u2 in buffer c-addr1 u1; c-addr3 u3 is the
  169:     \ remainder of the buffer.
  170:     assert( u1 u2 u>= )
  171:     c-addr2 c-addr1 u2 move
  172:     c-addr1 u1 u2 /string ;
  173: 
  174: : front-char { c-addr1 u1 c -- c-addr3 u2 }
  175:     \ insert c in buffer c-addr1 u1; c-addr3 u3 is the remainder of
  176:     \ the buffer.
  177:     assert( u1 0 u> )
  178:     c c-addr1 c!
  179:     c-addr1 u1 1 /string ;
  180: 
  181: : s+ { addr1 u1 addr2 u2 -- addr u }
  182:     u1 u2 + allocate throw { addr }
  183:     addr1 addr u1 move
  184:     addr2 addr u1 + u2 move
  185:     addr u1 u2 +
  186: ;
  187: 
  188: : append { addr1 u1 addr2 u2 -- addr u }
  189:     addr1 u1 u2 + dup { u } resize throw { addr }
  190:     addr2 addr u1 + u2 move
  191:     addr u ;
  192: 
  193: \ linked list stuff (should go elsewhere)
  194: 
  195: struct
  196:     cell% field list-next
  197:     1 0   field list-payload
  198: end-struct list%
  199: 
  200: : list-insert { node list -- }
  201:     list list-next @ node list-next !
  202:     node list list-next ! ;
  203: 
  204: : list-append { node endlistp -- }
  205:     \ insert node at place pointed to by endlistp
  206:     node endlistp @ list-insert
  207:     node list-next endlistp ! ;
  208: 
  209: : list-map ( ... list xt -- ... )
  210:     \ xt ( ... node -- ... )
  211:     { xt } begin { node }
  212: 	node while
  213: 	    node xt execute
  214: 	    node list-next @
  215:     repeat ;
  216: 
  217: \ C prefix lines
  218: 
  219: \ linked list of longcstrings: [ link | count-cell | characters ]
  220: 
  221: list%
  222:     cell% field c-prefix-count
  223:     1 0   field c-prefix-chars
  224: end-struct c-prefix%
  225: 
  226: variable c-prefix-lines 0 c-prefix-lines !
  227: variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
  228: 
  229: : print-c-prefix-line ( node -- )
  230:     dup c-prefix-chars swap c-prefix-count @ type cr ;
  231: 
  232: : print-c-prefix-lines ( -- )
  233:     c-prefix-lines @ ['] print-c-prefix-line list-map ;
  234: 
  235: : save-c-prefix-line ( c-addr u -- )
  236:     c-source-file-id @ ?dup-if
  237: 	>r 2dup r> write-line throw
  238:     then
  239:     align here 0 , c-prefix-lines-end list-append ( c-addr u )
  240:     longstring, ;
  241: 
  242: : \c ( "rest-of-line" -- ) \ gforth backslash-c
  243:     \G One line of C declarations for the C interface
  244:     -1 parse save-c-prefix-line ;
  245: 
  246: s" #include <gforth/" version-string s+ s" /libcc.h>" append ( c-addr u )
  247:   2dup save-c-prefix-line drop free throw
  248: 
  249: \ Types (for parsing)
  250: 
  251: wordlist constant libcc-types
  252: 
  253: get-current libcc-types set-current
  254: 
  255: \ index values
  256: -1
  257: const+ -- \ end of arguments
  258: const+ n \ integer cell
  259: const+ a \ address cell
  260: const+ d \ double
  261: const+ r \ float
  262: const+ func \ C function pointer
  263: const+ void
  264: drop
  265: 
  266: set-current
  267: 
  268: : parse-libcc-type ( "libcc-type" -- u )
  269:     parse-name libcc-types search-wordlist 0= -13 and throw execute ;
  270: 
  271: : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
  272:     here 2 chars allot here begin
  273: 	parse-libcc-type dup 0>= while
  274: 	    c,
  275:     repeat
  276:     drop here swap - over char+ c!
  277:     parse-libcc-type dup 0< -32 and throw swap c! ;
  278: 
  279: : type-letter ( n -- c )
  280:     chars s" nadrfv" drop + c@ ;
  281: 
  282: \ count-stacks
  283: 
  284: : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  285:     1+ ;
  286: 
  287: : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  288:     1+ ;
  289: 
  290: : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  291:     2 + ;
  292: 
  293: : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  294:     swap 1+ swap ;
  295: 
  296: : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  297:     1+ ;
  298: 
  299: : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  300: ;
  301: 
  302: create count-stacks-types
  303: ' count-stacks-n ,
  304: ' count-stacks-a ,
  305: ' count-stacks-d ,
  306: ' count-stacks-r ,
  307: ' count-stacks-func ,
  308: ' count-stacks-void ,
  309: 
  310: : count-stacks ( pars -- fp-change sp-change )
  311:     \ pars is an addr u pair
  312:     0 0 2swap over + swap u+do
  313: 	i c@ cells count-stacks-types + @ execute
  314:     loop ;
  315: 
  316: \ gen-pars
  317: 
  318: : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  319:     ." sp[" 1- dup .nb ." ]" ;
  320: 
  321: : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  322:     ." (void *)(" gen-par-n ." )" ;
  323: 
  324: : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  325:     ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
  326: 
  327: : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  328:     swap 1- tuck ." fp[" .nb ." ]" ;
  329: 
  330: : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  331:     gen-par-a ;
  332: 
  333: : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  334:     -32 throw ;
  335: 
  336: create gen-par-types
  337: ' gen-par-n ,
  338: ' gen-par-a ,
  339: ' gen-par-d ,
  340: ' gen-par-r ,
  341: ' gen-par-func ,
  342: ' gen-par-void ,
  343: 
  344: : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
  345:     cells gen-par-types + @ execute ;
  346: 
  347: \ the call itself
  348: 
  349: : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
  350:     c-name type ." ("
  351:     fp-change1 sp-change1 pars over + swap u+do 
  352: 	i c@ gen-par
  353: 	i 1+ i' < if
  354: 	    ." ,"
  355: 	endif
  356:     loop
  357:     2drop ." )" ;
  358: 
  359: \ calls for various kinds of return values
  360: 
  361: : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  362:     2dup 2>r gen-wrapped-call 2r> ;
  363: 
  364: : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  365:     2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
  366: 
  367: : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  368:     2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
  369: 
  370: : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  371:     ." gforth_ll2d(" gen-wrapped-void
  372:     ." ," gen-par-n ." ," gen-par-n ." )" ;
  373: 
  374: : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  375:     2dup gen-par-r 2>r ." =" gen-wrapped-call 2r> ;
  376: 
  377: : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  378:     gen-wrapped-a ;
  379: 
  380: create gen-wrapped-types
  381: ' gen-wrapped-n ,
  382: ' gen-wrapped-a ,
  383: ' gen-wrapped-d ,
  384: ' gen-wrapped-r ,
  385: ' gen-wrapped-func ,
  386: ' gen-wrapped-void ,
  387: 
  388: : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
  389:     cells gen-wrapped-types + @ execute ;
  390: 
  391: : wrapper-function-name ( addr -- c-addr u )
  392:     \ addr points to the return type index of a c-function descriptor
  393:     count { r-type } count { d: pars }
  394:     pars + count { d: c-name }
  395:     s" gforth_c_" { d: prefix }
  396:     prefix nip c-name nip + pars nip + 3 + { u }
  397:     u allocate throw { c-addr }
  398:     c-addr u
  399:     prefix front-string c-name front-string '_ front-char
  400:     pars bounds u+do
  401: 	i c@ type-letter front-char
  402:     loop
  403:     '_ front-char r-type type-letter front-char assert( dup 0= )
  404:     2drop c-addr u ;
  405: 
  406: : gen-wrapper-function ( addr -- )
  407:     \ addr points to the return type index of a c-function descriptor
  408:     dup { descriptor }
  409:     count { ret } count 2dup { d: pars } chars + count { d: c-name }
  410:     ." void " lib-modulename 2@ type ." _LTX_" descriptor wrapper-function-name 2dup type drop free throw
  411:     .\" (void)\n"
  412:     .\" {\n  Cell MAYBE_UNUSED *sp = gforth_SP;\n  Float MAYBE_UNUSED *fp = gforth_FP;\n  "
  413:     pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
  414:     ?dup-if
  415: 	."   gforth_SP = sp+" .nb .\" ;\n"
  416:     endif
  417:     ?dup-if
  418: 	."   gforth_FP = fp+" .nb .\" ;\n"
  419:     endif
  420:     .\" }\n" ;
  421: 
  422: : tempdir ( -- c-addr u )
  423:     s" TMPDIR" getenv dup 0= if
  424:         2drop s" /tmp"
  425:     then ;
  426: 
  427: : gen-filename ( x -- c-addr u )
  428:     \ generates a filename without extension for lib-handle-addr X
  429:     0 <<# ['] #s $10 base-execute #> 
  430:     tempdir s" /gforth_c_" s+ 2swap append #>> ;
  431: 
  432: : init-c-source-file ( -- )
  433:     c-source-file-id @ 0= if
  434:         here 0 , dup lib-handle-addr ! gen-filename 2dup lib-filename 2!
  435:         2dup tempdir nip 1+ /string lib-modulename 2!
  436: 	s" .c" s+ 2dup w/o create-file throw dup c-source-file-id !
  437:         ['] print-c-prefix-lines swap outfile-execute
  438:         drop free throw
  439:     endif ;
  440: 
  441: : c-source-file ( -- file-id )
  442:     c-source-file-id @ assert( dup ) ;
  443: 
  444: : .lib-error ( -- )
  445:     [ifdef] lib-error
  446:         ['] cr stderr outfile-execute
  447:         lib-error ['] type stderr outfile-execute
  448:     [then] ;
  449: 
  450: DEFER compile-wrapper-function
  451: :NONAME ( -- )
  452:     c-source-file close-file throw
  453:     0 c-source-file-id !
  454:     [ libtool-command s"  --silent --mode=compile gcc -I " s+
  455:     s" includedir" getenv append ] sliteral
  456:     s"  -O -c " s+ lib-filename 2@ append s" .c -o " append
  457:     lib-filename 2@ append s" .lo" append ( c-addr u )
  458:     2dup system drop free throw $? abort" libtool compile failed"
  459:     [ libtool-command s"  --silent --mode=link gcc -module -rpath " s+ ] sliteral
  460:     tempdir s+ s"  " append
  461:     lib-filename 2@ append s" .lo -o " append
  462:     lib-filename 2@ append s" .la" append ( c-addr u )
  463:     2dup system drop free throw $? abort" libtool link failed"
  464:     lib-filename 2@ s" .la" s+
  465: \    2dup type cr
  466:     2dup open-lib dup 0= if
  467:         .lib-error true abort" open-lib failed"
  468:     endif
  469:     ( lib-handle ) lib-handle-addr @ !
  470:     2dup delete-file throw drop free throw
  471:     lib-filename 2@ s" .c" s+ 2dup delete-file throw drop free throw
  472:     lib-filename 2@ drop free throw 0 0 lib-filename 2! ; IS compile-wrapper-function
  473: \    s" ar rcs xxx.a xxx.o" system
  474: \    $? abort" ar generated error" ;
  475: 
  476: : link-wrapper-function { cff -- sym }
  477:     cff cff-rtype wrapper-function-name { d: wrapper-name }
  478:     wrapper-name cff cff-lha @ @ assert( dup ) lib-sym dup 0= if
  479:         .lib-error -&32 throw
  480:     endif
  481:     wrapper-name drop free throw ;
  482: 
  483: : c-function-ft ( xt-defr xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
  484:     \ build time/first time action for c-function
  485:     init-c-source-file
  486:     noname create 2, lib-handle-addr @ ,
  487:     parse-name { d: c-name }
  488:     here parse-function-types c-name string,
  489:     ['] gen-wrapper-function c-source-file outfile-execute
  490:   does> ( ... -- ... )
  491:     dup 2@ { xt-defer xt-cfr }
  492:     dup cff-lha @ @ 0= if
  493: 	compile-wrapper-function
  494:     endif
  495:     link-wrapper-function xt-cfr >body !
  496:     xt-cfr xt-defer defer!
  497:     xt-cfr execute ;
  498: 
  499: : c-function-rt ( -- )
  500:     \ run-time definition for c function; addr is the address where
  501:     \ the sym should be stored
  502:     noname create 0 ,
  503:   does> ( ... -- ... )
  504:     @ call-c ;
  505: 
  506: : c-function ( "forth-name" "c-name" "@{type@}" "--" "type" -- ) \ gforth
  507:     \G Define a Forth word @i{forth-name}.  @i{Forth-name} has the
  508:     \G specified stack effect and calls the C function @code{c-name}.
  509:     defer lastxt dup c-function-rt lastxt c-function-ft
  510:     lastxt swap defer! ;

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