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