Chris Lattner | d9113c7 | 2003-10-13 04:22:07 +0000 | [diff] [blame] | 1 | //===-- ReaderInternals.h - Definitions internal to the reader --*- C++ -*-===// |
John Criswell | 856ba76 | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This header file defines various stuff that is used by the bytecode reader. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef READER_INTERNALS_H |
| 15 | #define READER_INTERNALS_H |
| 16 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 17 | #include "llvm/Constant.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 20 | #include "llvm/ModuleProvider.h" |
| 21 | #include "llvm/Bytecode/Primitives.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | #include <utility> |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 23 | #include <map> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 25 | // 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] | 26 | //#define TRACE_LEVEL 10 |
Alkis Evlogimenos | 60048b8 | 2003-10-30 18:33:58 +0000 | [diff] [blame^] | 27 | //#define DEBUG_OUTPUT |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 28 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 29 | #if TRACE_LEVEL // ByteCodeReading_TRACEr |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 30 | #define BCR_TRACE(n, X) \ |
| 31 | if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 32 | #else |
| 33 | #define BCR_TRACE(n, X) |
| 34 | #endif |
| 35 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 36 | struct LazyFunctionInfo { |
| 37 | const unsigned char *Buf, *EndBuf; |
| 38 | unsigned FunctionSlot; |
| 39 | }; |
| 40 | |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 41 | class BytecodeParser : public ModuleProvider { |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 42 | BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT |
| 43 | void operator=(const BytecodeParser &); // DO NOT IMPLEMENT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 44 | public: |
Misha Brukman | 96f7877 | 2003-09-22 23:58:08 +0000 | [diff] [blame] | 45 | BytecodeParser() { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | // Define this in case we don't see a ModuleGlobalInfo block. |
| 47 | FirstDerivedTyID = Type::FirstDerivedTyID; |
| 48 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 50 | ~BytecodeParser() { |
Chris Lattner | a2602f3 | 2003-05-22 18:26:48 +0000 | [diff] [blame] | 51 | freeState(); |
| 52 | } |
| 53 | void freeState() { |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 54 | freeTable(Values); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 55 | freeTable(ModuleValues); |
| 56 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 57 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 58 | Module* releaseModule() { |
| 59 | // Since we're losing control of this Module, we must hand it back complete |
Chris Lattner | 927b185 | 2003-10-09 20:22:47 +0000 | [diff] [blame] | 60 | Module *M = ModuleProvider::releaseModule(); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 61 | freeState(); |
Chris Lattner | 927b185 | 2003-10-09 20:22:47 +0000 | [diff] [blame] | 62 | return M; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 64 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 65 | void ParseBytecode(const unsigned char *Buf, unsigned Length, |
| 66 | const std::string &ModuleID); |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 67 | |
Chris Lattner | b3afb1f | 2002-04-04 19:24:11 +0000 | [diff] [blame] | 68 | void dump() const { |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 69 | std::cerr << "BytecodeParser instance!\n"; |
Chris Lattner | b3afb1f | 2002-04-04 19:24:11 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 6e44802 | 2003-10-08 21:51:46 +0000 | [diff] [blame] | 72 | private: |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 73 | struct ValueList : public User { |
Chris Lattner | 6e44802 | 2003-10-08 21:51:46 +0000 | [diff] [blame] | 74 | ValueList() : User(Type::TypeTy, Value::TypeVal) {} |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 75 | |
| 76 | // vector compatibility methods |
| 77 | unsigned size() const { return getNumOperands(); } |
| 78 | void push_back(Value *V) { Operands.push_back(Use(V, this)); } |
| 79 | Value *back() const { return Operands.back(); } |
| 80 | void pop_back() { Operands.pop_back(); } |
| 81 | bool empty() const { return Operands.empty(); } |
| 82 | |
| 83 | virtual void print(std::ostream& OS) const { |
| 84 | OS << "Bytecode Reader UseHandle!"; |
| 85 | } |
| 86 | }; |
| 87 | |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 88 | // Information about the module, extracted from the bytecode revision number. |
| 89 | unsigned char RevisionNum; // The rev # itself |
| 90 | unsigned char FirstDerivedTyID; // First variable index to use for type |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 91 | bool hasInternalMarkerOnly; // Only types of linkage are intern/external |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 92 | bool hasExtendedLinkageSpecs; // Supports more than 4 linkage types |
| 93 | bool hasOldStyleVarargs; // Has old version of varargs intrinsics? |
| 94 | bool hasVarArgCallPadding; // Bytecode has extra padding in vararg call |
| 95 | |
| 96 | bool usesOldStyleVarargs; // Does this module USE old style varargs? |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 98 | typedef std::vector<ValueList*> ValueTable; |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 99 | ValueTable Values; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 100 | ValueTable ModuleValues; |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 101 | std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 103 | std::vector<BasicBlock*> ParsedBasicBlocks; |
| 104 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 105 | // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward |
| 106 | // references to global values or constants. Such values may be referenced |
| 107 | // before they are defined, and if so, the temporary object that they |
| 108 | // represent is held here. |
| 109 | // |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 110 | typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType; |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 111 | GlobalRefsType GlobalRefs; |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 113 | // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used |
| 114 | // to deal with forward references to types. |
| 115 | // |
Chris Lattner | c7b6f03 | 2003-10-02 20:26:18 +0000 | [diff] [blame] | 116 | typedef std::vector<PATypeHolder> TypeValuesListTy; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 117 | TypeValuesListTy ModuleTypeValues; |
Chris Lattner | 6e5a0e4 | 2003-03-06 17:18:14 +0000 | [diff] [blame] | 118 | TypeValuesListTy FunctionTypeValues; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 120 | // When the ModuleGlobalInfo section is read, we create a function object for |
| 121 | // each function in the module. When the function is loaded, this function is |
| 122 | // filled in. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 123 | // |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 124 | std::vector<std::pair<Function*, unsigned> > FunctionSignatureList; |
| 125 | |
| 126 | // Constant values are read in after global variables. Because of this, we |
| 127 | // must defer setting the initializers on global variables until after module |
| 128 | // level constants have been read. In the mean time, this list keeps track of |
| 129 | // what we must do. |
| 130 | // |
| 131 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 132 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 133 | // For lazy reading-in of functions, we need to save away several pieces of |
| 134 | // information about each function: its begin and end pointer in the buffer |
| 135 | // and its FunctionSlot. |
| 136 | // |
| 137 | std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap; |
| 138 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 139 | private: |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 140 | void freeTable(ValueTable &Tab) { |
| 141 | while (!Tab.empty()) { |
| 142 | delete Tab.back(); |
| 143 | Tab.pop_back(); |
| 144 | } |
| 145 | } |
| 146 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 147 | public: |
| 148 | void ParseModule(const unsigned char * Buf, const unsigned char *End); |
| 149 | void materializeFunction(Function *F); |
| 150 | |
| 151 | private: |
| 152 | void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End); |
| 153 | void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E); |
| 154 | void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End, |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 155 | SymbolTable *, Function *CurrentFunction); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 156 | void ParseFunction(const unsigned char *&Buf, const unsigned char *End); |
| 157 | void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf); |
| 158 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 159 | BasicBlock *ParseBasicBlock(const unsigned char *&Buf, |
| 160 | const unsigned char *End, |
| 161 | unsigned BlockNo); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 162 | |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 163 | void ParseInstruction(const unsigned char *&Buf, const unsigned char *End, |
| 164 | std::vector<unsigned> &Args, BasicBlock *BB); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 165 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 166 | void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf, |
| 167 | ValueTable &Tab, TypeValuesListTy &TypeTab); |
Chris Lattner | 9e460f2 | 2003-10-04 20:00:03 +0000 | [diff] [blame] | 168 | Constant *parseConstantValue(const unsigned char *&Buf, |
| 169 | const unsigned char *End, |
| 170 | const Type *Ty); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 171 | void parseTypeConstants(const unsigned char *&Buf, |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 172 | const unsigned char *EndBuf, |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 173 | TypeValuesListTy &Tab, unsigned NumEntries); |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 174 | const Type *parseTypeConstant(const unsigned char *&Buf, |
| 175 | const unsigned char *EndBuf); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 176 | |
| 177 | Value *getValue(const Type *Ty, unsigned num, bool Create = true); |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 178 | Value *getValue(unsigned TypeID, unsigned num, bool Create = true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 179 | const Type *getType(unsigned ID); |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 180 | BasicBlock *getBasicBlock(unsigned ID); |
Chris Lattner | bbd4b30 | 2002-10-14 03:33:02 +0000 | [diff] [blame] | 181 | Constant *getConstantValue(const Type *Ty, unsigned num); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 927b185 | 2003-10-09 20:22:47 +0000 | [diff] [blame] | 183 | unsigned insertValue(Value *V, ValueTable &Table); |
Chris Lattner | f0d9273 | 2003-10-13 14:34:59 +0000 | [diff] [blame] | 184 | unsigned insertValue(Value *V, unsigned Type, ValueTable &Table); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 9e460f2 | 2003-10-04 20:00:03 +0000 | [diff] [blame] | 186 | unsigned getTypeSlot(const Type *Ty); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 188 | // resolve all references to the placeholder (if any) for the given value |
| 189 | void ResolveReferencesToValue(Value *Val, unsigned Slot); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | template<class SuperType> |
| 193 | class PlaceholderDef : public SuperType { |
| 194 | unsigned ID; |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 195 | PlaceholderDef(); // DO NOT IMPLEMENT |
| 196 | void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 197 | public: |
| 198 | PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {} |
| 199 | unsigned getID() { return ID; } |
| 200 | }; |
| 201 | |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame] | 202 | struct ConstantPlaceHolderHelper : public Constant { |
| 203 | ConstantPlaceHolderHelper(const Type *Ty) |
| 204 | : Constant(Ty) {} |
| 205 | virtual bool isNullValue() const { return false; } |
| 206 | }; |
| 207 | |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame] | 208 | typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder; |
| 209 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 210 | // Some common errors we find |
| 211 | static const std::string Error_readvbr = "read_vbr(): error reading."; |
| 212 | static const std::string Error_read = "read(): error reading."; |
| 213 | static const std::string Error_inputdata = "input_data(): error reading."; |
| 214 | static const std::string Error_DestSlot = "No destination slot found."; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 215 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 216 | static inline void readBlock(const unsigned char *&Buf, |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 217 | const unsigned char *EndBuf, |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 218 | unsigned &Type, unsigned &Size) { |
Alkis Evlogimenos | 60048b8 | 2003-10-30 18:33:58 +0000 | [diff] [blame^] | 219 | #ifdef DEBUG_OUTPUT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 220 | bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size); |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 221 | std::cerr << "StartLoc = " << ((unsigned)Buf & 4095) |
Alkis Evlogimenos | 60048b8 | 2003-10-30 18:33:58 +0000 | [diff] [blame^] | 222 | << " Type = " << Type << " Size = " << Size << std::endl; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 223 | if (Result) throw Error_read; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 224 | #else |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 225 | if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 226 | #endif |
| 227 | } |
| 228 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 229 | #endif |