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