BitcodeReader.h revision e54abc90fe9942ef3902040a7ac475ce0c369dc9
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/ModuleProvider.h" 18#include "llvm/Attributes.h" 19#include "llvm/Type.h" 20#include "llvm/OperandTraits.h" 21#include "llvm/Bitcode/BitstreamReader.h" 22#include "llvm/Bitcode/LLVMBitCodes.h" 23#include "llvm/Support/ValueHandle.h" 24#include "llvm/ADT/DenseMap.h" 25#include <vector> 26 27namespace llvm { 28 class MemoryBuffer; 29 class LLVMContext; 30 31//===----------------------------------------------------------------------===// 32// BitcodeReaderValueList Class 33//===----------------------------------------------------------------------===// 34 35class BitcodeReaderValueList { 36 std::vector<WeakVH> ValuePtrs; 37 38 /// ResolveConstants - As we resolve forward-referenced constants, we add 39 /// information about them to this vector. This allows us to resolve them in 40 /// bulk instead of resolving each reference at a time. See the code in 41 /// ResolveConstantForwardRefs for more information about this. 42 /// 43 /// The key of this vector is the placeholder constant, the value is the slot 44 /// number that holds the resolved value. 45 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; 46 ResolveConstantsTy ResolveConstants; 47 LLVMContext& Context; 48public: 49 BitcodeReaderValueList(LLVMContext& C) : Context(C) {} 50 ~BitcodeReaderValueList() { 51 assert(ResolveConstants.empty() && "Constants not resolved?"); 52 } 53 54 // vector compatibility methods 55 unsigned size() const { return ValuePtrs.size(); } 56 void resize(unsigned N) { ValuePtrs.resize(N); } 57 void push_back(Value *V) { 58 ValuePtrs.push_back(V); 59 } 60 61 void clear() { 62 assert(ResolveConstants.empty() && "Constants not resolved?"); 63 ValuePtrs.clear(); 64 } 65 66 Value *operator[](unsigned i) const { 67 assert(i < ValuePtrs.size()); 68 return ValuePtrs[i]; 69 } 70 71 Value *back() const { return ValuePtrs.back(); } 72 void pop_back() { ValuePtrs.pop_back(); } 73 bool empty() const { return ValuePtrs.empty(); } 74 void shrinkTo(unsigned N) { 75 assert(N <= size() && "Invalid shrinkTo request!"); 76 ValuePtrs.resize(N); 77 } 78 79 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty); 80 Value *getValueFwdRef(unsigned Idx, const Type *Ty); 81 82 void AssignValue(Value *V, unsigned Idx); 83 84 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk 85 /// resolves any forward references. 86 void ResolveConstantForwardRefs(); 87}; 88 89class BitcodeReader : public ModuleProvider { 90 LLVMContext& Context; 91 MemoryBuffer *Buffer; 92 BitstreamReader StreamFile; 93 BitstreamCursor Stream; 94 95 const char *ErrorString; 96 97 std::vector<PATypeHolder> TypeList; 98 BitcodeReaderValueList ValueList; 99 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; 100 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; 101 102 /// MAttributes - The set of attributes by index. Index zero in the 103 /// file is for null, and is thus not represented here. As such all indices 104 /// are off by one. 105 std::vector<AttrListPtr> MAttributes; 106 107 /// FunctionBBs - While parsing a function body, this is a list of the basic 108 /// blocks for the function. 109 std::vector<BasicBlock*> FunctionBBs; 110 111 // When reading the module header, this list is populated with functions that 112 // have bodies later in the file. 113 std::vector<Function*> FunctionsWithBodies; 114 115 // When intrinsic functions are encountered which require upgrading they are 116 // stored here with their replacement function. 117 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; 118 UpgradedIntrinsicMap UpgradedIntrinsics; 119 120 // After the module header has been read, the FunctionsWithBodies list is 121 // reversed. This keeps track of whether we've done this yet. 122 bool HasReversedFunctionsWithBodies; 123 124 /// DeferredFunctionInfo - When function bodies are initially scanned, this 125 /// map contains info about where to find deferred function body (in the 126 /// stream) and what linkage the original function had. 127 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo; 128public: 129 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C) 130 : Context(C), Buffer(buffer), ErrorString(0), ValueList(C) { 131 HasReversedFunctionsWithBodies = false; 132 } 133 ~BitcodeReader() { 134 FreeState(); 135 } 136 137 void FreeState(); 138 139 /// releaseMemoryBuffer - This causes the reader to completely forget about 140 /// the memory buffer it contains, which prevents the buffer from being 141 /// destroyed when it is deleted. 142 void releaseMemoryBuffer() { 143 Buffer = 0; 144 } 145 146 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0); 147 virtual Module *materializeModule(std::string *ErrInfo = 0); 148 virtual void dematerializeFunction(Function *F); 149 virtual Module *releaseModule(std::string *ErrInfo = 0); 150 151 bool Error(const char *Str) { 152 ErrorString = Str; 153 return true; 154 } 155 const char *getErrorString() const { return ErrorString; } 156 157 /// @brief Main interface to parsing a bitcode buffer. 158 /// @returns true if an error occurred. 159 bool ParseBitcode(); 160private: 161 const Type *getTypeByID(unsigned ID, bool isTypeTable = false); 162 Value *getFnValueByID(unsigned ID, const Type *Ty) { 163 return ValueList.getValueFwdRef(ID, Ty); 164 } 165 BasicBlock *getBasicBlock(unsigned ID) const { 166 if (ID >= FunctionBBs.size()) return 0; // Invalid ID 167 return FunctionBBs[ID]; 168 } 169 AttrListPtr getAttributes(unsigned i) const { 170 if (i-1 < MAttributes.size()) 171 return MAttributes[i-1]; 172 return AttrListPtr(); 173 } 174 175 /// getValueTypePair - Read a value/type pair out of the specified record from 176 /// slot 'Slot'. Increment Slot past the number of slots used in the record. 177 /// Return true on failure. 178 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot, 179 unsigned InstNum, Value *&ResVal) { 180 if (Slot == Record.size()) return true; 181 unsigned ValNo = (unsigned)Record[Slot++]; 182 if (ValNo < InstNum) { 183 // If this is not a forward reference, just return the value we already 184 // have. 185 ResVal = getFnValueByID(ValNo, 0); 186 return ResVal == 0; 187 } else if (Slot == Record.size()) { 188 return true; 189 } 190 191 unsigned TypeNo = (unsigned)Record[Slot++]; 192 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); 193 return ResVal == 0; 194 } 195 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot, 196 const Type *Ty, Value *&ResVal) { 197 if (Slot == Record.size()) return true; 198 unsigned ValNo = (unsigned)Record[Slot++]; 199 ResVal = getFnValueByID(ValNo, Ty); 200 return ResVal == 0; 201 } 202 203 204 bool ParseModule(const std::string &ModuleID); 205 bool ParseAttributeBlock(); 206 bool ParseTypeTable(); 207 bool ParseTypeSymbolTable(); 208 bool ParseValueSymbolTable(); 209 bool ParseConstants(); 210 bool RememberAndSkipFunctionBody(); 211 bool ParseFunctionBody(Function *F); 212 bool ResolveGlobalAndAliasInits(); 213 bool ParseMetadata(); 214}; 215 216} // End llvm namespace 217 218#endif 219