Build 如何将IR保存到文件并将其构建到可执行文件?
现在我使用clang将.c文件构建为.s文件。我已经使用LLVMAPI修改了IR。但是,现在我无法将修改后的IR保存到文件中。我想使用“LLVMWriteBitcodeToFile”,但找不到“LLVMOpaqueModule”的结构;我想使用“WriteBitcodeToFile”,它总是显示“类型不匹配”。我还想知道如何将IR文件构建为可执行文件Build 如何将IR保存到文件并将其构建到可执行文件?,build,llvm,llvm-ir,Build,Llvm,Llvm Ir,现在我使用clang将.c文件构建为.s文件。我已经使用LLVMAPI修改了IR。但是,现在我无法将修改后的IR保存到文件中。我想使用“LLVMWriteBitcodeToFile”,但找不到“LLVMOpaqueModule”的结构;我想使用“WriteBitcodeToFile”,它总是显示“类型不匹配”。我还想知道如何将IR文件构建为可执行文件 下面是我用来保存模块的两种方法: 1、 首次使用WriteBitCodeFile bool unbuffered = false; llvm::
下面是我用来保存模块的两种方法: 1、 首次使用WriteBitCodeFile
bool unbuffered = false;
llvm::raw_ostream ro(unbuffered);
WriteBitcodeToFile(m, ro);
2、 第二次使用LLVMWriteBitCodeFile
const char *Path = "hello2.s";
int ans = LLVMWriteBitcodeToFile(m, Path);
注:m是模块实例点
llc
工具并遵循其main
函数的功能在
llvm-c/TargetMachine.h
中查看这些函数:
/** Emits an asm or object file for the given module to the filename. This
wraps several c++ only classes (among them a file stream). Returns any
error in ErrorMessage. Use LLVMDisposeMessage to dispose the message. */
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage);
/** Compile the LLVM IR stored in \p M and store the result in \p OutMemBuf. */
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M,
LLVMCodeGenFileType codegen, char** ErrorMessage, LLVMMemoryBufferRef *OutMemBuf);
另请参见以了解如何将llvm位代码写入文件我所做的是:
std::error_code EC;
llvm::raw_fd_ostream OS("module", EC, llvm::sys::fs::F_None);
WriteBitcodeToFile(pBiFModule, OS);
OS.flush();
然后使用llvm dis进行反汇编。正确使用的函数确实是
WriteBitcodeToFile
fromBitcode/ReaderWriter.h
。如果使用失败,您应该提供您在这个问题中试图编译的代码;是的,下面是WriteBitcodeToFilevoid的原型:WriteBitcodeToFile(const Module*M,raw_ostream&Out);当我初始化原始_流时,我负担不起类型匹配参数。下面是我用来保存模块的两种方法:bool unbuffered=false;llvm::未经缓冲的原始蒸汽ro;可写资产负债表(m,ro);const char*Path=“hello2.s”;int ans=LLVMWriteBitCodeFile(m,路径);谢谢,但是当我初始化一个raw_ostream的实例时,它是WriteBitCodeFile的一个参数,我不能这样做。既不提示没有参数,也不提示类型不匹配。还有一个我记得llc只是将IR变成程序集,我们只是可以分配平台。接下来是我用来保存模块的两种方法:bool unbuffered=false;llvm::未经缓冲的原始蒸汽ro;可写资产负债表(m,ro);const char*Path=“hello2.s”;int ans=LLVMWriteBitCodeFile(m,路径)@KunLee:您能否编辑您的问题,以结构化、格式良好和表达良好的方式提供这些附加信息?这将大大增加你的问题被解决的机会answered@KunLee:是否要写入文件?然后使用raw\u fd\u ostream
。如果您只想写入标准输出,请使用outs()
。