1 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines the BitcodeReader class.
12 //===----------------------------------------------------------------------===//
14 #ifndef BITCODE_READER_H
15 #define BITCODE_READER_H
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"
30 //===----------------------------------------------------------------------===//
31 // BitcodeReaderValueList Class
32 //===----------------------------------------------------------------------===//
34 class BitcodeReaderValueList {
35 std::vector<WeakVH> ValuePtrs;
37 /// ResolveConstants - As we resolve forward-referenced constants, we add
38 /// information about them to this vector. This allows us to resolve them in
39 /// bulk instead of resolving each reference at a time. See the code in
40 /// ResolveConstantForwardRefs for more information about this.
42 /// The key of this vector is the placeholder constant, the value is the slot
43 /// number that holds the resolved value.
44 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
45 ResolveConstantsTy ResolveConstants;
47 BitcodeReaderValueList() {}
48 ~BitcodeReaderValueList() {
49 assert(ResolveConstants.empty() && "Constants not resolved?");
52 // vector compatibility methods
53 unsigned size() const { return ValuePtrs.size(); }
54 void resize(unsigned N) { ValuePtrs.resize(N); }
55 void push_back(Value *V) {
56 ValuePtrs.push_back(V);
60 assert(ResolveConstants.empty() && "Constants not resolved?");
64 Value *operator[](unsigned i) const {
65 assert(i < ValuePtrs.size());
69 Value *back() const { return ValuePtrs.back(); }
70 void pop_back() { ValuePtrs.pop_back(); }
71 bool empty() const { return ValuePtrs.empty(); }
72 void shrinkTo(unsigned N) {
73 assert(N <= size() && "Invalid shrinkTo request!");
77 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
78 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
80 void AssignValue(Value *V, unsigned Idx);
82 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
83 /// resolves any forward references.
84 void ResolveConstantForwardRefs();
87 class BitcodeReader : public ModuleProvider {
89 BitstreamReader Stream;
91 const char *ErrorString;
93 std::vector<PATypeHolder> TypeList;
94 BitcodeReaderValueList ValueList;
95 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
96 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
98 /// MAttributes - The set of attributes by index. Index zero in the
99 /// file is for null, and is thus not represented here. As such all indices
101 std::vector<AttrListPtr> MAttributes;
103 /// FunctionBBs - While parsing a function body, this is a list of the basic
104 /// blocks for the function.
105 std::vector<BasicBlock*> FunctionBBs;
107 // When reading the module header, this list is populated with functions that
108 // have bodies later in the file.
109 std::vector<Function*> FunctionsWithBodies;
111 // When intrinsic functions are encountered which require upgrading they are
112 // stored here with their replacement function.
113 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
114 UpgradedIntrinsicMap UpgradedIntrinsics;
116 // After the module header has been read, the FunctionsWithBodies list is
117 // reversed. This keeps track of whether we've done this yet.
118 bool HasReversedFunctionsWithBodies;
120 /// DeferredFunctionInfo - When function bodies are initially scanned, this
121 /// map contains info about where to find deferred function body (in the
122 /// stream) and what linkage the original function had.
123 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
125 explicit BitcodeReader(MemoryBuffer *buffer)
126 : Buffer(buffer), ErrorString(0) {
127 HasReversedFunctionsWithBodies = false;
135 /// releaseMemoryBuffer - This causes the reader to completely forget about
136 /// the memory buffer it contains, which prevents the buffer from being
137 /// destroyed when it is deleted.
138 void releaseMemoryBuffer() {
142 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
143 virtual Module *materializeModule(std::string *ErrInfo = 0);
144 virtual void dematerializeFunction(Function *F);
145 virtual Module *releaseModule(std::string *ErrInfo = 0);
147 bool Error(const char *Str) {
151 const char *getErrorString() const { return ErrorString; }
153 /// @brief Main interface to parsing a bitcode buffer.
154 /// @returns true if an error occurred.
157 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
158 Value *getFnValueByID(unsigned ID, const Type *Ty) {
159 return ValueList.getValueFwdRef(ID, Ty);
161 BasicBlock *getBasicBlock(unsigned ID) const {
162 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
163 return FunctionBBs[ID];
165 AttrListPtr getAttributes(unsigned i) const {
166 if (i-1 < MAttributes.size())
167 return MAttributes[i-1];
168 return AttrListPtr();
171 /// getValueTypePair - Read a value/type pair out of the specified record from
172 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
173 /// Return true on failure.
174 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
175 unsigned InstNum, Value *&ResVal) {
176 if (Slot == Record.size()) return true;
177 unsigned ValNo = (unsigned)Record[Slot++];
178 if (ValNo < InstNum) {
179 // If this is not a forward reference, just return the value we already
181 ResVal = getFnValueByID(ValNo, 0);
183 } else if (Slot == Record.size()) {
187 unsigned TypeNo = (unsigned)Record[Slot++];
188 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
191 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
192 const Type *Ty, Value *&ResVal) {
193 if (Slot == Record.size()) return true;
194 unsigned ValNo = (unsigned)Record[Slot++];
195 ResVal = getFnValueByID(ValNo, Ty);
200 bool ParseModule(const std::string &ModuleID);
201 bool ParseAttributeBlock();
202 bool ParseTypeTable();
203 bool ParseTypeSymbolTable();
204 bool ParseValueSymbolTable();
205 bool ParseConstants();
206 bool RememberAndSkipFunctionBody();
207 bool ParseFunctionBody(Function *F);
208 bool ResolveGlobalAndAliasInits();
211 } // End llvm namespace