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" |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 11 | #include "llvm/Function.h" |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 12 | #include "llvm/BasicBlock.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | #include "llvm/Instruction.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 14 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame^] | 15 | #include "llvm/Constant.h" |
| 16 | #include "Support/NonCopyable.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | #include <map> |
| 18 | #include <utility> |
Chris Lattner | 3ff4387 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 19 | #include <list> |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 20 | #include <iostream> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 22 | // 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] | 23 | #define TRACE_LEVEL 0 |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 24 | |
| 25 | #if TRACE_LEVEL // ByteCodeReading_TRACEer |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 26 | #define BCR_TRACE(n, X) if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 27 | #else |
| 28 | #define BCR_TRACE(n, X) |
| 29 | #endif |
| 30 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 31 | typedef unsigned char uchar; |
| 32 | |
| 33 | struct RawInst { // The raw fields out of the bytecode stream... |
| 34 | unsigned NumOperands; |
| 35 | unsigned Opcode; |
| 36 | const Type *Ty; |
| 37 | unsigned Arg1, Arg2; |
| 38 | union { |
| 39 | unsigned Arg3; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 40 | std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3 |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | }; |
| 42 | }; |
| 43 | |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame^] | 44 | |
| 45 | class ConstantFwdRefs: public NonCopyable { |
| 46 | Module* TheModule; |
| 47 | |
| 48 | // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward |
| 49 | // references to global values or constants. Such values may be referenced |
| 50 | // before they are defined, and if so, the temporary object that they |
| 51 | // represent is held here. |
| 52 | // |
| 53 | typedef std::map<std::pair<const Type *, unsigned>, |
| 54 | Value*> GlobalRefsType; |
| 55 | GlobalRefsType GlobalRefs; |
| 56 | |
| 57 | Value* find (const Type* Ty, unsigned Slot); |
| 58 | void insert (const Type* Ty, unsigned Slot, Value* V); |
| 59 | void erase (const Type* Ty, unsigned Slot); |
| 60 | |
| 61 | public: |
| 62 | // sets the current module pointer: needed to insert placeholder globals |
| 63 | void VisitingModule (Module* M) { TheModule = M; } |
| 64 | |
| 65 | // get a forward reference to a global or a constant |
| 66 | GlobalValue* GetFwdRefToGlobal (const PointerType* PT, unsigned Slot); |
| 67 | Constant* GetFwdRefToConstant (const Type* Ty, unsigned Slot); |
| 68 | |
| 69 | // resolve all references to the placeholder (if any) for the given value |
| 70 | void ResolveRefsToValue (Value* val, unsigned Slot); |
| 71 | }; |
| 72 | |
| 73 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 74 | class BytecodeParser : public AbstractTypeUser { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 75 | std::string Error; // Error message string goes here... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 76 | public: |
| 77 | BytecodeParser() { |
| 78 | // Define this in case we don't see a ModuleGlobalInfo block. |
| 79 | FirstDerivedTyID = Type::FirstDerivedTyID; |
| 80 | } |
| 81 | |
| 82 | Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf); |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 84 | std::string getError() const { return Error; } |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 85 | |
Chris Lattner | b3afb1f | 2002-04-04 19:24:11 +0000 | [diff] [blame] | 86 | void dump() const { |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 87 | std::cerr << "BytecodeParser instance!\n"; |
Chris Lattner | b3afb1f | 2002-04-04 19:24:11 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 90 | private: // All of this data is transient across calls to ParseBytecode |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 91 | Module *TheModule; // Current Module being read into... |
| 92 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 93 | typedef std::vector<Value *> ValueList; |
| 94 | typedef std::vector<ValueList> ValueTable; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 95 | ValueTable Values, LateResolveValues; |
| 96 | ValueTable ModuleValues, LateResolveModuleValues; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 97 | |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame^] | 98 | // fwdRefs - This manages forward references to global values. |
| 99 | ConstantFwdRefs fwdRefs; |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 101 | // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used |
| 102 | // to deal with forward references to types. |
| 103 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 104 | typedef std::vector<PATypeHandle<Type> > TypeValuesListTy; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 105 | TypeValuesListTy ModuleTypeValues; |
| 106 | TypeValuesListTy MethodTypeValues; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 107 | |
| 108 | // Information read from the ModuleGlobalInfo section of the file... |
| 109 | unsigned FirstDerivedTyID; |
| 110 | |
| 111 | // When the ModuleGlobalInfo section is read, we load the type of each method |
| 112 | // and the 'ModuleValues' slot that it lands in. We then load a placeholder |
| 113 | // into its slot to reserve it. When the method is loaded, this placeholder |
| 114 | // is replaced. |
| 115 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 116 | std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 117 | |
| 118 | private: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 119 | bool ParseModule (const uchar * Buf, const uchar *End, Module *&); |
| 120 | bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 121 | bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *); |
| 122 | bool ParseMethod (const uchar *&Buf, const uchar *End, Module *); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 123 | bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&); |
| 124 | bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&); |
| 125 | bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &); |
| 126 | |
| 127 | bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf, |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 128 | ValueTable &Tab, TypeValuesListTy &TypeTab); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 129 | bool parseConstantValue(const uchar *&Buf, const uchar *End, |
| 130 | const Type *Ty, Constant *&V); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 131 | bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf, |
| 132 | TypeValuesListTy &Tab, unsigned NumEntries); |
| 133 | const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 134 | |
| 135 | Value *getValue(const Type *Ty, unsigned num, bool Create = true); |
| 136 | const Type *getType(unsigned ID); |
| 137 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 138 | int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 139 | bool postResolveValues(ValueTable &ValTab); |
| 140 | |
| 141 | bool getTypeSlot(const Type *Ty, unsigned &Slot); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 142 | |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame^] | 143 | // resolveRefsToGlobal -- resolve forward references to a global |
| 144 | // resolveRefsToConstant -- resolve forward references to a constant |
| 145 | // |
| 146 | void resolveRefsToGlobal(GlobalValue* GV, unsigned Slot); |
| 147 | void resolveRefsToConstant(Constant* C, unsigned Slot); |
| 148 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 149 | // refineAbstractType - The callback method is invoked when one of the |
| 150 | // elements of TypeValues becomes more concrete... |
| 151 | // |
| 152 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | template<class SuperType> |
| 156 | class PlaceholderDef : public SuperType { |
| 157 | unsigned ID; |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame^] | 158 | PlaceholderDef(); // do not implement |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 159 | public: |
| 160 | PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {} |
| 161 | unsigned getID() { return ID; } |
| 162 | }; |
| 163 | |
| 164 | struct InstPlaceHolderHelper : public Instruction { |
| 165 | InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {} |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 166 | virtual const char *getOpcodeName() const { return "placeholder"; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 167 | |
| 168 | virtual Instruction *clone() const { abort(); return 0; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | struct BBPlaceHolderHelper : public BasicBlock { |
| 172 | BBPlaceHolderHelper(const Type *Ty) : BasicBlock() { |
Chris Lattner | 35e309a | 2002-04-08 21:59:36 +0000 | [diff] [blame] | 173 | assert(Ty == Type::LabelTy); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 174 | } |
| 175 | }; |
| 176 | |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 177 | struct MethPlaceHolderHelper : public Function { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 178 | MethPlaceHolderHelper(const Type *Ty) |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 179 | : Function(cast<const FunctionType>(Ty), true) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 180 | } |
| 181 | }; |
| 182 | |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame^] | 183 | struct ConstantPlaceHolderHelper : public Constant { |
| 184 | ConstantPlaceHolderHelper(const Type *Ty) |
| 185 | : Constant(Ty) {} |
| 186 | virtual bool isNullValue() const { return false; } |
| 187 | }; |
| 188 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 189 | typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder; |
| 190 | typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder; |
| 191 | typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder; |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame^] | 192 | typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder; |
| 193 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 194 | |
| 195 | static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) { |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame^] | 196 | if (isa<Constant>(Def)) |
| 197 | return ((ConstPHolder*)Def)->getID(); |
| 198 | |
| 199 | // else discriminate by type |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 200 | switch (Def->getType()->getPrimitiveID()) { |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 201 | case Type::LabelTyID: return ((BBPHolder*)Def)->getID(); |
| 202 | case Type::FunctionTyID: return ((MethPHolder*)Def)->getID(); |
| 203 | default: return ((DefPHolder*)Def)->getID(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf, |
| 208 | unsigned &Type, unsigned &Size) { |
| 209 | #if DEBUG_OUTPUT |
| 210 | bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size); |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 211 | std::cerr << "StartLoc = " << ((unsigned)Buf & 4095) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 212 | << " Type = " << Type << " Size = " << Size << endl; |
| 213 | return Result; |
| 214 | #else |
| 215 | return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size); |
| 216 | #endif |
| 217 | } |
| 218 | |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 219 | |
| 220 | // failure Template - This template function is used as a place to put |
| 221 | // breakpoints in to debug failures of the bytecode parser. |
| 222 | // |
| 223 | template <typename X> |
| 224 | static X failure(X Value) { |
| 225 | return Value; |
| 226 | } |
| 227 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 228 | #endif |