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/GVMaterializer.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
27namespace llvm {
28  class MemoryBuffer;
29  class LLVMContext;
30}
31
32namespace llvm_3_0 {
33
34using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37//                          BitcodeReaderValueList Class
38//===----------------------------------------------------------------------===//
39
40class BitcodeReaderValueList {
41  std::vector<WeakVH> ValuePtrs;
42
43  /// ResolveConstants - As we resolve forward-referenced constants, we add
44  /// information about them to this vector.  This allows us to resolve them in
45  /// bulk instead of resolving each reference at a time.  See the code in
46  /// ResolveConstantForwardRefs for more information about this.
47  ///
48  /// The key of this vector is the placeholder constant, the value is the slot
49  /// number that holds the resolved value.
50  typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
51  ResolveConstantsTy ResolveConstants;
52  LLVMContext &Context;
53public:
54  BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
55  ~BitcodeReaderValueList() {
56    assert(ResolveConstants.empty() && "Constants not resolved?");
57  }
58
59  // vector compatibility methods
60  unsigned size() const { return ValuePtrs.size(); }
61  void resize(unsigned N) { ValuePtrs.resize(N); }
62  void push_back(Value *V) {
63    ValuePtrs.push_back(V);
64  }
65
66  void clear() {
67    assert(ResolveConstants.empty() && "Constants not resolved?");
68    ValuePtrs.clear();
69  }
70
71  Value *operator[](unsigned i) const {
72    assert(i < ValuePtrs.size());
73    return ValuePtrs[i];
74  }
75
76  Value *back() const { return ValuePtrs.back(); }
77    void pop_back() { ValuePtrs.pop_back(); }
78  bool empty() const { return ValuePtrs.empty(); }
79  void shrinkTo(unsigned N) {
80    assert(N <= size() && "Invalid shrinkTo request!");
81    ValuePtrs.resize(N);
82  }
83
84  Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
85  Value *getValueFwdRef(unsigned Idx, Type *Ty);
86
87  void AssignValue(Value *V, unsigned Idx);
88
89  /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
90  /// resolves any forward references.
91  void ResolveConstantForwardRefs();
92};
93
94
95//===----------------------------------------------------------------------===//
96//                          BitcodeReaderMDValueList Class
97//===----------------------------------------------------------------------===//
98
99class BitcodeReaderMDValueList {
100  std::vector<WeakVH> MDValuePtrs;
101
102  LLVMContext &Context;
103public:
104  BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
105
106  // vector compatibility methods
107  unsigned size() const       { return MDValuePtrs.size(); }
108  void resize(unsigned N)     { MDValuePtrs.resize(N); }
109  void push_back(Value *V)    { MDValuePtrs.push_back(V);  }
110  void clear()                { MDValuePtrs.clear();  }
111  Value *back() const         { return MDValuePtrs.back(); }
112  void pop_back()             { MDValuePtrs.pop_back(); }
113  bool empty() const          { return MDValuePtrs.empty(); }
114
115  Value *operator[](unsigned i) const {
116    assert(i < MDValuePtrs.size());
117    return MDValuePtrs[i];
118  }
119
120  void shrinkTo(unsigned N) {
121    assert(N <= size() && "Invalid shrinkTo request!");
122    MDValuePtrs.resize(N);
123  }
124
125  Value *getValueFwdRef(unsigned Idx);
126  void AssignValue(Value *V, unsigned Idx);
127};
128
129class BitcodeReader : public GVMaterializer {
130  LLVMContext &Context;
131  Module *TheModule;
132  MemoryBuffer *Buffer;
133  bool BufferOwned;
134  BitstreamReader StreamFile;
135  BitstreamCursor Stream;
136
137  const char *ErrorString;
138
139  std::vector<Type*> TypeList;
140  BitcodeReaderValueList ValueList;
141  BitcodeReaderMDValueList MDValueList;
142  SmallVector<Instruction *, 64> InstructionList;
143
144  std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
145  std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
146
147  /// MAttributes - The set of attributes by index.  Index zero in the
148  /// file is for null, and is thus not represented here.  As such all indices
149  /// are off by one.
150  std::vector<AttrListPtr> MAttributes;
151
152  /// FunctionBBs - While parsing a function body, this is a list of the basic
153  /// blocks for the function.
154  std::vector<BasicBlock*> FunctionBBs;
155
156  // When reading the module header, this list is populated with functions that
157  // have bodies later in the file.
158  std::vector<Function*> FunctionsWithBodies;
159
160  // When intrinsic functions are encountered which require upgrading they are
161  // stored here with their replacement function.
162  typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
163  UpgradedIntrinsicMap UpgradedIntrinsics;
164
165  // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
166  DenseMap<unsigned, unsigned> MDKindMap;
167
168  // After the module header has been read, the FunctionsWithBodies list is
169  // reversed.  This keeps track of whether we've done this yet.
170  bool HasReversedFunctionsWithBodies;
171
172  /// DeferredFunctionInfo - When function bodies are initially scanned, this
173  /// map contains info about where to find deferred function body in the
174  /// stream.
175  DenseMap<Function*, uint64_t> DeferredFunctionInfo;
176
177  /// BlockAddrFwdRefs - These are blockaddr references to basic blocks.  These
178  /// are resolved lazily when functions are loaded.
179  typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
180  DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
181
182public:
183  explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
184    : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
185      ErrorString(0), ValueList(C), MDValueList(C) {
186    HasReversedFunctionsWithBodies = false;
187  }
188  ~BitcodeReader() {
189    FreeState();
190  }
191
192  void FreeState();
193
194  /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
195  /// when the reader is destroyed.
196  void setBufferOwned(bool Owned) { BufferOwned = Owned; }
197
198  virtual bool isMaterializable(const GlobalValue *GV) const;
199  virtual bool isDematerializable(const GlobalValue *GV) const;
200  virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
201  virtual bool MaterializeModule(Module *M, std::string *ErrInfo = 0);
202  virtual void Dematerialize(GlobalValue *GV);
203
204  bool Error(const char *Str) {
205    ErrorString = Str;
206    return true;
207  }
208  const char *getErrorString() const { return ErrorString; }
209
210  /// @brief Main interface to parsing a bitcode buffer.
211  /// @returns true if an error occurred.
212  bool ParseBitcodeInto(Module *M);
213
214  /// @brief Cheap mechanism to just extract module triple
215  /// @returns true if an error occurred.
216  bool ParseTriple(std::string &Triple);
217private:
218  Type *getTypeByID(unsigned ID);
219  Type *getTypeByIDOrNull(unsigned ID);
220  Value *getFnValueByID(unsigned ID, Type *Ty) {
221    if (Ty && Ty->isMetadataTy())
222      return MDValueList.getValueFwdRef(ID);
223    return ValueList.getValueFwdRef(ID, Ty);
224  }
225  BasicBlock *getBasicBlock(unsigned ID) const {
226    if (ID >= FunctionBBs.size()) return 0; // Invalid ID
227    return FunctionBBs[ID];
228  }
229  AttrListPtr getAttributes(unsigned i) const {
230    if (i-1 < MAttributes.size())
231      return MAttributes[i-1];
232    return AttrListPtr();
233  }
234
235  /// getValueTypePair - Read a value/type pair out of the specified record from
236  /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
237  /// Return true on failure.
238  bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
239                        unsigned InstNum, Value *&ResVal) {
240    if (Slot == Record.size()) return true;
241    unsigned ValNo = (unsigned)Record[Slot++];
242    if (ValNo < InstNum) {
243      // If this is not a forward reference, just return the value we already
244      // have.
245      ResVal = getFnValueByID(ValNo, 0);
246      return ResVal == 0;
247    } else if (Slot == Record.size()) {
248      return true;
249    }
250
251    unsigned TypeNo = (unsigned)Record[Slot++];
252    ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
253    return ResVal == 0;
254  }
255  bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
256                Type *Ty, Value *&ResVal) {
257    if (Slot == Record.size()) return true;
258    unsigned ValNo = (unsigned)Record[Slot++];
259    ResVal = getFnValueByID(ValNo, Ty);
260    return ResVal == 0;
261  }
262
263
264  bool ParseModule();
265  bool ParseAttributeBlock();
266  bool ParseTypeTable();
267  bool ParseOldTypeTable();         // FIXME: Remove in LLVM 3.1
268  bool ParseTypeTableBody();
269
270  bool ParseOldTypeSymbolTable();   // FIXME: Remove in LLVM 3.1
271  bool ParseValueSymbolTable();
272  bool ParseConstants();
273  bool RememberAndSkipFunctionBody();
274  bool ParseFunctionBody(Function *F);
275  bool ResolveGlobalAndAliasInits();
276  bool ParseMetadata();
277  bool ParseMetadataAttachment();
278  bool ParseModuleTriple(std::string &Triple);
279};
280
281} // End llvm_3_0 namespace
282
283#endif
284