LLVM API Documentation
00001 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- 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 the BitcodeReader class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef BITCODE_READER_H 00015 #define BITCODE_READER_H 00016 00017 #include "llvm/ModuleProvider.h" 00018 #include "llvm/Attributes.h" 00019 #include "llvm/Type.h" 00020 #include "llvm/OperandTraits.h" 00021 #include "llvm/Bitcode/BitstreamReader.h" 00022 #include "llvm/Bitcode/LLVMBitCodes.h" 00023 #include "llvm/Support/ValueHandle.h" 00024 #include "llvm/ADT/DenseMap.h" 00025 #include <vector> 00026 00027 namespace llvm { 00028 class MemoryBuffer; 00029 class LLVMContext; 00030 00031 //===----------------------------------------------------------------------===// 00032 // BitcodeReaderValueList Class 00033 //===----------------------------------------------------------------------===// 00034 00035 class BitcodeReaderValueList { 00036 std::vector<WeakVH> ValuePtrs; 00037 00038 /// ResolveConstants - As we resolve forward-referenced constants, we add 00039 /// information about them to this vector. This allows us to resolve them in 00040 /// bulk instead of resolving each reference at a time. See the code in 00041 /// ResolveConstantForwardRefs for more information about this. 00042 /// 00043 /// The key of this vector is the placeholder constant, the value is the slot 00044 /// number that holds the resolved value. 00045 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; 00046 ResolveConstantsTy ResolveConstants; 00047 LLVMContext& Context; 00048 public: 00049 BitcodeReaderValueList(LLVMContext& C) : Context(C) {} 00050 ~BitcodeReaderValueList() { 00051 assert(ResolveConstants.empty() && "Constants not resolved?"); 00052 } 00053 00054 // vector compatibility methods 00055 unsigned size() const { return ValuePtrs.size(); } 00056 void resize(unsigned N) { ValuePtrs.resize(N); } 00057 void push_back(Value *V) { 00058 ValuePtrs.push_back(V); 00059 } 00060 00061 void clear() { 00062 assert(ResolveConstants.empty() && "Constants not resolved?"); 00063 ValuePtrs.clear(); 00064 } 00065 00066 Value *operator[](unsigned i) const { 00067 assert(i < ValuePtrs.size()); 00068 return ValuePtrs[i]; 00069 } 00070 00071 Value *back() const { return ValuePtrs.back(); } 00072 void pop_back() { ValuePtrs.pop_back(); } 00073 bool empty() const { return ValuePtrs.empty(); } 00074 void shrinkTo(unsigned N) { 00075 assert(N <= size() && "Invalid shrinkTo request!"); 00076 ValuePtrs.resize(N); 00077 } 00078 00079 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty); 00080 Value *getValueFwdRef(unsigned Idx, const Type *Ty); 00081 00082 void AssignValue(Value *V, unsigned Idx); 00083 00084 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk 00085 /// resolves any forward references. 00086 void ResolveConstantForwardRefs(); 00087 }; 00088 00089 00090 //===----------------------------------------------------------------------===// 00091 // BitcodeReaderMDValueList Class 00092 //===----------------------------------------------------------------------===// 00093 00094 class BitcodeReaderMDValueList { 00095 std::vector<WeakVH> MDValuePtrs; 00096 00097 LLVMContext& Context; 00098 public: 00099 BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {} 00100 00101 // vector compatibility methods 00102 unsigned size() const { return MDValuePtrs.size(); } 00103 void resize(unsigned N) { MDValuePtrs.resize(N); } 00104 void push_back(Value *V) { MDValuePtrs.push_back(V); } 00105 void clear() { MDValuePtrs.clear(); } 00106 Value *back() const { return MDValuePtrs.back(); } 00107 void pop_back() { MDValuePtrs.pop_back(); } 00108 bool empty() const { return MDValuePtrs.empty(); } 00109 00110 Value *operator[](unsigned i) const { 00111 assert(i < MDValuePtrs.size()); 00112 return MDValuePtrs[i]; 00113 } 00114 00115 void shrinkTo(unsigned N) { 00116 assert(N <= size() && "Invalid shrinkTo request!"); 00117 MDValuePtrs.resize(N); 00118 } 00119 00120 Value *getValueFwdRef(unsigned Idx); 00121 void AssignValue(Value *V, unsigned Idx); 00122 }; 00123 00124 class BitcodeReader : public ModuleProvider { 00125 LLVMContext& Context; 00126 MemoryBuffer *Buffer; 00127 BitstreamReader StreamFile; 00128 BitstreamCursor Stream; 00129 00130 const char *ErrorString; 00131 00132 std::vector<PATypeHolder> TypeList; 00133 BitcodeReaderValueList ValueList; 00134 BitcodeReaderMDValueList MDValueList; 00135 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; 00136 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; 00137 00138 /// MAttributes - The set of attributes by index. Index zero in the 00139 /// file is for null, and is thus not represented here. As such all indices 00140 /// are off by one. 00141 std::vector<AttrListPtr> MAttributes; 00142 00143 /// FunctionBBs - While parsing a function body, this is a list of the basic 00144 /// blocks for the function. 00145 std::vector<BasicBlock*> FunctionBBs; 00146 00147 // When reading the module header, this list is populated with functions that 00148 // have bodies later in the file. 00149 std::vector<Function*> FunctionsWithBodies; 00150 00151 // When intrinsic functions are encountered which require upgrading they are 00152 // stored here with their replacement function. 00153 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; 00154 UpgradedIntrinsicMap UpgradedIntrinsics; 00155 00156 // After the module header has been read, the FunctionsWithBodies list is 00157 // reversed. This keeps track of whether we've done this yet. 00158 bool HasReversedFunctionsWithBodies; 00159 00160 /// DeferredFunctionInfo - When function bodies are initially scanned, this 00161 /// map contains info about where to find deferred function body (in the 00162 /// stream) and what linkage the original function had. 00163 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo; 00164 public: 00165 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C) 00166 : Context(C), Buffer(buffer), ErrorString(0), ValueList(C), MDValueList(C) { 00167 HasReversedFunctionsWithBodies = false; 00168 } 00169 ~BitcodeReader() { 00170 FreeState(); 00171 } 00172 00173 void FreeState(); 00174 00175 /// releaseMemoryBuffer - This causes the reader to completely forget about 00176 /// the memory buffer it contains, which prevents the buffer from being 00177 /// destroyed when it is deleted. 00178 void releaseMemoryBuffer() { 00179 Buffer = 0; 00180 } 00181 00182 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0); 00183 virtual Module *materializeModule(std::string *ErrInfo = 0); 00184 virtual void dematerializeFunction(Function *F); 00185 virtual Module *releaseModule(std::string *ErrInfo = 0); 00186 00187 bool Error(const char *Str) { 00188 ErrorString = Str; 00189 return true; 00190 } 00191 const char *getErrorString() const { return ErrorString; } 00192 00193 /// @brief Main interface to parsing a bitcode buffer. 00194 /// @returns true if an error occurred. 00195 bool ParseBitcode(); 00196 private: 00197 const Type *getTypeByID(unsigned ID, bool isTypeTable = false); 00198 Value *getFnValueByID(unsigned ID, const Type *Ty) { 00199 if (Ty == Type::getMetadataTy(Context)) 00200 return MDValueList.getValueFwdRef(ID); 00201 else 00202 return ValueList.getValueFwdRef(ID, Ty); 00203 } 00204 BasicBlock *getBasicBlock(unsigned ID) const { 00205 if (ID >= FunctionBBs.size()) return 0; // Invalid ID 00206 return FunctionBBs[ID]; 00207 } 00208 AttrListPtr getAttributes(unsigned i) const { 00209 if (i-1 < MAttributes.size()) 00210 return MAttributes[i-1]; 00211 return AttrListPtr(); 00212 } 00213 00214 /// getValueTypePair - Read a value/type pair out of the specified record from 00215 /// slot 'Slot'. Increment Slot past the number of slots used in the record. 00216 /// Return true on failure. 00217 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot, 00218 unsigned InstNum, Value *&ResVal) { 00219 if (Slot == Record.size()) return true; 00220 unsigned ValNo = (unsigned)Record[Slot++]; 00221 if (ValNo < InstNum) { 00222 // If this is not a forward reference, just return the value we already 00223 // have. 00224 ResVal = getFnValueByID(ValNo, 0); 00225 return ResVal == 0; 00226 } else if (Slot == Record.size()) { 00227 return true; 00228 } 00229 00230 unsigned TypeNo = (unsigned)Record[Slot++]; 00231 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); 00232 return ResVal == 0; 00233 } 00234 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot, 00235 const Type *Ty, Value *&ResVal) { 00236 if (Slot == Record.size()) return true; 00237 unsigned ValNo = (unsigned)Record[Slot++]; 00238 ResVal = getFnValueByID(ValNo, Ty); 00239 return ResVal == 0; 00240 } 00241 00242 00243 bool ParseModule(const std::string &ModuleID); 00244 bool ParseAttributeBlock(); 00245 bool ParseTypeTable(); 00246 bool ParseTypeSymbolTable(); 00247 bool ParseValueSymbolTable(); 00248 bool ParseConstants(); 00249 bool RememberAndSkipFunctionBody(); 00250 bool ParseFunctionBody(Function *F); 00251 bool ResolveGlobalAndAliasInits(); 00252 bool ParseMetadata(); 00253 }; 00254 00255 } // End llvm namespace 00256 00257 #endif