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
20 class BitstreamWriter;
28 /// getLazyBitcodeModule - Read the header of the specified bitcode buffer
29 /// and prepare for lazy deserialization of function bodies. If successful,
30 /// this takes ownership of 'buffer' and returns a non-null pointer. On
31 /// error, this returns null, *does not* take ownership of Buffer, and fills
32 /// in *ErrMsg with an error description if ErrMsg is non-null.
33 Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
35 std::string *ErrMsg = 0);
37 /// getStreamedBitcodeModule - Read the header of the specified stream
38 /// and prepare for lazy deserialization and streaming of function bodies.
39 /// On error, this returns null, and fills in *ErrMsg with an error
40 /// description if ErrMsg is non-null.
41 Module *getStreamedBitcodeModule(const std::string &name,
42 DataStreamer *streamer,
44 std::string *ErrMsg = 0);
46 /// getBitcodeTargetTriple - Read the header of the specified bitcode
47 /// buffer and extract just the triple information. If successful,
48 /// this returns a string and *does not* take ownership
49 /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
50 /// if ErrMsg is non-null.
51 std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
53 std::string *ErrMsg = 0);
55 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
56 /// If an error occurs, this returns null and fills in *ErrMsg if it is
57 /// non-null. This method *never* takes ownership of Buffer.
58 Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext &Context,
59 std::string *ErrMsg = 0);
61 /// WriteBitcodeToFile - Write the specified module to the specified
62 /// raw output stream. For streams where it matters, the given stream
63 /// should be in "binary" mode.
64 void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
66 /// WriteBitcodeToStream - Write the specified module to the specified
67 /// raw output stream.
68 void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
70 /// createBitcodeWriterPass - Create and return a pass that writes the module
71 /// to the specified ostream.
72 ModulePass *createBitcodeWriterPass(raw_ostream &Str);
75 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
76 /// for an LLVM IR bitcode wrapper.
78 static inline bool isBitcodeWrapper(const unsigned char *BufPtr,
79 const unsigned char *BufEnd) {
80 // See if you can find the hidden message in the magic bytes :-).
81 // (Hint: it's a little-endian encoding.)
82 return BufPtr != BufEnd &&
89 /// isRawBitcode - Return true if the given bytes are the magic bytes for
90 /// raw LLVM IR bitcode (without a wrapper).
92 static inline bool isRawBitcode(const unsigned char *BufPtr,
93 const unsigned char *BufEnd) {
94 // These bytes sort of have a hidden message, but it's not in
95 // little-endian this time, and it's a little redundant.
96 return BufPtr != BufEnd &&
103 /// isBitcode - Return true if the given bytes are the magic bytes for
104 /// LLVM IR bitcode, either with or without a wrapper.
106 static bool inline isBitcode(const unsigned char *BufPtr,
107 const unsigned char *BufEnd) {
108 return isBitcodeWrapper(BufPtr, BufEnd) ||
109 isRawBitcode(BufPtr, BufEnd);
112 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
113 /// header for padding or other reasons. The format of this header is:
115 /// struct bc_header {
116 /// uint32_t Magic; // 0x0B17C0DE
117 /// uint32_t Version; // Version, currently always 0.
118 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
119 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
120 /// ... potentially other gunk ...
123 /// This function is called when we find a file with a matching magic number.
124 /// In this case, skip down to the subsection of the file that is actually a
126 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
127 /// contain the whole bitcode file.
128 static inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
129 const unsigned char *&BufEnd,
130 bool VerifyBufferSize) {
132 KnownHeaderSize = 4*4, // Size of header we read.
133 OffsetField = 2*4, // Offset in bytes to Offset field.
134 SizeField = 3*4 // Offset in bytes to Size field.
137 // Must contain the header!
138 if (BufEnd-BufPtr < KnownHeaderSize) return true;
140 unsigned Offset = ( BufPtr[OffsetField ] |
141 (BufPtr[OffsetField+1] << 8) |
142 (BufPtr[OffsetField+2] << 16) |
143 (BufPtr[OffsetField+3] << 24));
144 unsigned Size = ( BufPtr[SizeField ] |
145 (BufPtr[SizeField +1] << 8) |
146 (BufPtr[SizeField +2] << 16) |
147 (BufPtr[SizeField +3] << 24));
149 // Verify that Offset+Size fits in the file.
150 if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
153 BufEnd = BufPtr+Size;
156 } // End llvm namespace