I'm writing a compiler with LLVM. Each source file is compiled into an LLVM bitcode file. Eventually the linker links and optimizes all the bitcode files into one final binary.
I need a way to read the bitcode files in the compiler in order to access the type information. The LLVM documentation shows a class called BitcodeReader
, but that appears to be internal to LLVM.
Is there any publicly accessible way to read a bitcode file into an llvm::Module
?
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.
So in that sense, it doesn't solve the aforementioned issues: The LLVM IR is not portable to platform with a different ABI. The only reason this is more portable is that the clients provide a layer which matches the PNaCl ABI to the actual ABI.
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.
I looked through the source to the llvm-dis
tool and found the function I was looking for:
Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
std::string *ErrMsg = 0);
from llvm/Bitcode/ReaderWriter.h
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With