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