a5b3b7acfd66b1b2d444d602549d8f5d7bf30832
[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/Bytecode/Primitives.h"
11 #include "llvm/SymTabValue.h"
12 #include "llvm/Method.h"
13 #include "llvm/BasicBlock.h"
14 #include "llvm/Instruction.h"
15 #include "llvm/DerivedTypes.h"
16 #include <map>
17 #include <utility>
18 #include <list>
19
20 // Enable to trace to figure out what the heck is going on when parsing fails
21 #define TRACE_LEVEL 0
22
23 #if TRACE_LEVEL    // ByteCodeReading_TRACEer
24 #include "llvm/Assembly/Writer.h"
25 #define BCR_TRACE(n, X) if (n < TRACE_LEVEL) cerr << std::string(n*2, ' ') << X
26 #else
27 #define BCR_TRACE(n, X)
28 #endif
29
30 class BasicBlock;
31 class Method;
32 class Module;
33 class Type;
34 class PointerType;
35
36 typedef unsigned char uchar;
37
38 struct RawInst {       // The raw fields out of the bytecode stream...
39   unsigned NumOperands;
40   unsigned Opcode;
41   const Type *Ty;
42   unsigned Arg1, Arg2;
43   union {
44     unsigned Arg3;
45     std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
46   };
47 };
48
49 class BytecodeParser : public AbstractTypeUser {
50   std::string Error;     // Error message string goes here...
51 public:
52   BytecodeParser() {
53     // Define this in case we don't see a ModuleGlobalInfo block.
54     FirstDerivedTyID = Type::FirstDerivedTyID;
55   }
56
57   Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
58
59   std::string getError() const { return Error; }
60
61 private:          // All of this data is transient across calls to ParseBytecode
62   Module *TheModule;   // Current Module being read into...
63   
64   typedef std::vector<Value *> ValueList;
65   typedef std::vector<ValueList> ValueTable;
66   ValueTable Values, LateResolveValues;
67   ValueTable ModuleValues, LateResolveModuleValues;
68
69   // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
70   // references to global values.  Global values may be referenced before they
71   // are defined, and if so, the temporary object that they represent is held
72   // here.
73   //
74   typedef std::map<std::pair<const PointerType *, unsigned>,
75                    GlobalVariable*>  GlobalRefsType;
76   GlobalRefsType GlobalRefs;
77
78   // TypesLoaded - This vector mirrors the Values[TypeTyID] plane.  It is used
79   // to deal with forward references to types.
80   //
81   typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
82   TypeValuesListTy ModuleTypeValues;
83   TypeValuesListTy MethodTypeValues;
84
85   // Information read from the ModuleGlobalInfo section of the file...
86   unsigned FirstDerivedTyID;
87
88   // When the ModuleGlobalInfo section is read, we load the type of each method
89   // and the 'ModuleValues' slot that it lands in.  We then load a placeholder
90   // into its slot to reserve it.  When the method is loaded, this placeholder
91   // is replaced.
92   //
93   std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
94
95 private:
96   bool ParseModule          (const uchar * Buf, const uchar *End, Module *&);
97   bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *);
98   bool ParseSymbolTable   (const uchar *&Buf, const uchar *End, SymbolTable *);
99   bool ParseMethod        (const uchar *&Buf, const uchar *End, Module *);
100   bool ParseBasicBlock    (const uchar *&Buf, const uchar *End, BasicBlock *&);
101   bool ParseInstruction   (const uchar *&Buf, const uchar *End, Instruction *&);
102   bool ParseRawInst       (const uchar *&Buf, const uchar *End, RawInst &);
103
104   bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
105                          ValueTable &Tab, TypeValuesListTy &TypeTab);
106   bool parseConstantValue(const uchar *&Buf, const uchar *End,
107                           const Type *Ty, Constant *&V);
108   bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
109                           TypeValuesListTy &Tab, unsigned NumEntries);
110   const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
111
112   Value      *getValue(const Type *Ty, unsigned num, bool Create = true);
113   const Type *getType(unsigned ID);
114
115   int insertValue(Value *D, std::vector<ValueList> &D);  // -1 = Failure
116   bool postResolveValues(ValueTable &ValTab);
117
118   bool getTypeSlot(const Type *Ty, unsigned &Slot);
119
120   // DeclareNewGlobalValue - Patch up forward references to global values in the
121   // form of ConstantPointerRefs.
122   //
123   void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
124
125   // refineAbstractType - The callback method is invoked when one of the
126   // elements of TypeValues becomes more concrete...
127   //
128   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
129 };
130
131 template<class SuperType>
132 class PlaceholderDef : public SuperType {
133   unsigned ID;
134 public:
135   PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
136   unsigned getID() { return ID; }
137 };
138
139 struct InstPlaceHolderHelper : public Instruction {
140   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
141   virtual const char *getOpcodeName() const { return "placeholder"; }
142
143   virtual Instruction *clone() const { abort(); return 0; }
144 };
145
146 struct BBPlaceHolderHelper : public BasicBlock {
147   BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
148     assert(Ty->isLabelType());
149   }
150 };
151
152 struct MethPlaceHolderHelper : public Method {
153   MethPlaceHolderHelper(const Type *Ty) 
154     : Method(cast<const MethodType>(Ty), true) {
155   }
156 };
157
158 typedef PlaceholderDef<InstPlaceHolderHelper>  DefPHolder;
159 typedef PlaceholderDef<BBPlaceHolderHelper>    BBPHolder;
160 typedef PlaceholderDef<MethPlaceHolderHelper>  MethPHolder;
161
162 static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
163   switch (Def->getType()->getPrimitiveID()) {
164   case Type::LabelTyID:  return ((BBPHolder*)Def)->getID();
165   case Type::MethodTyID: return ((MethPHolder*)Def)->getID();
166   default:               return ((DefPHolder*)Def)->getID();
167   }
168 }
169
170 static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf, 
171                              unsigned &Type, unsigned &Size) {
172 #if DEBUG_OUTPUT
173   bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
174   cerr << "StartLoc = " << ((unsigned)Buf & 4095)
175        << " Type = " << Type << " Size = " << Size << endl;
176   return Result;
177 #else
178   return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
179 #endif
180 }
181
182
183 // failure Template - This template function is used as a place to put
184 // breakpoints in to debug failures of the bytecode parser.
185 //
186 template <typename X>
187 static X failure(X Value) {
188   return Value;
189 }
190
191 #endif