Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 1 | //===- Reader.cpp - Code to read bytecode files ---------------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +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 library implements the functionality defined in llvm/Bytecode/Reader.h |
| 11 | // |
| 12 | // Note that this library should be as fast as possible, reentrant, and |
| 13 | // threadsafe!! |
| 14 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | // TODO: Allow passing in an option to ignore the symbol table |
| 16 | // |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 19 | #include "Reader.h" |
| 20 | #include "llvm/Bytecode/BytecodeHandler.h" |
| 21 | #include "llvm/BasicBlock.h" |
| 22 | #include "llvm/Constants.h" |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 23 | #include "llvm/Instructions.h" |
| 24 | #include "llvm/SymbolTable.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | #include "llvm/Bytecode/Format.h" |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 26 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 27 | #include "Support/StringExtras.h" |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 28 | #include <sstream> |
| 29 | |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 32 | /// A convenience macro for handling parsing errors. |
| 33 | #define PARSE_ERROR(inserters) { \ |
| 34 | std::ostringstream errormsg; \ |
| 35 | errormsg << inserters; \ |
| 36 | throw std::string(errormsg.str()); \ |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 39 | /// @brief A class for maintaining the slot number definition |
| 40 | /// as a placeholder for the actual definition. |
| 41 | template<class SuperType> |
| 42 | class PlaceholderDef : public SuperType { |
| 43 | unsigned ID; |
| 44 | PlaceholderDef(); // DO NOT IMPLEMENT |
| 45 | void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT |
| 46 | public: |
| 47 | PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {} |
| 48 | unsigned getID() { return ID; } |
| 49 | }; |
Chris Lattner | 9e460f2 | 2003-10-04 20:00:03 +0000 | [diff] [blame] | 50 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 51 | struct ConstantPlaceHolderHelper : public ConstantExpr { |
| 52 | ConstantPlaceHolderHelper(const Type *Ty) |
| 53 | : ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {} |
| 54 | }; |
| 55 | |
| 56 | typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder; |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | // Bytecode Reading Methods |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 62 | /// Determine if the current block being read contains any more data. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 63 | inline bool BytecodeReader::moreInBlock() { |
| 64 | return At < BlockEnd; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 67 | /// Throw an error if we've read past the end of the current block |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 68 | inline void BytecodeReader::checkPastBlockEnd(const char * block_name) { |
| 69 | if ( At > BlockEnd ) |
| 70 | PARSE_ERROR("Attempt to read past the end of " << block_name << " block."); |
| 71 | } |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 72 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 73 | /// Align the buffer position to a 32 bit boundary |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 74 | inline void BytecodeReader::align32() { |
| 75 | BufPtr Save = At; |
| 76 | At = (const unsigned char *)((unsigned long)(At+3) & (~3UL)); |
| 77 | if ( At > Save ) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 78 | if (Handler) Handler->handleAlignment( At - Save ); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 79 | if (At > BlockEnd) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 80 | throw std::string("Ran out of data while aligning!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 83 | /// Read a whole unsigned integer |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 84 | inline unsigned BytecodeReader::read_uint() { |
| 85 | if (At+4 > BlockEnd) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 86 | throw std::string("Ran out of data reading uint!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 87 | At += 4; |
| 88 | return At[-4] | (At[-3] << 8) | (At[-2] << 16) | (At[-1] << 24); |
| 89 | } |
| 90 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 91 | /// Read a variable-bit-rate encoded unsigned integer |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 92 | inline unsigned BytecodeReader::read_vbr_uint() { |
| 93 | unsigned Shift = 0; |
| 94 | unsigned Result = 0; |
| 95 | BufPtr Save = At; |
| 96 | |
| 97 | do { |
| 98 | if (At == BlockEnd) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 99 | throw std::string("Ran out of data reading vbr_uint!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 100 | Result |= (unsigned)((*At++) & 0x7F) << Shift; |
| 101 | Shift += 7; |
| 102 | } while (At[-1] & 0x80); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 103 | if (Handler) Handler->handleVBR32(At-Save); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 104 | return Result; |
| 105 | } |
| 106 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 107 | /// Read a variable-bit-rate encoded unsigned 64-bit integer. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 108 | inline uint64_t BytecodeReader::read_vbr_uint64() { |
| 109 | unsigned Shift = 0; |
| 110 | uint64_t Result = 0; |
| 111 | BufPtr Save = At; |
| 112 | |
| 113 | do { |
| 114 | if (At == BlockEnd) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 115 | throw std::string("Ran out of data reading vbr_uint64!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 116 | Result |= (uint64_t)((*At++) & 0x7F) << Shift; |
| 117 | Shift += 7; |
| 118 | } while (At[-1] & 0x80); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 119 | if (Handler) Handler->handleVBR64(At-Save); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 120 | return Result; |
| 121 | } |
| 122 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 123 | /// Read a variable-bit-rate encoded signed 64-bit integer. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 124 | inline int64_t BytecodeReader::read_vbr_int64() { |
| 125 | uint64_t R = read_vbr_uint64(); |
| 126 | if (R & 1) { |
| 127 | if (R != 1) |
| 128 | return -(int64_t)(R >> 1); |
| 129 | else // There is no such thing as -0 with integers. "-0" really means |
| 130 | // 0x8000000000000000. |
| 131 | return 1LL << 63; |
| 132 | } else |
| 133 | return (int64_t)(R >> 1); |
| 134 | } |
| 135 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 136 | /// Read a pascal-style string (length followed by text) |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 137 | inline std::string BytecodeReader::read_str() { |
| 138 | unsigned Size = read_vbr_uint(); |
| 139 | const unsigned char *OldAt = At; |
| 140 | At += Size; |
| 141 | if (At > BlockEnd) // Size invalid? |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 142 | throw std::string("Ran out of data reading a string!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 143 | return std::string((char*)OldAt, Size); |
| 144 | } |
| 145 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 146 | /// Read an arbitrary block of data |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 147 | inline void BytecodeReader::read_data(void *Ptr, void *End) { |
| 148 | unsigned char *Start = (unsigned char *)Ptr; |
| 149 | unsigned Amount = (unsigned char *)End - Start; |
| 150 | if (At+Amount > BlockEnd) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 151 | throw std::string("Ran out of data!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 152 | std::copy(At, At+Amount, Start); |
| 153 | At += Amount; |
| 154 | } |
| 155 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 156 | /// Read a block header and obtain its type and size |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 157 | inline void BytecodeReader::read_block(unsigned &Type, unsigned &Size) { |
| 158 | Type = read_uint(); |
| 159 | Size = read_uint(); |
| 160 | BlockStart = At; |
| 161 | if ( At + Size > BlockEnd ) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 162 | throw std::string("Attempt to size a block past end of memory"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 163 | BlockEnd = At + Size; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 164 | if (Handler) Handler->handleBlock( Type, BlockStart, Size ); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /// In LLVM 1.2 and before, Types were derived from Value and so they were |
| 169 | /// written as part of the type planes along with any other Value. In LLVM |
| 170 | /// 1.3 this changed so that Type does not derive from Value. Consequently, |
| 171 | /// the BytecodeReader's containers for Values can't contain Types because |
| 172 | /// there's no inheritance relationship. This means that the "Type Type" |
| 173 | /// plane is defunct along with the Type::TypeTyID TypeID. In LLVM 1.3 |
| 174 | /// whenever a bytecode construct must have both types and values together, |
| 175 | /// the types are always read/written first and then the Values. Furthermore |
| 176 | /// since Type::TypeTyID no longer exists, its value (12) now corresponds to |
| 177 | /// Type::LabelTyID. In order to overcome this we must "sanitize" all the |
| 178 | /// type TypeIDs we encounter. For LLVM 1.3 bytecode files, there's no change. |
| 179 | /// For LLVM 1.2 and before, this function will decrement the type id by |
| 180 | /// one to account for the missing Type::TypeTyID enumerator if the value is |
| 181 | /// larger than 12 (Type::LabelTyID). If the value is exactly 12, then this |
| 182 | /// function returns true, otherwise false. This helps detect situations |
| 183 | /// where the pre 1.3 bytecode is indicating that what follows is a type. |
| 184 | /// @returns true iff type id corresponds to pre 1.3 "type type" |
| 185 | inline bool BytecodeReader::sanitizeTypeId(unsigned &TypeId ) { |
| 186 | if ( hasTypeDerivedFromValue ) { /// do nothing if 1.3 or later |
| 187 | if ( TypeId == Type::LabelTyID ) { |
| 188 | TypeId = Type::VoidTyID; // sanitize it |
| 189 | return true; // indicate we got TypeTyID in pre 1.3 bytecode |
| 190 | } else if ( TypeId > Type::LabelTyID ) |
| 191 | --TypeId; // shift all planes down because type type plane is missing |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | /// Reads a vbr uint to read in a type id and does the necessary |
| 197 | /// conversion on it by calling sanitizeTypeId. |
| 198 | /// @returns true iff \p TypeId read corresponds to a pre 1.3 "type type" |
| 199 | /// @see sanitizeTypeId |
| 200 | inline bool BytecodeReader::read_typeid(unsigned &TypeId) { |
| 201 | TypeId = read_vbr_uint(); |
| 202 | return sanitizeTypeId(TypeId); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | // IR Lookup Methods |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 209 | /// Determine if a type id has an implicit null value |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 210 | inline bool BytecodeReader::hasImplicitNull(unsigned TyID ) { |
| 211 | if (!hasExplicitPrimitiveZeros) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 212 | return TyID != Type::LabelTyID && TyID != Type::VoidTyID; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 213 | return TyID >= Type::FirstDerivedTyID; |
| 214 | } |
| 215 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 216 | /// Obtain a type given a typeid and account for things like compaction tables, |
| 217 | /// function level vs module level, and the offsetting for the primitive types. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 218 | const Type *BytecodeReader::getType(unsigned ID) { |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 219 | if (ID < Type::FirstDerivedTyID) |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 220 | if (const Type *T = Type::getPrimitiveType((Type::TypeID)ID)) |
Chris Lattner | 927b185 | 2003-10-09 20:22:47 +0000 | [diff] [blame] | 221 | return T; // Asked for a primitive type... |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 222 | |
| 223 | // Otherwise, derived types need offset... |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 224 | ID -= Type::FirstDerivedTyID; |
| 225 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 226 | if (!CompactionTypes.empty()) { |
| 227 | if (ID >= CompactionTypes.size()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 228 | throw std::string("Type ID out of range for compaction table!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 229 | return CompactionTypes[ID]; |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 230 | } |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 231 | |
| 232 | // Is it a module-level type? |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 233 | if (ID < ModuleTypes.size()) |
| 234 | return ModuleTypes[ID].get(); |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 235 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 236 | // Nope, is it a function-level type? |
| 237 | ID -= ModuleTypes.size(); |
| 238 | if (ID < FunctionTypes.size()) |
| 239 | return FunctionTypes[ID].get(); |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 240 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 241 | throw std::string("Illegal type reference!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 242 | return Type::VoidTy; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 245 | /// Get a sanitized type id. This just makes sure that the \p ID |
| 246 | /// is both sanitized and not the "type type" of pre-1.3 bytecode. |
| 247 | /// @see sanitizeTypeId |
| 248 | inline const Type* BytecodeReader::getSanitizedType(unsigned& ID) { |
| 249 | bool isTypeType = sanitizeTypeId(ID); |
| 250 | assert(!isTypeType && "Invalid type id occurred"); |
| 251 | return getType(ID); |
| 252 | } |
| 253 | |
| 254 | /// This method just saves some coding. It uses read_typeid to read |
| 255 | /// in a sanitized type id, asserts that its not the type type, and |
| 256 | /// then calls getType to return the type value. |
| 257 | inline const Type* BytecodeReader::readSanitizedType() { |
| 258 | unsigned ID; |
| 259 | bool isTypeType = read_typeid(ID); |
| 260 | assert(!isTypeType && "Invalid type id occurred"); |
| 261 | return getType(ID); |
| 262 | } |
| 263 | |
| 264 | /// Get the slot number associated with a type accounting for primitive |
| 265 | /// types, compaction tables, and function level vs module level. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 266 | unsigned BytecodeReader::getTypeSlot(const Type *Ty) { |
| 267 | if (Ty->isPrimitiveType()) |
| 268 | return Ty->getTypeID(); |
| 269 | |
| 270 | // Scan the compaction table for the type if needed. |
| 271 | if (!CompactionTypes.empty()) { |
| 272 | std::vector<const Type*>::const_iterator I = |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 273 | find(CompactionTypes.begin(), CompactionTypes.end(), Ty); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 274 | |
| 275 | if (I == CompactionTypes.end()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 276 | throw std::string("Couldn't find type specified in compaction table!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 277 | return Type::FirstDerivedTyID + (&*I - &CompactionTypes[0]); |
| 278 | } |
| 279 | |
| 280 | // Check the function level types first... |
| 281 | TypeListTy::iterator I = find(FunctionTypes.begin(), FunctionTypes.end(), Ty); |
| 282 | |
| 283 | if (I != FunctionTypes.end()) |
| 284 | return Type::FirstDerivedTyID + ModuleTypes.size() + |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 285 | (&*I - &FunctionTypes[0]); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 286 | |
| 287 | // Check the module level types now... |
| 288 | I = find(ModuleTypes.begin(), ModuleTypes.end(), Ty); |
| 289 | if (I == ModuleTypes.end()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 290 | throw std::string("Didn't find type in ModuleTypes."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 291 | return Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]); |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 294 | /// This is just like getType, but when a compaction table is in use, it is |
| 295 | /// ignored. It also ignores function level types. |
| 296 | /// @see getType |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 297 | const Type *BytecodeReader::getGlobalTableType(unsigned Slot) { |
| 298 | if (Slot < Type::FirstDerivedTyID) { |
| 299 | const Type *Ty = Type::getPrimitiveType((Type::TypeID)Slot); |
| 300 | assert(Ty && "Not a primitive type ID?"); |
| 301 | return Ty; |
| 302 | } |
| 303 | Slot -= Type::FirstDerivedTyID; |
| 304 | if (Slot >= ModuleTypes.size()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 305 | throw std::string("Illegal compaction table type reference!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 306 | return ModuleTypes[Slot]; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 309 | /// This is just like getTypeSlot, but when a compaction table is in use, it |
| 310 | /// is ignored. It also ignores function level types. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 311 | unsigned BytecodeReader::getGlobalTableTypeSlot(const Type *Ty) { |
| 312 | if (Ty->isPrimitiveType()) |
| 313 | return Ty->getTypeID(); |
| 314 | TypeListTy::iterator I = find(ModuleTypes.begin(), |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 315 | ModuleTypes.end(), Ty); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 316 | if (I == ModuleTypes.end()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 317 | throw std::string("Didn't find type in ModuleTypes."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 318 | return Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]); |
| 319 | } |
| 320 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 321 | /// Retrieve a value of a given type and slot number, possibly creating |
| 322 | /// it if it doesn't already exist. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 323 | Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) { |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 324 | assert(type != Type::LabelTyID && "getValue() cannot get blocks!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 325 | unsigned Num = oNum; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 327 | // If there is a compaction table active, it defines the low-level numbers. |
| 328 | // If not, the module values define the low-level numbers. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 329 | if (CompactionValues.size() > type && !CompactionValues[type].empty()) { |
| 330 | if (Num < CompactionValues[type].size()) |
| 331 | return CompactionValues[type][Num]; |
| 332 | Num -= CompactionValues[type].size(); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 333 | } else { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 334 | // By default, the global type id is the type id passed in |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 335 | unsigned GlobalTyID = type; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 336 | |
| 337 | // If the type plane was compactified, figure out the global type ID |
| 338 | // by adding the derived type ids and the distance. |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 339 | if (!CompactionTypes.empty() && type >= Type::FirstDerivedTyID) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 340 | const Type *Ty = CompactionTypes[type-Type::FirstDerivedTyID]; |
| 341 | TypeListTy::iterator I = |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 342 | find(ModuleTypes.begin(), ModuleTypes.end(), Ty); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 343 | assert(I != ModuleTypes.end()); |
| 344 | GlobalTyID = Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]); |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 346 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 347 | if (hasImplicitNull(GlobalTyID)) { |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 348 | if (Num == 0) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 349 | return Constant::getNullValue(getType(type)); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 350 | --Num; |
| 351 | } |
| 352 | |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 353 | if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) { |
| 354 | if (Num < ModuleValues[GlobalTyID]->size()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 355 | return ModuleValues[GlobalTyID]->getOperand(Num); |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 356 | Num -= ModuleValues[GlobalTyID]->size(); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 357 | } |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 360 | if (FunctionValues.size() > type && |
| 361 | FunctionValues[type] && |
| 362 | Num < FunctionValues[type]->size()) |
| 363 | return FunctionValues[type]->getOperand(Num); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 365 | if (!Create) return 0; // Do not create a placeholder? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 367 | std::pair<unsigned,unsigned> KeyValue(type, oNum); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 368 | ForwardReferenceMap::iterator I = ForwardReferences.lower_bound(KeyValue); |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 369 | if (I != ForwardReferences.end() && I->first == KeyValue) |
| 370 | return I->second; // We have already created this placeholder |
| 371 | |
Chris Lattner | bf43ac6 | 2003-10-09 06:14:26 +0000 | [diff] [blame] | 372 | Value *Val = new Argument(getType(type)); |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 373 | ForwardReferences.insert(I, std::make_pair(KeyValue, Val)); |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 374 | return Val; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 377 | /// This is just like getValue, but when a compaction table is in use, it |
| 378 | /// is ignored. Also, no forward references or other fancy features are |
| 379 | /// supported. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 380 | Value* BytecodeReader::getGlobalTableValue(const Type *Ty, unsigned SlotNo) { |
| 381 | // FIXME: getTypeSlot is inefficient! |
| 382 | unsigned TyID = getGlobalTableTypeSlot(Ty); |
| 383 | |
| 384 | if (TyID != Type::LabelTyID) { |
| 385 | if (SlotNo == 0) |
| 386 | return Constant::getNullValue(Ty); |
| 387 | --SlotNo; |
| 388 | } |
| 389 | |
| 390 | if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 || |
| 391 | SlotNo >= ModuleValues[TyID]->size()) { |
| 392 | PARSE_ERROR("Corrupt compaction table entry!" |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 393 | << TyID << ", " << SlotNo << ": " << ModuleValues.size() << ", " |
| 394 | << (void*)ModuleValues[TyID] << ", " |
| 395 | << ModuleValues[TyID]->size() << "\n"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 396 | } |
| 397 | return ModuleValues[TyID]->getOperand(SlotNo); |
| 398 | } |
| 399 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 400 | /// Just like getValue, except that it returns a null pointer |
| 401 | /// only on error. It always returns a constant (meaning that if the value is |
| 402 | /// defined, but is not a constant, that is an error). If the specified |
| 403 | /// constant hasn't been parsed yet, a placeholder is defined and used. |
| 404 | /// Later, after the real value is parsed, the placeholder is eliminated. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 405 | Constant* BytecodeReader::getConstantValue(unsigned TypeSlot, unsigned Slot) { |
| 406 | if (Value *V = getValue(TypeSlot, Slot, false)) |
| 407 | if (Constant *C = dyn_cast<Constant>(V)) |
| 408 | return C; // If we already have the value parsed, just return it |
| 409 | else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 410 | // ConstantPointerRef's are an abomination, but at least they don't have |
| 411 | // to infest bytecode files. |
| 412 | return ConstantPointerRef::get(GV); |
| 413 | else |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 414 | throw std::string("Reference of a value is expected to be a constant!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 415 | |
| 416 | const Type *Ty = getType(TypeSlot); |
| 417 | std::pair<const Type*, unsigned> Key(Ty, Slot); |
| 418 | ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key); |
| 419 | |
| 420 | if (I != ConstantFwdRefs.end() && I->first == Key) { |
| 421 | return I->second; |
| 422 | } else { |
| 423 | // Create a placeholder for the constant reference and |
| 424 | // keep track of the fact that we have a forward ref to recycle it |
| 425 | Constant *C = new ConstPHolder(Ty, Slot); |
| 426 | |
| 427 | // Keep track of the fact that we have a forward ref to recycle it |
| 428 | ConstantFwdRefs.insert(I, std::make_pair(Key, C)); |
| 429 | return C; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | //===----------------------------------------------------------------------===// |
| 434 | // IR Construction Methods |
| 435 | //===----------------------------------------------------------------------===// |
| 436 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 437 | /// As values are created, they are inserted into the appropriate place |
| 438 | /// with this method. The ValueTable argument must be one of ModuleValues |
| 439 | /// or FunctionValues data members of this class. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 440 | unsigned BytecodeReader::insertValue( |
| 441 | Value *Val, unsigned type, ValueTable &ValueTab) { |
| 442 | assert((!isa<Constant>(Val) || !cast<Constant>(Val)->isNullValue()) || |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 443 | !hasImplicitNull(type) && |
| 444 | "Cannot read null values from bytecode!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 445 | |
| 446 | if (ValueTab.size() <= type) |
| 447 | ValueTab.resize(type+1); |
| 448 | |
| 449 | if (!ValueTab[type]) ValueTab[type] = new ValueList(); |
| 450 | |
| 451 | ValueTab[type]->push_back(Val); |
| 452 | |
| 453 | bool HasOffset = hasImplicitNull(type); |
| 454 | return ValueTab[type]->size()-1 + HasOffset; |
| 455 | } |
| 456 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 457 | /// Insert the arguments of a function as new values in the reader. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 458 | void BytecodeReader::insertArguments(Function* F ) { |
| 459 | const FunctionType *FT = F->getFunctionType(); |
| 460 | Function::aiterator AI = F->abegin(); |
| 461 | for (FunctionType::param_iterator It = FT->param_begin(); |
| 462 | It != FT->param_end(); ++It, ++AI) |
| 463 | insertValue(AI, getTypeSlot(AI->getType()), FunctionValues); |
| 464 | } |
| 465 | |
| 466 | //===----------------------------------------------------------------------===// |
| 467 | // Bytecode Parsing Methods |
| 468 | //===----------------------------------------------------------------------===// |
| 469 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 470 | /// This method parses a single instruction. The instruction is |
| 471 | /// inserted at the end of the \p BB provided. The arguments of |
| 472 | /// the instruction are provided in the \p Args vector. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 473 | void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds, |
| 474 | BasicBlock* BB) { |
| 475 | BufPtr SaveAt = At; |
| 476 | |
| 477 | // Clear instruction data |
| 478 | Oprnds.clear(); |
| 479 | unsigned iType = 0; |
| 480 | unsigned Opcode = 0; |
| 481 | unsigned Op = read_uint(); |
| 482 | |
| 483 | // bits Instruction format: Common to all formats |
| 484 | // -------------------------- |
| 485 | // 01-00: Opcode type, fixed to 1. |
| 486 | // 07-02: Opcode |
| 487 | Opcode = (Op >> 2) & 63; |
| 488 | Oprnds.resize((Op >> 0) & 03); |
| 489 | |
| 490 | // Extract the operands |
| 491 | switch (Oprnds.size()) { |
| 492 | case 1: |
| 493 | // bits Instruction format: |
| 494 | // -------------------------- |
| 495 | // 19-08: Resulting type plane |
| 496 | // 31-20: Operand #1 (if set to (2^12-1), then zero operands) |
| 497 | // |
| 498 | iType = (Op >> 8) & 4095; |
| 499 | Oprnds[0] = (Op >> 20) & 4095; |
| 500 | if (Oprnds[0] == 4095) // Handle special encoding for 0 operands... |
| 501 | Oprnds.resize(0); |
| 502 | break; |
| 503 | case 2: |
| 504 | // bits Instruction format: |
| 505 | // -------------------------- |
| 506 | // 15-08: Resulting type plane |
| 507 | // 23-16: Operand #1 |
| 508 | // 31-24: Operand #2 |
| 509 | // |
| 510 | iType = (Op >> 8) & 255; |
| 511 | Oprnds[0] = (Op >> 16) & 255; |
| 512 | Oprnds[1] = (Op >> 24) & 255; |
| 513 | break; |
| 514 | case 3: |
| 515 | // bits Instruction format: |
| 516 | // -------------------------- |
| 517 | // 13-08: Resulting type plane |
| 518 | // 19-14: Operand #1 |
| 519 | // 25-20: Operand #2 |
| 520 | // 31-26: Operand #3 |
| 521 | // |
| 522 | iType = (Op >> 8) & 63; |
| 523 | Oprnds[0] = (Op >> 14) & 63; |
| 524 | Oprnds[1] = (Op >> 20) & 63; |
| 525 | Oprnds[2] = (Op >> 26) & 63; |
| 526 | break; |
| 527 | case 0: |
| 528 | At -= 4; // Hrm, try this again... |
| 529 | Opcode = read_vbr_uint(); |
| 530 | Opcode >>= 2; |
| 531 | iType = read_vbr_uint(); |
| 532 | |
| 533 | unsigned NumOprnds = read_vbr_uint(); |
| 534 | Oprnds.resize(NumOprnds); |
| 535 | |
| 536 | if (NumOprnds == 0) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 537 | throw std::string("Zero-argument instruction found; this is invalid."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 538 | |
| 539 | for (unsigned i = 0; i != NumOprnds; ++i) |
| 540 | Oprnds[i] = read_vbr_uint(); |
| 541 | align32(); |
| 542 | break; |
| 543 | } |
| 544 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 545 | const Type *InstTy = getSanitizedType(iType); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 546 | |
| 547 | // Hae enough to inform the handler now |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 548 | if (Handler) Handler->handleInstruction(Opcode, InstTy, Oprnds, At-SaveAt); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 549 | |
| 550 | // Declare the resulting instruction we'll build. |
| 551 | Instruction *Result = 0; |
| 552 | |
| 553 | // Handle binary operators |
| 554 | if (Opcode >= Instruction::BinaryOpsBegin && |
| 555 | Opcode < Instruction::BinaryOpsEnd && Oprnds.size() == 2) |
| 556 | Result = BinaryOperator::create((Instruction::BinaryOps)Opcode, |
| 557 | getValue(iType, Oprnds[0]), |
| 558 | getValue(iType, Oprnds[1])); |
| 559 | |
| 560 | switch (Opcode) { |
| 561 | default: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 562 | if (Result == 0) |
| 563 | throw std::string("Illegal instruction read!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 564 | break; |
| 565 | case Instruction::VAArg: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 566 | Result = new VAArgInst(getValue(iType, Oprnds[0]), |
| 567 | getSanitizedType(Oprnds[1])); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 568 | break; |
| 569 | case Instruction::VANext: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 570 | Result = new VANextInst(getValue(iType, Oprnds[0]), |
| 571 | getSanitizedType(Oprnds[1])); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 572 | break; |
| 573 | case Instruction::Cast: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 574 | Result = new CastInst(getValue(iType, Oprnds[0]), |
| 575 | getSanitizedType(Oprnds[1])); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 576 | break; |
| 577 | case Instruction::Select: |
| 578 | Result = new SelectInst(getValue(Type::BoolTyID, Oprnds[0]), |
| 579 | getValue(iType, Oprnds[1]), |
| 580 | getValue(iType, Oprnds[2])); |
| 581 | break; |
| 582 | case Instruction::PHI: { |
| 583 | if (Oprnds.size() == 0 || (Oprnds.size() & 1)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 584 | throw std::string("Invalid phi node encountered!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 585 | |
| 586 | PHINode *PN = new PHINode(InstTy); |
| 587 | PN->op_reserve(Oprnds.size()); |
| 588 | for (unsigned i = 0, e = Oprnds.size(); i != e; i += 2) |
| 589 | PN->addIncoming(getValue(iType, Oprnds[i]), getBasicBlock(Oprnds[i+1])); |
| 590 | Result = PN; |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | case Instruction::Shl: |
| 595 | case Instruction::Shr: |
| 596 | Result = new ShiftInst((Instruction::OtherOps)Opcode, |
| 597 | getValue(iType, Oprnds[0]), |
| 598 | getValue(Type::UByteTyID, Oprnds[1])); |
| 599 | break; |
| 600 | case Instruction::Ret: |
| 601 | if (Oprnds.size() == 0) |
| 602 | Result = new ReturnInst(); |
| 603 | else if (Oprnds.size() == 1) |
| 604 | Result = new ReturnInst(getValue(iType, Oprnds[0])); |
| 605 | else |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 606 | throw std::string("Unrecognized instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 607 | break; |
| 608 | |
| 609 | case Instruction::Br: |
| 610 | if (Oprnds.size() == 1) |
| 611 | Result = new BranchInst(getBasicBlock(Oprnds[0])); |
| 612 | else if (Oprnds.size() == 3) |
| 613 | Result = new BranchInst(getBasicBlock(Oprnds[0]), |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 614 | getBasicBlock(Oprnds[1]), getValue(Type::BoolTyID , Oprnds[2])); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 615 | else |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 616 | throw std::string("Invalid number of operands for a 'br' instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 617 | break; |
| 618 | case Instruction::Switch: { |
| 619 | if (Oprnds.size() & 1) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 620 | throw std::string("Switch statement with odd number of arguments!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 621 | |
| 622 | SwitchInst *I = new SwitchInst(getValue(iType, Oprnds[0]), |
| 623 | getBasicBlock(Oprnds[1])); |
| 624 | for (unsigned i = 2, e = Oprnds.size(); i != e; i += 2) |
| 625 | I->addCase(cast<Constant>(getValue(iType, Oprnds[i])), |
| 626 | getBasicBlock(Oprnds[i+1])); |
| 627 | Result = I; |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | case Instruction::Call: { |
| 632 | if (Oprnds.size() == 0) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 633 | throw std::string("Invalid call instruction encountered!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 634 | |
| 635 | Value *F = getValue(iType, Oprnds[0]); |
| 636 | |
| 637 | // Check to make sure we have a pointer to function type |
| 638 | const PointerType *PTy = dyn_cast<PointerType>(F->getType()); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 639 | if (PTy == 0) throw std::string("Call to non function pointer value!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 640 | const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType()); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 641 | if (FTy == 0) throw std::string("Call to non function pointer value!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 642 | |
| 643 | std::vector<Value *> Params; |
| 644 | if (!FTy->isVarArg()) { |
| 645 | FunctionType::param_iterator It = FTy->param_begin(); |
| 646 | |
| 647 | for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) { |
| 648 | if (It == FTy->param_end()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 649 | throw std::string("Invalid call instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 650 | Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i])); |
| 651 | } |
| 652 | if (It != FTy->param_end()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 653 | throw std::string("Invalid call instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 654 | } else { |
| 655 | Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1); |
| 656 | |
| 657 | unsigned FirstVariableOperand; |
| 658 | if (Oprnds.size() < FTy->getNumParams()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 659 | throw std::string("Call instruction missing operands!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 660 | |
| 661 | // Read all of the fixed arguments |
| 662 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
| 663 | Params.push_back(getValue(getTypeSlot(FTy->getParamType(i)),Oprnds[i])); |
| 664 | |
| 665 | FirstVariableOperand = FTy->getNumParams(); |
| 666 | |
| 667 | if ((Oprnds.size()-FirstVariableOperand) & 1) // Must be pairs of type/value |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 668 | throw std::string("Invalid call instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 669 | |
| 670 | for (unsigned i = FirstVariableOperand, e = Oprnds.size(); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 671 | i != e; i += 2) |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 672 | Params.push_back(getValue(Oprnds[i], Oprnds[i+1])); |
| 673 | } |
| 674 | |
| 675 | Result = new CallInst(F, Params); |
| 676 | break; |
| 677 | } |
| 678 | case Instruction::Invoke: { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 679 | if (Oprnds.size() < 3) |
| 680 | throw std::string("Invalid invoke instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 681 | Value *F = getValue(iType, Oprnds[0]); |
| 682 | |
| 683 | // Check to make sure we have a pointer to function type |
| 684 | const PointerType *PTy = dyn_cast<PointerType>(F->getType()); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 685 | if (PTy == 0) |
| 686 | throw std::string("Invoke to non function pointer value!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 687 | const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType()); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 688 | if (FTy == 0) |
| 689 | throw std::string("Invoke to non function pointer value!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 690 | |
| 691 | std::vector<Value *> Params; |
| 692 | BasicBlock *Normal, *Except; |
| 693 | |
| 694 | if (!FTy->isVarArg()) { |
| 695 | Normal = getBasicBlock(Oprnds[1]); |
| 696 | Except = getBasicBlock(Oprnds[2]); |
| 697 | |
| 698 | FunctionType::param_iterator It = FTy->param_begin(); |
| 699 | for (unsigned i = 3, e = Oprnds.size(); i != e; ++i) { |
| 700 | if (It == FTy->param_end()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 701 | throw std::string("Invalid invoke instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 702 | Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i])); |
| 703 | } |
| 704 | if (It != FTy->param_end()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 705 | throw std::string("Invalid invoke instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 706 | } else { |
| 707 | Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1); |
| 708 | |
| 709 | Normal = getBasicBlock(Oprnds[0]); |
| 710 | Except = getBasicBlock(Oprnds[1]); |
| 711 | |
| 712 | unsigned FirstVariableArgument = FTy->getNumParams()+2; |
| 713 | for (unsigned i = 2; i != FirstVariableArgument; ++i) |
| 714 | Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)), |
| 715 | Oprnds[i])); |
| 716 | |
| 717 | if (Oprnds.size()-FirstVariableArgument & 1) // Must be type/value pairs |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 718 | throw std::string("Invalid invoke instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 719 | |
| 720 | for (unsigned i = FirstVariableArgument; i < Oprnds.size(); i += 2) |
| 721 | Params.push_back(getValue(Oprnds[i], Oprnds[i+1])); |
| 722 | } |
| 723 | |
| 724 | Result = new InvokeInst(F, Normal, Except, Params); |
| 725 | break; |
| 726 | } |
| 727 | case Instruction::Malloc: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 728 | if (Oprnds.size() > 2) |
| 729 | throw std::string("Invalid malloc instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 730 | if (!isa<PointerType>(InstTy)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 731 | throw std::string("Invalid malloc instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 732 | |
| 733 | Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(), |
| 734 | Oprnds.size() ? getValue(Type::UIntTyID, |
| 735 | Oprnds[0]) : 0); |
| 736 | break; |
| 737 | |
| 738 | case Instruction::Alloca: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 739 | if (Oprnds.size() > 2) |
| 740 | throw std::string("Invalid alloca instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 741 | if (!isa<PointerType>(InstTy)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 742 | throw std::string("Invalid alloca instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 743 | |
| 744 | Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(), |
| 745 | Oprnds.size() ? getValue(Type::UIntTyID, |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 746 | Oprnds[0]) :0); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 747 | break; |
| 748 | case Instruction::Free: |
| 749 | if (!isa<PointerType>(InstTy)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 750 | throw std::string("Invalid free instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 751 | Result = new FreeInst(getValue(iType, Oprnds[0])); |
| 752 | break; |
| 753 | case Instruction::GetElementPtr: { |
| 754 | if (Oprnds.size() == 0 || !isa<PointerType>(InstTy)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 755 | throw std::string("Invalid getelementptr instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 756 | |
| 757 | std::vector<Value*> Idx; |
| 758 | |
| 759 | const Type *NextTy = InstTy; |
| 760 | for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) { |
| 761 | const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 762 | if (!TopTy) |
| 763 | throw std::string("Invalid getelementptr instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 764 | |
| 765 | unsigned ValIdx = Oprnds[i]; |
| 766 | unsigned IdxTy = 0; |
| 767 | if (!hasRestrictedGEPTypes) { |
| 768 | // Struct indices are always uints, sequential type indices can be any |
| 769 | // of the 32 or 64-bit integer types. The actual choice of type is |
| 770 | // encoded in the low two bits of the slot number. |
| 771 | if (isa<StructType>(TopTy)) |
| 772 | IdxTy = Type::UIntTyID; |
| 773 | else { |
| 774 | switch (ValIdx & 3) { |
| 775 | default: |
| 776 | case 0: IdxTy = Type::UIntTyID; break; |
| 777 | case 1: IdxTy = Type::IntTyID; break; |
| 778 | case 2: IdxTy = Type::ULongTyID; break; |
| 779 | case 3: IdxTy = Type::LongTyID; break; |
| 780 | } |
| 781 | ValIdx >>= 2; |
| 782 | } |
| 783 | } else { |
| 784 | IdxTy = isa<StructType>(TopTy) ? Type::UByteTyID : Type::LongTyID; |
| 785 | } |
| 786 | |
| 787 | Idx.push_back(getValue(IdxTy, ValIdx)); |
| 788 | |
| 789 | // Convert ubyte struct indices into uint struct indices. |
| 790 | if (isa<StructType>(TopTy) && hasRestrictedGEPTypes) |
| 791 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(Idx.back())) |
| 792 | Idx[Idx.size()-1] = ConstantExpr::getCast(C, Type::UIntTy); |
| 793 | |
| 794 | NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true); |
| 795 | } |
| 796 | |
| 797 | Result = new GetElementPtrInst(getValue(iType, Oprnds[0]), Idx); |
| 798 | break; |
| 799 | } |
| 800 | |
| 801 | case 62: // volatile load |
| 802 | case Instruction::Load: |
| 803 | if (Oprnds.size() != 1 || !isa<PointerType>(InstTy)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 804 | throw std::string("Invalid load instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 805 | Result = new LoadInst(getValue(iType, Oprnds[0]), "", Opcode == 62); |
| 806 | break; |
| 807 | |
| 808 | case 63: // volatile store |
| 809 | case Instruction::Store: { |
| 810 | if (!isa<PointerType>(InstTy) || Oprnds.size() != 2) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 811 | throw std::string("Invalid store instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 812 | |
| 813 | Value *Ptr = getValue(iType, Oprnds[1]); |
| 814 | const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType(); |
| 815 | Result = new StoreInst(getValue(getTypeSlot(ValTy), Oprnds[0]), Ptr, |
| 816 | Opcode == 63); |
| 817 | break; |
| 818 | } |
| 819 | case Instruction::Unwind: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 820 | if (Oprnds.size() != 0) |
| 821 | throw std::string("Invalid unwind instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 822 | Result = new UnwindInst(); |
| 823 | break; |
| 824 | } // end switch(Opcode) |
| 825 | |
| 826 | unsigned TypeSlot; |
| 827 | if (Result->getType() == InstTy) |
| 828 | TypeSlot = iType; |
| 829 | else |
| 830 | TypeSlot = getTypeSlot(Result->getType()); |
| 831 | |
| 832 | insertValue(Result, TypeSlot, FunctionValues); |
| 833 | BB->getInstList().push_back(Result); |
| 834 | } |
| 835 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 836 | /// Get a particular numbered basic block, which might be a forward reference. |
| 837 | /// This works together with ParseBasicBlock to handle these forward references |
| 838 | /// in a clean manner. This function is used when constructing phi, br, switch, |
| 839 | /// and other instructions that reference basic blocks. Blocks are numbered |
| 840 | /// sequentially as they appear in the function. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 841 | BasicBlock *BytecodeReader::getBasicBlock(unsigned ID) { |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 842 | // Make sure there is room in the table... |
| 843 | if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1); |
| 844 | |
| 845 | // First check to see if this is a backwards reference, i.e., ParseBasicBlock |
| 846 | // has already created this block, or if the forward reference has already |
| 847 | // been created. |
| 848 | if (ParsedBasicBlocks[ID]) |
| 849 | return ParsedBasicBlocks[ID]; |
| 850 | |
| 851 | // Otherwise, the basic block has not yet been created. Do so and add it to |
| 852 | // the ParsedBasicBlocks list. |
| 853 | return ParsedBasicBlocks[ID] = new BasicBlock(); |
| 854 | } |
| 855 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 856 | /// In LLVM 1.0 bytecode files, we used to output one basicblock at a time. |
| 857 | /// This method reads in one of the basicblock packets. This method is not used |
| 858 | /// for bytecode files after LLVM 1.0 |
| 859 | /// @returns The basic block constructed. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 860 | BasicBlock *BytecodeReader::ParseBasicBlock( unsigned BlockNo) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 861 | if (Handler) Handler->handleBasicBlockBegin( BlockNo ); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 862 | |
| 863 | BasicBlock *BB = 0; |
| 864 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 865 | if (ParsedBasicBlocks.size() == BlockNo) |
| 866 | ParsedBasicBlocks.push_back(BB = new BasicBlock()); |
| 867 | else if (ParsedBasicBlocks[BlockNo] == 0) |
| 868 | BB = ParsedBasicBlocks[BlockNo] = new BasicBlock(); |
| 869 | else |
| 870 | BB = ParsedBasicBlocks[BlockNo]; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 871 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 872 | std::vector<unsigned> Operands; |
| 873 | while ( moreInBlock() ) |
| 874 | ParseInstruction(Operands, BB); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 875 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 876 | if (Handler) Handler->handleBasicBlockEnd( BlockNo ); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 877 | return BB; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 880 | /// Parse all of the BasicBlock's & Instruction's in the body of a function. |
| 881 | /// In post 1.0 bytecode files, we no longer emit basic block individually, |
| 882 | /// in order to avoid per-basic-block overhead. |
| 883 | /// @returns Rhe number of basic blocks encountered. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 884 | unsigned BytecodeReader::ParseInstructionList(Function* F) { |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 885 | unsigned BlockNo = 0; |
| 886 | std::vector<unsigned> Args; |
| 887 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 888 | while ( moreInBlock() ) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 889 | if (Handler) Handler->handleBasicBlockBegin( BlockNo ); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 890 | BasicBlock *BB; |
| 891 | if (ParsedBasicBlocks.size() == BlockNo) |
| 892 | ParsedBasicBlocks.push_back(BB = new BasicBlock()); |
| 893 | else if (ParsedBasicBlocks[BlockNo] == 0) |
| 894 | BB = ParsedBasicBlocks[BlockNo] = new BasicBlock(); |
| 895 | else |
| 896 | BB = ParsedBasicBlocks[BlockNo]; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 897 | if (Handler) Handler->handleBasicBlockEnd( BlockNo ); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 898 | ++BlockNo; |
| 899 | F->getBasicBlockList().push_back(BB); |
| 900 | |
| 901 | // Read instructions into this basic block until we get to a terminator |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 902 | while ( moreInBlock() && !BB->getTerminator()) |
| 903 | ParseInstruction(Args, BB); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 904 | |
| 905 | if (!BB->getTerminator()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 906 | throw std::string("Non-terminated basic block found!"); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | return BlockNo; |
| 910 | } |
| 911 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 912 | /// Parse a symbol table. This works for both module level and function |
| 913 | /// level symbol tables. For function level symbol tables, the CurrentFunction |
| 914 | /// parameter must be non-zero and the ST parameter must correspond to |
| 915 | /// CurrentFunction's symbol table. For Module level symbol tables, the |
| 916 | /// CurrentFunction argument must be zero. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 917 | void BytecodeReader::ParseSymbolTable(Function *CurrentFunction, |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 918 | SymbolTable *ST) { |
| 919 | if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 920 | |
Chris Lattner | 39cacce | 2003-10-10 05:43:47 +0000 | [diff] [blame] | 921 | // Allow efficient basic block lookup by number. |
| 922 | std::vector<BasicBlock*> BBMap; |
| 923 | if (CurrentFunction) |
| 924 | for (Function::iterator I = CurrentFunction->begin(), |
| 925 | E = CurrentFunction->end(); I != E; ++I) |
| 926 | BBMap.push_back(I); |
| 927 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 928 | /// In LLVM 1.3 we write types separately from values so |
| 929 | /// The types are always first in the symbol table. This is |
| 930 | /// because Type no longer derives from Value. |
| 931 | if ( ! hasTypeDerivedFromValue ) { |
| 932 | // Symtab block header: [num entries] |
| 933 | unsigned NumEntries = read_vbr_uint(); |
| 934 | for ( unsigned i = 0; i < NumEntries; ++i ) { |
| 935 | // Symtab entry: [def slot #][name] |
| 936 | unsigned slot = read_vbr_uint(); |
| 937 | std::string Name = read_str(); |
| 938 | const Type* T = getType(slot); |
| 939 | ST->insert(Name, T); |
| 940 | } |
| 941 | } |
| 942 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 943 | while ( moreInBlock() ) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 944 | // Symtab block header: [num entries][type id number] |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 945 | unsigned NumEntries = read_vbr_uint(); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 946 | unsigned Typ = 0; |
| 947 | bool isTypeType = read_typeid(Typ); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 948 | const Type *Ty = getType(Typ); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 949 | |
Chris Lattner | 7dc3a2e | 2003-10-13 14:57:53 +0000 | [diff] [blame] | 950 | for (unsigned i = 0; i != NumEntries; ++i) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 951 | // Symtab entry: [def slot #][name] |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 952 | unsigned slot = read_vbr_uint(); |
| 953 | std::string Name = read_str(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 954 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 955 | // if we're reading a pre 1.3 bytecode file and the type plane |
| 956 | // is the "type type", handle it here |
| 957 | if ( isTypeType ) { |
| 958 | const Type* T = getType(slot); |
| 959 | if ( T == 0 ) |
| 960 | PARSE_ERROR("Failed type look-up for name '" << Name << "'"); |
| 961 | ST->insert(Name, T); |
| 962 | continue; // code below must be short circuited |
Chris Lattner | 39cacce | 2003-10-10 05:43:47 +0000 | [diff] [blame] | 963 | } else { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 964 | Value *V = 0; |
| 965 | if (Typ == Type::LabelTyID) { |
| 966 | if (slot < BBMap.size()) |
| 967 | V = BBMap[slot]; |
| 968 | } else { |
| 969 | V = getValue(Typ, slot, false); // Find mapping... |
| 970 | } |
| 971 | if (V == 0) |
| 972 | PARSE_ERROR("Failed value look-up for name '" << Name << "'"); |
| 973 | V->setName(Name, ST); |
Chris Lattner | 39cacce | 2003-10-10 05:43:47 +0000 | [diff] [blame] | 974 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 975 | } |
| 976 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 977 | checkPastBlockEnd("Symbol Table"); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 978 | if (Handler) Handler->handleSymbolTableEnd(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 981 | /// Read in the types portion of a compaction table. |
| 982 | void BytecodeReader::ParseCompactionTypes( unsigned NumEntries ) { |
| 983 | for (unsigned i = 0; i != NumEntries; ++i) { |
| 984 | unsigned TypeSlot = 0; |
| 985 | bool isTypeType = read_typeid(TypeSlot); |
| 986 | assert(!isTypeType && "Invalid type in compaction table: type type"); |
| 987 | const Type *Typ = getGlobalTableType(TypeSlot); |
| 988 | CompactionTypes.push_back(Typ); |
| 989 | if (Handler) Handler->handleCompactionTableType( i, TypeSlot, Typ ); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | /// Parse a compaction table. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 994 | void BytecodeReader::ParseCompactionTable() { |
| 995 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 996 | if (Handler) Handler->handleCompactionTableBegin(); |
| 997 | |
| 998 | /// In LLVM 1.3 Type no longer derives from Value. So, |
| 999 | /// we always write them first in the compaction table |
| 1000 | /// because they can't occupy a "type plane" where the |
| 1001 | /// Values reside. |
| 1002 | if ( ! hasTypeDerivedFromValue ) { |
| 1003 | unsigned NumEntries = read_vbr_uint(); |
| 1004 | ParseCompactionTypes( NumEntries ); |
| 1005 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1006 | |
| 1007 | while ( moreInBlock() ) { |
| 1008 | unsigned NumEntries = read_vbr_uint(); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1009 | unsigned Ty = 0; |
| 1010 | unsigned isTypeType = false; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1011 | |
| 1012 | if ((NumEntries & 3) == 3) { |
| 1013 | NumEntries >>= 2; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1014 | isTypeType = read_typeid(Ty); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1015 | } else { |
| 1016 | Ty = NumEntries >> 2; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1017 | isTypeType = sanitizeTypeId(Ty); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1018 | NumEntries &= 3; |
| 1019 | } |
| 1020 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1021 | // if we're reading a pre 1.3 bytecode file and the type plane |
| 1022 | // is the "type type", handle it here |
| 1023 | if ( isTypeType ) { |
| 1024 | ParseCompactionTypes(NumEntries); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1025 | } else { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1026 | if (Ty >= CompactionValues.size()) |
| 1027 | CompactionValues.resize(Ty+1); |
| 1028 | |
| 1029 | if (!CompactionValues[Ty].empty()) |
| 1030 | throw std::string("Compaction table plane contains multiple entries!"); |
| 1031 | |
| 1032 | if (Handler) Handler->handleCompactionTablePlane( Ty, NumEntries ); |
| 1033 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1034 | const Type *Typ = getType(Ty); |
| 1035 | // Push the implicit zero |
| 1036 | CompactionValues[Ty].push_back(Constant::getNullValue(Typ)); |
| 1037 | for (unsigned i = 0; i != NumEntries; ++i) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1038 | unsigned ValSlot = read_vbr_uint(); |
| 1039 | Value *V = getGlobalTableValue(Typ, ValSlot); |
| 1040 | CompactionValues[Ty].push_back(V); |
| 1041 | if (Handler) Handler->handleCompactionTableValue( i, Ty, ValSlot, Typ ); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | } |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1045 | if (Handler) Handler->handleCompactionTableEnd(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // Parse a single type constant. |
| 1049 | const Type *BytecodeReader::ParseTypeConstant() { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1050 | unsigned PrimType = 0; |
| 1051 | bool isTypeType = read_typeid(PrimType); |
| 1052 | assert(!isTypeType && "Invalid type (type type) in type constants!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1053 | |
| 1054 | const Type *Result = 0; |
| 1055 | if ((Result = Type::getPrimitiveType((Type::TypeID)PrimType))) |
| 1056 | return Result; |
| 1057 | |
| 1058 | switch (PrimType) { |
| 1059 | case Type::FunctionTyID: { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1060 | const Type *RetType = readSanitizedType(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1061 | |
| 1062 | unsigned NumParams = read_vbr_uint(); |
| 1063 | |
| 1064 | std::vector<const Type*> Params; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1065 | while (NumParams--) |
| 1066 | Params.push_back(readSanitizedType()); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1067 | |
| 1068 | bool isVarArg = Params.size() && Params.back() == Type::VoidTy; |
| 1069 | if (isVarArg) Params.pop_back(); |
| 1070 | |
| 1071 | Result = FunctionType::get(RetType, Params, isVarArg); |
| 1072 | break; |
| 1073 | } |
| 1074 | case Type::ArrayTyID: { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1075 | const Type *ElementType = readSanitizedType(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1076 | unsigned NumElements = read_vbr_uint(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1077 | Result = ArrayType::get(ElementType, NumElements); |
| 1078 | break; |
| 1079 | } |
| 1080 | case Type::StructTyID: { |
| 1081 | std::vector<const Type*> Elements; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1082 | unsigned Typ = 0; |
| 1083 | bool isTypeType = read_typeid(Typ); |
| 1084 | assert(!isTypeType && "Invalid element type (type type) for structure!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1085 | while (Typ) { // List is terminated by void/0 typeid |
| 1086 | Elements.push_back(getType(Typ)); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1087 | bool isTypeType = read_typeid(Typ); |
| 1088 | assert(!isTypeType && "Invalid element type (type type) for structure!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | Result = StructType::get(Elements); |
| 1092 | break; |
| 1093 | } |
| 1094 | case Type::PointerTyID: { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1095 | Result = PointerType::get(readSanitizedType()); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1096 | break; |
| 1097 | } |
| 1098 | |
| 1099 | case Type::OpaqueTyID: { |
| 1100 | Result = OpaqueType::get(); |
| 1101 | break; |
| 1102 | } |
| 1103 | |
| 1104 | default: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1105 | PARSE_ERROR("Don't know how to deserialize primitive type" |
| 1106 | << PrimType << "\n"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1107 | break; |
| 1108 | } |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1109 | if (Handler) Handler->handleType( Result ); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1110 | return Result; |
| 1111 | } |
| 1112 | |
| 1113 | // ParseTypeConstants - We have to use this weird code to handle recursive |
| 1114 | // types. We know that recursive types will only reference the current slab of |
| 1115 | // values in the type plane, but they can forward reference types before they |
| 1116 | // have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might |
| 1117 | // be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix |
| 1118 | // this ugly problem, we pessimistically insert an opaque type for each type we |
| 1119 | // are about to read. This means that forward references will resolve to |
| 1120 | // something and when we reread the type later, we can replace the opaque type |
| 1121 | // with a new resolved concrete type. |
| 1122 | // |
| 1123 | void BytecodeReader::ParseTypeConstants(TypeListTy &Tab, unsigned NumEntries){ |
| 1124 | assert(Tab.size() == 0 && "should not have read type constants in before!"); |
| 1125 | |
| 1126 | // Insert a bunch of opaque types to be resolved later... |
| 1127 | Tab.reserve(NumEntries); |
| 1128 | for (unsigned i = 0; i != NumEntries; ++i) |
| 1129 | Tab.push_back(OpaqueType::get()); |
| 1130 | |
| 1131 | // Loop through reading all of the types. Forward types will make use of the |
| 1132 | // opaque types just inserted. |
| 1133 | // |
| 1134 | for (unsigned i = 0; i != NumEntries; ++i) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1135 | const Type* NewTy = ParseTypeConstant(); |
| 1136 | const Type* OldTy = Tab[i].get(); |
| 1137 | if (NewTy == 0) |
| 1138 | throw std::string("Couldn't parse type!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Don't directly push the new type on the Tab. Instead we want to replace |
| 1141 | // the opaque type we previously inserted with the new concrete value. This |
| 1142 | // approach helps with forward references to types. The refinement from the |
| 1143 | // abstract (opaque) type to the new type causes all uses of the abstract |
| 1144 | // type to use the concrete type (NewTy). This will also cause the opaque |
| 1145 | // type to be deleted. |
| 1146 | cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy); |
| 1147 | |
| 1148 | // This should have replaced the old opaque type with the new type in the |
| 1149 | // value table... or with a preexisting type that was already in the system. |
| 1150 | // Let's just make sure it did. |
| 1151 | assert(Tab[i] != OldTy && "refineAbstractType didn't work!"); |
| 1152 | } |
| 1153 | } |
| 1154 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1155 | /// Parse a single constant value |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1156 | Constant *BytecodeReader::ParseConstantValue( unsigned TypeID) { |
| 1157 | // We must check for a ConstantExpr before switching by type because |
| 1158 | // a ConstantExpr can be of any type, and has no explicit value. |
| 1159 | // |
| 1160 | // 0 if not expr; numArgs if is expr |
| 1161 | unsigned isExprNumArgs = read_vbr_uint(); |
| 1162 | |
| 1163 | if (isExprNumArgs) { |
| 1164 | // FIXME: Encoding of constant exprs could be much more compact! |
| 1165 | std::vector<Constant*> ArgVec; |
| 1166 | ArgVec.reserve(isExprNumArgs); |
| 1167 | unsigned Opcode = read_vbr_uint(); |
| 1168 | |
| 1169 | // Read the slot number and types of each of the arguments |
| 1170 | for (unsigned i = 0; i != isExprNumArgs; ++i) { |
| 1171 | unsigned ArgValSlot = read_vbr_uint(); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1172 | unsigned ArgTypeSlot = 0; |
| 1173 | bool isTypeType = read_typeid(ArgTypeSlot); |
| 1174 | assert(!isTypeType && "Invalid argument type (type type) for constant value"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Get the arg value from its slot if it exists, otherwise a placeholder |
| 1177 | ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot)); |
| 1178 | } |
| 1179 | |
| 1180 | // Construct a ConstantExpr of the appropriate kind |
| 1181 | if (isExprNumArgs == 1) { // All one-operand expressions |
| 1182 | assert(Opcode == Instruction::Cast); |
| 1183 | Constant* Result = ConstantExpr::getCast(ArgVec[0], getType(TypeID)); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1184 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1185 | return Result; |
| 1186 | } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr |
| 1187 | std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end()); |
| 1188 | |
| 1189 | if (hasRestrictedGEPTypes) { |
| 1190 | const Type *BaseTy = ArgVec[0]->getType(); |
| 1191 | generic_gep_type_iterator<std::vector<Constant*>::iterator> |
| 1192 | GTI = gep_type_begin(BaseTy, IdxList.begin(), IdxList.end()), |
| 1193 | E = gep_type_end(BaseTy, IdxList.begin(), IdxList.end()); |
| 1194 | for (unsigned i = 0; GTI != E; ++GTI, ++i) |
| 1195 | if (isa<StructType>(*GTI)) { |
| 1196 | if (IdxList[i]->getType() != Type::UByteTy) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1197 | throw std::string("Invalid index for getelementptr!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1198 | IdxList[i] = ConstantExpr::getCast(IdxList[i], Type::UIntTy); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | Constant* Result = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1203 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1204 | return Result; |
| 1205 | } else if (Opcode == Instruction::Select) { |
| 1206 | assert(ArgVec.size() == 3); |
| 1207 | Constant* Result = ConstantExpr::getSelect(ArgVec[0], ArgVec[1], |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1208 | ArgVec[2]); |
| 1209 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1210 | return Result; |
| 1211 | } else { // All other 2-operand expressions |
| 1212 | Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1213 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1214 | return Result; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | // Ok, not an ConstantExpr. We now know how to read the given type... |
| 1219 | const Type *Ty = getType(TypeID); |
| 1220 | switch (Ty->getTypeID()) { |
| 1221 | case Type::BoolTyID: { |
| 1222 | unsigned Val = read_vbr_uint(); |
| 1223 | if (Val != 0 && Val != 1) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1224 | throw std::string("Invalid boolean value read."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1225 | Constant* Result = ConstantBool::get(Val == 1); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1226 | if (Handler) Handler->handleConstantValue(Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1227 | return Result; |
| 1228 | } |
| 1229 | |
| 1230 | case Type::UByteTyID: // Unsigned integer types... |
| 1231 | case Type::UShortTyID: |
| 1232 | case Type::UIntTyID: { |
| 1233 | unsigned Val = read_vbr_uint(); |
| 1234 | if (!ConstantUInt::isValueValidForType(Ty, Val)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1235 | throw std::string("Invalid unsigned byte/short/int read."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1236 | Constant* Result = ConstantUInt::get(Ty, Val); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1237 | if (Handler) Handler->handleConstantValue(Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1238 | return Result; |
| 1239 | } |
| 1240 | |
| 1241 | case Type::ULongTyID: { |
| 1242 | Constant* Result = ConstantUInt::get(Ty, read_vbr_uint64()); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1243 | if (Handler) Handler->handleConstantValue(Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1244 | return Result; |
| 1245 | } |
| 1246 | |
| 1247 | case Type::SByteTyID: // Signed integer types... |
| 1248 | case Type::ShortTyID: |
| 1249 | case Type::IntTyID: { |
| 1250 | case Type::LongTyID: |
| 1251 | int64_t Val = read_vbr_int64(); |
| 1252 | if (!ConstantSInt::isValueValidForType(Ty, Val)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1253 | throw std::string("Invalid signed byte/short/int/long read."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1254 | Constant* Result = ConstantSInt::get(Ty, Val); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1255 | if (Handler) Handler->handleConstantValue(Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1256 | return Result; |
| 1257 | } |
| 1258 | |
| 1259 | case Type::FloatTyID: { |
| 1260 | float F; |
| 1261 | read_data(&F, &F+1); |
| 1262 | Constant* Result = ConstantFP::get(Ty, F); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1263 | if (Handler) Handler->handleConstantValue(Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1264 | return Result; |
| 1265 | } |
| 1266 | |
| 1267 | case Type::DoubleTyID: { |
| 1268 | double Val; |
| 1269 | read_data(&Val, &Val+1); |
| 1270 | Constant* Result = ConstantFP::get(Ty, Val); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1271 | if (Handler) Handler->handleConstantValue(Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1272 | return Result; |
| 1273 | } |
| 1274 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1275 | case Type::ArrayTyID: { |
| 1276 | const ArrayType *AT = cast<ArrayType>(Ty); |
| 1277 | unsigned NumElements = AT->getNumElements(); |
| 1278 | unsigned TypeSlot = getTypeSlot(AT->getElementType()); |
| 1279 | std::vector<Constant*> Elements; |
| 1280 | Elements.reserve(NumElements); |
| 1281 | while (NumElements--) // Read all of the elements of the constant. |
| 1282 | Elements.push_back(getConstantValue(TypeSlot, |
| 1283 | read_vbr_uint())); |
| 1284 | Constant* Result = ConstantArray::get(AT, Elements); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1285 | if (Handler) Handler->handleConstantArray(AT, Elements, TypeSlot, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1286 | return Result; |
| 1287 | } |
| 1288 | |
| 1289 | case Type::StructTyID: { |
| 1290 | const StructType *ST = cast<StructType>(Ty); |
| 1291 | |
| 1292 | std::vector<Constant *> Elements; |
| 1293 | Elements.reserve(ST->getNumElements()); |
| 1294 | for (unsigned i = 0; i != ST->getNumElements(); ++i) |
| 1295 | Elements.push_back(getConstantValue(ST->getElementType(i), |
| 1296 | read_vbr_uint())); |
| 1297 | |
| 1298 | Constant* Result = ConstantStruct::get(ST, Elements); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1299 | if (Handler) Handler->handleConstantStruct(ST, Elements, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1300 | return Result; |
| 1301 | } |
| 1302 | |
| 1303 | case Type::PointerTyID: { // ConstantPointerRef value... |
| 1304 | const PointerType *PT = cast<PointerType>(Ty); |
| 1305 | unsigned Slot = read_vbr_uint(); |
| 1306 | |
| 1307 | // Check to see if we have already read this global variable... |
| 1308 | Value *Val = getValue(TypeID, Slot, false); |
| 1309 | GlobalValue *GV; |
| 1310 | if (Val) { |
| 1311 | if (!(GV = dyn_cast<GlobalValue>(Val))) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1312 | throw std::string("Value of ConstantPointerRef not in ValueTable!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1313 | } else { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1314 | throw std::string("Forward references are not allowed here."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | Constant* Result = ConstantPointerRef::get(GV); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1318 | if (Handler) Handler->handleConstantPointer(PT, Slot, GV, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1319 | return Result; |
| 1320 | } |
| 1321 | |
| 1322 | default: |
| 1323 | PARSE_ERROR("Don't know how to deserialize constant value of type '"+ |
| 1324 | Ty->getDescription()); |
| 1325 | break; |
| 1326 | } |
| 1327 | } |
| 1328 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1329 | /// Resolve references for constants. This function resolves the forward |
| 1330 | /// referenced constants in the ConstantFwdRefs map. It uses the |
| 1331 | /// replaceAllUsesWith method of Value class to substitute the placeholder |
| 1332 | /// instance with the actual instance. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1333 | void BytecodeReader::ResolveReferencesToConstant(Constant *NewV, unsigned Slot){ |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 1334 | ConstantRefsType::iterator I = |
| 1335 | ConstantFwdRefs.find(std::make_pair(NewV->getType(), Slot)); |
| 1336 | if (I == ConstantFwdRefs.end()) return; // Never forward referenced? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1337 | |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 1338 | Value *PH = I->second; // Get the placeholder... |
| 1339 | PH->replaceAllUsesWith(NewV); |
| 1340 | delete PH; // Delete the old placeholder |
| 1341 | ConstantFwdRefs.erase(I); // Remove the map entry for it |
Vikram S. Adve | c1e4a81 | 2002-07-14 23:04:18 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1344 | /// Parse the constant strings section. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1345 | void BytecodeReader::ParseStringConstants(unsigned NumEntries, ValueTable &Tab){ |
| 1346 | for (; NumEntries; --NumEntries) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1347 | unsigned Typ = 0; |
| 1348 | bool isTypeType = read_typeid(Typ); |
| 1349 | assert(!isTypeType && "Invalid type (type type) for string constant"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1350 | const Type *Ty = getType(Typ); |
| 1351 | if (!isa<ArrayType>(Ty)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1352 | throw std::string("String constant data invalid!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1353 | |
| 1354 | const ArrayType *ATy = cast<ArrayType>(Ty); |
| 1355 | if (ATy->getElementType() != Type::SByteTy && |
| 1356 | ATy->getElementType() != Type::UByteTy) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1357 | throw std::string("String constant data invalid!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Read character data. The type tells us how long the string is. |
| 1360 | char Data[ATy->getNumElements()]; |
| 1361 | read_data(Data, Data+ATy->getNumElements()); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1362 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1363 | std::vector<Constant*> Elements(ATy->getNumElements()); |
| 1364 | if (ATy->getElementType() == Type::SByteTy) |
| 1365 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 1366 | Elements[i] = ConstantSInt::get(Type::SByteTy, (signed char)Data[i]); |
| 1367 | else |
| 1368 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 1369 | Elements[i] = ConstantUInt::get(Type::UByteTy, (unsigned char)Data[i]); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 1370 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1371 | // Create the constant, inserting it as needed. |
| 1372 | Constant *C = ConstantArray::get(ATy, Elements); |
| 1373 | unsigned Slot = insertValue(C, Typ, Tab); |
| 1374 | ResolveReferencesToConstant(C, Slot); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1375 | if (Handler) Handler->handleConstantString(cast<ConstantArray>(C)); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1376 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1379 | /// Parse the constant pool. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1380 | void BytecodeReader::ParseConstantPool(ValueTable &Tab, |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1381 | TypeListTy &TypeTab, |
| 1382 | bool isFunction) { |
| 1383 | if (Handler) Handler->handleGlobalConstantsBegin(); |
| 1384 | |
| 1385 | /// In LLVM 1.3 Type does not derive from Value so the types |
| 1386 | /// do not occupy a plane. Consequently, we read the types |
| 1387 | /// first in the constant pool. |
| 1388 | if ( isFunction && !hasTypeDerivedFromValue ) { |
| 1389 | unsigned NumEntries = read_vbr_uint(); |
| 1390 | ParseTypeConstants(TypeTab, NumEntries); |
| 1391 | } |
| 1392 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1393 | while ( moreInBlock() ) { |
| 1394 | unsigned NumEntries = read_vbr_uint(); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1395 | unsigned Typ = 0; |
| 1396 | bool isTypeType = read_typeid(Typ); |
| 1397 | |
| 1398 | /// In LLVM 1.2 and before, Types were written to the |
| 1399 | /// bytecode file in the "Type Type" plane (#12). |
| 1400 | /// In 1.3 plane 12 is now the label plane. Handle this here. |
| 1401 | if ( isTypeType ) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1402 | ParseTypeConstants(TypeTab, NumEntries); |
| 1403 | } else if (Typ == Type::VoidTyID) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1404 | /// Use of Type::VoidTyID is a misnomer. It actually means |
| 1405 | /// that the following plane is constant strings |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1406 | assert(&Tab == &ModuleValues && "Cannot read strings in functions!"); |
| 1407 | ParseStringConstants(NumEntries, Tab); |
| 1408 | } else { |
| 1409 | for (unsigned i = 0; i < NumEntries; ++i) { |
| 1410 | Constant *C = ParseConstantValue(Typ); |
| 1411 | assert(C && "ParseConstantValue returned NULL!"); |
| 1412 | unsigned Slot = insertValue(C, Typ, Tab); |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 1413 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1414 | // If we are reading a function constant table, make sure that we adjust |
| 1415 | // the slot number to be the real global constant number. |
| 1416 | // |
| 1417 | if (&Tab != &ModuleValues && Typ < ModuleValues.size() && |
| 1418 | ModuleValues[Typ]) |
| 1419 | Slot += ModuleValues[Typ]->size(); |
| 1420 | ResolveReferencesToConstant(C, Slot); |
| 1421 | } |
| 1422 | } |
| 1423 | } |
| 1424 | checkPastBlockEnd("Constant Pool"); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1425 | if (Handler) Handler->handleGlobalConstantsEnd(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1426 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1427 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1428 | /// Parse the contents of a function. Note that this function can be |
| 1429 | /// called lazily by materializeFunction |
| 1430 | /// @see materializeFunction |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1431 | void BytecodeReader::ParseFunctionBody(Function* F ) { |
| 1432 | |
| 1433 | unsigned FuncSize = BlockEnd - At; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 1434 | GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage; |
| 1435 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1436 | unsigned LinkageType = read_vbr_uint(); |
Chris Lattner | c08912f | 2004-01-14 16:44:44 +0000 | [diff] [blame] | 1437 | switch (LinkageType) { |
| 1438 | case 0: Linkage = GlobalValue::ExternalLinkage; break; |
| 1439 | case 1: Linkage = GlobalValue::WeakLinkage; break; |
| 1440 | case 2: Linkage = GlobalValue::AppendingLinkage; break; |
| 1441 | case 3: Linkage = GlobalValue::InternalLinkage; break; |
| 1442 | case 4: Linkage = GlobalValue::LinkOnceLinkage; break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1443 | default: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1444 | throw std::string("Invalid linkage type for Function."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1445 | Linkage = GlobalValue::InternalLinkage; |
| 1446 | break; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 1447 | } |
Chris Lattner | d23b1d3 | 2001-11-26 18:56:10 +0000 | [diff] [blame] | 1448 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1449 | F->setLinkage( Linkage ); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1450 | if (Handler) Handler->handleFunctionBegin(F,FuncSize); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 1452 | // Keep track of how many basic blocks we have read in... |
| 1453 | unsigned BlockNum = 0; |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1454 | bool InsertedArguments = false; |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 1455 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1456 | BufPtr MyEnd = BlockEnd; |
| 1457 | while ( At < MyEnd ) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1458 | unsigned Type, Size; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1459 | BufPtr OldAt = At; |
| 1460 | read_block(Type, Size); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1461 | |
| 1462 | switch (Type) { |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 1463 | case BytecodeFormat::ConstantPool: |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1464 | if (!InsertedArguments) { |
| 1465 | // Insert arguments into the value table before we parse the first basic |
| 1466 | // block in the function, but after we potentially read in the |
| 1467 | // compaction table. |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1468 | insertArguments(F); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1469 | InsertedArguments = true; |
| 1470 | } |
| 1471 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1472 | ParseConstantPool(FunctionValues, FunctionTypes, true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1473 | break; |
| 1474 | |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1475 | case BytecodeFormat::CompactionTable: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1476 | ParseCompactionTable(); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1477 | break; |
| 1478 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1479 | case BytecodeFormat::BasicBlock: { |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1480 | if (!InsertedArguments) { |
| 1481 | // Insert arguments into the value table before we parse the first basic |
| 1482 | // block in the function, but after we potentially read in the |
| 1483 | // compaction table. |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1484 | insertArguments(F); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1485 | InsertedArguments = true; |
| 1486 | } |
| 1487 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1488 | BasicBlock *BB = ParseBasicBlock(BlockNum++); |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 1489 | F->getBasicBlockList().push_back(BB); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1490 | break; |
| 1491 | } |
| 1492 | |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 1493 | case BytecodeFormat::InstructionList: { |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1494 | // Insert arguments into the value table before we parse the instruction |
| 1495 | // list for the function, but after we potentially read in the compaction |
| 1496 | // table. |
| 1497 | if (!InsertedArguments) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1498 | insertArguments(F); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1499 | InsertedArguments = true; |
| 1500 | } |
| 1501 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1502 | if (BlockNum) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1503 | throw std::string("Already parsed basic blocks!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1504 | BlockNum = ParseInstructionList(F); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 1505 | break; |
| 1506 | } |
| 1507 | |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 1508 | case BytecodeFormat::SymbolTable: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1509 | ParseSymbolTable(F, &F->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1510 | break; |
| 1511 | |
| 1512 | default: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1513 | At += Size; |
| 1514 | if (OldAt > At) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1515 | throw std::string("Wrapped around reading bytecode."); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1516 | break; |
| 1517 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1518 | BlockEnd = MyEnd; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 1519 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 1520 | // Malformed bc file if read past end of block. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1521 | align32(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 1524 | // Make sure there were no references to non-existant basic blocks. |
| 1525 | if (BlockNum != ParsedBasicBlocks.size()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1526 | throw std::string("Illegal basic block operand reference"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 1528 | ParsedBasicBlocks.clear(); |
| 1529 | |
Chris Lattner | 97330cf | 2003-10-09 23:10:14 +0000 | [diff] [blame] | 1530 | // Resolve forward references. Replace any uses of a forward reference value |
| 1531 | // with the real value. |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 1532 | |
Chris Lattner | 97330cf | 2003-10-09 23:10:14 +0000 | [diff] [blame] | 1533 | // replaceAllUsesWith is very inefficient for instructions which have a LARGE |
| 1534 | // number of operands. PHI nodes often have forward references, and can also |
| 1535 | // often have a very large number of operands. |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1536 | // |
| 1537 | // FIXME: REEVALUATE. replaceAllUsesWith is _much_ faster now, and this code |
| 1538 | // should be simplified back to using it! |
| 1539 | // |
Chris Lattner | 97330cf | 2003-10-09 23:10:14 +0000 | [diff] [blame] | 1540 | std::map<Value*, Value*> ForwardRefMapping; |
| 1541 | for (std::map<std::pair<unsigned,unsigned>, Value*>::iterator |
| 1542 | I = ForwardReferences.begin(), E = ForwardReferences.end(); |
| 1543 | I != E; ++I) |
| 1544 | ForwardRefMapping[I->second] = getValue(I->first.first, I->first.second, |
| 1545 | false); |
| 1546 | |
| 1547 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 1548 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 1549 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 1550 | if (Argument *A = dyn_cast<Argument>(I->getOperand(i))) { |
| 1551 | std::map<Value*, Value*>::iterator It = ForwardRefMapping.find(A); |
| 1552 | if (It != ForwardRefMapping.end()) I->setOperand(i, It->second); |
| 1553 | } |
| 1554 | |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 1555 | while (!ForwardReferences.empty()) { |
Chris Lattner | 35d2ca6 | 2003-10-09 22:39:30 +0000 | [diff] [blame] | 1556 | std::map<std::pair<unsigned,unsigned>, Value*>::iterator I = |
| 1557 | ForwardReferences.begin(); |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 1558 | Value *PlaceHolder = I->second; |
| 1559 | ForwardReferences.erase(I); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1560 | |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 1561 | // Now that all the uses are gone, delete the placeholder... |
| 1562 | // If we couldn't find a def (error case), then leak a little |
| 1563 | // memory, because otherwise we can't remove all uses! |
| 1564 | delete PlaceHolder; |
Chris Lattner | 6e44802 | 2003-10-08 21:51:46 +0000 | [diff] [blame] | 1565 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1566 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 1567 | // Clear out function-level types... |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1568 | FunctionTypes.clear(); |
| 1569 | CompactionTypes.clear(); |
| 1570 | CompactionValues.clear(); |
| 1571 | freeTable(FunctionValues); |
| 1572 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1573 | if (Handler) Handler->handleFunctionEnd(F); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1576 | /// This function parses LLVM functions lazily. It obtains the type of the |
| 1577 | /// function and records where the body of the function is in the bytecode |
| 1578 | /// buffer. The caller can then use the ParseNextFunction and |
| 1579 | /// ParseAllFunctionBodies to get handler events for the functions. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1580 | void BytecodeReader::ParseFunctionLazily() { |
| 1581 | if (FunctionSignatureList.empty()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1582 | throw std::string("FunctionSignatureList empty!"); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1583 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1584 | Function *Func = FunctionSignatureList.back(); |
| 1585 | FunctionSignatureList.pop_back(); |
Chris Lattner | 2410243 | 2004-01-18 22:35:34 +0000 | [diff] [blame] | 1586 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1587 | // Save the information for future reading of the function |
| 1588 | LazyFunctionLoadMap[Func] = LazyFunctionInfo(BlockStart, BlockEnd); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1589 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1590 | // Pretend we've `parsed' this function |
| 1591 | At = BlockEnd; |
| 1592 | } |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1593 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1594 | /// The ParserFunction method lazily parses one function. Use this method to |
| 1595 | /// casue the parser to parse a specific function in the module. Note that |
| 1596 | /// this will remove the function from what is to be included by |
| 1597 | /// ParseAllFunctionBodies. |
| 1598 | /// @see ParseAllFunctionBodies |
| 1599 | /// @see ParseBytecode |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1600 | void BytecodeReader::ParseFunction(Function* Func) { |
| 1601 | // Find {start, end} pointers and slot in the map. If not there, we're done. |
| 1602 | LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1603 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1604 | // Make sure we found it |
| 1605 | if ( Fi == LazyFunctionLoadMap.end() ) { |
| 1606 | PARSE_ERROR("Unrecognized function of type " << Func->getType()->getDescription()); |
| 1607 | return; |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1610 | BlockStart = At = Fi->second.Buf; |
| 1611 | BlockEnd = Fi->second.EndBuf; |
| 1612 | assert(Fi->first == Func); |
| 1613 | |
| 1614 | LazyFunctionLoadMap.erase(Fi); |
| 1615 | |
| 1616 | this->ParseFunctionBody( Func ); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1619 | /// The ParseAllFunctionBodies method parses through all the previously |
| 1620 | /// unparsed functions in the bytecode file. If you want to completely parse |
| 1621 | /// a bytecode file, this method should be called after Parsebytecode because |
| 1622 | /// Parsebytecode only records the locations in the bytecode file of where |
| 1623 | /// the function definitions are located. This function uses that information |
| 1624 | /// to materialize the functions. |
| 1625 | /// @see ParseBytecode |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1626 | void BytecodeReader::ParseAllFunctionBodies() { |
| 1627 | LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin(); |
| 1628 | LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end(); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1629 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1630 | while ( Fi != Fe ) { |
| 1631 | Function* Func = Fi->first; |
| 1632 | BlockStart = At = Fi->second.Buf; |
| 1633 | BlockEnd = Fi->second.EndBuf; |
| 1634 | this->ParseFunctionBody(Func); |
| 1635 | ++Fi; |
| 1636 | } |
| 1637 | } |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 1638 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1639 | /// Parse the global type list |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1640 | void BytecodeReader::ParseGlobalTypes() { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1641 | // Read the number of types |
| 1642 | unsigned NumEntries = read_vbr_uint(); |
| 1643 | ParseTypeConstants(ModuleTypes, NumEntries); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1646 | /// Parse the Global info (types, global vars, constants) |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1647 | void BytecodeReader::ParseModuleGlobalInfo() { |
| 1648 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1649 | if (Handler) Handler->handleModuleGlobalsBegin(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1651 | // Read global variables... |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1652 | unsigned VarType = read_vbr_uint(); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1653 | while (VarType != Type::VoidTyID) { // List is terminated by Void |
Chris Lattner | 9dd8770 | 2004-04-03 23:43:42 +0000 | [diff] [blame] | 1654 | // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3,4 = |
| 1655 | // Linkage, bit4+ = slot# |
| 1656 | unsigned SlotNo = VarType >> 5; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1657 | bool isTypeType = sanitizeTypeId(SlotNo); |
| 1658 | assert(!isTypeType && "Invalid type (type type) for global var!"); |
Chris Lattner | 9dd8770 | 2004-04-03 23:43:42 +0000 | [diff] [blame] | 1659 | unsigned LinkageID = (VarType >> 2) & 7; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1660 | bool isConstant = VarType & 1; |
| 1661 | bool hasInitializer = VarType & 2; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 1662 | GlobalValue::LinkageTypes Linkage; |
| 1663 | |
Chris Lattner | c08912f | 2004-01-14 16:44:44 +0000 | [diff] [blame] | 1664 | switch (LinkageID) { |
Chris Lattner | c08912f | 2004-01-14 16:44:44 +0000 | [diff] [blame] | 1665 | case 0: Linkage = GlobalValue::ExternalLinkage; break; |
| 1666 | case 1: Linkage = GlobalValue::WeakLinkage; break; |
| 1667 | case 2: Linkage = GlobalValue::AppendingLinkage; break; |
| 1668 | case 3: Linkage = GlobalValue::InternalLinkage; break; |
| 1669 | case 4: Linkage = GlobalValue::LinkOnceLinkage; break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1670 | default: |
| 1671 | PARSE_ERROR("Unknown linkage type: " << LinkageID); |
| 1672 | Linkage = GlobalValue::InternalLinkage; |
| 1673 | break; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | const Type *Ty = getType(SlotNo); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1677 | if ( !Ty ) { |
| 1678 | PARSE_ERROR("Global has no type! SlotNo=" << SlotNo); |
| 1679 | } |
| 1680 | |
| 1681 | if ( !isa<PointerType>(Ty)) { |
| 1682 | PARSE_ERROR("Global not a pointer type! Ty= " << Ty->getDescription()); |
| 1683 | } |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1684 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1685 | const Type *ElTy = cast<PointerType>(Ty)->getElementType(); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1686 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1687 | // Create the global variable... |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1688 | GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage, |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1689 | 0, "", TheModule); |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 1690 | insertValue(GV, SlotNo, ModuleValues); |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 1691 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1692 | unsigned initSlot = 0; |
| 1693 | if (hasInitializer) { |
| 1694 | initSlot = read_vbr_uint(); |
| 1695 | GlobalInits.push_back(std::make_pair(GV, initSlot)); |
| 1696 | } |
| 1697 | |
| 1698 | // Notify handler about the global value. |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1699 | if (Handler) Handler->handleGlobalVariable( ElTy, isConstant, Linkage, SlotNo, initSlot ); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1700 | |
| 1701 | // Get next item |
| 1702 | VarType = read_vbr_uint(); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1705 | // Read the function objects for all of the functions that are coming |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1706 | unsigned FnSignature = 0; |
| 1707 | bool isTypeType = read_typeid(FnSignature); |
| 1708 | assert(!isTypeType && "Invalid function type (type type) found"); |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 1709 | while (FnSignature != Type::VoidTyID) { // List is terminated by Void |
| 1710 | const Type *Ty = getType(FnSignature); |
Chris Lattner | 927b185 | 2003-10-09 20:22:47 +0000 | [diff] [blame] | 1711 | if (!isa<PointerType>(Ty) || |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1712 | !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) { |
| 1713 | PARSE_ERROR( "Function not a pointer to function type! Ty = " + |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 1714 | Ty->getDescription()); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1715 | // FIXME: what should Ty be if handler continues? |
| 1716 | } |
Chris Lattner | 8cdc6b7 | 2002-10-23 00:51:54 +0000 | [diff] [blame] | 1717 | |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 1718 | // We create functions by passing the underlying FunctionType to create... |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1719 | const FunctionType* FTy = |
| 1720 | cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1721 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1722 | // Insert the place hodler |
| 1723 | Function* Func = new Function(FTy, GlobalValue::InternalLinkage, |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1724 | "", TheModule); |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 1725 | insertValue(Func, FnSignature, ModuleValues); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1726 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1727 | // Save this for later so we know type of lazily instantiated functions |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 1728 | FunctionSignatureList.push_back(Func); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1729 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1730 | if (Handler) Handler->handleFunctionDeclaration(Func); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Get Next function signature |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1733 | isTypeType = read_typeid(FnSignature); |
| 1734 | assert(!isTypeType && "Invalid function type (type type) found"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Chris Lattner | 44d0eeb | 2004-01-15 17:55:01 +0000 | [diff] [blame] | 1737 | if (hasInconsistentModuleGlobalInfo) |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1738 | align32(); |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 1739 | |
| 1740 | // Now that the function signature list is set up, reverse it so that we can |
| 1741 | // remove elements efficiently from the back of the vector. |
| 1742 | std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1743 | |
| 1744 | // This is for future proofing... in the future extra fields may be added that |
| 1745 | // we don't understand, so we transparently ignore them. |
| 1746 | // |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1747 | At = BlockEnd; |
| 1748 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1749 | if (Handler) Handler->handleModuleGlobalsEnd(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1752 | /// Parse the version information and decode it by setting flags on the |
| 1753 | /// Reader that enable backward compatibility of the reader. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1754 | void BytecodeReader::ParseVersionInfo() { |
| 1755 | unsigned Version = read_vbr_uint(); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 1756 | |
| 1757 | // Unpack version number: low four bits are for flags, top bits = version |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 1758 | Module::Endianness Endianness; |
| 1759 | Module::PointerSize PointerSize; |
| 1760 | Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian; |
| 1761 | PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32; |
| 1762 | |
| 1763 | bool hasNoEndianness = Version & 4; |
| 1764 | bool hasNoPointerSize = Version & 8; |
| 1765 | |
| 1766 | RevisionNum = Version >> 4; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 1767 | |
| 1768 | // Default values for the current bytecode version |
Chris Lattner | 44d0eeb | 2004-01-15 17:55:01 +0000 | [diff] [blame] | 1769 | hasInconsistentModuleGlobalInfo = false; |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 1770 | hasExplicitPrimitiveZeros = false; |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame] | 1771 | hasRestrictedGEPTypes = false; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1772 | hasTypeDerivedFromValue = false; |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 1773 | |
| 1774 | switch (RevisionNum) { |
Chris Lattner | c08912f | 2004-01-14 16:44:44 +0000 | [diff] [blame] | 1775 | case 0: // LLVM 1.0, 1.1 release version |
Chris Lattner | 9e893e8 | 2004-01-14 23:35:21 +0000 | [diff] [blame] | 1776 | // Base LLVM 1.0 bytecode format. |
Chris Lattner | 44d0eeb | 2004-01-15 17:55:01 +0000 | [diff] [blame] | 1777 | hasInconsistentModuleGlobalInfo = true; |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 1778 | hasExplicitPrimitiveZeros = true; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1779 | |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 1780 | // FALL THROUGH |
Chris Lattner | c08912f | 2004-01-14 16:44:44 +0000 | [diff] [blame] | 1781 | case 1: // LLVM 1.2 release version |
Chris Lattner | 9e893e8 | 2004-01-14 23:35:21 +0000 | [diff] [blame] | 1782 | // LLVM 1.2 added explicit support for emitting strings efficiently. |
Chris Lattner | 44d0eeb | 2004-01-15 17:55:01 +0000 | [diff] [blame] | 1783 | |
| 1784 | // Also, it fixed the problem where the size of the ModuleGlobalInfo block |
| 1785 | // included the size for the alignment at the end, where the rest of the |
| 1786 | // blocks did not. |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame] | 1787 | |
| 1788 | // LLVM 1.2 and before required that GEP indices be ubyte constants for |
| 1789 | // structures and longs for sequential types. |
| 1790 | hasRestrictedGEPTypes = true; |
| 1791 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1792 | // LLVM 1.2 and before had the Type class derive from Value class. This |
| 1793 | // changed in release 1.3 and consequently LLVM 1.3 bytecode files are |
| 1794 | // written differently because Types can no longer be part of the |
| 1795 | // type planes for Values. |
| 1796 | hasTypeDerivedFromValue = true; |
| 1797 | |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame] | 1798 | // FALL THROUGH |
| 1799 | case 2: // LLVM 1.3 release version |
Chris Lattner | c08912f | 2004-01-14 16:44:44 +0000 | [diff] [blame] | 1800 | break; |
| 1801 | |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 1802 | default: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1803 | PARSE_ERROR("Unknown bytecode version number: " << RevisionNum); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 1806 | if (hasNoEndianness) Endianness = Module::AnyEndianness; |
| 1807 | if (hasNoPointerSize) PointerSize = Module::AnyPointerSize; |
Chris Lattner | 76e3896 | 2003-04-22 18:15:10 +0000 | [diff] [blame] | 1808 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1809 | if (Handler) Handler->handleVersionInfo(RevisionNum, Endianness, PointerSize ); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1812 | /// Parse a whole module. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1813 | void BytecodeReader::ParseModule() { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1814 | unsigned Type, Size; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1815 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1816 | FunctionSignatureList.clear(); // Just in case... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1817 | |
| 1818 | // Read into instance variables... |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1819 | ParseVersionInfo(); |
| 1820 | align32(); /// FIXME: Is this redundant? VI is first and 4 bytes! |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1821 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1822 | bool SeenModuleGlobalInfo = false; |
| 1823 | bool SeenGlobalTypePlane = false; |
| 1824 | BufPtr MyEnd = BlockEnd; |
| 1825 | while (At < MyEnd) { |
| 1826 | BufPtr OldAt = At; |
| 1827 | read_block(Type, Size); |
| 1828 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1829 | switch (Type) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1830 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1831 | case BytecodeFormat::GlobalTypePlane: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1832 | if ( SeenGlobalTypePlane ) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1833 | throw std::string("Two GlobalTypePlane Blocks Encountered!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1834 | |
| 1835 | ParseGlobalTypes(); |
| 1836 | SeenGlobalTypePlane = true; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1837 | break; |
| 1838 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1839 | case BytecodeFormat::ModuleGlobalInfo: |
| 1840 | if ( SeenModuleGlobalInfo ) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1841 | throw std::string("Two ModuleGlobalInfo Blocks Encountered!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1842 | ParseModuleGlobalInfo(); |
| 1843 | SeenModuleGlobalInfo = true; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 1846 | case BytecodeFormat::ConstantPool: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1847 | ParseConstantPool(ModuleValues, ModuleTypes,false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1848 | break; |
| 1849 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1850 | case BytecodeFormat::Function: |
| 1851 | ParseFunctionLazily(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1852 | break; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1853 | |
| 1854 | case BytecodeFormat::SymbolTable: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1855 | ParseSymbolTable(0, &TheModule->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1856 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1857 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1858 | default: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1859 | At += Size; |
| 1860 | if (OldAt > At) { |
| 1861 | PARSE_ERROR("Unexpected Block of Type" << Type << "encountered!" ); |
| 1862 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1863 | break; |
| 1864 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1865 | BlockEnd = MyEnd; |
| 1866 | align32(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1869 | // After the module constant pool has been read, we can safely initialize |
| 1870 | // global variables... |
| 1871 | while (!GlobalInits.empty()) { |
| 1872 | GlobalVariable *GV = GlobalInits.back().first; |
| 1873 | unsigned Slot = GlobalInits.back().second; |
| 1874 | GlobalInits.pop_back(); |
| 1875 | |
| 1876 | // Look up the initializer value... |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 1877 | // FIXME: Preserve this type ID! |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1878 | |
| 1879 | const llvm::PointerType* GVType = GV->getType(); |
| 1880 | unsigned TypeSlot = getTypeSlot(GVType->getElementType()); |
Chris Lattner | 9336199 | 2004-01-15 18:45:25 +0000 | [diff] [blame] | 1881 | if (Constant *CV = getConstantValue(TypeSlot, Slot)) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 1882 | if (GV->hasInitializer()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1883 | throw std::string("Global *already* has an initializer?!"); |
| 1884 | if (Handler) Handler->handleGlobalInitializer(GV,CV); |
Chris Lattner | 9336199 | 2004-01-15 18:45:25 +0000 | [diff] [blame] | 1885 | GV->setInitializer(CV); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1886 | } else |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1887 | throw std::string("Cannot find initializer value."); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1890 | /// Make sure we pulled them all out. If we didn't then there's a declaration |
| 1891 | /// but a missing body. That's not allowed. |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 1892 | if (!FunctionSignatureList.empty()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1893 | throw std::string( |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1894 | "Function declared, but bytecode stream ended before definition"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1897 | /// This function completely parses a bytecode buffer given by the \p Buf |
| 1898 | /// and \p Length parameters. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1899 | void BytecodeReader::ParseBytecode( |
| 1900 | BufPtr Buf, unsigned Length, |
| 1901 | const std::string &ModuleID) { |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 1902 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1903 | try { |
| 1904 | At = MemStart = BlockStart = Buf; |
| 1905 | MemEnd = BlockEnd = Buf + Length; |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 1906 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1907 | // Create the module |
| 1908 | TheModule = new Module(ModuleID); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1909 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1910 | if (Handler) Handler->handleStart(TheModule, Length); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1911 | |
| 1912 | // Read and check signature... |
| 1913 | unsigned Sig = read_uint(); |
| 1914 | if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24))) { |
| 1915 | PARSE_ERROR("Invalid bytecode signature: " << Sig); |
| 1916 | } |
| 1917 | |
| 1918 | |
| 1919 | // Tell the handler we're starting a module |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1920 | if (Handler) Handler->handleModuleBegin(ModuleID); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1921 | |
| 1922 | // Get the module block and size and verify |
| 1923 | unsigned Type, Size; |
| 1924 | read_block(Type, Size); |
| 1925 | if ( Type != BytecodeFormat::Module ) { |
| 1926 | PARSE_ERROR("Expected Module Block! At: " << unsigned(intptr_t(At)) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1927 | << ", Type:" << Type << ", Size:" << Size); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1928 | } |
| 1929 | if ( At + Size != MemEnd ) { |
| 1930 | PARSE_ERROR("Invalid Top Level Block Length! At: " |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1931 | << unsigned(intptr_t(At)) << ", Type:" << Type << ", Size:" << Size); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | // Parse the module contents |
| 1935 | this->ParseModule(); |
| 1936 | |
| 1937 | // Tell the handler we're done |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1938 | if (Handler) Handler->handleModuleEnd(ModuleID); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1939 | |
| 1940 | // Check for missing functions |
| 1941 | if ( hasFunctions() ) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1942 | throw std::string("Function expected, but bytecode stream ended!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1943 | |
| 1944 | // Tell the handler we're |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1945 | if (Handler) Handler->handleFinish(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1946 | |
| 1947 | } catch (std::string& errstr ) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1948 | if (Handler) Handler->handleError(errstr); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1949 | freeState(); |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 1950 | delete TheModule; |
| 1951 | TheModule = 0; |
Chris Lattner | b0b7c0d | 2003-09-26 14:44:52 +0000 | [diff] [blame] | 1952 | throw; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1953 | } catch (...) { |
| 1954 | std::string msg("Unknown Exception Occurred"); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame^] | 1955 | if (Handler) Handler->handleError(msg); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1956 | freeState(); |
| 1957 | delete TheModule; |
| 1958 | TheModule = 0; |
| 1959 | throw msg; |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 1960 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1961 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1962 | |
| 1963 | //===----------------------------------------------------------------------===// |
| 1964 | //=== Default Implementations of Handler Methods |
| 1965 | //===----------------------------------------------------------------------===// |
| 1966 | |
| 1967 | BytecodeHandler::~BytecodeHandler() {} |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1968 | |
| 1969 | // vim: sw=2 |