2012-12-18 85 views
5

现在我使用铛建立我的.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是一个点模块实例

+0

使用正确的函数确实是来自'Bitcode/ReaderWriter.h'的'WriteBitcodeToFile'。如果您不能使用它,您应该提供您在此问题中尝试编译的代码。 – Oak

+0

是的,Follow是WriteBitcodeToFilevoid的原型:WriteBitcodeToFile(const Module * M,raw_ostream &Out); –

+0

是的,Follow是WriteBitcodeToFilevoid的原型:WriteBitcodeToFile(const Module * M,raw_ostream &Out);当我初始化一个raw_stream时,我买不起一个。类型匹配参数具有默认的参数 –

回答

3
  1. 要将红外文件保存到文件中,请参阅此问题的答案:writing module to .bc bitcode file
  2. 要将IR编译为目标文件,请查看llc工具并遵循它的main函数所做的操作。
+0

谢谢,但是当我初始化一个raw_ostream的实例,它是WriteBitcodeToFile的一个参数时,不要提示没有参数,也不要输入类型不匹配。 –

+0

另一个我记得,公司只是让IR成为集会,我们只是可以分配平台。 –

+0

接下来是我用来保存模块的两种方法:bool unbuffered = false; llvm :: raw_ostream ro(unbuffered); WriteBitcodeToFile(m,ro); WriteBitcodeToFile(m,ro); \t \t const char * Path =“hello2.s”; int ans = LLVMWriteBitcodeToFile(m,Path); –

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

1

写LLVM位码到文件我做的是:

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

,然后拆开使用LLVM -dis。