[LLVMdev] transitions from C code to LLVM JIT code and back
sabre at nondot.org
Mon Oct 15 19:48:32 CDT 2007
On Oct 15, 2007, at 2:40 PM, Rob Barris wrote:
> Started playing with LLVM this week, picking apart the examples to
> see how they work.
> So I think I understand the basic process of instantiating a program
> and getting LLVM to generate the runnable code, and then running it.
> What I have not seen an example of, and want to know if it's
> possible, is having LLVM generated code calling back out to plain C
> code during its run.
Yep, it absolutely is.
> What operations do you insert to encode the
> concept of calling non-LLVM generated code (putting the needed args
> on the stack, actually making the call, then cleaning up on return to
> the LLVM generated code).
Assuming you're talking about simple C functions, there are two ways:
1) just make an llvm function declaration, use C calling convention,
and call it like any other function. If you're using the JIT, it
will call 'dlsym' to resolve the function to a function of the same
name, and generated code will call it directly. If you're using llc,
the native linker will match it up like any other function.
2) if you're using the JIT, you can explicitly add mappings to the
JIT table, by calling the ExecutionEngine::addGlobalMapping(Func,
addr) method. This lets you map an arbitrary llvm function (e.g. a
declaration) to an arbitrary address. This lets you avoid tying the
name of the llvm code into the name of functions in your address
space and even lets you have the LLVM JIT generate code that calls
other peoples' JITs if you care to ;-)
More information about the LLVMdev