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