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