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/Support/ErrorOr.h"
00018 #include <string>
00019 
00020 namespace llvm {
00021   class BitstreamWriter;
00022   class MemoryBuffer;
00023   class DataStreamer;
00024   class LLVMContext;
00025   class Module;
00026   class ModulePass;
00027   class raw_ostream;
00028 
00029   /// Read the header of the specified bitcode buffer and prepare for lazy
00030   /// deserialization of function bodies.  If successful, this takes ownership
00031   /// of 'buffer. On error, this *does not* take ownership of Buffer.
00032   ErrorOr<Module *> getLazyBitcodeModule(MemoryBuffer *Buffer,
00033                                          LLVMContext &Context);
00034 
00035   /// getStreamedBitcodeModule - Read the header of the specified stream
00036   /// and prepare for lazy deserialization and streaming of function bodies.
00037   /// On error, this returns null, and fills in *ErrMsg with an error
00038   /// description if ErrMsg is non-null.
00039   Module *getStreamedBitcodeModule(const std::string &name,
00040                                    DataStreamer *streamer,
00041                                    LLVMContext &Context,
00042                                    std::string *ErrMsg = nullptr);
00043 
00044   /// getBitcodeTargetTriple - Read the header of the specified bitcode
00045   /// buffer and extract just the triple information. If successful,
00046   /// this returns a string and *does not* take ownership
00047   /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
00048   /// if ErrMsg is non-null.
00049   std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
00050                                      LLVMContext &Context,
00051                                      std::string *ErrMsg = nullptr);
00052 
00053   /// Read the specified bitcode file, returning the module.
00054   /// This method *never* takes ownership of Buffer.
00055   ErrorOr<Module *> parseBitcodeFile(MemoryBuffer *Buffer,
00056                                      LLVMContext &Context);
00057 
00058   /// WriteBitcodeToFile - Write the specified module to the specified
00059   /// raw output stream.  For streams where it matters, the given stream
00060   /// should be in "binary" mode.
00061   void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
00062 
00063 
00064   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
00065   /// for an LLVM IR bitcode wrapper.
00066   ///
00067   inline bool isBitcodeWrapper(const unsigned char *BufPtr,
00068                                const unsigned char *BufEnd) {
00069     // See if you can find the hidden message in the magic bytes :-).
00070     // (Hint: it's a little-endian encoding.)
00071     return BufPtr != BufEnd &&
00072            BufPtr[0] == 0xDE &&
00073            BufPtr[1] == 0xC0 &&
00074            BufPtr[2] == 0x17 &&
00075            BufPtr[3] == 0x0B;
00076   }
00077 
00078   /// isRawBitcode - Return true if the given bytes are the magic bytes for
00079   /// raw LLVM IR bitcode (without a wrapper).
00080   ///
00081   inline bool isRawBitcode(const unsigned char *BufPtr,
00082                            const unsigned char *BufEnd) {
00083     // These bytes sort of have a hidden message, but it's not in
00084     // little-endian this time, and it's a little redundant.
00085     return BufPtr != BufEnd &&
00086            BufPtr[0] == 'B' &&
00087            BufPtr[1] == 'C' &&
00088            BufPtr[2] == 0xc0 &&
00089            BufPtr[3] == 0xde;
00090   }
00091 
00092   /// isBitcode - Return true if the given bytes are the magic bytes for
00093   /// LLVM IR bitcode, either with or without a wrapper.
00094   ///
00095   inline bool isBitcode(const unsigned char *BufPtr,
00096                         const unsigned char *BufEnd) {
00097     return isBitcodeWrapper(BufPtr, BufEnd) ||
00098            isRawBitcode(BufPtr, BufEnd);
00099   }
00100 
00101   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
00102   /// header for padding or other reasons.  The format of this header is:
00103   ///
00104   /// struct bc_header {
00105   ///   uint32_t Magic;         // 0x0B17C0DE
00106   ///   uint32_t Version;       // Version, currently always 0.
00107   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
00108   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
00109   ///   ... potentially other gunk ...
00110   /// };
00111   ///
00112   /// This function is called when we find a file with a matching magic number.
00113   /// In this case, skip down to the subsection of the file that is actually a
00114   /// BC file.
00115   /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
00116   /// contain the whole bitcode file.
00117   inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
00118                                        const unsigned char *&BufEnd,
00119                                        bool VerifyBufferSize) {
00120     enum {
00121       KnownHeaderSize = 4*4,  // Size of header we read.
00122       OffsetField = 2*4,      // Offset in bytes to Offset field.
00123       SizeField = 3*4         // Offset in bytes to Size field.
00124     };
00125 
00126     // Must contain the header!
00127     if (BufEnd-BufPtr < KnownHeaderSize) return true;
00128 
00129     unsigned Offset = ( BufPtr[OffsetField  ]        |
00130                        (BufPtr[OffsetField+1] << 8)  |
00131                        (BufPtr[OffsetField+2] << 16) |
00132                        (BufPtr[OffsetField+3] << 24));
00133     unsigned Size   = ( BufPtr[SizeField    ]        |
00134                        (BufPtr[SizeField  +1] << 8)  |
00135                        (BufPtr[SizeField  +2] << 16) |
00136                        (BufPtr[SizeField  +3] << 24));
00137 
00138     // Verify that Offset+Size fits in the file.
00139     if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
00140       return true;
00141     BufPtr += Offset;
00142     BufEnd = BufPtr+Size;
00143     return false;
00144   }
00145 } // End llvm namespace
00146 
00147 #endif