c75c6557648310544f9c546029478815495e8407
[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/Constants.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 namespace llvm {
26
27 // Enable to trace to figure out what the heck is going on when parsing fails
28 //#define TRACE_LEVEL 10
29 //#define DEBUG_OUTPUT
30
31 #if TRACE_LEVEL    // ByteCodeReading_TRACEr
32 #define BCR_TRACE(n, X) \
33     if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
34 #else
35 #define BCR_TRACE(n, X)
36 #endif
37
38 struct LazyFunctionInfo {
39   const unsigned char *Buf, *EndBuf;
40   unsigned FunctionSlot;
41 };
42
43 class BytecodeParser : public ModuleProvider {
44   BytecodeParser(const BytecodeParser &);  // DO NOT IMPLEMENT
45   void operator=(const BytecodeParser &);  // DO NOT IMPLEMENT
46 public:
47   BytecodeParser() {
48     // Define this in case we don't see a ModuleGlobalInfo block.
49     FirstDerivedTyID = Type::FirstDerivedTyID;
50   }
51   
52   ~BytecodeParser() {
53     freeState();
54   }
55   void freeState() {
56     freeTable(Values);
57     freeTable(ModuleValues);
58   }
59
60   Module* releaseModule() {
61     // Since we're losing control of this Module, we must hand it back complete
62     Module *M = ModuleProvider::releaseModule();
63     freeState();
64     return M;
65   }
66
67   void ParseBytecode(const unsigned char *Buf, unsigned Length,
68                      const std::string &ModuleID);
69
70   void dump() const {
71     std::cerr << "BytecodeParser instance!\n";
72   }
73
74 private:
75   struct ValueList : public User {
76     ValueList() : User(Type::TypeTy, Value::TypeVal) {}
77
78     // vector compatibility methods
79     unsigned size() const { return getNumOperands(); }
80     void push_back(Value *V) { Operands.push_back(Use(V, this)); }
81     Value *back() const { return Operands.back(); }
82     void pop_back() { Operands.pop_back(); }
83     bool empty() const { return Operands.empty(); }
84
85     virtual void print(std::ostream& OS) const {
86       OS << "Bytecode Reader UseHandle!";
87     }
88   };
89
90   // Information about the module, extracted from the bytecode revision number.
91   unsigned char RevisionNum;        // The rev # itself
92   unsigned char FirstDerivedTyID;   // First variable index to use for type
93   bool hasInternalMarkerOnly;       // Only types of linkage are intern/external
94   bool hasExtendedLinkageSpecs;     // Supports more than 4 linkage types
95   bool hasOldStyleVarargs;          // Has old version of varargs intrinsics?
96   bool hasVarArgCallPadding;        // Bytecode has extra padding in vararg call
97
98   bool usesOldStyleVarargs;         // Does this module USE old style varargs?
99
100   typedef std::vector<ValueList*> ValueTable;
101   ValueTable Values;
102   ValueTable ModuleValues;
103   std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
104
105   std::vector<BasicBlock*> ParsedBasicBlocks;
106
107   // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
108   // references to global values or constants.  Such values may be referenced
109   // before they are defined, and if so, the temporary object that they
110   // represent is held here.
111   //
112   typedef std::map<std::pair<const Type *, unsigned>, Value*>  GlobalRefsType;
113   GlobalRefsType GlobalRefs;
114
115   // TypesLoaded - This vector mirrors the Values[TypeTyID] plane.  It is used
116   // to deal with forward references to types.
117   //
118   typedef std::vector<PATypeHolder> TypeValuesListTy;
119   TypeValuesListTy ModuleTypeValues;
120   TypeValuesListTy FunctionTypeValues;
121
122   // When the ModuleGlobalInfo section is read, we create a function object for
123   // each function in the module.  When the function is loaded, this function is
124   // filled in.
125   //
126   std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
127
128   // Constant values are read in after global variables.  Because of this, we
129   // must defer setting the initializers on global variables until after module
130   // level constants have been read.  In the mean time, this list keeps track of
131   // what we must do.
132   //
133   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
134
135   // For lazy reading-in of functions, we need to save away several pieces of
136   // information about each function: its begin and end pointer in the buffer
137   // and its FunctionSlot.
138   // 
139   std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
140   
141 private:
142   void freeTable(ValueTable &Tab) {
143     while (!Tab.empty()) {
144       delete Tab.back();
145       Tab.pop_back();
146     }
147   }
148
149 public:
150   void ParseModule(const unsigned char * Buf, const unsigned char *End);
151   void materializeFunction(Function *F);
152
153 private:
154   void ParseVersionInfo   (const unsigned char *&Buf, const unsigned char *End);
155   void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
156   void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
157                         SymbolTable *, Function *CurrentFunction);
158   void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
159   void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
160
161   BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
162                               const unsigned char *End,
163                               unsigned BlockNo);
164
165   void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
166                         std::vector<unsigned> &Args, BasicBlock *BB);
167
168   void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
169                          ValueTable &Tab, TypeValuesListTy &TypeTab);
170   Constant *parseConstantValue(const unsigned char *&Buf,
171                                const unsigned char *End,
172                                const Type *Ty);
173   void parseTypeConstants(const unsigned char *&Buf,
174                           const unsigned char *EndBuf,
175                           TypeValuesListTy &Tab, unsigned NumEntries);
176   const Type *parseTypeConstant(const unsigned char *&Buf,
177                                 const unsigned char *EndBuf);
178
179   Value      *getValue(const Type *Ty, unsigned num, bool Create = true);
180   Value      *getValue(unsigned TypeID, unsigned num, bool Create = true);
181   const Type *getType(unsigned ID);
182   BasicBlock *getBasicBlock(unsigned ID);
183   Constant   *getConstantValue(const Type *Ty, unsigned num);
184
185   unsigned insertValue(Value *V, ValueTable &Table);
186   unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
187
188   unsigned getTypeSlot(const Type *Ty);
189
190   // resolve all references to the placeholder (if any) for the given value
191   void ResolveReferencesToValue(Value *Val, unsigned Slot);
192 };
193
194 template<class SuperType>
195 class PlaceholderDef : public SuperType {
196   unsigned ID;
197   PlaceholderDef();                       // DO NOT IMPLEMENT
198   void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
199 public:
200   PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
201   unsigned getID() { return ID; }
202 };
203
204 struct ConstantPlaceHolderHelper : public ConstantExpr {
205   ConstantPlaceHolderHelper(const Type *Ty) : ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {}
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 << "\n";
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 } // End llvm namespace
230
231 #endif