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