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