[LLVMdev] How can I create CallInst with a pointer to function

Gordon Henriksen gordonhenriksen at me.com
Wed Oct 8 20:55:52 PDT 2008


On 2008-10-08, at 12:56, Andrii Vasyliev wrote:

> I mean when I have in my program pointer to a function how should I
> build IR to call the function by the pointer?
> Or how can I create llvm::Function with a pointer to a function?

If you're just asking how to construct an indirect call, then calling  
a LoadInst* or Argument* or any other Value* is no different than  
calling a Function*.

If your goal is to take an arbitrary function pointer (possibly  
precompiled) from the compiler's context and statically embed a call  
to it in the program you're building, then you can do this in a couple  
of ways. Both only work in a JIT context, for reasons that should be  
obvious.

The first is to use something like this:

   Value *value = ConstantExpr::getIntToPtr(
       ConstantInt::get(reinterpret_cast<intptr_t>(fptr)),
       FunctionType::get(.......));

Secondly, you can create a Function* with no body (a declaration) and  
then use ExecutionEngine::addGlobalMapping.

— Gordon





More information about the llvm-dev mailing list