File:  [gforth] / gforth / libcc.fs
Revision 1.39: download - view: text, annotated - select for diffs
Sat Jul 5 20:09:42 2008 UTC (15 years, 8 months ago) by anton
Branches: MAIN
CVS tags: HEAD
fflib.fs now is a named C interface library
fixed bug in libcc.fs (unnamed libraries are now always rebuilt)

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

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