Richard Suchenwirth 2007-10-09 - tcltcc is a Tcl extension wrapping tcc (the tiny C compiler), which was modified by MJ so it:
LGPL, download at http://code.google.com/p/tcltcc/ .
Like Odyce, it also comes with a partial Critcl API emulation, so hopefully (at least some time in the future) people can just code against a subset of the Critcl API and run it with any of the three :^)
For one example usage, see ci - a tiny C interpreter.
MJ 2007-10-09 - Tclcc now also builds and works (passes the testsuite) on Linux
Elementary API
tcc has a single command tcc to create a compiler instance:
tcc tcc_libary_path ?output_type? handle
Output_type can be one of:
memory exe dll obj preprocess
handle is a string provided by the user to name this instance
Once you have an instance, it accepts the following methods:
add_include_path (path) add_file (filename) add_library (filename) add_library_path (path) add_symbol command (tclname) (cname) - create a Tcl command tclname which calls cname compile (code) - this is the main action, can only be called once per instance define (symbol) (value) - similar to #define get_symbol output_file (filename) - write DLL or EXE file undefine (symbol) - similar to #undef
To delete a tcc instance, just
rename handle {}
In addition, there's a scripted layer on top of these that emulates part of the Critcl API.
The first (and pretty unique) use case for tcc is that it can compile directly into memory, even in an interactive session. Example:
~ $ tclsh % packa re tcc 0.2 % tcc::cproc sigid {int i} char* {return Tcl_SignalId(i);} % sigid 4 SIGILL
<tcl.h> is always included, so if you run into an interesting C function, it's evidently extremely easy to wrap it up and try it out. Like in Odyce (which also builds on tcc), even simple things can substantially reduce runtime if done in C:
% tcc::cproc cadd {double a double b} double {return a+b;} % proc tadd {a b} {expr {$a+$b}} % time {cadd 1.2 3.4} 1000 19.777 microseconds per iteration % time {tadd 1.2 3.4} 1000 43.138 microseconds per iteration
Simple API for building DLLs
RS 2007-10-13: Since a couple of days, tcltcc can also build shared libraries (currently only DLLs for Windows, tested on Windows 95). Here's a cute and simple API for that:
package require tcc set n [tcc::dll ?name?]
Prepares the making of a DLL. Name is generated if not given, and in any case returned.
$n ccode string
Adds the string (which can be many lines) to the source buffer. Useful for #include/#define etc. preprocessor directives, static internal functions, etc.
$n cproc name argl rtype body
Creates code to implement a Tcl command name with typed arguments argl of C return type rtype with the specified C body, and appends that to the source buffer.
$n write ?-name str -dir path -code string?
Produces the DLL $path/$name[info sharedlibextension] by compiling the source buffer, creating an Init function which registers the commands as specified by cproc calls, and optionally adding the initialization code in string. After this call, no other ccode/cproc/write calls are useful.
Working example (from the test suite):
test tcc-10.3 "new DLL interface" { set d [tcc::dll] $d ccode {static int fib(int n) {return n <= 2? 1 : fib(n-1) + fib(n-2);}} $d cproc fiboy {int n} int {return fib(n);} $d cproc hello {} char* {return "world";} $d write -name fiboy -code {Tcl_Eval(interp,"puts {hello Fiboy!}");} load fiboy[info sharedlibextension] list [fiboy 20] [hello] } {6765 world}
For introspection and debugging, no cleanup takes place yet. You can see the generated code with
set tcc::dll::${n}::code
Ah, tcc is great fun... :^) - In the Tcl chatroom, Gerald Lester asked for the converse functionality: wrap Tcl code into a C function that relieves the user of explicit Tcl_Eval. See Tcl to C functions
Building executables
Before there is a simple API like the one for DLLs above, the following session transcript shows how to build and test an .exe main program pedestrianly:
% tcc . exe t % t add_file c/crt1.c % t compile {int main(int argc,char*argv[]){printf("hello world %d\n",argc);return(0);}} % t output_file hello.exe % exec hello.exe a b c hello world 4 %
RS 2007-10-15: current tcltcc passes all tests (tcc.test, pingpong.test) on W95 and Win XP.
See also: Critcl