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