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_READERWRITER_H
15 #define LLVM_BITCODE_READERWRITER_H
17 #include "llvm/Support/ErrorOr.h"
18 #include "llvm/Support/MemoryBuffer.h"
23 class BitstreamWriter;
30 /// Read the header of the specified bitcode buffer and prepare for lazy
31 /// deserialization of function bodies. If successful, this moves Buffer. On
32 /// error, this *does not* move Buffer.
33 ErrorOr<Module *> getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
34 LLVMContext &Context);
36 /// getStreamedBitcodeModule - Read the header of the specified stream
37 /// and prepare for lazy deserialization and streaming of function bodies.
38 /// On error, this returns null, and fills in *ErrMsg with an error
39 /// description if ErrMsg is non-null.
40 Module *getStreamedBitcodeModule(const std::string &name,
41 DataStreamer *streamer,
43 std::string *ErrMsg = nullptr);
45 /// Read the header of the specified bitcode buffer and extract just the
46 /// triple information. If successful, this returns a string. On error, this
48 std::string getBitcodeTargetTriple(MemoryBufferRef Buffer,
49 LLVMContext &Context);
51 /// Read the specified bitcode file, returning the module.
52 ErrorOr<Module *> parseBitcodeFile(MemoryBufferRef Buffer,
53 LLVMContext &Context);
55 /// WriteBitcodeToFile - Write the specified module to the specified
56 /// raw output stream. For streams where it matters, the given stream
57 /// should be in "binary" mode.
58 void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
61 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
62 /// for an LLVM IR bitcode wrapper.
64 inline bool isBitcodeWrapper(const unsigned char *BufPtr,
65 const unsigned char *BufEnd) {
66 // See if you can find the hidden message in the magic bytes :-).
67 // (Hint: it's a little-endian encoding.)
68 return BufPtr != BufEnd &&
75 /// isRawBitcode - Return true if the given bytes are the magic bytes for
76 /// raw LLVM IR bitcode (without a wrapper).
78 inline bool isRawBitcode(const unsigned char *BufPtr,
79 const unsigned char *BufEnd) {
80 // These bytes sort of have a hidden message, but it's not in
81 // little-endian this time, and it's a little redundant.
82 return BufPtr != BufEnd &&
89 /// isBitcode - Return true if the given bytes are the magic bytes for
90 /// LLVM IR bitcode, either with or without a wrapper.
92 inline bool isBitcode(const unsigned char *BufPtr,
93 const unsigned char *BufEnd) {
94 return isBitcodeWrapper(BufPtr, BufEnd) ||
95 isRawBitcode(BufPtr, BufEnd);
98 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
99 /// header for padding or other reasons. The format of this header is:
101 /// struct bc_header {
102 /// uint32_t Magic; // 0x0B17C0DE
103 /// uint32_t Version; // Version, currently always 0.
104 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
105 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
106 /// ... potentially other gunk ...
109 /// This function is called when we find a file with a matching magic number.
110 /// In this case, skip down to the subsection of the file that is actually a
112 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
113 /// contain the whole bitcode file.
114 inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
115 const unsigned char *&BufEnd,
116 bool VerifyBufferSize) {
118 KnownHeaderSize = 4*4, // Size of header we read.
119 OffsetField = 2*4, // Offset in bytes to Offset field.
120 SizeField = 3*4 // Offset in bytes to Size field.
123 // Must contain the header!
124 if (BufEnd-BufPtr < KnownHeaderSize) return true;
126 unsigned Offset = ( BufPtr[OffsetField ] |
127 (BufPtr[OffsetField+1] << 8) |
128 (BufPtr[OffsetField+2] << 16) |
129 (BufPtr[OffsetField+3] << 24));
130 unsigned Size = ( BufPtr[SizeField ] |
131 (BufPtr[SizeField +1] << 8) |
132 (BufPtr[SizeField +2] << 16) |
133 (BufPtr[SizeField +3] << 24));
135 // Verify that Offset+Size fits in the file.
136 if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
139 BufEnd = BufPtr+Size;
143 const std::error_category &BitcodeErrorCategory();
144 enum class BitcodeError {
145 ConflictingMETADATA_KINDRecords,
146 CouldNotFindFunctionInStream,
148 InsufficientFunctionProtos,
149 InvalidBitcodeSignature,
150 InvalidBitcodeWrapperHeader,
151 InvalidConstantReference,
152 InvalidID, // A read identifier is not found in the table it should be in.
153 InvalidInstructionWithNoBB,
154 InvalidRecord, // A read record doesn't have the expected size or structure
155 InvalidTypeForValue, // Type read OK, but is invalid for its use
157 InvalidType, // We were unable to read a type
158 MalformedBlock, // We are unable to advance in the stream.
159 MalformedGlobalInitializerSet,
160 InvalidMultipleBlocks, // We found multiple blocks of a kind that should
162 NeverResolvedValueFoundInFunction,
163 NeverResolvedFunctionFromBlockAddress,
164 InvalidValue // Invalid version, inst number, attr number, etc
166 inline std::error_code make_error_code(BitcodeError E) {
167 return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
170 } // End llvm namespace
173 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};