BitcodeReader.h revision 980e5aad4cfaa32e13b297f4201eb1088ca96cc4
1//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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/Type.h"
19#include "llvm/User.h"
20#include "llvm/Bitcode/BitstreamReader.h"
21#include "llvm/Bitcode/LLVMBitCodes.h"
22#include "llvm/ADT/DenseMap.h"
23#include <vector>
24
25namespace llvm {
26  class MemoryBuffer;
27
28class BitcodeReaderValueList : public User {
29  std::vector<Use> Uses;
30public:
31  BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
32
33  // vector compatibility methods
34  unsigned size() const { return getNumOperands(); }
35  void push_back(Value *V) {
36    Uses.push_back(Use(V, this));
37    OperandList = &Uses[0];
38    ++NumOperands;
39  }
40
41  Value *operator[](unsigned i) const { return getOperand(i); }
42
43  Value *back() const { return Uses.back(); }
44  void pop_back() { Uses.pop_back(); --NumOperands; }
45  bool empty() const { return NumOperands == 0; }
46  void shrinkTo(unsigned N) {
47    assert(N <= NumOperands && "Invalid shrinkTo request!");
48    Uses.resize(N);
49    NumOperands = N;
50  }
51  virtual void print(std::ostream&) const {}
52
53  Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
54  void initVal(unsigned Idx, Value *V) {
55    assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
56    Uses[Idx].init(V, this);
57  }
58};
59
60
61class BitcodeReader : public ModuleProvider {
62  MemoryBuffer *Buffer;
63  BitstreamReader Stream;
64
65  const char *ErrorString;
66
67  std::vector<PATypeHolder> TypeList;
68  BitcodeReaderValueList ValueList;
69  std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
70  std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
71
72  /// FunctionBBs - While parsing a function body, this is a list of the basic
73  /// blocks for the function.
74  std::vector<BasicBlock*> FunctionBBs;
75
76  // When reading the module header, this list is populated with functions that
77  // have bodies later in the file.
78  std::vector<Function*> FunctionsWithBodies;
79
80  // After the module header has been read, the FunctionsWithBodies list is
81  // reversed.  This keeps track of whether we've done this yet.
82  bool HasReversedFunctionsWithBodies;
83
84  /// DeferredFunctionInfo - When function bodies are initially scanned, this
85  /// map contains info about where to find deferred function body (in the
86  /// stream) and what linkage the original function had.
87  DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
88public:
89  BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
90    HasReversedFunctionsWithBodies = false;
91  }
92  ~BitcodeReader();
93
94
95  /// releaseMemoryBuffer - This causes the reader to completely forget about
96  /// the memory buffer it contains, which prevents the buffer from being
97  /// destroyed when it is deleted.
98  void releaseMemoryBuffer() {
99    Buffer = 0;
100  }
101
102  virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
103  virtual Module *materializeModule(std::string *ErrInfo = 0);
104
105  bool Error(const char *Str) {
106    ErrorString = Str;
107    return true;
108  }
109  const char *getErrorString() const { return ErrorString; }
110
111  /// @brief Main interface to parsing a bitcode buffer.
112  /// @returns true if an error occurred.
113  bool ParseBitcode();
114private:
115  const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
116
117  bool ParseModule(const std::string &ModuleID);
118  bool ParseTypeTable();
119  bool ParseTypeSymbolTable();
120  bool ParseValueSymbolTable();
121  bool ParseConstants();
122  bool RememberAndSkipFunctionBody();
123  bool ParseFunctionBody(Function *F);
124  bool ResolveGlobalAndAliasInits();
125};
126
127} // End llvm namespace
128
129#endif
130