Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
6.2 KiB

  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 <string>
  16. namespace llvm {
  17. class BitstreamWriter;
  18. class MemoryBuffer;
  19. class DataStreamer;
  20. class LLVMContext;
  21. class Module;
  22. class ModulePass;
  23. class raw_ostream;
  24. /// getLazyBitcodeModule - Read the header of the specified bitcode buffer
  25. /// and prepare for lazy deserialization of function bodies. If successful,
  26. /// this takes ownership of 'buffer' and returns a non-null pointer. On
  27. /// error, this returns null, *does not* take ownership of Buffer, and fills
  28. /// in *ErrMsg with an error description if ErrMsg is non-null.
  29. Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
  30. LLVMContext &Context,
  31. std::string *ErrMsg = 0);
  32. /// getStreamedBitcodeModule - Read the header of the specified stream
  33. /// and prepare for lazy deserialization and streaming of function bodies.
  34. /// On error, this returns null, and fills in *ErrMsg with an error
  35. /// description if ErrMsg is non-null.
  36. Module *getStreamedBitcodeModule(const std::string &name,
  37. DataStreamer *streamer,
  38. LLVMContext &Context,
  39. std::string *ErrMsg = 0);
  40. /// getBitcodeTargetTriple - Read the header of the specified bitcode
  41. /// buffer and extract just the triple information. If successful,
  42. /// this returns a string and *does not* take ownership
  43. /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
  44. /// if ErrMsg is non-null.
  45. std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
  46. LLVMContext &Context,
  47. std::string *ErrMsg = 0);
  48. /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
  49. /// If an error occurs, this returns null and fills in *ErrMsg if it is
  50. /// non-null. This method *never* takes ownership of Buffer.
  51. Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext &Context,
  52. std::string *ErrMsg = 0);
  53. /// WriteBitcodeToFile - Write the specified module to the specified
  54. /// raw output stream. For streams where it matters, the given stream
  55. /// should be in "binary" mode.
  56. void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
  57. /// createBitcodeWriterPass - Create and return a pass that writes the module
  58. /// to the specified ostream.
  59. ModulePass *createBitcodeWriterPass(raw_ostream &Str);
  60. /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
  61. /// for an LLVM IR bitcode wrapper.
  62. ///
  63. inline bool isBitcodeWrapper(const unsigned char *BufPtr,
  64. const unsigned char *BufEnd) {
  65. // See if you can find the hidden message in the magic bytes :-).
  66. // (Hint: it's a little-endian encoding.)
  67. return BufPtr != BufEnd &&
  68. BufPtr[0] == 0xDE &&
  69. BufPtr[1] == 0xC0 &&
  70. BufPtr[2] == 0x17 &&
  71. BufPtr[3] == 0x0B;
  72. }
  73. /// isRawBitcode - Return true if the given bytes are the magic bytes for
  74. /// raw LLVM IR bitcode (without a wrapper).
  75. ///
  76. inline bool isRawBitcode(const unsigned char *BufPtr,
  77. const unsigned char *BufEnd) {
  78. // These bytes sort of have a hidden message, but it's not in
  79. // little-endian this time, and it's a little redundant.
  80. return BufPtr != BufEnd &&
  81. BufPtr[0] == 'B' &&
  82. BufPtr[1] == 'C' &&
  83. BufPtr[2] == 0xc0 &&
  84. BufPtr[3] == 0xde;
  85. }
  86. /// isBitcode - Return true if the given bytes are the magic bytes for
  87. /// LLVM IR bitcode, either with or without a wrapper.
  88. ///
  89. inline bool isBitcode(const unsigned char *BufPtr,
  90. const unsigned char *BufEnd) {
  91. return isBitcodeWrapper(BufPtr, BufEnd) ||
  92. isRawBitcode(BufPtr, BufEnd);
  93. }
  94. /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
  95. /// header for padding or other reasons. The format of this header is:
  96. ///
  97. /// struct bc_header {
  98. /// uint32_t Magic; // 0x0B17C0DE
  99. /// uint32_t Version; // Version, currently always 0.
  100. /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
  101. /// uint32_t BitcodeSize; // Size of traditional bitcode file.
  102. /// ... potentially other gunk ...
  103. /// };
  104. ///
  105. /// This function is called when we find a file with a matching magic number.
  106. /// In this case, skip down to the subsection of the file that is actually a
  107. /// BC file.
  108. /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
  109. /// contain the whole bitcode file.
  110. inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
  111. const unsigned char *&BufEnd,
  112. bool VerifyBufferSize) {
  113. enum {
  114. KnownHeaderSize = 4*4, // Size of header we read.
  115. OffsetField = 2*4, // Offset in bytes to Offset field.
  116. SizeField = 3*4 // Offset in bytes to Size field.
  117. };
  118. // Must contain the header!
  119. if (BufEnd-BufPtr < KnownHeaderSize) return true;
  120. unsigned Offset = ( BufPtr[OffsetField ] |
  121. (BufPtr[OffsetField+1] << 8) |
  122. (BufPtr[OffsetField+2] << 16) |
  123. (BufPtr[OffsetField+3] << 24));
  124. unsigned Size = ( BufPtr[SizeField ] |
  125. (BufPtr[SizeField +1] << 8) |
  126. (BufPtr[SizeField +2] << 16) |
  127. (BufPtr[SizeField +3] << 24));
  128. // Verify that Offset+Size fits in the file.
  129. if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
  130. return true;
  131. BufPtr += Offset;
  132. BufEnd = BufPtr+Size;
  133. return false;
  134. }
  135. } // End llvm namespace
  136. #endif