如何在LLVM中声明一个函数(带有特定的签名)并创建一个调用,例如
llvm::Value* return = m_builder.CreateCall( function, arguments );
Run Code Online (Sandbox Code Playgroud)
但是之后定义函数体(必须是InlineAsm函数)?
我稍后以下列方式访问模块中的函数
for (llvm::Module::iterator it = mod->begin(), end = mod->end(); it != end; ++it)
{
if( needsImplementation(it) ) {
llvm::InlineAsm* inlineCall = ...
it.body = inlineCall // This doesn't exist, pseudocode for what I need
}
}
Run Code Online (Sandbox Code Playgroud)
既然签名是相同的,我相信这应该是可能的.
从"Kaleidoscope:代码生成到LLVM IR"手册:http://llvm.org/docs/tutorial/LangImpl3.html
3.4.功能代码生成
原型和函数的代码生成必须处理许多细节,这使得它们的代码不如表达式代码生成美观,但允许我们说明一些重要的观点.首先,我们来谈谈原型的代码生成:它们既用于函数体,也用于外部函数声明.代码以:
Function *PrototypeAST::Codegen() {
// Make the function type: double(double,double) etc.
std::vector<Type*> Doubles(Args.size(),
Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
Doubles, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
Run Code Online (Sandbox Code Playgroud)
稍后,当您想要向函数添加IR时,您应该从模块获取其声明:TheModule->getFunction(Name);并添加BasicBlock:
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
Builder.SetInsertPoint(BB);
Run Code Online (Sandbox Code Playgroud)
PS:答案未经测试,回答者不是LLVM的专家.
PPS:对于InlineAsm函数,我认为在使用MetaGer进行搜索之后,您无法声明Kaleidoscope引用的函数.唯一的方法是 InlineAsm在通话地点创建功能.这种用法的示例如下:CyanogenMod/android/art/compiler/llvm/runtime_support_builder_x86.cc#44
44 Value* RuntimeSupportBuilderX86::EmitGetCurrentThread() {
45 Function* ori_func = GetRuntimeSupportFunction(runtime_support::GetCurrentThread);
// ^^^^^ this is used only to know right Type of Function.
46 std::string inline_asm(StringPrintf("mov %%fs:%d, $0", Thread::SelfOffset().Int32Value())); // <<< define the body of InlineAsm
47 InlineAsm* func = InlineAsm::get(ori_func->getFunctionType(), inline_asm, "=r", false); // << Create InlineAsm function
48 CallInst* thread = irb_.CreateCall(func); // << Call it
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2755 次 |
| 最近记录: |