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