8

Now I use clang build my .c file to .s file. And I have used the llvm API modify the IR. However, now I can't save my modified IR to a file. I want to use "LLVMWriteBitcodeToFile", but I can't find the struct of "LLVMOpaqueModule"; I want to use "WriteBitcodeToFile", it always show me "type mismatch". And I also want to know how to build an IR file to a executable file.

Next are two methods I use to save a module:

1、First use WriteBitcodeToFile

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

2、Second use LLVMWriteBitcodeToFile

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

note: m is a point of Module instance

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Kun Lee
  • 97
  • 1
  • 8
  • The correct function to use is indeed `WriteBitcodeToFile` from `Bitcode/ReaderWriter.h`. If you fail using it, you should provide the code you are trying to compile in this question. – Oak Dec 18 '12 at 08:27
  • Yes,Follow is the prototype of WriteBitcodeToFilevoid: WriteBitcodeToFile(const Module *M, raw_ostream &Out); – Kun Lee Dec 18 '12 at 11:15
  • Yes,Follow is the prototype of WriteBitcodeToFilevoid: WriteBitcodeToFile(const Module *M, raw_ostream &Out); when I init a raw_stream, I can't afford a type match parameter. which has a defaulted parameter. – Kun Lee Dec 18 '12 at 11:20
  • Next are two methods I use to save a module: bool unbuffered = false; llvm::raw_ostream ro(unbuffered); WriteBitcodeToFile(m, ro); const char *Path = "hello2.s"; int ans = LLVMWriteBitcodeToFile(m, Path); – Kun Lee Dec 18 '12 at 16:12

3 Answers3

4
  1. For saving the IR into a file, see the answer to this question: writing module to .bc bitcode file
  2. For compiling IR to an object file, look at the llc tool and follow what its main function does.
Community
  • 1
  • 1
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Thanks, But when I init an instance of raw_ostream which is a parameter of WriteBitcodeToFile, I can't do it.Neither prompt no parameter ,nor type mismatch. – Kun Lee Dec 18 '12 at 15:55
  • And another I remember that llc just make the IR become assembly, we just can allocate the platform. – Kun Lee Dec 18 '12 at 16:01
  • Next are two methods I use to save a module: bool unbuffered = false; llvm::raw_ostream ro(unbuffered); WriteBitcodeToFile(m, ro); const char *Path = "hello2.s"; int ans = LLVMWriteBitcodeToFile(m, Path); – Kun Lee Dec 18 '12 at 16:15
  • @KunLee: could you edit your question to provide this additional information in a structured, well-formatted and well-expressed manner? That would greatly increase the chances of your question being answered – Eli Bendersky Dec 18 '12 at 16:44
  • @KunLee: do you want to write to a file? Then use `raw_fd_ostream`. If you just want to write to stdout, use `outs()`. – Eli Bendersky Dec 19 '12 at 18:56
0

Check out these functions in 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);

See also How to generate machine code with llvm

Community
  • 1
  • 1
andrewrk
  • 30,272
  • 27
  • 92
  • 113
0

for writing llvm bitcode to file what I do is :

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

and then disassemble using llvm-dis.

voidMainReturn
  • 3,339
  • 6
  • 38
  • 66