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