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