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