以下是打印LLVM IR的hello world
的代码:
#include "llvm/ADT/ArrayRef.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
#include <vector>
#include <string>
int main() {
llvm::LLVMContext &Context = llvm::getGlobalContext();
llvm::IRBuilder<> builder(Context);
llvm::Module *pModule = new llvm::Module("hello world", Context);
llvm::FunctionType *funcType = llvm::FunctionType::get(builder.getInt32Ty(), false);
llvm::Function *mainFunc = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", pModule);
llvm::BasicBlock *pEntryBB = llvm::BasicBlock::Create(Context, "EntryPoint", mainFunc);
builder.SetInsertPoint(pEntryBB);
llvm::Value *pHelloWorldStr = builder.CreateGlobalStringPtr("hello world!\n");
std::vector<llvm::Type *> PrintfProtoArgs;
PrintfProtoArgs.push_back(builder.getInt8Ty()->getPointerTo());
llvm::ArrayRef<llvm::Type*> argsRef(PrintfProtoArgs);
llvm::FunctionType *pPrintfType = llvm::FunctionType::get(builder.getInt32Ty(), argsRef, false);
llvm::Constant *pPrintfFunc = pModule->getOrInsertFunction("printf", pPrintfType);
builder.CreateCall(pPrintfFunc, pHelloWorldStr);
builder.CreateRetVoid();
pModule->dump();
return 0;
}
我正在尝试丢弃llvm::IRBuilder<>
并将其替换为某些Module / Function / BasicBlock指定的函数:
builder.CreateCall
- &gt; llvm::CallInst::llvm::CallInst::Create(pPrintfFunc, pHelloWorldStr, "callPrintf", pEntryBB)
builder.CreateRetVoid()
- &gt; llvm::ReturnInst::Create(Context, pConsIntZero, pEntryBB);
builder.getInt32Ty()
- &gt; llvm::Type::getInt32Ty(Context)
现在的问题是我找不到CreateGlobalStringPtr
的替代方案:
llvm::Value *pHelloWorldStr = builder.CreateGlobalStringPtr("hello world!\n");
那么,如何在不使用构建器的情况下声明全局字符串指针?
我认为在每种情况下都不需要llvm::IRBuilder
。有没有一种简单的方法可以找到这样的替代功能?