BitcodeReader.h revision 48c85b84c1b66fb6a1b0d2afddf33da5bd82960d
198760c18f85bafd98dde7a309e1b0e677abd47d8Marshall Clow//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
298760c18f85bafd98dde7a309e1b0e677abd47d8Marshall Clow//
398760c18f85bafd98dde7a309e1b0e677abd47d8Marshall Clow//                     The LLVM Compiler Infrastructure
498760c18f85bafd98dde7a309e1b0e677abd47d8Marshall Clow//
598760c18f85bafd98dde7a309e1b0e677abd47d8Marshall Clow// This file was developed by Chris Lattner and is distributed under
698760c18f85bafd98dde7a309e1b0e677abd47d8Marshall Clow// the University of Illinois Open Source License.  See LICENSE.TXT for details.
798760c18f85bafd98dde7a309e1b0e677abd47d8Marshall Clow//
898760c18f85bafd98dde7a309e1b0e677abd47d8Marshall Clow//===----------------------------------------------------------------------===//
998760c18f85bafd98dde7a309e1b0e677abd47d8Marshall Clow//
10bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// This header defines the BitcodeReader class.
11bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
12bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//===----------------------------------------------------------------------===//
13bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
14bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#ifndef BITCODE_READER_H
15bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#define BITCODE_READER_H
16bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
17bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "llvm/ModuleProvider.h"
18bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "llvm/Type.h"
19bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "llvm/User.h"
20bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "llvm/Bitcode/BitstreamReader.h"
21f8f852138f86e4588916021e1afedfcab25298c0Howard Hinnant#include "llvm/Bitcode/LLVMBitCodes.h"
22bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "llvm/ADT/DenseMap.h"
23bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <vector>
24bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
25bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantnamespace llvm {
26c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  class MemoryBuffer;
27  class ParamAttrsList;
28
29class BitcodeReaderValueList : public User {
30  std::vector<Use> Uses;
31public:
32  BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
33
34  // vector compatibility methods
35  unsigned size() const { return getNumOperands(); }
36  void push_back(Value *V) {
37    Uses.push_back(Use(V, this));
38    OperandList = &Uses[0];
39    ++NumOperands;
40  }
41
42  Value *operator[](unsigned i) const { return getOperand(i); }
43
44  Value *back() const { return Uses.back(); }
45  void pop_back() { Uses.pop_back(); --NumOperands; }
46  bool empty() const { return NumOperands == 0; }
47  void shrinkTo(unsigned N) {
48    assert(N <= NumOperands && "Invalid shrinkTo request!");
49    Uses.resize(N);
50    NumOperands = N;
51  }
52  virtual void print(std::ostream&) const {}
53
54  Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
55  Value *getValueFwdRef(unsigned Idx, const Type *Ty);
56
57  void AssignValue(Value *V, unsigned Idx) {
58    if (Idx == size()) {
59      push_back(V);
60    } else if (Value *OldV = getOperand(Idx)) {
61      // If there was a forward reference to this value, replace it.
62      setOperand(Idx, V);
63      OldV->replaceAllUsesWith(V);
64      delete OldV;
65    } else {
66      initVal(Idx, V);
67    }
68  }
69
70private:
71  void initVal(unsigned Idx, Value *V) {
72    assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
73    Uses[Idx].init(V, this);
74  }
75};
76
77
78class BitcodeReader : public ModuleProvider {
79  MemoryBuffer *Buffer;
80  BitstreamReader Stream;
81
82  const char *ErrorString;
83
84  std::vector<PATypeHolder> TypeList;
85  BitcodeReaderValueList ValueList;
86  std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
87  std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
88
89  /// ParamAttrs - The set of parameter attributes by index.  Index zero in the
90  /// file is for null, and is thus not represented here.  As such all indices
91  /// are off by one.
92  std::vector<const ParamAttrsList*> ParamAttrs;
93
94  /// FunctionBBs - While parsing a function body, this is a list of the basic
95  /// blocks for the function.
96  std::vector<BasicBlock*> FunctionBBs;
97
98  // When reading the module header, this list is populated with functions that
99  // have bodies later in the file.
100  std::vector<Function*> FunctionsWithBodies;
101
102  // After the module header has been read, the FunctionsWithBodies list is
103  // reversed.  This keeps track of whether we've done this yet.
104  bool HasReversedFunctionsWithBodies;
105
106  /// DeferredFunctionInfo - When function bodies are initially scanned, this
107  /// map contains info about where to find deferred function body (in the
108  /// stream) and what linkage the original function had.
109  DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
110public:
111  BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
112    HasReversedFunctionsWithBodies = false;
113  }
114  ~BitcodeReader();
115
116
117  /// releaseMemoryBuffer - This causes the reader to completely forget about
118  /// the memory buffer it contains, which prevents the buffer from being
119  /// destroyed when it is deleted.
120  void releaseMemoryBuffer() {
121    Buffer = 0;
122  }
123
124  virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
125  virtual Module *materializeModule(std::string *ErrInfo = 0);
126
127  bool Error(const char *Str) {
128    ErrorString = Str;
129    return true;
130  }
131  const char *getErrorString() const { return ErrorString; }
132
133  /// @brief Main interface to parsing a bitcode buffer.
134  /// @returns true if an error occurred.
135  bool ParseBitcode();
136private:
137  const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
138  Value *getFnValueByID(unsigned ID, const Type *Ty) {
139    return ValueList.getValueFwdRef(ID, Ty);
140  }
141  BasicBlock *getBasicBlock(unsigned ID) const {
142    if (ID >= FunctionBBs.size()) return 0; // Invalid ID
143    return FunctionBBs[ID];
144  }
145  const ParamAttrsList *getParamAttrs(unsigned i) const {
146    if (i-1 < ParamAttrs.size())
147      return ParamAttrs[i-1];
148    return 0;
149  }
150
151
152  bool ParseModule(const std::string &ModuleID);
153  bool ParseParamAttrBlock();
154  bool ParseTypeTable();
155  bool ParseTypeSymbolTable();
156  bool ParseValueSymbolTable();
157  bool ParseConstants();
158  bool RememberAndSkipFunctionBody();
159  bool ParseFunctionBody(Function *F);
160  bool ResolveGlobalAndAliasInits();
161};
162
163} // End llvm namespace
164
165#endif
166