问题描述
我正在编写LLVM脚本引擎,该引擎将JIT编译为自定义语言的脚本代码.我的问题是我无法调用外部函数(甚至C99 erf()函数失败).
I'm writing a LLVM scripting engine that JIT compiles scripting code in a custom language. My problem is that I'm unable to call external functions (even the C99 erf() function is failing).
例如,如果我将erf函数"C"换成外部,
For example if I extern "C" the erf function,
extern "C" double erft(double x){
return erf(x);
}
并创建具有外部链接的功能
and create a function with external linkage
std::vector<const Type*> Double1(1,Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),Double1,false);
Function *erft = Function::Create(FT,Function::ExternalLinkage,"erft",TheModule);
在使用erft(0.0)运行脚本时得到以下错误消息:
get the following error message when running my script with erft(0.0) :
手动进行映射
void ExecutionEngine::addGlobalMapping( const GlobalValue * erfF, void * erft);
会给我以下错误:
很明显,我做错了什么.任何帮助将不胜感激
Obviously I'm doing something very wrong. Any help would be much appreciated
推荐答案
假定您尚未禁用它(通过调用EE->DisableSymbolSearching()
),则LLVM将使用dlsym()
在JIT程序本身中查找符号.取决于您的平台,这可能意味着您需要使用-fPIC
来构建JIT,或者可能根本不可用(例如在Windows上).
Assuming you haven't disabled it (by calling EE->DisableSymbolSearching()
) then LLVM will use dlsym()
to find the symbols in the JIT program itself. Depending on your platform, that might mean that you need to build your JIT with -fPIC
, or that it might not be available at all (such as on Windows).
除了自动符号搜索外,您始终可以使用EE->addGlobalMapping(GV, &function)
自己注册各个函数,其中GV =与您要调用的本机函数匹配的llvm :: Function *函数声明.在您使用ertf()的情况下,是:
Aside from automatic symbol searching, you can always register the individual functions yourself using EE->addGlobalMapping(GV, &function)
where GV = the llvm::Function* function declaration that matches the native function you're calling. In your case with ertf() that's:
EE->addGlobalMapping(erft, &::erft);
请注意,您将全局函数命名为erft()
,将局部变量命名为erft
,因此命名为"::".下次请选择其他名称!
Note that you named the global function erft()
and the local variable erft
, hence the "::". Please pick different names next time!
这篇关于将LLVM JIT代码链接到外部C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!