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