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"
31 //===----------------------------------------------------------------------===//
32 // BitcodeReaderValueList Class
33 //===----------------------------------------------------------------------===//
35 class BitcodeReaderValueList {
36 std::vector<WeakVH> ValuePtrs;
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.
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;
48 BitcodeReaderValueList() {}
49 ~BitcodeReaderValueList() {
50 assert(ResolveConstants.empty() && "Constants not resolved?");
53 // vector compatibility methods
54 unsigned size() const { return ValuePtrs.size(); }
55 void resize(unsigned N) { ValuePtrs.resize(N); }
56 void push_back(Value *V) {
57 ValuePtrs.push_back(V);
61 assert(ResolveConstants.empty() && "Constants not resolved?");
65 Value *operator[](unsigned i) const {
66 assert(i < ValuePtrs.size());
70 Value *back() const { return ValuePtrs.back(); }
71 void pop_back() { ValuePtrs.pop_back(); }
72 bool empty() const { return ValuePtrs.empty(); }
73 void shrinkTo(unsigned N) {
74 assert(N <= size() && "Invalid shrinkTo request!");
78 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
79 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
81 void AssignValue(Value *V, unsigned Idx);
83 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
84 /// resolves any forward references.
85 void ResolveConstantForwardRefs();
88 class BitcodeReader : public ModuleProvider {
91 BitstreamReader StreamFile;
92 BitstreamCursor Stream;
94 const char *ErrorString;
96 std::vector<PATypeHolder> TypeList;
97 BitcodeReaderValueList ValueList;
98 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
99 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
101 /// MAttributes - The set of attributes by index. Index zero in the
102 /// file is for null, and is thus not represented here. As such all indices
104 std::vector<AttrListPtr> MAttributes;
106 /// FunctionBBs - While parsing a function body, this is a list of the basic
107 /// blocks for the function.
108 std::vector<BasicBlock*> FunctionBBs;
110 // When reading the module header, this list is populated with functions that
111 // have bodies later in the file.
112 std::vector<Function*> FunctionsWithBodies;
114 // When intrinsic functions are encountered which require upgrading they are
115 // stored here with their replacement function.
116 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
117 UpgradedIntrinsicMap UpgradedIntrinsics;
119 // After the module header has been read, the FunctionsWithBodies list is
120 // reversed. This keeps track of whether we've done this yet.
121 bool HasReversedFunctionsWithBodies;
123 /// DeferredFunctionInfo - When function bodies are initially scanned, this
124 /// map contains info about where to find deferred function body (in the
125 /// stream) and what linkage the original function had.
126 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
128 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C)
129 : Context(C), Buffer(buffer), ErrorString(0) {
130 HasReversedFunctionsWithBodies = false;
138 /// releaseMemoryBuffer - This causes the reader to completely forget about
139 /// the memory buffer it contains, which prevents the buffer from being
140 /// destroyed when it is deleted.
141 void releaseMemoryBuffer() {
145 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
146 virtual Module *materializeModule(std::string *ErrInfo = 0);
147 virtual void dematerializeFunction(Function *F);
148 virtual Module *releaseModule(std::string *ErrInfo = 0);
150 bool Error(const char *Str) {
154 const char *getErrorString() const { return ErrorString; }
156 /// @brief Main interface to parsing a bitcode buffer.
157 /// @returns true if an error occurred.
160 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
161 Value *getFnValueByID(unsigned ID, const Type *Ty) {
162 return ValueList.getValueFwdRef(ID, Ty);
164 BasicBlock *getBasicBlock(unsigned ID) const {
165 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
166 return FunctionBBs[ID];
168 AttrListPtr getAttributes(unsigned i) const {
169 if (i-1 < MAttributes.size())
170 return MAttributes[i-1];
171 return AttrListPtr();
174 /// getValueTypePair - Read a value/type pair out of the specified record from
175 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
176 /// Return true on failure.
177 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
178 unsigned InstNum, Value *&ResVal) {
179 if (Slot == Record.size()) return true;
180 unsigned ValNo = (unsigned)Record[Slot++];
181 if (ValNo < InstNum) {
182 // If this is not a forward reference, just return the value we already
184 ResVal = getFnValueByID(ValNo, 0);
186 } else if (Slot == Record.size()) {
190 unsigned TypeNo = (unsigned)Record[Slot++];
191 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
194 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
195 const Type *Ty, Value *&ResVal) {
196 if (Slot == Record.size()) return true;
197 unsigned ValNo = (unsigned)Record[Slot++];
198 ResVal = getFnValueByID(ValNo, Ty);
203 bool ParseModule(const std::string &ModuleID);
204 bool ParseAttributeBlock();
205 bool ParseTypeTable();
206 bool ParseTypeSymbolTable();
207 bool ParseValueSymbolTable();
208 bool ParseConstants();
209 bool RememberAndSkipFunctionBody();
210 bool ParseFunctionBody(Function *F);
211 bool ResolveGlobalAndAliasInits();
214 } // End llvm namespace