File:  [gforth] / gforth / libcc.fs
Revision 1.44: download - view: text, annotated - select for diffs
Sun Jul 13 19:20:54 2008 UTC (11 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
fixed some bugs so "make install" works (DESTDIR still probably buggy)

    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:   here 0 , lib-handle-addr ! \ just make sure LIB-HANDLE always works
  159: 2variable lib-filename   \ filename without extension
  160: 2variable lib-modulename \ basename of the file without extension
  161: 2variable libcc-named-dir-v \ directory for named libcc wrapper libraries
  162: 0 value libcc-path       \ pointer to path of library directories
  163: 
  164: : delete-file 2drop 0 ;
  165: 
  166: : .nb ( n -- )
  167:     0 .r ;
  168: 
  169: : const+ ( n1 "name" -- n2 )
  170:     dup constant 1+ ;
  171: 
  172: : front-string { c-addr1 u1 c-addr2 u2 -- c-addr3 u3 }
  173:     \ insert string c-addr2 u2 in buffer c-addr1 u1; c-addr3 u3 is the
  174:     \ remainder of the buffer.
  175:     assert( u1 u2 u>= )
  176:     c-addr2 c-addr1 u2 move
  177:     c-addr1 u1 u2 /string ;
  178: 
  179: : front-char { c-addr1 u1 c -- c-addr3 u2 }
  180:     \ insert c in buffer c-addr1 u1; c-addr3 u3 is the remainder of
  181:     \ the buffer.
  182:     assert( u1 0 u> )
  183:     c c-addr1 c!
  184:     c-addr1 u1 1 /string ;
  185: 
  186: : s+ { addr1 u1 addr2 u2 -- addr u }
  187:     u1 u2 + allocate throw { addr }
  188:     addr1 addr u1 move
  189:     addr2 addr u1 + u2 move
  190:     addr u1 u2 +
  191: ;
  192: 
  193: : append { addr1 u1 addr2 u2 -- addr u }
  194:     addr1 u1 u2 + dup { u } resize throw { addr }
  195:     addr2 addr u1 + u2 move
  196:     addr u ;
  197: 
  198: \ linked list stuff (should go elsewhere)
  199: 
  200: struct
  201:     cell% field list-next
  202:     1 0   field list-payload
  203: end-struct list%
  204: 
  205: : list-insert { node list -- }
  206:     list list-next @ node list-next !
  207:     node list list-next ! ;
  208: 
  209: : list-append { node endlistp -- }
  210:     \ insert node at place pointed to by endlistp
  211:     node endlistp @ list-insert
  212:     node list-next endlistp ! ;
  213: 
  214: : list-map ( ... list xt -- ... )
  215:     \ xt ( ... node -- ... )
  216:     { xt } begin { node }
  217: 	node while
  218: 	    node xt execute
  219: 	    node list-next @
  220:     repeat ;
  221: 
  222: \ linked libraries
  223: 
  224: list%
  225:     cell% 2* field c-lib-string
  226: end-struct c-lib%
  227: 
  228: variable c-libs \ linked list of library names (without "lib")
  229: 
  230: : add-lib ( c-addr u -- )
  231: \G Add library lib@i{string} to the list of libraries, where
  232: \G @i{string} is represented by @i{c-addr u}.
  233:     c-lib% %size allocate throw dup >r
  234:     c-lib-string 2!
  235:     r> c-libs list-insert ;
  236: 
  237: : append-l ( c-addr1 u1 node -- c-addr2 u2 )
  238:     \ append " -l<nodelib>" to string1
  239:     >r s"  -l" append r> c-lib-string 2@ append ;
  240: 
  241: \ C prefix lines
  242: 
  243: \ linked list of longcstrings: [ link | count-cell | characters ]
  244: 
  245: list%
  246:     cell% field c-prefix-count
  247:     1 0   field c-prefix-chars
  248: end-struct c-prefix%
  249: 
  250: variable c-prefix-lines 0 c-prefix-lines !
  251: variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
  252: 
  253: : print-c-prefix-line ( node -- )
  254:     dup c-prefix-chars swap c-prefix-count @ type cr ;
  255: 
  256: : print-c-prefix-lines ( -- )
  257:     c-prefix-lines @ ['] print-c-prefix-line list-map ;
  258: 
  259: : save-c-prefix-line ( c-addr u -- )
  260:     c-source-file-id @ ?dup-if
  261: 	>r 2dup r> write-line throw
  262:     then
  263:     align here 0 , c-prefix-lines-end list-append ( c-addr u )
  264:     longstring, ;
  265: 
  266: : \c ( "rest-of-line" -- ) \ gforth backslash-c
  267:     \G One line of C declarations for the C interface
  268:     -1 parse save-c-prefix-line ;
  269: 
  270: s" #include <gforth/" version-string s+ s" /libcc.h>" append ( c-addr u )
  271:   2dup save-c-prefix-line drop free throw
  272: 
  273: \ Types (for parsing)
  274: 
  275: wordlist constant libcc-types
  276: 
  277: get-current libcc-types set-current
  278: 
  279: \ index values
  280: -1
  281: const+ -- \ end of arguments
  282: const+ n \ integer cell
  283: const+ a \ address cell
  284: const+ d \ double
  285: const+ r \ float
  286: const+ func \ C function pointer
  287: const+ void
  288: drop
  289: 
  290: set-current
  291: 
  292: : parse-libcc-type ( "libcc-type" -- u )
  293:     parse-name libcc-types search-wordlist 0= -13 and throw execute ;
  294: 
  295: : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
  296:     here 2 chars allot here begin
  297: 	parse-libcc-type dup 0>= while
  298: 	    c,
  299:     repeat
  300:     drop here swap - over char+ c!
  301:     parse-libcc-type dup 0< -32 and throw swap c! ;
  302: 
  303: : type-letter ( n -- c )
  304:     chars s" nadrfv" drop + c@ ;
  305: 
  306: \ count-stacks
  307: 
  308: : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  309:     1+ ;
  310: 
  311: : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  312:     1+ ;
  313: 
  314: : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  315:     2 + ;
  316: 
  317: : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  318:     swap 1+ swap ;
  319: 
  320: : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  321:     1+ ;
  322: 
  323: : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  324: ;
  325: 
  326: create count-stacks-types
  327: ' count-stacks-n ,
  328: ' count-stacks-a ,
  329: ' count-stacks-d ,
  330: ' count-stacks-r ,
  331: ' count-stacks-func ,
  332: ' count-stacks-void ,
  333: 
  334: : count-stacks ( pars -- fp-change sp-change )
  335:     \ pars is an addr u pair
  336:     0 0 2swap over + swap u+do
  337: 	i c@ cells count-stacks-types + @ execute
  338:     loop ;
  339: 
  340: \ gen-pars
  341: 
  342: : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  343:     ." sp[" 1- dup .nb ." ]" ;
  344: 
  345: : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  346:     ." (void *)(" gen-par-n ." )" ;
  347: 
  348: : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  349:     ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
  350: 
  351: : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  352:     swap 1- tuck ." fp[" .nb ." ]" ;
  353: 
  354: : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  355:     gen-par-a ;
  356: 
  357: : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  358:     -32 throw ;
  359: 
  360: create gen-par-types
  361: ' gen-par-n ,
  362: ' gen-par-a ,
  363: ' gen-par-d ,
  364: ' gen-par-r ,
  365: ' gen-par-func ,
  366: ' gen-par-void ,
  367: 
  368: : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
  369:     cells gen-par-types + @ execute ;
  370: 
  371: \ the call itself
  372: 
  373: : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
  374:     c-name type ." ("
  375:     fp-change1 sp-change1 pars over + swap u+do 
  376: 	i c@ gen-par
  377: 	i 1+ i' < if
  378: 	    ." ,"
  379: 	endif
  380:     loop
  381:     2drop ." )" ;
  382: 
  383: \ calls for various kinds of return values
  384: 
  385: : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  386:     2dup 2>r gen-wrapped-call 2r> ;
  387: 
  388: : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  389:     2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
  390: 
  391: : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  392:     2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
  393: 
  394: : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  395:     ." gforth_ll2d(" gen-wrapped-void
  396:     ." ," gen-par-n ." ," gen-par-n ." )" ;
  397: 
  398: : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  399:     2dup gen-par-r 2>r ." =" gen-wrapped-call 2r> ;
  400: 
  401: : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  402:     gen-wrapped-a ;
  403: 
  404: create gen-wrapped-types
  405: ' gen-wrapped-n ,
  406: ' gen-wrapped-a ,
  407: ' gen-wrapped-d ,
  408: ' gen-wrapped-r ,
  409: ' gen-wrapped-func ,
  410: ' gen-wrapped-void ,
  411: 
  412: : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
  413:     cells gen-wrapped-types + @ execute ;
  414: 
  415: : wrapper-function-name ( addr -- c-addr u )
  416:     \ addr points to the return type index of a c-function descriptor
  417:     count { r-type } count { d: pars }
  418:     pars + count { d: c-name }
  419:     s" gforth_c_" { d: prefix }
  420:     prefix nip c-name nip + pars nip + 3 + { u }
  421:     u allocate throw { c-addr }
  422:     c-addr u
  423:     prefix front-string c-name front-string '_ front-char
  424:     pars bounds u+do
  425: 	i c@ type-letter front-char
  426:     loop
  427:     '_ front-char r-type type-letter front-char assert( dup 0= )
  428:     2drop c-addr u ;
  429: 
  430: : gen-wrapper-function ( addr -- )
  431:     \ addr points to the return type index of a c-function descriptor
  432:     dup { descriptor }
  433:     count { ret } count 2dup { d: pars } chars + count { d: c-name }
  434:     ." void " lib-modulename 2@ type ." _LTX_" descriptor wrapper-function-name 2dup type drop free throw
  435:     .\" (void)\n"
  436:     .\" {\n  Cell MAYBE_UNUSED *sp = gforth_SP;\n  Float MAYBE_UNUSED *fp = gforth_FP;\n  "
  437:     pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
  438:     ?dup-if
  439: 	."   gforth_SP = sp+" .nb .\" ;\n"
  440:     endif
  441:     ?dup-if
  442: 	."   gforth_FP = fp+" .nb .\" ;\n"
  443:     endif
  444:     .\" }\n" ;
  445: 
  446: : scan-back { c-addr u1 c -- c-addr u2 }
  447:     \ the last occurence of c in c-addr u1 is at u2-1; if it does not
  448:     \ occur, u2=0.
  449:     c-addr 1- c-addr u1 + 1- u-do
  450: 	i c@ c = if
  451: 	    c-addr i over - 1+ unloop exit endif
  452:     1 -loop
  453:     c-addr 0 ;
  454: 
  455: : dirname ( c-addr1 u1 -- c-addr2 u2 )
  456:     \ directory name of the file name c-addr1 u1, including the final "/".
  457:     '/ scan-back ;
  458: 
  459: : basename ( c-addr1 u1 -- c-addr2 u2 )
  460:     \ file name without directory component
  461:     2dup dirname nip /string ;
  462: 
  463: : gen-filename ( x -- c-addr u )
  464:     \ generates a file basename for lib-handle-addr X
  465:     0 <<# ['] #s $10 base-execute #> 
  466:     s" gforth_c_" 2swap s+ #>> ;
  467: 
  468: : libcc-named-dir ( -- c-addr u )
  469:     libcc-named-dir-v 2@ ;
  470: 
  471: : libcc-tmp-dir ( -- c-addr u )
  472:     s" ~/.gforth/libcc-tmp/" ;
  473: 
  474: : prepend-dirname ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
  475:     2over s+ 2swap drop free throw ;
  476: 
  477: : open-wrappers ( -- addr|0 )
  478:     lib-filename 2@ s" .la" s+
  479:     2dup libcc-named-dir string-prefix? if ( c-addr u )
  480: 	\ see if we can open it in the path
  481: 	libcc-named-dir nip /string
  482: 	libcc-path open-path-file if
  483: 	    0 exit endif
  484: 	( wfile-id c-addr2 u2 ) rot close-file throw save-mem ( c-addr2 u2 )
  485:     endif
  486:     \ 2dup cr type
  487:     2dup open-lib >r
  488:     drop free throw r> ;
  489: 
  490: : c-library-name-setup ( c-addr u -- )
  491:     assert( c-source-file-id @ 0= )
  492:     { d: filename }
  493:     here 0 , lib-handle-addr ! filename lib-filename 2!
  494:     filename basename lib-modulename 2! ;
  495:    
  496: : c-library-name-create ( -- )
  497:     lib-filename 2@ s" .c" s+ 2dup w/o create-file throw
  498:     dup c-source-file-id !
  499:     ['] print-c-prefix-lines swap outfile-execute
  500:     drop free throw ;
  501: 
  502: : c-named-library-name ( c-addr u -- )
  503:     \ set up filenames for a (possibly new) library; c-addr u is the
  504:     \ basename of the library
  505:     libcc-named-dir prepend-dirname c-library-name-setup
  506:     open-wrappers dup if
  507: 	lib-handle-addr @ !
  508:     else
  509: 	drop c-library-name-create
  510:     endif ;
  511: 
  512: : c-tmp-library-name ( c-addr u -- )
  513:     \ set up filenames for a new library; c-addr u is the basename of
  514:     \ the library
  515:     libcc-tmp-dir prepend-dirname c-library-name-setup c-library-name-create ;
  516: 
  517: : lib-handle ( -- addr )
  518:     lib-handle-addr @ @ ;
  519: 
  520: : init-c-source-file ( -- )
  521:     lib-handle 0= if
  522: 	c-source-file-id @ 0= if
  523: 	    here gen-filename c-tmp-library-name
  524: 	endif
  525:     endif ;
  526: 
  527: : c-source-file ( -- file-id )
  528:     c-source-file-id @ assert( dup ) ;
  529: 
  530: : notype-execute ( ... xt -- ... )
  531:     what's type { oldtype } try
  532: 	['] 2drop is type execute 0
  533:     restore
  534: 	oldtype is type
  535:     endtry
  536:     throw ;
  537: 
  538: : c-source-file-execute ( ... xt -- ... )
  539:     \ direct the output of xt to c-source-file, or nothing
  540:     lib-handle if
  541: 	notype-execute
  542:     else
  543: 	c-source-file outfile-execute
  544:     endif ;
  545: 
  546: : .lib-error ( -- )
  547:     [ifdef] lib-error
  548:         ['] cr stderr outfile-execute
  549:         lib-error ['] type stderr outfile-execute
  550:     [then] ;
  551: 
  552: DEFER compile-wrapper-function ( -- )
  553: : compile-wrapper-function1 ( -- )
  554:     lib-handle 0= if
  555: 	c-source-file close-file throw
  556: 	0 c-source-file-id !
  557: 	[ libtool-command s"  --silent --mode=compile gcc -I " s+
  558: 	s" includedir" getenv append ] sliteral
  559: 	s"  -O -c " s+ lib-filename 2@ append s" .c -o " append
  560: 	lib-filename 2@ append s" .lo" append ( c-addr u )
  561: 	\    cr 2dup type
  562: 	2dup system drop free throw $? abort" libtool compile failed"
  563: 	[ libtool-command s"  --silent --mode=link gcc -module -rpath " s+ ] sliteral
  564: 	lib-filename 2@ dirname s+ s"  " append
  565: 	lib-filename 2@ append s" .lo -o " append
  566: 	lib-filename 2@ append s" .la" append ( c-addr u )
  567: 	c-libs @ ['] append-l list-map
  568: 	\    2dup type cr
  569: 	2dup system drop free throw $? abort" libtool link failed"
  570: 	open-wrappers dup 0= if
  571: 	    .lib-error true abort" open-lib failed"
  572: 	endif
  573: 	( lib-handle ) lib-handle-addr @ !
  574:     endif
  575:     lib-filename 2@ drop free throw 0 0 lib-filename 2! ;
  576: ' compile-wrapper-function1 IS compile-wrapper-function
  577: \    s" ar rcs xxx.a xxx.o" system
  578: \    $? abort" ar generated error" ;
  579: 
  580: : link-wrapper-function { cff -- sym }
  581:     cff cff-rtype wrapper-function-name { d: wrapper-name }
  582:     wrapper-name cff cff-lha @ @ assert( dup ) lib-sym dup 0= if
  583:         .lib-error -&32 throw
  584:     endif
  585:     wrapper-name drop free throw ;
  586: 
  587: : c-function-ft ( xt-defr xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
  588:     \ build time/first time action for c-function
  589:     init-c-source-file
  590:     noname create 2, lib-handle-addr @ ,
  591:     parse-name { d: c-name }
  592:     here parse-function-types c-name string,
  593:     ['] gen-wrapper-function c-source-file-execute
  594:   does> ( ... -- ... )
  595:     dup 2@ { xt-defer xt-cfr }
  596:     dup cff-lha @ @ 0= if
  597: 	compile-wrapper-function
  598:     endif
  599:     link-wrapper-function xt-cfr >body !
  600:     xt-cfr xt-defer defer!
  601:     xt-cfr execute ;
  602: 
  603: : c-function-rt ( -- )
  604:     \ run-time definition for c function; addr is the address where
  605:     \ the sym should be stored
  606:     noname create 0 ,
  607:   does> ( ... -- ... )
  608:     @ call-c ;
  609: 
  610: : c-function ( "forth-name" "c-name" "@{type@}" "--" "type" -- ) \ gforth
  611:     \G Define a Forth word @i{forth-name}.  @i{Forth-name} has the
  612:     \G specified stack effect and calls the C function @code{c-name}.
  613:     defer lastxt dup c-function-rt lastxt c-function-ft
  614:     lastxt swap defer! ;
  615: 
  616: : clear-libs ( -- )
  617: \G Clear the list of libs
  618:     c-source-file-id @ if
  619: 	compile-wrapper-function
  620:     endif
  621:     0 c-libs ! ;
  622: clear-libs
  623: 
  624: : c-library-incomplete ( -- )
  625:     true abort" Called function of unfinished named C library" ;
  626: 
  627: : c-library-name ( c-addr u -- ) \ gforth
  628: \G Start a C library interface with name @i{c-addr u}.
  629:     clear-libs
  630:     ['] c-library-incomplete is compile-wrapper-function
  631:     c-named-library-name ;
  632: 
  633: : c-library ( "name" -- ) \ gforth
  634: \G Parsing version of @code{c-library-name}
  635:     parse-name save-mem c-library-name ;
  636: 
  637: : end-c-library ( -- ) \ gforth
  638: \G Finish and (if necessary) build the latest C library interface.
  639:     ['] compile-wrapper-function1 is compile-wrapper-function
  640:     compile-wrapper-function1 ;
  641: 
  642: : init-libcc ( -- )
  643:     s" ~/.gforth/libcc-named/" libcc-named-dir-v 2!
  644:     make-path to libcc-path
  645:     libcc-named-dir libcc-path also-path
  646:     [ s" libccdir" getenv ] sliteral libcc-path also-path
  647: ;
  648: 
  649: init-libcc
  650: 
  651: :noname ( -- )
  652:     defers 'cold
  653:     init-libcc ;
  654: is 'cold

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