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