1 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines interfaces to read and write LLVM bitcode files/streams.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_BITCODE_H
15 #define LLVM_BITCODE_H
23 class BitstreamWriter;
27 /// getLazyBitcodeModule - Read the header of the specified bitcode buffer
28 /// and prepare for lazy deserialization of function bodies. If successful,
29 /// this takes ownership of 'buffer' and returns a non-null pointer. On
30 /// error, this returns null, *does not* take ownership of Buffer, and fills
31 /// in *ErrMsg with an error description if ErrMsg is non-null.
32 Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
34 std::string *ErrMsg = 0);
36 /// getBitcodeTargetTriple - Read the header of the specified bitcode
37 /// buffer and extract just the triple information. If successful,
38 /// this returns a string and *does not* take ownership
39 /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
40 /// if ErrMsg is non-null.
41 std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
43 std::string *ErrMsg = 0);
45 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
46 /// If an error occurs, this returns null and fills in *ErrMsg if it is
47 /// non-null. This method *never* takes ownership of Buffer.
48 Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
49 std::string *ErrMsg = 0);
51 /// WriteBitcodeToFile - Write the specified module to the specified
52 /// raw output stream. For streams where it matters, the given stream
53 /// should be in "binary" mode.
54 void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
56 /// WriteBitcodeToStream - Write the specified module to the specified
57 /// raw output stream.
58 void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
60 /// createBitcodeWriterPass - Create and return a pass that writes the module
61 /// to the specified ostream.
62 ModulePass *createBitcodeWriterPass(raw_ostream &Str);
65 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
66 /// for an LLVM IR bitcode wrapper.
68 static inline bool isBitcodeWrapper(const unsigned char *BufPtr,
69 const unsigned char *BufEnd) {
70 // See if you can find the hidden message in the magic bytes :-).
71 // (Hint: it's a little-endian encoding.)
72 return BufPtr != BufEnd &&
79 /// isRawBitcode - Return true if the given bytes are the magic bytes for
80 /// raw LLVM IR bitcode (without a wrapper).
82 static inline bool isRawBitcode(const unsigned char *BufPtr,
83 const unsigned char *BufEnd) {
84 // These bytes sort of have a hidden message, but it's not in
85 // little-endian this time, and it's a little redundant.
86 return BufPtr != BufEnd &&
93 /// isBitcode - Return true if the given bytes are the magic bytes for
94 /// LLVM IR bitcode, either with or without a wrapper.
96 static bool inline isBitcode(const unsigned char *BufPtr,
97 const unsigned char *BufEnd) {
98 return isBitcodeWrapper(BufPtr, BufEnd) ||
99 isRawBitcode(BufPtr, BufEnd);
102 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
103 /// header for padding or other reasons. The format of this header is:
105 /// struct bc_header {
106 /// uint32_t Magic; // 0x0B17C0DE
107 /// uint32_t Version; // Version, currently always 0.
108 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
109 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
110 /// ... potentially other gunk ...
113 /// This function is called when we find a file with a matching magic number.
114 /// In this case, skip down to the subsection of the file that is actually a
116 static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
117 unsigned char *&BufEnd) {
119 KnownHeaderSize = 4*4, // Size of header we read.
120 OffsetField = 2*4, // Offset in bytes to Offset field.
121 SizeField = 3*4 // Offset in bytes to Size field.
124 // Must contain the header!
125 if (BufEnd-BufPtr < KnownHeaderSize) return true;
127 unsigned Offset = ( BufPtr[OffsetField ] |
128 (BufPtr[OffsetField+1] << 8) |
129 (BufPtr[OffsetField+2] << 16) |
130 (BufPtr[OffsetField+3] << 24));
131 unsigned Size = ( BufPtr[SizeField ] |
132 (BufPtr[SizeField +1] << 8) |
133 (BufPtr[SizeField +2] << 16) |
134 (BufPtr[SizeField +3] << 24));
136 // Verify that Offset+Size fits in the file.
137 if (Offset+Size > unsigned(BufEnd-BufPtr))
140 BufEnd = BufPtr+Size;
143 } // End llvm namespace