1 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This header defines the BitcodeReader class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef BITCODE_READER_H 15 #define BITCODE_READER_H 16 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/Bitcode/BitstreamReader.h" 19 #include "llvm/Bitcode/LLVMBitCodes.h" 20 #include "llvm/IR/Attributes.h" 21 #include "llvm/IR/GVMaterializer.h" 22 #include "llvm/IR/OperandTraits.h" 23 #include "llvm/IR/Type.h" 24 #include "llvm/IR/ValueHandle.h" 25 #include <vector> 26 27 namespace llvm { 28 class MemoryBuffer; 29 class LLVMContext; 30 } 31 32 namespace llvm_2_7 { 33 34 using namespace llvm; 35 36 //===----------------------------------------------------------------------===// 37 // BitcodeReaderValueList Class 38 //===----------------------------------------------------------------------===// 39 40 class BitcodeReaderValueList { 41 std::vector<WeakVH> ValuePtrs; 42 43 /// ResolveConstants - As we resolve forward-referenced constants, we add 44 /// information about them to this vector. This allows us to resolve them in 45 /// bulk instead of resolving each reference at a time. See the code in 46 /// ResolveConstantForwardRefs for more information about this. 47 /// 48 /// The key of this vector is the placeholder constant, the value is the slot 49 /// number that holds the resolved value. 50 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; 51 ResolveConstantsTy ResolveConstants; 52 LLVMContext &Context; 53 public: BitcodeReaderValueList(LLVMContext & C)54 BitcodeReaderValueList(LLVMContext &C) : Context(C) {} ~BitcodeReaderValueList()55 ~BitcodeReaderValueList() { 56 assert(ResolveConstants.empty() && "Constants not resolved?"); 57 } 58 59 // vector compatibility methods size()60 unsigned size() const { return ValuePtrs.size(); } resize(unsigned N)61 void resize(unsigned N) { ValuePtrs.resize(N); } push_back(Value * V)62 void push_back(Value *V) { 63 ValuePtrs.push_back(V); 64 } 65 clear()66 void clear() { 67 assert(ResolveConstants.empty() && "Constants not resolved?"); 68 ValuePtrs.clear(); 69 } 70 71 Value *operator[](unsigned i) const { 72 assert(i < ValuePtrs.size()); 73 return ValuePtrs[i]; 74 } 75 back()76 Value *back() const { return ValuePtrs.back(); } pop_back()77 void pop_back() { ValuePtrs.pop_back(); } empty()78 bool empty() const { return ValuePtrs.empty(); } shrinkTo(unsigned N)79 void shrinkTo(unsigned N) { 80 assert(N <= size() && "Invalid shrinkTo request!"); 81 ValuePtrs.resize(N); 82 } 83 84 Constant *getConstantFwdRef(unsigned Idx, Type *Ty); 85 Value *getValueFwdRef(unsigned Idx, Type *Ty); 86 87 void AssignValue(Value *V, unsigned Idx); 88 89 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk 90 /// resolves any forward references. 91 void ResolveConstantForwardRefs(); 92 }; 93 94 95 //===----------------------------------------------------------------------===// 96 // BitcodeReaderMDValueList Class 97 //===----------------------------------------------------------------------===// 98 99 class BitcodeReaderMDValueList { 100 std::vector<WeakVH> MDValuePtrs; 101 102 LLVMContext &Context; 103 public: BitcodeReaderMDValueList(LLVMContext & C)104 BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {} 105 106 // vector compatibility methods size()107 unsigned size() const { return MDValuePtrs.size(); } resize(unsigned N)108 void resize(unsigned N) { MDValuePtrs.resize(N); } push_back(Value * V)109 void push_back(Value *V) { MDValuePtrs.push_back(V); } clear()110 void clear() { MDValuePtrs.clear(); } back()111 Value *back() const { return MDValuePtrs.back(); } pop_back()112 void pop_back() { MDValuePtrs.pop_back(); } empty()113 bool empty() const { return MDValuePtrs.empty(); } 114 115 Value *operator[](unsigned i) const { 116 assert(i < MDValuePtrs.size()); 117 return MDValuePtrs[i]; 118 } 119 shrinkTo(unsigned N)120 void shrinkTo(unsigned N) { 121 assert(N <= size() && "Invalid shrinkTo request!"); 122 MDValuePtrs.resize(N); 123 } 124 125 Value *getValueFwdRef(unsigned Idx); 126 void AssignValue(Value *V, unsigned Idx); 127 }; 128 129 class BitcodeReader : public GVMaterializer { 130 LLVMContext &Context; 131 Module *TheModule; 132 MemoryBuffer *Buffer; 133 bool BufferOwned; 134 std::unique_ptr<BitstreamReader> StreamFile; 135 BitstreamCursor Stream; 136 DataStreamer *LazyStreamer; 137 uint64_t NextUnreadBit; 138 bool SeenValueSymbolTable; 139 140 std::vector<Type*> TypeList; 141 BitcodeReaderValueList ValueList; 142 BitcodeReaderMDValueList MDValueList; 143 SmallVector<Instruction *, 64> InstructionList; 144 145 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; 146 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; 147 148 /// MAttributes - The set of attributes by index. Index zero in the 149 /// file is for null, and is thus not represented here. As such all indices 150 /// are off by one. 151 std::vector<AttributeSet> MAttributes; 152 153 /// \brief The set of attribute groups. 154 std::map<unsigned, AttributeSet> MAttributeGroups; 155 156 /// FunctionBBs - While parsing a function body, this is a list of the basic 157 /// blocks for the function. 158 std::vector<BasicBlock*> FunctionBBs; 159 160 // When reading the module header, this list is populated with functions that 161 // have bodies later in the file. 162 std::vector<Function*> FunctionsWithBodies; 163 164 // When intrinsic functions are encountered which require upgrading they are 165 // stored here with their replacement function. 166 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; 167 UpgradedIntrinsicMap UpgradedIntrinsics; 168 169 // Map the bitcode's custom MDKind ID to the Module's MDKind ID. 170 DenseMap<unsigned, unsigned> MDKindMap; 171 172 // Several operations happen after the module header has been read, but 173 // before function bodies are processed. This keeps track of whether 174 // we've done this yet. 175 bool SeenFirstFunctionBody; 176 177 /// DeferredFunctionInfo - When function bodies are initially scanned, this 178 /// map contains info about where to find deferred function body in the 179 /// stream. 180 DenseMap<Function*, uint64_t> DeferredFunctionInfo; 181 182 /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These 183 /// are resolved lazily when functions are loaded. 184 typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy; 185 DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs; 186 187 /// LLVM2_7MetadataDetected - True if metadata produced by LLVM 2.7 or 188 /// earlier was detected, in which case we behave slightly differently, 189 /// for compatibility. 190 /// FIXME: Remove in LLVM 3.0. 191 bool LLVM2_7MetadataDetected; 192 static const std::error_category &BitcodeErrorCategory(); 193 194 public: 195 enum ErrorType { 196 BitcodeStreamInvalidSize, 197 ConflictingMETADATA_KINDRecords, 198 CouldNotFindFunctionInStream, 199 ExpectedConstant, 200 InsufficientFunctionProtos, 201 InvalidBitcodeSignature, 202 InvalidBitcodeWrapperHeader, 203 InvalidConstantReference, 204 InvalidID, // A read identifier is not found in the table it should be in. 205 InvalidInstructionWithNoBB, 206 InvalidRecord, // A read record doesn't have the expected size or structure 207 InvalidTypeForValue, // Type read OK, but is invalid for its use 208 InvalidTYPETable, 209 InvalidType, // We were unable to read a type 210 MalformedBlock, // We are unable to advance in the stream. 211 MalformedGlobalInitializerSet, 212 InvalidMultipleBlocks, // We found multiple blocks of a kind that should 213 // have only one 214 NeverResolvedValueFoundInFunction, 215 InvalidValue // Invalid version, inst number, attr number, etc 216 }; 217 Error(ErrorType E)218 std::error_code Error(ErrorType E) { 219 return std::error_code(E, BitcodeErrorCategory()); 220 } 221 BitcodeReader(MemoryBuffer * buffer,LLVMContext & C)222 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C) 223 : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false), 224 LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false), 225 ValueList(C), MDValueList(C), 226 SeenFirstFunctionBody(false), LLVM2_7MetadataDetected(false) { 227 } ~BitcodeReader()228 ~BitcodeReader() { 229 FreeState(); 230 } 231 232 void FreeState(); 233 234 /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer 235 /// when the reader is destroyed. setBufferOwned(bool Owned)236 void setBufferOwned(bool Owned) { BufferOwned = Owned; } 237 releaseBuffer()238 void releaseBuffer() { 239 Buffer = nullptr; 240 } 241 242 virtual bool isMaterializable(const GlobalValue *GV) const; 243 virtual bool isDematerializable(const GlobalValue *GV) const; 244 virtual std::error_code Materialize(GlobalValue *GV); 245 virtual std::error_code MaterializeModule(Module *M); 246 virtual void Dematerialize(GlobalValue *GV); 247 248 /// @brief Main interface to parsing a bitcode buffer. 249 /// @returns true if an error occurred. 250 std::error_code ParseBitcodeInto(Module *M); 251 252 /// @brief Cheap mechanism to just extract module triple 253 /// @returns true if an error occurred. 254 std::error_code ParseTriple(std::string &Triple); 255 256 static uint64_t decodeSignRotatedValue(uint64_t V); 257 258 private: 259 Type *getTypeByID(unsigned ID); 260 Type *getTypeByIDOrNull(unsigned ID); getFnValueByID(unsigned ID,Type * Ty)261 Value *getFnValueByID(unsigned ID, Type *Ty) { 262 if (Ty && Ty->isMetadataTy()) 263 return MDValueList.getValueFwdRef(ID); 264 return ValueList.getValueFwdRef(ID, Ty); 265 } getBasicBlock(unsigned ID)266 BasicBlock *getBasicBlock(unsigned ID) const { 267 if (ID >= FunctionBBs.size()) return 0; // Invalid ID 268 return FunctionBBs[ID]; 269 } getAttributes(unsigned i)270 AttributeSet getAttributes(unsigned i) const { 271 if (i-1 < MAttributes.size()) 272 return MAttributes[i-1]; 273 return AttributeSet(); 274 } 275 276 /// getValueTypePair - Read a value/type pair out of the specified record from 277 /// slot 'Slot'. Increment Slot past the number of slots used in the record. 278 /// Return true on failure. getValueTypePair(SmallVector<uint64_t,64> & Record,unsigned & Slot,unsigned InstNum,Value * & ResVal)279 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot, 280 unsigned InstNum, Value *&ResVal) { 281 if (Slot == Record.size()) return true; 282 unsigned ValNo = (unsigned)Record[Slot++]; 283 if (ValNo < InstNum) { 284 // If this is not a forward reference, just return the value we already 285 // have. 286 ResVal = getFnValueByID(ValNo, 0); 287 return ResVal == 0; 288 } else if (Slot == Record.size()) { 289 return true; 290 } 291 292 unsigned TypeNo = (unsigned)Record[Slot++]; 293 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); 294 return ResVal == 0; 295 } getValue(SmallVector<uint64_t,64> & Record,unsigned & Slot,Type * Ty,Value * & ResVal)296 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot, 297 Type *Ty, Value *&ResVal) { 298 if (Slot == Record.size()) return true; 299 unsigned ValNo = (unsigned)Record[Slot++]; 300 ResVal = getFnValueByID(ValNo, Ty); 301 return ResVal == 0; 302 } 303 304 305 std::error_code ParseModule(bool Resume); 306 std::error_code ParseAttributeBlock(); 307 std::error_code ParseTypeTable(); 308 std::error_code ParseOldTypeTable(); // FIXME: Remove in LLVM 3.1 309 std::error_code ParseTypeTableBody(); 310 311 std::error_code ParseOldTypeSymbolTable(); // FIXME: Remove in LLVM 3.1 312 std::error_code ParseValueSymbolTable(); 313 std::error_code ParseConstants(); 314 std::error_code RememberAndSkipFunctionBody(); 315 std::error_code ParseFunctionBody(Function *F); 316 std::error_code GlobalCleanup(); 317 std::error_code ResolveGlobalAndAliasInits(); 318 std::error_code ParseMetadata(); 319 std::error_code ParseMetadataAttachment(); 320 std::error_code ParseModuleTriple(std::string &Triple); 321 std::error_code InitStream(); 322 std::error_code InitStreamFromBuffer(); 323 std::error_code InitLazyStream(); 324 }; 325 326 } // End llvm_2_7 namespace 327 328 #endif 329