Remove a dead method
[oota-llvm.git] / lib / Bytecode / Reader / ReaderInternals.h
1 //===-- ReaderInternals.h - Definitions internal to the reader ---*- C++ -*--=//
2 //
3 //  This header file defines various stuff that is used by the bytecode reader.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef READER_INTERNALS_H
8 #define READER_INTERNALS_H
9
10 #include "llvm/Constant.h"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/Function.h"
13 #include "llvm/ModuleProvider.h"
14 #include "llvm/Bytecode/Primitives.h"
15 #include <utility>
16 #include <map>
17 #include <memory>
18 class Module;
19
20 // Enable to trace to figure out what the heck is going on when parsing fails
21 //#define TRACE_LEVEL 10
22
23 #if TRACE_LEVEL    // ByteCodeReading_TRACEr
24 #define BCR_TRACE(n, X) \
25     if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
26 #else
27 #define BCR_TRACE(n, X)
28 #endif
29
30 struct RawInst {       // The raw fields out of the bytecode stream...
31   unsigned NumOperands;
32   unsigned Opcode;
33   const Type *Ty;
34   unsigned Arg1, Arg2;
35   union {
36     unsigned Arg3;
37     std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
38   };
39 };
40
41 struct LazyFunctionInfo {
42   const unsigned char *Buf, *EndBuf;
43   unsigned FunctionSlot;
44 };
45
46 class BytecodeParser : public ModuleProvider {
47   BytecodeParser(const BytecodeParser &);  // DO NOT IMPLEMENT
48   void operator=(const BytecodeParser &);  // DO NOT IMPLEMENT
49 public:
50   BytecodeParser() {
51     // Define this in case we don't see a ModuleGlobalInfo block.
52     FirstDerivedTyID = Type::FirstDerivedTyID;
53   }
54   
55   ~BytecodeParser() {
56     freeState();
57   }
58   void freeState() {
59     freeTable(Values);
60     freeTable(LateResolveValues);
61     freeTable(ModuleValues);
62   }
63
64   Module* releaseModule() {
65     // Since we're losing control of this Module, we must hand it back complete
66     materializeModule();
67     freeState();
68     Module *tempM = TheModule; 
69     TheModule = 0; 
70     return tempM; 
71   }
72
73   void ParseBytecode(const unsigned char *Buf, unsigned Length,
74                      const std::string &ModuleID);
75
76   void dump() const {
77     std::cerr << "BytecodeParser instance!\n";
78   }
79
80 private:
81   struct ValueList : public User {
82     ValueList() : User(Type::TypeTy, Value::TypeVal) {}
83
84     // vector compatibility methods
85     unsigned size() const { return getNumOperands(); }
86     void push_back(Value *V) { Operands.push_back(Use(V, this)); }
87     Value *back() const { return Operands.back(); }
88     void pop_back() { Operands.pop_back(); }
89     bool empty() const { return Operands.empty(); }
90
91     virtual void print(std::ostream& OS) const {
92       OS << "Bytecode Reader UseHandle!";
93     }
94   };
95
96   // Information about the module, extracted from the bytecode revision number.
97   unsigned char RevisionNum;        // The rev # itself
98   unsigned char FirstDerivedTyID;   // First variable index to use for type
99   bool HasImplicitZeroInitializer;  // Is entry 0 of every slot implicity zeros?
100   bool hasInternalMarkerOnly;       // Only types of linkage are intern/external
101
102   typedef std::vector<ValueList*> ValueTable;
103   ValueTable Values, LateResolveValues;
104   ValueTable ModuleValues;
105
106   std::vector<BasicBlock*> ParsedBasicBlocks;
107
108   // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
109   // references to global values or constants.  Such values may be referenced
110   // before they are defined, and if so, the temporary object that they
111   // represent is held here.
112   //
113   typedef std::map<std::pair<const Type *, unsigned>, Value*>  GlobalRefsType;
114   GlobalRefsType GlobalRefs;
115
116   // TypesLoaded - This vector mirrors the Values[TypeTyID] plane.  It is used
117   // to deal with forward references to types.
118   //
119   typedef std::vector<PATypeHolder> TypeValuesListTy;
120   TypeValuesListTy ModuleTypeValues;
121   TypeValuesListTy FunctionTypeValues;
122
123   // When the ModuleGlobalInfo section is read, we create a function object for
124   // each function in the module.  When the function is loaded, this function is
125   // filled in.
126   //
127   std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
128
129   // Constant values are read in after global variables.  Because of this, we
130   // must defer setting the initializers on global variables until after module
131   // level constants have been read.  In the mean time, this list keeps track of
132   // what we must do.
133   //
134   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
135
136   // For lazy reading-in of functions, we need to save away several pieces of
137   // information about each function: its begin and end pointer in the buffer
138   // and its FunctionSlot.
139   // 
140   std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
141   
142 private:
143   void freeTable(ValueTable &Tab) {
144     while (!Tab.empty()) {
145       delete Tab.back();
146       Tab.pop_back();
147     }
148   }
149
150 public:
151   void ParseModule(const unsigned char * Buf, const unsigned char *End);
152   void materializeFunction(Function *F);
153
154 private:
155   void ParseVersionInfo   (const unsigned char *&Buf, const unsigned char *End);
156   void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
157   void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
158                         SymbolTable *, Function *CurrentFunction);
159   void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
160   void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
161
162   BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
163                               const unsigned char *End,
164                               unsigned BlockNo);
165
166   bool ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
167                         Instruction *&);
168   std::auto_ptr<RawInst> ParseRawInst(const unsigned char *&Buf,
169                                       const unsigned char *End);
170
171   void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
172                          ValueTable &Tab, TypeValuesListTy &TypeTab);
173   Constant *parseConstantValue(const unsigned char *&Buf,
174                                const unsigned char *End,
175                                const Type *Ty);
176   void parseTypeConstants(const unsigned char *&Buf,
177                           const unsigned char *EndBuf,
178                           TypeValuesListTy &Tab, unsigned NumEntries);
179   const Type *parseTypeConstant(const unsigned char *&Buf,
180                                 const unsigned char *EndBuf);
181
182   Value      *getValue(const Type *Ty, unsigned num, bool Create = true);
183   Value      *getValue(unsigned TypeID, unsigned num, bool Create = true);
184   const Type *getType(unsigned ID);
185   BasicBlock *getBasicBlock(unsigned ID);
186   Constant   *getConstantValue(const Type *Ty, unsigned num);
187
188   int insertValue(Value *V, ValueTable &Table);  // -1 = Failure
189
190   unsigned getTypeSlot(const Type *Ty);
191
192   // resolve all references to the placeholder (if any) for the given value
193   void ResolveReferencesToValue(Value *Val, unsigned Slot);
194 };
195
196 template<class SuperType>
197 class PlaceholderDef : public SuperType {
198   unsigned ID;
199   PlaceholderDef();                       // DO NOT IMPLEMENT
200   void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
201 public:
202   PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
203   unsigned getID() { return ID; }
204 };
205
206 struct InstPlaceHolderHelper : public Instruction {
207   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
208   virtual const char *getOpcodeName() const { return "placeholder"; }
209
210   virtual Instruction *clone() const { abort(); return 0; }
211 };
212
213 struct ConstantPlaceHolderHelper : public Constant {
214   ConstantPlaceHolderHelper(const Type *Ty)
215     : Constant(Ty) {}
216   virtual bool isNullValue() const { return false; }
217 };
218
219 typedef PlaceholderDef<InstPlaceHolderHelper>  ValPHolder;
220 typedef PlaceholderDef<ConstantPlaceHolderHelper>  ConstPHolder;
221
222 // Some common errors we find
223 static const std::string Error_readvbr   = "read_vbr(): error reading.";
224 static const std::string Error_read      = "read(): error reading.";
225 static const std::string Error_inputdata = "input_data(): error reading.";
226 static const std::string Error_DestSlot  = "No destination slot found.";
227
228 static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
229   if (isa<Constant>(Val))
230     return ((ConstPHolder*)Val)->getID();
231   return ((ValPHolder*)Val)->getID();
232 }
233
234 static inline void readBlock(const unsigned char *&Buf,
235                              const unsigned char *EndBuf, 
236                              unsigned &Type, unsigned &Size) {
237 #if DEBUG_OUTPUT
238   bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
239   std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
240        << " Type = " << Type << " Size = " << Size << endl;
241   if (Result) throw Error_read;
242 #else
243   if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;
244 #endif
245 }
246
247 #endif