File:  [gforth] / gforth / libcc.fs
Revision 1.3: download - view: text, annotated - select for diffs
Wed Dec 27 13:01:20 2006 UTC (17 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
more work on libcc.fs

    1: \ libcc.fs	foreign function interface implemented using a C compiler
    2: 
    3: \ Copyright (C) 2006 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 2
   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, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: 
   22: \ What this implementation does is this: if it sees a declaration like
   23: 
   24: \ \ something that tells it to include <unistd.h>
   25: \ \ something that tells it that the current library is libc
   26: 
   27: \ c-function dlseek lseek n d n -- d
   28: 
   29: \ it genererates C code similar to the following:
   30: 
   31: \ #include <gforth.h>
   32: \ #include <unistd.h>
   33: \ 
   34: \ void gforth_c_lseek_ndn_d(void)
   35: \ {
   36: \   Cell *sp = gforth_SP;
   37: \   Float *fp = gforth_FP;
   38: \   long long result;  /* longest type in C */
   39: \   gforth_ll2d(lseek(sp[3],gforth_d2ll(sp[2],sp[1]),sp[0]),sp[3],sp[2]);
   40: \   gforth_SP = sp+2;
   41: \ }
   42: 
   43: \ Then it compiles this code and dynamically links it into the Gforth
   44: \ system (batching and caching are future work).  It also dynamically
   45: \ links lseek.  Performing DLSEEK then puts the function pointer of
   46: \ the function pointer of gforth_c_lseek_ndn_d on the stack and
   47: \ calls CALL-C.
   48: 
   49: \ other things to do:
   50: 
   51: \ c-variable forth-name c-name
   52: \ c-constant forth-name c-name
   53: 
   54: 
   55: \ data structures
   56: 
   57: \ c-function word body:
   58: \  cell function pointer
   59: \  char return type index
   60: \  char parameter count n
   61: \  char*n parameters (type indices)
   62: \  counted string: c-name
   63: 
   64: : .nb ( n -- )
   65:     0 .r ;
   66: 
   67: : const+ ( n1 "name" -- n2 )
   68:     dup constant 1+ ;
   69: 
   70: wordlist constant libcc-types
   71: 
   72: get-current libcc-types set-current
   73: 
   74: \ index values
   75: -1
   76: const+ -- \ end of arguments
   77: const+ n \ integer cell
   78: const+ p \ pointer cell
   79: const+ d \ double
   80: const+ r \ float
   81: const+ func \ C function pointer
   82: const+ void
   83: drop
   84: 
   85: set-current
   86: 
   87: : parse-libcc-type ( "libcc-type" -- u )
   88:     parse-name libcc-types search-wordlist 0= -13 and throw execute ;
   89: 
   90: : parse-function-types ( "{libcc-type}" "--" "libcc-type" -- )
   91:     here 2 chars allot here begin
   92: 	parse-libcc-type dup 0>= while
   93: 	    c,
   94:     repeat
   95:     drop here swap - over char+ c!
   96:     parse-libcc-type dup 0< -32 and throw swap c! ;
   97: 
   98: : type-letter ( n -- c )
   99:     chars s" npdrfv" drop + c@ ;
  100: 
  101: \ count-stacks
  102: 
  103: : count-stacks-n ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  104:     1+ ;
  105: 
  106: : count-stacks-p ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  107:     1+ ;
  108: 
  109: : count-stacks-d ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  110:     2 + ;
  111: 
  112: : count-stacks-r ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  113:     swap 1+ swap ;
  114: 
  115: : count-stacks-func ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  116:     1+ ;
  117: 
  118: : count-stacks-void ( fp-change1 sp-change1 -- fp-change2 sp-change2 )
  119: ;
  120: 
  121: create count-stacks-types
  122: ' count-stacks-n ,
  123: ' count-stacks-p ,
  124: ' count-stacks-d ,
  125: ' count-stacks-r ,
  126: ' count-stacks-func ,
  127: ' count-stacks-void ,
  128: 
  129: : count-stacks ( pars -- fp-change sp-change )
  130:     \ pars is an addr u pair
  131:     0 0 2swap over + swap u+do
  132: 	i c@ cells count-stacks-types + @ execute
  133:     loop ;
  134: 
  135: \ gen-pars
  136: 
  137: : gen-par-n ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  138:     1- dup ." sp[" .nb ." ]" ;
  139: 
  140: : gen-par-p ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  141:     ." (void *)(" gen-par-n ." )" ;
  142: 
  143: : gen-par-d ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  144:     ." gforthd2ll(" gen-par-n ." ," gen-par-n ." )" ;
  145: 
  146: : gen-par-r ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  147:     swap 1- tuck ." fp[" .nb ." ]" ;
  148: 
  149: : gen-par-func ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  150:     gen-par-p ;
  151: 
  152: : gen-par-void ( fp-depth1 sp-depth1 -- fp-depth2 sp-depth2 )
  153:     -32 throw ;
  154: 
  155: create gen-par-types
  156: ' gen-par-n ,
  157: ' gen-par-p ,
  158: ' gen-par-d ,
  159: ' gen-par-r ,
  160: ' gen-par-func ,
  161: ' gen-par-void ,
  162: 
  163: : gen-par ( fp-depth1 sp-depth1 partype -- fp-depth2 sp-depth2 )
  164:     cells gen-par-types + @ execute ;
  165: 
  166: \ the call itself
  167: 
  168: : gen-wrapped-call { d: pars d: c-name fp-change1 sp-change1 -- }
  169:     c-name type ." ("
  170:     fp-change1 sp-change1 pars over + swap u+do 
  171: 	i c@ gen-par
  172: 	i 1+ i' < if
  173: 	    ." ,"
  174: 	endif
  175:     loop
  176:     2drop ." )" ;
  177: 
  178: \ calls for various kinds of return values
  179: 
  180: : gen-wrapped-void ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  181:     2dup 2>r gen-wrapped-call 2r> ;
  182: 
  183: : gen-wrapped-n ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  184:     1- dup ." sp[" .nb ." ]=" gen-wrapped-void ;
  185: 
  186: : gen-wrapped-p ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  187:     1- dup ." sp[" .nb ." ]=(Cell)" gen-wrapped-void ;
  188: 
  189: : gen-wrapped-d ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  190:     ." gforth_ll2d(" gen-wrapped-void
  191:     ." ,sp[" 1- dup .nb ." ],sp[" 1- dup .nb ." ])" ;
  192: 
  193: : gen-wrapped-r ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  194:     swap 1- tuck ." fp[" .nb ." ]=" gen-wrapped-void ;
  195: 
  196: : gen-wrapped-func ( pars c-name fp-change1 sp-change1 -- fp-change sp-change )
  197:     gen-wrapped-p ;
  198: 
  199: create gen-wrapped-types
  200: ' gen-wrapped-n ,
  201: ' gen-wrapped-p ,
  202: ' gen-wrapped-d ,
  203: ' gen-wrapped-r ,
  204: ' gen-wrapped-func ,
  205: ' gen-wrapped-void ,
  206: 
  207: : gen-wrapped-stmt ( pars c-name fp-change1 sp-change1 ret -- fp-change sp-change )
  208:     cells gen-wrapped-types + @ execute ;
  209: 
  210: : gen-wrapper-function ( addr -- )
  211:     \ addr points to the return type index of a c-function descriptor
  212:     c@+ { ret } count 2dup { d: pars } chars + count { d: c-name }
  213:     ." void gforth_c_" c-name type ." _"
  214:     pars 0 +do
  215: 	i chars over + c@ type-letter emit
  216:     loop
  217:     ." _" ret type-letter emit .\" (void)\n"
  218:     .\" {\n  Cell *sp = gforth_SP;\n  Float *fp = gforth_FP;\n  "
  219:     pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n"
  220:     ?dup-if
  221: 	."   gforth_SP = sp+" .nb .\" ;\n"
  222:     endif
  223:     ?dup-if
  224: 	."   gforth_FP = fp+" .nb .\" ;\n"
  225:     endif
  226:     .\" }\n" ;
  227: 
  228: : c-function ( "forth-name" "c-name" "{libcc-type}" "--" "libcc-type" -- )
  229:     create here >r 0 , \ place for the wrapper function pointer
  230:     parse-name { d: c-name }
  231:     parse-function-types c-name string,
  232:     r> cell+ gen-wrapper-function
  233: \    compile-wrapper-function
  234: \    link-wrapper-function
  235: \    r> !
  236:   does> ( ... -- ... )
  237:     @ call-c ;
  238: 
  239: 
  240: 
  241: 
  242: 
  243: s" Library not found" exception constant err-nolib
  244: 
  245: : library ( "name" "file" -- ) \ gforth
  246: \G Dynamically links the library specified by @i{file}.  Defines a
  247: \G word @i{name} ( -- lib ) that starts the declaration of a
  248: \G function from that library.
  249:     create parse-name open-lib dup 0= err-nolib and throw ,
  250:   does> ( -- lib )
  251:     @ ;
  252: 
  253: \ test
  254: 
  255: cr .( #include "engine/forth.h")
  256: cr .( #include <unistd.h>)
  257: cr ." typedef void (* func)(int);
  258: cr ." int test1(int,char*,long,double,void (*)(int));"
  259: cr ." Cell *test2(void);"
  260: cr ." int test3(void);"
  261: cr ." float test4(void);"
  262: cr ." func test5(void);"
  263: cr ." void test6(void);"
  264: cr
  265: 
  266: c-function dlseek lseek n d n -- d
  267: c-function n test1 n p d r func -- n
  268: c-function p test2 -- p
  269: c-function d test3 -- d
  270: c-function r test4 -- r
  271: c-function func test5 -- func
  272: c-function void test6 -- void

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