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