ReaderWriter.h 6.7 KB

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