Annotation of gforth/libcc.fs, revision 1.59

1.1       anton       1: \ libcc.fs     foreign function interface implemented using a C compiler
                      2: 
1.50      anton       3: \ Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
1.1       anton       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
1.28      anton       9: \ as published by the Free Software Foundation, either version 3
1.1       anton      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
1.28      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       anton      19: 
                     20: 
                     21: \ What this implementation does is this: if it sees a declaration like
                     22: 
1.2       anton      23: \ \ something that tells it that the current library is libc
1.6       anton      24: \ \c #include <unistd.h>
1.2       anton      25: \ c-function dlseek lseek n d n -- d
1.1       anton      26: 
                     27: \ it genererates C code similar to the following:
                     28: 
                     29: \ #include <gforth.h>
1.2       anton      30: \ #include <unistd.h>
1.1       anton      31: \ 
1.2       anton      32: \ void gforth_c_lseek_ndn_d(void)
1.1       anton      33: \ {
                     34: \   Cell *sp = gforth_SP;
                     35: \   Float *fp = gforth_FP;
1.2       anton      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;
1.1       anton      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
1.2       anton      44: \ the function pointer of gforth_c_lseek_ndn_d on the stack and
                     45: \ calls CALL-C.
                     46: 
1.7       anton      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: 
1.2       anton      70: \ other things to do:
                     71: 
                     72: \ c-variable forth-name c-name
                     73: \ c-constant forth-name c-name
                     74: 
1.20      anton      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: 
1.2       anton     117: 
                    118: \ data structures
                    119: 
1.10      anton     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: 
1.29      anton     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 ;
1.12      anton     136: 
                    137: require struct.fs
1.56      anton     138: require mkdir.fs
1.12      anton     139: 
1.10      anton     140: \ c-function-ft word body:
1.12      anton     141: struct
                    142:     cell% field cff-cfr \ xt of c-function-rt word
                    143:     cell% field cff-deferred \ xt of c-function deferred word
                    144:     cell% field cff-lha \ address of the lib-handle for the lib that
                    145:                         \ contains the wrapper function of the word
                    146:     char% field cff-rtype  \ return type
                    147:     char% field cff-np     \ number of parameters
                    148:     1 0   field cff-ptypes \ #npar parameter types
                    149:     \  counted string: c-name
                    150: end-struct cff%
                    151: 
1.14      anton     152: variable c-source-file-id \ contains the source file id of the current batch
                    153: 0 c-source-file-id !
                    154: variable lib-handle-addr \ points to the library handle of the current batch.
                    155:                          \ the library handle is 0 if the current
                    156:                          \ batch is not yet compiled.
1.39      anton     157:   here 0 , lib-handle-addr ! \ just make sure LIB-HANDLE always works
1.23      anton     158: 2variable lib-filename   \ filename without extension
                    159: 2variable lib-modulename \ basename of the file without extension
1.44      anton     160: 2variable libcc-named-dir-v \ directory for named libcc wrapper libraries
                    161: 0 value libcc-path       \ pointer to path of library directories
1.2       anton     162: 
1.49      anton     163: defer replace-rpath ( c-addr1 u1 -- c-addr2 u2 )
                    164: ' noop is replace-rpath
1.35      anton     165: 
1.3       anton     166: : .nb ( n -- )
1.2       anton     167:     0 .r ;
                    168: 
                    169: : const+ ( n1 "name" -- n2 )
                    170:     dup constant 1+ ;
                    171: 
1.10      anton     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: 
1.15      anton     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: 
1.6       anton     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: 
1.59    ! pazsan    222: 2variable c-libs \ library names in a string (without "lib")
1.33      anton     223: 
1.53      anton     224: : add-lib ( c-addr u -- ) \ gforth
1.33      anton     225: \G Add library lib@i{string} to the list of libraries, where
1.59    ! pazsan    226:     \G @i{string} is represented by @i{c-addr u}.
        !           227:     c-libs 2@ d0= IF  0 allocate throw 0 c-libs 2!  THEN
        !           228:     c-libs 2@ s"  -l" append 2swap append c-libs 2! ;
        !           229: 
        !           230: : add-libpath ( c-addr u -- ) \ gforth
        !           231: \G Add path @i{string} to the list of library search pathes, where
        !           232:     \G @i{string} is represented by @i{c-addr u}.
        !           233:     c-libs 2@ d0= IF  0 allocate throw 0 c-libs 2!  THEN
        !           234:     c-libs 2@ s"  -L" append 2swap append c-libs 2! ;
1.33      anton     235: 
1.6       anton     236: \ C prefix lines
                    237: 
                    238: \ linked list of longcstrings: [ link | count-cell | characters ]
                    239: 
                    240: list%
                    241:     cell% field c-prefix-count
                    242:     1 0   field c-prefix-chars
                    243: end-struct c-prefix%
                    244: 
                    245: variable c-prefix-lines 0 c-prefix-lines !
                    246: variable c-prefix-lines-end c-prefix-lines c-prefix-lines-end !
                    247: 
1.14      anton     248: : print-c-prefix-line ( node -- )
                    249:     dup c-prefix-chars swap c-prefix-count @ type cr ;
                    250: 
                    251: : print-c-prefix-lines ( -- )
                    252:     c-prefix-lines @ ['] print-c-prefix-line list-map ;
                    253: 
1.6       anton     254: : save-c-prefix-line ( c-addr u -- )
1.14      anton     255:     c-source-file-id @ ?dup-if
                    256:        >r 2dup r> write-line throw
                    257:     then
1.6       anton     258:     align here 0 , c-prefix-lines-end list-append ( c-addr u )
                    259:     longstring, ;
                    260: 
1.18      anton     261: : \c ( "rest-of-line" -- ) \ gforth backslash-c
1.17      anton     262:     \G One line of C declarations for the C interface
1.6       anton     263:     -1 parse save-c-prefix-line ;
                    264: 
1.19      anton     265: s" #include <gforth/" version-string s+ s" /libcc.h>" append ( c-addr u )
                    266:   2dup save-c-prefix-line drop free throw
1.5       anton     267: 
1.6       anton     268: \ Types (for parsing)
1.5       anton     269: 
1.2       anton     270: wordlist constant libcc-types
                    271: 
                    272: get-current libcc-types set-current
                    273: 
                    274: \ index values
                    275: -1
                    276: const+ -- \ end of arguments
                    277: const+ n \ integer cell
1.5       anton     278: const+ a \ address cell
1.2       anton     279: const+ d \ double
                    280: const+ r \ float
                    281: const+ func \ C function pointer
                    282: const+ void
                    283: drop
                    284: 
                    285: set-current
                    286: 
                    287: : parse-libcc-type ( "libcc-type" -- u )
                    288:     parse-name libcc-types search-wordlist 0= -13 and throw execute ;
                    289: 
                    290: : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
                    291:     here 2 chars allot here begin
                    292:        parse-libcc-type dup 0>= while
                    293:            c,
                    294:     repeat
1.3       anton     295:     drop here swap - over char+ c!
                    296:     parse-libcc-type dup 0< -32 and throw swap c! ;
1.2       anton     297: 
                    298: : type-letter ( n -- c )
1.52      pazsan    299:     chars s" nadrfv" drop + c@ ;
1.2       anton     300: 
                    301: \ count-stacks
                    302: 
                    303: : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    304:     1+ ;
                    305: 
1.5       anton     306: : count-stacks-a ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
1.2       anton     307:     1+ ;
                    308: 
                    309: : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    310:     2 + ;
                    311: 
                    312: : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    313:     swap 1+ swap ;
                    314: 
                    315: : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    316:     1+ ;
                    317: 
                    318: : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
                    319: ;
                    320: 
                    321: create count-stacks-types
                    322: ' count-stacks-n ,
1.5       anton     323: ' count-stacks-a ,
1.2       anton     324: ' count-stacks-d ,
                    325: ' count-stacks-r ,
                    326: ' count-stacks-func ,
                    327: ' count-stacks-void ,
                    328: 
                    329: : count-stacks ( pars -- fp-change sp-change )
                    330:     \ pars is an addr u pair
                    331:     0 0 2swap over + swap u+do
1.3       anton     332:        i c@ cells count-stacks-types + @ execute
1.2       anton     333:     loop ;
                    334: 
                    335: \ gen-pars
                    336: 
                    337: : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.5       anton     338:     ." sp[" 1- dup .nb ." ]" ;
1.2       anton     339: 
1.5       anton     340: : gen-par-a ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.2       anton     341:     ." (void *)(" gen-par-n ." )" ;
                    342: 
                    343: : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.4       anton     344:     ." gforth_d2ll(" gen-par-n ." ," gen-par-n ." )" ;
1.2       anton     345: 
                    346: : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.3       anton     347:     swap 1- tuck ." fp[" .nb ." ]" ;
1.2       anton     348: 
                    349: : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
1.5       anton     350:     gen-par-a ;
1.2       anton     351: 
                    352: : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
                    353:     -32 throw ;
                    354: 
                    355: create gen-par-types
                    356: ' gen-par-n ,
1.5       anton     357: ' gen-par-a ,
1.2       anton     358: ' gen-par-d ,
                    359: ' gen-par-r ,
                    360: ' gen-par-func ,
                    361: ' gen-par-void ,
                    362: 
                    363: : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
1.3       anton     364:     cells gen-par-types + @ execute ;
1.2       anton     365: 
                    366: \ the call itself
                    367: 
                    368: : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
                    369:     c-name type ." ("
1.3       anton     370:     fp-change1 sp-change1 pars over + swap u+do 
1.2       anton     371:        i c@ gen-par
                    372:        i 1+ i' < if
                    373:            ." ,"
                    374:        endif
                    375:     loop
                    376:     2drop ." )" ;
                    377: 
                    378: \ calls for various kinds of return values
                    379: 
                    380: : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
                    381:     2dup 2>r gen-wrapped-call 2r> ;
                    382: 
1.3       anton     383: : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5       anton     384:     2dup gen-par-n 2>r ." =" gen-wrapped-call 2r> ;
1.3       anton     385: 
1.5       anton     386: : gen-wrapped-a ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
                    387:     2dup gen-par-n 2>r ." =(Cell)" gen-wrapped-call 2r> ;
1.3       anton     388: 
                    389: : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
                    390:     ." gforth_ll2d(" gen-wrapped-void
1.5       anton     391:     ." ," gen-par-n ." ," gen-par-n ." )" ;
1.3       anton     392: 
                    393: : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.30      anton     394:     2dup gen-par-r 2>r ." =" gen-wrapped-call 2r> ;
1.3       anton     395: 
                    396: : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
1.5       anton     397:     gen-wrapped-a ;
1.3       anton     398: 
1.2       anton     399: create gen-wrapped-types
                    400: ' gen-wrapped-n ,
1.5       anton     401: ' gen-wrapped-a ,
1.2       anton     402: ' gen-wrapped-d ,
                    403: ' gen-wrapped-r ,
                    404: ' gen-wrapped-func ,
                    405: ' gen-wrapped-void ,
                    406: 
                    407: : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
1.3       anton     408:     cells gen-wrapped-types + @ execute ;
1.2       anton     409: 
1.10      anton     410: : wrapper-function-name ( addr -- c-addr u )
                    411:     \ addr points to the return type index of a c-function descriptor
                    412:     count { r-type } count { d: pars }
                    413:     pars + count { d: c-name }
                    414:     s" gforth_c_" { d: prefix }
                    415:     prefix nip c-name nip + pars nip + 3 + { u }
                    416:     u allocate throw { c-addr }
                    417:     c-addr u
                    418:     prefix front-string c-name front-string '_ front-char
                    419:     pars bounds u+do
                    420:        i c@ type-letter front-char
                    421:     loop
                    422:     '_ front-char r-type type-letter front-char assert( dup 0= )
                    423:     2drop c-addr u ;
                    424: 
1.2       anton     425: : gen-wrapper-function ( addr -- )
                    426:     \ addr points to the return type index of a c-function descriptor
1.10      anton     427:     dup { descriptor }
1.13      anton     428:     count { ret } count 2dup { d: pars } chars + count { d: c-name }
1.23      anton     429:     ." void " lib-modulename 2@ type ." _LTX_" descriptor wrapper-function-name 2dup type drop free throw
1.54      pazsan    430:     .\" (GFORTH_ARGS)\n"
1.4       anton     431:     .\" {\n  Cell MAYBE_UNUSED *sp = gforth_SP;\n  Float MAYBE_UNUSED *fp = gforth_FP;\n  "
1.2       anton     432:     pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
                    433:     ?dup-if
1.3       anton     434:        ."   gforth_SP = sp+" .nb .\" ;\n"
1.2       anton     435:     endif
                    436:     ?dup-if
1.3       anton     437:        ."   gforth_FP = fp+" .nb .\" ;\n"
1.2       anton     438:     endif
1.3       anton     439:     .\" }\n" ;
1.2       anton     440: 
1.40      anton     441: : scan-back { c-addr u1 c -- c-addr u2 }
                    442:     \ the last occurence of c in c-addr u1 is at u2-1; if it does not
                    443:     \ occur, u2=0.
                    444:     c-addr 1- c-addr u1 + 1- u-do
                    445:        i c@ c = if
                    446:            c-addr i over - 1+ unloop exit endif
                    447:     1 -loop
                    448:     c-addr 0 ;
                    449: 
                    450: : dirname ( c-addr1 u1 -- c-addr2 u2 )
                    451:     \ directory name of the file name c-addr1 u1, including the final "/".
                    452:     '/ scan-back ;
                    453: 
                    454: : basename ( c-addr1 u1 -- c-addr2 u2 )
                    455:     \ file name without directory component
                    456:     2dup dirname nip /string ;
1.16      anton     457: 
1.15      anton     458: : gen-filename ( x -- c-addr u )
1.35      anton     459:     \ generates a file basename for lib-handle-addr X
1.16      anton     460:     0 <<# ['] #s $10 base-execute #> 
1.35      anton     461:     s" gforth_c_" 2swap s+ #>> ;
                    462: 
1.40      anton     463: : libcc-named-dir ( -- c-addr u )
1.43      anton     464:     libcc-named-dir-v 2@ ;
1.40      anton     465: 
                    466: : libcc-tmp-dir ( -- c-addr u )
1.41      anton     467:     s" ~/.gforth/libcc-tmp/" ;
1.40      anton     468: 
                    469: : prepend-dirname ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
1.41      anton     470:     2over s+ 2swap drop free throw ;
1.35      anton     471: 
1.42      anton     472: : open-wrappers ( -- addr|0 )
                    473:     lib-filename 2@ s" .la" s+
                    474:     2dup libcc-named-dir string-prefix? if ( c-addr u )
                    475:        \ see if we can open it in the path
                    476:        libcc-named-dir nip /string
                    477:        libcc-path open-path-file if
                    478:            0 exit endif
                    479:        ( wfile-id c-addr2 u2 ) rot close-file throw save-mem ( c-addr2 u2 )
                    480:     endif
                    481:     \ 2dup cr type
                    482:     2dup open-lib >r
                    483:     drop free throw r> ;
                    484: 
1.39      anton     485: : c-library-name-setup ( c-addr u -- )
1.35      anton     486:     assert( c-source-file-id @ 0= )
1.40      anton     487:     { d: filename }
1.37      anton     488:     here 0 , lib-handle-addr ! filename lib-filename 2!
1.40      anton     489:     filename basename lib-modulename 2! ;
1.39      anton     490:    
                    491: : c-library-name-create ( -- )
                    492:     lib-filename 2@ s" .c" s+ 2dup w/o create-file throw
                    493:     dup c-source-file-id !
                    494:     ['] print-c-prefix-lines swap outfile-execute
                    495:     drop free throw ;
                    496: 
1.40      anton     497: : c-named-library-name ( c-addr u -- )
1.39      anton     498:     \ set up filenames for a (possibly new) library; c-addr u is the
                    499:     \ basename of the library
1.40      anton     500:     libcc-named-dir prepend-dirname c-library-name-setup
1.37      anton     501:     open-wrappers dup if
                    502:        lib-handle-addr @ !
                    503:     else
1.56      anton     504:         libcc-named-dir $1ff mkdir-parents drop
1.39      anton     505:        drop c-library-name-create
1.37      anton     506:     endif ;
                    507: 
1.40      anton     508: : c-tmp-library-name ( c-addr u -- )
1.39      anton     509:     \ set up filenames for a new library; c-addr u is the basename of
                    510:     \ the library
1.56      anton     511:     libcc-tmp-dir 2dup $1ff mkdir-parents drop
                    512:     prepend-dirname c-library-name-setup c-library-name-create ;
1.39      anton     513: 
1.37      anton     514: : lib-handle ( -- addr )
                    515:     lib-handle-addr @ @ ;
1.15      anton     516: 
1.12      anton     517: : init-c-source-file ( -- )
1.37      anton     518:     lib-handle 0= if
                    519:        c-source-file-id @ 0= if
1.40      anton     520:            here gen-filename c-tmp-library-name
1.37      anton     521:        endif
1.12      anton     522:     endif ;
1.11      anton     523: 
                    524: : c-source-file ( -- file-id )
1.12      anton     525:     c-source-file-id @ assert( dup ) ;
1.11      anton     526: 
1.37      anton     527: : notype-execute ( ... xt -- ... )
                    528:     what's type { oldtype } try
                    529:        ['] 2drop is type execute 0
                    530:     restore
                    531:        oldtype is type
                    532:     endtry
                    533:     throw ;
                    534: 
                    535: : c-source-file-execute ( ... xt -- ... )
                    536:     \ direct the output of xt to c-source-file, or nothing
                    537:     lib-handle if
                    538:        notype-execute
                    539:     else
                    540:        c-source-file outfile-execute
                    541:     endif ;
                    542: 
1.24      anton     543: : .lib-error ( -- )
                    544:     [ifdef] lib-error
                    545:         ['] cr stderr outfile-execute
1.31      anton     546:         lib-error ['] type stderr outfile-execute
1.24      anton     547:     [then] ;
1.23      anton     548: 
1.33      anton     549: DEFER compile-wrapper-function ( -- )
1.35      anton     550: : compile-wrapper-function1 ( -- )
1.37      anton     551:     lib-handle 0= if
                    552:        c-source-file close-file throw
                    553:        0 c-source-file-id !
1.57      anton     554:        [ libtool-command s"  --silent --mode=compile " s+
1.58      pazsan    555:          libtool-cc append s"  -I '" append
                    556:          s" includedir" getenv append  s" '" append ] sliteral
1.37      anton     557:        s"  -O -c " s+ lib-filename 2@ append s" .c -o " append
                    558:        lib-filename 2@ append s" .lo" append ( c-addr u )
1.55      pazsan    559:        \    2dup type cr
1.37      anton     560:        2dup system drop free throw $? abort" libtool compile failed"
1.57      anton     561:        [ libtool-command s"  --silent --mode=link " s+
1.55      pazsan    562:          libtool-cc append libtool-flags append s"  -module -rpath " s+ ] sliteral
1.49      anton     563:        lib-filename 2@ dirname replace-rpath s+ s"  " append
1.37      anton     564:        lib-filename 2@ append s" .lo -o " append
                    565:        lib-filename 2@ append s" .la" append ( c-addr u )
1.59    ! pazsan    566:        c-libs 2@ append
1.47      pazsan    567:        \    2dup type cr
1.37      anton     568:        2dup system drop free throw $? abort" libtool link failed"
                    569:        open-wrappers dup 0= if
                    570:            .lib-error true abort" open-lib failed"
                    571:        endif
                    572:        ( lib-handle ) lib-handle-addr @ !
1.23      anton     573:     endif
1.35      anton     574:     lib-filename 2@ drop free throw 0 0 lib-filename 2! ;
                    575: ' compile-wrapper-function1 IS compile-wrapper-function
1.5       anton     576: \    s" ar rcs xxx.a xxx.o" system
                    577: \    $? abort" ar generated error" ;
                    578: 
1.12      anton     579: : link-wrapper-function { cff -- sym }
                    580:     cff cff-rtype wrapper-function-name { d: wrapper-name }
1.23      anton     581:     wrapper-name cff cff-lha @ @ assert( dup ) lib-sym dup 0= if
1.24      anton     582:         .lib-error -&32 throw
1.23      anton     583:     endif
1.10      anton     584:     wrapper-name drop free throw ;
1.8       anton     585: 
1.12      anton     586: : c-function-ft ( xt-defr xt-cfr "c-name" "{libcc-type}" "--" "libcc-type" -- )
1.8       anton     587:     \ build time/first time action for c-function
1.12      anton     588:     init-c-source-file
                    589:     noname create 2, lib-handle-addr @ ,
1.2       anton     590:     parse-name { d: c-name }
1.8       anton     591:     here parse-function-types c-name string,
1.37      anton     592:     ['] gen-wrapper-function c-source-file-execute
1.8       anton     593:   does> ( ... -- ... )
1.10      anton     594:     dup 2@ { xt-defer xt-cfr }
1.12      anton     595:     dup cff-lha @ @ 0= if
                    596:        compile-wrapper-function
                    597:     endif
                    598:     link-wrapper-function xt-cfr >body !
1.8       anton     599:     xt-cfr xt-defer defer!
                    600:     xt-cfr execute ;
                    601: 
                    602: : c-function-rt ( -- )
                    603:     \ run-time definition for c function; addr is the address where
                    604:     \ the sym should be stored
                    605:     noname create 0 ,
1.2       anton     606:   does> ( ... -- ... )
                    607:     @ call-c ;
                    608: 
1.18      anton     609: : c-function ( "forth-name" "c-name" "@{type@}" "--" "type" -- ) \ gforth
1.17      anton     610:     \G Define a Forth word @i{forth-name}.  @i{Forth-name} has the
                    611:     \G specified stack effect and calls the C function @code{c-name}.
1.8       anton     612:     defer lastxt dup c-function-rt lastxt c-function-ft
                    613:     lastxt swap defer! ;
1.33      anton     614: 
1.53      anton     615: : clear-libs ( -- ) \ gforth
1.33      anton     616: \G Clear the list of libs
                    617:     c-source-file-id @ if
                    618:        compile-wrapper-function
                    619:     endif
1.59    ! pazsan    620:     0. c-libs 2! ;
1.33      anton     621: clear-libs
1.35      anton     622: 
                    623: : c-library-incomplete ( -- )
                    624:     true abort" Called function of unfinished named C library" ;
                    625: 
                    626: : c-library-name ( c-addr u -- ) \ gforth
1.38      anton     627: \G Start a C library interface with name @i{c-addr u}.
1.35      anton     628:     clear-libs
                    629:     ['] c-library-incomplete is compile-wrapper-function
1.40      anton     630:     c-named-library-name ;
1.35      anton     631: 
                    632: : c-library ( "name" -- ) \ gforth
                    633: \G Parsing version of @code{c-library-name}
1.39      anton     634:     parse-name save-mem c-library-name ;
1.35      anton     635: 
1.38      anton     636: : end-c-library ( -- ) \ gforth
                    637: \G Finish and (if necessary) build the latest C library interface.
1.35      anton     638:     ['] compile-wrapper-function1 is compile-wrapper-function
                    639:     compile-wrapper-function1 ;
1.43      anton     640: 
                    641: : init-libcc ( -- )
                    642:     s" ~/.gforth/libcc-named/" libcc-named-dir-v 2!
1.45      pazsan    643: [IFDEF] make-path
1.43      anton     644:     make-path to libcc-path
                    645:     libcc-named-dir libcc-path also-path
                    646:     [ s" libccdir" getenv ] sliteral libcc-path also-path
1.45      pazsan    647: [THEN]
1.43      anton     648: ;
                    649: 
                    650: init-libcc
                    651: 
                    652: :noname ( -- )
                    653:     defers 'cold
                    654:     init-libcc ;
                    655: is 'cold

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