ReaderWriter.h 6.5 KB

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