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