LLVM  3.7.0
ReaderWriter.h
Go to the documentation of this file.
1 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header defines interfaces to read and write LLVM bitcode files/streams.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_BITCODE_READERWRITER_H
15 #define LLVM_BITCODE_READERWRITER_H
16 
17 #include "llvm/IR/DiagnosticInfo.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/ErrorOr.h"
21 #include <memory>
22 #include <string>
23 
24 namespace llvm {
25  class BitstreamWriter;
26  class DataStreamer;
27  class LLVMContext;
28  class Module;
29  class ModulePass;
30  class raw_ostream;
31 
32  /// Read the header of the specified bitcode buffer and prepare for lazy
33  /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
34  /// lazily load metadata as well. If successful, this moves Buffer. On
35  /// error, this *does not* move Buffer.
36  ErrorOr<std::unique_ptr<Module>>
37  getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
38  LLVMContext &Context,
39  DiagnosticHandlerFunction DiagnosticHandler = nullptr,
40  bool ShouldLazyLoadMetadata = false);
41 
42  /// Read the header of the specified stream and prepare for lazy
43  /// deserialization and streaming of function bodies.
44  ErrorOr<std::unique_ptr<Module>> getStreamedBitcodeModule(
45  StringRef Name, std::unique_ptr<DataStreamer> Streamer,
46  LLVMContext &Context,
47  DiagnosticHandlerFunction DiagnosticHandler = nullptr);
48 
49  /// Read the header of the specified bitcode buffer and extract just the
50  /// triple information. If successful, this returns a string. On error, this
51  /// returns "".
52  std::string
53  getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
54  DiagnosticHandlerFunction DiagnosticHandler = nullptr);
55 
56  /// Read the specified bitcode file, returning the module.
57  ErrorOr<std::unique_ptr<Module>>
58  parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
59  DiagnosticHandlerFunction DiagnosticHandler = nullptr);
60 
61  /// \brief Write the specified module to the specified raw output stream.
62  ///
63  /// For streams where it matters, the given stream should be in "binary"
64  /// mode.
65  ///
66  /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
67  /// Value in \c M. These will be reconstructed exactly when \a M is
68  /// deserialized.
69  void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
70  bool ShouldPreserveUseListOrder = false);
71 
72  /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
73  /// for an LLVM IR bitcode wrapper.
74  ///
75  inline bool isBitcodeWrapper(const unsigned char *BufPtr,
76  const unsigned char *BufEnd) {
77  // See if you can find the hidden message in the magic bytes :-).
78  // (Hint: it's a little-endian encoding.)
79  return BufPtr != BufEnd &&
80  BufPtr[0] == 0xDE &&
81  BufPtr[1] == 0xC0 &&
82  BufPtr[2] == 0x17 &&
83  BufPtr[3] == 0x0B;
84  }
85 
86  /// isRawBitcode - Return true if the given bytes are the magic bytes for
87  /// raw LLVM IR bitcode (without a wrapper).
88  ///
89  inline bool isRawBitcode(const unsigned char *BufPtr,
90  const unsigned char *BufEnd) {
91  // These bytes sort of have a hidden message, but it's not in
92  // little-endian this time, and it's a little redundant.
93  return BufPtr != BufEnd &&
94  BufPtr[0] == 'B' &&
95  BufPtr[1] == 'C' &&
96  BufPtr[2] == 0xc0 &&
97  BufPtr[3] == 0xde;
98  }
99 
100  /// isBitcode - Return true if the given bytes are the magic bytes for
101  /// LLVM IR bitcode, either with or without a wrapper.
102  ///
103  inline bool isBitcode(const unsigned char *BufPtr,
104  const unsigned char *BufEnd) {
105  return isBitcodeWrapper(BufPtr, BufEnd) ||
106  isRawBitcode(BufPtr, BufEnd);
107  }
108 
109  /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
110  /// header for padding or other reasons. The format of this header is:
111  ///
112  /// struct bc_header {
113  /// uint32_t Magic; // 0x0B17C0DE
114  /// uint32_t Version; // Version, currently always 0.
115  /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
116  /// uint32_t BitcodeSize; // Size of traditional bitcode file.
117  /// ... potentially other gunk ...
118  /// };
119  ///
120  /// This function is called when we find a file with a matching magic number.
121  /// In this case, skip down to the subsection of the file that is actually a
122  /// BC file.
123  /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
124  /// contain the whole bitcode file.
125  inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
126  const unsigned char *&BufEnd,
127  bool VerifyBufferSize) {
128  enum {
129  KnownHeaderSize = 4*4, // Size of header we read.
130  OffsetField = 2*4, // Offset in bytes to Offset field.
131  SizeField = 3*4 // Offset in bytes to Size field.
132  };
133 
134  // Must contain the header!
135  if (BufEnd-BufPtr < KnownHeaderSize) return true;
136 
137  unsigned Offset = support::endian::read32le(&BufPtr[OffsetField]);
138  unsigned Size = support::endian::read32le(&BufPtr[SizeField]);
139 
140  // Verify that Offset+Size fits in the file.
141  if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
142  return true;
143  BufPtr += Offset;
144  BufEnd = BufPtr+Size;
145  return false;
146  }
147 
150  inline std::error_code make_error_code(BitcodeError E) {
151  return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
152  }
153 
155  const Twine &Msg;
156  std::error_code EC;
157 
158  public:
159  BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity,
160  const Twine &Msg);
161  void print(DiagnosticPrinter &DP) const override;
162  std::error_code getError() const { return EC; };
163 
164  static bool classof(const DiagnosticInfo *DI) {
165  return DI->getKind() == DK_Bitcode;
166  }
167  };
168 
169 } // End llvm namespace
170 
171 namespace std {
172 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};
173 }
174 
175 #endif
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
BitcodeError
Definition: ReaderWriter.h:149
bool isBitcodeWrapper(const unsigned char *BufPtr, const unsigned char *BufEnd)
isBitcodeWrapper - Return true if the given bytes are the magic bytes for an LLVM IR bitcode wrapper...
Definition: ReaderWriter.h:75
ErrorOr< std::unique_ptr< Module > > getStreamedBitcodeModule(StringRef Name, std::unique_ptr< DataStreamer > Streamer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr)
Read the header of the specified stream and prepare for lazy deserialization and streaming of functio...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
std::error_code make_error_code(BitcodeError E)
Definition: ReaderWriter.h:150
Interface for custom diagnostic printing.
const std::error_category & BitcodeErrorCategory()
BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity, const Twine &Msg)
This is the base abstract class for diagnostic reporting in the backend.
uint32_t read32le(const void *p)
Definition: Endian.h:212
void WriteBitcodeToFile(const Module *M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false)
Write the specified module to the specified raw output stream.
ErrorOr< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr)
Read the specified bitcode file, returning the module.
static ManagedStatic< _object_error_category > error_category
bool isBitcode(const unsigned char *BufPtr, const unsigned char *BufEnd)
isBitcode - Return true if the given bytes are the magic bytes for LLVM IR bitcode, either with or without a wrapper.
Definition: ReaderWriter.h:103
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
std::function< void(const DiagnosticInfo &)> DiagnosticHandlerFunction
std::string getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr)
Read the header of the specified bitcode buffer and extract just the triple information.
bool isRawBitcode(const unsigned char *BufPtr, const unsigned char *BufEnd)
isRawBitcode - Return true if the given bytes are the magic bytes for raw LLVM IR bitcode (without a ...
Definition: ReaderWriter.h:89
bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr, const unsigned char *&BufEnd, bool VerifyBufferSize)
SkipBitcodeWrapperHeader - Some systems wrap bc files with a special header for padding or other reas...
Definition: ReaderWriter.h:125
static bool classof(const DiagnosticInfo *DI)
Definition: ReaderWriter.h:164
std::error_code getError() const
Definition: ReaderWriter.h:162
Provides ErrorOr<T> smart pointer.
ErrorOr< std::unique_ptr< Module > > getLazyBitcodeModule(std::unique_ptr< MemoryBuffer > &&Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr, bool ShouldLazyLoadMetadata=false)
Read the header of the specified bitcode buffer and prepare for lazy deserialization of function bodi...
int getKind() const