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);
64 /// createBitcodeVerifierPass - Create a pass that writes a module to disk and
65 /// then reads the module back in to verify bitcode serialization and
67 ModulePass *createBitcodeVerifierPass(raw_ostream &Str);
69 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
70 /// for an LLVM IR bitcode wrapper.
72 static inline bool isBitcodeWrapper(const unsigned char *BufPtr,
73 const unsigned char *BufEnd) {
74 // See if you can find the hidden message in the magic bytes :-).
75 // (Hint: it's a little-endian encoding.)
76 return BufPtr != BufEnd &&
83 /// isRawBitcode - Return true if the given bytes are the magic bytes for
84 /// raw LLVM IR bitcode (without a wrapper).
86 static inline bool isRawBitcode(const unsigned char *BufPtr,
87 const unsigned char *BufEnd) {
88 // These bytes sort of have a hidden message, but it's not in
89 // little-endian this time, and it's a little redundant.
90 return BufPtr != BufEnd &&
97 /// isBitcode - Return true if the given bytes are the magic bytes for
98 /// LLVM IR bitcode, either with or without a wrapper.
100 static bool inline isBitcode(const unsigned char *BufPtr,
101 const unsigned char *BufEnd) {
102 return isBitcodeWrapper(BufPtr, BufEnd) ||
103 isRawBitcode(BufPtr, BufEnd);
106 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
107 /// header for padding or other reasons. The format of this header is:
109 /// struct bc_header {
110 /// uint32_t Magic; // 0x0B17C0DE
111 /// uint32_t Version; // Version, currently always 0.
112 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
113 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
114 /// ... potentially other gunk ...
117 /// This function is called when we find a file with a matching magic number.
118 /// In this case, skip down to the subsection of the file that is actually a
120 static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
121 unsigned char *&BufEnd) {
123 KnownHeaderSize = 4*4, // Size of header we read.
124 OffsetField = 2*4, // Offset in bytes to Offset field.
125 SizeField = 3*4 // Offset in bytes to Size field.
128 // Must contain the header!
129 if (BufEnd-BufPtr < KnownHeaderSize) return true;
131 unsigned Offset = ( BufPtr[OffsetField ] |
132 (BufPtr[OffsetField+1] << 8) |
133 (BufPtr[OffsetField+2] << 16) |
134 (BufPtr[OffsetField+3] << 24));
135 unsigned Size = ( BufPtr[SizeField ] |
136 (BufPtr[SizeField +1] << 8) |
137 (BufPtr[SizeField +2] << 16) |
138 (BufPtr[SizeField +3] << 24));
140 // Verify that Offset+Size fits in the file.
141 if (Offset+Size > unsigned(BufEnd-BufPtr))
144 BufEnd = BufPtr+Size;
147 } // End llvm namespace