reimplement BitcodeReaderValueList in terms of WeakVH instead of making
[oota-llvm.git] / lib / Bitcode / Reader / BitcodeReader.h
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
27 namespace llvm {
28   class MemoryBuffer;
29   
30 //===----------------------------------------------------------------------===//
31 //                          BitcodeReaderValueList Class
32 //===----------------------------------------------------------------------===//
33
34 class BitcodeReaderValueList {
35   std::vector<WeakVH> ValuePtrs;
36   
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.
41   ///
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;
46 public:
47   BitcodeReaderValueList() {}
48   ~BitcodeReaderValueList() {
49     assert(ResolveConstants.empty() && "Constants not resolved?");
50   }
51
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);
57   }
58   
59   void clear() {
60     assert(ResolveConstants.empty() && "Constants not resolved?");
61     ValuePtrs.clear();
62   }
63   
64   Value *operator[](unsigned i) const {
65     assert(i < ValuePtrs.size());
66     return ValuePtrs[i];
67   }
68   
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!");
74     ValuePtrs.resize(N);
75   }
76   
77   Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
78   Value *getValueFwdRef(unsigned Idx, const Type *Ty);
79   
80   void AssignValue(Value *V, unsigned Idx);
81   
82   /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
83   /// resolves any forward references.
84   void ResolveConstantForwardRefs();
85 };
86
87 class BitcodeReader : public ModuleProvider {
88   MemoryBuffer *Buffer;
89   BitstreamReader Stream;
90   
91   const char *ErrorString;
92   
93   std::vector<PATypeHolder> TypeList;
94   BitcodeReaderValueList ValueList;
95   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
96   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
97   
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
100   /// are off by one.
101   std::vector<AttrListPtr> MAttributes;
102   
103   /// FunctionBBs - While parsing a function body, this is a list of the basic
104   /// blocks for the function.
105   std::vector<BasicBlock*> FunctionBBs;
106   
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;
110
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;
115   
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;
119   
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;
124 public:
125   explicit BitcodeReader(MemoryBuffer *buffer)
126       : Buffer(buffer), ErrorString(0) {
127     HasReversedFunctionsWithBodies = false;
128   }
129   ~BitcodeReader() {
130     FreeState();
131   }
132   
133   void FreeState();
134   
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() {
139     Buffer = 0;
140   }
141   
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);
146
147   bool Error(const char *Str) {
148     ErrorString = Str;
149     return true;
150   }
151   const char *getErrorString() const { return ErrorString; }
152   
153   /// @brief Main interface to parsing a bitcode buffer.
154   /// @returns true if an error occurred.
155   bool ParseBitcode();
156 private:
157   const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
158   Value *getFnValueByID(unsigned ID, const Type *Ty) {
159     return ValueList.getValueFwdRef(ID, Ty);
160   }
161   BasicBlock *getBasicBlock(unsigned ID) const {
162     if (ID >= FunctionBBs.size()) return 0; // Invalid ID
163     return FunctionBBs[ID];
164   }
165   AttrListPtr getAttributes(unsigned i) const {
166     if (i-1 < MAttributes.size())
167       return MAttributes[i-1];
168     return AttrListPtr();
169   }
170   
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
180       // have.
181       ResVal = getFnValueByID(ValNo, 0);
182       return ResVal == 0;
183     } else if (Slot == Record.size()) {
184       return true;
185     }
186     
187     unsigned TypeNo = (unsigned)Record[Slot++];
188     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
189     return ResVal == 0;
190   }
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);
196     return ResVal == 0;
197   }
198
199   
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();
209 };
210   
211 } // End llvm namespace
212
213 #endif