3

hello worldLLVM IRを出力するコードは次のとおりです。

#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->llvm::CallInst::llvm::CallInst::Create(pPrintfFunc, pHelloWorldStr, "callPrintf", pEntryBB)

builder.CreateRetVoid()->llvm::ReturnInst::Create(Context, pConsIntZero, pEntryBB);

builder.getInt32Ty()->llvm::Type::getInt32Ty(Context)

今の問題は、次の代替手段が見つからなかったことですCreateGlobalStringPtr:

llvm::Value *pHelloWorldStr = builder.CreateGlobalStringPtr("hello world!\n");

では、ビルダーを使用せずにグローバル文字列ポインターを宣言するにはどうすればよいでしょうか?

llvm::IRBuilderすべての場合に必要ではないと思います。そのような代替機能を見つける簡単な方法はありますか?

4

0 に答える 0