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 | |
Chris Lattner | 3446ae8 | 2004-01-10 19:00:15 +0000 | [diff] [blame^] | 17 | #include "ReaderPrimitives.h" |
Chris Lattner | 8dc6ba9 | 2003-11-14 06:38:46 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 19 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 21 | #include "llvm/ModuleProvider.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 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | namespace llvm { |
| 26 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 27 | // 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] | 28 | //#define TRACE_LEVEL 10 |
Alkis Evlogimenos | 60048b8 | 2003-10-30 18:33:58 +0000 | [diff] [blame] | 29 | //#define DEBUG_OUTPUT |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 30 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 31 | #if TRACE_LEVEL // ByteCodeReading_TRACEr |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 32 | #define BCR_TRACE(n, X) \ |
| 33 | if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 34 | #else |
| 35 | #define BCR_TRACE(n, X) |
| 36 | #endif |
| 37 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 38 | struct LazyFunctionInfo { |
| 39 | const unsigned char *Buf, *EndBuf; |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 40 | LazyFunctionInfo(const unsigned char *B = 0, const unsigned char *EB = 0) |
| 41 | : Buf(B), EndBuf(EB) {} |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 44 | class BytecodeParser : public ModuleProvider { |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 45 | BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT |
| 46 | void operator=(const BytecodeParser &); // DO NOT IMPLEMENT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | public: |
Misha Brukman | 96f7877 | 2003-09-22 23:58:08 +0000 | [diff] [blame] | 48 | BytecodeParser() { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | // Define this in case we don't see a ModuleGlobalInfo block. |
| 50 | FirstDerivedTyID = Type::FirstDerivedTyID; |
| 51 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 53 | ~BytecodeParser() { |
Chris Lattner | a2602f3 | 2003-05-22 18:26:48 +0000 | [diff] [blame] | 54 | freeState(); |
| 55 | } |
| 56 | void freeState() { |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 57 | freeTable(Values); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 58 | freeTable(ModuleValues); |
| 59 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 61 | Module* releaseModule() { |
| 62 | // 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] | 63 | Module *M = ModuleProvider::releaseModule(); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 64 | freeState(); |
Chris Lattner | 927b185 | 2003-10-09 20:22:47 +0000 | [diff] [blame] | 65 | return M; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 67 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 68 | void ParseBytecode(const unsigned char *Buf, unsigned Length, |
| 69 | const std::string &ModuleID); |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 70 | |
Chris Lattner | b3afb1f | 2002-04-04 19:24:11 +0000 | [diff] [blame] | 71 | void dump() const { |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 72 | std::cerr << "BytecodeParser instance!\n"; |
Chris Lattner | b3afb1f | 2002-04-04 19:24:11 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | 6e44802 | 2003-10-08 21:51:46 +0000 | [diff] [blame] | 75 | private: |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 76 | struct ValueList : public User { |
Chris Lattner | 6e44802 | 2003-10-08 21:51:46 +0000 | [diff] [blame] | 77 | ValueList() : User(Type::TypeTy, Value::TypeVal) {} |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 78 | |
| 79 | // vector compatibility methods |
| 80 | unsigned size() const { return getNumOperands(); } |
| 81 | void push_back(Value *V) { Operands.push_back(Use(V, this)); } |
| 82 | Value *back() const { return Operands.back(); } |
| 83 | void pop_back() { Operands.pop_back(); } |
| 84 | bool empty() const { return Operands.empty(); } |
| 85 | |
| 86 | virtual void print(std::ostream& OS) const { |
| 87 | OS << "Bytecode Reader UseHandle!"; |
| 88 | } |
| 89 | }; |
| 90 | |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 91 | // Information about the module, extracted from the bytecode revision number. |
| 92 | unsigned char RevisionNum; // The rev # itself |
| 93 | unsigned char FirstDerivedTyID; // First variable index to use for type |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 94 | bool hasInternalMarkerOnly; // Only types of linkage are intern/external |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 95 | bool hasExtendedLinkageSpecs; // Supports more than 4 linkage types |
| 96 | bool hasOldStyleVarargs; // Has old version of varargs intrinsics? |
| 97 | bool hasVarArgCallPadding; // Bytecode has extra padding in vararg call |
| 98 | |
| 99 | bool usesOldStyleVarargs; // Does this module USE old style varargs? |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 101 | typedef std::vector<ValueList*> ValueTable; |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 102 | ValueTable Values; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 103 | ValueTable ModuleValues; |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 104 | std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 106 | std::vector<BasicBlock*> ParsedBasicBlocks; |
| 107 | |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 108 | // ConstantFwdRefs - This maintains a mapping between <Type, Slot #>'s and |
| 109 | // forward references to constants. Such values may be referenced before they |
| 110 | // are defined, and if so, the temporary object that they represent is held |
| 111 | // here. |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 112 | // |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 113 | typedef std::map<std::pair<const Type*,unsigned>, Constant*> ConstantRefsType; |
| 114 | ConstantRefsType ConstantFwdRefs; |
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 | c7b6f03 | 2003-10-02 20:26:18 +0000 | [diff] [blame] | 119 | typedef std::vector<PATypeHolder> 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 | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 127 | std::vector<Function*> FunctionSignatureList; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 140 | std::map<Function*, LazyFunctionInfo> LazyFunctionLoadMap; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 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, |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 158 | SymbolTable *, Function *CurrentFunction); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 159 | void ParseFunction(const unsigned char *&Buf, const unsigned char *End); |
| 160 | void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf); |
| 161 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 162 | BasicBlock *ParseBasicBlock(const unsigned char *&Buf, |
| 163 | const unsigned char *End, |
| 164 | unsigned BlockNo); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 165 | unsigned ParseInstructionList(Function *F, const unsigned char *&Buf, |
| 166 | const unsigned char *EndBuf); |
| 167 | |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 168 | void ParseInstruction(const unsigned char *&Buf, const unsigned char *End, |
| 169 | std::vector<unsigned> &Args, BasicBlock *BB); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 170 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 171 | void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf, |
| 172 | ValueTable &Tab, TypeValuesListTy &TypeTab); |
Chris Lattner | 9e460f2 | 2003-10-04 20:00:03 +0000 | [diff] [blame] | 173 | Constant *parseConstantValue(const unsigned char *&Buf, |
| 174 | const unsigned char *End, |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 175 | unsigned TypeID); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 176 | void parseTypeConstants(const unsigned char *&Buf, |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 177 | const unsigned char *EndBuf, |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 178 | TypeValuesListTy &Tab, unsigned NumEntries); |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 179 | const Type *parseTypeConstant(const unsigned char *&Buf, |
| 180 | const unsigned char *EndBuf); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 182 | Value *getValue(unsigned TypeID, unsigned num, bool Create = true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 183 | const Type *getType(unsigned ID); |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 184 | BasicBlock *getBasicBlock(unsigned ID); |
Chris Lattner | 1c3673b | 2003-11-19 06:01:12 +0000 | [diff] [blame] | 185 | Constant *getConstantValue(unsigned TypeID, unsigned num); |
| 186 | Constant *getConstantValue(const Type *Ty, unsigned num) { |
| 187 | return getConstantValue(getTypeSlot(Ty), num); |
| 188 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 189 | |
Chris Lattner | f0d9273 | 2003-10-13 14:34:59 +0000 | [diff] [blame] | 190 | unsigned insertValue(Value *V, unsigned Type, ValueTable &Table); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 9e460f2 | 2003-10-04 20:00:03 +0000 | [diff] [blame] | 192 | unsigned getTypeSlot(const Type *Ty); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 194 | // resolve all references to the placeholder (if any) for the given constant |
| 195 | void ResolveReferencesToConstant(Constant *C, unsigned Slot); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | template<class SuperType> |
| 199 | class PlaceholderDef : public SuperType { |
| 200 | unsigned ID; |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 201 | PlaceholderDef(); // DO NOT IMPLEMENT |
| 202 | void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 203 | public: |
| 204 | PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {} |
| 205 | unsigned getID() { return ID; } |
| 206 | }; |
| 207 | |
Chris Lattner | 8dc6ba9 | 2003-11-14 06:38:46 +0000 | [diff] [blame] | 208 | struct ConstantPlaceHolderHelper : public ConstantExpr { |
Chris Lattner | db9546e | 2003-11-14 16:34:25 +0000 | [diff] [blame] | 209 | ConstantPlaceHolderHelper(const Type *Ty) |
| 210 | : ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {} |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Vikram S. Adve | c668b7c | 2002-07-14 23:05:09 +0000 | [diff] [blame] | 213 | typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder; |
| 214 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 215 | // Some common errors we find |
| 216 | static const std::string Error_readvbr = "read_vbr(): error reading."; |
| 217 | static const std::string Error_read = "read(): error reading."; |
| 218 | static const std::string Error_inputdata = "input_data(): error reading."; |
| 219 | static const std::string Error_DestSlot = "No destination slot found."; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 220 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 221 | static inline void readBlock(const unsigned char *&Buf, |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 222 | const unsigned char *EndBuf, |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 223 | unsigned &Type, unsigned &Size) { |
Alkis Evlogimenos | 60048b8 | 2003-10-30 18:33:58 +0000 | [diff] [blame] | 224 | #ifdef DEBUG_OUTPUT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 225 | bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size); |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 226 | std::cerr << "StartLoc = " << ((unsigned)Buf & 4095) |
Chris Lattner | 095be96 | 2003-11-05 19:53:32 +0000 | [diff] [blame] | 227 | << " Type = " << Type << " Size = " << Size << "\n"; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 228 | if (Result) throw Error_read; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 229 | #else |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 230 | if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 231 | #endif |
| 232 | } |
| 233 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 234 | } // End llvm namespace |
| 235 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 236 | #endif |