C++ 如何在不使用llvm::IRBuilder的情况下声明全局字符串指针

C++ 如何在不使用llvm::IRBuilder的情况下声明全局字符串指针,c++,llvm,llvm-ir,llvm-c++-api,C++,Llvm,Llvm Ir,Llvm C++ Api,以下是打印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 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/ADT/ArrayRef.h”
#包括“llvm/IR/LLVMContext.h”
#包括“llvm/IR/Module.h”
#包括“llvm/IR/IRBuilder.h”
#包括
#包括
int main(){
llvm::LLVMContext&Context=llvm::getGlobalContext();
llvm::IRBuilder(上下文);
llvm::Module*pModule=新的llvm::Module(“你好,世界”,上下文);
llvm::FunctionType*FunctionType=llvm::FunctionType::get(builder.getInt32Ty(),false);
llvm::Function*mainFunc=llvm::Function::Create(funcType,llvm::Function::ExternalLinkage,“main”,pModule);
llvm::BasicBlock*pEntryBB=llvm::BasicBlock::Create(上下文,“入口点”,mainFunc);
构建器设置插入点(pEntryBB);
llvm::Value*pHelloWorldStr=builder.CreateGlobalStringPtr(“hello world!\n”);
std::矢量打印打印程序;
PrintfProtoArgs.push_back(builder.getInt8Ty()->getPointerTo());
llvm::ArrayRef argsRef(PrintfProtoArgs);
llvm::FunctionType*pPrintfType=llvm::FunctionType::get(builder.getInt32Ty(),argsRef,false);
llvm::Constant*pPrintfFunc=pModule->getOrInsertFunction(“printf”,pPrintfType);
builder.CreateCall(pPrintfFunc,pHelloWorldStr);
CreateRetVoid();
pModule->dump();
返回0;
}
我正在尝试丢弃
llvm::IRBuilder
,并将其替换为某些模块/函数/BasicBlock指定的函数:

builder.CreateCall
->
llvm::CallInst::llvm::CallInst::Create(pPrintfFunc,pHelloWorldStr,“callPrintf”,pEntryBB)

builder.CreateRetVoid()

builder.getInt32Ty()
->
llvm::Type::getInt32Ty(上下文)

现在的问题是,我找不到
CreateGlobalStringPtr
的替代方案:

llvm::Value*Phellowordstr=builder.CreateGlobalStringPtr(“hello world!\n”)

那么,如何在不使用生成器的情况下声明全局字符串指针呢


我想并非所有情况下都需要
llvm::IRBuilder
。有没有一种简单的方法可以找到这样的替代功能?

您可以查看
IRBuilder
的代码来了解它的功能。