Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing module to .bc bitcode file

Tags:

file-io

llvm

i've assumed that dumping a .bc file from a module was a trivial operation, but now, first time i have to actually do it from code, for the life of me i can't find one missing step in the process:

static void WriteModule ( const Module *  M, BitstreamWriter &  Stream )

http://llvm.org/docs/doxygen/html/BitcodeWriter_8cpp.html#a828cec7a8fed9d232556420efef7ae89

to write that module, first i need a BistreamWriter

BitstreamWriter::BitstreamWriter (SmallVectorImpl< char > &O)

http://llvm.org/docs/doxygen/html/classllvm_1_1BitstreamWriter.html

and for a BitstreamWriter i need a SmallVectorImpl. But, what next? Should i write the content of the SmallVectorImpl byte by byte on a file handler myself? is there a llvm api for this? do i need something else?

like image 946
lurscher Avatar asked Dec 16 '12 17:12

lurscher


People also ask

What is a Bitcode file?

What is commonly known as the LLVM bitcode file format (also, sometimes anachronistically known as bytecode) is actually two things: a bitstream container format and an encoding of LLVM IR into the container format. The bitstream format is an abstract encoding of structured data, very similar to XML in some ways.

What is LLVM byte code?

The LLVM bytecode representation is used to store the intermediate representation on disk in compacted form. The LLVM bytecode format may change in the future, but LLVM will always be backwards compatible with older formats. This document will only describe the most current version of the bytecode format.

How do I run a BC file?

It is possible to execute a bc-file with lli command. However that doesn't create a stand alone executable product. Show activity on this post. There's always the option of using llc to compile to assembly from which you can generate an executable.

Is LLVM IR bytecode?

LLVM is a three-phase compiler that supports multiple source languages and target architectures. It uses a common intermediate representation (IR) bytecode in its optimizer. Consequently, any supported high-level programming language is trans- formed to this IR bytecode as part of the LLVM compilation process.


1 Answers

The WriteModule function is static within lib/Bitcode/Writer/BitcodeWriter.cpp, which means it's not there for outside consumption (you can't even access it).

The same file has another function, however, called WriteBitcodeToFile, with this interface:

/// WriteBitcodeToFile - Write the specified module to the specified output
/// stream.
void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out);

I can't imagine a more convenient interface. The header file declaring it is ./include/llvm/Bitcode/ReaderWriter.h, by the way.

like image 190
Eli Bendersky Avatar answered Sep 30 '22 21:09

Eli Bendersky