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 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 10 | #include "llvm/Constant.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 11 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 12 | #include "llvm/Function.h" |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 13 | #include "llvm/ModuleProvider.h" |
| 14 | #include "llvm/Bytecode/Primitives.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | #include <utility> |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 16 | #include <map> |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 17 | #include <memory> |
| 18 | class Module; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 20 | // Enable to trace to figure out what the heck is going on when parsing fails |
Chris Lattner | d256ed8 | 2003-09-04 23:47:07 +0000 | [diff] [blame] | 21 | //#define TRACE_LEVEL 10 |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 22 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 23 | #if TRACE_LEVEL // ByteCodeReading_TRACEr |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 24 | #define BCR_TRACE(n, X) \ |
| 25 | if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 26 | #else |
| 27 | #define BCR_TRACE(n, X) |
| 28 | #endif |
| 29 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | struct RawInst { // The raw fields out of the bytecode stream... |
| 31 | unsigned NumOperands; |
| 32 | unsigned Opcode; |
| 33 | const Type *Ty; |
| 34 | unsigned Arg1, Arg2; |
| 35 | union { |
| 36 | unsigned Arg3; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 37 | std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3 |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | }; |
| 39 | }; |
| 40 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 41 | struct LazyFunctionInfo { |
| 42 | const unsigned char *Buf, *EndBuf; |
| 43 | unsigned FunctionSlot; |
| 44 | }; |
| 45 | |
| 46 | class BytecodeParser : public AbstractTypeUser, public AbstractModuleProvider { |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 47 | BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT |
| 48 | void operator=(const BytecodeParser &); // DO NOT IMPLEMENT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | public: |
Misha Brukman | 96f7877 | 2003-09-22 23:58:08 +0000 | [diff] [blame^] | 50 | BytecodeParser() { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 51 | // Define this in case we don't see a ModuleGlobalInfo block. |
| 52 | FirstDerivedTyID = Type::FirstDerivedTyID; |
| 53 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 55 | ~BytecodeParser() { |
Chris Lattner | a2602f3 | 2003-05-22 18:26:48 +0000 | [diff] [blame] | 56 | freeState(); |
| 57 | } |
| 58 | void freeState() { |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 59 | freeTable(Values); |
| 60 | freeTable(LateResolveValues); |
| 61 | freeTable(ModuleValues); |
| 62 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 63 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 64 | Module* releaseModule() { |
| 65 | // Since we're losing control of this Module, we must hand it back complete |
| 66 | materializeModule(); |
| 67 | freeState(); |
| 68 | Module *tempM = TheModule; |
| 69 | TheModule = 0; |
| 70 | return tempM; |
| 71 | } |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 72 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 73 | void ParseBytecode(const unsigned char *Buf, unsigned Length, |
| 74 | const std::string &ModuleID); |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 75 | |
Chris Lattner | b3afb1f | 2002-04-04 19:24:11 +0000 | [diff] [blame] | 76 | void dump() const { |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 77 | std::cerr << "BytecodeParser instance!\n"; |
Chris Lattner | b3afb1f | 2002-04-04 19:24:11 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 80 | private: // All of this data is transient across calls to ParseBytecode |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 81 | struct ValueList : public User { |
| 82 | ValueList() : User(Type::TypeTy, Value::TypeVal) { |
| 83 | } |
| 84 | ~ValueList() {} |
| 85 | |
| 86 | // vector compatibility methods |
| 87 | unsigned size() const { return getNumOperands(); } |
| 88 | void push_back(Value *V) { Operands.push_back(Use(V, this)); } |
| 89 | Value *back() const { return Operands.back(); } |
| 90 | void pop_back() { Operands.pop_back(); } |
| 91 | bool empty() const { return Operands.empty(); } |
| 92 | |
| 93 | virtual void print(std::ostream& OS) const { |
| 94 | OS << "Bytecode Reader UseHandle!"; |
| 95 | } |
| 96 | }; |
| 97 | |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 98 | // Information about the module, extracted from the bytecode revision number. |
| 99 | unsigned char RevisionNum; // The rev # itself |
| 100 | unsigned char FirstDerivedTyID; // First variable index to use for type |
| 101 | bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros? |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 102 | bool hasInternalMarkerOnly; // Only types of linkage are intern/external |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 104 | typedef std::vector<ValueList*> ValueTable; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 105 | ValueTable Values, LateResolveValues; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 106 | ValueTable ModuleValues; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 108 | // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward |
| 109 | // references to global values or constants. Such values may be referenced |
| 110 | // before they are defined, and if so, the temporary object that they |
| 111 | // represent is held here. |
| 112 | // |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 113 | typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType; |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 114 | GlobalRefsType GlobalRefs; |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 116 | // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used |
| 117 | // to deal with forward references to types. |
| 118 | // |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame] | 119 | typedef std::vector<PATypeHandle> TypeValuesListTy; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 120 | TypeValuesListTy ModuleTypeValues; |
Chris Lattner | 6e5a0e4 | 2003-03-06 17:18:14 +0000 | [diff] [blame] | 121 | TypeValuesListTy FunctionTypeValues; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 123 | // When the ModuleGlobalInfo section is read, we create a function object for |
| 124 | // each function in the module. When the function is loaded, this function is |
| 125 | // filled in. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 126 | // |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 127 | std::vector<std::pair<Function*, unsigned> > FunctionSignatureList; |
| 128 | |
| 129 | // Constant values are read in after global variables. Because of this, we |
| 130 | // must defer setting the initializers on global variables until after module |
| 131 | // level constants have been read. In the mean time, this list keeps track of |
| 132 | // what we must do. |
| 133 | // |
| 134 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 135 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 136 | // For lazy reading-in of functions, we need to save away several pieces of |
| 137 | // information about each function: its begin and end pointer in the buffer |
| 138 | // and its FunctionSlot. |
| 139 | // |
| 140 | std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap; |
| 141 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 142 | private: |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 143 | void freeTable(ValueTable &Tab) { |
| 144 | while (!Tab.empty()) { |
| 145 | delete Tab.back(); |
| 146 | Tab.pop_back(); |
| 147 | } |
| 148 | } |
| 149 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 150 | public: |
| 151 | void ParseModule(const unsigned char * Buf, const unsigned char *End); |
| 152 | void materializeFunction(Function *F); |
| 153 | |
| 154 | private: |
| 155 | void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End); |
| 156 | void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E); |
| 157 | void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End, |
| 158 | SymbolTable *); |
| 159 | void ParseFunction(const unsigned char *&Buf, const unsigned char *End); |
| 160 | void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf); |
| 161 | |
| 162 | std::auto_ptr<BasicBlock> |
| 163 | ParseBasicBlock(const unsigned char *&Buf, const unsigned char *End); |
| 164 | |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 165 | bool ParseInstruction (const unsigned char *&Buf, const unsigned char *End, |
Chris Lattner | 09bd025 | 2003-09-08 18:04:16 +0000 | [diff] [blame] | 166 | Instruction *&); |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 167 | bool ParseRawInst (const unsigned char *&Buf, const unsigned char *End, |
| 168 | RawInst &); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 169 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 170 | void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf, |
| 171 | ValueTable &Tab, TypeValuesListTy &TypeTab); |
| 172 | void parseConstantValue(const unsigned char *&Buf, const unsigned char *End, |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 173 | const Type *Ty, Constant *&V); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 174 | void parseTypeConstants(const unsigned char *&Buf, |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 175 | const unsigned char *EndBuf, |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 176 | TypeValuesListTy &Tab, unsigned NumEntries); |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 177 | const Type *parseTypeConstant(const unsigned char *&Buf, |
| 178 | const unsigned char *EndBuf); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 179 | |
| 180 | Value *getValue(const Type *Ty, unsigned num, bool Create = true); |
| 181 | const Type *getType(unsigned ID); |
Chris Lattner | bbd4b30 | 2002-10-14 03:33:02 +0000 | [diff] [blame] | 182 | Constant *getConstantValue(const Type *Ty, unsigned num); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 184 | int insertValue(Value *V, ValueTable &Table); // -1 = Failure |
| 185 | void setValueTo(ValueTable &D, unsigned Slot, Value *V); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 186 | void postResolveValues(ValueTable &ValTab); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 187 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 188 | void getTypeSlot(const Type *Ty, unsigned &Slot); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 190 | // resolve all references to the placeholder (if any) for the given value |
| 191 | void ResolveReferencesToValue(Value *Val, unsigned Slot); |
| 192 | |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 194 | // refineAbstractType - The callback method is invoked when one of the |
| 195 | // elements of TypeValues becomes more concrete... |
| 196 | // |
| 197 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | template<class SuperType> |
| 201 | class PlaceholderDef : public SuperType { |
| 202 | unsigned ID; |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 203 | PlaceholderDef(); // DO NOT IMPLEMENT |
| 204 | void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | public: |
| 206 | PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {} |
| 207 | unsigned getID() { return ID; } |
| 208 | }; |
| 209 | |
| 210 | struct InstPlaceHolderHelper : public Instruction { |
| 211 | InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {} |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 212 | virtual const char *getOpcodeName() const { return "placeholder"; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 213 | |
| 214 | virtual Instruction *clone() const { abort(); return 0; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | struct BBPlaceHolderHelper : public BasicBlock { |
| 218 | BBPlaceHolderHelper(const Type *Ty) : BasicBlock() { |
Chris Lattner | 35e309a | 2002-04-08 21:59:36 +0000 | [diff] [blame] | 219 | assert(Ty == Type::LabelTy); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 220 | } |
| 221 | }; |
| 222 | |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame] | 223 | struct ConstantPlaceHolderHelper : public Constant { |
| 224 | ConstantPlaceHolderHelper(const Type *Ty) |
| 225 | : Constant(Ty) {} |
| 226 | virtual bool isNullValue() const { return false; } |
| 227 | }; |
| 228 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 229 | typedef PlaceholderDef<InstPlaceHolderHelper> ValPHolder; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 230 | typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder; |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame] | 231 | typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder; |
| 232 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 233 | // Some common errors we find |
| 234 | static const std::string Error_readvbr = "read_vbr(): error reading."; |
| 235 | static const std::string Error_read = "read(): error reading."; |
| 236 | static const std::string Error_inputdata = "input_data(): error reading."; |
| 237 | static const std::string Error_DestSlot = "No destination slot found."; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 239 | static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) { |
| 240 | if (isa<Constant>(Val)) |
| 241 | return ((ConstPHolder*)Val)->getID(); |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame] | 242 | |
| 243 | // else discriminate by type |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 244 | switch (Val->getType()->getPrimitiveID()) { |
| 245 | case Type::LabelTyID: return ((BBPHolder*)Val)->getID(); |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 246 | default: return ((ValPHolder*)Val)->getID(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 250 | static inline void readBlock(const unsigned char *&Buf, |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 251 | const unsigned char *EndBuf, |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 252 | unsigned &Type, unsigned &Size) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 253 | #if DEBUG_OUTPUT |
| 254 | bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size); |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 255 | std::cerr << "StartLoc = " << ((unsigned)Buf & 4095) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 256 | << " Type = " << Type << " Size = " << Size << endl; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 257 | if (Result) throw Error_read; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 258 | #else |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 259 | if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 260 | #endif |
| 261 | } |
| 262 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 263 | #endif |