LLVM  mainline
ReaderWriter.h
Go to the documentation of this file.
00001 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This header defines interfaces to read and write LLVM bitcode files/streams.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_BITCODE_READERWRITER_H
00015 #define LLVM_BITCODE_READERWRITER_H
00016 
00017 #include "llvm/IR/DiagnosticInfo.h"
00018 #include "llvm/Support/ErrorOr.h"
00019 #include "llvm/Support/MemoryBuffer.h"
00020 #include <memory>
00021 #include <string>
00022 
00023 namespace llvm {
00024   class BitstreamWriter;
00025   class DataStreamer;
00026   class LLVMContext;
00027   class Module;
00028   class ModulePass;
00029   class raw_ostream;
00030 
00031   /// Read the header of the specified bitcode buffer and prepare for lazy
00032   /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
00033   /// lazily load metadata as well. If successful, this moves Buffer. On
00034   /// error, this *does not* move Buffer.
00035   ErrorOr<Module *>
00036   getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
00037                        LLVMContext &Context,
00038                        DiagnosticHandlerFunction DiagnosticHandler = nullptr,
00039                        bool ShouldLazyLoadMetadata = false);
00040 
00041   /// Read the header of the specified stream and prepare for lazy
00042   /// deserialization and streaming of function bodies.
00043   ErrorOr<std::unique_ptr<Module>> getStreamedBitcodeModule(
00044       StringRef Name, DataStreamer *Streamer, LLVMContext &Context,
00045       DiagnosticHandlerFunction DiagnosticHandler = nullptr);
00046 
00047   /// Read the header of the specified bitcode buffer and extract just the
00048   /// triple information. If successful, this returns a string. On error, this
00049   /// returns "".
00050   std::string
00051   getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
00052                          DiagnosticHandlerFunction DiagnosticHandler = nullptr);
00053 
00054   /// Read the specified bitcode file, returning the module.
00055   ErrorOr<Module *>
00056   parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
00057                    DiagnosticHandlerFunction DiagnosticHandler = nullptr);
00058 
00059   /// \brief Write the specified module to the specified raw output stream.
00060   ///
00061   /// For streams where it matters, the given stream should be in "binary"
00062   /// mode.
00063   ///
00064   /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
00065   /// Value in \c M.  These will be reconstructed exactly when \a M is
00066   /// deserialized.
00067   void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
00068                           bool ShouldPreserveUseListOrder = false);
00069 
00070   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
00071   /// for an LLVM IR bitcode wrapper.
00072   ///
00073   inline bool isBitcodeWrapper(const unsigned char *BufPtr,
00074                                const unsigned char *BufEnd) {
00075     // See if you can find the hidden message in the magic bytes :-).
00076     // (Hint: it's a little-endian encoding.)
00077     return BufPtr != BufEnd &&
00078            BufPtr[0] == 0xDE &&
00079            BufPtr[1] == 0xC0 &&
00080            BufPtr[2] == 0x17 &&
00081            BufPtr[3] == 0x0B;
00082   }
00083 
00084   /// isRawBitcode - Return true if the given bytes are the magic bytes for
00085   /// raw LLVM IR bitcode (without a wrapper).
00086   ///
00087   inline bool isRawBitcode(const unsigned char *BufPtr,
00088                            const unsigned char *BufEnd) {
00089     // These bytes sort of have a hidden message, but it's not in
00090     // little-endian this time, and it's a little redundant.
00091     return BufPtr != BufEnd &&
00092            BufPtr[0] == 'B' &&
00093            BufPtr[1] == 'C' &&
00094            BufPtr[2] == 0xc0 &&
00095            BufPtr[3] == 0xde;
00096   }
00097 
00098   /// isBitcode - Return true if the given bytes are the magic bytes for
00099   /// LLVM IR bitcode, either with or without a wrapper.
00100   ///
00101   inline bool isBitcode(const unsigned char *BufPtr,
00102                         const unsigned char *BufEnd) {
00103     return isBitcodeWrapper(BufPtr, BufEnd) ||
00104            isRawBitcode(BufPtr, BufEnd);
00105   }
00106 
00107   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
00108   /// header for padding or other reasons.  The format of this header is:
00109   ///
00110   /// struct bc_header {
00111   ///   uint32_t Magic;         // 0x0B17C0DE
00112   ///   uint32_t Version;       // Version, currently always 0.
00113   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
00114   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
00115   ///   ... potentially other gunk ...
00116   /// };
00117   ///
00118   /// This function is called when we find a file with a matching magic number.
00119   /// In this case, skip down to the subsection of the file that is actually a
00120   /// BC file.
00121   /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
00122   /// contain the whole bitcode file.
00123   inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
00124                                        const unsigned char *&BufEnd,
00125                                        bool VerifyBufferSize) {
00126     enum {
00127       KnownHeaderSize = 4*4,  // Size of header we read.
00128       OffsetField = 2*4,      // Offset in bytes to Offset field.
00129       SizeField = 3*4         // Offset in bytes to Size field.
00130     };
00131 
00132     // Must contain the header!
00133     if (BufEnd-BufPtr < KnownHeaderSize) return true;
00134 
00135     unsigned Offset = ( BufPtr[OffsetField  ]        |
00136                        (BufPtr[OffsetField+1] << 8)  |
00137                        (BufPtr[OffsetField+2] << 16) |
00138                        (BufPtr[OffsetField+3] << 24));
00139     unsigned Size   = ( BufPtr[SizeField    ]        |
00140                        (BufPtr[SizeField  +1] << 8)  |
00141                        (BufPtr[SizeField  +2] << 16) |
00142                        (BufPtr[SizeField  +3] << 24));
00143 
00144     // Verify that Offset+Size fits in the file.
00145     if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
00146       return true;
00147     BufPtr += Offset;
00148     BufEnd = BufPtr+Size;
00149     return false;
00150   }
00151 
00152   const std::error_category &BitcodeErrorCategory();
00153   enum class BitcodeError { InvalidBitcodeSignature, CorruptedBitcode };
00154   inline std::error_code make_error_code(BitcodeError E) {
00155     return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
00156   }
00157 
00158   class BitcodeDiagnosticInfo : public DiagnosticInfo {
00159     const Twine &Msg;
00160     std::error_code EC;
00161 
00162   public:
00163     BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity,
00164                           const Twine &Msg);
00165     void print(DiagnosticPrinter &DP) const override;
00166     std::error_code getError() const { return EC; };
00167 
00168     static bool classof(const DiagnosticInfo *DI) {
00169       return DI->getKind() == DK_Bitcode;
00170     }
00171   };
00172 
00173 } // End llvm namespace
00174 
00175 namespace std {
00176 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};
00177 }
00178 
00179 #endif