如何将IR保存到文件并将其构建为可执行文件?

时间:2012-12-18 07:26:58

标签: build llvm llvm-ir

现在我使用clang将我的.c文件构建到.s文件中。而且我已经使用llvm API修改了IR。但是,现在我无法将修改后的IR保存到文件中。我想使用“LLVMWriteBitcodeToFile”,但我找不到“LLVMOpaqueModule”的结构;我想使用“WriteBitcodeToFile”,它总是显示“类型不匹配”。我还想知道如何将IR文件构建到可执行文件中。

接下来是我用来保存模块的两种方法:

1,首先使用WriteBitcodeToFile

bool unbuffered = false; 
llvm::raw_ostream ro(unbuffered); 
WriteBitcodeToFile(m, ro); 

2,二次使用LLVMWriteBitcodeToFile

const char *Path = "hello2.s"; 
int ans = LLVMWriteBitcodeToFile(m, Path); 

注意:m是模块实例的一个点

3 个答案:

答案 0 :(得分:3)

  1. 要将IR保存到文件中,请参阅此问题的答案:writing module to .bc bitcode file
  2. 要将IR编译为目标文件,请查看llc工具,并按照main函数的作用进行操作。

答案 1 :(得分:1)

将llvm bitcode写入文件我的目的是:

std::error_code EC;
llvm::raw_fd_ostream OS("module", EC, llvm::sys::fs::F_None);
WriteBitcodeToFile(pBiFModule, OS);
OS.flush();

然后使用llvm-dis。

进行反汇编

答案 2 :(得分:0)

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);

另见How to generate machine code with llvm

相关问题