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_H
00015 #define LLVM_BITCODE_H
00016 
00017 #include <iosfwd>
00018 #include <string>
00019 
00020 namespace llvm {
00021   class Module;
00022   class ModuleProvider;
00023   class MemoryBuffer;
00024   class ModulePass;
00025   class BitstreamWriter;
00026   class LLVMContext;
00027   class raw_ostream;
00028   
00029   /// getBitcodeModuleProvider - Read the header of the specified bitcode buffer
00030   /// and prepare for lazy deserialization of function bodies.  If successful,
00031   /// this takes ownership of 'buffer' and returns a non-null pointer.  On
00032   /// error, this returns null, *does not* take ownership of Buffer, and fills
00033   /// in *ErrMsg with an error description if ErrMsg is non-null.
00034   ModuleProvider *getBitcodeModuleProvider(MemoryBuffer *Buffer,
00035                                            LLVMContext& Context,
00036                                            std::string *ErrMsg = 0);
00037 
00038   /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
00039   /// If an error occurs, this returns null and fills in *ErrMsg if it is
00040   /// non-null.  This method *never* takes ownership of Buffer.
00041   Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
00042                            std::string *ErrMsg = 0);
00043 
00044   /// WriteBitcodeToFile - Write the specified module to the specified output
00045   /// stream.
00046   void WriteBitcodeToFile(const Module *M, std::ostream &Out);
00047 
00048   /// WriteBitcodeToFile - Write the specified module to the specified
00049   /// raw output stream.
00050   void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
00051 
00052   /// WriteBitcodeToStream - Write the specified module to the specified
00053   /// raw output stream.
00054   void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
00055 
00056   /// CreateBitcodeWriterPass - Create and return a pass that writes the module
00057   /// to the specified ostream.
00058   ModulePass *CreateBitcodeWriterPass(std::ostream &Str);
00059 
00060   /// createBitcodeWriterPass - Create and return a pass that writes the module
00061   /// to the specified ostream.
00062   ModulePass *createBitcodeWriterPass(raw_ostream &Str);
00063   
00064   
00065   /// isBitcodeWrapper - Return true fi this is a wrapper for LLVM IR bitcode
00066   /// files.
00067   static bool inline isBitcodeWrapper(unsigned char *BufPtr,
00068                                       unsigned char *BufEnd) {
00069     return (BufPtr != BufEnd && BufPtr[0] == 0xDE && BufPtr[1] == 0xC0 && 
00070             BufPtr[2] == 0x17 && BufPtr[3] == 0x0B);
00071   }
00072   
00073   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
00074   /// header for padding or other reasons.  The format of this header is:
00075   ///
00076   /// struct bc_header {
00077   ///   uint32_t Magic;         // 0x0B17C0DE
00078   ///   uint32_t Version;       // Version, currently always 0.
00079   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
00080   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
00081   ///   ... potentially other gunk ...
00082   /// };
00083   /// 
00084   /// This function is called when we find a file with a matching magic number.
00085   /// In this case, skip down to the subsection of the file that is actually a
00086   /// BC file.
00087   static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
00088                                               unsigned char *&BufEnd) {
00089     enum {
00090       KnownHeaderSize = 4*4,  // Size of header we read.
00091       OffsetField = 2*4,      // Offset in bytes to Offset field.
00092       SizeField = 3*4         // Offset in bytes to Size field.
00093     };
00094     
00095     // Must contain the header!
00096     if (BufEnd-BufPtr < KnownHeaderSize) return true;
00097     
00098     unsigned Offset = ( BufPtr[OffsetField  ]        |
00099                        (BufPtr[OffsetField+1] << 8)  |
00100                        (BufPtr[OffsetField+2] << 16) |
00101                        (BufPtr[OffsetField+3] << 24));
00102     unsigned Size   = ( BufPtr[SizeField    ]        |
00103                        (BufPtr[SizeField  +1] << 8)  |
00104                        (BufPtr[SizeField  +2] << 16) |
00105                        (BufPtr[SizeField  +3] << 24));
00106     
00107     // Verify that Offset+Size fits in the file.
00108     if (Offset+Size > unsigned(BufEnd-BufPtr))
00109       return true;
00110     BufPtr += Offset;
00111     BufEnd = BufPtr+Size;
00112     return false;
00113   }
00114 } // End llvm namespace
00115 
00116 #endif