LLVM API Documentation

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