File:  [gforth] / gforth / libcc.fs
Revision 1.72: download - view: text, annotated - select for diffs
Mon Jul 23 12:49:06 2012 UTC (6 years, 11 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added lib prefix to library files

    1: \ libcc.fs	foreign function interface implemented using a C compiler
    2: 
    3: \ Copyright (C) 2006,2007,2008,2009,2010,2011 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$ARCH/$VERSION/libcc/ or
   68: \ ~/.gforth$ARCH/libcc/$HOST/.
   69: 
   70: \ Todo: conversion between function pointers and xts (both directions)
   71: 
   72: \ taking an xt and turning it into a function pointer:
   73: 
   74: \ e.g., assume we have the xt of + and want to create a C function int
   75: \ gforth_callback_plus(int, int), and then pass the pointer to that
   76: \ function:
   77: 
   78: \ There should be Forth code like this:
   79: \   ] + 0 (bye)
   80: \ Assume that the start of this code is START
   81:         
   82: \ Now, there should be a C function:
   83: 
   84: \ int gforth_callback_plus(int p1, int p2)
   85: \ {
   86: \   Cell   *sp = gforth_SP;
   87: \   Float  *fp = gforth_FP;
   88: \   Float  *fp = gforth_FP;
   89: \   Address lp = gforth_LP;
   90: \   sp -= 2;
   91: \   sp[0] = p1;
   92: \   sp[1] = p2;
   93: \   gforth_engine(START, sp, rp, fp, lp);
   94: \   sp += 1;
   95: \   gforth_RP = rp;
   96: \   gforth_SP = sp;
   97: \   gforth_FP = fp;
   98: \   gforth_LP = lp;
   99: \   return sp[0];
  100: \ }
  101: 
  102: \ and the pointer to that function is the C function pointer for the XT of +.
  103: 
  104: \ Future problems:
  105: \   how to combine the Forth code generation with inlining
  106: \   START is not a constant across executions (when caching the C files)
  107: \      Solution: make START a variable, and store into it on startup with dlsym
  108: 
  109: \ Syntax:
  110: \  callback <rettype> <params> <paramtypes> -- <rettype>
  111: 
  112: 
  113: \ data structures
  114: 
  115: \ For every c-function, we have three words: two anonymous words
  116: \ created by c-function-ft (first time) and c-function-rt (run-time),
  117: \ and a named deferred word.  The deferred word first points to the
  118: \ first-time word, then to the run-time word; the run-time word calls
  119: \ the c function.
  120: 
  121: [ifundef] parse-name
  122:     ' parse-word alias parse-name
  123: [then]
  124: [ifundef] defer!
  125: : defer! ( xt xt-deferred -- ) \ gforth  defer-store
  126: \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
  127:     >body [ has? rom [IF] ] @ [ [THEN] ] ! ;
  128: [then]
  129: 
  130: \ : delete-file 2drop 0 ;
  131: 
  132: require struct.fs
  133: require mkdir.fs
  134: 
  135: \ c-function-ft word body:
  136: struct
  137:     cell% field cff-cfr \ xt of c-function-rt word
  138:     cell% field cff-deferred \ xt of c-function deferred word
  139:     cell% field cff-lha \ address of the lib-handle for the lib that
  140:                         \ contains the wrapper function of the word
  141:     char% field cff-ctype  \ call type (function=1, value=0)
  142:     char% field cff-rtype  \ return type
  143:     char% field cff-np     \ number of parameters
  144:     1 0   field cff-ptypes \ #npar parameter types
  145:     \  counted string: c-name
  146: end-struct cff%
  147: 
  148: variable c-source-file-id \ contains the source file id of the current batch
  149: 0 c-source-file-id !
  150: variable lib-handle-addr \ points to the library handle of the current batch.
  151:                          \ the library handle is 0 if the current
  152:                          \ batch is not yet compiled.
  153:   here 0 , lib-handle-addr ! \ just make sure LIB-HANDLE always works
  154: 2variable lib-filename   \ filename without extension
  155: 2variable lib-modulename \ basename of the file without extension
  156: 2variable libcc-named-dir-v \ directory for named libcc wrapper libraries
  157: Variable libcc-path      \ pointer to path of library directories
  158: 
  159: defer replace-rpath ( c-addr1 u1 -- c-addr2 u2 )
  160: ' noop is replace-rpath
  161: 
  162: : .nb ( n -- )
  163:     0 .r ;
  164: 
  165: : const+ ( n1 "name" -- n2 )
  166:     dup constant 1+ ;
  167: 
  168: : front-string { c-addr1 u1 c-addr2 u2 -- c-addr3 u3 }
  169:     \ insert string c-addr2 u2 in buffer c-addr1 u1; c-addr3 u3 is the
  170:     \ remainder of the buffer.
  171:     assert( u1 u2 u>= )
  172:     c-addr2 c-addr1 u2 move
  173:     c-addr1 u1 u2 /string ;
  174: 
  175: : front-char { c-addr1 u1 c -- c-addr3 u2 }
  176:     \ insert c in buffer c-addr1 u1; c-addr3 u3 is the remainder of
  177:     \ the buffer.
  178:     assert( u1 0 u> )
  179:     c c-addr1 c!
  180:     c-addr1 u1 1 /string ;
  181: 
  182: : s+ { addr1 u1 addr2 u2 -- addr u }
  183:     u1 u2 + allocate throw { addr }
  184:     addr1 addr u1 move
  185:     addr2 addr u1 + u2 move
  186:     addr u1 u2 +
  187: ;
  188: 
  189: : append { addr1 u1 addr2 u2 -- addr u }
  190:     addr1 u1 u2 + dup { u } resize throw { addr }
  191:     addr2 addr u1 + u2 move
  192:     addr u ;
  193: 
  194: \ linked list stuff (should go elsewhere)
  195: 
  196: struct
  197:     cell% field list-next
  198:     1 0   field list-payload
  199: end-struct list%
  200: 
  201: : list-insert { node list -- }
  202:     list list-next @ node list-next !
  203:     node list list-next ! ;
  204: 
  205: : list-append { node endlistp -- }
  206:     \ insert node at place pointed to by endlistp
  207:     node endlistp @ list-insert
  208:     node list-next endlistp ! ;
  209: 
  210: : list-map ( ... list xt -- ... )
  211:     \ xt ( ... node -- ... )
  212:     { xt } begin { node }
  213: 	node while
  214: 	    node xt execute
  215: 	    node list-next @
  216:     repeat ;
  217: 
  218: 2variable c-libs \ library names in a string (without "lib")
  219: 
  220: : add-lib ( c-addr u -- ) \ gforth
  221: \G Add library lib@i{string} to the list of libraries, where
  222:     \G @i{string} is represented by @i{c-addr u}.
  223:     c-libs 2@ d0= IF  0 allocate throw 0 c-libs 2!  THEN
  224:     c-libs 2@ s"  -l" append 2swap append c-libs 2! ;
  225: 
  226: : add-libpath ( c-addr u -- ) \ gforth
  227: \G Add path @i{string} to the list of library search pathes, where
  228:     \G @i{string} is represented by @i{c-addr u}.
  229:     c-libs 2@ d0= IF  0 allocate throw 0 c-libs 2!  THEN
  230:     c-libs 2@ s"  -L" append 2swap append c-libs 2! ;
  231: 
  232: \ C prefix lines
  233: 
  234: \ linked list of longcstrings: [ link | count-cell | characters ]
  235: 
  236: list%
  237:     cell% field c-prefix-count
  238:     1 0   field c-prefix-chars
  239: end-struct c-prefix%
  240: 
  241: variable c-prefix-lines 0 c-prefix-lines !
  242: variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
  243: 
  244: : print-c-prefix-line ( node -- )
  245:     dup c-prefix-chars swap c-prefix-count @ type cr ;
  246: 
  247: : print-c-prefix-lines ( -- )
  248:     c-prefix-lines @ ['] print-c-prefix-line list-map ;
  249: 
  250: : write-c-prefix-line ( c-addr u -- )
  251:     c-source-file-id @ dup if
  252: 	write-line throw
  253:     else
  254: 	drop 2drop
  255:     then ;
  256: 
  257: : save-c-prefix-line1 ( c-addr u -- )
  258:     2dup write-c-prefix-line
  259:     align here 0 , c-prefix-lines-end list-append ( c-addr u )
  260:     longstring, ;
  261: 
  262: defer save-c-prefix-line ( c-addr u -- )
  263: ' save-c-prefix-line1 is save-c-prefix-line
  264: 
  265: : \c ( "rest-of-line" -- ) \ gforth backslash-c
  266:     \G One line of C declarations for the C interface
  267:     -1 parse save-c-prefix-line ;
  268: 
  269: s" #include <gforth" arch-modifier s+ s" /" s+ version-string s+ s" /libcc.h>" append ( c-addr u )
  270:   2dup save-c-prefix-line drop free throw
  271: 
  272: \ Types (for parsing)
  273: 
  274: wordlist constant libcc-types
  275: 
  276: get-current libcc-types set-current
  277: 
  278: \ index values
  279: -1
  280: const+ -- \ end of arguments
  281: const+ n \ integer cell
  282: const+ a \ address cell
  283: const+ d \ double
  284: const+ r \ float
  285: const+ func \ C function pointer
  286: const+ void
  287: drop
  288: 
  289: set-current
  290: 
  291: \ call types
  292: 0
  293: const+ c-func
  294: const+ c-val
  295: const+ c-var
  296: drop
  297: 
  298: : libcc-type ( c-addr u -- u2 )
  299: 	libcc-types search-wordlist 0= -13 and throw execute ;
  300: 
  301: : parse-libcc-type ( "libcc-type" -- u )  parse-name libcc-type ;
  302: 
  303: : parse-return-type ( "libcc-type" -- u )
  304: 	parse-libcc-type dup 0< -32 and throw ;
  305: 
  306: : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- addr )
  307:     c-func c, here
  308:     dup 2 chars allot here begin
  309: 	parse-libcc-type dup 0>= while
  310: 	    c,
  311:     repeat
  312:     drop here swap - over char+ c!
  313:     parse-return-type swap c! ;
  314: 
  315: : parse-value-type ( "{--}" "libcc-type" -- addr )
  316:     c-val c, here
  317:     parse-libcc-type  dup 0< if drop parse-return-type then
  318:     c,  0 c, ;
  319: 
  320: : parse-variable-type ( -- addr )
  321: 	c-var c, here
  322: 	s" a" libcc-type c,  0 c, ;
  323: 
  324: : type-letter ( n -- c )
  325:     chars s" nadrfv" drop + c@ ;
  326: 
  327: \ count-stacks
  328: 
  329: : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  330:     1+ ;
  331: 
  332: : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  333:     1+ ;
  334: 
  335: : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  336:     2 + ;
  337: 
  338: : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  339:     swap 1+ swap ;
  340: 
  341: : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  342:     1+ ;
  343: 
  344: : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  345: ;
  346: 
  347: create count-stacks-types
  348: ' count-stacks-n ,
  349: ' count-stacks-a ,
  350: ' count-stacks-d ,
  351: ' count-stacks-r ,
  352: ' count-stacks-func ,
  353: ' count-stacks-void ,
  354: 
  355: : count-stacks ( pars -- fp-change sp-change )
  356:     \ pars is an addr u pair
  357:     0 0 2swap over + swap u+do
  358: 	i c@ cells count-stacks-types + @ execute
  359:     loop ;
  360: 
  361: \ gen-pars
  362: 
  363: : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  364:     ." sp[" 1- dup .nb ." ]" ;
  365: 
  366: : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  367:     ." (void *)(" gen-par-n ." )" ;
  368: 
  369: : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  370:     ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
  371: 
  372: : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  373:     swap 1- tuck ." fp[" .nb ." ]" ;
  374: 
  375: : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  376:     gen-par-a ;
  377: 
  378: : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  379:     -32 throw ;
  380: 
  381: create gen-par-types
  382: ' gen-par-n ,
  383: ' gen-par-a ,
  384: ' gen-par-d ,
  385: ' gen-par-r ,
  386: ' gen-par-func ,
  387: ' gen-par-void ,
  388: 
  389: : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
  390:     cells gen-par-types + @ execute ;
  391: 
  392: \ the call itself
  393: 
  394: : gen-wrapped-func { d: pars d: c-name fp-change1 sp-change1 -- }
  395:     c-name type ." ("
  396:     fp-change1 sp-change1 pars over + swap u+do 
  397: 	i c@ gen-par
  398: 	i 1+ i' < if
  399: 	    ." ,"
  400: 	endif
  401:     loop
  402:     2drop ." )" ;
  403: 
  404: : gen-wrapped-const { d: pars d: c-name fp-change1 sp-change1 -- }
  405: 	." (" c-name type ." )" ;
  406: 
  407: : gen-wrapped-var { d: pars d: c-name fp-change1 sp-change1 -- }
  408: 	." &(" c-name type ." )" ;
  409: 
  410: create gen-call-types
  411: ' gen-wrapped-func ,
  412: ' gen-wrapped-const ,
  413: ' gen-wrapped-var ,
  414: 
  415: : gen-wrapped-call ( pars c-name fp-change1 sp-change1 -- )
  416: 	5 pick 3 chars - c@ cells gen-call-types + @ execute ;
  417: 
  418: \ calls for various kinds of return values
  419: 
  420: : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  421:     2dup 2>r gen-wrapped-call 2r> ;
  422: 
  423: : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  424:     2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
  425: 
  426: : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  427:     2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
  428: 
  429: : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  430:     ." gforth_ll2d(" gen-wrapped-void
  431:     ." ," gen-par-n ." ," gen-par-n ." )" ;
  432: 
  433: : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  434:     2dup gen-par-r 2>r ." =" gen-wrapped-call 2r> ;
  435: 
  436: : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  437:     gen-wrapped-a ;
  438: 
  439: create gen-wrapped-types
  440: ' gen-wrapped-n ,
  441: ' gen-wrapped-a ,
  442: ' gen-wrapped-d ,
  443: ' gen-wrapped-r ,
  444: ' gen-wrapped-func ,
  445: ' gen-wrapped-void ,
  446: 
  447: : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
  448:     cells gen-wrapped-types + @ execute ;
  449: 
  450: : wrapper-function-name ( addr -- c-addr u )
  451:     \ addr points to the return type index of a c-function descriptor
  452:     count { r-type } count { d: pars }
  453:     pars + count { d: c-name }
  454:     s" gforth_c_" { d: prefix }
  455:     prefix nip c-name nip + pars nip + 3 + { u }
  456:     u allocate throw { c-addr }
  457:     c-addr u
  458:     prefix front-string c-name front-string '_ front-char
  459:     pars bounds u+do
  460: 	i c@ type-letter front-char
  461:     loop
  462:     '_ front-char r-type type-letter front-char assert( dup 0= )
  463:     2drop c-addr u ;
  464: 
  465: : gen-wrapper-function ( addr -- )
  466:     \ addr points to the return type index of a c-function descriptor
  467:     dup { descriptor }
  468:     count { ret } count 2dup { d: pars } chars + count { d: c-name }
  469:     ." void "
  470:     [ lib-suffix s" .la" str= [IF] ] ." lib" lib-modulename 2@ type ." _LTX_" [ [THEN] ]
  471:     descriptor wrapper-function-name 2dup type drop free throw
  472:     .\" (GFORTH_ARGS)\n"
  473:     .\" {\n  Cell MAYBE_UNUSED *sp = gforth_SP;\n  Float MAYBE_UNUSED *fp = gforth_FP;\n  "
  474:     pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
  475:     ?dup-if
  476: 	."   gforth_SP = sp+" .nb .\" ;\n"
  477:     endif
  478:     ?dup-if
  479: 	."   gforth_FP = fp+" .nb .\" ;\n"
  480:     endif
  481:     .\" }\n" ;
  482: 
  483: : scan-back { c-addr u1 c -- c-addr u2 }
  484:     \ the last occurence of c in c-addr u1 is at u2-1; if it does not
  485:     \ occur, u2=0.
  486:     c-addr 1- c-addr u1 + 1- u-do
  487: 	i c@ c = if
  488: 	    c-addr i over - 1+ unloop exit endif
  489:     1 -loop
  490:     c-addr 0 ;
  491: 
  492: : dirname ( c-addr1 u1 -- c-addr2 u2 )
  493:     \ directory name of the file name c-addr1 u1, including the final "/".
  494:     '/ scan-back ;
  495: 
  496: : basename ( c-addr1 u1 -- c-addr2 u2 )
  497:     \ file name without directory component
  498:     2dup dirname nip /string ;
  499: 
  500: : gen-filename ( x -- c-addr u )
  501:     \ generates a file basename for lib-handle-addr X
  502:     0 <<# ['] #s $10 base-execute #> 
  503:     s" gforth_c_" 2swap s+ #>> ;
  504: 
  505: : libcc-named-dir ( -- c-addr u )
  506:     libcc-named-dir-v 2@ ;
  507: 
  508: : libcc-tmp-dir ( -- c-addr u )
  509:     s" ~/.gforth" arch-modifier s+ s" /libcc-tmp/" s+ ;
  510: 
  511: : prepend-dirname ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
  512:     2over s+ 2swap drop free throw ;
  513: 
  514: : open-wrappers ( -- addr|0 )
  515:     lib-filename 2@ dirname s" lib" s+ lib-filename 2@ basename s+ lib-suffix s+
  516:     2dup libcc-named-dir string-prefix? if ( c-addr u )
  517: 	\ see if we can open it in the path
  518: 	libcc-named-dir nip /string
  519: 	libcc-path open-path-file if
  520: 	    0 exit endif
  521: 	( wfile-id c-addr2 u2 ) rot close-file throw save-mem ( c-addr2 u2 )
  522:     endif
  523:     \ 2dup cr type
  524:     2dup open-lib >r
  525:     drop free throw r> ;
  526: 
  527: : c-library-name-setup ( c-addr u -- )
  528:     assert( c-source-file-id @ 0= )
  529:     { d: filename }
  530:     here 0 , lib-handle-addr ! filename lib-filename 2!
  531:     filename basename lib-modulename 2!
  532:     ['] write-c-prefix-line is save-c-prefix-line ;
  533:    
  534: : c-library-name-create ( -- )
  535:     lib-filename 2@ s" .c" s+ 2dup w/o create-file throw
  536:     c-source-file-id !
  537:     drop free throw ;
  538: 
  539: : c-named-library-name ( c-addr u -- )
  540:     \ set up filenames for a (possibly new) library; c-addr u is the
  541:     \ basename of the library
  542:     libcc-named-dir prepend-dirname c-library-name-setup
  543:     open-wrappers dup if
  544: 	lib-handle-addr @ !
  545:     else
  546:         libcc-named-dir $1ff mkdir-parents drop
  547: 	drop c-library-name-create
  548: 	c-prefix-lines @ ['] print-c-prefix-line \ first line only
  549: 	c-source-file-id @ outfile-execute
  550:     endif ;
  551: 
  552: : c-tmp-library-name ( c-addr u -- )
  553:     \ set up filenames for a new library; c-addr u is the basename of
  554:     \ the library
  555:     libcc-tmp-dir 2dup $1ff mkdir-parents drop
  556:     prepend-dirname c-library-name-setup c-library-name-create
  557:     ['] print-c-prefix-lines c-source-file-id @ outfile-execute ;
  558: 
  559: : lib-handle ( -- addr )
  560:     lib-handle-addr @ @ ;
  561: 
  562: : init-c-source-file ( -- )
  563:     lib-handle 0= if
  564: 	c-source-file-id @ 0= if
  565: 	    here gen-filename c-tmp-library-name
  566: 	endif
  567:     endif ;
  568: 
  569: : c-source-file ( -- file-id )
  570:     c-source-file-id @ assert( dup ) ;
  571: 
  572: : notype-execute ( ... xt -- ... )
  573:     what's type { oldtype } try
  574: 	['] 2drop is type execute 0
  575:     restore
  576: 	oldtype is type
  577:     endtry
  578:     throw ;
  579: 
  580: : c-source-file-execute ( ... xt -- ... )
  581:     \ direct the output of xt to c-source-file, or nothing
  582:     lib-handle if
  583: 	notype-execute
  584:     else
  585: 	c-source-file outfile-execute
  586:     endif ;
  587: 
  588: : .lib-error ( -- )
  589:     [ifdef] lib-error
  590:         ['] cr stderr outfile-execute
  591:         lib-error ['] type stderr outfile-execute
  592:     [then] ;
  593: 
  594: DEFER compile-wrapper-function ( -- )
  595: : compile-wrapper-function1 ( -- )
  596:     lib-handle 0= if
  597: 	c-source-file close-file throw
  598: 	0 c-source-file-id !
  599: 	[ libtool-command s"  --silent --tag=CC --mode=compile " s+
  600: 	  libtool-cc append s"  -I '" append
  601: 	  s" includedir" getenv append  s" '" append ] sliteral
  602: 	s"  -O -c " s+ lib-filename 2@ append s" .c -o " append
  603: 	lib-filename 2@ append s" .lo" append ( c-addr u )
  604: 	\    2dup type cr
  605: 	2dup system drop free throw $? abort" libtool compile failed"
  606: 	[ libtool-command s"  --silent --tag=CC --mode=link " s+
  607: 	  libtool-cc append libtool-flags append s"  -module -rpath " s+ ] sliteral
  608: 	lib-filename 2@ dirname replace-rpath s+ s"  " append
  609: 	lib-filename 2@ append s" .lo -o " append
  610: 	lib-filename 2@ dirname append s" lib" append
  611: 	lib-filename 2@ basename append s" .la" append ( c-addr u )
  612: 	c-libs 2@ append
  613: 	\    2dup type cr
  614: 	2dup system drop free throw $? abort" libtool link failed"
  615: 	open-wrappers dup 0= if
  616: 	    .lib-error true abort" open-lib failed"
  617: 	endif
  618: 	( lib-handle ) lib-handle-addr @ !
  619:     endif
  620:     lib-filename 2@ drop free throw 0 0 lib-filename 2! ;
  621: ' compile-wrapper-function1 IS compile-wrapper-function
  622: \    s" ar rcs xxx.a xxx.o" system
  623: \    $? abort" ar generated error" ;
  624: 
  625: : link-wrapper-function { cff -- sym }
  626:     cff cff-rtype wrapper-function-name { d: wrapper-name }
  627:     wrapper-name cff cff-lha @ @ assert( dup ) lib-sym dup 0= if
  628:         .lib-error -&32 throw
  629:     endif
  630:     wrapper-name drop free throw ;
  631: 
  632: : c-function-ft ( xt-defr xt-cfr xt-parse "c-name" "type signature" -- )
  633:     \ build time/first time action for c-function
  634:     { xt-parse-types } init-c-source-file
  635:     noname create 2, lib-handle-addr @ ,
  636:     parse-name { d: c-name }
  637:     xt-parse-types execute c-name string,
  638:     ['] gen-wrapper-function c-source-file-execute
  639:   does> ( ... -- ... )
  640:     dup 2@ { xt-defer xt-cfr }
  641:     dup cff-lha @ @ 0= if
  642: 	compile-wrapper-function
  643:     endif
  644:     link-wrapper-function xt-cfr >body !
  645:     xt-cfr xt-defer defer!
  646:     xt-cfr execute ;
  647: 
  648: : c-function-rt ( -- )
  649:     \ run-time definition for c function; addr is the address where
  650:     \ the sym should be stored
  651:     noname create 0 ,
  652:   does> ( ... -- ... )
  653:     @ call-c ;
  654: 
  655: : (c-function) ( xt-parse "forth-name" "c-name" "{stack effect}" -- )
  656:     { xt-parse-types } defer lastxt dup c-function-rt
  657:     lastxt xt-parse-types c-function-ft
  658:     lastxt swap defer! ;
  659: 
  660: : c-function ( "forth-name" "c-name" "@{type@}" "---" "type" -- ) \ gforth
  661:     \G Define a Forth word @i{forth-name}.  @i{Forth-name} has the
  662:     \G specified stack effect and calls the C function @code{c-name}.
  663:     ['] parse-function-types (c-function) ;
  664: 
  665: : c-value ( "forth-name" "c-name" "---" "type" -- ) \ gforth
  666:     \G Define a Forth word @i{forth-name}.  @i{Forth-name} has the
  667:     \G specified stack effect and gives the C value of @code{c-name}.
  668:     ['] parse-value-type (c-function) ;
  669: 
  670: : c-variable ( "forth-name" "c-name" -- ) \ gforth
  671:     \G Define a Forth word @i{forth-name}.  @i{Forth-name} returns the
  672:     \G address of @code{c-name}.
  673:     ['] parse-variable-type (c-function) ;
  674: 
  675: : clear-libs ( -- ) \ gforth
  676: \G Clear the list of libs
  677:     c-source-file-id @ if
  678: 	compile-wrapper-function
  679:     endif
  680:     0. c-libs 2! ;
  681: clear-libs
  682: 
  683: : c-library-incomplete ( -- )
  684:     true abort" Called function of unfinished named C library" ;
  685: 
  686: : c-library-name ( c-addr u -- ) \ gforth
  687: \G Start a C library interface with name @i{c-addr u}.
  688:     clear-libs
  689:     ['] c-library-incomplete is compile-wrapper-function
  690:     c-named-library-name ;
  691: 
  692: : c-library ( "name" -- ) \ gforth
  693: \G Parsing version of @code{c-library-name}
  694:     parse-name save-mem c-library-name ;
  695: 
  696: : end-c-library ( -- ) \ gforth
  697: \G Finish and (if necessary) build the latest C library interface.
  698:     ['] save-c-prefix-line1 is save-c-prefix-line
  699:     ['] compile-wrapper-function1 is compile-wrapper-function
  700:     compile-wrapper-function1 ;
  701: 
  702: : init-libcc ( -- )
  703:     s" ~/.gforth" arch-modifier s+ s" /libcc-named/" s+ libcc-named-dir-v 2!
  704: [IFDEF] $init
  705:     libcc-path $init
  706:     libcc-named-dir libcc-path also-path
  707:     [ s" libccdir" getenv ] sliteral libcc-path also-path
  708: [THEN]
  709: ;
  710: 
  711: init-libcc
  712: 
  713: :noname ( -- )
  714:     defers 'cold
  715:     init-libcc ;
  716: is 'cold

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