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/ParameterAttributes.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/ADT/DenseMap.h"
29 //===----------------------------------------------------------------------===//
30 // BitcodeReaderValueList Class
31 //===----------------------------------------------------------------------===//
33 class BitcodeReaderValueList : public User {
36 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0)
39 /// Provide fast operand accessors
40 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
42 // vector compatibility methods
43 unsigned size() const { return getNumOperands(); }
44 void resize(unsigned);
45 void push_back(Value *V) {
46 unsigned OldOps(NumOperands), NewOps(NumOperands + 1);
49 OperandList[OldOps] = V;
53 if (OperandList) dropHungoffUses(OperandList);
57 Value *operator[](unsigned i) const { return getOperand(i); }
59 Value *back() const { return getOperand(size() - 1); }
60 void pop_back() { setOperand(size() - 1, 0); --NumOperands; }
61 bool empty() const { return NumOperands == 0; }
62 void shrinkTo(unsigned N) {
63 assert(N <= NumOperands && "Invalid shrinkTo request!");
64 while (NumOperands > N)
67 virtual void print(std::ostream&) const {}
69 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
70 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
72 void AssignValue(Value *V, unsigned Idx) {
75 } else if (Value *OldV = getOperand(Idx)) {
76 // If there was a forward reference to this value, replace it.
78 OldV->replaceAllUsesWith(V);
86 void initVal(unsigned Idx, Value *V) {
88 // Insert a bunch of null values.
91 assert(getOperand(Idx) == 0 && "Cannot init an already init'd Use!");
92 OperandList[Idx].init(V, this);
97 struct OperandTraits<BitcodeReaderValueList> : HungoffOperandTraits</*16 FIXME*/> {
100 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BitcodeReaderValueList, Value)
102 class BitcodeReader : public ModuleProvider {
103 MemoryBuffer *Buffer;
104 BitstreamReader Stream;
106 const char *ErrorString;
108 std::vector<PATypeHolder> TypeList;
109 BitcodeReaderValueList ValueList;
110 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
111 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
113 /// ParamAttrs - The set of parameter attributes by index. Index zero in the
114 /// file is for null, and is thus not represented here. As such all indices
116 std::vector<PAListPtr> ParamAttrs;
118 /// FunctionBBs - While parsing a function body, this is a list of the basic
119 /// blocks for the function.
120 std::vector<BasicBlock*> FunctionBBs;
122 // When reading the module header, this list is populated with functions that
123 // have bodies later in the file.
124 std::vector<Function*> FunctionsWithBodies;
126 // When intrinsic functions are encountered which require upgrading they are
127 // stored here with their replacement function.
128 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
129 UpgradedIntrinsicMap UpgradedIntrinsics;
131 // After the module header has been read, the FunctionsWithBodies list is
132 // reversed. This keeps track of whether we've done this yet.
133 bool HasReversedFunctionsWithBodies;
135 /// DeferredFunctionInfo - When function bodies are initially scanned, this
136 /// map contains info about where to find deferred function body (in the
137 /// stream) and what linkage the original function had.
138 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
140 explicit BitcodeReader(MemoryBuffer *buffer)
141 : Buffer(buffer), ErrorString(0) {
142 HasReversedFunctionsWithBodies = false;
150 /// releaseMemoryBuffer - This causes the reader to completely forget about
151 /// the memory buffer it contains, which prevents the buffer from being
152 /// destroyed when it is deleted.
153 void releaseMemoryBuffer() {
157 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
158 virtual Module *materializeModule(std::string *ErrInfo = 0);
159 virtual void dematerializeFunction(Function *F);
160 virtual Module *releaseModule(std::string *ErrInfo = 0);
162 bool Error(const char *Str) {
166 const char *getErrorString() const { return ErrorString; }
168 /// @brief Main interface to parsing a bitcode buffer.
169 /// @returns true if an error occurred.
172 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
173 Value *getFnValueByID(unsigned ID, const Type *Ty) {
174 return ValueList.getValueFwdRef(ID, Ty);
176 BasicBlock *getBasicBlock(unsigned ID) const {
177 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
178 return FunctionBBs[ID];
180 PAListPtr getParamAttrs(unsigned i) const {
181 if (i-1 < ParamAttrs.size())
182 return ParamAttrs[i-1];
186 /// getValueTypePair - Read a value/type pair out of the specified record from
187 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
188 /// Return true on failure.
189 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
190 unsigned InstNum, Value *&ResVal) {
191 if (Slot == Record.size()) return true;
192 unsigned ValNo = (unsigned)Record[Slot++];
193 if (ValNo < InstNum) {
194 // If this is not a forward reference, just return the value we already
196 ResVal = getFnValueByID(ValNo, 0);
198 } else if (Slot == Record.size()) {
202 unsigned TypeNo = (unsigned)Record[Slot++];
203 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
206 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
207 const Type *Ty, Value *&ResVal) {
208 if (Slot == Record.size()) return true;
209 unsigned ValNo = (unsigned)Record[Slot++];
210 ResVal = getFnValueByID(ValNo, Ty);
215 bool ParseModule(const std::string &ModuleID);
216 bool ParseParamAttrBlock();
217 bool ParseTypeTable();
218 bool ParseTypeSymbolTable();
219 bool ParseValueSymbolTable();
220 bool ParseConstants();
221 bool RememberAndSkipFunctionBody();
222 bool ParseFunctionBody(Function *F);
223 bool ResolveGlobalAndAliasInits();
226 } // End llvm namespace