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