Hi
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?
I want to have something like this:
int factorial (int n) { return n==0 ? 1 : n*factorial(n-1); }
typedef int (*intFunc) (int);
intFunc fptr = factorial;
Value * N = ConstantInt::get(Type::Int32Ty,10);
irb.CreateCall(fptr,N);
OR
Function *F = Function::Create(intFuncTy,Function::ExternalLinkage,fptr,myModule);
irb.CreateCall(F,N);
Thanks
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