您无法从中获得太多信息compiled_func
- 正如你所写,这只是一个void*
。但是当你写“一般来说,我不知道函数签名是什么”时,这是不准确的 - 你刚刚编译了该函数,所以你应该有权访问 LLVMFunction
对象,可以查询其类型。确实,它是 LLVM IR 类型而不是 C++ 类型,但您通常可以知道哪个类型转换为哪个类型。
例如,如果我们借用代码教程中有关 JITting 万花筒的部分 http://llvm.org/docs/tutorial/LangImpl4.html#adding-a-jit-compiler:
if (Function *LF = F->Codegen()) {
LF->dump(); // Dump the function for exposition purposes.
// JIT the function, returning a function pointer.
void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
// Cast it to the right type (takes no arguments, returns a double) so we
// can call it as a native function.
double (*FP)() = (double (*)())(intptr_t)FPtr;
fprintf(stderr, "Evaluated to %f\n", FP());
}
好的,FPtr
被“假定”为类型double ()
,但也有LF
类型的Function*
在这里,所以你could做过类似的事情:
Type* RetTy = LF->getReturnType();
if (RetTy->isDoubleTy()) {
double (*FP)() = (double (*)())(intptr_t)FPtr;
fprintf(stderr, "Evaluated to %f\n", FP());
} else if (RetTy->isIntegerTy(32)) {
int (*FP)() = (int (*)())(intptr_t)FPtr;
fprintf(stderr, "Evaluated to %d\n", FP());
} else ...
以大致相同的方式,您可以查询函数的参数类型。
有点麻烦?您可以使用执行引擎通过其方便的方式调用该函数runFunction http://llvm.org/docs/doxygen/html/classllvm_1_1ExecutionEngine.html#a83ac805ce571233b79802b8210332039方法,它接收一个向量GenericValue http://llvm.org/docs/doxygen/html/structllvm_1_1GenericValue.htmls 并返回 aGenericValue
。您仍然应该查询Function
type 来查找每个下的基础类型GenericValue
应该。