【发布时间】:2019-02-27 21:21:01
【问题描述】:
假设我想手动将下面的代码转换为 IR 代码:
#include <stdio.h>
int main()
{
int (*p)(const char *__s); // how to implement this?
p = puts; // and this?
p("Hello World!\n");
}
我发现函数指针的IR表示是这样的:
%p = alloca i32 (i8*)*, align 8
store i32 (i8*)* @puts, i32 (i8*)** %p, align 8
但我不知道应该使用哪个 api 来生成它。
这是我的部分实现:
#include "llvm/Support/raw_ostream.h"
int main() {
llvm::LLVMContext context;
llvm::IRBuilder<> builder(context);
llvm::Module *module = new llvm::Module("top", context);
llvm::FunctionType *functionType = llvm::FunctionType::get(builder.getInt32Ty(), false);
llvm::Function *mainFunction = llvm::Function::Create(functionType, llvm::Function::ExternalLinkage, "main", module);
llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "entrypoint", mainFunction);
builder.SetInsertPoint(entry);
llvm::Value *helloWorld = builder.CreateGlobalStringPtr("hello world\n");
std::vector<llvm::Type *> putArgs;
putArgs.push_back(builder.getInt8Ty()->getPointerTo());
llvm::ArrayRef<llvm::Type *> argsRef(putArgs);
llvm::FunctionType *putsType = llvm::FunctionType::get(builder.getInt32Ty(), argsRef, false);
llvm::Constant *putFunction = module->getOrInsertFunction("puts", putsType);
// code to implement function pointer
// code to assign puts() to function pointer
builder.CreateCall(putFunction, helloWorld); // call the function pointer instead of the function it self
builder.CreateRet(llvm::ConstantInt::get(builder.getInt32Ty(), 0));
module->print(llvm::errs(), nullptr);
}
我发现llvm::Function 是llvm::Value 的子类,所以我猜llvm::Constant *putFunction 本身就是我要查找的函数指针,但是如何使这个值在IR 代码中表示呢?具体来说,如何使用builder生成IR码?
【问题讨论】:
-
@puts已经是一个函数指针。alloca代码只是将该指针复制到堆栈上。根据您尝试执行的操作,您将不需要它(例如,如果您尝试创建一个 vtable,就像在您的其他问题中一样,您可以直接在全局数组中使用@puts- 和“直接”是指“附加了比特广播”,因为通常不是 vtable 中的每个函数都具有相同的类型)。 -
@sepp2k 很高兴再次见到你。这个问题的最初目的是我不知道如何将 C 风格的函数指针生成到 ir 代码中。我认为你是对的,我必须走错路。在汇编语言视图中看到一段代码对我来说很难,您有什么建议吗?我只是想发明一种 oop 玩具语言,尽可能简单,但似乎比我想象的要难。