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 "llvm/Support/ErrorOr.h" 00018 #include "llvm/Support/MemoryBuffer.h" 00019 #include <memory> 00020 #include <string> 00021 00022 namespace llvm { 00023 class BitstreamWriter; 00024 class DataStreamer; 00025 class LLVMContext; 00026 class Module; 00027 class ModulePass; 00028 class raw_ostream; 00029 00030 /// Read the header of the specified bitcode buffer and prepare for lazy 00031 /// deserialization of function bodies. If successful, this moves Buffer. On 00032 /// error, this *does not* move Buffer. 00033 ErrorOr<Module *> getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, 00034 LLVMContext &Context); 00035 00036 /// getStreamedBitcodeModule - Read the header of the specified stream 00037 /// and prepare for lazy deserialization and streaming of function bodies. 00038 /// On error, this returns null, and fills in *ErrMsg with an error 00039 /// description if ErrMsg is non-null. 00040 Module *getStreamedBitcodeModule(const std::string &name, 00041 DataStreamer *streamer, 00042 LLVMContext &Context, 00043 std::string *ErrMsg = 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 getBitcodeTargetTriple(MemoryBufferRef Buffer, 00049 LLVMContext &Context); 00050 00051 /// Read the specified bitcode file, returning the module. 00052 ErrorOr<Module *> parseBitcodeFile(MemoryBufferRef 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 00143 const std::error_category &BitcodeErrorCategory(); 00144 enum class BitcodeError { 00145 ConflictingMETADATA_KINDRecords, 00146 CouldNotFindFunctionInStream, 00147 ExpectedConstant, 00148 InsufficientFunctionProtos, 00149 InvalidBitcodeSignature, 00150 InvalidBitcodeWrapperHeader, 00151 InvalidConstantReference, 00152 InvalidID, // A read identifier is not found in the table it should be in. 00153 InvalidInstructionWithNoBB, 00154 InvalidRecord, // A read record doesn't have the expected size or structure 00155 InvalidTypeForValue, // Type read OK, but is invalid for its use 00156 InvalidTYPETable, 00157 InvalidType, // We were unable to read a type 00158 MalformedBlock, // We are unable to advance in the stream. 00159 MalformedGlobalInitializerSet, 00160 InvalidMultipleBlocks, // We found multiple blocks of a kind that should 00161 // have only one 00162 NeverResolvedValueFoundInFunction, 00163 NeverResolvedFunctionFromBlockAddress, 00164 InvalidValue // Invalid version, inst number, attr number, etc 00165 }; 00166 inline std::error_code make_error_code(BitcodeError E) { 00167 return std::error_code(static_cast<int>(E), BitcodeErrorCategory()); 00168 } 00169 00170 } // End llvm namespace 00171 00172 namespace std { 00173 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {}; 00174 } 00175 00176 #endif