implement scafolding for lazy deserialization of function bodies
[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 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
25 namespace llvm {
26   class BitstreamReader;
27   class MemoryBuffer;
28   
29 class BitcodeReaderValueList : public User {
30   std::vector<Use> Uses;
31 public:
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   void initVal(unsigned Idx, Value *V) {
56     assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
57     Uses[Idx].init(V, this);
58   }
59 };
60   
61
62 class BitcodeReader : public ModuleProvider {
63   MemoryBuffer *Buffer;
64   BitstreamReader Stream;
65   
66   const char *ErrorString;
67   
68   std::vector<PATypeHolder> TypeList;
69   BitcodeReaderValueList ValueList;
70   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
71   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
72   
73   // When reading the module header, this list is populated with functions that
74   // have bodies later in the file.
75   std::vector<Function*> FunctionsWithBodies;
76   
77   // After the module header has been read, the FunctionsWithBodies list is 
78   // reversed.  This keeps track of whether we've done this yet.
79   bool HasReversedFunctionsWithBodies;
80   
81   /// DeferredFunctionInfo - When function bodies are initially scanned, this
82   /// map contains info about where to find deferred function body (in the
83   /// stream) and what linkage the original function had.
84   DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
85 public:
86   BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
87     HasReversedFunctionsWithBodies = false;
88   }
89   ~BitcodeReader();
90   
91   
92   /// releaseMemoryBuffer - This causes the reader to completely forget about
93   /// the memory buffer it contains, which prevents the buffer from being
94   /// destroyed when it is deleted.
95   void releaseMemoryBuffer() {
96     Buffer = 0;
97   }
98   
99   virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
100   
101   virtual Module *materializeModule(std::string *ErrInfo = 0) {
102     // FIXME: TODO
103     //if (ParseAllFunctionBodies(ErrMsg))
104     //  return 0;
105     return TheModule;
106   }
107   
108   bool Error(const char *Str) {
109     ErrorString = Str;
110     return true;
111   }
112   const char *getErrorString() const { return ErrorString; }
113   
114   /// @brief Main interface to parsing a bitcode buffer.
115   /// @returns true if an error occurred.
116   bool ParseBitcode();
117 private:
118   const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
119   
120   bool ParseModule(BitstreamReader &Stream, const std::string &ModuleID);
121   bool ParseTypeTable(BitstreamReader &Stream);
122   bool ParseTypeSymbolTable(BitstreamReader &Stream);
123   bool ParseValueSymbolTable(BitstreamReader &Stream);
124   bool ParseConstants(BitstreamReader &Stream);
125   bool ParseFunction(BitstreamReader &Stream);
126   bool ResolveGlobalAndAliasInits();
127 };
128   
129 } // End llvm namespace
130
131 #endif