* Cleaned up code:
[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 AbstractTypeUser, public AbstractModuleProvider {
47   unsigned char *Buffer;
48   BytecodeParser(const BytecodeParser &);  // DO NOT IMPLEMENT
49   void operator=(const BytecodeParser &);  // DO NOT IMPLEMENT
50 public:
51   BytecodeParser() : Buffer(0) {
52     // Define this in case we don't see a ModuleGlobalInfo block.
53     FirstDerivedTyID = Type::FirstDerivedTyID;
54   }
55   
56   ~BytecodeParser() {
57     freeState();
58   }
59   void freeState() {
60     freeTable(Values);
61     freeTable(LateResolveValues);
62     freeTable(ModuleValues);
63     delete [] Buffer;
64     Buffer = 0;
65   }
66
67   Module* releaseModule() {
68     // Since we're losing control of this Module, we must hand it back complete
69     materializeModule();
70     freeState();
71     Module *tempM = TheModule; 
72     TheModule = 0; 
73     return tempM; 
74   }
75
76   void ParseBytecode(const unsigned char *Buf, unsigned Length,
77                      const std::string &ModuleID);
78
79   void dump() const {
80     std::cerr << "BytecodeParser instance!\n";
81   }
82
83 private:          // All of this data is transient across calls to ParseBytecode
84   struct ValueList : public User {
85     ValueList() : User(Type::TypeTy, Value::TypeVal) {
86     }
87     ~ValueList() {}
88
89     // vector compatibility methods
90     unsigned size() const { return getNumOperands(); }
91     void push_back(Value *V) { Operands.push_back(Use(V, this)); }
92     Value *back() const { return Operands.back(); }
93     void pop_back() { Operands.pop_back(); }
94     bool empty() const { return Operands.empty(); }
95
96     virtual void print(std::ostream& OS) const {
97       OS << "Bytecode Reader UseHandle!";
98     }
99   };
100
101   // Information about the module, extracted from the bytecode revision number.
102   unsigned char RevisionNum;        // The rev # itself
103   unsigned char FirstDerivedTyID;   // First variable index to use for type
104   bool HasImplicitZeroInitializer;  // Is entry 0 of every slot implicity zeros?
105   bool hasInternalMarkerOnly;       // Only types of linkage are intern/external
106
107   typedef std::vector<ValueList*> ValueTable;
108   ValueTable Values, LateResolveValues;
109   ValueTable ModuleValues;
110
111   // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
112   // references to global values or constants.  Such values may be referenced
113   // before they are defined, and if so, the temporary object that they
114   // represent is held here.
115   //
116   typedef std::map<std::pair<const Type *, unsigned>, Value*>  GlobalRefsType;
117   GlobalRefsType GlobalRefs;
118
119   // TypesLoaded - This vector mirrors the Values[TypeTyID] plane.  It is used
120   // to deal with forward references to types.
121   //
122   typedef std::vector<PATypeHandle> TypeValuesListTy;
123   TypeValuesListTy ModuleTypeValues;
124   TypeValuesListTy FunctionTypeValues;
125
126   // When the ModuleGlobalInfo section is read, we create a function object for
127   // each function in the module.  When the function is loaded, this function is
128   // filled in.
129   //
130   std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
131
132   // Constant values are read in after global variables.  Because of this, we
133   // must defer setting the initializers on global variables until after module
134   // level constants have been read.  In the mean time, this list keeps track of
135   // what we must do.
136   //
137   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
138
139   // For lazy reading-in of functions, we need to save away several pieces of
140   // information about each function: its begin and end pointer in the buffer
141   // and its FunctionSlot.
142   // 
143   std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
144   
145 private:
146   void freeTable(ValueTable &Tab) {
147     while (!Tab.empty()) {
148       delete Tab.back();
149       Tab.pop_back();
150     }
151   }
152
153 public:
154   void ParseModule(const unsigned char * Buf, const unsigned char *End);
155   void materializeFunction(Function *F);
156
157 private:
158   void ParseVersionInfo   (const unsigned char *&Buf, const unsigned char *End);
159   void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
160   void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
161                         SymbolTable *);
162   void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
163   void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
164
165   std::auto_ptr<BasicBlock>
166   ParseBasicBlock(const unsigned char *&Buf, const unsigned char *End);
167
168   bool ParseInstruction   (const unsigned char *&Buf, const unsigned char *End,
169                            Instruction *&);
170   bool ParseRawInst       (const unsigned char *&Buf, const unsigned char *End,
171                            RawInst &);
172
173   void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
174                          ValueTable &Tab, TypeValuesListTy &TypeTab);
175   void parseConstantValue(const unsigned char *&Buf, const unsigned char *End,
176                           const Type *Ty, Constant *&V);
177   void parseTypeConstants(const unsigned char *&Buf,
178                           const unsigned char *EndBuf,
179                           TypeValuesListTy &Tab, unsigned NumEntries);
180   const Type *parseTypeConstant(const unsigned char *&Buf,
181                                 const unsigned char *EndBuf);
182
183   Value      *getValue(const Type *Ty, unsigned num, bool Create = true);
184   const Type *getType(unsigned ID);
185   Constant   *getConstantValue(const Type *Ty, unsigned num);
186
187   int insertValue(Value *V, ValueTable &Table);  // -1 = Failure
188   void setValueTo(ValueTable &D, unsigned Slot, Value *V);
189   void postResolveValues(ValueTable &ValTab);
190
191   void getTypeSlot(const Type *Ty, unsigned &Slot);
192
193   // resolve all references to the placeholder (if any) for the given value
194   void ResolveReferencesToValue(Value *Val, unsigned Slot);
195
196   
197   // refineAbstractType - The callback method is invoked when one of the
198   // elements of TypeValues becomes more concrete...
199   //
200   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
201 };
202
203 template<class SuperType>
204 class PlaceholderDef : public SuperType {
205   unsigned ID;
206   PlaceholderDef();                       // DO NOT IMPLEMENT
207   void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
208 public:
209   PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
210   unsigned getID() { return ID; }
211 };
212
213 struct InstPlaceHolderHelper : public Instruction {
214   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
215   virtual const char *getOpcodeName() const { return "placeholder"; }
216
217   virtual Instruction *clone() const { abort(); return 0; }
218 };
219
220 struct BBPlaceHolderHelper : public BasicBlock {
221   BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
222     assert(Ty == Type::LabelTy);
223   }
224 };
225
226 struct ConstantPlaceHolderHelper : public Constant {
227   ConstantPlaceHolderHelper(const Type *Ty)
228     : Constant(Ty) {}
229   virtual bool isNullValue() const { return false; }
230 };
231
232 typedef PlaceholderDef<InstPlaceHolderHelper>  ValPHolder;
233 typedef PlaceholderDef<BBPlaceHolderHelper>    BBPHolder;
234 typedef PlaceholderDef<ConstantPlaceHolderHelper>  ConstPHolder;
235
236 // Some common errors we find
237 static const std::string Error_readvbr   = "read_vbr(): error reading.";
238 static const std::string Error_read      = "read(): error reading.";
239 static const std::string Error_inputdata = "input_data(): error reading.";
240 static const std::string Error_DestSlot  = "No destination slot found.";
241
242 static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
243   if (isa<Constant>(Val))
244     return ((ConstPHolder*)Val)->getID();
245   
246   // else discriminate by type
247   switch (Val->getType()->getPrimitiveID()) {
248   case Type::LabelTyID:    return ((BBPHolder*)Val)->getID();
249   default:                 return ((ValPHolder*)Val)->getID();
250   }
251 }
252
253 static inline void readBlock(const unsigned char *&Buf,
254                              const unsigned char *EndBuf, 
255                              unsigned &Type, unsigned &Size) {
256 #if DEBUG_OUTPUT
257   bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
258   std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
259        << " Type = " << Type << " Size = " << Size << endl;
260   if (Result) throw Error_read;
261 #else
262   if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;
263 #endif
264 }
265
266 #endif