[LLVMdev] Beginner's question concerning JIT

Eli Friedman eli.friedman at gmail.com
Sun Nov 30 12:14:52 PST 2008


On Sun, Nov 30, 2008 at 6:59 AM, Carter Cheng <carter_cheng at yahoo.com> wrote:
> Hi,
>
> I have been looking at the LLVM JIT system as a basis for a project I am working on and had a few beginner's questions which I was hoping someone might be able to answer and which I haven't yet been able to figure out reading the source code. If anyone could provide some help I would appreciate it.
>
> 1) How does one go about calling a precompiled function external to the JIT from a JIT created function assuming I have the address? I basically want to create my own dispatcher which I will use to dispatch calls based on the arguments passed.

There are a few ways to call external functions: one, if symbols are
available, and you know the name of the relevant symbol, the JIT can
do a limited form of linking for you.  Two, you can use
addGlobalMapping to associate a function in the JIT with an arbitrary
address.  Three, you can create a ConstantInt with the address, use
inttoptr on it, and call the result.

> 2) I am assuming this function with return the address of the function in question back to the JIT. How does one go about invoking the (void*) pointer in the IR format?

For questions like this, it's often helpful to look at the IL output
of llvm-gcc (http://llvm.org/demo/, or use "llvm-gcc -emit-llvm -S").
Example:

Input:
int f(void* X) {
  return ((unsigned(*)(unsigned))X)(1);
}

Output:
define i32 @f(i8* %X) nounwind {
entry:
	%0 = bitcast i8* %X to i32 (i32)*		; <i32 (i32)*> [#uses=1]
	%1 = tail call i32 %0(i32 1) nounwind		; <i32> [#uses=1]
	ret i32 %1
}

-Eli



More information about the llvm-dev mailing list