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