如何在LLVM中声明函数并稍后定义它
How to declare a function in LLVM and define it later
我如何在LLVM中声明一个函数(带有特定的签名)并创建对它的调用,例如
llvm::Value* return = m_builder.CreateCall( function, arguments );
但是稍后定义函数体(必须是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
}
}
由于签名是相同的,我相信这应该是可能的。
选自《万花筒:代码生成到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);
之后,当你想要添加IR到函数时,你应该从模块:TheModule->getFunction(Name);
中获得它的声明,并添加一个BasicBlock:
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
Builder.SetInsertPoint(BB);
PS:答案是未经测试的,回答者不是LLVM专家。
PPS:对于InlineAsm函数,正如我在使用MetaGer进行搜索后认为的那样,您不能声明函数引用自万花筒。唯一的方法是在调用的地方创建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
相关文章:
- 在命名空间中定义函数还是限定函数
- 为什么在定义函数之前先声明它
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 这个c++代码是如何在没有定义函数的情况下运行的
- 具有外部"c"和程序集的未定义函数
- 已定义函数时出现 G++ "未定义的引用"错误
- 将自定义函数传递到基抽象类中以延迟执行
- C++使用 rand 定义函数语法
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 将具有固定签名的自定义函数名称注入 CRTP
- 使用定义函数模板别名
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 在内联程序集中定义函数和从 C++ 调用时出现问题
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 声明和定义函数静态会产生"undefined reference to function_name()"
- 朋友定义函数的名称空间是什么
- 介子 对用户定义函数的未定义引用
- 如何使用 "using" 关键字定义函数原型/签名
- 在 C 结构中定义C++函数