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