[LLVMdev] Using JIT to construct an invocation of a non-JIT function, or how do I interface to GCC exception handling

Nuno Lopes nunoplopes at sapo.pt
Tue Aug 12 12:08:43 PDT 2008


Hi,

Regarding the JIT, what you describe is exactly what Apple has done with 
their opengl shader JIT compiler and what we have done with the PHP JIT 
compiler. You can find some high-level info about how those two were done in 
the 2 last LLVM confs (http://llvm.org/devmtg/). The idea is to compile the 
e.g. doPUT() function to LLVM bitcode and load it at runtime. Then you do 
the work of the interpreter at JIT time, and produce LLVM bitcode that calls 
the previously compiled functions (e.g. doPUT()). LLVM can then perform some 
optimizations for you, like inlining. So it can be done and it's not that 
difficult. The source code of the PHP JIT compiler (still in early stages) 
is available at http://cvs.php.net//pecl/llvm/

Regarding the exceptions, to my best knowledge (derived from some chats last 
week :) I think LLVM will do the right thing for you automatically. If you 
JIT compile some function, it will automatically generate the stabs info for 
exception handling.

Nuno


----- Original Message -----
> I'm just starting to have a look at LLVM, so forgive me if this is a
> simple question.
>
> What I'd like to do is use the JIT api to construct a "program" that
> calls a series of functions. In other words I'm wanting to translate a
> scripting language into some executable assembler where each primitive
> of my scripting language is implemented with a "normal" function. That
> is, lets say I've got two primitives PUT and GET and my C++ interpretter
> goes somehting like
>
> int doPUT() {
> // Do the work of PUT
> }
>
> int doGET() {
> // Do the work of GET
> }
>
> int main() {
> // read in the script file and construct a Module containing a Function
> containing suitably ordered
> // CallInst objects that invoke doPUT and doGET
> }
>
> Can I do that? I can't figure out how to looking at the APIs.
>
> What I really want to do is figure out how GCC exception handling
> structures work, and I was using LLVM as an investigative tool. I'm some
> code to GCC on Linux where we "JIT" a function into memory (not using
> LLVM, I've just done it by manually generating x86 instructions) where
> that dynamically created function just consists of a series of function
> calls to other, "normal" functions that do the work. However I'm having
> trouble with getting exceptions propagated through my dynamically
> created function and I'm struggling to figure out quite what exception
> handling tables I need to generate and quite how I make the GCC
> exception handler find them. So in other words I've got
>
> int doPUT() {
> throw IOException;
> }
>
> int doGET() {
> throw IOException
> }
>
> int main() {
> // magic up my function in memory containing calls to doGET and doPUT.
> try {
> // call my magic'd function
> }
> catch (IOException) {
> }
> }
>
>
>
> I've found various bits of information about the structure of the
> exception handling tables that GCC uses, but so far I can't see the
> whole picture in terms of what I'd actually do with those tables in
> terms of registering them with the exception handler. I was hoping that
> I could mock up something similar using LLVM to see what it did.
>
> Any help on this would be great thanks.
>
> Tom Quarendon. 




More information about the llvm-dev mailing list