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 |
| 19 | #define TRACE_LEVEL 0 |
| 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 | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 48 | public: |
| 49 | BytecodeParser() { |
| 50 | // Define this in case we don't see a ModuleGlobalInfo block. |
| 51 | FirstDerivedTyID = Type::FirstDerivedTyID; |
| 52 | } |
| 53 | |
| 54 | Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf); |
| 55 | private: // All of this data is transient across calls to ParseBytecode |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 56 | Module *TheModule; // Current Module being read into... |
| 57 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | typedef vector<Value *> ValueList; |
| 59 | typedef vector<ValueList> ValueTable; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | ValueTable Values, LateResolveValues; |
| 61 | ValueTable ModuleValues, LateResolveModuleValues; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 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 map<pair<const PointerType *, unsigned>, GlobalVariable*> |
| 69 | GlobalRefsType; |
| 70 | GlobalRefsType GlobalRefs; |
| 71 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 72 | // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used |
| 73 | // to deal with forward references to types. |
| 74 | // |
| 75 | typedef vector<PATypeHandle<Type> > TypeValuesListTy; |
| 76 | TypeValuesListTy ModuleTypeValues; |
| 77 | TypeValuesListTy MethodTypeValues; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | ef9c23f | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 87 | list<pair<const PointerType *, unsigned> > MethodSignatureList; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | bool ParseModule (const uchar * Buf, const uchar *End, Module *&); |
| 91 | bool ParseModuleGlobalInfo (const uchar *&Buf, const uchar *End, Module *); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 92 | bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *); |
| 93 | bool ParseMethod (const uchar *&Buf, const uchar *End, Module *); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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, |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 99 | ValueTable &Tab, TypeValuesListTy &TypeTab); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 100 | bool parseConstPoolValue(const uchar *&Buf, const uchar *End, |
| 101 | const Type *Ty, ConstPoolVal *&V); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 102 | bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf, |
| 103 | TypeValuesListTy &Tab, unsigned NumEntries); |
| 104 | const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 105 | |
| 106 | Value *getValue(const Type *Ty, unsigned num, bool Create = true); |
| 107 | const Type *getType(unsigned ID); |
| 108 | |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 109 | int insertValue(Value *D, vector<ValueList> &D); // -1 = Failure |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 110 | bool postResolveValues(ValueTable &ValTab); |
| 111 | |
| 112 | bool getTypeSlot(const Type *Ty, unsigned &Slot); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 114 | // DeclareNewGlobalValue - Patch up forward references to global values in the |
Chris Lattner | c18545d | 2001-10-15 13:21:42 +0000 | [diff] [blame^] | 115 | // form of ConstPoolPointerRefs. |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 116 | // |
| 117 | void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 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); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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, "") {} |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 135 | virtual const char *getOpcodeName() const { return "placeholder"; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 136 | |
| 137 | virtual Instruction *clone() const { abort(); return 0; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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) |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 148 | : Method(cast<const MethodType>(Ty)) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 185 | #endif |