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