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