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/IR/DiagnosticInfo.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/MemoryBuffer.h"
24 class BitstreamWriter;
31 /// Read the header of the specified bitcode buffer and prepare for lazy
32 /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
33 /// lazily load metadata as well. If successful, this moves Buffer. On
34 /// error, this *does not* move Buffer.
36 getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
38 DiagnosticHandlerFunction DiagnosticHandler = nullptr,
39 bool ShouldLazyLoadMetadata = false);
41 /// Read the header of the specified stream and prepare for lazy
42 /// deserialization and streaming of function bodies.
43 ErrorOr<std::unique_ptr<Module>> getStreamedBitcodeModule(
44 StringRef Name, DataStreamer *Streamer, LLVMContext &Context,
45 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
47 /// Read the header of the specified bitcode buffer and extract just the
48 /// triple information. If successful, this returns a string. On error, this
51 getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
52 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
54 /// Read the specified bitcode file, returning the module.
56 parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
57 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
59 /// WriteBitcodeToFile - Write the specified module to the specified
60 /// raw output stream. For streams where it matters, the given stream
61 /// should be in "binary" mode.
62 void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
65 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
66 /// for an LLVM IR bitcode wrapper.
68 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 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 inline bool 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 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
117 /// contain the whole bitcode file.
118 inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
119 const unsigned char *&BufEnd,
120 bool VerifyBufferSize) {
122 KnownHeaderSize = 4*4, // Size of header we read.
123 OffsetField = 2*4, // Offset in bytes to Offset field.
124 SizeField = 3*4 // Offset in bytes to Size field.
127 // Must contain the header!
128 if (BufEnd-BufPtr < KnownHeaderSize) return true;
130 unsigned Offset = ( BufPtr[OffsetField ] |
131 (BufPtr[OffsetField+1] << 8) |
132 (BufPtr[OffsetField+2] << 16) |
133 (BufPtr[OffsetField+3] << 24));
134 unsigned Size = ( BufPtr[SizeField ] |
135 (BufPtr[SizeField +1] << 8) |
136 (BufPtr[SizeField +2] << 16) |
137 (BufPtr[SizeField +3] << 24));
139 // Verify that Offset+Size fits in the file.
140 if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
143 BufEnd = BufPtr+Size;
147 const std::error_category &BitcodeErrorCategory();
148 enum class BitcodeError { InvalidBitcodeSignature, CorruptedBitcode };
149 inline std::error_code make_error_code(BitcodeError E) {
150 return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
153 class BitcodeDiagnosticInfo : public DiagnosticInfo {
158 BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity,
160 void print(DiagnosticPrinter &DP) const override;
161 std::error_code getError() const { return EC; };
163 static bool classof(const DiagnosticInfo *DI) {
164 return DI->getKind() == DK_Bitcode;
168 } // End llvm namespace
171 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};