Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 1 | //===- Reader.cpp - Code to read bytecode files ---------------------------===// |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | // |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 12 | // Note that this library should be as fast as possible, reentrant, and |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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" |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 20 | #include "llvm/Assembly/AutoUpgrade.h" |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 21 | #include "llvm/Bytecode/BytecodeHandler.h" |
| 22 | #include "llvm/BasicBlock.h" |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 23 | #include "llvm/CallingConv.h" |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 25 | #include "llvm/InlineAsm.h" |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 26 | #include "llvm/Instructions.h" |
| 27 | #include "llvm/SymbolTable.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 28 | #include "llvm/Bytecode/Format.h" |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 29 | #include "llvm/Config/alloca.h" |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 30 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Compressor.h" |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 34 | #include <sstream> |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 38 | namespace { |
Chris Lattner | cad28bd | 2005-01-29 00:36:19 +0000 | [diff] [blame] | 39 | /// @brief A class for maintaining the slot number definition |
| 40 | /// as a placeholder for the actual definition for forward constants defs. |
| 41 | class ConstantPlaceHolder : public ConstantExpr { |
| 42 | ConstantPlaceHolder(); // DO NOT IMPLEMENT |
| 43 | void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT |
| 44 | public: |
Chris Lattner | 6132332 | 2005-01-31 01:11:13 +0000 | [diff] [blame] | 45 | Use Op; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 46 | ConstantPlaceHolder(const Type *Ty) |
Chris Lattner | 6132332 | 2005-01-31 01:11:13 +0000 | [diff] [blame] | 47 | : ConstantExpr(Ty, Instruction::UserOp1, &Op, 1), |
| 48 | Op(UndefValue::get(Type::IntTy), this) { |
| 49 | } |
Chris Lattner | cad28bd | 2005-01-29 00:36:19 +0000 | [diff] [blame] | 50 | }; |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 51 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 52 | |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 53 | // Provide some details on error |
Reid Spencer | 233fe72 | 2006-08-22 16:09:19 +0000 | [diff] [blame] | 54 | inline void BytecodeReader::error(const std::string& err) { |
| 55 | ErrorMsg = err + " (Vers=" + itostr(RevisionNum) + ", Pos=" |
| 56 | + itostr(At-MemStart) + ")"; |
| 57 | longjmp(context,1); |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
| 61 | // Bytecode Reading Methods |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 64 | /// Determine if the current block being read contains any more data. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 65 | inline bool BytecodeReader::moreInBlock() { |
| 66 | return At < BlockEnd; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 69 | /// 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] | 70 | inline void BytecodeReader::checkPastBlockEnd(const char * block_name) { |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 71 | if (At > BlockEnd) |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 72 | error(std::string("Attempt to read past the end of ") + block_name + |
| 73 | " block."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 74 | } |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 75 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 76 | /// Read a whole unsigned integer |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 77 | inline unsigned BytecodeReader::read_uint() { |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 78 | if (At+4 > BlockEnd) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 79 | error("Ran out of data reading uint!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 80 | At += 4; |
| 81 | return At[-4] | (At[-3] << 8) | (At[-2] << 16) | (At[-1] << 24); |
| 82 | } |
| 83 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 84 | /// Read a variable-bit-rate encoded unsigned integer |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 85 | inline unsigned BytecodeReader::read_vbr_uint() { |
| 86 | unsigned Shift = 0; |
| 87 | unsigned Result = 0; |
| 88 | BufPtr Save = At; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 89 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 90 | do { |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 91 | if (At == BlockEnd) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 92 | error("Ran out of data reading vbr_uint!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 93 | Result |= (unsigned)((*At++) & 0x7F) << Shift; |
| 94 | Shift += 7; |
| 95 | } while (At[-1] & 0x80); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 96 | if (Handler) Handler->handleVBR32(At-Save); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 97 | return Result; |
| 98 | } |
| 99 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 100 | /// Read a variable-bit-rate encoded unsigned 64-bit integer. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 101 | inline uint64_t BytecodeReader::read_vbr_uint64() { |
| 102 | unsigned Shift = 0; |
| 103 | uint64_t Result = 0; |
| 104 | BufPtr Save = At; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 105 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 106 | do { |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 107 | if (At == BlockEnd) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 108 | error("Ran out of data reading vbr_uint64!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 109 | Result |= (uint64_t)((*At++) & 0x7F) << Shift; |
| 110 | Shift += 7; |
| 111 | } while (At[-1] & 0x80); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 112 | if (Handler) Handler->handleVBR64(At-Save); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 113 | return Result; |
| 114 | } |
| 115 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 116 | /// Read a variable-bit-rate encoded signed 64-bit integer. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 117 | inline int64_t BytecodeReader::read_vbr_int64() { |
| 118 | uint64_t R = read_vbr_uint64(); |
| 119 | if (R & 1) { |
| 120 | if (R != 1) |
| 121 | return -(int64_t)(R >> 1); |
| 122 | else // There is no such thing as -0 with integers. "-0" really means |
| 123 | // 0x8000000000000000. |
| 124 | return 1LL << 63; |
| 125 | } else |
| 126 | return (int64_t)(R >> 1); |
| 127 | } |
| 128 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 129 | /// Read a pascal-style string (length followed by text) |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 130 | inline std::string BytecodeReader::read_str() { |
| 131 | unsigned Size = read_vbr_uint(); |
| 132 | const unsigned char *OldAt = At; |
| 133 | At += Size; |
| 134 | if (At > BlockEnd) // Size invalid? |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 135 | error("Ran out of data reading a string!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 136 | return std::string((char*)OldAt, Size); |
| 137 | } |
| 138 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 139 | /// Read an arbitrary block of data |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 140 | inline void BytecodeReader::read_data(void *Ptr, void *End) { |
| 141 | unsigned char *Start = (unsigned char *)Ptr; |
| 142 | unsigned Amount = (unsigned char *)End - Start; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 143 | if (At+Amount > BlockEnd) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 144 | error("Ran out of data!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 145 | std::copy(At, At+Amount, Start); |
| 146 | At += Amount; |
| 147 | } |
| 148 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 149 | /// Read a float value in little-endian order |
| 150 | inline void BytecodeReader::read_float(float& FloatVal) { |
Reid Spencer | ada1618 | 2004-07-25 21:36:26 +0000 | [diff] [blame] | 151 | /// FIXME: This isn't optimal, it has size problems on some platforms |
| 152 | /// where FP is not IEEE. |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 153 | FloatVal = BitsToFloat(At[0] | (At[1] << 8) | (At[2] << 16) | (At[3] << 24)); |
Reid Spencer | ada1618 | 2004-07-25 21:36:26 +0000 | [diff] [blame] | 154 | At+=sizeof(uint32_t); |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /// Read a double value in little-endian order |
| 158 | inline void BytecodeReader::read_double(double& DoubleVal) { |
Reid Spencer | ada1618 | 2004-07-25 21:36:26 +0000 | [diff] [blame] | 159 | /// FIXME: This isn't optimal, it has size problems on some platforms |
| 160 | /// where FP is not IEEE. |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 161 | DoubleVal = BitsToDouble((uint64_t(At[0]) << 0) | (uint64_t(At[1]) << 8) | |
| 162 | (uint64_t(At[2]) << 16) | (uint64_t(At[3]) << 24) | |
| 163 | (uint64_t(At[4]) << 32) | (uint64_t(At[5]) << 40) | |
| 164 | (uint64_t(At[6]) << 48) | (uint64_t(At[7]) << 56)); |
Reid Spencer | ada1618 | 2004-07-25 21:36:26 +0000 | [diff] [blame] | 165 | At+=sizeof(uint64_t); |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 168 | /// Read a block header and obtain its type and size |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 169 | inline void BytecodeReader::read_block(unsigned &Type, unsigned &Size) { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 170 | Size = read_uint(); // Read the header |
| 171 | Type = Size & 0x1F; // mask low order five bits to get type |
| 172 | Size >>= 5; // high order 27 bits is the size |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 173 | BlockStart = At; |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 174 | if (At + Size > BlockEnd) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 175 | error("Attempt to size a block past end of memory"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 176 | BlockEnd = At + Size; |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 177 | if (Handler) Handler->handleBlock(Type, BlockStart, Size); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 180 | //===----------------------------------------------------------------------===// |
| 181 | // IR Lookup Methods |
| 182 | //===----------------------------------------------------------------------===// |
| 183 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 184 | /// Determine if a type id has an implicit null value |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 185 | inline bool BytecodeReader::hasImplicitNull(unsigned TyID) { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 186 | return TyID != Type::LabelTyID && TyID != Type::VoidTyID; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 189 | /// Obtain a type given a typeid and account for things like compaction tables, |
| 190 | /// function level vs module level, and the offsetting for the primitive types. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 191 | const Type *BytecodeReader::getType(unsigned ID) { |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 192 | if (ID < Type::FirstDerivedTyID) |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 193 | if (const Type *T = Type::getPrimitiveType((Type::TypeID)ID)) |
Chris Lattner | 927b185 | 2003-10-09 20:22:47 +0000 | [diff] [blame] | 194 | return T; // Asked for a primitive type... |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 195 | |
| 196 | // Otherwise, derived types need offset... |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 197 | ID -= Type::FirstDerivedTyID; |
| 198 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 199 | if (!CompactionTypes.empty()) { |
| 200 | if (ID >= CompactionTypes.size()) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 201 | error("Type ID out of range for compaction table!"); |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 202 | return CompactionTypes[ID].first; |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 203 | } |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 204 | |
| 205 | // Is it a module-level type? |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 206 | if (ID < ModuleTypes.size()) |
| 207 | return ModuleTypes[ID].get(); |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 208 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 209 | // Nope, is it a function-level type? |
| 210 | ID -= ModuleTypes.size(); |
| 211 | if (ID < FunctionTypes.size()) |
| 212 | return FunctionTypes[ID].get(); |
Chris Lattner | 36392bc | 2003-10-08 21:18:57 +0000 | [diff] [blame] | 213 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 214 | error("Illegal type reference!"); |
| 215 | return Type::VoidTy; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 218 | /// This method just saves some coding. It uses read_vbr_uint to read |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 219 | /// 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] | 220 | /// then calls getType to return the type value. |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 221 | inline const Type* BytecodeReader::readType() { |
| 222 | return getType(read_vbr_uint()); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /// Get the slot number associated with a type accounting for primitive |
| 226 | /// types, compaction tables, and function level vs module level. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 227 | unsigned BytecodeReader::getTypeSlot(const Type *Ty) { |
| 228 | if (Ty->isPrimitiveType()) |
| 229 | return Ty->getTypeID(); |
| 230 | |
| 231 | // Scan the compaction table for the type if needed. |
| 232 | if (!CompactionTypes.empty()) { |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 233 | for (unsigned i = 0, e = CompactionTypes.size(); i != e; ++i) |
| 234 | if (CompactionTypes[i].first == Ty) |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 235 | return Type::FirstDerivedTyID + i; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 237 | error("Couldn't find type specified in compaction table!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // Check the function level types first... |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 241 | TypeListTy::iterator I = std::find(FunctionTypes.begin(), |
| 242 | FunctionTypes.end(), Ty); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 243 | |
| 244 | if (I != FunctionTypes.end()) |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 245 | return Type::FirstDerivedTyID + ModuleTypes.size() + |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 246 | (&*I - &FunctionTypes[0]); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 247 | |
Chris Lattner | eebac5f | 2005-10-03 21:26:53 +0000 | [diff] [blame] | 248 | // If we don't have our cache yet, build it now. |
| 249 | if (ModuleTypeIDCache.empty()) { |
| 250 | unsigned N = 0; |
| 251 | ModuleTypeIDCache.reserve(ModuleTypes.size()); |
| 252 | for (TypeListTy::iterator I = ModuleTypes.begin(), E = ModuleTypes.end(); |
| 253 | I != E; ++I, ++N) |
| 254 | ModuleTypeIDCache.push_back(std::make_pair(*I, N)); |
| 255 | |
| 256 | std::sort(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end()); |
| 257 | } |
| 258 | |
| 259 | // Binary search the cache for the entry. |
| 260 | std::vector<std::pair<const Type*, unsigned> >::iterator IT = |
| 261 | std::lower_bound(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end(), |
| 262 | std::make_pair(Ty, 0U)); |
| 263 | if (IT == ModuleTypeIDCache.end() || IT->first != Ty) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 264 | error("Didn't find type in ModuleTypes."); |
Chris Lattner | eebac5f | 2005-10-03 21:26:53 +0000 | [diff] [blame] | 265 | |
| 266 | return Type::FirstDerivedTyID + IT->second; |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 269 | /// This is just like getType, but when a compaction table is in use, it is |
| 270 | /// ignored. It also ignores function level types. |
| 271 | /// @see getType |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 272 | const Type *BytecodeReader::getGlobalTableType(unsigned Slot) { |
| 273 | if (Slot < Type::FirstDerivedTyID) { |
| 274 | const Type *Ty = Type::getPrimitiveType((Type::TypeID)Slot); |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 275 | if (!Ty) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 276 | error("Not a primitive type ID?"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 277 | return Ty; |
| 278 | } |
| 279 | Slot -= Type::FirstDerivedTyID; |
| 280 | if (Slot >= ModuleTypes.size()) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 281 | error("Illegal compaction table type reference!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 282 | return ModuleTypes[Slot]; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 285 | /// This is just like getTypeSlot, but when a compaction table is in use, it |
| 286 | /// is ignored. It also ignores function level types. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 287 | unsigned BytecodeReader::getGlobalTableTypeSlot(const Type *Ty) { |
| 288 | if (Ty->isPrimitiveType()) |
| 289 | return Ty->getTypeID(); |
Chris Lattner | eebac5f | 2005-10-03 21:26:53 +0000 | [diff] [blame] | 290 | |
| 291 | // If we don't have our cache yet, build it now. |
| 292 | if (ModuleTypeIDCache.empty()) { |
| 293 | unsigned N = 0; |
| 294 | ModuleTypeIDCache.reserve(ModuleTypes.size()); |
| 295 | for (TypeListTy::iterator I = ModuleTypes.begin(), E = ModuleTypes.end(); |
| 296 | I != E; ++I, ++N) |
| 297 | ModuleTypeIDCache.push_back(std::make_pair(*I, N)); |
| 298 | |
| 299 | std::sort(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end()); |
| 300 | } |
| 301 | |
| 302 | // Binary search the cache for the entry. |
| 303 | std::vector<std::pair<const Type*, unsigned> >::iterator IT = |
| 304 | std::lower_bound(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end(), |
| 305 | std::make_pair(Ty, 0U)); |
| 306 | if (IT == ModuleTypeIDCache.end() || IT->first != Ty) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 307 | error("Didn't find type in ModuleTypes."); |
Chris Lattner | eebac5f | 2005-10-03 21:26:53 +0000 | [diff] [blame] | 308 | |
| 309 | return Type::FirstDerivedTyID + IT->second; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 312 | /// Retrieve a value of a given type and slot number, possibly creating |
| 313 | /// it if it doesn't already exist. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 314 | Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) { |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 315 | assert(type != Type::LabelTyID && "getValue() cannot get blocks!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 316 | unsigned Num = oNum; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 318 | // If there is a compaction table active, it defines the low-level numbers. |
| 319 | // If not, the module values define the low-level numbers. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 320 | if (CompactionValues.size() > type && !CompactionValues[type].empty()) { |
| 321 | if (Num < CompactionValues[type].size()) |
| 322 | return CompactionValues[type][Num]; |
| 323 | Num -= CompactionValues[type].size(); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 324 | } else { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 325 | // By default, the global type id is the type id passed in |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 326 | unsigned GlobalTyID = type; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 328 | // If the type plane was compactified, figure out the global type ID by |
| 329 | // adding the derived type ids and the distance. |
| 330 | if (!CompactionTypes.empty() && type >= Type::FirstDerivedTyID) |
| 331 | GlobalTyID = CompactionTypes[type-Type::FirstDerivedTyID].second; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 332 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 333 | if (hasImplicitNull(GlobalTyID)) { |
Chris Lattner | aba5ff5 | 2005-05-05 20:57:00 +0000 | [diff] [blame] | 334 | const Type *Ty = getType(type); |
| 335 | if (!isa<OpaqueType>(Ty)) { |
| 336 | if (Num == 0) |
| 337 | return Constant::getNullValue(Ty); |
| 338 | --Num; |
| 339 | } |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 342 | if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) { |
| 343 | if (Num < ModuleValues[GlobalTyID]->size()) |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 344 | return ModuleValues[GlobalTyID]->getOperand(Num); |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 345 | Num -= ModuleValues[GlobalTyID]->size(); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 346 | } |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 349 | if (FunctionValues.size() > type && |
| 350 | FunctionValues[type] && |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 351 | Num < FunctionValues[type]->size()) |
| 352 | return FunctionValues[type]->getOperand(Num); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 354 | if (!Create) return 0; // Do not create a placeholder? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 355 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 356 | // Did we already create a place holder? |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 357 | std::pair<unsigned,unsigned> KeyValue(type, oNum); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 358 | ForwardReferenceMap::iterator I = ForwardReferences.lower_bound(KeyValue); |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 359 | if (I != ForwardReferences.end() && I->first == KeyValue) |
| 360 | return I->second; // We have already created this placeholder |
| 361 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 362 | // If the type exists (it should) |
| 363 | if (const Type* Ty = getType(type)) { |
| 364 | // Create the place holder |
| 365 | Value *Val = new Argument(Ty); |
| 366 | ForwardReferences.insert(I, std::make_pair(KeyValue, Val)); |
| 367 | return Val; |
| 368 | } |
Reid Spencer | 233fe72 | 2006-08-22 16:09:19 +0000 | [diff] [blame] | 369 | error("Can't create placeholder for value of type slot #" + utostr(type)); |
| 370 | return 0; // just silence warning, error calls longjmp |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 373 | /// This is just like getValue, but when a compaction table is in use, it |
| 374 | /// is ignored. Also, no forward references or other fancy features are |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 375 | /// supported. |
Chris Lattner | 2c6c14d | 2004-08-04 00:19:23 +0000 | [diff] [blame] | 376 | Value* BytecodeReader::getGlobalTableValue(unsigned TyID, unsigned SlotNo) { |
| 377 | if (SlotNo == 0) |
| 378 | return Constant::getNullValue(getType(TyID)); |
| 379 | |
| 380 | if (!CompactionTypes.empty() && TyID >= Type::FirstDerivedTyID) { |
| 381 | TyID -= Type::FirstDerivedTyID; |
| 382 | if (TyID >= CompactionTypes.size()) |
| 383 | error("Type ID out of range for compaction table!"); |
| 384 | TyID = CompactionTypes[TyID].second; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Chris Lattner | 2c6c14d | 2004-08-04 00:19:23 +0000 | [diff] [blame] | 387 | --SlotNo; |
| 388 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 389 | if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 || |
| 390 | SlotNo >= ModuleValues[TyID]->size()) { |
Chris Lattner | 2c6c14d | 2004-08-04 00:19:23 +0000 | [diff] [blame] | 391 | if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0) |
| 392 | error("Corrupt compaction table entry!" |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 393 | + utostr(TyID) + ", " + utostr(SlotNo) + ": " |
Chris Lattner | 2c6c14d | 2004-08-04 00:19:23 +0000 | [diff] [blame] | 394 | + utostr(ModuleValues.size())); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 395 | else |
Chris Lattner | 2c6c14d | 2004-08-04 00:19:23 +0000 | [diff] [blame] | 396 | error("Corrupt compaction table entry!" |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 397 | + utostr(TyID) + ", " + utostr(SlotNo) + ": " |
Chris Lattner | 2c6c14d | 2004-08-04 00:19:23 +0000 | [diff] [blame] | 398 | + utostr(ModuleValues.size()) + ", " |
Reid Spencer | 9a7e0c5 | 2004-08-04 22:56:46 +0000 | [diff] [blame] | 399 | + utohexstr(reinterpret_cast<uint64_t>(((void*)ModuleValues[TyID]))) |
| 400 | + ", " |
Chris Lattner | 2c6c14d | 2004-08-04 00:19:23 +0000 | [diff] [blame] | 401 | + utostr(ModuleValues[TyID]->size())); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 402 | } |
| 403 | return ModuleValues[TyID]->getOperand(SlotNo); |
| 404 | } |
| 405 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 406 | /// Just like getValue, except that it returns a null pointer |
| 407 | /// only on error. It always returns a constant (meaning that if the value is |
| 408 | /// defined, but is not a constant, that is an error). If the specified |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 409 | /// constant hasn't been parsed yet, a placeholder is defined and used. |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 410 | /// Later, after the real value is parsed, the placeholder is eliminated. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 411 | Constant* BytecodeReader::getConstantValue(unsigned TypeSlot, unsigned Slot) { |
| 412 | if (Value *V = getValue(TypeSlot, Slot, false)) |
| 413 | if (Constant *C = dyn_cast<Constant>(V)) |
| 414 | return C; // If we already have the value parsed, just return it |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 415 | else |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 416 | error("Value for slot " + utostr(Slot) + |
Reid Spencer | a86037e | 2004-07-18 00:12:03 +0000 | [diff] [blame] | 417 | " is expected to be a constant!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 389bd04 | 2004-12-09 06:19:44 +0000 | [diff] [blame] | 419 | std::pair<unsigned, unsigned> Key(TypeSlot, Slot); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 420 | ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key); |
| 421 | |
| 422 | if (I != ConstantFwdRefs.end() && I->first == Key) { |
| 423 | return I->second; |
| 424 | } else { |
| 425 | // Create a placeholder for the constant reference and |
| 426 | // keep track of the fact that we have a forward ref to recycle it |
Chris Lattner | 389bd04 | 2004-12-09 06:19:44 +0000 | [diff] [blame] | 427 | Constant *C = new ConstantPlaceHolder(getType(TypeSlot)); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 428 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 429 | // Keep track of the fact that we have a forward ref to recycle it |
| 430 | ConstantFwdRefs.insert(I, std::make_pair(Key, C)); |
| 431 | return C; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | //===----------------------------------------------------------------------===// |
| 436 | // IR Construction Methods |
| 437 | //===----------------------------------------------------------------------===// |
| 438 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 439 | /// As values are created, they are inserted into the appropriate place |
| 440 | /// with this method. The ValueTable argument must be one of ModuleValues |
| 441 | /// or FunctionValues data members of this class. |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 442 | unsigned BytecodeReader::insertValue(Value *Val, unsigned type, |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 443 | ValueTable &ValueTab) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 444 | if (ValueTab.size() <= type) |
| 445 | ValueTab.resize(type+1); |
| 446 | |
| 447 | if (!ValueTab[type]) ValueTab[type] = new ValueList(); |
| 448 | |
| 449 | ValueTab[type]->push_back(Val); |
| 450 | |
Chris Lattner | aba5ff5 | 2005-05-05 20:57:00 +0000 | [diff] [blame] | 451 | bool HasOffset = hasImplicitNull(type) && !isa<OpaqueType>(Val->getType()); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 452 | return ValueTab[type]->size()-1 + HasOffset; |
| 453 | } |
| 454 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 455 | /// Insert the arguments of a function as new values in the reader. |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 456 | void BytecodeReader::insertArguments(Function* F) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 457 | const FunctionType *FT = F->getFunctionType(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 458 | Function::arg_iterator AI = F->arg_begin(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 459 | for (FunctionType::param_iterator It = FT->param_begin(); |
| 460 | It != FT->param_end(); ++It, ++AI) |
| 461 | insertValue(AI, getTypeSlot(AI->getType()), FunctionValues); |
| 462 | } |
| 463 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 464 | /// Convert previous opcode values into the current value and/or construct |
| 465 | /// the instruction. This function handles all *abnormal* cases for instruction |
| 466 | /// generation based on obsolete opcode values. The normal cases are handled |
| 467 | /// in ParseInstruction below. Generally this function just produces a new |
| 468 | /// Opcode value (first argument). In a few cases (VAArg, VANext) the upgrade |
| 469 | /// path requies that the instruction (sequence) be generated differently from |
| 470 | /// the normal case in order to preserve the original semantics. In these |
| 471 | /// cases the result of the function will be a non-zero Instruction pointer. In |
| 472 | /// all other cases, zero will be returned indicating that the *normal* |
| 473 | /// instruction generation should be used, but with the new Opcode value. |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 474 | Instruction* |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 475 | BytecodeReader::upgradeInstrOpcodes( |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 476 | unsigned &Opcode, ///< The old opcode, possibly updated by this function |
| 477 | std::vector<unsigned> &Oprnds, ///< The operands to the instruction |
| 478 | unsigned &iType, ///< The type code from the bytecode file |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 479 | const Type *InstTy, ///< The type of the instruction |
| 480 | BasicBlock *BB ///< The basic block to insert into, if we need to |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 481 | ) { |
| 482 | |
| 483 | // First, short circuit this if no conversion is required. When signless |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 484 | // instructions were implemented the entire opcode sequence was revised in |
| 485 | // two stages: first Div/Rem became signed, then Shr/Cast/Setcc became |
| 486 | // signed. If all of these instructions are signed then we don't have to |
| 487 | // upgrade the opcode. |
| 488 | if (!hasSignlessDivRem && !hasSignlessShrCastSetcc) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 489 | return 0; // The opcode is fine the way it is. |
| 490 | |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 491 | // If this is bytecode version 6, that only had signed Rem and Div |
| 492 | // instructions, then we must compensate for those two instructions only. |
| 493 | // So that the switch statement below works, we're trying to turn this into |
| 494 | // a version 5 opcode. To do that we must adjust the opcode to 10 (Div) if its |
| 495 | // any of the UDiv, SDiv or FDiv instructions; or, adjust the opcode to |
| 496 | // 11 (Rem) if its any of the URem, SRem, or FRem instructions; or, simply |
| 497 | // decrement the instruction code if its beyond FRem. |
| 498 | if (!hasSignlessDivRem) { |
| 499 | // If its one of the signed Div/Rem opcodes, its fine the way it is |
| 500 | if (Opcode >= 10 && Opcode <= 12) // UDiv through FDiv |
| 501 | Opcode = 10; // Div |
| 502 | else if (Opcode >=13 && Opcode <= 15) // URem through FRem |
| 503 | Opcode = 11; // Rem |
| 504 | else if (Opcode >= 16 && Opcode <= 35) // And through Shr |
| 505 | // Adjust for new instruction codes |
| 506 | Opcode -= 4; |
| 507 | else if (Opcode >= 36 && Opcode <= 42) // Everything after Select |
| 508 | // In vers 6 bytecode we eliminated the placeholders for the obsolete |
| 509 | // VAARG and VANEXT instructions. Consequently those two slots were |
| 510 | // filled starting with Select (36) which was 34. So now we only need |
| 511 | // to subtract two. This circumvents hitting opcodes 32 and 33 |
| 512 | Opcode -= 2; |
| 513 | else { // Opcode < 10 or > 42 |
| 514 | // No upgrade necessary. |
| 515 | return 0; |
| 516 | } |
| 517 | } |
| 518 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 519 | // Declare the resulting instruction we might build. In general we just |
| 520 | // change the Opcode argument but in a few cases we need to generate the |
| 521 | // Instruction here because the upgrade case is significantly different from |
| 522 | // the normal case. |
| 523 | Instruction *Result = 0; |
| 524 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 525 | // We're dealing with an upgrade situation. For each of the opcode values, |
| 526 | // perform the necessary conversion. |
| 527 | switch (Opcode) { |
| 528 | default: // Error |
| 529 | // This switch statement provides cases for all known opcodes prior to |
| 530 | // version 6 bytecode format. We know we're in an upgrade situation so |
| 531 | // if there isn't a match in this switch, then something is horribly |
| 532 | // wrong. |
| 533 | error("Unknown obsolete opcode encountered."); |
| 534 | break; |
| 535 | case 1: // Ret |
| 536 | Opcode = Instruction::Ret; |
| 537 | break; |
| 538 | case 2: // Br |
| 539 | Opcode = Instruction::Br; |
| 540 | break; |
| 541 | case 3: // Switch |
| 542 | Opcode = Instruction::Switch; |
| 543 | break; |
| 544 | case 4: // Invoke |
| 545 | Opcode = Instruction::Invoke; |
| 546 | break; |
| 547 | case 5: // Unwind |
| 548 | Opcode = Instruction::Unwind; |
| 549 | break; |
| 550 | case 6: // Unreachable |
| 551 | Opcode = Instruction::Unreachable; |
| 552 | break; |
| 553 | case 7: // Add |
| 554 | Opcode = Instruction::Add; |
| 555 | break; |
| 556 | case 8: // Sub |
| 557 | Opcode = Instruction::Sub; |
| 558 | break; |
| 559 | case 9: // Mul |
| 560 | Opcode = Instruction::Mul; |
| 561 | break; |
| 562 | case 10: // Div |
| 563 | // The type of the instruction is based on the operands. We need to select |
| 564 | // fdiv, udiv or sdiv based on that type. The iType values are hardcoded |
| 565 | // to the values used in bytecode version 5 (and prior) because it is |
| 566 | // likely these codes will change in future versions of LLVM. |
| 567 | if (iType == 10 || iType == 11 ) |
| 568 | Opcode = Instruction::FDiv; |
| 569 | else if (iType >= 2 && iType <= 9 && iType % 2 != 0) |
| 570 | Opcode = Instruction::SDiv; |
| 571 | else |
| 572 | Opcode = Instruction::UDiv; |
| 573 | break; |
| 574 | |
| 575 | case 11: // Rem |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 576 | // As with "Div", make the signed/unsigned or floating point Rem |
| 577 | // instruction choice based on the type of the operands. |
| 578 | if (iType == 10 || iType == 11) |
| 579 | Opcode = Instruction::FRem; |
| 580 | else if (iType >= 2 && iType <= 9 && iType % 2 != 0) |
| 581 | Opcode = Instruction::SRem; |
| 582 | else |
| 583 | Opcode = Instruction::URem; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 584 | break; |
| 585 | case 12: // And |
| 586 | Opcode = Instruction::And; |
| 587 | break; |
| 588 | case 13: // Or |
| 589 | Opcode = Instruction::Or; |
| 590 | break; |
| 591 | case 14: // Xor |
| 592 | Opcode = Instruction::Xor; |
| 593 | break; |
| 594 | case 15: // SetEQ |
| 595 | Opcode = Instruction::SetEQ; |
| 596 | break; |
| 597 | case 16: // SetNE |
| 598 | Opcode = Instruction::SetNE; |
| 599 | break; |
| 600 | case 17: // SetLE |
| 601 | Opcode = Instruction::SetLE; |
| 602 | break; |
| 603 | case 18: // SetGE |
| 604 | Opcode = Instruction::SetGE; |
| 605 | break; |
| 606 | case 19: // SetLT |
| 607 | Opcode = Instruction::SetLT; |
| 608 | break; |
| 609 | case 20: // SetGT |
| 610 | Opcode = Instruction::SetGT; |
| 611 | break; |
| 612 | case 21: // Malloc |
| 613 | Opcode = Instruction::Malloc; |
| 614 | break; |
| 615 | case 22: // Free |
| 616 | Opcode = Instruction::Free; |
| 617 | break; |
| 618 | case 23: // Alloca |
| 619 | Opcode = Instruction::Alloca; |
| 620 | break; |
| 621 | case 24: // Load |
| 622 | Opcode = Instruction::Load; |
| 623 | break; |
| 624 | case 25: // Store |
| 625 | Opcode = Instruction::Store; |
| 626 | break; |
| 627 | case 26: // GetElementPtr |
| 628 | Opcode = Instruction::GetElementPtr; |
| 629 | break; |
| 630 | case 27: // PHI |
| 631 | Opcode = Instruction::PHI; |
| 632 | break; |
| 633 | case 28: // Cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 634 | { |
| 635 | Value *Source = getValue(iType, Oprnds[0]); |
| 636 | const Type *DestTy = getType(Oprnds[1]); |
| 637 | // The previous definition of cast to bool was a compare against zero. |
| 638 | // We have to retain that semantic so we do it here. |
| 639 | if (DestTy == Type::BoolTy) { // if its a cast to bool |
| 640 | Opcode = Instruction::SetNE; |
| 641 | Result = new SetCondInst(Instruction::SetNE, Source, |
| 642 | Constant::getNullValue(Source->getType())); |
| 643 | } else if (Source->getType()->isFloatingPoint() && |
| 644 | isa<PointerType>(DestTy)) { |
| 645 | // Upgrade what is now an illegal cast (fp -> ptr) into two casts, |
| 646 | // fp -> ui, and ui -> ptr |
| 647 | CastInst *CI = new FPToUIInst(Source, Type::ULongTy); |
| 648 | BB->getInstList().push_back(CI); |
| 649 | Result = new IntToPtrInst(CI, DestTy); |
| 650 | } else { |
| 651 | Result = CastInst::createInferredCast(Source, DestTy); |
| 652 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 653 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 654 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 655 | case 29: // Call |
| 656 | Opcode = Instruction::Call; |
| 657 | break; |
| 658 | case 30: // Shl |
| 659 | Opcode = Instruction::Shl; |
| 660 | break; |
| 661 | case 31: // Shr |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 662 | // The type of the instruction is based on the operands. We need to |
| 663 | // select ashr or lshr based on that type. The iType values are hardcoded |
| 664 | // to the values used in bytecode version 5 (and prior) because it is |
| 665 | // likely these codes will change in future versions of LLVM. This if |
| 666 | // statement says "if (integer type and signed)" |
| 667 | if (iType >= 2 && iType <= 9 && iType % 2 != 0) |
| 668 | Opcode = Instruction::AShr; |
| 669 | else |
| 670 | Opcode = Instruction::LShr; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 671 | break; |
| 672 | case 32: { //VANext_old ( <= llvm 1.5 ) |
| 673 | const Type* ArgTy = getValue(iType, Oprnds[0])->getType(); |
| 674 | Function* NF = TheModule->getOrInsertFunction( |
| 675 | "llvm.va_copy", ArgTy, ArgTy, (Type *)0); |
| 676 | |
| 677 | // In llvm 1.6 the VANext instruction was dropped because it was only |
| 678 | // necessary to have a VAArg instruction. The code below transforms an |
| 679 | // old vanext instruction into the equivalent code given only the |
| 680 | // availability of the new vaarg instruction. Essentially, the transform |
| 681 | // is as follows: |
| 682 | // b = vanext a, t -> |
| 683 | // foo = alloca 1 of t |
| 684 | // bar = vacopy a |
| 685 | // store bar -> foo |
| 686 | // tmp = vaarg foo, t |
| 687 | // b = load foo |
| 688 | AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix"); |
| 689 | BB->getInstList().push_back(foo); |
| 690 | CallInst* bar = new CallInst(NF, getValue(iType, Oprnds[0])); |
| 691 | BB->getInstList().push_back(bar); |
| 692 | BB->getInstList().push_back(new StoreInst(bar, foo)); |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 693 | Instruction* tmp = new VAArgInst(foo, getType(Oprnds[1])); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 694 | BB->getInstList().push_back(tmp); |
| 695 | Result = new LoadInst(foo); |
| 696 | break; |
| 697 | } |
| 698 | case 33: { //VAArg_old |
| 699 | const Type* ArgTy = getValue(iType, Oprnds[0])->getType(); |
| 700 | Function* NF = TheModule->getOrInsertFunction( |
| 701 | "llvm.va_copy", ArgTy, ArgTy, (Type *)0); |
| 702 | |
| 703 | // In llvm 1.6 the VAArg's instruction semantics were changed. The code |
| 704 | // below transforms an old vaarg instruction into the equivalent code |
| 705 | // given only the availability of the new vaarg instruction. Essentially, |
| 706 | // the transform is as follows: |
| 707 | // b = vaarg a, t -> |
| 708 | // foo = alloca 1 of t |
| 709 | // bar = vacopy a |
| 710 | // store bar -> foo |
| 711 | // b = vaarg foo, t |
| 712 | AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix"); |
| 713 | BB->getInstList().push_back(foo); |
| 714 | CallInst* bar = new CallInst(NF, getValue(iType, Oprnds[0])); |
| 715 | BB->getInstList().push_back(bar); |
| 716 | BB->getInstList().push_back(new StoreInst(bar, foo)); |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 717 | Result = new VAArgInst(foo, getType(Oprnds[1])); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 718 | break; |
| 719 | } |
| 720 | case 34: // Select |
| 721 | Opcode = Instruction::Select; |
| 722 | break; |
| 723 | case 35: // UserOp1 |
| 724 | Opcode = Instruction::UserOp1; |
| 725 | break; |
| 726 | case 36: // UserOp2 |
| 727 | Opcode = Instruction::UserOp2; |
| 728 | break; |
| 729 | case 37: // VAArg |
| 730 | Opcode = Instruction::VAArg; |
| 731 | break; |
| 732 | case 38: // ExtractElement |
| 733 | Opcode = Instruction::ExtractElement; |
| 734 | break; |
| 735 | case 39: // InsertElement |
| 736 | Opcode = Instruction::InsertElement; |
| 737 | break; |
| 738 | case 40: // ShuffleVector |
| 739 | Opcode = Instruction::ShuffleVector; |
| 740 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 741 | case 56: // Invoke with encoded CC |
| 742 | case 57: { // Invoke Fast CC |
| 743 | if (Oprnds.size() < 3) |
| 744 | error("Invalid invoke instruction!"); |
| 745 | Value *F = getValue(iType, Oprnds[0]); |
| 746 | |
| 747 | // Check to make sure we have a pointer to function type |
| 748 | const PointerType *PTy = dyn_cast<PointerType>(F->getType()); |
| 749 | if (PTy == 0) |
| 750 | error("Invoke to non function pointer value!"); |
| 751 | const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType()); |
| 752 | if (FTy == 0) |
| 753 | error("Invoke to non function pointer value!"); |
| 754 | |
| 755 | std::vector<Value *> Params; |
| 756 | BasicBlock *Normal, *Except; |
| 757 | unsigned CallingConv = CallingConv::C; |
| 758 | if (Opcode == 57) |
| 759 | CallingConv = CallingConv::Fast; |
| 760 | else if (Opcode == 56) { |
| 761 | CallingConv = Oprnds.back(); |
| 762 | Oprnds.pop_back(); |
| 763 | } |
| 764 | Opcode = Instruction::Invoke; |
| 765 | |
| 766 | if (!FTy->isVarArg()) { |
| 767 | Normal = getBasicBlock(Oprnds[1]); |
| 768 | Except = getBasicBlock(Oprnds[2]); |
| 769 | |
| 770 | FunctionType::param_iterator It = FTy->param_begin(); |
| 771 | for (unsigned i = 3, e = Oprnds.size(); i != e; ++i) { |
| 772 | if (It == FTy->param_end()) |
| 773 | error("Invalid invoke instruction!"); |
| 774 | Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i])); |
| 775 | } |
| 776 | if (It != FTy->param_end()) |
| 777 | error("Invalid invoke instruction!"); |
| 778 | } else { |
| 779 | Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1); |
| 780 | |
| 781 | Normal = getBasicBlock(Oprnds[0]); |
| 782 | Except = getBasicBlock(Oprnds[1]); |
| 783 | |
| 784 | unsigned FirstVariableArgument = FTy->getNumParams()+2; |
| 785 | for (unsigned i = 2; i != FirstVariableArgument; ++i) |
| 786 | Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)), |
| 787 | Oprnds[i])); |
| 788 | |
| 789 | // Must be type/value pairs. If not, error out. |
| 790 | if (Oprnds.size()-FirstVariableArgument & 1) |
| 791 | error("Invalid invoke instruction!"); |
| 792 | |
| 793 | for (unsigned i = FirstVariableArgument; i < Oprnds.size(); i += 2) |
| 794 | Params.push_back(getValue(Oprnds[i], Oprnds[i+1])); |
| 795 | } |
| 796 | |
| 797 | Result = new InvokeInst(F, Normal, Except, Params); |
| 798 | if (CallingConv) cast<InvokeInst>(Result)->setCallingConv(CallingConv); |
| 799 | break; |
| 800 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 801 | case 58: // Call with extra operand for calling conv |
| 802 | case 59: // tail call, Fast CC |
| 803 | case 60: // normal call, Fast CC |
| 804 | case 61: // tail call, C Calling Conv |
| 805 | case 62: // volatile load |
| 806 | case 63: // volatile store |
| 807 | // In all these cases, we pass the opcode through. The new version uses |
| 808 | // the same code (for now, this might change in 2.0). These are listed |
| 809 | // here to document the opcodes in use in vers 5 bytecode and to make it |
| 810 | // easier to migrate these opcodes in the future. |
| 811 | break; |
| 812 | } |
| 813 | return Result; |
| 814 | } |
| 815 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 816 | //===----------------------------------------------------------------------===// |
| 817 | // Bytecode Parsing Methods |
| 818 | //===----------------------------------------------------------------------===// |
| 819 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 820 | /// This method parses a single instruction. The instruction is |
| 821 | /// inserted at the end of the \p BB provided. The arguments of |
Misha Brukman | 44666b1 | 2004-09-28 16:57:46 +0000 | [diff] [blame] | 822 | /// the instruction are provided in the \p Oprnds vector. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 823 | void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds, |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 824 | BasicBlock* BB) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 825 | BufPtr SaveAt = At; |
| 826 | |
| 827 | // Clear instruction data |
| 828 | Oprnds.clear(); |
| 829 | unsigned iType = 0; |
| 830 | unsigned Opcode = 0; |
| 831 | unsigned Op = read_uint(); |
| 832 | |
| 833 | // bits Instruction format: Common to all formats |
| 834 | // -------------------------- |
| 835 | // 01-00: Opcode type, fixed to 1. |
| 836 | // 07-02: Opcode |
| 837 | Opcode = (Op >> 2) & 63; |
| 838 | Oprnds.resize((Op >> 0) & 03); |
| 839 | |
| 840 | // Extract the operands |
| 841 | switch (Oprnds.size()) { |
| 842 | case 1: |
| 843 | // bits Instruction format: |
| 844 | // -------------------------- |
| 845 | // 19-08: Resulting type plane |
| 846 | // 31-20: Operand #1 (if set to (2^12-1), then zero operands) |
| 847 | // |
| 848 | iType = (Op >> 8) & 4095; |
| 849 | Oprnds[0] = (Op >> 20) & 4095; |
| 850 | if (Oprnds[0] == 4095) // Handle special encoding for 0 operands... |
| 851 | Oprnds.resize(0); |
| 852 | break; |
| 853 | case 2: |
| 854 | // bits Instruction format: |
| 855 | // -------------------------- |
| 856 | // 15-08: Resulting type plane |
| 857 | // 23-16: Operand #1 |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 858 | // 31-24: Operand #2 |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 859 | // |
| 860 | iType = (Op >> 8) & 255; |
| 861 | Oprnds[0] = (Op >> 16) & 255; |
| 862 | Oprnds[1] = (Op >> 24) & 255; |
| 863 | break; |
| 864 | case 3: |
| 865 | // bits Instruction format: |
| 866 | // -------------------------- |
| 867 | // 13-08: Resulting type plane |
| 868 | // 19-14: Operand #1 |
| 869 | // 25-20: Operand #2 |
| 870 | // 31-26: Operand #3 |
| 871 | // |
| 872 | iType = (Op >> 8) & 63; |
| 873 | Oprnds[0] = (Op >> 14) & 63; |
| 874 | Oprnds[1] = (Op >> 20) & 63; |
| 875 | Oprnds[2] = (Op >> 26) & 63; |
| 876 | break; |
| 877 | case 0: |
| 878 | At -= 4; // Hrm, try this again... |
| 879 | Opcode = read_vbr_uint(); |
| 880 | Opcode >>= 2; |
| 881 | iType = read_vbr_uint(); |
| 882 | |
| 883 | unsigned NumOprnds = read_vbr_uint(); |
| 884 | Oprnds.resize(NumOprnds); |
| 885 | |
| 886 | if (NumOprnds == 0) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 887 | error("Zero-argument instruction found; this is invalid."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 888 | |
| 889 | for (unsigned i = 0; i != NumOprnds; ++i) |
| 890 | Oprnds[i] = read_vbr_uint(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 891 | break; |
| 892 | } |
| 893 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 894 | const Type *InstTy = getType(iType); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 895 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 896 | // Make the necessary adjustments for dealing with backwards compatibility |
| 897 | // of opcodes. |
| 898 | Instruction* Result = |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 899 | upgradeInstrOpcodes(Opcode, Oprnds, iType, InstTy, BB); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 900 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 901 | // We have enough info to inform the handler now. |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 902 | if (Handler) |
| 903 | Handler->handleInstruction(Opcode, InstTy, Oprnds, At-SaveAt); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 904 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 905 | // If the backwards compatibility code didn't produce an instruction then |
| 906 | // we do the *normal* thing .. |
| 907 | if (!Result) { |
| 908 | // First, handle the easy binary operators case |
| 909 | if (Opcode >= Instruction::BinaryOpsBegin && |
| 910 | Opcode < Instruction::BinaryOpsEnd && Oprnds.size() == 2) |
| 911 | Result = BinaryOperator::create(Instruction::BinaryOps(Opcode), |
| 912 | getValue(iType, Oprnds[0]), |
| 913 | getValue(iType, Oprnds[1])); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 914 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 915 | // Indicate that we don't think this is a call instruction (yet). |
| 916 | // Process based on the Opcode read |
| 917 | switch (Opcode) { |
| 918 | default: // There was an error, this shouldn't happen. |
| 919 | if (Result == 0) |
| 920 | error("Illegal instruction read!"); |
| 921 | break; |
| 922 | case Instruction::VAArg: |
| 923 | if (Oprnds.size() != 2) |
| 924 | error("Invalid VAArg instruction!"); |
| 925 | Result = new VAArgInst(getValue(iType, Oprnds[0]), |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 926 | getType(Oprnds[1])); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 927 | break; |
| 928 | case Instruction::ExtractElement: { |
| 929 | if (Oprnds.size() != 2) |
| 930 | error("Invalid extractelement instruction!"); |
| 931 | Value *V1 = getValue(iType, Oprnds[0]); |
| 932 | Value *V2 = getValue(Type::UIntTyID, Oprnds[1]); |
Chris Lattner | 59fecec | 2006-04-08 04:09:19 +0000 | [diff] [blame] | 933 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 934 | if (!ExtractElementInst::isValidOperands(V1, V2)) |
| 935 | error("Invalid extractelement instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 936 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 937 | Result = new ExtractElementInst(V1, V2); |
| 938 | break; |
Chris Lattner | a65371e | 2006-05-26 18:42:34 +0000 | [diff] [blame] | 939 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 940 | case Instruction::InsertElement: { |
| 941 | const PackedType *PackedTy = dyn_cast<PackedType>(InstTy); |
| 942 | if (!PackedTy || Oprnds.size() != 3) |
| 943 | error("Invalid insertelement instruction!"); |
| 944 | |
| 945 | Value *V1 = getValue(iType, Oprnds[0]); |
| 946 | Value *V2 = getValue(getTypeSlot(PackedTy->getElementType()),Oprnds[1]); |
| 947 | Value *V3 = getValue(Type::UIntTyID, Oprnds[2]); |
| 948 | |
| 949 | if (!InsertElementInst::isValidOperands(V1, V2, V3)) |
| 950 | error("Invalid insertelement instruction!"); |
| 951 | Result = new InsertElementInst(V1, V2, V3); |
| 952 | break; |
| 953 | } |
| 954 | case Instruction::ShuffleVector: { |
| 955 | const PackedType *PackedTy = dyn_cast<PackedType>(InstTy); |
| 956 | if (!PackedTy || Oprnds.size() != 3) |
| 957 | error("Invalid shufflevector instruction!"); |
| 958 | Value *V1 = getValue(iType, Oprnds[0]); |
| 959 | Value *V2 = getValue(iType, Oprnds[1]); |
| 960 | const PackedType *EltTy = |
| 961 | PackedType::get(Type::UIntTy, PackedTy->getNumElements()); |
| 962 | Value *V3 = getValue(getTypeSlot(EltTy), Oprnds[2]); |
| 963 | if (!ShuffleVectorInst::isValidOperands(V1, V2, V3)) |
| 964 | error("Invalid shufflevector instruction!"); |
| 965 | Result = new ShuffleVectorInst(V1, V2, V3); |
| 966 | break; |
| 967 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 968 | case Instruction::Trunc: |
| 969 | if (Oprnds.size() != 2) |
| 970 | error("Invalid cast instruction!"); |
| 971 | Result = new TruncInst(getValue(iType, Oprnds[0]), |
| 972 | getType(Oprnds[1])); |
| 973 | break; |
| 974 | case Instruction::ZExt: |
| 975 | if (Oprnds.size() != 2) |
| 976 | error("Invalid cast instruction!"); |
| 977 | Result = new ZExtInst(getValue(iType, Oprnds[0]), |
| 978 | getType(Oprnds[1])); |
| 979 | break; |
| 980 | case Instruction::SExt: |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 981 | if (Oprnds.size() != 2) |
| 982 | error("Invalid Cast instruction!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 983 | Result = new SExtInst(getValue(iType, Oprnds[0]), |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 984 | getType(Oprnds[1])); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 985 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 986 | case Instruction::FPTrunc: |
| 987 | if (Oprnds.size() != 2) |
| 988 | error("Invalid cast instruction!"); |
| 989 | Result = new FPTruncInst(getValue(iType, Oprnds[0]), |
| 990 | getType(Oprnds[1])); |
| 991 | break; |
| 992 | case Instruction::FPExt: |
| 993 | if (Oprnds.size() != 2) |
| 994 | error("Invalid cast instruction!"); |
| 995 | Result = new FPExtInst(getValue(iType, Oprnds[0]), |
| 996 | getType(Oprnds[1])); |
| 997 | break; |
| 998 | case Instruction::UIToFP: |
| 999 | if (Oprnds.size() != 2) |
| 1000 | error("Invalid cast instruction!"); |
| 1001 | Result = new UIToFPInst(getValue(iType, Oprnds[0]), |
| 1002 | getType(Oprnds[1])); |
| 1003 | break; |
| 1004 | case Instruction::SIToFP: |
| 1005 | if (Oprnds.size() != 2) |
| 1006 | error("Invalid cast instruction!"); |
| 1007 | Result = new SIToFPInst(getValue(iType, Oprnds[0]), |
| 1008 | getType(Oprnds[1])); |
| 1009 | break; |
| 1010 | case Instruction::FPToUI: |
| 1011 | if (Oprnds.size() != 2) |
| 1012 | error("Invalid cast instruction!"); |
| 1013 | Result = new FPToUIInst(getValue(iType, Oprnds[0]), |
| 1014 | getType(Oprnds[1])); |
| 1015 | break; |
| 1016 | case Instruction::FPToSI: |
| 1017 | if (Oprnds.size() != 2) |
| 1018 | error("Invalid cast instruction!"); |
| 1019 | Result = new FPToSIInst(getValue(iType, Oprnds[0]), |
| 1020 | getType(Oprnds[1])); |
| 1021 | break; |
| 1022 | case Instruction::IntToPtr: |
| 1023 | if (Oprnds.size() != 2) |
| 1024 | error("Invalid cast instruction!"); |
| 1025 | Result = new IntToPtrInst(getValue(iType, Oprnds[0]), |
| 1026 | getType(Oprnds[1])); |
| 1027 | break; |
| 1028 | case Instruction::PtrToInt: |
| 1029 | if (Oprnds.size() != 2) |
| 1030 | error("Invalid cast instruction!"); |
| 1031 | Result = new PtrToIntInst(getValue(iType, Oprnds[0]), |
| 1032 | getType(Oprnds[1])); |
| 1033 | break; |
| 1034 | case Instruction::BitCast: |
| 1035 | if (Oprnds.size() != 2) |
| 1036 | error("Invalid cast instruction!"); |
| 1037 | Result = new BitCastInst(getValue(iType, Oprnds[0]), |
| 1038 | getType(Oprnds[1])); |
| 1039 | break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1040 | case Instruction::Select: |
| 1041 | if (Oprnds.size() != 3) |
| 1042 | error("Invalid Select instruction!"); |
| 1043 | Result = new SelectInst(getValue(Type::BoolTyID, Oprnds[0]), |
| 1044 | getValue(iType, Oprnds[1]), |
| 1045 | getValue(iType, Oprnds[2])); |
| 1046 | break; |
| 1047 | case Instruction::PHI: { |
| 1048 | if (Oprnds.size() == 0 || (Oprnds.size() & 1)) |
| 1049 | error("Invalid phi node encountered!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1050 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1051 | PHINode *PN = new PHINode(InstTy); |
| 1052 | PN->reserveOperandSpace(Oprnds.size()); |
| 1053 | for (unsigned i = 0, e = Oprnds.size(); i != e; i += 2) |
| 1054 | PN->addIncoming( |
| 1055 | getValue(iType, Oprnds[i]), getBasicBlock(Oprnds[i+1])); |
| 1056 | Result = PN; |
| 1057 | break; |
| 1058 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1059 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1060 | case Instruction::LShr: |
| 1061 | case Instruction::AShr: |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1062 | Result = new ShiftInst(Instruction::OtherOps(Opcode), |
| 1063 | getValue(iType, Oprnds[0]), |
| 1064 | getValue(Type::UByteTyID, Oprnds[1])); |
| 1065 | break; |
| 1066 | case Instruction::Ret: |
| 1067 | if (Oprnds.size() == 0) |
| 1068 | Result = new ReturnInst(); |
| 1069 | else if (Oprnds.size() == 1) |
| 1070 | Result = new ReturnInst(getValue(iType, Oprnds[0])); |
| 1071 | else |
| 1072 | error("Unrecognized instruction!"); |
| 1073 | break; |
| 1074 | |
| 1075 | case Instruction::Br: |
| 1076 | if (Oprnds.size() == 1) |
| 1077 | Result = new BranchInst(getBasicBlock(Oprnds[0])); |
| 1078 | else if (Oprnds.size() == 3) |
| 1079 | Result = new BranchInst(getBasicBlock(Oprnds[0]), |
| 1080 | getBasicBlock(Oprnds[1]), getValue(Type::BoolTyID , Oprnds[2])); |
| 1081 | else |
| 1082 | error("Invalid number of operands for a 'br' instruction!"); |
| 1083 | break; |
| 1084 | case Instruction::Switch: { |
| 1085 | if (Oprnds.size() & 1) |
| 1086 | error("Switch statement with odd number of arguments!"); |
| 1087 | |
| 1088 | SwitchInst *I = new SwitchInst(getValue(iType, Oprnds[0]), |
| 1089 | getBasicBlock(Oprnds[1]), |
| 1090 | Oprnds.size()/2-1); |
| 1091 | for (unsigned i = 2, e = Oprnds.size(); i != e; i += 2) |
| 1092 | I->addCase(cast<ConstantInt>(getValue(iType, Oprnds[i])), |
| 1093 | getBasicBlock(Oprnds[i+1])); |
| 1094 | Result = I; |
| 1095 | break; |
| 1096 | } |
| 1097 | case 58: // Call with extra operand for calling conv |
| 1098 | case 59: // tail call, Fast CC |
| 1099 | case 60: // normal call, Fast CC |
| 1100 | case 61: // tail call, C Calling Conv |
| 1101 | case Instruction::Call: { // Normal Call, C Calling Convention |
| 1102 | if (Oprnds.size() == 0) |
| 1103 | error("Invalid call instruction encountered!"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1104 | Value *F = getValue(iType, Oprnds[0]); |
| 1105 | |
| 1106 | unsigned CallingConv = CallingConv::C; |
| 1107 | bool isTailCall = false; |
| 1108 | |
| 1109 | if (Opcode == 61 || Opcode == 59) |
| 1110 | isTailCall = true; |
| 1111 | |
| 1112 | if (Opcode == 58) { |
| 1113 | isTailCall = Oprnds.back() & 1; |
| 1114 | CallingConv = Oprnds.back() >> 1; |
| 1115 | Oprnds.pop_back(); |
| 1116 | } else if (Opcode == 59 || Opcode == 60) { |
| 1117 | CallingConv = CallingConv::Fast; |
| 1118 | } |
| 1119 | |
| 1120 | // Check to make sure we have a pointer to function type |
| 1121 | const PointerType *PTy = dyn_cast<PointerType>(F->getType()); |
| 1122 | if (PTy == 0) error("Call to non function pointer value!"); |
| 1123 | const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType()); |
| 1124 | if (FTy == 0) error("Call to non function pointer value!"); |
| 1125 | |
| 1126 | std::vector<Value *> Params; |
| 1127 | if (!FTy->isVarArg()) { |
| 1128 | FunctionType::param_iterator It = FTy->param_begin(); |
| 1129 | |
| 1130 | for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) { |
| 1131 | if (It == FTy->param_end()) |
| 1132 | error("Invalid call instruction!"); |
| 1133 | Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i])); |
| 1134 | } |
| 1135 | if (It != FTy->param_end()) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1136 | error("Invalid call instruction!"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1137 | } else { |
| 1138 | Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1); |
| 1139 | |
| 1140 | unsigned FirstVariableOperand; |
| 1141 | if (Oprnds.size() < FTy->getNumParams()) |
| 1142 | error("Call instruction missing operands!"); |
| 1143 | |
| 1144 | // Read all of the fixed arguments |
| 1145 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
| 1146 | Params.push_back( |
| 1147 | getValue(getTypeSlot(FTy->getParamType(i)),Oprnds[i])); |
| 1148 | |
| 1149 | FirstVariableOperand = FTy->getNumParams(); |
| 1150 | |
| 1151 | if ((Oprnds.size()-FirstVariableOperand) & 1) |
| 1152 | error("Invalid call instruction!"); // Must be pairs of type/value |
| 1153 | |
| 1154 | for (unsigned i = FirstVariableOperand, e = Oprnds.size(); |
| 1155 | i != e; i += 2) |
| 1156 | Params.push_back(getValue(Oprnds[i], Oprnds[i+1])); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1157 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1158 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1159 | Result = new CallInst(F, Params); |
| 1160 | if (isTailCall) cast<CallInst>(Result)->setTailCall(); |
| 1161 | if (CallingConv) cast<CallInst>(Result)->setCallingConv(CallingConv); |
| 1162 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1163 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1164 | case Instruction::Invoke: { // Invoke C CC |
| 1165 | if (Oprnds.size() < 3) |
| 1166 | error("Invalid invoke instruction!"); |
| 1167 | Value *F = getValue(iType, Oprnds[0]); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1168 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1169 | // Check to make sure we have a pointer to function type |
| 1170 | const PointerType *PTy = dyn_cast<PointerType>(F->getType()); |
| 1171 | if (PTy == 0) |
| 1172 | error("Invoke to non function pointer value!"); |
| 1173 | const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType()); |
| 1174 | if (FTy == 0) |
| 1175 | error("Invoke to non function pointer value!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1176 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1177 | std::vector<Value *> Params; |
| 1178 | BasicBlock *Normal, *Except; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1179 | unsigned CallingConv = Oprnds.back(); |
| 1180 | Oprnds.pop_back(); |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 1181 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1182 | if (!FTy->isVarArg()) { |
| 1183 | Normal = getBasicBlock(Oprnds[1]); |
| 1184 | Except = getBasicBlock(Oprnds[2]); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1185 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1186 | FunctionType::param_iterator It = FTy->param_begin(); |
| 1187 | for (unsigned i = 3, e = Oprnds.size(); i != e; ++i) { |
| 1188 | if (It == FTy->param_end()) |
| 1189 | error("Invalid invoke instruction!"); |
| 1190 | Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i])); |
| 1191 | } |
| 1192 | if (It != FTy->param_end()) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1193 | error("Invalid invoke instruction!"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1194 | } else { |
| 1195 | Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1); |
| 1196 | |
| 1197 | Normal = getBasicBlock(Oprnds[0]); |
| 1198 | Except = getBasicBlock(Oprnds[1]); |
| 1199 | |
| 1200 | unsigned FirstVariableArgument = FTy->getNumParams()+2; |
| 1201 | for (unsigned i = 2; i != FirstVariableArgument; ++i) |
| 1202 | Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)), |
| 1203 | Oprnds[i])); |
| 1204 | |
| 1205 | // Must be type/value pairs. If not, error out. |
| 1206 | if (Oprnds.size()-FirstVariableArgument & 1) |
| 1207 | error("Invalid invoke instruction!"); |
| 1208 | |
| 1209 | for (unsigned i = FirstVariableArgument; i < Oprnds.size(); i += 2) |
| 1210 | Params.push_back(getValue(Oprnds[i], Oprnds[i+1])); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1211 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1212 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1213 | Result = new InvokeInst(F, Normal, Except, Params); |
| 1214 | if (CallingConv) cast<InvokeInst>(Result)->setCallingConv(CallingConv); |
| 1215 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1216 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1217 | case Instruction::Malloc: { |
| 1218 | unsigned Align = 0; |
| 1219 | if (Oprnds.size() == 2) |
| 1220 | Align = (1 << Oprnds[1]) >> 1; |
| 1221 | else if (Oprnds.size() > 2) |
| 1222 | error("Invalid malloc instruction!"); |
| 1223 | if (!isa<PointerType>(InstTy)) |
| 1224 | error("Invalid malloc instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1225 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1226 | Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(), |
| 1227 | getValue(Type::UIntTyID, Oprnds[0]), Align); |
| 1228 | break; |
| 1229 | } |
| 1230 | case Instruction::Alloca: { |
| 1231 | unsigned Align = 0; |
| 1232 | if (Oprnds.size() == 2) |
| 1233 | Align = (1 << Oprnds[1]) >> 1; |
| 1234 | else if (Oprnds.size() > 2) |
| 1235 | error("Invalid alloca instruction!"); |
| 1236 | if (!isa<PointerType>(InstTy)) |
| 1237 | error("Invalid alloca instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1238 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1239 | Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(), |
| 1240 | getValue(Type::UIntTyID, Oprnds[0]), Align); |
| 1241 | break; |
| 1242 | } |
| 1243 | case Instruction::Free: |
| 1244 | if (!isa<PointerType>(InstTy)) |
| 1245 | error("Invalid free instruction!"); |
| 1246 | Result = new FreeInst(getValue(iType, Oprnds[0])); |
| 1247 | break; |
| 1248 | case Instruction::GetElementPtr: { |
| 1249 | if (Oprnds.size() == 0 || !isa<PointerType>(InstTy)) |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1250 | error("Invalid getelementptr instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1251 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1252 | std::vector<Value*> Idx; |
| 1253 | |
| 1254 | const Type *NextTy = InstTy; |
| 1255 | for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) { |
| 1256 | const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy); |
| 1257 | if (!TopTy) |
| 1258 | error("Invalid getelementptr instruction!"); |
| 1259 | |
| 1260 | unsigned ValIdx = Oprnds[i]; |
| 1261 | unsigned IdxTy = 0; |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1262 | // Struct indices are always uints, sequential type indices can be |
| 1263 | // any of the 32 or 64-bit integer types. The actual choice of |
| 1264 | // type is encoded in the low two bits of the slot number. |
| 1265 | if (isa<StructType>(TopTy)) |
| 1266 | IdxTy = Type::UIntTyID; |
| 1267 | else { |
| 1268 | switch (ValIdx & 3) { |
| 1269 | default: |
| 1270 | case 0: IdxTy = Type::UIntTyID; break; |
| 1271 | case 1: IdxTy = Type::IntTyID; break; |
| 1272 | case 2: IdxTy = Type::ULongTyID; break; |
| 1273 | case 3: IdxTy = Type::LongTyID; break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1274 | } |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1275 | ValIdx >>= 2; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1276 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1277 | Idx.push_back(getValue(IdxTy, ValIdx)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1278 | NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1281 | Result = new GetElementPtrInst(getValue(iType, Oprnds[0]), Idx); |
| 1282 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1283 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1284 | case 62: // volatile load |
| 1285 | case Instruction::Load: |
| 1286 | if (Oprnds.size() != 1 || !isa<PointerType>(InstTy)) |
| 1287 | error("Invalid load instruction!"); |
| 1288 | Result = new LoadInst(getValue(iType, Oprnds[0]), "", Opcode == 62); |
| 1289 | break; |
| 1290 | case 63: // volatile store |
| 1291 | case Instruction::Store: { |
| 1292 | if (!isa<PointerType>(InstTy) || Oprnds.size() != 2) |
| 1293 | error("Invalid store instruction!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1294 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1295 | Value *Ptr = getValue(iType, Oprnds[1]); |
| 1296 | const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType(); |
| 1297 | Result = new StoreInst(getValue(getTypeSlot(ValTy), Oprnds[0]), Ptr, |
| 1298 | Opcode == 63); |
| 1299 | break; |
| 1300 | } |
| 1301 | case Instruction::Unwind: |
| 1302 | if (Oprnds.size() != 0) error("Invalid unwind instruction!"); |
| 1303 | Result = new UnwindInst(); |
| 1304 | break; |
| 1305 | case Instruction::Unreachable: |
| 1306 | if (Oprnds.size() != 0) error("Invalid unreachable instruction!"); |
| 1307 | Result = new UnreachableInst(); |
| 1308 | break; |
| 1309 | } // end switch(Opcode) |
| 1310 | } // end if *normal* |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1311 | |
Reid Spencer | e1e96c0 | 2006-01-19 07:02:16 +0000 | [diff] [blame] | 1312 | BB->getInstList().push_back(Result); |
| 1313 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1314 | unsigned TypeSlot; |
| 1315 | if (Result->getType() == InstTy) |
| 1316 | TypeSlot = iType; |
| 1317 | else |
| 1318 | TypeSlot = getTypeSlot(Result->getType()); |
| 1319 | |
| 1320 | insertValue(Result, TypeSlot, FunctionValues); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1323 | /// Get a particular numbered basic block, which might be a forward reference. |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1324 | /// This works together with ParseInstructionList to handle these forward |
| 1325 | /// references in a clean manner. This function is used when constructing |
| 1326 | /// phi, br, switch, and other instructions that reference basic blocks. |
| 1327 | /// Blocks are numbered sequentially as they appear in the function. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1328 | BasicBlock *BytecodeReader::getBasicBlock(unsigned ID) { |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 1329 | // Make sure there is room in the table... |
| 1330 | if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1); |
| 1331 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1332 | // First check to see if this is a backwards reference, i.e. this block |
| 1333 | // has already been created, or if the forward reference has already |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 1334 | // been created. |
| 1335 | if (ParsedBasicBlocks[ID]) |
| 1336 | return ParsedBasicBlocks[ID]; |
| 1337 | |
| 1338 | // Otherwise, the basic block has not yet been created. Do so and add it to |
| 1339 | // the ParsedBasicBlocks list. |
| 1340 | return ParsedBasicBlocks[ID] = new BasicBlock(); |
| 1341 | } |
| 1342 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1343 | /// Parse all of the BasicBlock's & Instruction's in the body of a function. |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1344 | /// In post 1.0 bytecode files, we no longer emit basic block individually, |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1345 | /// in order to avoid per-basic-block overhead. |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1346 | /// @returns the number of basic blocks encountered. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1347 | unsigned BytecodeReader::ParseInstructionList(Function* F) { |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 1348 | unsigned BlockNo = 0; |
| 1349 | std::vector<unsigned> Args; |
| 1350 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1351 | while (moreInBlock()) { |
| 1352 | if (Handler) Handler->handleBasicBlockBegin(BlockNo); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 1353 | BasicBlock *BB; |
| 1354 | if (ParsedBasicBlocks.size() == BlockNo) |
| 1355 | ParsedBasicBlocks.push_back(BB = new BasicBlock()); |
| 1356 | else if (ParsedBasicBlocks[BlockNo] == 0) |
| 1357 | BB = ParsedBasicBlocks[BlockNo] = new BasicBlock(); |
| 1358 | else |
| 1359 | BB = ParsedBasicBlocks[BlockNo]; |
| 1360 | ++BlockNo; |
| 1361 | F->getBasicBlockList().push_back(BB); |
| 1362 | |
| 1363 | // Read instructions into this basic block until we get to a terminator |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1364 | while (moreInBlock() && !BB->getTerminator()) |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1365 | ParseInstruction(Args, BB); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 1366 | |
| 1367 | if (!BB->getTerminator()) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1368 | error("Non-terminated basic block found!"); |
Reid Spencer | 5c15fe5 | 2004-07-05 00:57:50 +0000 | [diff] [blame] | 1369 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1370 | if (Handler) Handler->handleBasicBlockEnd(BlockNo-1); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | return BlockNo; |
| 1374 | } |
| 1375 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1376 | /// Parse a symbol table. This works for both module level and function |
| 1377 | /// level symbol tables. For function level symbol tables, the CurrentFunction |
| 1378 | /// parameter must be non-zero and the ST parameter must correspond to |
| 1379 | /// CurrentFunction's symbol table. For Module level symbol tables, the |
| 1380 | /// CurrentFunction argument must be zero. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1381 | void BytecodeReader::ParseSymbolTable(Function *CurrentFunction, |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1382 | SymbolTable *ST) { |
| 1383 | if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | 39cacce | 2003-10-10 05:43:47 +0000 | [diff] [blame] | 1385 | // Allow efficient basic block lookup by number. |
| 1386 | std::vector<BasicBlock*> BBMap; |
| 1387 | if (CurrentFunction) |
| 1388 | for (Function::iterator I = CurrentFunction->begin(), |
| 1389 | E = CurrentFunction->end(); I != E; ++I) |
| 1390 | BBMap.push_back(I); |
| 1391 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1392 | // Symtab block header: [num entries] |
| 1393 | unsigned NumEntries = read_vbr_uint(); |
| 1394 | for (unsigned i = 0; i < NumEntries; ++i) { |
| 1395 | // Symtab entry: [def slot #][name] |
| 1396 | unsigned slot = read_vbr_uint(); |
| 1397 | std::string Name = read_str(); |
| 1398 | const Type* T = getType(slot); |
| 1399 | ST->insert(Name, T); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1402 | while (moreInBlock()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1403 | // Symtab block header: [num entries][type id number] |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1404 | unsigned NumEntries = read_vbr_uint(); |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1405 | unsigned Typ = read_vbr_uint(); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 1406 | |
Chris Lattner | 7dc3a2e | 2003-10-13 14:57:53 +0000 | [diff] [blame] | 1407 | for (unsigned i = 0; i != NumEntries; ++i) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1408 | // Symtab entry: [def slot #][name] |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1409 | unsigned slot = read_vbr_uint(); |
| 1410 | std::string Name = read_str(); |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1411 | Value *V = 0; |
| 1412 | if (Typ == Type::LabelTyID) { |
| 1413 | if (slot < BBMap.size()) |
| 1414 | V = BBMap[slot]; |
Chris Lattner | 39cacce | 2003-10-10 05:43:47 +0000 | [diff] [blame] | 1415 | } else { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1416 | V = getValue(Typ, slot, false); // Find mapping... |
Chris Lattner | 39cacce | 2003-10-10 05:43:47 +0000 | [diff] [blame] | 1417 | } |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1418 | if (V == 0) |
| 1419 | error("Failed value look-up for name '" + Name + "'"); |
| 1420 | V->setName(Name); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1423 | checkPastBlockEnd("Symbol Table"); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1424 | if (Handler) Handler->handleSymbolTableEnd(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1427 | /// Read in the types portion of a compaction table. |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1428 | void BytecodeReader::ParseCompactionTypes(unsigned NumEntries) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1429 | for (unsigned i = 0; i != NumEntries; ++i) { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1430 | unsigned TypeSlot = read_vbr_uint(); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1431 | const Type *Typ = getGlobalTableType(TypeSlot); |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 1432 | CompactionTypes.push_back(std::make_pair(Typ, TypeSlot)); |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1433 | if (Handler) Handler->handleCompactionTableType(i, TypeSlot, Typ); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | /// Parse a compaction table. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1438 | void BytecodeReader::ParseCompactionTable() { |
| 1439 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1440 | // Notify handler that we're beginning a compaction table. |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1441 | if (Handler) Handler->handleCompactionTableBegin(); |
| 1442 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1443 | // Get the types for the compaction table. |
| 1444 | unsigned NumEntries = read_vbr_uint(); |
| 1445 | ParseCompactionTypes(NumEntries); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1446 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1447 | // Compaction tables live in separate blocks so we have to loop |
| 1448 | // until we've read the whole thing. |
| 1449 | while (moreInBlock()) { |
| 1450 | // Read the number of Value* entries in the compaction table |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1451 | unsigned NumEntries = read_vbr_uint(); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1452 | unsigned Ty = 0; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1453 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1454 | // Decode the type from value read in. Most compaction table |
| 1455 | // planes will have one or two entries in them. If that's the |
| 1456 | // case then the length is encoded in the bottom two bits and |
| 1457 | // the higher bits encode the type. This saves another VBR value. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1458 | if ((NumEntries & 3) == 3) { |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1459 | // In this case, both low-order bits are set (value 3). This |
| 1460 | // is a signal that the typeid follows. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1461 | NumEntries >>= 2; |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1462 | Ty = read_vbr_uint(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1463 | } else { |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1464 | // In this case, the low-order bits specify the number of entries |
| 1465 | // and the high order bits specify the type. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1466 | Ty = NumEntries >> 2; |
| 1467 | NumEntries &= 3; |
| 1468 | } |
| 1469 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1470 | // Make sure we have enough room for the plane. |
| 1471 | if (Ty >= CompactionValues.size()) |
| 1472 | CompactionValues.resize(Ty+1); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1473 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1474 | // Make sure the plane is empty or we have some kind of error. |
| 1475 | if (!CompactionValues[Ty].empty()) |
| 1476 | error("Compaction table plane contains multiple entries!"); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1477 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1478 | // Notify handler about the plane. |
| 1479 | if (Handler) Handler->handleCompactionTablePlane(Ty, NumEntries); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1480 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1481 | // Push the implicit zero. |
| 1482 | CompactionValues[Ty].push_back(Constant::getNullValue(getType(Ty))); |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1483 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1484 | // Read in each of the entries, put them in the compaction table |
| 1485 | // and notify the handler that we have a new compaction table value. |
| 1486 | for (unsigned i = 0; i != NumEntries; ++i) { |
| 1487 | unsigned ValSlot = read_vbr_uint(); |
| 1488 | Value *V = getGlobalTableValue(Ty, ValSlot); |
| 1489 | CompactionValues[Ty].push_back(V); |
| 1490 | if (Handler) Handler->handleCompactionTableValue(i, Ty, ValSlot); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1493 | // Notify handler that the compaction table is done. |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1494 | if (Handler) Handler->handleCompactionTableEnd(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1495 | } |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1496 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1497 | // Parse a single type. The typeid is read in first. If its a primitive type |
| 1498 | // then nothing else needs to be read, we know how to instantiate it. If its |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1499 | // a derived type, then additional data is read to fill out the type |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1500 | // definition. |
| 1501 | const Type *BytecodeReader::ParseType() { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1502 | unsigned PrimType = read_vbr_uint(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1503 | const Type *Result = 0; |
| 1504 | if ((Result = Type::getPrimitiveType((Type::TypeID)PrimType))) |
| 1505 | return Result; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1506 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1507 | switch (PrimType) { |
| 1508 | case Type::FunctionTyID: { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1509 | const Type *RetType = readType(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1510 | |
| 1511 | unsigned NumParams = read_vbr_uint(); |
| 1512 | |
| 1513 | std::vector<const Type*> Params; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1514 | while (NumParams--) |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1515 | Params.push_back(readType()); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1516 | |
| 1517 | bool isVarArg = Params.size() && Params.back() == Type::VoidTy; |
| 1518 | if (isVarArg) Params.pop_back(); |
| 1519 | |
| 1520 | Result = FunctionType::get(RetType, Params, isVarArg); |
| 1521 | break; |
| 1522 | } |
| 1523 | case Type::ArrayTyID: { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1524 | const Type *ElementType = readType(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1525 | unsigned NumElements = read_vbr_uint(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1526 | Result = ArrayType::get(ElementType, NumElements); |
| 1527 | break; |
| 1528 | } |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1529 | case Type::PackedTyID: { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1530 | const Type *ElementType = readType(); |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1531 | unsigned NumElements = read_vbr_uint(); |
| 1532 | Result = PackedType::get(ElementType, NumElements); |
| 1533 | break; |
| 1534 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1535 | case Type::StructTyID: { |
| 1536 | std::vector<const Type*> Elements; |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1537 | unsigned Typ = read_vbr_uint(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1538 | while (Typ) { // List is terminated by void/0 typeid |
| 1539 | Elements.push_back(getType(Typ)); |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1540 | Typ = read_vbr_uint(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | Result = StructType::get(Elements); |
| 1544 | break; |
| 1545 | } |
| 1546 | case Type::PointerTyID: { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1547 | Result = PointerType::get(readType()); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1548 | break; |
| 1549 | } |
| 1550 | |
| 1551 | case Type::OpaqueTyID: { |
| 1552 | Result = OpaqueType::get(); |
| 1553 | break; |
| 1554 | } |
| 1555 | |
| 1556 | default: |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1557 | error("Don't know how to deserialize primitive type " + utostr(PrimType)); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1558 | break; |
| 1559 | } |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1560 | if (Handler) Handler->handleType(Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1561 | return Result; |
| 1562 | } |
| 1563 | |
Reid Spencer | 5b472d9 | 2004-08-21 20:49:23 +0000 | [diff] [blame] | 1564 | // ParseTypes - We have to use this weird code to handle recursive |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1565 | // types. We know that recursive types will only reference the current slab of |
| 1566 | // values in the type plane, but they can forward reference types before they |
| 1567 | // have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might |
| 1568 | // be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix |
| 1569 | // this ugly problem, we pessimistically insert an opaque type for each type we |
| 1570 | // are about to read. This means that forward references will resolve to |
| 1571 | // something and when we reread the type later, we can replace the opaque type |
| 1572 | // with a new resolved concrete type. |
| 1573 | // |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1574 | void BytecodeReader::ParseTypes(TypeListTy &Tab, unsigned NumEntries){ |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1575 | assert(Tab.size() == 0 && "should not have read type constants in before!"); |
| 1576 | |
| 1577 | // Insert a bunch of opaque types to be resolved later... |
| 1578 | Tab.reserve(NumEntries); |
| 1579 | for (unsigned i = 0; i != NumEntries; ++i) |
| 1580 | Tab.push_back(OpaqueType::get()); |
| 1581 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1582 | if (Handler) |
Reid Spencer | 5b472d9 | 2004-08-21 20:49:23 +0000 | [diff] [blame] | 1583 | Handler->handleTypeList(NumEntries); |
| 1584 | |
Chris Lattner | eebac5f | 2005-10-03 21:26:53 +0000 | [diff] [blame] | 1585 | // If we are about to resolve types, make sure the type cache is clear. |
| 1586 | if (NumEntries) |
| 1587 | ModuleTypeIDCache.clear(); |
| 1588 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1589 | // Loop through reading all of the types. Forward types will make use of the |
| 1590 | // opaque types just inserted. |
| 1591 | // |
| 1592 | for (unsigned i = 0; i != NumEntries; ++i) { |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1593 | const Type* NewTy = ParseType(); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1594 | const Type* OldTy = Tab[i].get(); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1595 | if (NewTy == 0) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1596 | error("Couldn't parse type!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1597 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1598 | // Don't directly push the new type on the Tab. Instead we want to replace |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1599 | // the opaque type we previously inserted with the new concrete value. This |
| 1600 | // approach helps with forward references to types. The refinement from the |
| 1601 | // abstract (opaque) type to the new type causes all uses of the abstract |
| 1602 | // type to use the concrete type (NewTy). This will also cause the opaque |
| 1603 | // type to be deleted. |
| 1604 | cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy); |
| 1605 | |
| 1606 | // This should have replaced the old opaque type with the new type in the |
| 1607 | // value table... or with a preexisting type that was already in the system. |
| 1608 | // Let's just make sure it did. |
| 1609 | assert(Tab[i] != OldTy && "refineAbstractType didn't work!"); |
| 1610 | } |
| 1611 | } |
| 1612 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1613 | // Upgrade obsolete constant expression opcodes (ver. 5 and prior) to the new |
| 1614 | // values used after ver 6. bytecode format. The operands are provided to the |
| 1615 | // function so that decisions based on the operand type can be made when |
| 1616 | // auto-upgrading obsolete opcodes to the new ones. |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 1617 | // NOTE: This code needs to be kept synchronized with upgradeInstrOpcodes. |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1618 | // We can't use that function because of that functions argument requirements. |
| 1619 | // This function only deals with the subset of opcodes that are applicable to |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 1620 | // constant expressions and is therefore simpler than upgradeInstrOpcodes. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1621 | inline Constant *BytecodeReader::upgradeCEOpcodes( |
| 1622 | unsigned &Opcode, const std::vector<Constant*> &ArgVec, unsigned TypeID |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1623 | ) { |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 1624 | // Determine if no upgrade necessary |
| 1625 | if (!hasSignlessDivRem && !hasSignlessShrCastSetcc) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1626 | return 0; |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 1627 | |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 1628 | // If this is bytecode version 6, that only had signed Rem and Div |
| 1629 | // instructions, then we must compensate for those two instructions only. |
| 1630 | // So that the switch statement below works, we're trying to turn this into |
| 1631 | // a version 5 opcode. To do that we must adjust the opcode to 10 (Div) if its |
| 1632 | // any of the UDiv, SDiv or FDiv instructions; or, adjust the opcode to |
| 1633 | // 11 (Rem) if its any of the URem, SRem, or FRem instructions; or, simply |
| 1634 | // decrement the instruction code if its beyond FRem. |
| 1635 | if (!hasSignlessDivRem) { |
| 1636 | // If its one of the signed Div/Rem opcodes, its fine the way it is |
| 1637 | if (Opcode >= 10 && Opcode <= 12) // UDiv through FDiv |
| 1638 | Opcode = 10; // Div |
| 1639 | else if (Opcode >=13 && Opcode <= 15) // URem through FRem |
| 1640 | Opcode = 11; // Rem |
Reid Spencer | 967413f | 2006-11-18 04:37:19 +0000 | [diff] [blame] | 1641 | else if (Opcode >= 16 && Opcode <= 35) // And through Shr |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 1642 | // Adjust for new instruction codes |
| 1643 | Opcode -= 4; |
Reid Spencer | 967413f | 2006-11-18 04:37:19 +0000 | [diff] [blame] | 1644 | else if (Opcode >= 36 && Opcode <= 42) // Everything after Select |
| 1645 | // In vers 6 bytecode we eliminated the placeholders for the obsolete |
| 1646 | // VAARG and VANEXT instructions. Consequently those two slots were |
| 1647 | // filled starting with Select (36) which was 34. So now we only need |
| 1648 | // to subtract two. This circumvents hitting opcodes 32 and 33 |
| 1649 | Opcode -= 2; |
| 1650 | else { // Opcode < 10 or > 42 |
| 1651 | // No upgrade necessary. |
| 1652 | return 0; |
| 1653 | } |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1656 | switch (Opcode) { |
| 1657 | default: // Pass Through |
| 1658 | // If we don't match any of the cases here then the opcode is fine the |
| 1659 | // way it is. |
| 1660 | break; |
| 1661 | case 7: // Add |
| 1662 | Opcode = Instruction::Add; |
| 1663 | break; |
| 1664 | case 8: // Sub |
| 1665 | Opcode = Instruction::Sub; |
| 1666 | break; |
| 1667 | case 9: // Mul |
| 1668 | Opcode = Instruction::Mul; |
| 1669 | break; |
| 1670 | case 10: // Div |
| 1671 | // The type of the instruction is based on the operands. We need to select |
| 1672 | // either udiv or sdiv based on that type. This expression selects the |
| 1673 | // cases where the type is floating point or signed in which case we |
| 1674 | // generated an sdiv instruction. |
| 1675 | if (ArgVec[0]->getType()->isFloatingPoint()) |
| 1676 | Opcode = Instruction::FDiv; |
| 1677 | else if (ArgVec[0]->getType()->isSigned()) |
| 1678 | Opcode = Instruction::SDiv; |
| 1679 | else |
| 1680 | Opcode = Instruction::UDiv; |
| 1681 | break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1682 | case 11: // Rem |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1683 | // As with "Div", make the signed/unsigned or floating point Rem |
| 1684 | // instruction choice based on the type of the operands. |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1685 | if (ArgVec[0]->getType()->isFloatingPoint()) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1686 | Opcode = Instruction::FRem; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1687 | else if (ArgVec[0]->getType()->isSigned()) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1688 | Opcode = Instruction::SRem; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1689 | else |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1690 | Opcode = Instruction::URem; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1691 | break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1692 | case 12: // And |
| 1693 | Opcode = Instruction::And; |
| 1694 | break; |
| 1695 | case 13: // Or |
| 1696 | Opcode = Instruction::Or; |
| 1697 | break; |
| 1698 | case 14: // Xor |
| 1699 | Opcode = Instruction::Xor; |
| 1700 | break; |
| 1701 | case 15: // SetEQ |
| 1702 | Opcode = Instruction::SetEQ; |
| 1703 | break; |
| 1704 | case 16: // SetNE |
| 1705 | Opcode = Instruction::SetNE; |
| 1706 | break; |
| 1707 | case 17: // SetLE |
| 1708 | Opcode = Instruction::SetLE; |
| 1709 | break; |
| 1710 | case 18: // SetGE |
| 1711 | Opcode = Instruction::SetGE; |
| 1712 | break; |
| 1713 | case 19: // SetLT |
| 1714 | Opcode = Instruction::SetLT; |
| 1715 | break; |
| 1716 | case 20: // SetGT |
| 1717 | Opcode = Instruction::SetGT; |
| 1718 | break; |
| 1719 | case 26: // GetElementPtr |
| 1720 | Opcode = Instruction::GetElementPtr; |
| 1721 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1722 | case 28: { // Cast |
| 1723 | const Type *Ty = getType(TypeID); |
| 1724 | if (Ty == Type::BoolTy) { |
| 1725 | // The previous definition of cast to bool was a compare against zero. |
| 1726 | // We have to retain that semantic so we do it here. |
| 1727 | Opcode = Instruction::SetEQ; |
| 1728 | return ConstantExpr::get(Instruction::SetEQ, ArgVec[0], |
| 1729 | Constant::getNullValue(ArgVec[0]->getType())); |
| 1730 | } else if (ArgVec[0]->getType()->isFloatingPoint() && |
| 1731 | isa<PointerType>(Ty)) { |
| 1732 | // Upgrade what is now an illegal cast (fp -> ptr) into two casts, |
| 1733 | // fp -> ui, and ui -> ptr |
| 1734 | Constant *CE = ConstantExpr::getFPToUI(ArgVec[0], Type::ULongTy); |
| 1735 | return ConstantExpr::getIntToPtr(CE, Ty); |
| 1736 | } else { |
| 1737 | Opcode = CastInst::getCastOpcode(ArgVec[0], Ty); |
| 1738 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1739 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1740 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1741 | case 30: // Shl |
| 1742 | Opcode = Instruction::Shl; |
| 1743 | break; |
| 1744 | case 31: // Shr |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1745 | if (ArgVec[0]->getType()->isSigned()) |
| 1746 | Opcode = Instruction::AShr; |
| 1747 | else |
| 1748 | Opcode = Instruction::LShr; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1749 | break; |
| 1750 | case 34: // Select |
| 1751 | Opcode = Instruction::Select; |
| 1752 | break; |
| 1753 | case 38: // ExtractElement |
| 1754 | Opcode = Instruction::ExtractElement; |
| 1755 | break; |
| 1756 | case 39: // InsertElement |
| 1757 | Opcode = Instruction::InsertElement; |
| 1758 | break; |
| 1759 | case 40: // ShuffleVector |
| 1760 | Opcode = Instruction::ShuffleVector; |
| 1761 | break; |
| 1762 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1763 | return 0; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1766 | /// Parse a single constant value |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 1767 | Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1768 | // We must check for a ConstantExpr before switching by type because |
| 1769 | // a ConstantExpr can be of any type, and has no explicit value. |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1770 | // |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1771 | // 0 if not expr; numArgs if is expr |
| 1772 | unsigned isExprNumArgs = read_vbr_uint(); |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 1773 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1774 | if (isExprNumArgs) { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1775 | // 'undef' is encoded with 'exprnumargs' == 1. |
| 1776 | if (isExprNumArgs == 1) |
| 1777 | return UndefValue::get(getType(TypeID)); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1778 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1779 | // Inline asm is encoded with exprnumargs == ~0U. |
| 1780 | if (isExprNumArgs == ~0U) { |
| 1781 | std::string AsmStr = read_str(); |
| 1782 | std::string ConstraintStr = read_str(); |
| 1783 | unsigned Flags = read_vbr_uint(); |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 1784 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1785 | const PointerType *PTy = dyn_cast<PointerType>(getType(TypeID)); |
| 1786 | const FunctionType *FTy = |
| 1787 | PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0; |
| 1788 | |
| 1789 | if (!FTy || !InlineAsm::Verify(FTy, ConstraintStr)) |
| 1790 | error("Invalid constraints for inline asm"); |
| 1791 | if (Flags & ~1U) |
| 1792 | error("Invalid flags for inline asm"); |
| 1793 | bool HasSideEffects = Flags & 1; |
| 1794 | return InlineAsm::get(FTy, AsmStr, ConstraintStr, HasSideEffects); |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 1795 | } |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1796 | |
| 1797 | --isExprNumArgs; |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 1798 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1799 | // FIXME: Encoding of constant exprs could be much more compact! |
| 1800 | std::vector<Constant*> ArgVec; |
| 1801 | ArgVec.reserve(isExprNumArgs); |
| 1802 | unsigned Opcode = read_vbr_uint(); |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 1803 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1804 | // Read the slot number and types of each of the arguments |
| 1805 | for (unsigned i = 0; i != isExprNumArgs; ++i) { |
| 1806 | unsigned ArgValSlot = read_vbr_uint(); |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 1807 | unsigned ArgTypeSlot = read_vbr_uint(); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1808 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1809 | // Get the arg value from its slot if it exists, otherwise a placeholder |
| 1810 | ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot)); |
| 1811 | } |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1812 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1813 | // Handle backwards compatibility for the opcode numbers |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1814 | if (Constant *C = upgradeCEOpcodes(Opcode, ArgVec, TypeID)) { |
| 1815 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, C); |
| 1816 | return C; |
| 1817 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1818 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1819 | // Construct a ConstantExpr of the appropriate kind |
| 1820 | if (isExprNumArgs == 1) { // All one-operand expressions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1821 | if (!Instruction::isCast(Opcode)) |
Chris Lattner | 02dce16 | 2004-12-04 05:28:27 +0000 | [diff] [blame] | 1822 | error("Only cast instruction has one argument for ConstantExpr"); |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1823 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1824 | Constant *Result = ConstantExpr::getCast(ArgVec[0], getType(TypeID)); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1825 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1826 | return Result; |
| 1827 | } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr |
| 1828 | std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1829 | Constant *Result = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1830 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1831 | return Result; |
| 1832 | } else if (Opcode == Instruction::Select) { |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1833 | if (ArgVec.size() != 3) |
| 1834 | error("Select instruction must have three arguments."); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1835 | Constant* Result = ConstantExpr::getSelect(ArgVec[0], ArgVec[1], |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1836 | ArgVec[2]); |
| 1837 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1838 | return Result; |
Robert Bocchino | fee31b3 | 2006-01-10 19:04:39 +0000 | [diff] [blame] | 1839 | } else if (Opcode == Instruction::ExtractElement) { |
Chris Lattner | 59fecec | 2006-04-08 04:09:19 +0000 | [diff] [blame] | 1840 | if (ArgVec.size() != 2 || |
| 1841 | !ExtractElementInst::isValidOperands(ArgVec[0], ArgVec[1])) |
| 1842 | error("Invalid extractelement constand expr arguments"); |
Robert Bocchino | fee31b3 | 2006-01-10 19:04:39 +0000 | [diff] [blame] | 1843 | Constant* Result = ConstantExpr::getExtractElement(ArgVec[0], ArgVec[1]); |
| 1844 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
| 1845 | return Result; |
Robert Bocchino | b1f240b | 2006-01-17 20:06:35 +0000 | [diff] [blame] | 1846 | } else if (Opcode == Instruction::InsertElement) { |
Chris Lattner | 59fecec | 2006-04-08 04:09:19 +0000 | [diff] [blame] | 1847 | if (ArgVec.size() != 3 || |
| 1848 | !InsertElementInst::isValidOperands(ArgVec[0], ArgVec[1], ArgVec[2])) |
| 1849 | error("Invalid insertelement constand expr arguments"); |
| 1850 | |
| 1851 | Constant *Result = |
Robert Bocchino | b1f240b | 2006-01-17 20:06:35 +0000 | [diff] [blame] | 1852 | ConstantExpr::getInsertElement(ArgVec[0], ArgVec[1], ArgVec[2]); |
| 1853 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
| 1854 | return Result; |
Chris Lattner | 30b44b6 | 2006-04-08 01:17:59 +0000 | [diff] [blame] | 1855 | } else if (Opcode == Instruction::ShuffleVector) { |
| 1856 | if (ArgVec.size() != 3 || |
| 1857 | !ShuffleVectorInst::isValidOperands(ArgVec[0], ArgVec[1], ArgVec[2])) |
Chris Lattner | 59fecec | 2006-04-08 04:09:19 +0000 | [diff] [blame] | 1858 | error("Invalid shufflevector constant expr arguments."); |
Chris Lattner | 30b44b6 | 2006-04-08 01:17:59 +0000 | [diff] [blame] | 1859 | Constant *Result = |
| 1860 | ConstantExpr::getShuffleVector(ArgVec[0], ArgVec[1], ArgVec[2]); |
| 1861 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
| 1862 | return Result; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1863 | } else { // All other 2-operand expressions |
| 1864 | Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1865 | if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1866 | return Result; |
| 1867 | } |
| 1868 | } |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1869 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1870 | // Ok, not an ConstantExpr. We now know how to read the given type... |
| 1871 | const Type *Ty = getType(TypeID); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1872 | Constant *Result = 0; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1873 | switch (Ty->getTypeID()) { |
| 1874 | case Type::BoolTyID: { |
| 1875 | unsigned Val = read_vbr_uint(); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1876 | if (Val != 0 && Val != 1) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1877 | error("Invalid boolean value read."); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1878 | Result = ConstantBool::get(Val == 1); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1879 | if (Handler) Handler->handleConstantValue(Result); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1880 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | case Type::UByteTyID: // Unsigned integer types... |
| 1884 | case Type::UShortTyID: |
| 1885 | case Type::UIntTyID: { |
| 1886 | unsigned Val = read_vbr_uint(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1887 | if (!ConstantInt::isValueValidForType(Ty, uint64_t(Val))) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1888 | error("Invalid unsigned byte/short/int read."); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1889 | Result = ConstantInt::get(Ty, Val); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1890 | if (Handler) Handler->handleConstantValue(Result); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1891 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1894 | case Type::ULongTyID: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1895 | Result = ConstantInt::get(Ty, read_vbr_uint64()); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1896 | if (Handler) Handler->handleConstantValue(Result); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1897 | break; |
| 1898 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1899 | case Type::SByteTyID: // Signed integer types... |
| 1900 | case Type::ShortTyID: |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1901 | case Type::IntTyID: |
| 1902 | case Type::LongTyID: { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1903 | int64_t Val = read_vbr_int64(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1904 | if (!ConstantInt::isValueValidForType(Ty, Val)) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1905 | error("Invalid signed byte/short/int/long read."); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1906 | Result = ConstantInt::get(Ty, Val); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1907 | if (Handler) Handler->handleConstantValue(Result); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1908 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | case Type::FloatTyID: { |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1912 | float Val; |
| 1913 | read_float(Val); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1914 | Result = ConstantFP::get(Ty, Val); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1915 | if (Handler) Handler->handleConstantValue(Result); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1916 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | case Type::DoubleTyID: { |
| 1920 | double Val; |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 1921 | read_double(Val); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1922 | Result = ConstantFP::get(Ty, Val); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1923 | if (Handler) Handler->handleConstantValue(Result); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1924 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1927 | case Type::ArrayTyID: { |
| 1928 | const ArrayType *AT = cast<ArrayType>(Ty); |
| 1929 | unsigned NumElements = AT->getNumElements(); |
| 1930 | unsigned TypeSlot = getTypeSlot(AT->getElementType()); |
| 1931 | std::vector<Constant*> Elements; |
| 1932 | Elements.reserve(NumElements); |
| 1933 | while (NumElements--) // Read all of the elements of the constant. |
| 1934 | Elements.push_back(getConstantValue(TypeSlot, |
| 1935 | read_vbr_uint())); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1936 | Result = ConstantArray::get(AT, Elements); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1937 | if (Handler) Handler->handleConstantArray(AT, Elements, TypeSlot, Result); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1938 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | case Type::StructTyID: { |
| 1942 | const StructType *ST = cast<StructType>(Ty); |
| 1943 | |
| 1944 | std::vector<Constant *> Elements; |
| 1945 | Elements.reserve(ST->getNumElements()); |
| 1946 | for (unsigned i = 0; i != ST->getNumElements(); ++i) |
| 1947 | Elements.push_back(getConstantValue(ST->getElementType(i), |
| 1948 | read_vbr_uint())); |
| 1949 | |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1950 | Result = ConstantStruct::get(ST, Elements); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 1951 | if (Handler) Handler->handleConstantStruct(ST, Elements, Result); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1952 | break; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1953 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1954 | |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1955 | case Type::PackedTyID: { |
| 1956 | const PackedType *PT = cast<PackedType>(Ty); |
| 1957 | unsigned NumElements = PT->getNumElements(); |
| 1958 | unsigned TypeSlot = getTypeSlot(PT->getElementType()); |
| 1959 | std::vector<Constant*> Elements; |
| 1960 | Elements.reserve(NumElements); |
| 1961 | while (NumElements--) // Read all of the elements of the constant. |
| 1962 | Elements.push_back(getConstantValue(TypeSlot, |
| 1963 | read_vbr_uint())); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1964 | Result = ConstantPacked::get(PT, Elements); |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1965 | if (Handler) Handler->handleConstantPacked(PT, Elements, TypeSlot, Result); |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1966 | break; |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Chris Lattner | 638c381 | 2004-11-19 16:24:05 +0000 | [diff] [blame] | 1969 | case Type::PointerTyID: { // ConstantPointerRef value (backwards compat). |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1970 | const PointerType *PT = cast<PointerType>(Ty); |
| 1971 | unsigned Slot = read_vbr_uint(); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 1972 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1973 | // Check to see if we have already read this global variable... |
| 1974 | Value *Val = getValue(TypeID, Slot, false); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1975 | if (Val) { |
Chris Lattner | bcb11cf | 2004-07-27 02:34:49 +0000 | [diff] [blame] | 1976 | GlobalValue *GV = dyn_cast<GlobalValue>(Val); |
| 1977 | if (!GV) error("GlobalValue not in ValueTable!"); |
| 1978 | if (Handler) Handler->handleConstantPointer(PT, Slot, GV); |
| 1979 | return GV; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1980 | } else { |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1981 | error("Forward references are not allowed here."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1982 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | default: |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 1986 | error("Don't know how to deserialize constant value of type '" + |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1987 | Ty->getDescription()); |
| 1988 | break; |
| 1989 | } |
Chris Lattner | d2cfb7a | 2006-04-07 05:00:02 +0000 | [diff] [blame] | 1990 | |
| 1991 | // Check that we didn't read a null constant if they are implicit for this |
| 1992 | // type plane. Do not do this check for constantexprs, as they may be folded |
| 1993 | // to a null value in a way that isn't predicted when a .bc file is initially |
| 1994 | // produced. |
| 1995 | assert((!isa<Constant>(Result) || !cast<Constant>(Result)->isNullValue()) || |
| 1996 | !hasImplicitNull(TypeID) && |
| 1997 | "Cannot read null values from bytecode!"); |
| 1998 | return Result; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2001 | /// Resolve references for constants. This function resolves the forward |
| 2002 | /// referenced constants in the ConstantFwdRefs map. It uses the |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2003 | /// replaceAllUsesWith method of Value class to substitute the placeholder |
| 2004 | /// instance with the actual instance. |
Chris Lattner | 389bd04 | 2004-12-09 06:19:44 +0000 | [diff] [blame] | 2005 | void BytecodeReader::ResolveReferencesToConstant(Constant *NewV, unsigned Typ, |
| 2006 | unsigned Slot) { |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 2007 | ConstantRefsType::iterator I = |
Chris Lattner | 389bd04 | 2004-12-09 06:19:44 +0000 | [diff] [blame] | 2008 | ConstantFwdRefs.find(std::make_pair(Typ, Slot)); |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 2009 | if (I == ConstantFwdRefs.end()) return; // Never forward referenced? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2010 | |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 2011 | Value *PH = I->second; // Get the placeholder... |
| 2012 | PH->replaceAllUsesWith(NewV); |
| 2013 | delete PH; // Delete the old placeholder |
| 2014 | ConstantFwdRefs.erase(I); // Remove the map entry for it |
Vikram S. Adve | c1e4a81 | 2002-07-14 23:04:18 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2017 | /// Parse the constant strings section. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2018 | void BytecodeReader::ParseStringConstants(unsigned NumEntries, ValueTable &Tab){ |
| 2019 | for (; NumEntries; --NumEntries) { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2020 | unsigned Typ = read_vbr_uint(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2021 | const Type *Ty = getType(Typ); |
| 2022 | if (!isa<ArrayType>(Ty)) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2023 | error("String constant data invalid!"); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2024 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2025 | const ArrayType *ATy = cast<ArrayType>(Ty); |
| 2026 | if (ATy->getElementType() != Type::SByteTy && |
| 2027 | ATy->getElementType() != Type::UByteTy) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2028 | error("String constant data invalid!"); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2029 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2030 | // Read character data. The type tells us how long the string is. |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2031 | char *Data = reinterpret_cast<char *>(alloca(ATy->getNumElements())); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2032 | read_data(Data, Data+ATy->getNumElements()); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2033 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2034 | std::vector<Constant*> Elements(ATy->getNumElements()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2035 | const Type* ElemType = ATy->getElementType(); |
| 2036 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 2037 | Elements[i] = ConstantInt::get(ElemType, (unsigned char)Data[i]); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 2038 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2039 | // Create the constant, inserting it as needed. |
| 2040 | Constant *C = ConstantArray::get(ATy, Elements); |
| 2041 | unsigned Slot = insertValue(C, Typ, Tab); |
Chris Lattner | 389bd04 | 2004-12-09 06:19:44 +0000 | [diff] [blame] | 2042 | ResolveReferencesToConstant(C, Typ, Slot); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2043 | if (Handler) Handler->handleConstantString(cast<ConstantArray>(C)); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2044 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2047 | /// Parse the constant pool. |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2048 | void BytecodeReader::ParseConstantPool(ValueTable &Tab, |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2049 | TypeListTy &TypeTab, |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2050 | bool isFunction) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2051 | if (Handler) Handler->handleGlobalConstantsBegin(); |
| 2052 | |
| 2053 | /// In LLVM 1.3 Type does not derive from Value so the types |
| 2054 | /// do not occupy a plane. Consequently, we read the types |
| 2055 | /// first in the constant pool. |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2056 | if (isFunction) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2057 | unsigned NumEntries = read_vbr_uint(); |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2058 | ParseTypes(TypeTab, NumEntries); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2061 | while (moreInBlock()) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2062 | unsigned NumEntries = read_vbr_uint(); |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2063 | unsigned Typ = read_vbr_uint(); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2064 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2065 | if (Typ == Type::VoidTyID) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2066 | /// Use of Type::VoidTyID is a misnomer. It actually means |
| 2067 | /// that the following plane is constant strings |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2068 | assert(&Tab == &ModuleValues && "Cannot read strings in functions!"); |
| 2069 | ParseStringConstants(NumEntries, Tab); |
| 2070 | } else { |
| 2071 | for (unsigned i = 0; i < NumEntries; ++i) { |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 2072 | Value *V = ParseConstantPoolValue(Typ); |
| 2073 | assert(V && "ParseConstantPoolValue returned NULL!"); |
| 2074 | unsigned Slot = insertValue(V, Typ, Tab); |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 2075 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2076 | // If we are reading a function constant table, make sure that we adjust |
| 2077 | // the slot number to be the real global constant number. |
| 2078 | // |
| 2079 | if (&Tab != &ModuleValues && Typ < ModuleValues.size() && |
| 2080 | ModuleValues[Typ]) |
| 2081 | Slot += ModuleValues[Typ]->size(); |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 2082 | if (Constant *C = dyn_cast<Constant>(V)) |
| 2083 | ResolveReferencesToConstant(C, Typ, Slot); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2084 | } |
| 2085 | } |
| 2086 | } |
Chris Lattner | 02dce16 | 2004-12-04 05:28:27 +0000 | [diff] [blame] | 2087 | |
| 2088 | // After we have finished parsing the constant pool, we had better not have |
| 2089 | // any dangling references left. |
Reid Spencer | 3c39127 | 2004-12-04 22:19:53 +0000 | [diff] [blame] | 2090 | if (!ConstantFwdRefs.empty()) { |
Reid Spencer | 3c39127 | 2004-12-04 22:19:53 +0000 | [diff] [blame] | 2091 | ConstantRefsType::const_iterator I = ConstantFwdRefs.begin(); |
Reid Spencer | 3c39127 | 2004-12-04 22:19:53 +0000 | [diff] [blame] | 2092 | Constant* missingConst = I->second; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2093 | error(utostr(ConstantFwdRefs.size()) + |
| 2094 | " unresolved constant reference exist. First one is '" + |
| 2095 | missingConst->getName() + "' of type '" + |
Chris Lattner | 389bd04 | 2004-12-09 06:19:44 +0000 | [diff] [blame] | 2096 | missingConst->getType()->getDescription() + "'."); |
Reid Spencer | 3c39127 | 2004-12-04 22:19:53 +0000 | [diff] [blame] | 2097 | } |
Chris Lattner | 02dce16 | 2004-12-04 05:28:27 +0000 | [diff] [blame] | 2098 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2099 | checkPastBlockEnd("Constant Pool"); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2100 | if (Handler) Handler->handleGlobalConstantsEnd(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2101 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2102 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2103 | /// Parse the contents of a function. Note that this function can be |
| 2104 | /// called lazily by materializeFunction |
| 2105 | /// @see materializeFunction |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2106 | void BytecodeReader::ParseFunctionBody(Function* F) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2107 | |
| 2108 | unsigned FuncSize = BlockEnd - At; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 2109 | GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage; |
| 2110 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2111 | unsigned LinkageType = read_vbr_uint(); |
Chris Lattner | c08912f | 2004-01-14 16:44:44 +0000 | [diff] [blame] | 2112 | switch (LinkageType) { |
| 2113 | case 0: Linkage = GlobalValue::ExternalLinkage; break; |
| 2114 | case 1: Linkage = GlobalValue::WeakLinkage; break; |
| 2115 | case 2: Linkage = GlobalValue::AppendingLinkage; break; |
| 2116 | case 3: Linkage = GlobalValue::InternalLinkage; break; |
| 2117 | case 4: Linkage = GlobalValue::LinkOnceLinkage; break; |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 2118 | case 5: Linkage = GlobalValue::DLLImportLinkage; break; |
| 2119 | case 6: Linkage = GlobalValue::DLLExportLinkage; break; |
| 2120 | case 7: Linkage = GlobalValue::ExternalWeakLinkage; break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2121 | default: |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2122 | error("Invalid linkage type for Function."); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2123 | Linkage = GlobalValue::InternalLinkage; |
| 2124 | break; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 2125 | } |
Chris Lattner | d23b1d3 | 2001-11-26 18:56:10 +0000 | [diff] [blame] | 2126 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2127 | F->setLinkage(Linkage); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2128 | if (Handler) Handler->handleFunctionBegin(F,FuncSize); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2129 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 2130 | // Keep track of how many basic blocks we have read in... |
| 2131 | unsigned BlockNum = 0; |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2132 | bool InsertedArguments = false; |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 2133 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2134 | BufPtr MyEnd = BlockEnd; |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2135 | while (At < MyEnd) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2136 | unsigned Type, Size; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2137 | BufPtr OldAt = At; |
| 2138 | read_block(Type, Size); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2139 | |
| 2140 | switch (Type) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2141 | case BytecodeFormat::ConstantPoolBlockID: |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2142 | if (!InsertedArguments) { |
| 2143 | // Insert arguments into the value table before we parse the first basic |
| 2144 | // block in the function, but after we potentially read in the |
| 2145 | // compaction table. |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2146 | insertArguments(F); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2147 | InsertedArguments = true; |
| 2148 | } |
| 2149 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2150 | ParseConstantPool(FunctionValues, FunctionTypes, true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2151 | break; |
| 2152 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2153 | case BytecodeFormat::CompactionTableBlockID: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2154 | ParseCompactionTable(); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2155 | break; |
| 2156 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2157 | case BytecodeFormat::InstructionListBlockID: { |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2158 | // Insert arguments into the value table before we parse the instruction |
| 2159 | // list for the function, but after we potentially read in the compaction |
| 2160 | // table. |
| 2161 | if (!InsertedArguments) { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2162 | insertArguments(F); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2163 | InsertedArguments = true; |
| 2164 | } |
| 2165 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2166 | if (BlockNum) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2167 | error("Already parsed basic blocks!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2168 | BlockNum = ParseInstructionList(F); |
Chris Lattner | 8d1dbd2 | 2003-12-01 07:05:31 +0000 | [diff] [blame] | 2169 | break; |
| 2170 | } |
| 2171 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2172 | case BytecodeFormat::SymbolTableBlockID: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2173 | ParseSymbolTable(F, &F->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2174 | break; |
| 2175 | |
| 2176 | default: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2177 | At += Size; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2178 | if (OldAt > At) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2179 | error("Wrapped around reading bytecode."); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2180 | break; |
| 2181 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2182 | BlockEnd = MyEnd; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 2185 | // Make sure there were no references to non-existant basic blocks. |
| 2186 | if (BlockNum != ParsedBasicBlocks.size()) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2187 | error("Illegal basic block operand reference"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2188 | |
Chris Lattner | 4ee8ef2 | 2003-10-08 22:52:54 +0000 | [diff] [blame] | 2189 | ParsedBasicBlocks.clear(); |
| 2190 | |
Chris Lattner | 97330cf | 2003-10-09 23:10:14 +0000 | [diff] [blame] | 2191 | // Resolve forward references. Replace any uses of a forward reference value |
| 2192 | // with the real value. |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 2193 | while (!ForwardReferences.empty()) { |
Chris Lattner | c4d6916 | 2004-12-09 04:51:50 +0000 | [diff] [blame] | 2194 | std::map<std::pair<unsigned,unsigned>, Value*>::iterator |
| 2195 | I = ForwardReferences.begin(); |
| 2196 | Value *V = getValue(I->first.first, I->first.second, false); |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 2197 | Value *PlaceHolder = I->second; |
Chris Lattner | c4d6916 | 2004-12-09 04:51:50 +0000 | [diff] [blame] | 2198 | PlaceHolder->replaceAllUsesWith(V); |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 2199 | ForwardReferences.erase(I); |
Chris Lattner | 8eb10ce | 2003-10-09 06:05:40 +0000 | [diff] [blame] | 2200 | delete PlaceHolder; |
Chris Lattner | 6e44802 | 2003-10-08 21:51:46 +0000 | [diff] [blame] | 2201 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2202 | |
Reid Spencer | e2a5fb0 | 2006-01-27 11:49:27 +0000 | [diff] [blame] | 2203 | // If upgraded intrinsic functions were detected during reading of the |
| 2204 | // module information, then we need to look for instructions that need to |
| 2205 | // be upgraded. This can't be done while the instructions are read in because |
| 2206 | // additional instructions inserted mess up the slot numbering. |
| 2207 | if (!upgradedFunctions.empty()) { |
| 2208 | for (Function::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI) |
| 2209 | for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); |
Jim Laskey | f4321a3 | 2006-03-13 13:07:37 +0000 | [diff] [blame] | 2210 | II != IE;) |
| 2211 | if (CallInst* CI = dyn_cast<CallInst>(II++)) { |
Reid Spencer | e2a5fb0 | 2006-01-27 11:49:27 +0000 | [diff] [blame] | 2212 | std::map<Function*,Function*>::iterator FI = |
| 2213 | upgradedFunctions.find(CI->getCalledFunction()); |
Chris Lattner | bad0800 | 2006-03-02 23:59:12 +0000 | [diff] [blame] | 2214 | if (FI != upgradedFunctions.end()) |
| 2215 | UpgradeIntrinsicCall(CI, FI->second); |
Reid Spencer | e2a5fb0 | 2006-01-27 11:49:27 +0000 | [diff] [blame] | 2216 | } |
| 2217 | } |
| 2218 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 2219 | // Clear out function-level types... |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2220 | FunctionTypes.clear(); |
| 2221 | CompactionTypes.clear(); |
| 2222 | CompactionValues.clear(); |
| 2223 | freeTable(FunctionValues); |
| 2224 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2225 | if (Handler) Handler->handleFunctionEnd(F); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2228 | /// This function parses LLVM functions lazily. It obtains the type of the |
| 2229 | /// function and records where the body of the function is in the bytecode |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2230 | /// buffer. The caller can then use the ParseNextFunction and |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2231 | /// ParseAllFunctionBodies to get handler events for the functions. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2232 | void BytecodeReader::ParseFunctionLazily() { |
| 2233 | if (FunctionSignatureList.empty()) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2234 | error("FunctionSignatureList empty!"); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2235 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2236 | Function *Func = FunctionSignatureList.back(); |
| 2237 | FunctionSignatureList.pop_back(); |
Chris Lattner | 2410243 | 2004-01-18 22:35:34 +0000 | [diff] [blame] | 2238 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2239 | // Save the information for future reading of the function |
| 2240 | LazyFunctionLoadMap[Func] = LazyFunctionInfo(BlockStart, BlockEnd); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2241 | |
Misha Brukman | a3e6ad6 | 2004-11-14 21:02:55 +0000 | [diff] [blame] | 2242 | // This function has a body but it's not loaded so it appears `External'. |
| 2243 | // Mark it as a `Ghost' instead to notify the users that it has a body. |
| 2244 | Func->setLinkage(GlobalValue::GhostLinkage); |
| 2245 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2246 | // Pretend we've `parsed' this function |
| 2247 | At = BlockEnd; |
| 2248 | } |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2249 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2250 | /// The ParserFunction method lazily parses one function. Use this method to |
| 2251 | /// casue the parser to parse a specific function in the module. Note that |
| 2252 | /// this will remove the function from what is to be included by |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2253 | /// ParseAllFunctionBodies. |
| 2254 | /// @see ParseAllFunctionBodies |
| 2255 | /// @see ParseBytecode |
Reid Spencer | 99655e1 | 2006-08-25 19:54:53 +0000 | [diff] [blame] | 2256 | bool BytecodeReader::ParseFunction(Function* Func, std::string* ErrMsg) { |
| 2257 | |
| 2258 | if (setjmp(context)) |
| 2259 | return true; |
| 2260 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2261 | // Find {start, end} pointers and slot in the map. If not there, we're done. |
| 2262 | LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2263 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2264 | // Make sure we found it |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2265 | if (Fi == LazyFunctionLoadMap.end()) { |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2266 | error("Unrecognized function of type " + Func->getType()->getDescription()); |
Reid Spencer | 99655e1 | 2006-08-25 19:54:53 +0000 | [diff] [blame] | 2267 | return true; |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2270 | BlockStart = At = Fi->second.Buf; |
| 2271 | BlockEnd = Fi->second.EndBuf; |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2272 | assert(Fi->first == Func && "Found wrong function?"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2273 | |
| 2274 | LazyFunctionLoadMap.erase(Fi); |
| 2275 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2276 | this->ParseFunctionBody(Func); |
Reid Spencer | 99655e1 | 2006-08-25 19:54:53 +0000 | [diff] [blame] | 2277 | return false; |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2280 | /// The ParseAllFunctionBodies method parses through all the previously |
| 2281 | /// unparsed functions in the bytecode file. If you want to completely parse |
| 2282 | /// a bytecode file, this method should be called after Parsebytecode because |
| 2283 | /// Parsebytecode only records the locations in the bytecode file of where |
| 2284 | /// the function definitions are located. This function uses that information |
| 2285 | /// to materialize the functions. |
| 2286 | /// @see ParseBytecode |
Reid Spencer | 99655e1 | 2006-08-25 19:54:53 +0000 | [diff] [blame] | 2287 | bool BytecodeReader::ParseAllFunctionBodies(std::string* ErrMsg) { |
| 2288 | if (setjmp(context)) |
| 2289 | return true; |
| 2290 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2291 | LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin(); |
| 2292 | LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end(); |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2293 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2294 | while (Fi != Fe) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2295 | Function* Func = Fi->first; |
| 2296 | BlockStart = At = Fi->second.Buf; |
| 2297 | BlockEnd = Fi->second.EndBuf; |
Chris Lattner | b52f1c2 | 2005-02-13 17:48:18 +0000 | [diff] [blame] | 2298 | ParseFunctionBody(Func); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2299 | ++Fi; |
| 2300 | } |
Chris Lattner | b52f1c2 | 2005-02-13 17:48:18 +0000 | [diff] [blame] | 2301 | LazyFunctionLoadMap.clear(); |
Reid Spencer | 99655e1 | 2006-08-25 19:54:53 +0000 | [diff] [blame] | 2302 | return false; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2303 | } |
Chris Lattner | 89e0253 | 2004-01-18 21:08:15 +0000 | [diff] [blame] | 2304 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2305 | /// Parse the global type list |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2306 | void BytecodeReader::ParseGlobalTypes() { |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2307 | // Read the number of types |
| 2308 | unsigned NumEntries = read_vbr_uint(); |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2309 | ParseTypes(ModuleTypes, NumEntries); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2312 | /// Parse the Global info (types, global vars, constants) |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2313 | void BytecodeReader::ParseModuleGlobalInfo() { |
| 2314 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2315 | if (Handler) Handler->handleModuleGlobalsBegin(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2316 | |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 2317 | // SectionID - If a global has an explicit section specified, this map |
| 2318 | // remembers the ID until we can translate it into a string. |
| 2319 | std::map<GlobalValue*, unsigned> SectionID; |
| 2320 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 2321 | // Read global variables... |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2322 | unsigned VarType = read_vbr_uint(); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 2323 | while (VarType != Type::VoidTyID) { // List is terminated by Void |
Chris Lattner | 9dd8770 | 2004-04-03 23:43:42 +0000 | [diff] [blame] | 2324 | // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3,4 = |
| 2325 | // Linkage, bit4+ = slot# |
| 2326 | unsigned SlotNo = VarType >> 5; |
| 2327 | unsigned LinkageID = (VarType >> 2) & 7; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2328 | bool isConstant = VarType & 1; |
Chris Lattner | ce5e04e | 2005-11-06 08:23:17 +0000 | [diff] [blame] | 2329 | bool hasInitializer = (VarType & 2) != 0; |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 2330 | unsigned Alignment = 0; |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 2331 | unsigned GlobalSectionID = 0; |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 2332 | |
| 2333 | // An extension word is present when linkage = 3 (internal) and hasinit = 0. |
| 2334 | if (LinkageID == 3 && !hasInitializer) { |
| 2335 | unsigned ExtWord = read_vbr_uint(); |
| 2336 | // The extension word has this format: bit 0 = has initializer, bit 1-3 = |
| 2337 | // linkage, bit 4-8 = alignment (log2), bits 10+ = future use. |
| 2338 | hasInitializer = ExtWord & 1; |
| 2339 | LinkageID = (ExtWord >> 1) & 7; |
| 2340 | Alignment = (1 << ((ExtWord >> 4) & 31)) >> 1; |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 2341 | |
| 2342 | if (ExtWord & (1 << 9)) // Has a section ID. |
| 2343 | GlobalSectionID = read_vbr_uint(); |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 2344 | } |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 2345 | |
Chris Lattner | ce5e04e | 2005-11-06 08:23:17 +0000 | [diff] [blame] | 2346 | GlobalValue::LinkageTypes Linkage; |
Chris Lattner | c08912f | 2004-01-14 16:44:44 +0000 | [diff] [blame] | 2347 | switch (LinkageID) { |
Chris Lattner | c08912f | 2004-01-14 16:44:44 +0000 | [diff] [blame] | 2348 | case 0: Linkage = GlobalValue::ExternalLinkage; break; |
| 2349 | case 1: Linkage = GlobalValue::WeakLinkage; break; |
| 2350 | case 2: Linkage = GlobalValue::AppendingLinkage; break; |
| 2351 | case 3: Linkage = GlobalValue::InternalLinkage; break; |
| 2352 | case 4: Linkage = GlobalValue::LinkOnceLinkage; break; |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 2353 | case 5: Linkage = GlobalValue::DLLImportLinkage; break; |
| 2354 | case 6: Linkage = GlobalValue::DLLExportLinkage; break; |
| 2355 | case 7: Linkage = GlobalValue::ExternalWeakLinkage; break; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2356 | default: |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2357 | error("Unknown linkage type: " + utostr(LinkageID)); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2358 | Linkage = GlobalValue::InternalLinkage; |
| 2359 | break; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | const Type *Ty = getType(SlotNo); |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 2363 | if (!Ty) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2364 | error("Global has no type! SlotNo=" + utostr(SlotNo)); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2365 | |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 2366 | if (!isa<PointerType>(Ty)) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2367 | error("Global not a pointer type! Ty= " + Ty->getDescription()); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 2368 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2369 | const Type *ElTy = cast<PointerType>(Ty)->getElementType(); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 2370 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 2371 | // Create the global variable... |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2372 | GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage, |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2373 | 0, "", TheModule); |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 2374 | GV->setAlignment(Alignment); |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 2375 | insertValue(GV, SlotNo, ModuleValues); |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 2376 | |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 2377 | if (GlobalSectionID != 0) |
| 2378 | SectionID[GV] = GlobalSectionID; |
| 2379 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2380 | unsigned initSlot = 0; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2381 | if (hasInitializer) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2382 | initSlot = read_vbr_uint(); |
| 2383 | GlobalInits.push_back(std::make_pair(GV, initSlot)); |
| 2384 | } |
| 2385 | |
| 2386 | // Notify handler about the global value. |
Chris Lattner | 4a242b3 | 2004-10-14 01:39:18 +0000 | [diff] [blame] | 2387 | if (Handler) |
| 2388 | Handler->handleGlobalVariable(ElTy, isConstant, Linkage, SlotNo,initSlot); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2389 | |
| 2390 | // Get next item |
| 2391 | VarType = read_vbr_uint(); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2394 | // Read the function objects for all of the functions that are coming |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 2395 | unsigned FnSignature = read_vbr_uint(); |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2396 | |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 2397 | // List is terminated by VoidTy. |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 2398 | while (((FnSignature & (~0U >> 1)) >> 5) != Type::VoidTyID) { |
| 2399 | const Type *Ty = getType((FnSignature & (~0U >> 1)) >> 5); |
Chris Lattner | 927b185 | 2003-10-09 20:22:47 +0000 | [diff] [blame] | 2400 | if (!isa<PointerType>(Ty) || |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2401 | !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) { |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2402 | error("Function not a pointer to function type! Ty = " + |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2403 | Ty->getDescription()); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2404 | } |
Chris Lattner | 8cdc6b7 | 2002-10-23 00:51:54 +0000 | [diff] [blame] | 2405 | |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 2406 | // We create functions by passing the underlying FunctionType to create... |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2407 | const FunctionType* FTy = |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2408 | cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2409 | |
Chris Lattner | 18549c2 | 2004-11-15 21:43:03 +0000 | [diff] [blame] | 2410 | // Insert the place holder. |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 2411 | Function *Func = new Function(FTy, GlobalValue::ExternalLinkage, |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2412 | "", TheModule); |
Reid Spencer | e1e96c0 | 2006-01-19 07:02:16 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 2414 | insertValue(Func, (FnSignature & (~0U >> 1)) >> 5, ModuleValues); |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 2415 | |
| 2416 | // Flags are not used yet. |
Chris Lattner | 97fbc50 | 2004-11-15 22:38:52 +0000 | [diff] [blame] | 2417 | unsigned Flags = FnSignature & 31; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2418 | |
Chris Lattner | 97fbc50 | 2004-11-15 22:38:52 +0000 | [diff] [blame] | 2419 | // Save this for later so we know type of lazily instantiated functions. |
| 2420 | // Note that known-external functions do not have FunctionInfo blocks, so we |
| 2421 | // do not add them to the FunctionSignatureList. |
| 2422 | if ((Flags & (1 << 4)) == 0) |
| 2423 | FunctionSignatureList.push_back(Func); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2424 | |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 2425 | // Get the calling convention from the low bits. |
| 2426 | unsigned CC = Flags & 15; |
| 2427 | unsigned Alignment = 0; |
| 2428 | if (FnSignature & (1 << 31)) { // Has extension word? |
| 2429 | unsigned ExtWord = read_vbr_uint(); |
| 2430 | Alignment = (1 << (ExtWord & 31)) >> 1; |
| 2431 | CC |= ((ExtWord >> 5) & 15) << 4; |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 2432 | |
| 2433 | if (ExtWord & (1 << 10)) // Has a section ID. |
| 2434 | SectionID[Func] = read_vbr_uint(); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 2435 | |
| 2436 | // Parse external declaration linkage |
| 2437 | switch ((ExtWord >> 11) & 3) { |
| 2438 | case 0: break; |
| 2439 | case 1: Func->setLinkage(Function::DLLImportLinkage); break; |
| 2440 | case 2: Func->setLinkage(Function::ExternalWeakLinkage); break; |
| 2441 | default: assert(0 && "Unsupported external linkage"); |
| 2442 | } |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Chris Lattner | 54b369e | 2005-11-06 07:46:13 +0000 | [diff] [blame] | 2445 | Func->setCallingConv(CC-1); |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 2446 | Func->setAlignment(Alignment); |
Chris Lattner | 479ffeb | 2005-05-06 20:42:57 +0000 | [diff] [blame] | 2447 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2448 | if (Handler) Handler->handleFunctionDeclaration(Func); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2449 | |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 2450 | // Get the next function signature. |
| 2451 | FnSignature = read_vbr_uint(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2454 | // Now that the function signature list is set up, reverse it so that we can |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 2455 | // remove elements efficiently from the back of the vector. |
| 2456 | std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2457 | |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 2458 | /// SectionNames - This contains the list of section names encoded in the |
| 2459 | /// moduleinfoblock. Functions and globals with an explicit section index |
| 2460 | /// into this to get their section name. |
| 2461 | std::vector<std::string> SectionNames; |
| 2462 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2463 | // Read in the dependent library information. |
| 2464 | unsigned num_dep_libs = read_vbr_uint(); |
| 2465 | std::string dep_lib; |
| 2466 | while (num_dep_libs--) { |
| 2467 | dep_lib = read_str(); |
| 2468 | TheModule->addLibrary(dep_lib); |
Reid Spencer | 5b472d9 | 2004-08-21 20:49:23 +0000 | [diff] [blame] | 2469 | if (Handler) |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2470 | Handler->handleDependentLibrary(dep_lib); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2473 | // Read target triple and place into the module. |
| 2474 | std::string triple = read_str(); |
| 2475 | TheModule->setTargetTriple(triple); |
| 2476 | if (Handler) |
| 2477 | Handler->handleTargetTriple(triple); |
| 2478 | |
| 2479 | if (At != BlockEnd) { |
| 2480 | // If the file has section info in it, read the section names now. |
| 2481 | unsigned NumSections = read_vbr_uint(); |
| 2482 | while (NumSections--) |
| 2483 | SectionNames.push_back(read_str()); |
| 2484 | } |
| 2485 | |
| 2486 | // If the file has module-level inline asm, read it now. |
| 2487 | if (At != BlockEnd) |
| 2488 | TheModule->setModuleInlineAsm(read_str()); |
| 2489 | |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 2490 | // If any globals are in specified sections, assign them now. |
| 2491 | for (std::map<GlobalValue*, unsigned>::iterator I = SectionID.begin(), E = |
| 2492 | SectionID.end(); I != E; ++I) |
| 2493 | if (I->second) { |
| 2494 | if (I->second > SectionID.size()) |
| 2495 | error("SectionID out of range for global!"); |
| 2496 | I->first->setSection(SectionNames[I->second-1]); |
| 2497 | } |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2498 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2499 | // This is for future proofing... in the future extra fields may be added that |
| 2500 | // we don't understand, so we transparently ignore them. |
| 2501 | // |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2502 | At = BlockEnd; |
| 2503 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2504 | if (Handler) Handler->handleModuleGlobalsEnd(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2507 | /// Parse the version information and decode it by setting flags on the |
| 2508 | /// Reader that enable backward compatibility of the reader. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2509 | void BytecodeReader::ParseVersionInfo() { |
| 2510 | unsigned Version = read_vbr_uint(); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 2511 | |
| 2512 | // Unpack version number: low four bits are for flags, top bits = version |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 2513 | Module::Endianness Endianness; |
| 2514 | Module::PointerSize PointerSize; |
| 2515 | Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian; |
| 2516 | PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32; |
| 2517 | |
| 2518 | bool hasNoEndianness = Version & 4; |
| 2519 | bool hasNoPointerSize = Version & 8; |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2520 | |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 2521 | RevisionNum = Version >> 4; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 2522 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2523 | // Default the backwards compatibility flag values for the current BC version |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 2524 | hasSignlessDivRem = false; |
| 2525 | hasSignlessShrCastSetcc = false; |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 2526 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2527 | // Determine which backwards compatibility flags to set based on the |
| 2528 | // bytecode file's version number |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 2529 | switch (RevisionNum) { |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2530 | case 0: // LLVM 1.0, 1.1 (Released) |
| 2531 | case 1: // LLVM 1.2 (Released) |
| 2532 | case 2: // 1.2.5 (Not Released) |
| 2533 | case 3: // LLVM 1.3 (Released) |
| 2534 | case 4: // 1.3.1 (Not Released) |
| 2535 | error("Old bytecode formats no longer supported"); |
| 2536 | break; |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2537 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2538 | case 5: // 1.4 (Released) |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 2539 | // In version 6, the Div and Rem instructions were converted to their |
| 2540 | // signed and floating point counterparts: UDiv, SDiv, FDiv, URem, SRem, |
| 2541 | // and FRem. Versions prior to 6 need to indicate that they have the |
| 2542 | // signless Div and Rem instructions. |
| 2543 | hasSignlessDivRem = true; |
| 2544 | |
| 2545 | // FALL THROUGH |
| 2546 | |
Reid Spencer | d798a51 | 2006-11-14 04:47:22 +0000 | [diff] [blame] | 2547 | case 6: // 1.9 (Released) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2548 | // In version 5 and prior, instructions were signless while integer types |
| 2549 | // were signed. In version 6, instructions became signed and types became |
| 2550 | // signless. For example in version 5 we have the DIV instruction but in |
| 2551 | // version 6 we have FDIV, SDIV and UDIV to replace it. This caused a |
| 2552 | // renumbering of the instruction codes in version 6 that must be dealt with |
| 2553 | // when reading old bytecode files. |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 2554 | hasSignlessShrCastSetcc = true; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2555 | |
| 2556 | // FALL THROUGH |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 2557 | |
| 2558 | case 7: |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 2559 | break; |
| 2560 | |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 2561 | default: |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2562 | error("Unknown bytecode version number: " + itostr(RevisionNum)); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 2565 | if (hasNoEndianness) Endianness = Module::AnyEndianness; |
| 2566 | if (hasNoPointerSize) PointerSize = Module::AnyPointerSize; |
Chris Lattner | 76e3896 | 2003-04-22 18:15:10 +0000 | [diff] [blame] | 2567 | |
Brian Gaeke | fe2102b | 2004-07-14 20:33:13 +0000 | [diff] [blame] | 2568 | TheModule->setEndianness(Endianness); |
| 2569 | TheModule->setPointerSize(PointerSize); |
| 2570 | |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2571 | if (Handler) Handler->handleVersionInfo(RevisionNum, Endianness, PointerSize); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2574 | /// Parse a whole module. |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2575 | void BytecodeReader::ParseModule() { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2576 | unsigned Type, Size; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2577 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2578 | FunctionSignatureList.clear(); // Just in case... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2579 | |
| 2580 | // Read into instance variables... |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2581 | ParseVersionInfo(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2582 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2583 | bool SeenModuleGlobalInfo = false; |
| 2584 | bool SeenGlobalTypePlane = false; |
| 2585 | BufPtr MyEnd = BlockEnd; |
| 2586 | while (At < MyEnd) { |
| 2587 | BufPtr OldAt = At; |
| 2588 | read_block(Type, Size); |
| 2589 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2590 | switch (Type) { |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2591 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2592 | case BytecodeFormat::GlobalTypePlaneBlockID: |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2593 | if (SeenGlobalTypePlane) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2594 | error("Two GlobalTypePlane Blocks Encountered!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2595 | |
Reid Spencer | 5b472d9 | 2004-08-21 20:49:23 +0000 | [diff] [blame] | 2596 | if (Size > 0) |
| 2597 | ParseGlobalTypes(); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2598 | SeenGlobalTypePlane = true; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2599 | break; |
| 2600 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2601 | case BytecodeFormat::ModuleGlobalInfoBlockID: |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2602 | if (SeenModuleGlobalInfo) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2603 | error("Two ModuleGlobalInfo Blocks Encountered!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2604 | ParseModuleGlobalInfo(); |
| 2605 | SeenModuleGlobalInfo = true; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2606 | break; |
| 2607 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2608 | case BytecodeFormat::ConstantPoolBlockID: |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2609 | ParseConstantPool(ModuleValues, ModuleTypes,false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2610 | break; |
| 2611 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2612 | case BytecodeFormat::FunctionBlockID: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2613 | ParseFunctionLazily(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2614 | break; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2615 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 2616 | case BytecodeFormat::SymbolTableBlockID: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2617 | ParseSymbolTable(0, &TheModule->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2618 | break; |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2619 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2620 | default: |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2621 | At += Size; |
| 2622 | if (OldAt > At) { |
Reid Spencer | 46b002c | 2004-07-11 17:28:43 +0000 | [diff] [blame] | 2623 | error("Unexpected Block of Type #" + utostr(Type) + " encountered!"); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2624 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2625 | break; |
| 2626 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2627 | BlockEnd = MyEnd; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2630 | // After the module constant pool has been read, we can safely initialize |
| 2631 | // global variables... |
| 2632 | while (!GlobalInits.empty()) { |
| 2633 | GlobalVariable *GV = GlobalInits.back().first; |
| 2634 | unsigned Slot = GlobalInits.back().second; |
| 2635 | GlobalInits.pop_back(); |
| 2636 | |
| 2637 | // Look up the initializer value... |
Chris Lattner | 29b789b | 2003-11-19 17:27:18 +0000 | [diff] [blame] | 2638 | // FIXME: Preserve this type ID! |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2639 | |
| 2640 | const llvm::PointerType* GVType = GV->getType(); |
| 2641 | unsigned TypeSlot = getTypeSlot(GVType->getElementType()); |
Chris Lattner | 9336199 | 2004-01-15 18:45:25 +0000 | [diff] [blame] | 2642 | if (Constant *CV = getConstantValue(TypeSlot, Slot)) { |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2643 | if (GV->hasInitializer()) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2644 | error("Global *already* has an initializer?!"); |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2645 | if (Handler) Handler->handleGlobalInitializer(GV,CV); |
Chris Lattner | 9336199 | 2004-01-15 18:45:25 +0000 | [diff] [blame] | 2646 | GV->setInitializer(CV); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2647 | } else |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2648 | error("Cannot find initializer value."); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Chris Lattner | aba5ff5 | 2005-05-05 20:57:00 +0000 | [diff] [blame] | 2651 | if (!ConstantFwdRefs.empty()) |
| 2652 | error("Use of undefined constants in a module"); |
| 2653 | |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2654 | /// Make sure we pulled them all out. If we didn't then there's a declaration |
| 2655 | /// but a missing body. That's not allowed. |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 2656 | if (!FunctionSignatureList.empty()) |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 2657 | error("Function declared, but bytecode stream ended before definition"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Reid Spencer | 04cde2c | 2004-07-04 11:33:49 +0000 | [diff] [blame] | 2660 | /// This function completely parses a bytecode buffer given by the \p Buf |
| 2661 | /// and \p Length parameters. |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 2662 | bool BytecodeReader::ParseBytecode(volatile BufPtr Buf, unsigned Length, |
Reid Spencer | 233fe72 | 2006-08-22 16:09:19 +0000 | [diff] [blame] | 2663 | const std::string &ModuleID, |
| 2664 | std::string* ErrMsg) { |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 2665 | |
Reid Spencer | 233fe72 | 2006-08-22 16:09:19 +0000 | [diff] [blame] | 2666 | /// We handle errors by |
| 2667 | if (setjmp(context)) { |
| 2668 | // Cleanup after error |
| 2669 | if (Handler) Handler->handleError(ErrorMsg); |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2670 | freeState(); |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 2671 | delete TheModule; |
| 2672 | TheModule = 0; |
Chris Lattner | 3bdad69 | 2004-11-15 21:55:33 +0000 | [diff] [blame] | 2673 | if (decompressedBlock != 0 ) { |
Reid Spencer | 61aaf2e | 2004-11-14 21:59:21 +0000 | [diff] [blame] | 2674 | ::free(decompressedBlock); |
Chris Lattner | 3bdad69 | 2004-11-15 21:55:33 +0000 | [diff] [blame] | 2675 | decompressedBlock = 0; |
| 2676 | } |
Reid Spencer | 233fe72 | 2006-08-22 16:09:19 +0000 | [diff] [blame] | 2677 | // Set caller's error message, if requested |
| 2678 | if (ErrMsg) |
| 2679 | *ErrMsg = ErrorMsg; |
| 2680 | // Indicate an error occurred |
| 2681 | return true; |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 2682 | } |
Reid Spencer | 233fe72 | 2006-08-22 16:09:19 +0000 | [diff] [blame] | 2683 | |
| 2684 | RevisionNum = 0; |
| 2685 | At = MemStart = BlockStart = Buf; |
| 2686 | MemEnd = BlockEnd = Buf + Length; |
| 2687 | |
| 2688 | // Create the module |
| 2689 | TheModule = new Module(ModuleID); |
| 2690 | |
| 2691 | if (Handler) Handler->handleStart(TheModule, Length); |
| 2692 | |
| 2693 | // Read the four bytes of the signature. |
| 2694 | unsigned Sig = read_uint(); |
| 2695 | |
| 2696 | // If this is a compressed file |
| 2697 | if (Sig == ('l' | ('l' << 8) | ('v' << 16) | ('c' << 24))) { |
| 2698 | |
| 2699 | // Invoke the decompression of the bytecode. Note that we have to skip the |
| 2700 | // file's magic number which is not part of the compressed block. Hence, |
| 2701 | // the Buf+4 and Length-4. The result goes into decompressedBlock, a data |
| 2702 | // member for retention until BytecodeReader is destructed. |
| 2703 | unsigned decompressedLength = Compressor::decompressToNewBuffer( |
| 2704 | (char*)Buf+4,Length-4,decompressedBlock); |
| 2705 | |
| 2706 | // We must adjust the buffer pointers used by the bytecode reader to point |
| 2707 | // into the new decompressed block. After decompression, the |
| 2708 | // decompressedBlock will point to a contiguous memory area that has |
| 2709 | // the decompressed data. |
| 2710 | At = MemStart = BlockStart = Buf = (BufPtr) decompressedBlock; |
| 2711 | MemEnd = BlockEnd = Buf + decompressedLength; |
| 2712 | |
| 2713 | // else if this isn't a regular (uncompressed) bytecode file, then its |
| 2714 | // and error, generate that now. |
| 2715 | } else if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24))) { |
| 2716 | error("Invalid bytecode signature: " + utohexstr(Sig)); |
| 2717 | } |
| 2718 | |
| 2719 | // Tell the handler we're starting a module |
| 2720 | if (Handler) Handler->handleModuleBegin(ModuleID); |
| 2721 | |
| 2722 | // Get the module block and size and verify. This is handled specially |
| 2723 | // because the module block/size is always written in long format. Other |
| 2724 | // blocks are written in short format so the read_block method is used. |
| 2725 | unsigned Type, Size; |
| 2726 | Type = read_uint(); |
| 2727 | Size = read_uint(); |
| 2728 | if (Type != BytecodeFormat::ModuleBlockID) { |
| 2729 | error("Expected Module Block! Type:" + utostr(Type) + ", Size:" |
| 2730 | + utostr(Size)); |
| 2731 | } |
| 2732 | |
| 2733 | // It looks like the darwin ranlib program is broken, and adds trailing |
| 2734 | // garbage to the end of some bytecode files. This hack allows the bc |
| 2735 | // reader to ignore trailing garbage on bytecode files. |
| 2736 | if (At + Size < MemEnd) |
| 2737 | MemEnd = BlockEnd = At+Size; |
| 2738 | |
| 2739 | if (At + Size != MemEnd) |
| 2740 | error("Invalid Top Level Block Length! Type:" + utostr(Type) |
| 2741 | + ", Size:" + utostr(Size)); |
| 2742 | |
| 2743 | // Parse the module contents |
| 2744 | this->ParseModule(); |
| 2745 | |
| 2746 | // Check for missing functions |
| 2747 | if (hasFunctions()) |
| 2748 | error("Function expected, but bytecode stream ended!"); |
| 2749 | |
| 2750 | // Look for intrinsic functions to upgrade, upgrade them, and save the |
| 2751 | // mapping from old function to new for use later when instructions are |
| 2752 | // converted. |
| 2753 | for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 2754 | FI != FE; ++FI) |
| 2755 | if (Function* newF = UpgradeIntrinsicFunction(FI)) { |
| 2756 | upgradedFunctions.insert(std::make_pair(FI, newF)); |
| 2757 | FI->setName(""); |
| 2758 | } |
| 2759 | |
| 2760 | // Tell the handler we're done with the module |
| 2761 | if (Handler) |
| 2762 | Handler->handleModuleEnd(ModuleID); |
| 2763 | |
| 2764 | // Tell the handler we're finished the parse |
| 2765 | if (Handler) Handler->handleFinish(); |
| 2766 | |
| 2767 | return false; |
| 2768 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2769 | } |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2770 | |
| 2771 | //===----------------------------------------------------------------------===// |
| 2772 | //=== Default Implementations of Handler Methods |
| 2773 | //===----------------------------------------------------------------------===// |
| 2774 | |
| 2775 | BytecodeHandler::~BytecodeHandler() {} |
Reid Spencer | 060d25d | 2004-06-29 23:29:38 +0000 | [diff] [blame] | 2776 | |