Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1 | //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header defines the BitcodeReader class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 14 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 15 | #include "BitcodeReader.h" |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 2bce93a | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 18 | #include "llvm/InlineAsm.h" |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 21 | #include "llvm/AutoUpgrade.h" |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Devang Patel | f4511cd | 2008-02-26 19:38:17 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 28 | void BitcodeReader::FreeState() { |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 29 | delete Buffer; |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 30 | Buffer = 0; |
| 31 | std::vector<PATypeHolder>().swap(TypeList); |
| 32 | ValueList.clear(); |
Chris Lattner | 461edd9 | 2008-03-12 02:25:52 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame^] | 34 | std::vector<PAListPtr>().swap(ParamAttrs); |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 35 | std::vector<BasicBlock*>().swap(FunctionBBs); |
| 36 | std::vector<Function*>().swap(FunctionsWithBodies); |
| 37 | DeferredFunctionInfo.clear(); |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // Helper functions to implement forward reference resolution, etc. |
| 42 | //===----------------------------------------------------------------------===// |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 43 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 44 | /// ConvertToString - Convert a string from a record into an std::string, return |
| 45 | /// true on failure. |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 46 | template<typename StrTy> |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 47 | static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx, |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 48 | StrTy &Result) { |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 49 | if (Idx > Record.size()) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 50 | return true; |
| 51 | |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 52 | for (unsigned i = Idx, e = Record.size(); i != e; ++i) |
| 53 | Result += (char)Record[i]; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | |
| 57 | static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { |
| 58 | switch (Val) { |
| 59 | default: // Map unknown/new linkages to external |
| 60 | case 0: return GlobalValue::ExternalLinkage; |
| 61 | case 1: return GlobalValue::WeakLinkage; |
| 62 | case 2: return GlobalValue::AppendingLinkage; |
| 63 | case 3: return GlobalValue::InternalLinkage; |
| 64 | case 4: return GlobalValue::LinkOnceLinkage; |
| 65 | case 5: return GlobalValue::DLLImportLinkage; |
| 66 | case 6: return GlobalValue::DLLExportLinkage; |
| 67 | case 7: return GlobalValue::ExternalWeakLinkage; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { |
| 72 | switch (Val) { |
| 73 | default: // Map unknown visibilities to default. |
| 74 | case 0: return GlobalValue::DefaultVisibility; |
| 75 | case 1: return GlobalValue::HiddenVisibility; |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 76 | case 2: return GlobalValue::ProtectedVisibility; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 80 | static int GetDecodedCastOpcode(unsigned Val) { |
| 81 | switch (Val) { |
| 82 | default: return -1; |
| 83 | case bitc::CAST_TRUNC : return Instruction::Trunc; |
| 84 | case bitc::CAST_ZEXT : return Instruction::ZExt; |
| 85 | case bitc::CAST_SEXT : return Instruction::SExt; |
| 86 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
| 87 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
| 88 | case bitc::CAST_UITOFP : return Instruction::UIToFP; |
| 89 | case bitc::CAST_SITOFP : return Instruction::SIToFP; |
| 90 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
| 91 | case bitc::CAST_FPEXT : return Instruction::FPExt; |
| 92 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
| 93 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
| 94 | case bitc::CAST_BITCAST : return Instruction::BitCast; |
| 95 | } |
| 96 | } |
| 97 | static int GetDecodedBinaryOpcode(unsigned Val, const Type *Ty) { |
| 98 | switch (Val) { |
| 99 | default: return -1; |
| 100 | case bitc::BINOP_ADD: return Instruction::Add; |
| 101 | case bitc::BINOP_SUB: return Instruction::Sub; |
| 102 | case bitc::BINOP_MUL: return Instruction::Mul; |
| 103 | case bitc::BINOP_UDIV: return Instruction::UDiv; |
| 104 | case bitc::BINOP_SDIV: |
| 105 | return Ty->isFPOrFPVector() ? Instruction::FDiv : Instruction::SDiv; |
| 106 | case bitc::BINOP_UREM: return Instruction::URem; |
| 107 | case bitc::BINOP_SREM: |
| 108 | return Ty->isFPOrFPVector() ? Instruction::FRem : Instruction::SRem; |
| 109 | case bitc::BINOP_SHL: return Instruction::Shl; |
| 110 | case bitc::BINOP_LSHR: return Instruction::LShr; |
| 111 | case bitc::BINOP_ASHR: return Instruction::AShr; |
| 112 | case bitc::BINOP_AND: return Instruction::And; |
| 113 | case bitc::BINOP_OR: return Instruction::Or; |
| 114 | case bitc::BINOP_XOR: return Instruction::Xor; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 119 | namespace { |
| 120 | /// @brief A class for maintaining the slot number definition |
| 121 | /// as a placeholder for the actual definition for forward constants defs. |
| 122 | class ConstantPlaceHolder : public ConstantExpr { |
| 123 | ConstantPlaceHolder(); // DO NOT IMPLEMENT |
| 124 | void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 125 | public: |
| 126 | Use Op; |
Dan Gohman | adf3eab | 2007-11-19 15:30:20 +0000 | [diff] [blame] | 127 | explicit ConstantPlaceHolder(const Type *Ty) |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 128 | : ConstantExpr(Ty, Instruction::UserOp1, &Op, 1), |
| 129 | Op(UndefValue::get(Type::Int32Ty), this) { |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 130 | } |
| 131 | }; |
| 132 | } |
| 133 | |
| 134 | Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, |
| 135 | const Type *Ty) { |
| 136 | if (Idx >= size()) { |
| 137 | // Insert a bunch of null values. |
| 138 | Uses.resize(Idx+1); |
| 139 | OperandList = &Uses[0]; |
| 140 | NumOperands = Idx+1; |
| 141 | } |
| 142 | |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 143 | if (Value *V = Uses[Idx]) { |
| 144 | assert(Ty == V->getType() && "Type mismatch in constant table!"); |
| 145 | return cast<Constant>(V); |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 147 | |
| 148 | // Create and return a placeholder, which will later be RAUW'd. |
| 149 | Constant *C = new ConstantPlaceHolder(Ty); |
| 150 | Uses[Idx].init(C, this); |
| 151 | return C; |
| 152 | } |
| 153 | |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 154 | Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) { |
| 155 | if (Idx >= size()) { |
| 156 | // Insert a bunch of null values. |
| 157 | Uses.resize(Idx+1); |
| 158 | OperandList = &Uses[0]; |
| 159 | NumOperands = Idx+1; |
| 160 | } |
| 161 | |
| 162 | if (Value *V = Uses[Idx]) { |
| 163 | assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!"); |
| 164 | return V; |
| 165 | } |
| 166 | |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 167 | // No type specified, must be invalid reference. |
| 168 | if (Ty == 0) return 0; |
| 169 | |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 170 | // Create and return a placeholder, which will later be RAUW'd. |
| 171 | Value *V = new Argument(Ty); |
| 172 | Uses[Idx].init(V, this); |
| 173 | return V; |
| 174 | } |
| 175 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 176 | |
| 177 | const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) { |
| 178 | // If the TypeID is in range, return it. |
| 179 | if (ID < TypeList.size()) |
| 180 | return TypeList[ID].get(); |
| 181 | if (!isTypeTable) return 0; |
| 182 | |
| 183 | // The type table allows forward references. Push as many Opaque types as |
| 184 | // needed to get up to ID. |
| 185 | while (TypeList.size() <= ID) |
| 186 | TypeList.push_back(OpaqueType::get()); |
| 187 | return TypeList.back().get(); |
| 188 | } |
| 189 | |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 190 | //===----------------------------------------------------------------------===// |
| 191 | // Functions for parsing blocks from the bitcode file |
| 192 | //===----------------------------------------------------------------------===// |
| 193 | |
| 194 | bool BitcodeReader::ParseParamAttrBlock() { |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 195 | if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 196 | return Error("Malformed block record"); |
| 197 | |
| 198 | if (!ParamAttrs.empty()) |
| 199 | return Error("Multiple PARAMATTR blocks found!"); |
| 200 | |
| 201 | SmallVector<uint64_t, 64> Record; |
| 202 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame^] | 203 | SmallVector<ParamAttrsWithIndex, 8> Attrs; |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 204 | |
| 205 | // Read all the records. |
| 206 | while (1) { |
| 207 | unsigned Code = Stream.ReadCode(); |
| 208 | if (Code == bitc::END_BLOCK) { |
| 209 | if (Stream.ReadBlockEnd()) |
| 210 | return Error("Error at end of PARAMATTR block"); |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 215 | // No known subblocks, always skip them. |
| 216 | Stream.ReadSubBlockID(); |
| 217 | if (Stream.SkipBlock()) |
| 218 | return Error("Malformed block record"); |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | if (Code == bitc::DEFINE_ABBREV) { |
| 223 | Stream.ReadAbbrevRecord(); |
| 224 | continue; |
| 225 | } |
| 226 | |
| 227 | // Read a record. |
| 228 | Record.clear(); |
| 229 | switch (Stream.ReadRecord(Code, Record)) { |
| 230 | default: // Default behavior: ignore. |
| 231 | break; |
| 232 | case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [paramidx0, attr0, ...] |
| 233 | if (Record.size() & 1) |
| 234 | return Error("Invalid ENTRY record"); |
| 235 | |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 236 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
Duncan Sands | 5e41f65 | 2007-11-20 14:09:29 +0000 | [diff] [blame] | 237 | if (Record[i+1] != ParamAttr::None) |
| 238 | Attrs.push_back(ParamAttrsWithIndex::get(Record[i], Record[i+1])); |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | 461edd9 | 2008-03-12 02:25:52 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame^] | 241 | ParamAttrs.push_back(PAListPtr::get(Attrs.begin(), Attrs.end())); |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 242 | Attrs.clear(); |
| 243 | break; |
| 244 | } |
Duncan Sands | 5e41f65 | 2007-11-20 14:09:29 +0000 | [diff] [blame] | 245 | } |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 250 | bool BitcodeReader::ParseTypeTable() { |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 251 | if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID)) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 252 | return Error("Malformed block record"); |
| 253 | |
| 254 | if (!TypeList.empty()) |
| 255 | return Error("Multiple TYPE_BLOCKs found!"); |
| 256 | |
| 257 | SmallVector<uint64_t, 64> Record; |
| 258 | unsigned NumRecords = 0; |
| 259 | |
| 260 | // Read all the records for this type table. |
| 261 | while (1) { |
| 262 | unsigned Code = Stream.ReadCode(); |
| 263 | if (Code == bitc::END_BLOCK) { |
| 264 | if (NumRecords != TypeList.size()) |
| 265 | return Error("Invalid type forward reference in TYPE_BLOCK"); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 266 | if (Stream.ReadBlockEnd()) |
| 267 | return Error("Error at end of type table block"); |
| 268 | return false; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 272 | // No known subblocks, always skip them. |
| 273 | Stream.ReadSubBlockID(); |
| 274 | if (Stream.SkipBlock()) |
| 275 | return Error("Malformed block record"); |
| 276 | continue; |
| 277 | } |
| 278 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 279 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 280 | Stream.ReadAbbrevRecord(); |
| 281 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Read a record. |
| 285 | Record.clear(); |
| 286 | const Type *ResultTy = 0; |
| 287 | switch (Stream.ReadRecord(Code, Record)) { |
| 288 | default: // Default behavior: unknown type. |
| 289 | ResultTy = 0; |
| 290 | break; |
| 291 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
| 292 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
| 293 | // type list. This allows us to reserve space. |
| 294 | if (Record.size() < 1) |
| 295 | return Error("Invalid TYPE_CODE_NUMENTRY record"); |
| 296 | TypeList.reserve(Record[0]); |
| 297 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 298 | case bitc::TYPE_CODE_VOID: // VOID |
| 299 | ResultTy = Type::VoidTy; |
| 300 | break; |
| 301 | case bitc::TYPE_CODE_FLOAT: // FLOAT |
| 302 | ResultTy = Type::FloatTy; |
| 303 | break; |
| 304 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
| 305 | ResultTy = Type::DoubleTy; |
| 306 | break; |
Dale Johannesen | 320fc8a | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 307 | case bitc::TYPE_CODE_X86_FP80: // X86_FP80 |
| 308 | ResultTy = Type::X86_FP80Ty; |
| 309 | break; |
| 310 | case bitc::TYPE_CODE_FP128: // FP128 |
| 311 | ResultTy = Type::FP128Ty; |
| 312 | break; |
| 313 | case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 |
| 314 | ResultTy = Type::PPC_FP128Ty; |
| 315 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 316 | case bitc::TYPE_CODE_LABEL: // LABEL |
| 317 | ResultTy = Type::LabelTy; |
| 318 | break; |
| 319 | case bitc::TYPE_CODE_OPAQUE: // OPAQUE |
| 320 | ResultTy = 0; |
| 321 | break; |
| 322 | case bitc::TYPE_CODE_INTEGER: // INTEGER: [width] |
| 323 | if (Record.size() < 1) |
| 324 | return Error("Invalid Integer type record"); |
| 325 | |
| 326 | ResultTy = IntegerType::get(Record[0]); |
| 327 | break; |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 328 | case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or |
| 329 | // [pointee type, address space] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 330 | if (Record.size() < 1) |
| 331 | return Error("Invalid POINTER type record"); |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 332 | unsigned AddressSpace = 0; |
| 333 | if (Record.size() == 2) |
| 334 | AddressSpace = Record[1]; |
| 335 | ResultTy = PointerType::get(getTypeByID(Record[0], true), AddressSpace); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 336 | break; |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 337 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 338 | case bitc::TYPE_CODE_FUNCTION: { |
Chris Lattner | a1afde7 | 2007-11-27 17:48:06 +0000 | [diff] [blame] | 339 | // FIXME: attrid is dead, remove it in LLVM 3.0 |
| 340 | // FUNCTION: [vararg, attrid, retty, paramty x N] |
| 341 | if (Record.size() < 3) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 342 | return Error("Invalid FUNCTION type record"); |
| 343 | std::vector<const Type*> ArgTys; |
Chris Lattner | a1afde7 | 2007-11-27 17:48:06 +0000 | [diff] [blame] | 344 | for (unsigned i = 3, e = Record.size(); i != e; ++i) |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 345 | ArgTys.push_back(getTypeByID(Record[i], true)); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 346 | |
Chris Lattner | a1afde7 | 2007-11-27 17:48:06 +0000 | [diff] [blame] | 347 | ResultTy = FunctionType::get(getTypeByID(Record[2], true), ArgTys, |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 348 | Record[0]); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 349 | break; |
| 350 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 351 | case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, eltty x N] |
Chris Lattner | 7108dce | 2007-05-06 08:21:50 +0000 | [diff] [blame] | 352 | if (Record.size() < 1) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 353 | return Error("Invalid STRUCT type record"); |
| 354 | std::vector<const Type*> EltTys; |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 355 | for (unsigned i = 1, e = Record.size(); i != e; ++i) |
| 356 | EltTys.push_back(getTypeByID(Record[i], true)); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 357 | ResultTy = StructType::get(EltTys, Record[0]); |
| 358 | break; |
| 359 | } |
| 360 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
| 361 | if (Record.size() < 2) |
| 362 | return Error("Invalid ARRAY type record"); |
| 363 | ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]); |
| 364 | break; |
| 365 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] |
| 366 | if (Record.size() < 2) |
| 367 | return Error("Invalid VECTOR type record"); |
| 368 | ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]); |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | if (NumRecords == TypeList.size()) { |
| 373 | // If this is a new type slot, just append it. |
| 374 | TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get()); |
| 375 | ++NumRecords; |
| 376 | } else if (ResultTy == 0) { |
| 377 | // Otherwise, this was forward referenced, so an opaque type was created, |
| 378 | // but the result type is actually just an opaque. Leave the one we |
| 379 | // created previously. |
| 380 | ++NumRecords; |
| 381 | } else { |
| 382 | // Otherwise, this was forward referenced, so an opaque type was created. |
| 383 | // Resolve the opaque type to the real type now. |
| 384 | assert(NumRecords < TypeList.size() && "Typelist imbalance"); |
| 385 | const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get()); |
| 386 | |
| 387 | // Don't directly push the new type on the Tab. Instead we want to replace |
| 388 | // the opaque type we previously inserted with the new concrete value. The |
| 389 | // refinement from the abstract (opaque) type to the new type causes all |
| 390 | // uses of the abstract type to use the concrete type (NewTy). This will |
| 391 | // also cause the opaque type to be deleted. |
| 392 | const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy); |
| 393 | |
| 394 | // This should have replaced the old opaque type with the new type in the |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 395 | // value table... or with a preexisting type that was already in the |
| 396 | // system. Let's just make sure it did. |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 397 | assert(TypeList[NumRecords-1].get() != OldTy && |
| 398 | "refineAbstractType didn't work!"); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 404 | bool BitcodeReader::ParseTypeSymbolTable() { |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 405 | if (Stream.EnterSubBlock(bitc::TYPE_SYMTAB_BLOCK_ID)) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 406 | return Error("Malformed block record"); |
| 407 | |
| 408 | SmallVector<uint64_t, 64> Record; |
| 409 | |
| 410 | // Read all the records for this type table. |
| 411 | std::string TypeName; |
| 412 | while (1) { |
| 413 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 414 | if (Code == bitc::END_BLOCK) { |
| 415 | if (Stream.ReadBlockEnd()) |
| 416 | return Error("Error at end of type symbol table block"); |
| 417 | return false; |
| 418 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 419 | |
| 420 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 421 | // No known subblocks, always skip them. |
| 422 | Stream.ReadSubBlockID(); |
| 423 | if (Stream.SkipBlock()) |
| 424 | return Error("Malformed block record"); |
| 425 | continue; |
| 426 | } |
| 427 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 428 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 429 | Stream.ReadAbbrevRecord(); |
| 430 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // Read a record. |
| 434 | Record.clear(); |
| 435 | switch (Stream.ReadRecord(Code, Record)) { |
| 436 | default: // Default behavior: unknown type. |
| 437 | break; |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 438 | case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namechar x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 439 | if (ConvertToString(Record, 1, TypeName)) |
| 440 | return Error("Invalid TST_ENTRY record"); |
| 441 | unsigned TypeID = Record[0]; |
| 442 | if (TypeID >= TypeList.size()) |
| 443 | return Error("Invalid Type ID in TST_ENTRY record"); |
| 444 | |
| 445 | TheModule->addTypeName(TypeName, TypeList[TypeID].get()); |
| 446 | TypeName.clear(); |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 452 | bool BitcodeReader::ParseValueSymbolTable() { |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 453 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 454 | return Error("Malformed block record"); |
| 455 | |
| 456 | SmallVector<uint64_t, 64> Record; |
| 457 | |
| 458 | // Read all the records for this value table. |
| 459 | SmallString<128> ValueName; |
| 460 | while (1) { |
| 461 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 462 | if (Code == bitc::END_BLOCK) { |
| 463 | if (Stream.ReadBlockEnd()) |
| 464 | return Error("Error at end of value symbol table block"); |
| 465 | return false; |
| 466 | } |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 467 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 468 | // No known subblocks, always skip them. |
| 469 | Stream.ReadSubBlockID(); |
| 470 | if (Stream.SkipBlock()) |
| 471 | return Error("Malformed block record"); |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | if (Code == bitc::DEFINE_ABBREV) { |
| 476 | Stream.ReadAbbrevRecord(); |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | // Read a record. |
| 481 | Record.clear(); |
| 482 | switch (Stream.ReadRecord(Code, Record)) { |
| 483 | default: // Default behavior: unknown type. |
| 484 | break; |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 485 | case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N] |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 486 | if (ConvertToString(Record, 1, ValueName)) |
| 487 | return Error("Invalid TST_ENTRY record"); |
| 488 | unsigned ValueID = Record[0]; |
| 489 | if (ValueID >= ValueList.size()) |
| 490 | return Error("Invalid Value ID in VST_ENTRY record"); |
| 491 | Value *V = ValueList[ValueID]; |
| 492 | |
| 493 | V->setName(&ValueName[0], ValueName.size()); |
| 494 | ValueName.clear(); |
| 495 | break; |
Reid Spencer | c8f8a24 | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 496 | } |
| 497 | case bitc::VST_CODE_BBENTRY: { |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 498 | if (ConvertToString(Record, 1, ValueName)) |
| 499 | return Error("Invalid VST_BBENTRY record"); |
| 500 | BasicBlock *BB = getBasicBlock(Record[0]); |
| 501 | if (BB == 0) |
| 502 | return Error("Invalid BB ID in VST_BBENTRY record"); |
| 503 | |
| 504 | BB->setName(&ValueName[0], ValueName.size()); |
| 505 | ValueName.clear(); |
| 506 | break; |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 507 | } |
Reid Spencer | c8f8a24 | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 508 | } |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 512 | /// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in |
| 513 | /// the LSB for dense VBR encoding. |
| 514 | static uint64_t DecodeSignRotatedValue(uint64_t V) { |
| 515 | if ((V & 1) == 0) |
| 516 | return V >> 1; |
| 517 | if (V != 1) |
| 518 | return -(V >> 1); |
| 519 | // There is no such thing as -0 with integers. "-0" really means MININT. |
| 520 | return 1ULL << 63; |
| 521 | } |
| 522 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 523 | /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global |
| 524 | /// values and aliases that we can. |
| 525 | bool BitcodeReader::ResolveGlobalAndAliasInits() { |
| 526 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; |
| 527 | std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; |
| 528 | |
| 529 | GlobalInitWorklist.swap(GlobalInits); |
| 530 | AliasInitWorklist.swap(AliasInits); |
| 531 | |
| 532 | while (!GlobalInitWorklist.empty()) { |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 533 | unsigned ValID = GlobalInitWorklist.back().second; |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 534 | if (ValID >= ValueList.size()) { |
| 535 | // Not ready to resolve this yet, it requires something later in the file. |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 536 | GlobalInits.push_back(GlobalInitWorklist.back()); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 537 | } else { |
| 538 | if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) |
| 539 | GlobalInitWorklist.back().first->setInitializer(C); |
| 540 | else |
| 541 | return Error("Global variable initializer is not a constant!"); |
| 542 | } |
| 543 | GlobalInitWorklist.pop_back(); |
| 544 | } |
| 545 | |
| 546 | while (!AliasInitWorklist.empty()) { |
| 547 | unsigned ValID = AliasInitWorklist.back().second; |
| 548 | if (ValID >= ValueList.size()) { |
| 549 | AliasInits.push_back(AliasInitWorklist.back()); |
| 550 | } else { |
| 551 | if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) |
Anton Korobeynikov | 7dde0ff | 2007-04-28 14:57:59 +0000 | [diff] [blame] | 552 | AliasInitWorklist.back().first->setAliasee(C); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 553 | else |
| 554 | return Error("Alias initializer is not a constant!"); |
| 555 | } |
| 556 | AliasInitWorklist.pop_back(); |
| 557 | } |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 562 | bool BitcodeReader::ParseConstants() { |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 563 | if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 564 | return Error("Malformed block record"); |
| 565 | |
| 566 | SmallVector<uint64_t, 64> Record; |
| 567 | |
| 568 | // Read all the records for this value table. |
| 569 | const Type *CurTy = Type::Int32Ty; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 570 | unsigned NextCstNo = ValueList.size(); |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 571 | while (1) { |
| 572 | unsigned Code = Stream.ReadCode(); |
| 573 | if (Code == bitc::END_BLOCK) { |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 574 | if (NextCstNo != ValueList.size()) |
| 575 | return Error("Invalid constant reference!"); |
| 576 | |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 577 | if (Stream.ReadBlockEnd()) |
| 578 | return Error("Error at end of constants block"); |
| 579 | return false; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 583 | // No known subblocks, always skip them. |
| 584 | Stream.ReadSubBlockID(); |
| 585 | if (Stream.SkipBlock()) |
| 586 | return Error("Malformed block record"); |
| 587 | continue; |
| 588 | } |
| 589 | |
| 590 | if (Code == bitc::DEFINE_ABBREV) { |
| 591 | Stream.ReadAbbrevRecord(); |
| 592 | continue; |
| 593 | } |
| 594 | |
| 595 | // Read a record. |
| 596 | Record.clear(); |
| 597 | Value *V = 0; |
| 598 | switch (Stream.ReadRecord(Code, Record)) { |
| 599 | default: // Default behavior: unknown constant |
| 600 | case bitc::CST_CODE_UNDEF: // UNDEF |
| 601 | V = UndefValue::get(CurTy); |
| 602 | break; |
| 603 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
| 604 | if (Record.empty()) |
| 605 | return Error("Malformed CST_SETTYPE record"); |
| 606 | if (Record[0] >= TypeList.size()) |
| 607 | return Error("Invalid Type ID in CST_SETTYPE record"); |
| 608 | CurTy = TypeList[Record[0]]; |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 609 | continue; // Skip the ValueList manipulation. |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 610 | case bitc::CST_CODE_NULL: // NULL |
| 611 | V = Constant::getNullValue(CurTy); |
| 612 | break; |
| 613 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 614 | if (!isa<IntegerType>(CurTy) || Record.empty()) |
| 615 | return Error("Invalid CST_INTEGER record"); |
| 616 | V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0])); |
| 617 | break; |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 618 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] |
| 619 | if (!isa<IntegerType>(CurTy) || Record.empty()) |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 620 | return Error("Invalid WIDE_INTEGER record"); |
| 621 | |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 622 | unsigned NumWords = Record.size(); |
Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 623 | SmallVector<uint64_t, 8> Words; |
| 624 | Words.resize(NumWords); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 625 | for (unsigned i = 0; i != NumWords; ++i) |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 626 | Words[i] = DecodeSignRotatedValue(Record[i]); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 627 | V = ConstantInt::get(APInt(cast<IntegerType>(CurTy)->getBitWidth(), |
Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 628 | NumWords, &Words[0])); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 631 | case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 632 | if (Record.empty()) |
| 633 | return Error("Invalid FLOAT record"); |
| 634 | if (CurTy == Type::FloatTy) |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 635 | V = ConstantFP::get(CurTy, APFloat(APInt(32, (uint32_t)Record[0]))); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 636 | else if (CurTy == Type::DoubleTy) |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 637 | V = ConstantFP::get(CurTy, APFloat(APInt(64, Record[0]))); |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 638 | else if (CurTy == Type::X86_FP80Ty) |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 639 | V = ConstantFP::get(CurTy, APFloat(APInt(80, 2, &Record[0]))); |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 640 | else if (CurTy == Type::FP128Ty) |
Dale Johannesen | a471c2e | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 641 | V = ConstantFP::get(CurTy, APFloat(APInt(128, 2, &Record[0]), true)); |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 642 | else if (CurTy == Type::PPC_FP128Ty) |
Dale Johannesen | a471c2e | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 643 | V = ConstantFP::get(CurTy, APFloat(APInt(128, 2, &Record[0]))); |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 644 | else |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 645 | V = UndefValue::get(CurTy); |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 646 | break; |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 647 | } |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 649 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] |
| 650 | if (Record.empty()) |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 651 | return Error("Invalid CST_AGGREGATE record"); |
| 652 | |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 653 | unsigned Size = Record.size(); |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 654 | std::vector<Constant*> Elts; |
| 655 | |
| 656 | if (const StructType *STy = dyn_cast<StructType>(CurTy)) { |
| 657 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 658 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 659 | STy->getElementType(i))); |
| 660 | V = ConstantStruct::get(STy, Elts); |
| 661 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { |
| 662 | const Type *EltTy = ATy->getElementType(); |
| 663 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 664 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 665 | V = ConstantArray::get(ATy, Elts); |
| 666 | } else if (const VectorType *VTy = dyn_cast<VectorType>(CurTy)) { |
| 667 | const Type *EltTy = VTy->getElementType(); |
| 668 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 669 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 670 | V = ConstantVector::get(Elts); |
| 671 | } else { |
| 672 | V = UndefValue::get(CurTy); |
| 673 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 674 | break; |
| 675 | } |
Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 676 | case bitc::CST_CODE_STRING: { // STRING: [values] |
| 677 | if (Record.empty()) |
| 678 | return Error("Invalid CST_AGGREGATE record"); |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 679 | |
Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 680 | const ArrayType *ATy = cast<ArrayType>(CurTy); |
| 681 | const Type *EltTy = ATy->getElementType(); |
| 682 | |
| 683 | unsigned Size = Record.size(); |
| 684 | std::vector<Constant*> Elts; |
Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 685 | for (unsigned i = 0; i != Size; ++i) |
| 686 | Elts.push_back(ConstantInt::get(EltTy, Record[i])); |
| 687 | V = ConstantArray::get(ATy, Elts); |
| 688 | break; |
| 689 | } |
Chris Lattner | cb3d91b | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 690 | case bitc::CST_CODE_CSTRING: { // CSTRING: [values] |
| 691 | if (Record.empty()) |
| 692 | return Error("Invalid CST_AGGREGATE record"); |
| 693 | |
| 694 | const ArrayType *ATy = cast<ArrayType>(CurTy); |
| 695 | const Type *EltTy = ATy->getElementType(); |
| 696 | |
| 697 | unsigned Size = Record.size(); |
| 698 | std::vector<Constant*> Elts; |
| 699 | for (unsigned i = 0; i != Size; ++i) |
| 700 | Elts.push_back(ConstantInt::get(EltTy, Record[i])); |
| 701 | Elts.push_back(Constant::getNullValue(EltTy)); |
| 702 | V = ConstantArray::get(ATy, Elts); |
| 703 | break; |
| 704 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 705 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
| 706 | if (Record.size() < 3) return Error("Invalid CE_BINOP record"); |
| 707 | int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 708 | if (Opc < 0) { |
| 709 | V = UndefValue::get(CurTy); // Unknown binop. |
| 710 | } else { |
| 711 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); |
| 712 | Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); |
| 713 | V = ConstantExpr::get(Opc, LHS, RHS); |
| 714 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 715 | break; |
| 716 | } |
| 717 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
| 718 | if (Record.size() < 3) return Error("Invalid CE_CAST record"); |
| 719 | int Opc = GetDecodedCastOpcode(Record[0]); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 720 | if (Opc < 0) { |
| 721 | V = UndefValue::get(CurTy); // Unknown cast. |
| 722 | } else { |
| 723 | const Type *OpTy = getTypeByID(Record[1]); |
Chris Lattner | bfcc380 | 2007-05-06 07:33:01 +0000 | [diff] [blame] | 724 | if (!OpTy) return Error("Invalid CE_CAST record"); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 725 | Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 726 | V = ConstantExpr::getCast(Opc, Op, CurTy); |
| 727 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 728 | break; |
| 729 | } |
| 730 | case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 731 | if (Record.size() & 1) return Error("Invalid CE_GEP record"); |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 732 | SmallVector<Constant*, 16> Elts; |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 733 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 734 | const Type *ElTy = getTypeByID(Record[i]); |
| 735 | if (!ElTy) return Error("Invalid CE_GEP record"); |
| 736 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); |
| 737 | } |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 738 | V = ConstantExpr::getGetElementPtr(Elts[0], &Elts[1], Elts.size()-1); |
| 739 | break; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 740 | } |
| 741 | case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#] |
| 742 | if (Record.size() < 3) return Error("Invalid CE_SELECT record"); |
| 743 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], |
| 744 | Type::Int1Ty), |
| 745 | ValueList.getConstantFwdRef(Record[1],CurTy), |
| 746 | ValueList.getConstantFwdRef(Record[2],CurTy)); |
| 747 | break; |
| 748 | case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval] |
| 749 | if (Record.size() < 3) return Error("Invalid CE_EXTRACTELT record"); |
| 750 | const VectorType *OpTy = |
| 751 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
| 752 | if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record"); |
| 753 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 754 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], |
| 755 | OpTy->getElementType()); |
| 756 | V = ConstantExpr::getExtractElement(Op0, Op1); |
| 757 | break; |
| 758 | } |
| 759 | case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval] |
| 760 | const VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
| 761 | if (Record.size() < 3 || OpTy == 0) |
| 762 | return Error("Invalid CE_INSERTELT record"); |
| 763 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 764 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], |
| 765 | OpTy->getElementType()); |
| 766 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::Int32Ty); |
| 767 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 768 | break; |
| 769 | } |
| 770 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
| 771 | const VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
| 772 | if (Record.size() < 3 || OpTy == 0) |
| 773 | return Error("Invalid CE_INSERTELT record"); |
| 774 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 775 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 776 | const Type *ShufTy=VectorType::get(Type::Int32Ty, OpTy->getNumElements()); |
| 777 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); |
| 778 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
| 779 | break; |
| 780 | } |
| 781 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
| 782 | if (Record.size() < 4) return Error("Invalid CE_CMP record"); |
| 783 | const Type *OpTy = getTypeByID(Record[0]); |
| 784 | if (OpTy == 0) return Error("Invalid CE_CMP record"); |
| 785 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 786 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 787 | |
| 788 | if (OpTy->isFloatingPoint()) |
| 789 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); |
| 790 | else |
| 791 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); |
| 792 | break; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 793 | } |
Chris Lattner | 2bce93a | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 794 | case bitc::CST_CODE_INLINEASM: { |
| 795 | if (Record.size() < 2) return Error("Invalid INLINEASM record"); |
| 796 | std::string AsmStr, ConstrStr; |
| 797 | bool HasSideEffects = Record[0]; |
| 798 | unsigned AsmStrSize = Record[1]; |
| 799 | if (2+AsmStrSize >= Record.size()) |
| 800 | return Error("Invalid INLINEASM record"); |
| 801 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 802 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
| 803 | return Error("Invalid INLINEASM record"); |
| 804 | |
| 805 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 806 | AsmStr += (char)Record[2+i]; |
| 807 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 808 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
| 809 | const PointerType *PTy = cast<PointerType>(CurTy); |
| 810 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
| 811 | AsmStr, ConstrStr, HasSideEffects); |
| 812 | break; |
| 813 | } |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 816 | ValueList.AssignValue(V, NextCstNo); |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 817 | ++NextCstNo; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 821 | /// RememberAndSkipFunctionBody - When we see the block for a function body, |
| 822 | /// remember where it is and then skip it. This lets us lazily deserialize the |
| 823 | /// functions. |
| 824 | bool BitcodeReader::RememberAndSkipFunctionBody() { |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 825 | // Get the function we are talking about. |
| 826 | if (FunctionsWithBodies.empty()) |
| 827 | return Error("Insufficient function protos"); |
| 828 | |
| 829 | Function *Fn = FunctionsWithBodies.back(); |
| 830 | FunctionsWithBodies.pop_back(); |
| 831 | |
| 832 | // Save the current stream state. |
| 833 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
| 834 | DeferredFunctionInfo[Fn] = std::make_pair(CurBit, Fn->getLinkage()); |
| 835 | |
| 836 | // Set the functions linkage to GhostLinkage so we know it is lazily |
| 837 | // deserialized. |
| 838 | Fn->setLinkage(GlobalValue::GhostLinkage); |
| 839 | |
| 840 | // Skip over the function block for now. |
| 841 | if (Stream.SkipBlock()) |
| 842 | return Error("Malformed block record"); |
| 843 | return false; |
| 844 | } |
| 845 | |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 846 | bool BitcodeReader::ParseModule(const std::string &ModuleID) { |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 847 | // Reject multiple MODULE_BLOCK's in a single bitstream. |
| 848 | if (TheModule) |
| 849 | return Error("Multiple MODULE_BLOCKs in same stream"); |
| 850 | |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 851 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 852 | return Error("Malformed block record"); |
| 853 | |
| 854 | // Otherwise, create the module. |
| 855 | TheModule = new Module(ModuleID); |
| 856 | |
| 857 | SmallVector<uint64_t, 64> Record; |
| 858 | std::vector<std::string> SectionTable; |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 859 | std::vector<std::string> CollectorTable; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 860 | |
| 861 | // Read all the records for this module. |
| 862 | while (!Stream.AtEndOfStream()) { |
| 863 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 864 | if (Code == bitc::END_BLOCK) { |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 865 | if (Stream.ReadBlockEnd()) |
| 866 | return Error("Error at end of module block"); |
| 867 | |
| 868 | // Patch the initializers for globals and aliases up. |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 869 | ResolveGlobalAndAliasInits(); |
| 870 | if (!GlobalInits.empty() || !AliasInits.empty()) |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 871 | return Error("Malformed global initializer set"); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 872 | if (!FunctionsWithBodies.empty()) |
| 873 | return Error("Too few function bodies found"); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 874 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 875 | // Look for intrinsic functions which need to be upgraded at some point |
| 876 | for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 877 | FI != FE; ++FI) { |
Evan Cheng | f9b83fc | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 878 | Function* NewFn; |
| 879 | if (UpgradeIntrinsicFunction(FI, NewFn)) |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 880 | UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn)); |
| 881 | } |
| 882 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 883 | // Force deallocation of memory for these vectors to favor the client that |
| 884 | // want lazy deserialization. |
| 885 | std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); |
| 886 | std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); |
| 887 | std::vector<Function*>().swap(FunctionsWithBodies); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 888 | return false; |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 889 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 890 | |
| 891 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 892 | switch (Stream.ReadSubBlockID()) { |
| 893 | default: // Skip unknown content. |
| 894 | if (Stream.SkipBlock()) |
| 895 | return Error("Malformed block record"); |
| 896 | break; |
Chris Lattner | 3f79980 | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 897 | case bitc::BLOCKINFO_BLOCK_ID: |
| 898 | if (Stream.ReadBlockInfoBlock()) |
| 899 | return Error("Malformed BlockInfoBlock"); |
| 900 | break; |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 901 | case bitc::PARAMATTR_BLOCK_ID: |
| 902 | if (ParseParamAttrBlock()) |
| 903 | return true; |
| 904 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 905 | case bitc::TYPE_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 906 | if (ParseTypeTable()) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 907 | return true; |
| 908 | break; |
| 909 | case bitc::TYPE_SYMTAB_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 910 | if (ParseTypeSymbolTable()) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 911 | return true; |
| 912 | break; |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 913 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 914 | if (ParseValueSymbolTable()) |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 915 | return true; |
| 916 | break; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 917 | case bitc::CONSTANTS_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 918 | if (ParseConstants() || ResolveGlobalAndAliasInits()) |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 919 | return true; |
| 920 | break; |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 921 | case bitc::FUNCTION_BLOCK_ID: |
| 922 | // If this is the first function body we've seen, reverse the |
| 923 | // FunctionsWithBodies list. |
| 924 | if (!HasReversedFunctionsWithBodies) { |
| 925 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); |
| 926 | HasReversedFunctionsWithBodies = true; |
| 927 | } |
| 928 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 929 | if (RememberAndSkipFunctionBody()) |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 930 | return true; |
| 931 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 932 | } |
| 933 | continue; |
| 934 | } |
| 935 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 936 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 937 | Stream.ReadAbbrevRecord(); |
| 938 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | // Read a record. |
| 942 | switch (Stream.ReadRecord(Code, Record)) { |
| 943 | default: break; // Default behavior, ignore unknown content. |
| 944 | case bitc::MODULE_CODE_VERSION: // VERSION: [version#] |
| 945 | if (Record.size() < 1) |
| 946 | return Error("Malformed MODULE_CODE_VERSION"); |
| 947 | // Only version #0 is supported so far. |
| 948 | if (Record[0] != 0) |
| 949 | return Error("Unknown bitstream version!"); |
| 950 | break; |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 951 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 952 | std::string S; |
| 953 | if (ConvertToString(Record, 0, S)) |
| 954 | return Error("Invalid MODULE_CODE_TRIPLE record"); |
| 955 | TheModule->setTargetTriple(S); |
| 956 | break; |
| 957 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 958 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 959 | std::string S; |
| 960 | if (ConvertToString(Record, 0, S)) |
| 961 | return Error("Invalid MODULE_CODE_DATALAYOUT record"); |
| 962 | TheModule->setDataLayout(S); |
| 963 | break; |
| 964 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 965 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 966 | std::string S; |
| 967 | if (ConvertToString(Record, 0, S)) |
| 968 | return Error("Invalid MODULE_CODE_ASM record"); |
| 969 | TheModule->setModuleInlineAsm(S); |
| 970 | break; |
| 971 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 972 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 973 | std::string S; |
| 974 | if (ConvertToString(Record, 0, S)) |
| 975 | return Error("Invalid MODULE_CODE_DEPLIB record"); |
| 976 | TheModule->addLibrary(S); |
| 977 | break; |
| 978 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 979 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 980 | std::string S; |
| 981 | if (ConvertToString(Record, 0, S)) |
| 982 | return Error("Invalid MODULE_CODE_SECTIONNAME record"); |
| 983 | SectionTable.push_back(S); |
| 984 | break; |
| 985 | } |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 986 | case bitc::MODULE_CODE_COLLECTORNAME: { // SECTIONNAME: [strchr x N] |
| 987 | std::string S; |
| 988 | if (ConvertToString(Record, 0, S)) |
| 989 | return Error("Invalid MODULE_CODE_COLLECTORNAME record"); |
| 990 | CollectorTable.push_back(S); |
| 991 | break; |
| 992 | } |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 993 | // GLOBALVAR: [pointer type, isconst, initid, |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 994 | // linkage, alignment, section, visibility, threadlocal] |
| 995 | case bitc::MODULE_CODE_GLOBALVAR: { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 996 | if (Record.size() < 6) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 997 | return Error("Invalid MODULE_CODE_GLOBALVAR record"); |
| 998 | const Type *Ty = getTypeByID(Record[0]); |
| 999 | if (!isa<PointerType>(Ty)) |
| 1000 | return Error("Global not a pointer type!"); |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1001 | unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1002 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 1003 | |
| 1004 | bool isConstant = Record[1]; |
| 1005 | GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]); |
| 1006 | unsigned Alignment = (1 << Record[4]) >> 1; |
| 1007 | std::string Section; |
| 1008 | if (Record[5]) { |
| 1009 | if (Record[5]-1 >= SectionTable.size()) |
| 1010 | return Error("Invalid section ID"); |
| 1011 | Section = SectionTable[Record[5]-1]; |
| 1012 | } |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 1013 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
Chris Lattner | 5f32c01 | 2007-05-06 19:27:46 +0000 | [diff] [blame] | 1014 | if (Record.size() > 6) |
| 1015 | Visibility = GetDecodedVisibility(Record[6]); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 1016 | bool isThreadLocal = false; |
Chris Lattner | 5f32c01 | 2007-05-06 19:27:46 +0000 | [diff] [blame] | 1017 | if (Record.size() > 7) |
| 1018 | isThreadLocal = Record[7]; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1019 | |
| 1020 | GlobalVariable *NewGV = |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1021 | new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule, |
| 1022 | isThreadLocal, AddressSpace); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1023 | NewGV->setAlignment(Alignment); |
| 1024 | if (!Section.empty()) |
| 1025 | NewGV->setSection(Section); |
| 1026 | NewGV->setVisibility(Visibility); |
| 1027 | NewGV->setThreadLocal(isThreadLocal); |
| 1028 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1029 | ValueList.push_back(NewGV); |
| 1030 | |
Chris Lattner | 6dbfd7b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 1031 | // Remember which value to use for the global initializer. |
| 1032 | if (unsigned InitID = Record[2]) |
| 1033 | GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1034 | break; |
| 1035 | } |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1036 | // FUNCTION: [type, callingconv, isproto, linkage, paramattr, |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1037 | // alignment, section, visibility, collector] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1038 | case bitc::MODULE_CODE_FUNCTION: { |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1039 | if (Record.size() < 8) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1040 | return Error("Invalid MODULE_CODE_FUNCTION record"); |
| 1041 | const Type *Ty = getTypeByID(Record[0]); |
| 1042 | if (!isa<PointerType>(Ty)) |
| 1043 | return Error("Function not a pointer type!"); |
| 1044 | const FunctionType *FTy = |
| 1045 | dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); |
| 1046 | if (!FTy) |
| 1047 | return Error("Function not a pointer to function type!"); |
| 1048 | |
| 1049 | Function *Func = new Function(FTy, GlobalValue::ExternalLinkage, |
| 1050 | "", TheModule); |
| 1051 | |
| 1052 | Func->setCallingConv(Record[1]); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1053 | bool isProto = Record[2]; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1054 | Func->setLinkage(GetDecodedLinkage(Record[3])); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame^] | 1055 | Func->setParamAttrs(getParamAttrs(Record[4])); |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1056 | |
| 1057 | Func->setAlignment((1 << Record[5]) >> 1); |
| 1058 | if (Record[6]) { |
| 1059 | if (Record[6]-1 >= SectionTable.size()) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1060 | return Error("Invalid section ID"); |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1061 | Func->setSection(SectionTable[Record[6]-1]); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1062 | } |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1063 | Func->setVisibility(GetDecodedVisibility(Record[7])); |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1064 | if (Record.size() > 8 && Record[8]) { |
| 1065 | if (Record[8]-1 > CollectorTable.size()) |
| 1066 | return Error("Invalid collector ID"); |
| 1067 | Func->setCollector(CollectorTable[Record[8]-1].c_str()); |
| 1068 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1070 | ValueList.push_back(Func); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1071 | |
| 1072 | // If this is a function with a body, remember the prototype we are |
| 1073 | // creating now, so that we can match up the body with them later. |
| 1074 | if (!isProto) |
| 1075 | FunctionsWithBodies.push_back(Func); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1076 | break; |
| 1077 | } |
Anton Korobeynikov | 91342d8 | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 1078 | // ALIAS: [alias type, aliasee val#, linkage] |
Anton Korobeynikov | f8342b9 | 2008-03-11 21:40:17 +0000 | [diff] [blame] | 1079 | // ALIAS: [alias type, aliasee val#, linkage, visibility] |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1080 | case bitc::MODULE_CODE_ALIAS: { |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1081 | if (Record.size() < 3) |
| 1082 | return Error("Invalid MODULE_ALIAS record"); |
| 1083 | const Type *Ty = getTypeByID(Record[0]); |
| 1084 | if (!isa<PointerType>(Ty)) |
| 1085 | return Error("Function not a pointer type!"); |
| 1086 | |
| 1087 | GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]), |
| 1088 | "", 0, TheModule); |
Anton Korobeynikov | 91342d8 | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 1089 | // Old bitcode files didn't have visibility field. |
| 1090 | if (Record.size() > 3) |
| 1091 | NewGA->setVisibility(GetDecodedVisibility(Record[3])); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1092 | ValueList.push_back(NewGA); |
| 1093 | AliasInits.push_back(std::make_pair(NewGA, Record[1])); |
| 1094 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1095 | } |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1096 | /// MODULE_CODE_PURGEVALS: [numvals] |
| 1097 | case bitc::MODULE_CODE_PURGEVALS: |
| 1098 | // Trim down the value list to the specified size. |
| 1099 | if (Record.size() < 1 || Record[0] > ValueList.size()) |
| 1100 | return Error("Invalid MODULE_PURGEVALS record"); |
| 1101 | ValueList.shrinkTo(Record[0]); |
| 1102 | break; |
| 1103 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1104 | Record.clear(); |
| 1105 | } |
| 1106 | |
| 1107 | return Error("Premature end of bitstream"); |
| 1108 | } |
| 1109 | |
| 1110 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1111 | bool BitcodeReader::ParseBitcode() { |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1112 | TheModule = 0; |
| 1113 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1114 | if (Buffer->getBufferSize() & 3) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1115 | return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
| 1116 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1117 | unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1118 | Stream.init(BufPtr, BufPtr+Buffer->getBufferSize()); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1119 | |
| 1120 | // Sniff for the signature. |
| 1121 | if (Stream.Read(8) != 'B' || |
| 1122 | Stream.Read(8) != 'C' || |
| 1123 | Stream.Read(4) != 0x0 || |
| 1124 | Stream.Read(4) != 0xC || |
| 1125 | Stream.Read(4) != 0xE || |
| 1126 | Stream.Read(4) != 0xD) |
| 1127 | return Error("Invalid bitcode signature"); |
| 1128 | |
| 1129 | // We expect a number of well-defined blocks, though we don't necessarily |
| 1130 | // need to understand them all. |
| 1131 | while (!Stream.AtEndOfStream()) { |
| 1132 | unsigned Code = Stream.ReadCode(); |
| 1133 | |
| 1134 | if (Code != bitc::ENTER_SUBBLOCK) |
| 1135 | return Error("Invalid record at top-level"); |
| 1136 | |
| 1137 | unsigned BlockID = Stream.ReadSubBlockID(); |
| 1138 | |
| 1139 | // We only know the MODULE subblock ID. |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1140 | switch (BlockID) { |
| 1141 | case bitc::BLOCKINFO_BLOCK_ID: |
| 1142 | if (Stream.ReadBlockInfoBlock()) |
| 1143 | return Error("Malformed BlockInfoBlock"); |
| 1144 | break; |
| 1145 | case bitc::MODULE_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 1146 | if (ParseModule(Buffer->getBufferIdentifier())) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1147 | return true; |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1148 | break; |
| 1149 | default: |
| 1150 | if (Stream.SkipBlock()) |
| 1151 | return Error("Malformed block record"); |
| 1152 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | return false; |
| 1157 | } |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1158 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1160 | /// ParseFunctionBody - Lazily parse the specified function body block. |
| 1161 | bool BitcodeReader::ParseFunctionBody(Function *F) { |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1162 | if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1163 | return Error("Malformed block record"); |
| 1164 | |
| 1165 | unsigned ModuleValueListSize = ValueList.size(); |
| 1166 | |
| 1167 | // Add all the function arguments to the value table. |
| 1168 | for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
| 1169 | ValueList.push_back(I); |
| 1170 | |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1171 | unsigned NextValueNo = ValueList.size(); |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1172 | BasicBlock *CurBB = 0; |
| 1173 | unsigned CurBBNo = 0; |
| 1174 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1175 | // Read all the records. |
| 1176 | SmallVector<uint64_t, 64> Record; |
| 1177 | while (1) { |
| 1178 | unsigned Code = Stream.ReadCode(); |
| 1179 | if (Code == bitc::END_BLOCK) { |
| 1180 | if (Stream.ReadBlockEnd()) |
| 1181 | return Error("Error at end of function block"); |
| 1182 | break; |
| 1183 | } |
| 1184 | |
| 1185 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 1186 | switch (Stream.ReadSubBlockID()) { |
| 1187 | default: // Skip unknown content. |
| 1188 | if (Stream.SkipBlock()) |
| 1189 | return Error("Malformed block record"); |
| 1190 | break; |
| 1191 | case bitc::CONSTANTS_BLOCK_ID: |
| 1192 | if (ParseConstants()) return true; |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1193 | NextValueNo = ValueList.size(); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1194 | break; |
| 1195 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
| 1196 | if (ParseValueSymbolTable()) return true; |
| 1197 | break; |
| 1198 | } |
| 1199 | continue; |
| 1200 | } |
| 1201 | |
| 1202 | if (Code == bitc::DEFINE_ABBREV) { |
| 1203 | Stream.ReadAbbrevRecord(); |
| 1204 | continue; |
| 1205 | } |
| 1206 | |
| 1207 | // Read a record. |
| 1208 | Record.clear(); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1209 | Instruction *I = 0; |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1210 | switch (Stream.ReadRecord(Code, Record)) { |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1211 | default: // Default behavior: reject |
| 1212 | return Error("Unknown instruction"); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1213 | case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks] |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1214 | if (Record.size() < 1 || Record[0] == 0) |
| 1215 | return Error("Invalid DECLAREBLOCKS record"); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1216 | // Create all the basic blocks for the function. |
Chris Lattner | f61e645 | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 1217 | FunctionBBs.resize(Record[0]); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1218 | for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) |
| 1219 | FunctionBBs[i] = new BasicBlock("", F); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1220 | CurBB = FunctionBBs[0]; |
| 1221 | continue; |
| 1222 | |
Nick Lewycky | fc82fab | 2008-03-02 02:48:09 +0000 | [diff] [blame] | 1223 | case bitc::FUNC_CODE_INST_BB_UNWINDDEST: // BB_UNWINDDEST: [bb#] |
| 1224 | if (CurBB->getUnwindDest()) |
| 1225 | return Error("Only permit one BB_UNWINDDEST per BB"); |
| 1226 | if (Record.size() != 1) |
| 1227 | return Error("Invalid BB_UNWINDDEST record"); |
| 1228 | |
| 1229 | CurBB->setUnwindDest(getBasicBlock(Record[0])); |
| 1230 | continue; |
| 1231 | |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1232 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] |
| 1233 | unsigned OpNum = 0; |
| 1234 | Value *LHS, *RHS; |
| 1235 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
| 1236 | getValue(Record, OpNum, LHS->getType(), RHS) || |
| 1237 | OpNum+1 != Record.size()) |
| 1238 | return Error("Invalid BINOP record"); |
| 1239 | |
| 1240 | int Opc = GetDecodedBinaryOpcode(Record[OpNum], LHS->getType()); |
| 1241 | if (Opc == -1) return Error("Invalid BINOP record"); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1242 | I = BinaryOperator::create((Instruction::BinaryOps)Opc, LHS, RHS); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1243 | break; |
| 1244 | } |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1245 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] |
| 1246 | unsigned OpNum = 0; |
| 1247 | Value *Op; |
| 1248 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 1249 | OpNum+2 != Record.size()) |
| 1250 | return Error("Invalid CAST record"); |
| 1251 | |
| 1252 | const Type *ResTy = getTypeByID(Record[OpNum]); |
| 1253 | int Opc = GetDecodedCastOpcode(Record[OpNum+1]); |
| 1254 | if (Opc == -1 || ResTy == 0) |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1255 | return Error("Invalid CAST record"); |
| 1256 | I = CastInst::create((Instruction::CastOps)Opc, Op, ResTy); |
| 1257 | break; |
| 1258 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1259 | case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1260 | unsigned OpNum = 0; |
| 1261 | Value *BasePtr; |
| 1262 | if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1263 | return Error("Invalid GEP record"); |
| 1264 | |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1265 | SmallVector<Value*, 16> GEPIdx; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1266 | while (OpNum != Record.size()) { |
| 1267 | Value *Op; |
| 1268 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1269 | return Error("Invalid GEP record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1270 | GEPIdx.push_back(Op); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1273 | I = new GetElementPtrInst(BasePtr, GEPIdx.begin(), GEPIdx.end()); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | } |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1276 | |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1277 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] |
| 1278 | unsigned OpNum = 0; |
| 1279 | Value *TrueVal, *FalseVal, *Cond; |
| 1280 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
| 1281 | getValue(Record, OpNum, TrueVal->getType(), FalseVal) || |
| 1282 | getValue(Record, OpNum, Type::Int1Ty, Cond)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1283 | return Error("Invalid SELECT record"); |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1284 | |
| 1285 | I = new SelectInst(Cond, TrueVal, FalseVal); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1286 | break; |
| 1287 | } |
| 1288 | |
| 1289 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1290 | unsigned OpNum = 0; |
| 1291 | Value *Vec, *Idx; |
| 1292 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || |
| 1293 | getValue(Record, OpNum, Type::Int32Ty, Idx)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1294 | return Error("Invalid EXTRACTELT record"); |
| 1295 | I = new ExtractElementInst(Vec, Idx); |
| 1296 | break; |
| 1297 | } |
| 1298 | |
| 1299 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1300 | unsigned OpNum = 0; |
| 1301 | Value *Vec, *Elt, *Idx; |
| 1302 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || |
| 1303 | getValue(Record, OpNum, |
| 1304 | cast<VectorType>(Vec->getType())->getElementType(), Elt) || |
| 1305 | getValue(Record, OpNum, Type::Int32Ty, Idx)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1306 | return Error("Invalid INSERTELT record"); |
| 1307 | I = new InsertElementInst(Vec, Elt, Idx); |
| 1308 | break; |
| 1309 | } |
| 1310 | |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1311 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] |
| 1312 | unsigned OpNum = 0; |
| 1313 | Value *Vec1, *Vec2, *Mask; |
| 1314 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || |
| 1315 | getValue(Record, OpNum, Vec1->getType(), Vec2)) |
| 1316 | return Error("Invalid SHUFFLEVEC record"); |
| 1317 | |
| 1318 | const Type *MaskTy = |
| 1319 | VectorType::get(Type::Int32Ty, |
| 1320 | cast<VectorType>(Vec1->getType())->getNumElements()); |
| 1321 | |
| 1322 | if (getValue(Record, OpNum, MaskTy, Mask)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1323 | return Error("Invalid SHUFFLEVEC record"); |
| 1324 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); |
| 1325 | break; |
| 1326 | } |
| 1327 | |
| 1328 | case bitc::FUNC_CODE_INST_CMP: { // CMP: [opty, opval, opval, pred] |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1329 | unsigned OpNum = 0; |
| 1330 | Value *LHS, *RHS; |
| 1331 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
| 1332 | getValue(Record, OpNum, LHS->getType(), RHS) || |
| 1333 | OpNum+1 != Record.size()) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1334 | return Error("Invalid CMP record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1335 | |
| 1336 | if (LHS->getType()->isFPOrFPVector()) |
| 1337 | I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1338 | else |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1339 | I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1340 | break; |
| 1341 | } |
Devang Patel | 197be3d | 2008-02-22 02:49:49 +0000 | [diff] [blame] | 1342 | case bitc::FUNC_CODE_INST_GETRESULT: { // GETRESULT: [ty, val, n] |
| 1343 | if (Record.size() != 2) |
| 1344 | return Error("Invalid GETRESULT record"); |
| 1345 | unsigned OpNum = 0; |
| 1346 | Value *Op; |
| 1347 | getValueTypePair(Record, OpNum, NextValueNo, Op); |
| 1348 | unsigned Index = Record[1]; |
| 1349 | I = new GetResultInst(Op, Index); |
| 1350 | break; |
| 1351 | } |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1352 | |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1353 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] |
Devang Patel | d9d99ff | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 1354 | { |
| 1355 | unsigned Size = Record.size(); |
| 1356 | if (Size == 0) { |
| 1357 | I = new ReturnInst(); |
| 1358 | break; |
| 1359 | } else { |
| 1360 | unsigned OpNum = 0; |
Devang Patel | f4511cd | 2008-02-26 19:38:17 +0000 | [diff] [blame] | 1361 | SmallVector<Value *,4> Vs; |
Devang Patel | d9d99ff | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 1362 | do { |
| 1363 | Value *Op = NULL; |
| 1364 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
| 1365 | return Error("Invalid RET record"); |
| 1366 | Vs.push_back(Op); |
| 1367 | } while(OpNum != Record.size()); |
| 1368 | |
Devang Patel | f4511cd | 2008-02-26 19:38:17 +0000 | [diff] [blame] | 1369 | // SmallVector Vs has at least one element. |
| 1370 | I = new ReturnInst(&Vs[0], Vs.size()); |
Devang Patel | d9d99ff | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 1371 | break; |
| 1372 | } |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1373 | } |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1374 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] |
Chris Lattner | f61e645 | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 1375 | if (Record.size() != 1 && Record.size() != 3) |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1376 | return Error("Invalid BR record"); |
| 1377 | BasicBlock *TrueDest = getBasicBlock(Record[0]); |
| 1378 | if (TrueDest == 0) |
| 1379 | return Error("Invalid BR record"); |
| 1380 | |
| 1381 | if (Record.size() == 1) |
| 1382 | I = new BranchInst(TrueDest); |
| 1383 | else { |
| 1384 | BasicBlock *FalseDest = getBasicBlock(Record[1]); |
| 1385 | Value *Cond = getFnValueByID(Record[2], Type::Int1Ty); |
| 1386 | if (FalseDest == 0 || Cond == 0) |
| 1387 | return Error("Invalid BR record"); |
| 1388 | I = new BranchInst(TrueDest, FalseDest, Cond); |
| 1389 | } |
| 1390 | break; |
| 1391 | } |
| 1392 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, opval, n, n x ops] |
| 1393 | if (Record.size() < 3 || (Record.size() & 1) == 0) |
| 1394 | return Error("Invalid SWITCH record"); |
| 1395 | const Type *OpTy = getTypeByID(Record[0]); |
| 1396 | Value *Cond = getFnValueByID(Record[1], OpTy); |
| 1397 | BasicBlock *Default = getBasicBlock(Record[2]); |
| 1398 | if (OpTy == 0 || Cond == 0 || Default == 0) |
| 1399 | return Error("Invalid SWITCH record"); |
| 1400 | unsigned NumCases = (Record.size()-3)/2; |
| 1401 | SwitchInst *SI = new SwitchInst(Cond, Default, NumCases); |
| 1402 | for (unsigned i = 0, e = NumCases; i != e; ++i) { |
| 1403 | ConstantInt *CaseVal = |
| 1404 | dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); |
| 1405 | BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); |
| 1406 | if (CaseVal == 0 || DestBB == 0) { |
| 1407 | delete SI; |
| 1408 | return Error("Invalid SWITCH record!"); |
| 1409 | } |
| 1410 | SI->addCase(CaseVal, DestBB); |
| 1411 | } |
| 1412 | I = SI; |
| 1413 | break; |
| 1414 | } |
| 1415 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1416 | case bitc::FUNC_CODE_INST_INVOKE: { |
| 1417 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1418 | if (Record.size() < 4) return Error("Invalid INVOKE record"); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame^] | 1419 | PAListPtr PAL = getParamAttrs(Record[0]); |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1420 | unsigned CCInfo = Record[1]; |
| 1421 | BasicBlock *NormalBB = getBasicBlock(Record[2]); |
| 1422 | BasicBlock *UnwindBB = getBasicBlock(Record[3]); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1424 | unsigned OpNum = 4; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1425 | Value *Callee; |
| 1426 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1427 | return Error("Invalid INVOKE record"); |
| 1428 | |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1429 | const PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); |
| 1430 | const FunctionType *FTy = !CalleeTy ? 0 : |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1431 | dyn_cast<FunctionType>(CalleeTy->getElementType()); |
| 1432 | |
| 1433 | // Check that the right number of fixed parameters are here. |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1434 | if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 || |
| 1435 | Record.size() < OpNum+FTy->getNumParams()) |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1436 | return Error("Invalid INVOKE record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1437 | |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1438 | SmallVector<Value*, 16> Ops; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1439 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
| 1440 | Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); |
| 1441 | if (Ops.back() == 0) return Error("Invalid INVOKE record"); |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1444 | if (!FTy->isVarArg()) { |
| 1445 | if (Record.size() != OpNum) |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1446 | return Error("Invalid INVOKE record"); |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1447 | } else { |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1448 | // Read type/value pairs for varargs params. |
| 1449 | while (OpNum != Record.size()) { |
| 1450 | Value *Op; |
| 1451 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
| 1452 | return Error("Invalid INVOKE record"); |
| 1453 | Ops.push_back(Op); |
| 1454 | } |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
David Greene | f1355a5 | 2007-08-27 19:04:21 +0000 | [diff] [blame] | 1457 | I = new InvokeInst(Callee, NormalBB, UnwindBB, Ops.begin(), Ops.end()); |
Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1458 | cast<InvokeInst>(I)->setCallingConv(CCInfo); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1459 | cast<InvokeInst>(I)->setParamAttrs(PAL); |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1460 | break; |
| 1461 | } |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1462 | case bitc::FUNC_CODE_INST_UNWIND: // UNWIND |
| 1463 | I = new UnwindInst(); |
| 1464 | break; |
| 1465 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE |
| 1466 | I = new UnreachableInst(); |
| 1467 | break; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1468 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1469 | if (Record.size() < 1 || ((Record.size()-1)&1)) |
Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1470 | return Error("Invalid PHI record"); |
| 1471 | const Type *Ty = getTypeByID(Record[0]); |
| 1472 | if (!Ty) return Error("Invalid PHI record"); |
| 1473 | |
| 1474 | PHINode *PN = new PHINode(Ty); |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1475 | PN->reserveOperandSpace(Record.size()-1); |
Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1476 | |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1477 | for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { |
| 1478 | Value *V = getFnValueByID(Record[1+i], Ty); |
| 1479 | BasicBlock *BB = getBasicBlock(Record[2+i]); |
Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1480 | if (!V || !BB) return Error("Invalid PHI record"); |
| 1481 | PN->addIncoming(V, BB); |
| 1482 | } |
| 1483 | I = PN; |
| 1484 | break; |
| 1485 | } |
| 1486 | |
| 1487 | case bitc::FUNC_CODE_INST_MALLOC: { // MALLOC: [instty, op, align] |
| 1488 | if (Record.size() < 3) |
| 1489 | return Error("Invalid MALLOC record"); |
| 1490 | const PointerType *Ty = |
| 1491 | dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); |
| 1492 | Value *Size = getFnValueByID(Record[1], Type::Int32Ty); |
| 1493 | unsigned Align = Record[2]; |
| 1494 | if (!Ty || !Size) return Error("Invalid MALLOC record"); |
| 1495 | I = new MallocInst(Ty->getElementType(), Size, (1 << Align) >> 1); |
| 1496 | break; |
| 1497 | } |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1498 | case bitc::FUNC_CODE_INST_FREE: { // FREE: [op, opty] |
| 1499 | unsigned OpNum = 0; |
| 1500 | Value *Op; |
| 1501 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 1502 | OpNum != Record.size()) |
Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1503 | return Error("Invalid FREE record"); |
| 1504 | I = new FreeInst(Op); |
| 1505 | break; |
| 1506 | } |
| 1507 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, op, align] |
| 1508 | if (Record.size() < 3) |
| 1509 | return Error("Invalid ALLOCA record"); |
| 1510 | const PointerType *Ty = |
| 1511 | dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); |
| 1512 | Value *Size = getFnValueByID(Record[1], Type::Int32Ty); |
| 1513 | unsigned Align = Record[2]; |
| 1514 | if (!Ty || !Size) return Error("Invalid ALLOCA record"); |
| 1515 | I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1); |
| 1516 | break; |
| 1517 | } |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1518 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1519 | unsigned OpNum = 0; |
| 1520 | Value *Op; |
| 1521 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 1522 | OpNum+2 != Record.size()) |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1523 | return Error("Invalid LOAD record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1524 | |
| 1525 | I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1526 | break; |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1527 | } |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1528 | case bitc::FUNC_CODE_INST_STORE2: { // STORE2:[ptrty, ptr, val, align, vol] |
| 1529 | unsigned OpNum = 0; |
| 1530 | Value *Val, *Ptr; |
| 1531 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
| 1532 | getValue(Record, OpNum, |
| 1533 | cast<PointerType>(Ptr->getType())->getElementType(), Val) || |
| 1534 | OpNum+2 != Record.size()) |
| 1535 | return Error("Invalid STORE record"); |
| 1536 | |
| 1537 | I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); |
| 1538 | break; |
| 1539 | } |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1540 | case bitc::FUNC_CODE_INST_STORE: { // STORE:[val, valty, ptr, align, vol] |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1541 | // FIXME: Legacy form of store instruction. Should be removed in LLVM 3.0. |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1542 | unsigned OpNum = 0; |
| 1543 | Value *Val, *Ptr; |
| 1544 | if (getValueTypePair(Record, OpNum, NextValueNo, Val) || |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1545 | getValue(Record, OpNum, PointerType::getUnqual(Val->getType()), Ptr)|| |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1546 | OpNum+2 != Record.size()) |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1547 | return Error("Invalid STORE record"); |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1548 | |
| 1549 | I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1550 | break; |
| 1551 | } |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1552 | case bitc::FUNC_CODE_INST_CALL: { |
| 1553 | // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...] |
| 1554 | if (Record.size() < 3) |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1555 | return Error("Invalid CALL record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame^] | 1557 | PAListPtr PAL = getParamAttrs(Record[0]); |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1558 | unsigned CCInfo = Record[1]; |
| 1559 | |
| 1560 | unsigned OpNum = 2; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1561 | Value *Callee; |
| 1562 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
| 1563 | return Error("Invalid CALL record"); |
| 1564 | |
| 1565 | const PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1566 | const FunctionType *FTy = 0; |
| 1567 | if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType()); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1568 | if (!FTy || Record.size() < FTy->getNumParams()+OpNum) |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1569 | return Error("Invalid CALL record"); |
| 1570 | |
| 1571 | SmallVector<Value*, 16> Args; |
| 1572 | // Read the fixed params. |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1573 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1574 | if (FTy->getParamType(i)->getTypeID()==Type::LabelTyID) |
| 1575 | Args.push_back(getBasicBlock(Record[OpNum])); |
| 1576 | else |
| 1577 | Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1578 | if (Args.back() == 0) return Error("Invalid CALL record"); |
| 1579 | } |
| 1580 | |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1581 | // Read type/value pairs for varargs params. |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1582 | if (!FTy->isVarArg()) { |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1583 | if (OpNum != Record.size()) |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1584 | return Error("Invalid CALL record"); |
| 1585 | } else { |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1586 | while (OpNum != Record.size()) { |
| 1587 | Value *Op; |
| 1588 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
| 1589 | return Error("Invalid CALL record"); |
| 1590 | Args.push_back(Op); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 1594 | I = new CallInst(Callee, Args.begin(), Args.end()); |
Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1595 | cast<CallInst>(I)->setCallingConv(CCInfo>>1); |
| 1596 | cast<CallInst>(I)->setTailCall(CCInfo & 1); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1597 | cast<CallInst>(I)->setParamAttrs(PAL); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1598 | break; |
| 1599 | } |
| 1600 | case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] |
| 1601 | if (Record.size() < 3) |
| 1602 | return Error("Invalid VAARG record"); |
| 1603 | const Type *OpTy = getTypeByID(Record[0]); |
| 1604 | Value *Op = getFnValueByID(Record[1], OpTy); |
| 1605 | const Type *ResTy = getTypeByID(Record[2]); |
| 1606 | if (!OpTy || !Op || !ResTy) |
| 1607 | return Error("Invalid VAARG record"); |
| 1608 | I = new VAArgInst(Op, ResTy); |
| 1609 | break; |
| 1610 | } |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | // Add instruction to end of current BB. If there is no current BB, reject |
| 1614 | // this file. |
| 1615 | if (CurBB == 0) { |
| 1616 | delete I; |
| 1617 | return Error("Invalid instruction with no BB"); |
| 1618 | } |
| 1619 | CurBB->getInstList().push_back(I); |
| 1620 | |
| 1621 | // If this was a terminator instruction, move to the next block. |
| 1622 | if (isa<TerminatorInst>(I)) { |
| 1623 | ++CurBBNo; |
| 1624 | CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0; |
| 1625 | } |
| 1626 | |
| 1627 | // Non-void values get registered in the value table for future use. |
| 1628 | if (I && I->getType() != Type::VoidTy) |
| 1629 | ValueList.AssignValue(I, NextValueNo++); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1632 | // Check the function list for unresolved values. |
| 1633 | if (Argument *A = dyn_cast<Argument>(ValueList.back())) { |
| 1634 | if (A->getParent() == 0) { |
| 1635 | // We found at least one unresolved value. Nuke them all to avoid leaks. |
| 1636 | for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ |
| 1637 | if ((A = dyn_cast<Argument>(ValueList.back())) && A->getParent() == 0) { |
| 1638 | A->replaceAllUsesWith(UndefValue::get(A->getType())); |
| 1639 | delete A; |
| 1640 | } |
| 1641 | } |
Chris Lattner | 35a0470 | 2007-05-04 03:50:29 +0000 | [diff] [blame] | 1642 | return Error("Never resolved value found in function!"); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1643 | } |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1644 | } |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1645 | |
| 1646 | // Trim the value list down to the size it was before we parsed this function. |
| 1647 | ValueList.shrinkTo(ModuleValueListSize); |
| 1648 | std::vector<BasicBlock*>().swap(FunctionBBs); |
| 1649 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1650 | return false; |
| 1651 | } |
| 1652 | |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1653 | //===----------------------------------------------------------------------===// |
| 1654 | // ModuleProvider implementation |
| 1655 | //===----------------------------------------------------------------------===// |
| 1656 | |
| 1657 | |
| 1658 | bool BitcodeReader::materializeFunction(Function *F, std::string *ErrInfo) { |
| 1659 | // If it already is material, ignore the request. |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 1660 | if (!F->hasNotBeenReadFromBitcode()) return false; |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1661 | |
| 1662 | DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator DFII = |
| 1663 | DeferredFunctionInfo.find(F); |
| 1664 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); |
| 1665 | |
| 1666 | // Move the bit stream to the saved position of the deferred function body and |
| 1667 | // restore the real linkage type for the function. |
| 1668 | Stream.JumpToBit(DFII->second.first); |
| 1669 | F->setLinkage((GlobalValue::LinkageTypes)DFII->second.second); |
| 1670 | |
| 1671 | if (ParseFunctionBody(F)) { |
| 1672 | if (ErrInfo) *ErrInfo = ErrorString; |
| 1673 | return true; |
| 1674 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1675 | |
| 1676 | // Upgrade any old intrinsic calls in the function. |
| 1677 | for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(), |
| 1678 | E = UpgradedIntrinsics.end(); I != E; ++I) { |
| 1679 | if (I->first != I->second) { |
| 1680 | for (Value::use_iterator UI = I->first->use_begin(), |
| 1681 | UE = I->first->use_end(); UI != UE; ) { |
| 1682 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 1683 | UpgradeIntrinsicCall(CI, I->second); |
| 1684 | } |
| 1685 | } |
| 1686 | } |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1687 | |
| 1688 | return false; |
| 1689 | } |
| 1690 | |
| 1691 | void BitcodeReader::dematerializeFunction(Function *F) { |
| 1692 | // If this function isn't materialized, or if it is a proto, this is a noop. |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 1693 | if (F->hasNotBeenReadFromBitcode() || F->isDeclaration()) |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1694 | return; |
| 1695 | |
| 1696 | assert(DeferredFunctionInfo.count(F) && "No info to read function later?"); |
| 1697 | |
| 1698 | // Just forget the function body, we can remat it later. |
| 1699 | F->deleteBody(); |
| 1700 | F->setLinkage(GlobalValue::GhostLinkage); |
| 1701 | } |
| 1702 | |
| 1703 | |
| 1704 | Module *BitcodeReader::materializeModule(std::string *ErrInfo) { |
| 1705 | for (DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator I = |
| 1706 | DeferredFunctionInfo.begin(), E = DeferredFunctionInfo.end(); I != E; |
| 1707 | ++I) { |
| 1708 | Function *F = I->first; |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 1709 | if (F->hasNotBeenReadFromBitcode() && |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1710 | materializeFunction(F, ErrInfo)) |
| 1711 | return 0; |
| 1712 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1713 | |
| 1714 | // Upgrade any intrinsic calls that slipped through (should not happen!) and |
| 1715 | // delete the old functions to clean up. We can't do this unless the entire |
| 1716 | // module is materialized because there could always be another function body |
| 1717 | // with calls to the old function. |
| 1718 | for (std::vector<std::pair<Function*, Function*> >::iterator I = |
| 1719 | UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) { |
| 1720 | if (I->first != I->second) { |
| 1721 | for (Value::use_iterator UI = I->first->use_begin(), |
| 1722 | UE = I->first->use_end(); UI != UE; ) { |
| 1723 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 1724 | UpgradeIntrinsicCall(CI, I->second); |
| 1725 | } |
| 1726 | ValueList.replaceUsesOfWith(I->first, I->second); |
| 1727 | I->first->eraseFromParent(); |
| 1728 | } |
| 1729 | } |
| 1730 | std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics); |
| 1731 | |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1732 | return TheModule; |
| 1733 | } |
| 1734 | |
| 1735 | |
| 1736 | /// This method is provided by the parent ModuleProvde class and overriden |
| 1737 | /// here. It simply releases the module from its provided and frees up our |
| 1738 | /// state. |
| 1739 | /// @brief Release our hold on the generated module |
| 1740 | Module *BitcodeReader::releaseModule(std::string *ErrInfo) { |
| 1741 | // Since we're losing control of this Module, we must hand it back complete |
| 1742 | Module *M = ModuleProvider::releaseModule(ErrInfo); |
| 1743 | FreeState(); |
| 1744 | return M; |
| 1745 | } |
| 1746 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1748 | //===----------------------------------------------------------------------===// |
| 1749 | // External interface |
| 1750 | //===----------------------------------------------------------------------===// |
| 1751 | |
| 1752 | /// getBitcodeModuleProvider - lazy function-at-a-time loading from a file. |
| 1753 | /// |
| 1754 | ModuleProvider *llvm::getBitcodeModuleProvider(MemoryBuffer *Buffer, |
| 1755 | std::string *ErrMsg) { |
| 1756 | BitcodeReader *R = new BitcodeReader(Buffer); |
| 1757 | if (R->ParseBitcode()) { |
| 1758 | if (ErrMsg) |
| 1759 | *ErrMsg = R->getErrorString(); |
| 1760 | |
| 1761 | // Don't let the BitcodeReader dtor delete 'Buffer'. |
| 1762 | R->releaseMemoryBuffer(); |
| 1763 | delete R; |
| 1764 | return 0; |
| 1765 | } |
| 1766 | return R; |
| 1767 | } |
| 1768 | |
| 1769 | /// ParseBitcodeFile - Read the specified bitcode file, returning the module. |
| 1770 | /// If an error occurs, return null and fill in *ErrMsg if non-null. |
| 1771 | Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg){ |
| 1772 | BitcodeReader *R; |
| 1773 | R = static_cast<BitcodeReader*>(getBitcodeModuleProvider(Buffer, ErrMsg)); |
| 1774 | if (!R) return 0; |
| 1775 | |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1776 | // Read in the entire module. |
| 1777 | Module *M = R->materializeModule(ErrMsg); |
| 1778 | |
| 1779 | // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether |
| 1780 | // there was an error. |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1781 | R->releaseMemoryBuffer(); |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1782 | |
| 1783 | // If there was no error, tell ModuleProvider not to delete it when its dtor |
| 1784 | // is run. |
| 1785 | if (M) |
| 1786 | M = R->releaseModule(ErrMsg); |
| 1787 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1788 | delete R; |
| 1789 | return M; |
| 1790 | } |