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