1f22ef01cSRoman Divacky //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // This header defines the BitcodeReader class. 11f22ef01cSRoman Divacky // 12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 13f22ef01cSRoman Divacky 14f22ef01cSRoman Divacky #include "llvm/Bitcode/ReaderWriter.h" 15f22ef01cSRoman Divacky #include "BitcodeReader.h" 16f22ef01cSRoman Divacky #include "llvm/Constants.h" 17f22ef01cSRoman Divacky #include "llvm/DerivedTypes.h" 18f22ef01cSRoman Divacky #include "llvm/InlineAsm.h" 19f22ef01cSRoman Divacky #include "llvm/IntrinsicInst.h" 20f22ef01cSRoman Divacky #include "llvm/Module.h" 21f22ef01cSRoman Divacky #include "llvm/Operator.h" 22f22ef01cSRoman Divacky #include "llvm/AutoUpgrade.h" 23f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h" 24f22ef01cSRoman Divacky #include "llvm/ADT/SmallVector.h" 25f22ef01cSRoman Divacky #include "llvm/Support/MathExtras.h" 26f22ef01cSRoman Divacky #include "llvm/Support/MemoryBuffer.h" 27f22ef01cSRoman Divacky #include "llvm/OperandTraits.h" 28f22ef01cSRoman Divacky using namespace llvm; 29f22ef01cSRoman Divacky 30f22ef01cSRoman Divacky void BitcodeReader::FreeState() { 31f22ef01cSRoman Divacky if (BufferOwned) 32f22ef01cSRoman Divacky delete Buffer; 33f22ef01cSRoman Divacky Buffer = 0; 34f22ef01cSRoman Divacky std::vector<PATypeHolder>().swap(TypeList); 35f22ef01cSRoman Divacky ValueList.clear(); 36f22ef01cSRoman Divacky MDValueList.clear(); 37f22ef01cSRoman Divacky 38f22ef01cSRoman Divacky std::vector<AttrListPtr>().swap(MAttributes); 39f22ef01cSRoman Divacky std::vector<BasicBlock*>().swap(FunctionBBs); 40f22ef01cSRoman Divacky std::vector<Function*>().swap(FunctionsWithBodies); 41f22ef01cSRoman Divacky DeferredFunctionInfo.clear(); 42e580952dSDimitry Andric MDKindMap.clear(); 43f22ef01cSRoman Divacky } 44f22ef01cSRoman Divacky 45f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 46f22ef01cSRoman Divacky // Helper functions to implement forward reference resolution, etc. 47f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 48f22ef01cSRoman Divacky 49f22ef01cSRoman Divacky /// ConvertToString - Convert a string from a record into an std::string, return 50f22ef01cSRoman Divacky /// true on failure. 51f22ef01cSRoman Divacky template<typename StrTy> 52f22ef01cSRoman Divacky static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx, 53f22ef01cSRoman Divacky StrTy &Result) { 54f22ef01cSRoman Divacky if (Idx > Record.size()) 55f22ef01cSRoman Divacky return true; 56f22ef01cSRoman Divacky 57f22ef01cSRoman Divacky for (unsigned i = Idx, e = Record.size(); i != e; ++i) 58f22ef01cSRoman Divacky Result += (char)Record[i]; 59f22ef01cSRoman Divacky return false; 60f22ef01cSRoman Divacky } 61f22ef01cSRoman Divacky 62f22ef01cSRoman Divacky static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { 63f22ef01cSRoman Divacky switch (Val) { 64f22ef01cSRoman Divacky default: // Map unknown/new linkages to external 65f22ef01cSRoman Divacky case 0: return GlobalValue::ExternalLinkage; 66f22ef01cSRoman Divacky case 1: return GlobalValue::WeakAnyLinkage; 67f22ef01cSRoman Divacky case 2: return GlobalValue::AppendingLinkage; 68f22ef01cSRoman Divacky case 3: return GlobalValue::InternalLinkage; 69f22ef01cSRoman Divacky case 4: return GlobalValue::LinkOnceAnyLinkage; 70f22ef01cSRoman Divacky case 5: return GlobalValue::DLLImportLinkage; 71f22ef01cSRoman Divacky case 6: return GlobalValue::DLLExportLinkage; 72f22ef01cSRoman Divacky case 7: return GlobalValue::ExternalWeakLinkage; 73f22ef01cSRoman Divacky case 8: return GlobalValue::CommonLinkage; 74f22ef01cSRoman Divacky case 9: return GlobalValue::PrivateLinkage; 75f22ef01cSRoman Divacky case 10: return GlobalValue::WeakODRLinkage; 76f22ef01cSRoman Divacky case 11: return GlobalValue::LinkOnceODRLinkage; 77f22ef01cSRoman Divacky case 12: return GlobalValue::AvailableExternallyLinkage; 78f22ef01cSRoman Divacky case 13: return GlobalValue::LinkerPrivateLinkage; 79ffd1746dSEd Schouten case 14: return GlobalValue::LinkerPrivateWeakLinkage; 80e580952dSDimitry Andric case 15: return GlobalValue::LinkerPrivateWeakDefAutoLinkage; 81f22ef01cSRoman Divacky } 82f22ef01cSRoman Divacky } 83f22ef01cSRoman Divacky 84f22ef01cSRoman Divacky static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { 85f22ef01cSRoman Divacky switch (Val) { 86f22ef01cSRoman Divacky default: // Map unknown visibilities to default. 87f22ef01cSRoman Divacky case 0: return GlobalValue::DefaultVisibility; 88f22ef01cSRoman Divacky case 1: return GlobalValue::HiddenVisibility; 89f22ef01cSRoman Divacky case 2: return GlobalValue::ProtectedVisibility; 90f22ef01cSRoman Divacky } 91f22ef01cSRoman Divacky } 92f22ef01cSRoman Divacky 93f22ef01cSRoman Divacky static int GetDecodedCastOpcode(unsigned Val) { 94f22ef01cSRoman Divacky switch (Val) { 95f22ef01cSRoman Divacky default: return -1; 96f22ef01cSRoman Divacky case bitc::CAST_TRUNC : return Instruction::Trunc; 97f22ef01cSRoman Divacky case bitc::CAST_ZEXT : return Instruction::ZExt; 98f22ef01cSRoman Divacky case bitc::CAST_SEXT : return Instruction::SExt; 99f22ef01cSRoman Divacky case bitc::CAST_FPTOUI : return Instruction::FPToUI; 100f22ef01cSRoman Divacky case bitc::CAST_FPTOSI : return Instruction::FPToSI; 101f22ef01cSRoman Divacky case bitc::CAST_UITOFP : return Instruction::UIToFP; 102f22ef01cSRoman Divacky case bitc::CAST_SITOFP : return Instruction::SIToFP; 103f22ef01cSRoman Divacky case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; 104f22ef01cSRoman Divacky case bitc::CAST_FPEXT : return Instruction::FPExt; 105f22ef01cSRoman Divacky case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; 106f22ef01cSRoman Divacky case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; 107f22ef01cSRoman Divacky case bitc::CAST_BITCAST : return Instruction::BitCast; 108f22ef01cSRoman Divacky } 109f22ef01cSRoman Divacky } 110f22ef01cSRoman Divacky static int GetDecodedBinaryOpcode(unsigned Val, const Type *Ty) { 111f22ef01cSRoman Divacky switch (Val) { 112f22ef01cSRoman Divacky default: return -1; 113f22ef01cSRoman Divacky case bitc::BINOP_ADD: 114f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add; 115f22ef01cSRoman Divacky case bitc::BINOP_SUB: 116f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub; 117f22ef01cSRoman Divacky case bitc::BINOP_MUL: 118f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul; 119f22ef01cSRoman Divacky case bitc::BINOP_UDIV: return Instruction::UDiv; 120f22ef01cSRoman Divacky case bitc::BINOP_SDIV: 121f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv; 122f22ef01cSRoman Divacky case bitc::BINOP_UREM: return Instruction::URem; 123f22ef01cSRoman Divacky case bitc::BINOP_SREM: 124f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem; 125f22ef01cSRoman Divacky case bitc::BINOP_SHL: return Instruction::Shl; 126f22ef01cSRoman Divacky case bitc::BINOP_LSHR: return Instruction::LShr; 127f22ef01cSRoman Divacky case bitc::BINOP_ASHR: return Instruction::AShr; 128f22ef01cSRoman Divacky case bitc::BINOP_AND: return Instruction::And; 129f22ef01cSRoman Divacky case bitc::BINOP_OR: return Instruction::Or; 130f22ef01cSRoman Divacky case bitc::BINOP_XOR: return Instruction::Xor; 131f22ef01cSRoman Divacky } 132f22ef01cSRoman Divacky } 133f22ef01cSRoman Divacky 134f22ef01cSRoman Divacky namespace llvm { 135f22ef01cSRoman Divacky namespace { 136f22ef01cSRoman Divacky /// @brief A class for maintaining the slot number definition 137f22ef01cSRoman Divacky /// as a placeholder for the actual definition for forward constants defs. 138f22ef01cSRoman Divacky class ConstantPlaceHolder : public ConstantExpr { 139f22ef01cSRoman Divacky ConstantPlaceHolder(); // DO NOT IMPLEMENT 140f22ef01cSRoman Divacky void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT 141f22ef01cSRoman Divacky public: 142f22ef01cSRoman Divacky // allocate space for exactly one operand 143f22ef01cSRoman Divacky void *operator new(size_t s) { 144f22ef01cSRoman Divacky return User::operator new(s, 1); 145f22ef01cSRoman Divacky } 146f22ef01cSRoman Divacky explicit ConstantPlaceHolder(const Type *Ty, LLVMContext& Context) 147f22ef01cSRoman Divacky : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) { 148f22ef01cSRoman Divacky Op<0>() = UndefValue::get(Type::getInt32Ty(Context)); 149f22ef01cSRoman Divacky } 150f22ef01cSRoman Divacky 151f22ef01cSRoman Divacky /// @brief Methods to support type inquiry through isa, cast, and dyn_cast. 152f22ef01cSRoman Divacky static inline bool classof(const ConstantPlaceHolder *) { return true; } 153f22ef01cSRoman Divacky static bool classof(const Value *V) { 154f22ef01cSRoman Divacky return isa<ConstantExpr>(V) && 155f22ef01cSRoman Divacky cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1; 156f22ef01cSRoman Divacky } 157f22ef01cSRoman Divacky 158f22ef01cSRoman Divacky 159f22ef01cSRoman Divacky /// Provide fast operand accessors 160f22ef01cSRoman Divacky //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 161f22ef01cSRoman Divacky }; 162f22ef01cSRoman Divacky } 163f22ef01cSRoman Divacky 164f22ef01cSRoman Divacky // FIXME: can we inherit this from ConstantExpr? 165f22ef01cSRoman Divacky template <> 166f22ef01cSRoman Divacky struct OperandTraits<ConstantPlaceHolder> : public FixedNumOperandTraits<1> { 167f22ef01cSRoman Divacky }; 168f22ef01cSRoman Divacky } 169f22ef01cSRoman Divacky 170f22ef01cSRoman Divacky 171f22ef01cSRoman Divacky void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) { 172f22ef01cSRoman Divacky if (Idx == size()) { 173f22ef01cSRoman Divacky push_back(V); 174f22ef01cSRoman Divacky return; 175f22ef01cSRoman Divacky } 176f22ef01cSRoman Divacky 177f22ef01cSRoman Divacky if (Idx >= size()) 178f22ef01cSRoman Divacky resize(Idx+1); 179f22ef01cSRoman Divacky 180f22ef01cSRoman Divacky WeakVH &OldV = ValuePtrs[Idx]; 181f22ef01cSRoman Divacky if (OldV == 0) { 182f22ef01cSRoman Divacky OldV = V; 183f22ef01cSRoman Divacky return; 184f22ef01cSRoman Divacky } 185f22ef01cSRoman Divacky 186f22ef01cSRoman Divacky // Handle constants and non-constants (e.g. instrs) differently for 187f22ef01cSRoman Divacky // efficiency. 188f22ef01cSRoman Divacky if (Constant *PHC = dyn_cast<Constant>(&*OldV)) { 189f22ef01cSRoman Divacky ResolveConstants.push_back(std::make_pair(PHC, Idx)); 190f22ef01cSRoman Divacky OldV = V; 191f22ef01cSRoman Divacky } else { 192f22ef01cSRoman Divacky // If there was a forward reference to this value, replace it. 193f22ef01cSRoman Divacky Value *PrevVal = OldV; 194f22ef01cSRoman Divacky OldV->replaceAllUsesWith(V); 195f22ef01cSRoman Divacky delete PrevVal; 196f22ef01cSRoman Divacky } 197f22ef01cSRoman Divacky } 198f22ef01cSRoman Divacky 199f22ef01cSRoman Divacky 200f22ef01cSRoman Divacky Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, 201f22ef01cSRoman Divacky const Type *Ty) { 202f22ef01cSRoman Divacky if (Idx >= size()) 203f22ef01cSRoman Divacky resize(Idx + 1); 204f22ef01cSRoman Divacky 205f22ef01cSRoman Divacky if (Value *V = ValuePtrs[Idx]) { 206f22ef01cSRoman Divacky assert(Ty == V->getType() && "Type mismatch in constant table!"); 207f22ef01cSRoman Divacky return cast<Constant>(V); 208f22ef01cSRoman Divacky } 209f22ef01cSRoman Divacky 210f22ef01cSRoman Divacky // Create and return a placeholder, which will later be RAUW'd. 211f22ef01cSRoman Divacky Constant *C = new ConstantPlaceHolder(Ty, Context); 212f22ef01cSRoman Divacky ValuePtrs[Idx] = C; 213f22ef01cSRoman Divacky return C; 214f22ef01cSRoman Divacky } 215f22ef01cSRoman Divacky 216f22ef01cSRoman Divacky Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) { 217f22ef01cSRoman Divacky if (Idx >= size()) 218f22ef01cSRoman Divacky resize(Idx + 1); 219f22ef01cSRoman Divacky 220f22ef01cSRoman Divacky if (Value *V = ValuePtrs[Idx]) { 221f22ef01cSRoman Divacky assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!"); 222f22ef01cSRoman Divacky return V; 223f22ef01cSRoman Divacky } 224f22ef01cSRoman Divacky 225f22ef01cSRoman Divacky // No type specified, must be invalid reference. 226f22ef01cSRoman Divacky if (Ty == 0) return 0; 227f22ef01cSRoman Divacky 228f22ef01cSRoman Divacky // Create and return a placeholder, which will later be RAUW'd. 229f22ef01cSRoman Divacky Value *V = new Argument(Ty); 230f22ef01cSRoman Divacky ValuePtrs[Idx] = V; 231f22ef01cSRoman Divacky return V; 232f22ef01cSRoman Divacky } 233f22ef01cSRoman Divacky 234f22ef01cSRoman Divacky /// ResolveConstantForwardRefs - Once all constants are read, this method bulk 235f22ef01cSRoman Divacky /// resolves any forward references. The idea behind this is that we sometimes 236f22ef01cSRoman Divacky /// get constants (such as large arrays) which reference *many* forward ref 237f22ef01cSRoman Divacky /// constants. Replacing each of these causes a lot of thrashing when 238f22ef01cSRoman Divacky /// building/reuniquing the constant. Instead of doing this, we look at all the 239f22ef01cSRoman Divacky /// uses and rewrite all the place holders at once for any constant that uses 240f22ef01cSRoman Divacky /// a placeholder. 241f22ef01cSRoman Divacky void BitcodeReaderValueList::ResolveConstantForwardRefs() { 242f22ef01cSRoman Divacky // Sort the values by-pointer so that they are efficient to look up with a 243f22ef01cSRoman Divacky // binary search. 244f22ef01cSRoman Divacky std::sort(ResolveConstants.begin(), ResolveConstants.end()); 245f22ef01cSRoman Divacky 246f22ef01cSRoman Divacky SmallVector<Constant*, 64> NewOps; 247f22ef01cSRoman Divacky 248f22ef01cSRoman Divacky while (!ResolveConstants.empty()) { 249f22ef01cSRoman Divacky Value *RealVal = operator[](ResolveConstants.back().second); 250f22ef01cSRoman Divacky Constant *Placeholder = ResolveConstants.back().first; 251f22ef01cSRoman Divacky ResolveConstants.pop_back(); 252f22ef01cSRoman Divacky 253f22ef01cSRoman Divacky // Loop over all users of the placeholder, updating them to reference the 254f22ef01cSRoman Divacky // new value. If they reference more than one placeholder, update them all 255f22ef01cSRoman Divacky // at once. 256f22ef01cSRoman Divacky while (!Placeholder->use_empty()) { 257f22ef01cSRoman Divacky Value::use_iterator UI = Placeholder->use_begin(); 258ffd1746dSEd Schouten User *U = *UI; 259f22ef01cSRoman Divacky 260f22ef01cSRoman Divacky // If the using object isn't uniqued, just update the operands. This 261f22ef01cSRoman Divacky // handles instructions and initializers for global variables. 262ffd1746dSEd Schouten if (!isa<Constant>(U) || isa<GlobalValue>(U)) { 263f22ef01cSRoman Divacky UI.getUse().set(RealVal); 264f22ef01cSRoman Divacky continue; 265f22ef01cSRoman Divacky } 266f22ef01cSRoman Divacky 267f22ef01cSRoman Divacky // Otherwise, we have a constant that uses the placeholder. Replace that 268f22ef01cSRoman Divacky // constant with a new constant that has *all* placeholder uses updated. 269ffd1746dSEd Schouten Constant *UserC = cast<Constant>(U); 270f22ef01cSRoman Divacky for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end(); 271f22ef01cSRoman Divacky I != E; ++I) { 272f22ef01cSRoman Divacky Value *NewOp; 273f22ef01cSRoman Divacky if (!isa<ConstantPlaceHolder>(*I)) { 274f22ef01cSRoman Divacky // Not a placeholder reference. 275f22ef01cSRoman Divacky NewOp = *I; 276f22ef01cSRoman Divacky } else if (*I == Placeholder) { 277f22ef01cSRoman Divacky // Common case is that it just references this one placeholder. 278f22ef01cSRoman Divacky NewOp = RealVal; 279f22ef01cSRoman Divacky } else { 280f22ef01cSRoman Divacky // Otherwise, look up the placeholder in ResolveConstants. 281f22ef01cSRoman Divacky ResolveConstantsTy::iterator It = 282f22ef01cSRoman Divacky std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(), 283f22ef01cSRoman Divacky std::pair<Constant*, unsigned>(cast<Constant>(*I), 284f22ef01cSRoman Divacky 0)); 285f22ef01cSRoman Divacky assert(It != ResolveConstants.end() && It->first == *I); 286f22ef01cSRoman Divacky NewOp = operator[](It->second); 287f22ef01cSRoman Divacky } 288f22ef01cSRoman Divacky 289f22ef01cSRoman Divacky NewOps.push_back(cast<Constant>(NewOp)); 290f22ef01cSRoman Divacky } 291f22ef01cSRoman Divacky 292f22ef01cSRoman Divacky // Make the new constant. 293f22ef01cSRoman Divacky Constant *NewC; 294f22ef01cSRoman Divacky if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) { 295f22ef01cSRoman Divacky NewC = ConstantArray::get(UserCA->getType(), &NewOps[0], 296f22ef01cSRoman Divacky NewOps.size()); 297f22ef01cSRoman Divacky } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) { 298f22ef01cSRoman Divacky NewC = ConstantStruct::get(Context, &NewOps[0], NewOps.size(), 299f22ef01cSRoman Divacky UserCS->getType()->isPacked()); 300f22ef01cSRoman Divacky } else if (isa<ConstantVector>(UserC)) { 301f22ef01cSRoman Divacky NewC = ConstantVector::get(&NewOps[0], NewOps.size()); 302f22ef01cSRoman Divacky } else { 303f22ef01cSRoman Divacky assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr."); 304f22ef01cSRoman Divacky NewC = cast<ConstantExpr>(UserC)->getWithOperands(&NewOps[0], 305f22ef01cSRoman Divacky NewOps.size()); 306f22ef01cSRoman Divacky } 307f22ef01cSRoman Divacky 308f22ef01cSRoman Divacky UserC->replaceAllUsesWith(NewC); 309f22ef01cSRoman Divacky UserC->destroyConstant(); 310f22ef01cSRoman Divacky NewOps.clear(); 311f22ef01cSRoman Divacky } 312f22ef01cSRoman Divacky 313f22ef01cSRoman Divacky // Update all ValueHandles, they should be the only users at this point. 314f22ef01cSRoman Divacky Placeholder->replaceAllUsesWith(RealVal); 315f22ef01cSRoman Divacky delete Placeholder; 316f22ef01cSRoman Divacky } 317f22ef01cSRoman Divacky } 318f22ef01cSRoman Divacky 319f22ef01cSRoman Divacky void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) { 320f22ef01cSRoman Divacky if (Idx == size()) { 321f22ef01cSRoman Divacky push_back(V); 322f22ef01cSRoman Divacky return; 323f22ef01cSRoman Divacky } 324f22ef01cSRoman Divacky 325f22ef01cSRoman Divacky if (Idx >= size()) 326f22ef01cSRoman Divacky resize(Idx+1); 327f22ef01cSRoman Divacky 328f22ef01cSRoman Divacky WeakVH &OldV = MDValuePtrs[Idx]; 329f22ef01cSRoman Divacky if (OldV == 0) { 330f22ef01cSRoman Divacky OldV = V; 331f22ef01cSRoman Divacky return; 332f22ef01cSRoman Divacky } 333f22ef01cSRoman Divacky 334f22ef01cSRoman Divacky // If there was a forward reference to this value, replace it. 335e580952dSDimitry Andric MDNode *PrevVal = cast<MDNode>(OldV); 336f22ef01cSRoman Divacky OldV->replaceAllUsesWith(V); 337e580952dSDimitry Andric MDNode::deleteTemporary(PrevVal); 338f22ef01cSRoman Divacky // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new 339f22ef01cSRoman Divacky // value for Idx. 340f22ef01cSRoman Divacky MDValuePtrs[Idx] = V; 341f22ef01cSRoman Divacky } 342f22ef01cSRoman Divacky 343f22ef01cSRoman Divacky Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { 344f22ef01cSRoman Divacky if (Idx >= size()) 345f22ef01cSRoman Divacky resize(Idx + 1); 346f22ef01cSRoman Divacky 347f22ef01cSRoman Divacky if (Value *V = MDValuePtrs[Idx]) { 348f22ef01cSRoman Divacky assert(V->getType()->isMetadataTy() && "Type mismatch in value table!"); 349f22ef01cSRoman Divacky return V; 350f22ef01cSRoman Divacky } 351f22ef01cSRoman Divacky 352f22ef01cSRoman Divacky // Create and return a placeholder, which will later be RAUW'd. 353e580952dSDimitry Andric Value *V = MDNode::getTemporary(Context, 0, 0); 354f22ef01cSRoman Divacky MDValuePtrs[Idx] = V; 355f22ef01cSRoman Divacky return V; 356f22ef01cSRoman Divacky } 357f22ef01cSRoman Divacky 358f22ef01cSRoman Divacky const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) { 359f22ef01cSRoman Divacky // If the TypeID is in range, return it. 360f22ef01cSRoman Divacky if (ID < TypeList.size()) 361f22ef01cSRoman Divacky return TypeList[ID].get(); 362f22ef01cSRoman Divacky if (!isTypeTable) return 0; 363f22ef01cSRoman Divacky 364f22ef01cSRoman Divacky // The type table allows forward references. Push as many Opaque types as 365f22ef01cSRoman Divacky // needed to get up to ID. 366f22ef01cSRoman Divacky while (TypeList.size() <= ID) 367f22ef01cSRoman Divacky TypeList.push_back(OpaqueType::get(Context)); 368f22ef01cSRoman Divacky return TypeList.back().get(); 369f22ef01cSRoman Divacky } 370f22ef01cSRoman Divacky 371f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 372f22ef01cSRoman Divacky // Functions for parsing blocks from the bitcode file 373f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 374f22ef01cSRoman Divacky 375f22ef01cSRoman Divacky bool BitcodeReader::ParseAttributeBlock() { 376f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) 377f22ef01cSRoman Divacky return Error("Malformed block record"); 378f22ef01cSRoman Divacky 379f22ef01cSRoman Divacky if (!MAttributes.empty()) 380f22ef01cSRoman Divacky return Error("Multiple PARAMATTR blocks found!"); 381f22ef01cSRoman Divacky 382f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 383f22ef01cSRoman Divacky 384f22ef01cSRoman Divacky SmallVector<AttributeWithIndex, 8> Attrs; 385f22ef01cSRoman Divacky 386f22ef01cSRoman Divacky // Read all the records. 387f22ef01cSRoman Divacky while (1) { 388f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 389f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 390f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 391f22ef01cSRoman Divacky return Error("Error at end of PARAMATTR block"); 392f22ef01cSRoman Divacky return false; 393f22ef01cSRoman Divacky } 394f22ef01cSRoman Divacky 395f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 396f22ef01cSRoman Divacky // No known subblocks, always skip them. 397f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 398f22ef01cSRoman Divacky if (Stream.SkipBlock()) 399f22ef01cSRoman Divacky return Error("Malformed block record"); 400f22ef01cSRoman Divacky continue; 401f22ef01cSRoman Divacky } 402f22ef01cSRoman Divacky 403f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 404f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 405f22ef01cSRoman Divacky continue; 406f22ef01cSRoman Divacky } 407f22ef01cSRoman Divacky 408f22ef01cSRoman Divacky // Read a record. 409f22ef01cSRoman Divacky Record.clear(); 410f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 411f22ef01cSRoman Divacky default: // Default behavior: ignore. 412f22ef01cSRoman Divacky break; 413f22ef01cSRoman Divacky case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [paramidx0, attr0, ...] 414f22ef01cSRoman Divacky if (Record.size() & 1) 415f22ef01cSRoman Divacky return Error("Invalid ENTRY record"); 416f22ef01cSRoman Divacky 417f22ef01cSRoman Divacky // FIXME : Remove this autoupgrade code in LLVM 3.0. 418f22ef01cSRoman Divacky // If Function attributes are using index 0 then transfer them 419f22ef01cSRoman Divacky // to index ~0. Index 0 is used for return value attributes but used to be 420f22ef01cSRoman Divacky // used for function attributes. 421f22ef01cSRoman Divacky Attributes RetAttribute = Attribute::None; 422f22ef01cSRoman Divacky Attributes FnAttribute = Attribute::None; 423f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 424f22ef01cSRoman Divacky // FIXME: remove in LLVM 3.0 425f22ef01cSRoman Divacky // The alignment is stored as a 16-bit raw value from bits 31--16. 426f22ef01cSRoman Divacky // We shift the bits above 31 down by 11 bits. 427f22ef01cSRoman Divacky 428f22ef01cSRoman Divacky unsigned Alignment = (Record[i+1] & (0xffffull << 16)) >> 16; 429f22ef01cSRoman Divacky if (Alignment && !isPowerOf2_32(Alignment)) 430f22ef01cSRoman Divacky return Error("Alignment is not a power of two."); 431f22ef01cSRoman Divacky 432f22ef01cSRoman Divacky Attributes ReconstitutedAttr = Record[i+1] & 0xffff; 433f22ef01cSRoman Divacky if (Alignment) 434f22ef01cSRoman Divacky ReconstitutedAttr |= Attribute::constructAlignmentFromInt(Alignment); 435f22ef01cSRoman Divacky ReconstitutedAttr |= (Record[i+1] & (0xffffull << 32)) >> 11; 436f22ef01cSRoman Divacky Record[i+1] = ReconstitutedAttr; 437f22ef01cSRoman Divacky 438f22ef01cSRoman Divacky if (Record[i] == 0) 439f22ef01cSRoman Divacky RetAttribute = Record[i+1]; 440f22ef01cSRoman Divacky else if (Record[i] == ~0U) 441f22ef01cSRoman Divacky FnAttribute = Record[i+1]; 442f22ef01cSRoman Divacky } 443f22ef01cSRoman Divacky 444f22ef01cSRoman Divacky unsigned OldRetAttrs = (Attribute::NoUnwind|Attribute::NoReturn| 445f22ef01cSRoman Divacky Attribute::ReadOnly|Attribute::ReadNone); 446f22ef01cSRoman Divacky 447f22ef01cSRoman Divacky if (FnAttribute == Attribute::None && RetAttribute != Attribute::None && 448f22ef01cSRoman Divacky (RetAttribute & OldRetAttrs) != 0) { 449f22ef01cSRoman Divacky if (FnAttribute == Attribute::None) { // add a slot so they get added. 450f22ef01cSRoman Divacky Record.push_back(~0U); 451f22ef01cSRoman Divacky Record.push_back(0); 452f22ef01cSRoman Divacky } 453f22ef01cSRoman Divacky 454f22ef01cSRoman Divacky FnAttribute |= RetAttribute & OldRetAttrs; 455f22ef01cSRoman Divacky RetAttribute &= ~OldRetAttrs; 456f22ef01cSRoman Divacky } 457f22ef01cSRoman Divacky 458f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 459f22ef01cSRoman Divacky if (Record[i] == 0) { 460f22ef01cSRoman Divacky if (RetAttribute != Attribute::None) 461f22ef01cSRoman Divacky Attrs.push_back(AttributeWithIndex::get(0, RetAttribute)); 462f22ef01cSRoman Divacky } else if (Record[i] == ~0U) { 463f22ef01cSRoman Divacky if (FnAttribute != Attribute::None) 464f22ef01cSRoman Divacky Attrs.push_back(AttributeWithIndex::get(~0U, FnAttribute)); 465f22ef01cSRoman Divacky } else if (Record[i+1] != Attribute::None) 466f22ef01cSRoman Divacky Attrs.push_back(AttributeWithIndex::get(Record[i], Record[i+1])); 467f22ef01cSRoman Divacky } 468f22ef01cSRoman Divacky 469f22ef01cSRoman Divacky MAttributes.push_back(AttrListPtr::get(Attrs.begin(), Attrs.end())); 470f22ef01cSRoman Divacky Attrs.clear(); 471f22ef01cSRoman Divacky break; 472f22ef01cSRoman Divacky } 473f22ef01cSRoman Divacky } 474f22ef01cSRoman Divacky } 475f22ef01cSRoman Divacky } 476f22ef01cSRoman Divacky 477f22ef01cSRoman Divacky 478f22ef01cSRoman Divacky bool BitcodeReader::ParseTypeTable() { 479f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID)) 480f22ef01cSRoman Divacky return Error("Malformed block record"); 481f22ef01cSRoman Divacky 482f22ef01cSRoman Divacky if (!TypeList.empty()) 483f22ef01cSRoman Divacky return Error("Multiple TYPE_BLOCKs found!"); 484f22ef01cSRoman Divacky 485f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 486f22ef01cSRoman Divacky unsigned NumRecords = 0; 487f22ef01cSRoman Divacky 488f22ef01cSRoman Divacky // Read all the records for this type table. 489f22ef01cSRoman Divacky while (1) { 490f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 491f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 492f22ef01cSRoman Divacky if (NumRecords != TypeList.size()) 493f22ef01cSRoman Divacky return Error("Invalid type forward reference in TYPE_BLOCK"); 494f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 495f22ef01cSRoman Divacky return Error("Error at end of type table block"); 496f22ef01cSRoman Divacky return false; 497f22ef01cSRoman Divacky } 498f22ef01cSRoman Divacky 499f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 500f22ef01cSRoman Divacky // No known subblocks, always skip them. 501f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 502f22ef01cSRoman Divacky if (Stream.SkipBlock()) 503f22ef01cSRoman Divacky return Error("Malformed block record"); 504f22ef01cSRoman Divacky continue; 505f22ef01cSRoman Divacky } 506f22ef01cSRoman Divacky 507f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 508f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 509f22ef01cSRoman Divacky continue; 510f22ef01cSRoman Divacky } 511f22ef01cSRoman Divacky 512f22ef01cSRoman Divacky // Read a record. 513f22ef01cSRoman Divacky Record.clear(); 514f22ef01cSRoman Divacky const Type *ResultTy = 0; 515f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 516f22ef01cSRoman Divacky default: // Default behavior: unknown type. 517f22ef01cSRoman Divacky ResultTy = 0; 518f22ef01cSRoman Divacky break; 519f22ef01cSRoman Divacky case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 520f22ef01cSRoman Divacky // TYPE_CODE_NUMENTRY contains a count of the number of types in the 521f22ef01cSRoman Divacky // type list. This allows us to reserve space. 522f22ef01cSRoman Divacky if (Record.size() < 1) 523f22ef01cSRoman Divacky return Error("Invalid TYPE_CODE_NUMENTRY record"); 524f22ef01cSRoman Divacky TypeList.reserve(Record[0]); 525f22ef01cSRoman Divacky continue; 526f22ef01cSRoman Divacky case bitc::TYPE_CODE_VOID: // VOID 527f22ef01cSRoman Divacky ResultTy = Type::getVoidTy(Context); 528f22ef01cSRoman Divacky break; 529f22ef01cSRoman Divacky case bitc::TYPE_CODE_FLOAT: // FLOAT 530f22ef01cSRoman Divacky ResultTy = Type::getFloatTy(Context); 531f22ef01cSRoman Divacky break; 532f22ef01cSRoman Divacky case bitc::TYPE_CODE_DOUBLE: // DOUBLE 533f22ef01cSRoman Divacky ResultTy = Type::getDoubleTy(Context); 534f22ef01cSRoman Divacky break; 535f22ef01cSRoman Divacky case bitc::TYPE_CODE_X86_FP80: // X86_FP80 536f22ef01cSRoman Divacky ResultTy = Type::getX86_FP80Ty(Context); 537f22ef01cSRoman Divacky break; 538f22ef01cSRoman Divacky case bitc::TYPE_CODE_FP128: // FP128 539f22ef01cSRoman Divacky ResultTy = Type::getFP128Ty(Context); 540f22ef01cSRoman Divacky break; 541f22ef01cSRoman Divacky case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 542f22ef01cSRoman Divacky ResultTy = Type::getPPC_FP128Ty(Context); 543f22ef01cSRoman Divacky break; 544f22ef01cSRoman Divacky case bitc::TYPE_CODE_LABEL: // LABEL 545f22ef01cSRoman Divacky ResultTy = Type::getLabelTy(Context); 546f22ef01cSRoman Divacky break; 547f22ef01cSRoman Divacky case bitc::TYPE_CODE_OPAQUE: // OPAQUE 548f22ef01cSRoman Divacky ResultTy = 0; 549f22ef01cSRoman Divacky break; 550f22ef01cSRoman Divacky case bitc::TYPE_CODE_METADATA: // METADATA 551f22ef01cSRoman Divacky ResultTy = Type::getMetadataTy(Context); 552f22ef01cSRoman Divacky break; 553f22ef01cSRoman Divacky case bitc::TYPE_CODE_INTEGER: // INTEGER: [width] 554f22ef01cSRoman Divacky if (Record.size() < 1) 555f22ef01cSRoman Divacky return Error("Invalid Integer type record"); 556f22ef01cSRoman Divacky 557f22ef01cSRoman Divacky ResultTy = IntegerType::get(Context, Record[0]); 558f22ef01cSRoman Divacky break; 559f22ef01cSRoman Divacky case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 560f22ef01cSRoman Divacky // [pointee type, address space] 561f22ef01cSRoman Divacky if (Record.size() < 1) 562f22ef01cSRoman Divacky return Error("Invalid POINTER type record"); 563f22ef01cSRoman Divacky unsigned AddressSpace = 0; 564f22ef01cSRoman Divacky if (Record.size() == 2) 565f22ef01cSRoman Divacky AddressSpace = Record[1]; 566f22ef01cSRoman Divacky ResultTy = PointerType::get(getTypeByID(Record[0], true), 567f22ef01cSRoman Divacky AddressSpace); 568f22ef01cSRoman Divacky break; 569f22ef01cSRoman Divacky } 570f22ef01cSRoman Divacky case bitc::TYPE_CODE_FUNCTION: { 571f22ef01cSRoman Divacky // FIXME: attrid is dead, remove it in LLVM 3.0 572f22ef01cSRoman Divacky // FUNCTION: [vararg, attrid, retty, paramty x N] 573f22ef01cSRoman Divacky if (Record.size() < 3) 574f22ef01cSRoman Divacky return Error("Invalid FUNCTION type record"); 575f22ef01cSRoman Divacky std::vector<const Type*> ArgTys; 576f22ef01cSRoman Divacky for (unsigned i = 3, e = Record.size(); i != e; ++i) 577f22ef01cSRoman Divacky ArgTys.push_back(getTypeByID(Record[i], true)); 578f22ef01cSRoman Divacky 579f22ef01cSRoman Divacky ResultTy = FunctionType::get(getTypeByID(Record[2], true), ArgTys, 580f22ef01cSRoman Divacky Record[0]); 581f22ef01cSRoman Divacky break; 582f22ef01cSRoman Divacky } 583f22ef01cSRoman Divacky case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, eltty x N] 584f22ef01cSRoman Divacky if (Record.size() < 1) 585f22ef01cSRoman Divacky return Error("Invalid STRUCT type record"); 586f22ef01cSRoman Divacky std::vector<const Type*> EltTys; 587f22ef01cSRoman Divacky for (unsigned i = 1, e = Record.size(); i != e; ++i) 588f22ef01cSRoman Divacky EltTys.push_back(getTypeByID(Record[i], true)); 589f22ef01cSRoman Divacky ResultTy = StructType::get(Context, EltTys, Record[0]); 590f22ef01cSRoman Divacky break; 591f22ef01cSRoman Divacky } 592f22ef01cSRoman Divacky case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] 593f22ef01cSRoman Divacky if (Record.size() < 2) 594f22ef01cSRoman Divacky return Error("Invalid ARRAY type record"); 595f22ef01cSRoman Divacky ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]); 596f22ef01cSRoman Divacky break; 597f22ef01cSRoman Divacky case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] 598f22ef01cSRoman Divacky if (Record.size() < 2) 599f22ef01cSRoman Divacky return Error("Invalid VECTOR type record"); 600f22ef01cSRoman Divacky ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]); 601f22ef01cSRoman Divacky break; 602f22ef01cSRoman Divacky } 603f22ef01cSRoman Divacky 604f22ef01cSRoman Divacky if (NumRecords == TypeList.size()) { 605f22ef01cSRoman Divacky // If this is a new type slot, just append it. 606f22ef01cSRoman Divacky TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get(Context)); 607f22ef01cSRoman Divacky ++NumRecords; 608f22ef01cSRoman Divacky } else if (ResultTy == 0) { 609f22ef01cSRoman Divacky // Otherwise, this was forward referenced, so an opaque type was created, 610f22ef01cSRoman Divacky // but the result type is actually just an opaque. Leave the one we 611f22ef01cSRoman Divacky // created previously. 612f22ef01cSRoman Divacky ++NumRecords; 613f22ef01cSRoman Divacky } else { 614f22ef01cSRoman Divacky // Otherwise, this was forward referenced, so an opaque type was created. 615f22ef01cSRoman Divacky // Resolve the opaque type to the real type now. 616f22ef01cSRoman Divacky assert(NumRecords < TypeList.size() && "Typelist imbalance"); 617f22ef01cSRoman Divacky const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get()); 618f22ef01cSRoman Divacky 619f22ef01cSRoman Divacky // Don't directly push the new type on the Tab. Instead we want to replace 620f22ef01cSRoman Divacky // the opaque type we previously inserted with the new concrete value. The 621f22ef01cSRoman Divacky // refinement from the abstract (opaque) type to the new type causes all 622f22ef01cSRoman Divacky // uses of the abstract type to use the concrete type (NewTy). This will 623f22ef01cSRoman Divacky // also cause the opaque type to be deleted. 624f22ef01cSRoman Divacky const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy); 625f22ef01cSRoman Divacky 626f22ef01cSRoman Divacky // This should have replaced the old opaque type with the new type in the 627f22ef01cSRoman Divacky // value table... or with a preexisting type that was already in the 628f22ef01cSRoman Divacky // system. Let's just make sure it did. 629f22ef01cSRoman Divacky assert(TypeList[NumRecords-1].get() != OldTy && 630f22ef01cSRoman Divacky "refineAbstractType didn't work!"); 631f22ef01cSRoman Divacky } 632f22ef01cSRoman Divacky } 633f22ef01cSRoman Divacky } 634f22ef01cSRoman Divacky 635f22ef01cSRoman Divacky 636f22ef01cSRoman Divacky bool BitcodeReader::ParseTypeSymbolTable() { 637f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::TYPE_SYMTAB_BLOCK_ID)) 638f22ef01cSRoman Divacky return Error("Malformed block record"); 639f22ef01cSRoman Divacky 640f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 641f22ef01cSRoman Divacky 642f22ef01cSRoman Divacky // Read all the records for this type table. 643f22ef01cSRoman Divacky std::string TypeName; 644f22ef01cSRoman Divacky while (1) { 645f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 646f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 647f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 648f22ef01cSRoman Divacky return Error("Error at end of type symbol table block"); 649f22ef01cSRoman Divacky return false; 650f22ef01cSRoman Divacky } 651f22ef01cSRoman Divacky 652f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 653f22ef01cSRoman Divacky // No known subblocks, always skip them. 654f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 655f22ef01cSRoman Divacky if (Stream.SkipBlock()) 656f22ef01cSRoman Divacky return Error("Malformed block record"); 657f22ef01cSRoman Divacky continue; 658f22ef01cSRoman Divacky } 659f22ef01cSRoman Divacky 660f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 661f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 662f22ef01cSRoman Divacky continue; 663f22ef01cSRoman Divacky } 664f22ef01cSRoman Divacky 665f22ef01cSRoman Divacky // Read a record. 666f22ef01cSRoman Divacky Record.clear(); 667f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 668f22ef01cSRoman Divacky default: // Default behavior: unknown type. 669f22ef01cSRoman Divacky break; 670f22ef01cSRoman Divacky case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namechar x N] 671f22ef01cSRoman Divacky if (ConvertToString(Record, 1, TypeName)) 672f22ef01cSRoman Divacky return Error("Invalid TST_ENTRY record"); 673f22ef01cSRoman Divacky unsigned TypeID = Record[0]; 674f22ef01cSRoman Divacky if (TypeID >= TypeList.size()) 675f22ef01cSRoman Divacky return Error("Invalid Type ID in TST_ENTRY record"); 676f22ef01cSRoman Divacky 677f22ef01cSRoman Divacky TheModule->addTypeName(TypeName, TypeList[TypeID].get()); 678f22ef01cSRoman Divacky TypeName.clear(); 679f22ef01cSRoman Divacky break; 680f22ef01cSRoman Divacky } 681f22ef01cSRoman Divacky } 682f22ef01cSRoman Divacky } 683f22ef01cSRoman Divacky 684f22ef01cSRoman Divacky bool BitcodeReader::ParseValueSymbolTable() { 685f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 686f22ef01cSRoman Divacky return Error("Malformed block record"); 687f22ef01cSRoman Divacky 688f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 689f22ef01cSRoman Divacky 690f22ef01cSRoman Divacky // Read all the records for this value table. 691f22ef01cSRoman Divacky SmallString<128> ValueName; 692f22ef01cSRoman Divacky while (1) { 693f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 694f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 695f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 696f22ef01cSRoman Divacky return Error("Error at end of value symbol table block"); 697f22ef01cSRoman Divacky return false; 698f22ef01cSRoman Divacky } 699f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 700f22ef01cSRoman Divacky // No known subblocks, always skip them. 701f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 702f22ef01cSRoman Divacky if (Stream.SkipBlock()) 703f22ef01cSRoman Divacky return Error("Malformed block record"); 704f22ef01cSRoman Divacky continue; 705f22ef01cSRoman Divacky } 706f22ef01cSRoman Divacky 707f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 708f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 709f22ef01cSRoman Divacky continue; 710f22ef01cSRoman Divacky } 711f22ef01cSRoman Divacky 712f22ef01cSRoman Divacky // Read a record. 713f22ef01cSRoman Divacky Record.clear(); 714f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 715f22ef01cSRoman Divacky default: // Default behavior: unknown type. 716f22ef01cSRoman Divacky break; 717f22ef01cSRoman Divacky case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N] 718f22ef01cSRoman Divacky if (ConvertToString(Record, 1, ValueName)) 719f22ef01cSRoman Divacky return Error("Invalid VST_ENTRY record"); 720f22ef01cSRoman Divacky unsigned ValueID = Record[0]; 721f22ef01cSRoman Divacky if (ValueID >= ValueList.size()) 722f22ef01cSRoman Divacky return Error("Invalid Value ID in VST_ENTRY record"); 723f22ef01cSRoman Divacky Value *V = ValueList[ValueID]; 724f22ef01cSRoman Divacky 725f22ef01cSRoman Divacky V->setName(StringRef(ValueName.data(), ValueName.size())); 726f22ef01cSRoman Divacky ValueName.clear(); 727f22ef01cSRoman Divacky break; 728f22ef01cSRoman Divacky } 729f22ef01cSRoman Divacky case bitc::VST_CODE_BBENTRY: { 730f22ef01cSRoman Divacky if (ConvertToString(Record, 1, ValueName)) 731f22ef01cSRoman Divacky return Error("Invalid VST_BBENTRY record"); 732f22ef01cSRoman Divacky BasicBlock *BB = getBasicBlock(Record[0]); 733f22ef01cSRoman Divacky if (BB == 0) 734f22ef01cSRoman Divacky return Error("Invalid BB ID in VST_BBENTRY record"); 735f22ef01cSRoman Divacky 736f22ef01cSRoman Divacky BB->setName(StringRef(ValueName.data(), ValueName.size())); 737f22ef01cSRoman Divacky ValueName.clear(); 738f22ef01cSRoman Divacky break; 739f22ef01cSRoman Divacky } 740f22ef01cSRoman Divacky } 741f22ef01cSRoman Divacky } 742f22ef01cSRoman Divacky } 743f22ef01cSRoman Divacky 744f22ef01cSRoman Divacky bool BitcodeReader::ParseMetadata() { 745f22ef01cSRoman Divacky unsigned NextMDValueNo = MDValueList.size(); 746f22ef01cSRoman Divacky 747f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) 748f22ef01cSRoman Divacky return Error("Malformed block record"); 749f22ef01cSRoman Divacky 750f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 751f22ef01cSRoman Divacky 752f22ef01cSRoman Divacky // Read all the records. 753f22ef01cSRoman Divacky while (1) { 754f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 755f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 756f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 757f22ef01cSRoman Divacky return Error("Error at end of PARAMATTR block"); 758f22ef01cSRoman Divacky return false; 759f22ef01cSRoman Divacky } 760f22ef01cSRoman Divacky 761f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 762f22ef01cSRoman Divacky // No known subblocks, always skip them. 763f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 764f22ef01cSRoman Divacky if (Stream.SkipBlock()) 765f22ef01cSRoman Divacky return Error("Malformed block record"); 766f22ef01cSRoman Divacky continue; 767f22ef01cSRoman Divacky } 768f22ef01cSRoman Divacky 769f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 770f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 771f22ef01cSRoman Divacky continue; 772f22ef01cSRoman Divacky } 773f22ef01cSRoman Divacky 774f22ef01cSRoman Divacky bool IsFunctionLocal = false; 775f22ef01cSRoman Divacky // Read a record. 776f22ef01cSRoman Divacky Record.clear(); 777e580952dSDimitry Andric Code = Stream.ReadRecord(Code, Record); 778e580952dSDimitry Andric switch (Code) { 779f22ef01cSRoman Divacky default: // Default behavior: ignore. 780f22ef01cSRoman Divacky break; 781f22ef01cSRoman Divacky case bitc::METADATA_NAME: { 782f22ef01cSRoman Divacky // Read named of the named metadata. 783f22ef01cSRoman Divacky unsigned NameLength = Record.size(); 784f22ef01cSRoman Divacky SmallString<8> Name; 785f22ef01cSRoman Divacky Name.resize(NameLength); 786f22ef01cSRoman Divacky for (unsigned i = 0; i != NameLength; ++i) 787f22ef01cSRoman Divacky Name[i] = Record[i]; 788f22ef01cSRoman Divacky Record.clear(); 789f22ef01cSRoman Divacky Code = Stream.ReadCode(); 790f22ef01cSRoman Divacky 791e580952dSDimitry Andric // METADATA_NAME is always followed by METADATA_NAMED_NODE2. 792e580952dSDimitry Andric // Or METADATA_NAMED_NODE in LLVM 2.7. FIXME: Remove this in LLVM 3.0. 793e580952dSDimitry Andric unsigned NextBitCode = Stream.ReadRecord(Code, Record); 794e580952dSDimitry Andric if (NextBitCode == bitc::METADATA_NAMED_NODE) { 795e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 796e580952dSDimitry Andric } else if (NextBitCode != bitc::METADATA_NAMED_NODE2) 797f22ef01cSRoman Divacky assert ( 0 && "Inavlid Named Metadata record"); 798f22ef01cSRoman Divacky 799f22ef01cSRoman Divacky // Read named metadata elements. 800f22ef01cSRoman Divacky unsigned Size = Record.size(); 801e580952dSDimitry Andric NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name); 802f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) { 803f22ef01cSRoman Divacky MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i])); 804f22ef01cSRoman Divacky if (MD == 0) 805f22ef01cSRoman Divacky return Error("Malformed metadata record"); 806e580952dSDimitry Andric NMD->addOperand(MD); 807f22ef01cSRoman Divacky } 808e580952dSDimitry Andric // Backwards compatibility hack: NamedMDValues used to be Values, 809e580952dSDimitry Andric // and they got their own slots in the value numbering. They are no 810e580952dSDimitry Andric // longer Values, however we still need to account for them in the 811e580952dSDimitry Andric // numbering in order to be able to read old bitcode files. 812e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 813e580952dSDimitry Andric if (LLVM2_7MetadataDetected) 814e580952dSDimitry Andric MDValueList.AssignValue(0, NextMDValueNo++); 815f22ef01cSRoman Divacky break; 816f22ef01cSRoman Divacky } 817e580952dSDimitry Andric case bitc::METADATA_FN_NODE: // FIXME: Remove in LLVM 3.0. 818e580952dSDimitry Andric case bitc::METADATA_FN_NODE2: 819f22ef01cSRoman Divacky IsFunctionLocal = true; 820f22ef01cSRoman Divacky // fall-through 821e580952dSDimitry Andric case bitc::METADATA_NODE: // FIXME: Remove in LLVM 3.0. 822e580952dSDimitry Andric case bitc::METADATA_NODE2: { 823e580952dSDimitry Andric 824e580952dSDimitry Andric // Detect 2.7-era metadata. 825e580952dSDimitry Andric // FIXME: Remove in LLVM 3.0. 826e580952dSDimitry Andric if (Code == bitc::METADATA_FN_NODE || Code == bitc::METADATA_NODE) 827e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 828e580952dSDimitry Andric 829ffd1746dSEd Schouten if (Record.size() % 2 == 1) 830e580952dSDimitry Andric return Error("Invalid METADATA_NODE2 record"); 831f22ef01cSRoman Divacky 832f22ef01cSRoman Divacky unsigned Size = Record.size(); 833f22ef01cSRoman Divacky SmallVector<Value*, 8> Elts; 834f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; i += 2) { 835f22ef01cSRoman Divacky const Type *Ty = getTypeByID(Record[i], false); 836f22ef01cSRoman Divacky if (Ty->isMetadataTy()) 837f22ef01cSRoman Divacky Elts.push_back(MDValueList.getValueFwdRef(Record[i+1])); 838f22ef01cSRoman Divacky else if (!Ty->isVoidTy()) 839f22ef01cSRoman Divacky Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty)); 840f22ef01cSRoman Divacky else 841f22ef01cSRoman Divacky Elts.push_back(NULL); 842f22ef01cSRoman Divacky } 843ffd1746dSEd Schouten Value *V = MDNode::getWhenValsUnresolved(Context, 844ffd1746dSEd Schouten Elts.data(), Elts.size(), 845f22ef01cSRoman Divacky IsFunctionLocal); 846f22ef01cSRoman Divacky IsFunctionLocal = false; 847f22ef01cSRoman Divacky MDValueList.AssignValue(V, NextMDValueNo++); 848f22ef01cSRoman Divacky break; 849f22ef01cSRoman Divacky } 850f22ef01cSRoman Divacky case bitc::METADATA_STRING: { 851f22ef01cSRoman Divacky unsigned MDStringLength = Record.size(); 852f22ef01cSRoman Divacky SmallString<8> String; 853f22ef01cSRoman Divacky String.resize(MDStringLength); 854f22ef01cSRoman Divacky for (unsigned i = 0; i != MDStringLength; ++i) 855f22ef01cSRoman Divacky String[i] = Record[i]; 856f22ef01cSRoman Divacky Value *V = MDString::get(Context, 857f22ef01cSRoman Divacky StringRef(String.data(), String.size())); 858f22ef01cSRoman Divacky MDValueList.AssignValue(V, NextMDValueNo++); 859f22ef01cSRoman Divacky break; 860f22ef01cSRoman Divacky } 861f22ef01cSRoman Divacky case bitc::METADATA_KIND: { 862f22ef01cSRoman Divacky unsigned RecordLength = Record.size(); 863f22ef01cSRoman Divacky if (Record.empty() || RecordLength < 2) 864f22ef01cSRoman Divacky return Error("Invalid METADATA_KIND record"); 865f22ef01cSRoman Divacky SmallString<8> Name; 866f22ef01cSRoman Divacky Name.resize(RecordLength-1); 867f22ef01cSRoman Divacky unsigned Kind = Record[0]; 868f22ef01cSRoman Divacky for (unsigned i = 1; i != RecordLength; ++i) 869f22ef01cSRoman Divacky Name[i-1] = Record[i]; 870f22ef01cSRoman Divacky 871f22ef01cSRoman Divacky unsigned NewKind = TheModule->getMDKindID(Name.str()); 872e580952dSDimitry Andric if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) 873e580952dSDimitry Andric return Error("Conflicting METADATA_KIND records"); 874f22ef01cSRoman Divacky break; 875f22ef01cSRoman Divacky } 876f22ef01cSRoman Divacky } 877f22ef01cSRoman Divacky } 878f22ef01cSRoman Divacky } 879f22ef01cSRoman Divacky 880f22ef01cSRoman Divacky /// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in 881f22ef01cSRoman Divacky /// the LSB for dense VBR encoding. 882f22ef01cSRoman Divacky static uint64_t DecodeSignRotatedValue(uint64_t V) { 883f22ef01cSRoman Divacky if ((V & 1) == 0) 884f22ef01cSRoman Divacky return V >> 1; 885f22ef01cSRoman Divacky if (V != 1) 886f22ef01cSRoman Divacky return -(V >> 1); 887f22ef01cSRoman Divacky // There is no such thing as -0 with integers. "-0" really means MININT. 888f22ef01cSRoman Divacky return 1ULL << 63; 889f22ef01cSRoman Divacky } 890f22ef01cSRoman Divacky 891f22ef01cSRoman Divacky /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global 892f22ef01cSRoman Divacky /// values and aliases that we can. 893f22ef01cSRoman Divacky bool BitcodeReader::ResolveGlobalAndAliasInits() { 894f22ef01cSRoman Divacky std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; 895f22ef01cSRoman Divacky std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; 896f22ef01cSRoman Divacky 897f22ef01cSRoman Divacky GlobalInitWorklist.swap(GlobalInits); 898f22ef01cSRoman Divacky AliasInitWorklist.swap(AliasInits); 899f22ef01cSRoman Divacky 900f22ef01cSRoman Divacky while (!GlobalInitWorklist.empty()) { 901f22ef01cSRoman Divacky unsigned ValID = GlobalInitWorklist.back().second; 902f22ef01cSRoman Divacky if (ValID >= ValueList.size()) { 903f22ef01cSRoman Divacky // Not ready to resolve this yet, it requires something later in the file. 904f22ef01cSRoman Divacky GlobalInits.push_back(GlobalInitWorklist.back()); 905f22ef01cSRoman Divacky } else { 906f22ef01cSRoman Divacky if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) 907f22ef01cSRoman Divacky GlobalInitWorklist.back().first->setInitializer(C); 908f22ef01cSRoman Divacky else 909f22ef01cSRoman Divacky return Error("Global variable initializer is not a constant!"); 910f22ef01cSRoman Divacky } 911f22ef01cSRoman Divacky GlobalInitWorklist.pop_back(); 912f22ef01cSRoman Divacky } 913f22ef01cSRoman Divacky 914f22ef01cSRoman Divacky while (!AliasInitWorklist.empty()) { 915f22ef01cSRoman Divacky unsigned ValID = AliasInitWorklist.back().second; 916f22ef01cSRoman Divacky if (ValID >= ValueList.size()) { 917f22ef01cSRoman Divacky AliasInits.push_back(AliasInitWorklist.back()); 918f22ef01cSRoman Divacky } else { 919f22ef01cSRoman Divacky if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) 920f22ef01cSRoman Divacky AliasInitWorklist.back().first->setAliasee(C); 921f22ef01cSRoman Divacky else 922f22ef01cSRoman Divacky return Error("Alias initializer is not a constant!"); 923f22ef01cSRoman Divacky } 924f22ef01cSRoman Divacky AliasInitWorklist.pop_back(); 925f22ef01cSRoman Divacky } 926f22ef01cSRoman Divacky return false; 927f22ef01cSRoman Divacky } 928f22ef01cSRoman Divacky 929f22ef01cSRoman Divacky bool BitcodeReader::ParseConstants() { 930f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) 931f22ef01cSRoman Divacky return Error("Malformed block record"); 932f22ef01cSRoman Divacky 933f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 934f22ef01cSRoman Divacky 935f22ef01cSRoman Divacky // Read all the records for this value table. 936f22ef01cSRoman Divacky const Type *CurTy = Type::getInt32Ty(Context); 937f22ef01cSRoman Divacky unsigned NextCstNo = ValueList.size(); 938f22ef01cSRoman Divacky while (1) { 939f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 940f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) 941f22ef01cSRoman Divacky break; 942f22ef01cSRoman Divacky 943f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 944f22ef01cSRoman Divacky // No known subblocks, always skip them. 945f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 946f22ef01cSRoman Divacky if (Stream.SkipBlock()) 947f22ef01cSRoman Divacky return Error("Malformed block record"); 948f22ef01cSRoman Divacky continue; 949f22ef01cSRoman Divacky } 950f22ef01cSRoman Divacky 951f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 952f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 953f22ef01cSRoman Divacky continue; 954f22ef01cSRoman Divacky } 955f22ef01cSRoman Divacky 956f22ef01cSRoman Divacky // Read a record. 957f22ef01cSRoman Divacky Record.clear(); 958f22ef01cSRoman Divacky Value *V = 0; 959f22ef01cSRoman Divacky unsigned BitCode = Stream.ReadRecord(Code, Record); 960f22ef01cSRoman Divacky switch (BitCode) { 961f22ef01cSRoman Divacky default: // Default behavior: unknown constant 962f22ef01cSRoman Divacky case bitc::CST_CODE_UNDEF: // UNDEF 963f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 964f22ef01cSRoman Divacky break; 965f22ef01cSRoman Divacky case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] 966f22ef01cSRoman Divacky if (Record.empty()) 967f22ef01cSRoman Divacky return Error("Malformed CST_SETTYPE record"); 968f22ef01cSRoman Divacky if (Record[0] >= TypeList.size()) 969f22ef01cSRoman Divacky return Error("Invalid Type ID in CST_SETTYPE record"); 970f22ef01cSRoman Divacky CurTy = TypeList[Record[0]]; 971f22ef01cSRoman Divacky continue; // Skip the ValueList manipulation. 972f22ef01cSRoman Divacky case bitc::CST_CODE_NULL: // NULL 973f22ef01cSRoman Divacky V = Constant::getNullValue(CurTy); 974f22ef01cSRoman Divacky break; 975f22ef01cSRoman Divacky case bitc::CST_CODE_INTEGER: // INTEGER: [intval] 976f22ef01cSRoman Divacky if (!CurTy->isIntegerTy() || Record.empty()) 977f22ef01cSRoman Divacky return Error("Invalid CST_INTEGER record"); 978f22ef01cSRoman Divacky V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0])); 979f22ef01cSRoman Divacky break; 980f22ef01cSRoman Divacky case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] 981f22ef01cSRoman Divacky if (!CurTy->isIntegerTy() || Record.empty()) 982f22ef01cSRoman Divacky return Error("Invalid WIDE_INTEGER record"); 983f22ef01cSRoman Divacky 984f22ef01cSRoman Divacky unsigned NumWords = Record.size(); 985f22ef01cSRoman Divacky SmallVector<uint64_t, 8> Words; 986f22ef01cSRoman Divacky Words.resize(NumWords); 987f22ef01cSRoman Divacky for (unsigned i = 0; i != NumWords; ++i) 988f22ef01cSRoman Divacky Words[i] = DecodeSignRotatedValue(Record[i]); 989f22ef01cSRoman Divacky V = ConstantInt::get(Context, 990f22ef01cSRoman Divacky APInt(cast<IntegerType>(CurTy)->getBitWidth(), 991f22ef01cSRoman Divacky NumWords, &Words[0])); 992f22ef01cSRoman Divacky break; 993f22ef01cSRoman Divacky } 994f22ef01cSRoman Divacky case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] 995f22ef01cSRoman Divacky if (Record.empty()) 996f22ef01cSRoman Divacky return Error("Invalid FLOAT record"); 997f22ef01cSRoman Divacky if (CurTy->isFloatTy()) 998f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(32, (uint32_t)Record[0]))); 999f22ef01cSRoman Divacky else if (CurTy->isDoubleTy()) 1000f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(64, Record[0]))); 1001f22ef01cSRoman Divacky else if (CurTy->isX86_FP80Ty()) { 1002f22ef01cSRoman Divacky // Bits are not stored the same way as a normal i80 APInt, compensate. 1003f22ef01cSRoman Divacky uint64_t Rearrange[2]; 1004f22ef01cSRoman Divacky Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); 1005f22ef01cSRoman Divacky Rearrange[1] = Record[0] >> 48; 1006f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(80, 2, Rearrange))); 1007f22ef01cSRoman Divacky } else if (CurTy->isFP128Ty()) 1008f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(128, 2, &Record[0]), true)); 1009f22ef01cSRoman Divacky else if (CurTy->isPPC_FP128Ty()) 1010f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(128, 2, &Record[0]))); 1011f22ef01cSRoman Divacky else 1012f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 1013f22ef01cSRoman Divacky break; 1014f22ef01cSRoman Divacky } 1015f22ef01cSRoman Divacky 1016f22ef01cSRoman Divacky case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] 1017f22ef01cSRoman Divacky if (Record.empty()) 1018f22ef01cSRoman Divacky return Error("Invalid CST_AGGREGATE record"); 1019f22ef01cSRoman Divacky 1020f22ef01cSRoman Divacky unsigned Size = Record.size(); 1021f22ef01cSRoman Divacky std::vector<Constant*> Elts; 1022f22ef01cSRoman Divacky 1023f22ef01cSRoman Divacky if (const StructType *STy = dyn_cast<StructType>(CurTy)) { 1024f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1025f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], 1026f22ef01cSRoman Divacky STy->getElementType(i))); 1027f22ef01cSRoman Divacky V = ConstantStruct::get(STy, Elts); 1028f22ef01cSRoman Divacky } else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { 1029f22ef01cSRoman Divacky const Type *EltTy = ATy->getElementType(); 1030f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1031f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 1032f22ef01cSRoman Divacky V = ConstantArray::get(ATy, Elts); 1033f22ef01cSRoman Divacky } else if (const VectorType *VTy = dyn_cast<VectorType>(CurTy)) { 1034f22ef01cSRoman Divacky const Type *EltTy = VTy->getElementType(); 1035f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1036f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 1037f22ef01cSRoman Divacky V = ConstantVector::get(Elts); 1038f22ef01cSRoman Divacky } else { 1039f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 1040f22ef01cSRoman Divacky } 1041f22ef01cSRoman Divacky break; 1042f22ef01cSRoman Divacky } 1043f22ef01cSRoman Divacky case bitc::CST_CODE_STRING: { // STRING: [values] 1044f22ef01cSRoman Divacky if (Record.empty()) 1045f22ef01cSRoman Divacky return Error("Invalid CST_AGGREGATE record"); 1046f22ef01cSRoman Divacky 1047f22ef01cSRoman Divacky const ArrayType *ATy = cast<ArrayType>(CurTy); 1048f22ef01cSRoman Divacky const Type *EltTy = ATy->getElementType(); 1049f22ef01cSRoman Divacky 1050f22ef01cSRoman Divacky unsigned Size = Record.size(); 1051f22ef01cSRoman Divacky std::vector<Constant*> Elts; 1052f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1053f22ef01cSRoman Divacky Elts.push_back(ConstantInt::get(EltTy, Record[i])); 1054f22ef01cSRoman Divacky V = ConstantArray::get(ATy, Elts); 1055f22ef01cSRoman Divacky break; 1056f22ef01cSRoman Divacky } 1057f22ef01cSRoman Divacky case bitc::CST_CODE_CSTRING: { // CSTRING: [values] 1058f22ef01cSRoman Divacky if (Record.empty()) 1059f22ef01cSRoman Divacky return Error("Invalid CST_AGGREGATE record"); 1060f22ef01cSRoman Divacky 1061f22ef01cSRoman Divacky const ArrayType *ATy = cast<ArrayType>(CurTy); 1062f22ef01cSRoman Divacky const Type *EltTy = ATy->getElementType(); 1063f22ef01cSRoman Divacky 1064f22ef01cSRoman Divacky unsigned Size = Record.size(); 1065f22ef01cSRoman Divacky std::vector<Constant*> Elts; 1066f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1067f22ef01cSRoman Divacky Elts.push_back(ConstantInt::get(EltTy, Record[i])); 1068f22ef01cSRoman Divacky Elts.push_back(Constant::getNullValue(EltTy)); 1069f22ef01cSRoman Divacky V = ConstantArray::get(ATy, Elts); 1070f22ef01cSRoman Divacky break; 1071f22ef01cSRoman Divacky } 1072f22ef01cSRoman Divacky case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] 1073f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_BINOP record"); 1074f22ef01cSRoman Divacky int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); 1075f22ef01cSRoman Divacky if (Opc < 0) { 1076f22ef01cSRoman Divacky V = UndefValue::get(CurTy); // Unknown binop. 1077f22ef01cSRoman Divacky } else { 1078f22ef01cSRoman Divacky Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); 1079f22ef01cSRoman Divacky Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); 1080f22ef01cSRoman Divacky unsigned Flags = 0; 1081f22ef01cSRoman Divacky if (Record.size() >= 4) { 1082f22ef01cSRoman Divacky if (Opc == Instruction::Add || 1083f22ef01cSRoman Divacky Opc == Instruction::Sub || 1084f22ef01cSRoman Divacky Opc == Instruction::Mul) { 1085f22ef01cSRoman Divacky if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 1086f22ef01cSRoman Divacky Flags |= OverflowingBinaryOperator::NoSignedWrap; 1087f22ef01cSRoman Divacky if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 1088f22ef01cSRoman Divacky Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 1089f22ef01cSRoman Divacky } else if (Opc == Instruction::SDiv) { 1090f22ef01cSRoman Divacky if (Record[3] & (1 << bitc::SDIV_EXACT)) 1091f22ef01cSRoman Divacky Flags |= SDivOperator::IsExact; 1092f22ef01cSRoman Divacky } 1093f22ef01cSRoman Divacky } 1094f22ef01cSRoman Divacky V = ConstantExpr::get(Opc, LHS, RHS, Flags); 1095f22ef01cSRoman Divacky } 1096f22ef01cSRoman Divacky break; 1097f22ef01cSRoman Divacky } 1098f22ef01cSRoman Divacky case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] 1099f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_CAST record"); 1100f22ef01cSRoman Divacky int Opc = GetDecodedCastOpcode(Record[0]); 1101f22ef01cSRoman Divacky if (Opc < 0) { 1102f22ef01cSRoman Divacky V = UndefValue::get(CurTy); // Unknown cast. 1103f22ef01cSRoman Divacky } else { 1104f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[1]); 1105f22ef01cSRoman Divacky if (!OpTy) return Error("Invalid CE_CAST record"); 1106f22ef01cSRoman Divacky Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); 1107f22ef01cSRoman Divacky V = ConstantExpr::getCast(Opc, Op, CurTy); 1108f22ef01cSRoman Divacky } 1109f22ef01cSRoman Divacky break; 1110f22ef01cSRoman Divacky } 1111f22ef01cSRoman Divacky case bitc::CST_CODE_CE_INBOUNDS_GEP: 1112f22ef01cSRoman Divacky case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] 1113f22ef01cSRoman Divacky if (Record.size() & 1) return Error("Invalid CE_GEP record"); 1114f22ef01cSRoman Divacky SmallVector<Constant*, 16> Elts; 1115f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 1116f22ef01cSRoman Divacky const Type *ElTy = getTypeByID(Record[i]); 1117f22ef01cSRoman Divacky if (!ElTy) return Error("Invalid CE_GEP record"); 1118f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); 1119f22ef01cSRoman Divacky } 1120f22ef01cSRoman Divacky if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) 1121f22ef01cSRoman Divacky V = ConstantExpr::getInBoundsGetElementPtr(Elts[0], &Elts[1], 1122f22ef01cSRoman Divacky Elts.size()-1); 1123f22ef01cSRoman Divacky else 1124f22ef01cSRoman Divacky V = ConstantExpr::getGetElementPtr(Elts[0], &Elts[1], 1125f22ef01cSRoman Divacky Elts.size()-1); 1126f22ef01cSRoman Divacky break; 1127f22ef01cSRoman Divacky } 1128f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#] 1129f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_SELECT record"); 1130f22ef01cSRoman Divacky V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], 1131f22ef01cSRoman Divacky Type::getInt1Ty(Context)), 1132f22ef01cSRoman Divacky ValueList.getConstantFwdRef(Record[1],CurTy), 1133f22ef01cSRoman Divacky ValueList.getConstantFwdRef(Record[2],CurTy)); 1134f22ef01cSRoman Divacky break; 1135f22ef01cSRoman Divacky case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval] 1136f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_EXTRACTELT record"); 1137f22ef01cSRoman Divacky const VectorType *OpTy = 1138f22ef01cSRoman Divacky dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 1139f22ef01cSRoman Divacky if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record"); 1140f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 1141f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 1142f22ef01cSRoman Divacky V = ConstantExpr::getExtractElement(Op0, Op1); 1143f22ef01cSRoman Divacky break; 1144f22ef01cSRoman Divacky } 1145f22ef01cSRoman Divacky case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval] 1146f22ef01cSRoman Divacky const VectorType *OpTy = dyn_cast<VectorType>(CurTy); 1147f22ef01cSRoman Divacky if (Record.size() < 3 || OpTy == 0) 1148f22ef01cSRoman Divacky return Error("Invalid CE_INSERTELT record"); 1149f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 1150f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[1], 1151f22ef01cSRoman Divacky OpTy->getElementType()); 1152f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 1153f22ef01cSRoman Divacky V = ConstantExpr::getInsertElement(Op0, Op1, Op2); 1154f22ef01cSRoman Divacky break; 1155f22ef01cSRoman Divacky } 1156f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] 1157f22ef01cSRoman Divacky const VectorType *OpTy = dyn_cast<VectorType>(CurTy); 1158f22ef01cSRoman Divacky if (Record.size() < 3 || OpTy == 0) 1159f22ef01cSRoman Divacky return Error("Invalid CE_SHUFFLEVEC record"); 1160f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 1161f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); 1162f22ef01cSRoman Divacky const Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 1163f22ef01cSRoman Divacky OpTy->getNumElements()); 1164f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); 1165f22ef01cSRoman Divacky V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 1166f22ef01cSRoman Divacky break; 1167f22ef01cSRoman Divacky } 1168f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] 1169f22ef01cSRoman Divacky const VectorType *RTy = dyn_cast<VectorType>(CurTy); 1170f22ef01cSRoman Divacky const VectorType *OpTy = dyn_cast<VectorType>(getTypeByID(Record[0])); 1171f22ef01cSRoman Divacky if (Record.size() < 4 || RTy == 0 || OpTy == 0) 1172f22ef01cSRoman Divacky return Error("Invalid CE_SHUFVEC_EX record"); 1173f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 1174f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 1175f22ef01cSRoman Divacky const Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 1176f22ef01cSRoman Divacky RTy->getNumElements()); 1177f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); 1178f22ef01cSRoman Divacky V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 1179f22ef01cSRoman Divacky break; 1180f22ef01cSRoman Divacky } 1181f22ef01cSRoman Divacky case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] 1182f22ef01cSRoman Divacky if (Record.size() < 4) return Error("Invalid CE_CMP record"); 1183f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[0]); 1184f22ef01cSRoman Divacky if (OpTy == 0) return Error("Invalid CE_CMP record"); 1185f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 1186f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 1187f22ef01cSRoman Divacky 1188f22ef01cSRoman Divacky if (OpTy->isFPOrFPVectorTy()) 1189f22ef01cSRoman Divacky V = ConstantExpr::getFCmp(Record[3], Op0, Op1); 1190f22ef01cSRoman Divacky else 1191f22ef01cSRoman Divacky V = ConstantExpr::getICmp(Record[3], Op0, Op1); 1192f22ef01cSRoman Divacky break; 1193f22ef01cSRoman Divacky } 1194f22ef01cSRoman Divacky case bitc::CST_CODE_INLINEASM: { 1195f22ef01cSRoman Divacky if (Record.size() < 2) return Error("Invalid INLINEASM record"); 1196f22ef01cSRoman Divacky std::string AsmStr, ConstrStr; 1197f22ef01cSRoman Divacky bool HasSideEffects = Record[0] & 1; 1198f22ef01cSRoman Divacky bool IsAlignStack = Record[0] >> 1; 1199f22ef01cSRoman Divacky unsigned AsmStrSize = Record[1]; 1200f22ef01cSRoman Divacky if (2+AsmStrSize >= Record.size()) 1201f22ef01cSRoman Divacky return Error("Invalid INLINEASM record"); 1202f22ef01cSRoman Divacky unsigned ConstStrSize = Record[2+AsmStrSize]; 1203f22ef01cSRoman Divacky if (3+AsmStrSize+ConstStrSize > Record.size()) 1204f22ef01cSRoman Divacky return Error("Invalid INLINEASM record"); 1205f22ef01cSRoman Divacky 1206f22ef01cSRoman Divacky for (unsigned i = 0; i != AsmStrSize; ++i) 1207f22ef01cSRoman Divacky AsmStr += (char)Record[2+i]; 1208f22ef01cSRoman Divacky for (unsigned i = 0; i != ConstStrSize; ++i) 1209f22ef01cSRoman Divacky ConstrStr += (char)Record[3+AsmStrSize+i]; 1210f22ef01cSRoman Divacky const PointerType *PTy = cast<PointerType>(CurTy); 1211f22ef01cSRoman Divacky V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), 1212f22ef01cSRoman Divacky AsmStr, ConstrStr, HasSideEffects, IsAlignStack); 1213f22ef01cSRoman Divacky break; 1214f22ef01cSRoman Divacky } 1215f22ef01cSRoman Divacky case bitc::CST_CODE_BLOCKADDRESS:{ 1216f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_BLOCKADDRESS record"); 1217f22ef01cSRoman Divacky const Type *FnTy = getTypeByID(Record[0]); 1218f22ef01cSRoman Divacky if (FnTy == 0) return Error("Invalid CE_BLOCKADDRESS record"); 1219f22ef01cSRoman Divacky Function *Fn = 1220f22ef01cSRoman Divacky dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); 1221f22ef01cSRoman Divacky if (Fn == 0) return Error("Invalid CE_BLOCKADDRESS record"); 1222f22ef01cSRoman Divacky 1223f22ef01cSRoman Divacky GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(), 1224f22ef01cSRoman Divacky Type::getInt8Ty(Context), 1225f22ef01cSRoman Divacky false, GlobalValue::InternalLinkage, 1226f22ef01cSRoman Divacky 0, ""); 1227f22ef01cSRoman Divacky BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef)); 1228f22ef01cSRoman Divacky V = FwdRef; 1229f22ef01cSRoman Divacky break; 1230f22ef01cSRoman Divacky } 1231f22ef01cSRoman Divacky } 1232f22ef01cSRoman Divacky 1233f22ef01cSRoman Divacky ValueList.AssignValue(V, NextCstNo); 1234f22ef01cSRoman Divacky ++NextCstNo; 1235f22ef01cSRoman Divacky } 1236f22ef01cSRoman Divacky 1237f22ef01cSRoman Divacky if (NextCstNo != ValueList.size()) 1238f22ef01cSRoman Divacky return Error("Invalid constant reference!"); 1239f22ef01cSRoman Divacky 1240f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 1241f22ef01cSRoman Divacky return Error("Error at end of constants block"); 1242f22ef01cSRoman Divacky 1243f22ef01cSRoman Divacky // Once all the constants have been read, go through and resolve forward 1244f22ef01cSRoman Divacky // references. 1245f22ef01cSRoman Divacky ValueList.ResolveConstantForwardRefs(); 1246f22ef01cSRoman Divacky return false; 1247f22ef01cSRoman Divacky } 1248f22ef01cSRoman Divacky 1249f22ef01cSRoman Divacky /// RememberAndSkipFunctionBody - When we see the block for a function body, 1250f22ef01cSRoman Divacky /// remember where it is and then skip it. This lets us lazily deserialize the 1251f22ef01cSRoman Divacky /// functions. 1252f22ef01cSRoman Divacky bool BitcodeReader::RememberAndSkipFunctionBody() { 1253f22ef01cSRoman Divacky // Get the function we are talking about. 1254f22ef01cSRoman Divacky if (FunctionsWithBodies.empty()) 1255f22ef01cSRoman Divacky return Error("Insufficient function protos"); 1256f22ef01cSRoman Divacky 1257f22ef01cSRoman Divacky Function *Fn = FunctionsWithBodies.back(); 1258f22ef01cSRoman Divacky FunctionsWithBodies.pop_back(); 1259f22ef01cSRoman Divacky 1260f22ef01cSRoman Divacky // Save the current stream state. 1261f22ef01cSRoman Divacky uint64_t CurBit = Stream.GetCurrentBitNo(); 1262f22ef01cSRoman Divacky DeferredFunctionInfo[Fn] = CurBit; 1263f22ef01cSRoman Divacky 1264f22ef01cSRoman Divacky // Skip over the function block for now. 1265f22ef01cSRoman Divacky if (Stream.SkipBlock()) 1266f22ef01cSRoman Divacky return Error("Malformed block record"); 1267f22ef01cSRoman Divacky return false; 1268f22ef01cSRoman Divacky } 1269f22ef01cSRoman Divacky 1270f22ef01cSRoman Divacky bool BitcodeReader::ParseModule() { 1271f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 1272f22ef01cSRoman Divacky return Error("Malformed block record"); 1273f22ef01cSRoman Divacky 1274f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1275f22ef01cSRoman Divacky std::vector<std::string> SectionTable; 1276f22ef01cSRoman Divacky std::vector<std::string> GCTable; 1277f22ef01cSRoman Divacky 1278f22ef01cSRoman Divacky // Read all the records for this module. 1279f22ef01cSRoman Divacky while (!Stream.AtEndOfStream()) { 1280f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 1281f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 1282f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 1283f22ef01cSRoman Divacky return Error("Error at end of module block"); 1284f22ef01cSRoman Divacky 1285f22ef01cSRoman Divacky // Patch the initializers for globals and aliases up. 1286f22ef01cSRoman Divacky ResolveGlobalAndAliasInits(); 1287f22ef01cSRoman Divacky if (!GlobalInits.empty() || !AliasInits.empty()) 1288f22ef01cSRoman Divacky return Error("Malformed global initializer set"); 1289f22ef01cSRoman Divacky if (!FunctionsWithBodies.empty()) 1290f22ef01cSRoman Divacky return Error("Too few function bodies found"); 1291f22ef01cSRoman Divacky 1292f22ef01cSRoman Divacky // Look for intrinsic functions which need to be upgraded at some point 1293f22ef01cSRoman Divacky for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); 1294f22ef01cSRoman Divacky FI != FE; ++FI) { 1295f22ef01cSRoman Divacky Function* NewFn; 1296f22ef01cSRoman Divacky if (UpgradeIntrinsicFunction(FI, NewFn)) 1297f22ef01cSRoman Divacky UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn)); 1298f22ef01cSRoman Divacky } 1299f22ef01cSRoman Divacky 1300e580952dSDimitry Andric // Look for global variables which need to be renamed. 1301e580952dSDimitry Andric for (Module::global_iterator 1302e580952dSDimitry Andric GI = TheModule->global_begin(), GE = TheModule->global_end(); 1303e580952dSDimitry Andric GI != GE; ++GI) 1304e580952dSDimitry Andric UpgradeGlobalVariable(GI); 1305e580952dSDimitry Andric 1306f22ef01cSRoman Divacky // Force deallocation of memory for these vectors to favor the client that 1307f22ef01cSRoman Divacky // want lazy deserialization. 1308f22ef01cSRoman Divacky std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); 1309f22ef01cSRoman Divacky std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); 1310f22ef01cSRoman Divacky std::vector<Function*>().swap(FunctionsWithBodies); 1311f22ef01cSRoman Divacky return false; 1312f22ef01cSRoman Divacky } 1313f22ef01cSRoman Divacky 1314f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 1315f22ef01cSRoman Divacky switch (Stream.ReadSubBlockID()) { 1316f22ef01cSRoman Divacky default: // Skip unknown content. 1317f22ef01cSRoman Divacky if (Stream.SkipBlock()) 1318f22ef01cSRoman Divacky return Error("Malformed block record"); 1319f22ef01cSRoman Divacky break; 1320f22ef01cSRoman Divacky case bitc::BLOCKINFO_BLOCK_ID: 1321f22ef01cSRoman Divacky if (Stream.ReadBlockInfoBlock()) 1322f22ef01cSRoman Divacky return Error("Malformed BlockInfoBlock"); 1323f22ef01cSRoman Divacky break; 1324f22ef01cSRoman Divacky case bitc::PARAMATTR_BLOCK_ID: 1325f22ef01cSRoman Divacky if (ParseAttributeBlock()) 1326f22ef01cSRoman Divacky return true; 1327f22ef01cSRoman Divacky break; 1328f22ef01cSRoman Divacky case bitc::TYPE_BLOCK_ID: 1329f22ef01cSRoman Divacky if (ParseTypeTable()) 1330f22ef01cSRoman Divacky return true; 1331f22ef01cSRoman Divacky break; 1332f22ef01cSRoman Divacky case bitc::TYPE_SYMTAB_BLOCK_ID: 1333f22ef01cSRoman Divacky if (ParseTypeSymbolTable()) 1334f22ef01cSRoman Divacky return true; 1335f22ef01cSRoman Divacky break; 1336f22ef01cSRoman Divacky case bitc::VALUE_SYMTAB_BLOCK_ID: 1337f22ef01cSRoman Divacky if (ParseValueSymbolTable()) 1338f22ef01cSRoman Divacky return true; 1339f22ef01cSRoman Divacky break; 1340f22ef01cSRoman Divacky case bitc::CONSTANTS_BLOCK_ID: 1341f22ef01cSRoman Divacky if (ParseConstants() || ResolveGlobalAndAliasInits()) 1342f22ef01cSRoman Divacky return true; 1343f22ef01cSRoman Divacky break; 1344f22ef01cSRoman Divacky case bitc::METADATA_BLOCK_ID: 1345f22ef01cSRoman Divacky if (ParseMetadata()) 1346f22ef01cSRoman Divacky return true; 1347f22ef01cSRoman Divacky break; 1348f22ef01cSRoman Divacky case bitc::FUNCTION_BLOCK_ID: 1349f22ef01cSRoman Divacky // If this is the first function body we've seen, reverse the 1350f22ef01cSRoman Divacky // FunctionsWithBodies list. 1351f22ef01cSRoman Divacky if (!HasReversedFunctionsWithBodies) { 1352f22ef01cSRoman Divacky std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 1353f22ef01cSRoman Divacky HasReversedFunctionsWithBodies = true; 1354f22ef01cSRoman Divacky } 1355f22ef01cSRoman Divacky 1356f22ef01cSRoman Divacky if (RememberAndSkipFunctionBody()) 1357f22ef01cSRoman Divacky return true; 1358f22ef01cSRoman Divacky break; 1359f22ef01cSRoman Divacky } 1360f22ef01cSRoman Divacky continue; 1361f22ef01cSRoman Divacky } 1362f22ef01cSRoman Divacky 1363f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 1364f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 1365f22ef01cSRoman Divacky continue; 1366f22ef01cSRoman Divacky } 1367f22ef01cSRoman Divacky 1368f22ef01cSRoman Divacky // Read a record. 1369f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 1370f22ef01cSRoman Divacky default: break; // Default behavior, ignore unknown content. 1371f22ef01cSRoman Divacky case bitc::MODULE_CODE_VERSION: // VERSION: [version#] 1372f22ef01cSRoman Divacky if (Record.size() < 1) 1373f22ef01cSRoman Divacky return Error("Malformed MODULE_CODE_VERSION"); 1374f22ef01cSRoman Divacky // Only version #0 is supported so far. 1375f22ef01cSRoman Divacky if (Record[0] != 0) 1376f22ef01cSRoman Divacky return Error("Unknown bitstream version!"); 1377f22ef01cSRoman Divacky break; 1378f22ef01cSRoman Divacky case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 1379f22ef01cSRoman Divacky std::string S; 1380f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1381f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_TRIPLE record"); 1382f22ef01cSRoman Divacky TheModule->setTargetTriple(S); 1383f22ef01cSRoman Divacky break; 1384f22ef01cSRoman Divacky } 1385f22ef01cSRoman Divacky case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] 1386f22ef01cSRoman Divacky std::string S; 1387f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1388f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_DATALAYOUT record"); 1389f22ef01cSRoman Divacky TheModule->setDataLayout(S); 1390f22ef01cSRoman Divacky break; 1391f22ef01cSRoman Divacky } 1392f22ef01cSRoman Divacky case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] 1393f22ef01cSRoman Divacky std::string S; 1394f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1395f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_ASM record"); 1396f22ef01cSRoman Divacky TheModule->setModuleInlineAsm(S); 1397f22ef01cSRoman Divacky break; 1398f22ef01cSRoman Divacky } 1399f22ef01cSRoman Divacky case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] 1400f22ef01cSRoman Divacky std::string S; 1401f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1402f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_DEPLIB record"); 1403f22ef01cSRoman Divacky TheModule->addLibrary(S); 1404f22ef01cSRoman Divacky break; 1405f22ef01cSRoman Divacky } 1406f22ef01cSRoman Divacky case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 1407f22ef01cSRoman Divacky std::string S; 1408f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1409f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_SECTIONNAME record"); 1410f22ef01cSRoman Divacky SectionTable.push_back(S); 1411f22ef01cSRoman Divacky break; 1412f22ef01cSRoman Divacky } 1413f22ef01cSRoman Divacky case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] 1414f22ef01cSRoman Divacky std::string S; 1415f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1416f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_GCNAME record"); 1417f22ef01cSRoman Divacky GCTable.push_back(S); 1418f22ef01cSRoman Divacky break; 1419f22ef01cSRoman Divacky } 1420f22ef01cSRoman Divacky // GLOBALVAR: [pointer type, isconst, initid, 1421f22ef01cSRoman Divacky // linkage, alignment, section, visibility, threadlocal] 1422f22ef01cSRoman Divacky case bitc::MODULE_CODE_GLOBALVAR: { 1423f22ef01cSRoman Divacky if (Record.size() < 6) 1424f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_GLOBALVAR record"); 1425f22ef01cSRoman Divacky const Type *Ty = getTypeByID(Record[0]); 1426f22ef01cSRoman Divacky if (!Ty->isPointerTy()) 1427f22ef01cSRoman Divacky return Error("Global not a pointer type!"); 1428f22ef01cSRoman Divacky unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); 1429f22ef01cSRoman Divacky Ty = cast<PointerType>(Ty)->getElementType(); 1430f22ef01cSRoman Divacky 1431f22ef01cSRoman Divacky bool isConstant = Record[1]; 1432f22ef01cSRoman Divacky GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]); 1433f22ef01cSRoman Divacky unsigned Alignment = (1 << Record[4]) >> 1; 1434f22ef01cSRoman Divacky std::string Section; 1435f22ef01cSRoman Divacky if (Record[5]) { 1436f22ef01cSRoman Divacky if (Record[5]-1 >= SectionTable.size()) 1437f22ef01cSRoman Divacky return Error("Invalid section ID"); 1438f22ef01cSRoman Divacky Section = SectionTable[Record[5]-1]; 1439f22ef01cSRoman Divacky } 1440f22ef01cSRoman Divacky GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; 1441f22ef01cSRoman Divacky if (Record.size() > 6) 1442f22ef01cSRoman Divacky Visibility = GetDecodedVisibility(Record[6]); 1443f22ef01cSRoman Divacky bool isThreadLocal = false; 1444f22ef01cSRoman Divacky if (Record.size() > 7) 1445f22ef01cSRoman Divacky isThreadLocal = Record[7]; 1446f22ef01cSRoman Divacky 1447f22ef01cSRoman Divacky GlobalVariable *NewGV = 1448f22ef01cSRoman Divacky new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0, 1449f22ef01cSRoman Divacky isThreadLocal, AddressSpace); 1450f22ef01cSRoman Divacky NewGV->setAlignment(Alignment); 1451f22ef01cSRoman Divacky if (!Section.empty()) 1452f22ef01cSRoman Divacky NewGV->setSection(Section); 1453f22ef01cSRoman Divacky NewGV->setVisibility(Visibility); 1454f22ef01cSRoman Divacky NewGV->setThreadLocal(isThreadLocal); 1455f22ef01cSRoman Divacky 1456f22ef01cSRoman Divacky ValueList.push_back(NewGV); 1457f22ef01cSRoman Divacky 1458f22ef01cSRoman Divacky // Remember which value to use for the global initializer. 1459f22ef01cSRoman Divacky if (unsigned InitID = Record[2]) 1460f22ef01cSRoman Divacky GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); 1461f22ef01cSRoman Divacky break; 1462f22ef01cSRoman Divacky } 1463f22ef01cSRoman Divacky // FUNCTION: [type, callingconv, isproto, linkage, paramattr, 1464f22ef01cSRoman Divacky // alignment, section, visibility, gc] 1465f22ef01cSRoman Divacky case bitc::MODULE_CODE_FUNCTION: { 1466f22ef01cSRoman Divacky if (Record.size() < 8) 1467f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_FUNCTION record"); 1468f22ef01cSRoman Divacky const Type *Ty = getTypeByID(Record[0]); 1469f22ef01cSRoman Divacky if (!Ty->isPointerTy()) 1470f22ef01cSRoman Divacky return Error("Function not a pointer type!"); 1471f22ef01cSRoman Divacky const FunctionType *FTy = 1472f22ef01cSRoman Divacky dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); 1473f22ef01cSRoman Divacky if (!FTy) 1474f22ef01cSRoman Divacky return Error("Function not a pointer to function type!"); 1475f22ef01cSRoman Divacky 1476f22ef01cSRoman Divacky Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, 1477f22ef01cSRoman Divacky "", TheModule); 1478f22ef01cSRoman Divacky 1479f22ef01cSRoman Divacky Func->setCallingConv(static_cast<CallingConv::ID>(Record[1])); 1480f22ef01cSRoman Divacky bool isProto = Record[2]; 1481f22ef01cSRoman Divacky Func->setLinkage(GetDecodedLinkage(Record[3])); 1482f22ef01cSRoman Divacky Func->setAttributes(getAttributes(Record[4])); 1483f22ef01cSRoman Divacky 1484f22ef01cSRoman Divacky Func->setAlignment((1 << Record[5]) >> 1); 1485f22ef01cSRoman Divacky if (Record[6]) { 1486f22ef01cSRoman Divacky if (Record[6]-1 >= SectionTable.size()) 1487f22ef01cSRoman Divacky return Error("Invalid section ID"); 1488f22ef01cSRoman Divacky Func->setSection(SectionTable[Record[6]-1]); 1489f22ef01cSRoman Divacky } 1490f22ef01cSRoman Divacky Func->setVisibility(GetDecodedVisibility(Record[7])); 1491f22ef01cSRoman Divacky if (Record.size() > 8 && Record[8]) { 1492f22ef01cSRoman Divacky if (Record[8]-1 > GCTable.size()) 1493f22ef01cSRoman Divacky return Error("Invalid GC ID"); 1494f22ef01cSRoman Divacky Func->setGC(GCTable[Record[8]-1].c_str()); 1495f22ef01cSRoman Divacky } 1496f22ef01cSRoman Divacky ValueList.push_back(Func); 1497f22ef01cSRoman Divacky 1498f22ef01cSRoman Divacky // If this is a function with a body, remember the prototype we are 1499f22ef01cSRoman Divacky // creating now, so that we can match up the body with them later. 1500f22ef01cSRoman Divacky if (!isProto) 1501f22ef01cSRoman Divacky FunctionsWithBodies.push_back(Func); 1502f22ef01cSRoman Divacky break; 1503f22ef01cSRoman Divacky } 1504f22ef01cSRoman Divacky // ALIAS: [alias type, aliasee val#, linkage] 1505f22ef01cSRoman Divacky // ALIAS: [alias type, aliasee val#, linkage, visibility] 1506f22ef01cSRoman Divacky case bitc::MODULE_CODE_ALIAS: { 1507f22ef01cSRoman Divacky if (Record.size() < 3) 1508f22ef01cSRoman Divacky return Error("Invalid MODULE_ALIAS record"); 1509f22ef01cSRoman Divacky const Type *Ty = getTypeByID(Record[0]); 1510f22ef01cSRoman Divacky if (!Ty->isPointerTy()) 1511f22ef01cSRoman Divacky return Error("Function not a pointer type!"); 1512f22ef01cSRoman Divacky 1513f22ef01cSRoman Divacky GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]), 1514f22ef01cSRoman Divacky "", 0, TheModule); 1515f22ef01cSRoman Divacky // Old bitcode files didn't have visibility field. 1516f22ef01cSRoman Divacky if (Record.size() > 3) 1517f22ef01cSRoman Divacky NewGA->setVisibility(GetDecodedVisibility(Record[3])); 1518f22ef01cSRoman Divacky ValueList.push_back(NewGA); 1519f22ef01cSRoman Divacky AliasInits.push_back(std::make_pair(NewGA, Record[1])); 1520f22ef01cSRoman Divacky break; 1521f22ef01cSRoman Divacky } 1522f22ef01cSRoman Divacky /// MODULE_CODE_PURGEVALS: [numvals] 1523f22ef01cSRoman Divacky case bitc::MODULE_CODE_PURGEVALS: 1524f22ef01cSRoman Divacky // Trim down the value list to the specified size. 1525f22ef01cSRoman Divacky if (Record.size() < 1 || Record[0] > ValueList.size()) 1526f22ef01cSRoman Divacky return Error("Invalid MODULE_PURGEVALS record"); 1527f22ef01cSRoman Divacky ValueList.shrinkTo(Record[0]); 1528f22ef01cSRoman Divacky break; 1529f22ef01cSRoman Divacky } 1530f22ef01cSRoman Divacky Record.clear(); 1531f22ef01cSRoman Divacky } 1532f22ef01cSRoman Divacky 1533f22ef01cSRoman Divacky return Error("Premature end of bitstream"); 1534f22ef01cSRoman Divacky } 1535f22ef01cSRoman Divacky 1536f22ef01cSRoman Divacky bool BitcodeReader::ParseBitcodeInto(Module *M) { 1537f22ef01cSRoman Divacky TheModule = 0; 1538f22ef01cSRoman Divacky 1539f22ef01cSRoman Divacky unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); 1540f22ef01cSRoman Divacky unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); 1541f22ef01cSRoman Divacky 1542f22ef01cSRoman Divacky if (Buffer->getBufferSize() & 3) { 1543f22ef01cSRoman Divacky if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd)) 1544f22ef01cSRoman Divacky return Error("Invalid bitcode signature"); 1545f22ef01cSRoman Divacky else 1546f22ef01cSRoman Divacky return Error("Bitcode stream should be a multiple of 4 bytes in length"); 1547f22ef01cSRoman Divacky } 1548f22ef01cSRoman Divacky 1549f22ef01cSRoman Divacky // If we have a wrapper header, parse it and ignore the non-bc file contents. 1550f22ef01cSRoman Divacky // The magic number is 0x0B17C0DE stored in little endian. 1551f22ef01cSRoman Divacky if (isBitcodeWrapper(BufPtr, BufEnd)) 1552f22ef01cSRoman Divacky if (SkipBitcodeWrapperHeader(BufPtr, BufEnd)) 1553f22ef01cSRoman Divacky return Error("Invalid bitcode wrapper header"); 1554f22ef01cSRoman Divacky 1555f22ef01cSRoman Divacky StreamFile.init(BufPtr, BufEnd); 1556f22ef01cSRoman Divacky Stream.init(StreamFile); 1557f22ef01cSRoman Divacky 1558f22ef01cSRoman Divacky // Sniff for the signature. 1559f22ef01cSRoman Divacky if (Stream.Read(8) != 'B' || 1560f22ef01cSRoman Divacky Stream.Read(8) != 'C' || 1561f22ef01cSRoman Divacky Stream.Read(4) != 0x0 || 1562f22ef01cSRoman Divacky Stream.Read(4) != 0xC || 1563f22ef01cSRoman Divacky Stream.Read(4) != 0xE || 1564f22ef01cSRoman Divacky Stream.Read(4) != 0xD) 1565f22ef01cSRoman Divacky return Error("Invalid bitcode signature"); 1566f22ef01cSRoman Divacky 1567f22ef01cSRoman Divacky // We expect a number of well-defined blocks, though we don't necessarily 1568f22ef01cSRoman Divacky // need to understand them all. 1569f22ef01cSRoman Divacky while (!Stream.AtEndOfStream()) { 1570f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 1571f22ef01cSRoman Divacky 1572f22ef01cSRoman Divacky if (Code != bitc::ENTER_SUBBLOCK) 1573f22ef01cSRoman Divacky return Error("Invalid record at top-level"); 1574f22ef01cSRoman Divacky 1575f22ef01cSRoman Divacky unsigned BlockID = Stream.ReadSubBlockID(); 1576f22ef01cSRoman Divacky 1577f22ef01cSRoman Divacky // We only know the MODULE subblock ID. 1578f22ef01cSRoman Divacky switch (BlockID) { 1579f22ef01cSRoman Divacky case bitc::BLOCKINFO_BLOCK_ID: 1580f22ef01cSRoman Divacky if (Stream.ReadBlockInfoBlock()) 1581f22ef01cSRoman Divacky return Error("Malformed BlockInfoBlock"); 1582f22ef01cSRoman Divacky break; 1583f22ef01cSRoman Divacky case bitc::MODULE_BLOCK_ID: 1584f22ef01cSRoman Divacky // Reject multiple MODULE_BLOCK's in a single bitstream. 1585f22ef01cSRoman Divacky if (TheModule) 1586f22ef01cSRoman Divacky return Error("Multiple MODULE_BLOCKs in same stream"); 1587f22ef01cSRoman Divacky TheModule = M; 1588f22ef01cSRoman Divacky if (ParseModule()) 1589f22ef01cSRoman Divacky return true; 1590f22ef01cSRoman Divacky break; 1591f22ef01cSRoman Divacky default: 1592f22ef01cSRoman Divacky if (Stream.SkipBlock()) 1593f22ef01cSRoman Divacky return Error("Malformed block record"); 1594f22ef01cSRoman Divacky break; 1595f22ef01cSRoman Divacky } 1596f22ef01cSRoman Divacky } 1597f22ef01cSRoman Divacky 1598f22ef01cSRoman Divacky return false; 1599f22ef01cSRoman Divacky } 1600f22ef01cSRoman Divacky 1601f22ef01cSRoman Divacky /// ParseMetadataAttachment - Parse metadata attachments. 1602f22ef01cSRoman Divacky bool BitcodeReader::ParseMetadataAttachment() { 1603f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) 1604f22ef01cSRoman Divacky return Error("Malformed block record"); 1605f22ef01cSRoman Divacky 1606f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1607f22ef01cSRoman Divacky while(1) { 1608f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 1609f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 1610f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 1611f22ef01cSRoman Divacky return Error("Error at end of PARAMATTR block"); 1612f22ef01cSRoman Divacky break; 1613f22ef01cSRoman Divacky } 1614f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 1615f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 1616f22ef01cSRoman Divacky continue; 1617f22ef01cSRoman Divacky } 1618f22ef01cSRoman Divacky // Read a metadata attachment record. 1619f22ef01cSRoman Divacky Record.clear(); 1620f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 1621f22ef01cSRoman Divacky default: // Default behavior: ignore. 1622f22ef01cSRoman Divacky break; 1623e580952dSDimitry Andric // FIXME: Remove in LLVM 3.0. 1624e580952dSDimitry Andric case bitc::METADATA_ATTACHMENT: 1625e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 1626e580952dSDimitry Andric case bitc::METADATA_ATTACHMENT2: { 1627f22ef01cSRoman Divacky unsigned RecordLength = Record.size(); 1628f22ef01cSRoman Divacky if (Record.empty() || (RecordLength - 1) % 2 == 1) 1629f22ef01cSRoman Divacky return Error ("Invalid METADATA_ATTACHMENT reader!"); 1630f22ef01cSRoman Divacky Instruction *Inst = InstructionList[Record[0]]; 1631f22ef01cSRoman Divacky for (unsigned i = 1; i != RecordLength; i = i+2) { 1632f22ef01cSRoman Divacky unsigned Kind = Record[i]; 1633e580952dSDimitry Andric DenseMap<unsigned, unsigned>::iterator I = 1634e580952dSDimitry Andric MDKindMap.find(Kind); 1635e580952dSDimitry Andric if (I == MDKindMap.end()) 1636e580952dSDimitry Andric return Error("Invalid metadata kind ID"); 1637f22ef01cSRoman Divacky Value *Node = MDValueList.getValueFwdRef(Record[i+1]); 1638e580952dSDimitry Andric Inst->setMetadata(I->second, cast<MDNode>(Node)); 1639f22ef01cSRoman Divacky } 1640f22ef01cSRoman Divacky break; 1641f22ef01cSRoman Divacky } 1642f22ef01cSRoman Divacky } 1643f22ef01cSRoman Divacky } 1644f22ef01cSRoman Divacky return false; 1645f22ef01cSRoman Divacky } 1646f22ef01cSRoman Divacky 1647f22ef01cSRoman Divacky /// ParseFunctionBody - Lazily parse the specified function body block. 1648f22ef01cSRoman Divacky bool BitcodeReader::ParseFunctionBody(Function *F) { 1649f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) 1650f22ef01cSRoman Divacky return Error("Malformed block record"); 1651f22ef01cSRoman Divacky 1652f22ef01cSRoman Divacky InstructionList.clear(); 1653f22ef01cSRoman Divacky unsigned ModuleValueListSize = ValueList.size(); 1654e580952dSDimitry Andric unsigned ModuleMDValueListSize = MDValueList.size(); 1655f22ef01cSRoman Divacky 1656f22ef01cSRoman Divacky // Add all the function arguments to the value table. 1657f22ef01cSRoman Divacky for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) 1658f22ef01cSRoman Divacky ValueList.push_back(I); 1659f22ef01cSRoman Divacky 1660f22ef01cSRoman Divacky unsigned NextValueNo = ValueList.size(); 1661f22ef01cSRoman Divacky BasicBlock *CurBB = 0; 1662f22ef01cSRoman Divacky unsigned CurBBNo = 0; 1663f22ef01cSRoman Divacky 1664f22ef01cSRoman Divacky DebugLoc LastLoc; 1665f22ef01cSRoman Divacky 1666f22ef01cSRoman Divacky // Read all the records. 1667f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1668f22ef01cSRoman Divacky while (1) { 1669f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 1670f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 1671f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 1672f22ef01cSRoman Divacky return Error("Error at end of function block"); 1673f22ef01cSRoman Divacky break; 1674f22ef01cSRoman Divacky } 1675f22ef01cSRoman Divacky 1676f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 1677f22ef01cSRoman Divacky switch (Stream.ReadSubBlockID()) { 1678f22ef01cSRoman Divacky default: // Skip unknown content. 1679f22ef01cSRoman Divacky if (Stream.SkipBlock()) 1680f22ef01cSRoman Divacky return Error("Malformed block record"); 1681f22ef01cSRoman Divacky break; 1682f22ef01cSRoman Divacky case bitc::CONSTANTS_BLOCK_ID: 1683f22ef01cSRoman Divacky if (ParseConstants()) return true; 1684f22ef01cSRoman Divacky NextValueNo = ValueList.size(); 1685f22ef01cSRoman Divacky break; 1686f22ef01cSRoman Divacky case bitc::VALUE_SYMTAB_BLOCK_ID: 1687f22ef01cSRoman Divacky if (ParseValueSymbolTable()) return true; 1688f22ef01cSRoman Divacky break; 1689f22ef01cSRoman Divacky case bitc::METADATA_ATTACHMENT_ID: 1690f22ef01cSRoman Divacky if (ParseMetadataAttachment()) return true; 1691f22ef01cSRoman Divacky break; 1692f22ef01cSRoman Divacky case bitc::METADATA_BLOCK_ID: 1693f22ef01cSRoman Divacky if (ParseMetadata()) return true; 1694f22ef01cSRoman Divacky break; 1695f22ef01cSRoman Divacky } 1696f22ef01cSRoman Divacky continue; 1697f22ef01cSRoman Divacky } 1698f22ef01cSRoman Divacky 1699f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 1700f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 1701f22ef01cSRoman Divacky continue; 1702f22ef01cSRoman Divacky } 1703f22ef01cSRoman Divacky 1704f22ef01cSRoman Divacky // Read a record. 1705f22ef01cSRoman Divacky Record.clear(); 1706f22ef01cSRoman Divacky Instruction *I = 0; 1707f22ef01cSRoman Divacky unsigned BitCode = Stream.ReadRecord(Code, Record); 1708f22ef01cSRoman Divacky switch (BitCode) { 1709f22ef01cSRoman Divacky default: // Default behavior: reject 1710f22ef01cSRoman Divacky return Error("Unknown instruction"); 1711f22ef01cSRoman Divacky case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks] 1712f22ef01cSRoman Divacky if (Record.size() < 1 || Record[0] == 0) 1713f22ef01cSRoman Divacky return Error("Invalid DECLAREBLOCKS record"); 1714f22ef01cSRoman Divacky // Create all the basic blocks for the function. 1715f22ef01cSRoman Divacky FunctionBBs.resize(Record[0]); 1716f22ef01cSRoman Divacky for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) 1717f22ef01cSRoman Divacky FunctionBBs[i] = BasicBlock::Create(Context, "", F); 1718f22ef01cSRoman Divacky CurBB = FunctionBBs[0]; 1719f22ef01cSRoman Divacky continue; 1720f22ef01cSRoman Divacky 1721f22ef01cSRoman Divacky 1722f22ef01cSRoman Divacky case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN 1723f22ef01cSRoman Divacky // This record indicates that the last instruction is at the same 1724f22ef01cSRoman Divacky // location as the previous instruction with a location. 1725f22ef01cSRoman Divacky I = 0; 1726f22ef01cSRoman Divacky 1727f22ef01cSRoman Divacky // Get the last instruction emitted. 1728f22ef01cSRoman Divacky if (CurBB && !CurBB->empty()) 1729f22ef01cSRoman Divacky I = &CurBB->back(); 1730f22ef01cSRoman Divacky else if (CurBBNo && FunctionBBs[CurBBNo-1] && 1731f22ef01cSRoman Divacky !FunctionBBs[CurBBNo-1]->empty()) 1732f22ef01cSRoman Divacky I = &FunctionBBs[CurBBNo-1]->back(); 1733f22ef01cSRoman Divacky 1734f22ef01cSRoman Divacky if (I == 0) return Error("Invalid DEBUG_LOC_AGAIN record"); 1735f22ef01cSRoman Divacky I->setDebugLoc(LastLoc); 1736f22ef01cSRoman Divacky I = 0; 1737f22ef01cSRoman Divacky continue; 1738f22ef01cSRoman Divacky 1739e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 1740e580952dSDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC: 1741e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 1742e580952dSDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC2: { // DEBUG_LOC: [line, col, scope, ia] 1743f22ef01cSRoman Divacky I = 0; // Get the last instruction emitted. 1744f22ef01cSRoman Divacky if (CurBB && !CurBB->empty()) 1745f22ef01cSRoman Divacky I = &CurBB->back(); 1746f22ef01cSRoman Divacky else if (CurBBNo && FunctionBBs[CurBBNo-1] && 1747f22ef01cSRoman Divacky !FunctionBBs[CurBBNo-1]->empty()) 1748f22ef01cSRoman Divacky I = &FunctionBBs[CurBBNo-1]->back(); 1749f22ef01cSRoman Divacky if (I == 0 || Record.size() < 4) 1750f22ef01cSRoman Divacky return Error("Invalid FUNC_CODE_DEBUG_LOC record"); 1751f22ef01cSRoman Divacky 1752f22ef01cSRoman Divacky unsigned Line = Record[0], Col = Record[1]; 1753f22ef01cSRoman Divacky unsigned ScopeID = Record[2], IAID = Record[3]; 1754f22ef01cSRoman Divacky 1755f22ef01cSRoman Divacky MDNode *Scope = 0, *IA = 0; 1756f22ef01cSRoman Divacky if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1)); 1757f22ef01cSRoman Divacky if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1)); 1758f22ef01cSRoman Divacky LastLoc = DebugLoc::get(Line, Col, Scope, IA); 1759f22ef01cSRoman Divacky I->setDebugLoc(LastLoc); 1760f22ef01cSRoman Divacky I = 0; 1761f22ef01cSRoman Divacky continue; 1762f22ef01cSRoman Divacky } 1763f22ef01cSRoman Divacky 1764f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] 1765f22ef01cSRoman Divacky unsigned OpNum = 0; 1766f22ef01cSRoman Divacky Value *LHS, *RHS; 1767f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 1768f22ef01cSRoman Divacky getValue(Record, OpNum, LHS->getType(), RHS) || 1769f22ef01cSRoman Divacky OpNum+1 > Record.size()) 1770f22ef01cSRoman Divacky return Error("Invalid BINOP record"); 1771f22ef01cSRoman Divacky 1772f22ef01cSRoman Divacky int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); 1773f22ef01cSRoman Divacky if (Opc == -1) return Error("Invalid BINOP record"); 1774f22ef01cSRoman Divacky I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 1775f22ef01cSRoman Divacky InstructionList.push_back(I); 1776f22ef01cSRoman Divacky if (OpNum < Record.size()) { 1777f22ef01cSRoman Divacky if (Opc == Instruction::Add || 1778f22ef01cSRoman Divacky Opc == Instruction::Sub || 1779f22ef01cSRoman Divacky Opc == Instruction::Mul) { 1780f22ef01cSRoman Divacky if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 1781f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setHasNoSignedWrap(true); 1782f22ef01cSRoman Divacky if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 1783f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); 1784f22ef01cSRoman Divacky } else if (Opc == Instruction::SDiv) { 1785f22ef01cSRoman Divacky if (Record[OpNum] & (1 << bitc::SDIV_EXACT)) 1786f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setIsExact(true); 1787f22ef01cSRoman Divacky } 1788f22ef01cSRoman Divacky } 1789f22ef01cSRoman Divacky break; 1790f22ef01cSRoman Divacky } 1791f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] 1792f22ef01cSRoman Divacky unsigned OpNum = 0; 1793f22ef01cSRoman Divacky Value *Op; 1794f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 1795f22ef01cSRoman Divacky OpNum+2 != Record.size()) 1796f22ef01cSRoman Divacky return Error("Invalid CAST record"); 1797f22ef01cSRoman Divacky 1798f22ef01cSRoman Divacky const Type *ResTy = getTypeByID(Record[OpNum]); 1799f22ef01cSRoman Divacky int Opc = GetDecodedCastOpcode(Record[OpNum+1]); 1800f22ef01cSRoman Divacky if (Opc == -1 || ResTy == 0) 1801f22ef01cSRoman Divacky return Error("Invalid CAST record"); 1802f22ef01cSRoman Divacky I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy); 1803f22ef01cSRoman Divacky InstructionList.push_back(I); 1804f22ef01cSRoman Divacky break; 1805f22ef01cSRoman Divacky } 1806f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INBOUNDS_GEP: 1807f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] 1808f22ef01cSRoman Divacky unsigned OpNum = 0; 1809f22ef01cSRoman Divacky Value *BasePtr; 1810f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) 1811f22ef01cSRoman Divacky return Error("Invalid GEP record"); 1812f22ef01cSRoman Divacky 1813f22ef01cSRoman Divacky SmallVector<Value*, 16> GEPIdx; 1814f22ef01cSRoman Divacky while (OpNum != Record.size()) { 1815f22ef01cSRoman Divacky Value *Op; 1816f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 1817f22ef01cSRoman Divacky return Error("Invalid GEP record"); 1818f22ef01cSRoman Divacky GEPIdx.push_back(Op); 1819f22ef01cSRoman Divacky } 1820f22ef01cSRoman Divacky 1821f22ef01cSRoman Divacky I = GetElementPtrInst::Create(BasePtr, GEPIdx.begin(), GEPIdx.end()); 1822f22ef01cSRoman Divacky InstructionList.push_back(I); 1823f22ef01cSRoman Divacky if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP) 1824f22ef01cSRoman Divacky cast<GetElementPtrInst>(I)->setIsInBounds(true); 1825f22ef01cSRoman Divacky break; 1826f22ef01cSRoman Divacky } 1827f22ef01cSRoman Divacky 1828f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_EXTRACTVAL: { 1829f22ef01cSRoman Divacky // EXTRACTVAL: [opty, opval, n x indices] 1830f22ef01cSRoman Divacky unsigned OpNum = 0; 1831f22ef01cSRoman Divacky Value *Agg; 1832f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 1833f22ef01cSRoman Divacky return Error("Invalid EXTRACTVAL record"); 1834f22ef01cSRoman Divacky 1835f22ef01cSRoman Divacky SmallVector<unsigned, 4> EXTRACTVALIdx; 1836f22ef01cSRoman Divacky for (unsigned RecSize = Record.size(); 1837f22ef01cSRoman Divacky OpNum != RecSize; ++OpNum) { 1838f22ef01cSRoman Divacky uint64_t Index = Record[OpNum]; 1839f22ef01cSRoman Divacky if ((unsigned)Index != Index) 1840f22ef01cSRoman Divacky return Error("Invalid EXTRACTVAL index"); 1841f22ef01cSRoman Divacky EXTRACTVALIdx.push_back((unsigned)Index); 1842f22ef01cSRoman Divacky } 1843f22ef01cSRoman Divacky 1844f22ef01cSRoman Divacky I = ExtractValueInst::Create(Agg, 1845f22ef01cSRoman Divacky EXTRACTVALIdx.begin(), EXTRACTVALIdx.end()); 1846f22ef01cSRoman Divacky InstructionList.push_back(I); 1847f22ef01cSRoman Divacky break; 1848f22ef01cSRoman Divacky } 1849f22ef01cSRoman Divacky 1850f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INSERTVAL: { 1851f22ef01cSRoman Divacky // INSERTVAL: [opty, opval, opty, opval, n x indices] 1852f22ef01cSRoman Divacky unsigned OpNum = 0; 1853f22ef01cSRoman Divacky Value *Agg; 1854f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 1855f22ef01cSRoman Divacky return Error("Invalid INSERTVAL record"); 1856f22ef01cSRoman Divacky Value *Val; 1857f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Val)) 1858f22ef01cSRoman Divacky return Error("Invalid INSERTVAL record"); 1859f22ef01cSRoman Divacky 1860f22ef01cSRoman Divacky SmallVector<unsigned, 4> INSERTVALIdx; 1861f22ef01cSRoman Divacky for (unsigned RecSize = Record.size(); 1862f22ef01cSRoman Divacky OpNum != RecSize; ++OpNum) { 1863f22ef01cSRoman Divacky uint64_t Index = Record[OpNum]; 1864f22ef01cSRoman Divacky if ((unsigned)Index != Index) 1865f22ef01cSRoman Divacky return Error("Invalid INSERTVAL index"); 1866f22ef01cSRoman Divacky INSERTVALIdx.push_back((unsigned)Index); 1867f22ef01cSRoman Divacky } 1868f22ef01cSRoman Divacky 1869f22ef01cSRoman Divacky I = InsertValueInst::Create(Agg, Val, 1870f22ef01cSRoman Divacky INSERTVALIdx.begin(), INSERTVALIdx.end()); 1871f22ef01cSRoman Divacky InstructionList.push_back(I); 1872f22ef01cSRoman Divacky break; 1873f22ef01cSRoman Divacky } 1874f22ef01cSRoman Divacky 1875f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] 1876f22ef01cSRoman Divacky // obsolete form of select 1877f22ef01cSRoman Divacky // handles select i1 ... in old bitcode 1878f22ef01cSRoman Divacky unsigned OpNum = 0; 1879f22ef01cSRoman Divacky Value *TrueVal, *FalseVal, *Cond; 1880f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 1881f22ef01cSRoman Divacky getValue(Record, OpNum, TrueVal->getType(), FalseVal) || 1882f22ef01cSRoman Divacky getValue(Record, OpNum, Type::getInt1Ty(Context), Cond)) 1883f22ef01cSRoman Divacky return Error("Invalid SELECT record"); 1884f22ef01cSRoman Divacky 1885f22ef01cSRoman Divacky I = SelectInst::Create(Cond, TrueVal, FalseVal); 1886f22ef01cSRoman Divacky InstructionList.push_back(I); 1887f22ef01cSRoman Divacky break; 1888f22ef01cSRoman Divacky } 1889f22ef01cSRoman Divacky 1890f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] 1891f22ef01cSRoman Divacky // new form of select 1892f22ef01cSRoman Divacky // handles select i1 or select [N x i1] 1893f22ef01cSRoman Divacky unsigned OpNum = 0; 1894f22ef01cSRoman Divacky Value *TrueVal, *FalseVal, *Cond; 1895f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 1896f22ef01cSRoman Divacky getValue(Record, OpNum, TrueVal->getType(), FalseVal) || 1897f22ef01cSRoman Divacky getValueTypePair(Record, OpNum, NextValueNo, Cond)) 1898f22ef01cSRoman Divacky return Error("Invalid SELECT record"); 1899f22ef01cSRoman Divacky 1900f22ef01cSRoman Divacky // select condition can be either i1 or [N x i1] 1901f22ef01cSRoman Divacky if (const VectorType* vector_type = 1902f22ef01cSRoman Divacky dyn_cast<const VectorType>(Cond->getType())) { 1903f22ef01cSRoman Divacky // expect <n x i1> 1904f22ef01cSRoman Divacky if (vector_type->getElementType() != Type::getInt1Ty(Context)) 1905f22ef01cSRoman Divacky return Error("Invalid SELECT condition type"); 1906f22ef01cSRoman Divacky } else { 1907f22ef01cSRoman Divacky // expect i1 1908f22ef01cSRoman Divacky if (Cond->getType() != Type::getInt1Ty(Context)) 1909f22ef01cSRoman Divacky return Error("Invalid SELECT condition type"); 1910f22ef01cSRoman Divacky } 1911f22ef01cSRoman Divacky 1912f22ef01cSRoman Divacky I = SelectInst::Create(Cond, TrueVal, FalseVal); 1913f22ef01cSRoman Divacky InstructionList.push_back(I); 1914f22ef01cSRoman Divacky break; 1915f22ef01cSRoman Divacky } 1916f22ef01cSRoman Divacky 1917f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] 1918f22ef01cSRoman Divacky unsigned OpNum = 0; 1919f22ef01cSRoman Divacky Value *Vec, *Idx; 1920f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || 1921f22ef01cSRoman Divacky getValue(Record, OpNum, Type::getInt32Ty(Context), Idx)) 1922f22ef01cSRoman Divacky return Error("Invalid EXTRACTELT record"); 1923f22ef01cSRoman Divacky I = ExtractElementInst::Create(Vec, Idx); 1924f22ef01cSRoman Divacky InstructionList.push_back(I); 1925f22ef01cSRoman Divacky break; 1926f22ef01cSRoman Divacky } 1927f22ef01cSRoman Divacky 1928f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] 1929f22ef01cSRoman Divacky unsigned OpNum = 0; 1930f22ef01cSRoman Divacky Value *Vec, *Elt, *Idx; 1931f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || 1932f22ef01cSRoman Divacky getValue(Record, OpNum, 1933f22ef01cSRoman Divacky cast<VectorType>(Vec->getType())->getElementType(), Elt) || 1934f22ef01cSRoman Divacky getValue(Record, OpNum, Type::getInt32Ty(Context), Idx)) 1935f22ef01cSRoman Divacky return Error("Invalid INSERTELT record"); 1936f22ef01cSRoman Divacky I = InsertElementInst::Create(Vec, Elt, Idx); 1937f22ef01cSRoman Divacky InstructionList.push_back(I); 1938f22ef01cSRoman Divacky break; 1939f22ef01cSRoman Divacky } 1940f22ef01cSRoman Divacky 1941f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] 1942f22ef01cSRoman Divacky unsigned OpNum = 0; 1943f22ef01cSRoman Divacky Value *Vec1, *Vec2, *Mask; 1944f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || 1945f22ef01cSRoman Divacky getValue(Record, OpNum, Vec1->getType(), Vec2)) 1946f22ef01cSRoman Divacky return Error("Invalid SHUFFLEVEC record"); 1947f22ef01cSRoman Divacky 1948f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) 1949f22ef01cSRoman Divacky return Error("Invalid SHUFFLEVEC record"); 1950f22ef01cSRoman Divacky I = new ShuffleVectorInst(Vec1, Vec2, Mask); 1951f22ef01cSRoman Divacky InstructionList.push_back(I); 1952f22ef01cSRoman Divacky break; 1953f22ef01cSRoman Divacky } 1954f22ef01cSRoman Divacky 1955f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] 1956f22ef01cSRoman Divacky // Old form of ICmp/FCmp returning bool 1957f22ef01cSRoman Divacky // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were 1958f22ef01cSRoman Divacky // both legal on vectors but had different behaviour. 1959f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] 1960f22ef01cSRoman Divacky // FCmp/ICmp returning bool or vector of bool 1961f22ef01cSRoman Divacky 1962f22ef01cSRoman Divacky unsigned OpNum = 0; 1963f22ef01cSRoman Divacky Value *LHS, *RHS; 1964f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 1965f22ef01cSRoman Divacky getValue(Record, OpNum, LHS->getType(), RHS) || 1966f22ef01cSRoman Divacky OpNum+1 != Record.size()) 1967f22ef01cSRoman Divacky return Error("Invalid CMP record"); 1968f22ef01cSRoman Divacky 1969f22ef01cSRoman Divacky if (LHS->getType()->isFPOrFPVectorTy()) 1970f22ef01cSRoman Divacky I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); 1971f22ef01cSRoman Divacky else 1972f22ef01cSRoman Divacky I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS); 1973f22ef01cSRoman Divacky InstructionList.push_back(I); 1974f22ef01cSRoman Divacky break; 1975f22ef01cSRoman Divacky } 1976f22ef01cSRoman Divacky 1977f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_GETRESULT: { // GETRESULT: [ty, val, n] 1978f22ef01cSRoman Divacky if (Record.size() != 2) 1979f22ef01cSRoman Divacky return Error("Invalid GETRESULT record"); 1980f22ef01cSRoman Divacky unsigned OpNum = 0; 1981f22ef01cSRoman Divacky Value *Op; 1982f22ef01cSRoman Divacky getValueTypePair(Record, OpNum, NextValueNo, Op); 1983f22ef01cSRoman Divacky unsigned Index = Record[1]; 1984f22ef01cSRoman Divacky I = ExtractValueInst::Create(Op, Index); 1985f22ef01cSRoman Divacky InstructionList.push_back(I); 1986f22ef01cSRoman Divacky break; 1987f22ef01cSRoman Divacky } 1988f22ef01cSRoman Divacky 1989f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] 1990f22ef01cSRoman Divacky { 1991f22ef01cSRoman Divacky unsigned Size = Record.size(); 1992f22ef01cSRoman Divacky if (Size == 0) { 1993f22ef01cSRoman Divacky I = ReturnInst::Create(Context); 1994f22ef01cSRoman Divacky InstructionList.push_back(I); 1995f22ef01cSRoman Divacky break; 1996f22ef01cSRoman Divacky } 1997f22ef01cSRoman Divacky 1998f22ef01cSRoman Divacky unsigned OpNum = 0; 1999f22ef01cSRoman Divacky SmallVector<Value *,4> Vs; 2000f22ef01cSRoman Divacky do { 2001f22ef01cSRoman Divacky Value *Op = NULL; 2002f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 2003f22ef01cSRoman Divacky return Error("Invalid RET record"); 2004f22ef01cSRoman Divacky Vs.push_back(Op); 2005f22ef01cSRoman Divacky } while(OpNum != Record.size()); 2006f22ef01cSRoman Divacky 2007f22ef01cSRoman Divacky const Type *ReturnType = F->getReturnType(); 2008e580952dSDimitry Andric // Handle multiple return values. FIXME: Remove in LLVM 3.0. 2009f22ef01cSRoman Divacky if (Vs.size() > 1 || 2010f22ef01cSRoman Divacky (ReturnType->isStructTy() && 2011f22ef01cSRoman Divacky (Vs.empty() || Vs[0]->getType() != ReturnType))) { 2012f22ef01cSRoman Divacky Value *RV = UndefValue::get(ReturnType); 2013f22ef01cSRoman Divacky for (unsigned i = 0, e = Vs.size(); i != e; ++i) { 2014f22ef01cSRoman Divacky I = InsertValueInst::Create(RV, Vs[i], i, "mrv"); 2015f22ef01cSRoman Divacky InstructionList.push_back(I); 2016f22ef01cSRoman Divacky CurBB->getInstList().push_back(I); 2017f22ef01cSRoman Divacky ValueList.AssignValue(I, NextValueNo++); 2018f22ef01cSRoman Divacky RV = I; 2019f22ef01cSRoman Divacky } 2020f22ef01cSRoman Divacky I = ReturnInst::Create(Context, RV); 2021f22ef01cSRoman Divacky InstructionList.push_back(I); 2022f22ef01cSRoman Divacky break; 2023f22ef01cSRoman Divacky } 2024f22ef01cSRoman Divacky 2025f22ef01cSRoman Divacky I = ReturnInst::Create(Context, Vs[0]); 2026f22ef01cSRoman Divacky InstructionList.push_back(I); 2027f22ef01cSRoman Divacky break; 2028f22ef01cSRoman Divacky } 2029f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] 2030f22ef01cSRoman Divacky if (Record.size() != 1 && Record.size() != 3) 2031f22ef01cSRoman Divacky return Error("Invalid BR record"); 2032f22ef01cSRoman Divacky BasicBlock *TrueDest = getBasicBlock(Record[0]); 2033f22ef01cSRoman Divacky if (TrueDest == 0) 2034f22ef01cSRoman Divacky return Error("Invalid BR record"); 2035f22ef01cSRoman Divacky 2036f22ef01cSRoman Divacky if (Record.size() == 1) { 2037f22ef01cSRoman Divacky I = BranchInst::Create(TrueDest); 2038f22ef01cSRoman Divacky InstructionList.push_back(I); 2039f22ef01cSRoman Divacky } 2040f22ef01cSRoman Divacky else { 2041f22ef01cSRoman Divacky BasicBlock *FalseDest = getBasicBlock(Record[1]); 2042f22ef01cSRoman Divacky Value *Cond = getFnValueByID(Record[2], Type::getInt1Ty(Context)); 2043f22ef01cSRoman Divacky if (FalseDest == 0 || Cond == 0) 2044f22ef01cSRoman Divacky return Error("Invalid BR record"); 2045f22ef01cSRoman Divacky I = BranchInst::Create(TrueDest, FalseDest, Cond); 2046f22ef01cSRoman Divacky InstructionList.push_back(I); 2047f22ef01cSRoman Divacky } 2048f22ef01cSRoman Divacky break; 2049f22ef01cSRoman Divacky } 2050f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] 2051f22ef01cSRoman Divacky if (Record.size() < 3 || (Record.size() & 1) == 0) 2052f22ef01cSRoman Divacky return Error("Invalid SWITCH record"); 2053f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[0]); 2054f22ef01cSRoman Divacky Value *Cond = getFnValueByID(Record[1], OpTy); 2055f22ef01cSRoman Divacky BasicBlock *Default = getBasicBlock(Record[2]); 2056f22ef01cSRoman Divacky if (OpTy == 0 || Cond == 0 || Default == 0) 2057f22ef01cSRoman Divacky return Error("Invalid SWITCH record"); 2058f22ef01cSRoman Divacky unsigned NumCases = (Record.size()-3)/2; 2059f22ef01cSRoman Divacky SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 2060f22ef01cSRoman Divacky InstructionList.push_back(SI); 2061f22ef01cSRoman Divacky for (unsigned i = 0, e = NumCases; i != e; ++i) { 2062f22ef01cSRoman Divacky ConstantInt *CaseVal = 2063f22ef01cSRoman Divacky dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); 2064f22ef01cSRoman Divacky BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); 2065f22ef01cSRoman Divacky if (CaseVal == 0 || DestBB == 0) { 2066f22ef01cSRoman Divacky delete SI; 2067f22ef01cSRoman Divacky return Error("Invalid SWITCH record!"); 2068f22ef01cSRoman Divacky } 2069f22ef01cSRoman Divacky SI->addCase(CaseVal, DestBB); 2070f22ef01cSRoman Divacky } 2071f22ef01cSRoman Divacky I = SI; 2072f22ef01cSRoman Divacky break; 2073f22ef01cSRoman Divacky } 2074f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] 2075f22ef01cSRoman Divacky if (Record.size() < 2) 2076f22ef01cSRoman Divacky return Error("Invalid INDIRECTBR record"); 2077f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[0]); 2078f22ef01cSRoman Divacky Value *Address = getFnValueByID(Record[1], OpTy); 2079f22ef01cSRoman Divacky if (OpTy == 0 || Address == 0) 2080f22ef01cSRoman Divacky return Error("Invalid INDIRECTBR record"); 2081f22ef01cSRoman Divacky unsigned NumDests = Record.size()-2; 2082f22ef01cSRoman Divacky IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); 2083f22ef01cSRoman Divacky InstructionList.push_back(IBI); 2084f22ef01cSRoman Divacky for (unsigned i = 0, e = NumDests; i != e; ++i) { 2085f22ef01cSRoman Divacky if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { 2086f22ef01cSRoman Divacky IBI->addDestination(DestBB); 2087f22ef01cSRoman Divacky } else { 2088f22ef01cSRoman Divacky delete IBI; 2089f22ef01cSRoman Divacky return Error("Invalid INDIRECTBR record!"); 2090f22ef01cSRoman Divacky } 2091f22ef01cSRoman Divacky } 2092f22ef01cSRoman Divacky I = IBI; 2093f22ef01cSRoman Divacky break; 2094f22ef01cSRoman Divacky } 2095f22ef01cSRoman Divacky 2096f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INVOKE: { 2097f22ef01cSRoman Divacky // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] 2098f22ef01cSRoman Divacky if (Record.size() < 4) return Error("Invalid INVOKE record"); 2099f22ef01cSRoman Divacky AttrListPtr PAL = getAttributes(Record[0]); 2100f22ef01cSRoman Divacky unsigned CCInfo = Record[1]; 2101f22ef01cSRoman Divacky BasicBlock *NormalBB = getBasicBlock(Record[2]); 2102f22ef01cSRoman Divacky BasicBlock *UnwindBB = getBasicBlock(Record[3]); 2103f22ef01cSRoman Divacky 2104f22ef01cSRoman Divacky unsigned OpNum = 4; 2105f22ef01cSRoman Divacky Value *Callee; 2106f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 2107f22ef01cSRoman Divacky return Error("Invalid INVOKE record"); 2108f22ef01cSRoman Divacky 2109f22ef01cSRoman Divacky const PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); 2110f22ef01cSRoman Divacky const FunctionType *FTy = !CalleeTy ? 0 : 2111f22ef01cSRoman Divacky dyn_cast<FunctionType>(CalleeTy->getElementType()); 2112f22ef01cSRoman Divacky 2113f22ef01cSRoman Divacky // Check that the right number of fixed parameters are here. 2114f22ef01cSRoman Divacky if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 || 2115f22ef01cSRoman Divacky Record.size() < OpNum+FTy->getNumParams()) 2116f22ef01cSRoman Divacky return Error("Invalid INVOKE record"); 2117f22ef01cSRoman Divacky 2118f22ef01cSRoman Divacky SmallVector<Value*, 16> Ops; 2119f22ef01cSRoman Divacky for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 2120f22ef01cSRoman Divacky Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); 2121f22ef01cSRoman Divacky if (Ops.back() == 0) return Error("Invalid INVOKE record"); 2122f22ef01cSRoman Divacky } 2123f22ef01cSRoman Divacky 2124f22ef01cSRoman Divacky if (!FTy->isVarArg()) { 2125f22ef01cSRoman Divacky if (Record.size() != OpNum) 2126f22ef01cSRoman Divacky return Error("Invalid INVOKE record"); 2127f22ef01cSRoman Divacky } else { 2128f22ef01cSRoman Divacky // Read type/value pairs for varargs params. 2129f22ef01cSRoman Divacky while (OpNum != Record.size()) { 2130f22ef01cSRoman Divacky Value *Op; 2131f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 2132f22ef01cSRoman Divacky return Error("Invalid INVOKE record"); 2133f22ef01cSRoman Divacky Ops.push_back(Op); 2134f22ef01cSRoman Divacky } 2135f22ef01cSRoman Divacky } 2136f22ef01cSRoman Divacky 2137f22ef01cSRoman Divacky I = InvokeInst::Create(Callee, NormalBB, UnwindBB, 2138f22ef01cSRoman Divacky Ops.begin(), Ops.end()); 2139f22ef01cSRoman Divacky InstructionList.push_back(I); 2140f22ef01cSRoman Divacky cast<InvokeInst>(I)->setCallingConv( 2141f22ef01cSRoman Divacky static_cast<CallingConv::ID>(CCInfo)); 2142f22ef01cSRoman Divacky cast<InvokeInst>(I)->setAttributes(PAL); 2143f22ef01cSRoman Divacky break; 2144f22ef01cSRoman Divacky } 2145f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_UNWIND: // UNWIND 2146f22ef01cSRoman Divacky I = new UnwindInst(Context); 2147f22ef01cSRoman Divacky InstructionList.push_back(I); 2148f22ef01cSRoman Divacky break; 2149f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE 2150f22ef01cSRoman Divacky I = new UnreachableInst(Context); 2151f22ef01cSRoman Divacky InstructionList.push_back(I); 2152f22ef01cSRoman Divacky break; 2153f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] 2154f22ef01cSRoman Divacky if (Record.size() < 1 || ((Record.size()-1)&1)) 2155f22ef01cSRoman Divacky return Error("Invalid PHI record"); 2156f22ef01cSRoman Divacky const Type *Ty = getTypeByID(Record[0]); 2157f22ef01cSRoman Divacky if (!Ty) return Error("Invalid PHI record"); 2158f22ef01cSRoman Divacky 2159f22ef01cSRoman Divacky PHINode *PN = PHINode::Create(Ty); 2160f22ef01cSRoman Divacky InstructionList.push_back(PN); 2161f22ef01cSRoman Divacky PN->reserveOperandSpace((Record.size()-1)/2); 2162f22ef01cSRoman Divacky 2163f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { 2164f22ef01cSRoman Divacky Value *V = getFnValueByID(Record[1+i], Ty); 2165f22ef01cSRoman Divacky BasicBlock *BB = getBasicBlock(Record[2+i]); 2166f22ef01cSRoman Divacky if (!V || !BB) return Error("Invalid PHI record"); 2167f22ef01cSRoman Divacky PN->addIncoming(V, BB); 2168f22ef01cSRoman Divacky } 2169f22ef01cSRoman Divacky I = PN; 2170f22ef01cSRoman Divacky break; 2171f22ef01cSRoman Divacky } 2172f22ef01cSRoman Divacky 2173f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_MALLOC: { // MALLOC: [instty, op, align] 2174f22ef01cSRoman Divacky // Autoupgrade malloc instruction to malloc call. 2175f22ef01cSRoman Divacky // FIXME: Remove in LLVM 3.0. 2176f22ef01cSRoman Divacky if (Record.size() < 3) 2177f22ef01cSRoman Divacky return Error("Invalid MALLOC record"); 2178f22ef01cSRoman Divacky const PointerType *Ty = 2179f22ef01cSRoman Divacky dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); 2180f22ef01cSRoman Divacky Value *Size = getFnValueByID(Record[1], Type::getInt32Ty(Context)); 2181f22ef01cSRoman Divacky if (!Ty || !Size) return Error("Invalid MALLOC record"); 2182f22ef01cSRoman Divacky if (!CurBB) return Error("Invalid malloc instruction with no BB"); 2183f22ef01cSRoman Divacky const Type *Int32Ty = IntegerType::getInt32Ty(CurBB->getContext()); 2184f22ef01cSRoman Divacky Constant *AllocSize = ConstantExpr::getSizeOf(Ty->getElementType()); 2185f22ef01cSRoman Divacky AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, Int32Ty); 2186f22ef01cSRoman Divacky I = CallInst::CreateMalloc(CurBB, Int32Ty, Ty->getElementType(), 2187f22ef01cSRoman Divacky AllocSize, Size, NULL); 2188f22ef01cSRoman Divacky InstructionList.push_back(I); 2189f22ef01cSRoman Divacky break; 2190f22ef01cSRoman Divacky } 2191f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_FREE: { // FREE: [op, opty] 2192f22ef01cSRoman Divacky unsigned OpNum = 0; 2193f22ef01cSRoman Divacky Value *Op; 2194f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 2195f22ef01cSRoman Divacky OpNum != Record.size()) 2196f22ef01cSRoman Divacky return Error("Invalid FREE record"); 2197f22ef01cSRoman Divacky if (!CurBB) return Error("Invalid free instruction with no BB"); 2198f22ef01cSRoman Divacky I = CallInst::CreateFree(Op, CurBB); 2199f22ef01cSRoman Divacky InstructionList.push_back(I); 2200f22ef01cSRoman Divacky break; 2201f22ef01cSRoman Divacky } 2202ffd1746dSEd Schouten case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] 2203ffd1746dSEd Schouten // For backward compatibility, tolerate a lack of an opty, and use i32. 2204e580952dSDimitry Andric // Remove this in LLVM 3.0. 2205ffd1746dSEd Schouten if (Record.size() < 3 || Record.size() > 4) 2206f22ef01cSRoman Divacky return Error("Invalid ALLOCA record"); 2207ffd1746dSEd Schouten unsigned OpNum = 0; 2208f22ef01cSRoman Divacky const PointerType *Ty = 2209ffd1746dSEd Schouten dyn_cast_or_null<PointerType>(getTypeByID(Record[OpNum++])); 2210ffd1746dSEd Schouten const Type *OpTy = Record.size() == 4 ? getTypeByID(Record[OpNum++]) : 2211ffd1746dSEd Schouten Type::getInt32Ty(Context); 2212ffd1746dSEd Schouten Value *Size = getFnValueByID(Record[OpNum++], OpTy); 2213ffd1746dSEd Schouten unsigned Align = Record[OpNum++]; 2214f22ef01cSRoman Divacky if (!Ty || !Size) return Error("Invalid ALLOCA record"); 2215f22ef01cSRoman Divacky I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1); 2216f22ef01cSRoman Divacky InstructionList.push_back(I); 2217f22ef01cSRoman Divacky break; 2218f22ef01cSRoman Divacky } 2219f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] 2220f22ef01cSRoman Divacky unsigned OpNum = 0; 2221f22ef01cSRoman Divacky Value *Op; 2222f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 2223f22ef01cSRoman Divacky OpNum+2 != Record.size()) 2224f22ef01cSRoman Divacky return Error("Invalid LOAD record"); 2225f22ef01cSRoman Divacky 2226f22ef01cSRoman Divacky I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1); 2227f22ef01cSRoman Divacky InstructionList.push_back(I); 2228f22ef01cSRoman Divacky break; 2229f22ef01cSRoman Divacky } 2230f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_STORE2: { // STORE2:[ptrty, ptr, val, align, vol] 2231f22ef01cSRoman Divacky unsigned OpNum = 0; 2232f22ef01cSRoman Divacky Value *Val, *Ptr; 2233f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 2234f22ef01cSRoman Divacky getValue(Record, OpNum, 2235f22ef01cSRoman Divacky cast<PointerType>(Ptr->getType())->getElementType(), Val) || 2236f22ef01cSRoman Divacky OpNum+2 != Record.size()) 2237f22ef01cSRoman Divacky return Error("Invalid STORE record"); 2238f22ef01cSRoman Divacky 2239f22ef01cSRoman Divacky I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); 2240f22ef01cSRoman Divacky InstructionList.push_back(I); 2241f22ef01cSRoman Divacky break; 2242f22ef01cSRoman Divacky } 2243f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_STORE: { // STORE:[val, valty, ptr, align, vol] 2244f22ef01cSRoman Divacky // FIXME: Legacy form of store instruction. Should be removed in LLVM 3.0. 2245f22ef01cSRoman Divacky unsigned OpNum = 0; 2246f22ef01cSRoman Divacky Value *Val, *Ptr; 2247f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Val) || 2248f22ef01cSRoman Divacky getValue(Record, OpNum, 2249f22ef01cSRoman Divacky PointerType::getUnqual(Val->getType()), Ptr)|| 2250f22ef01cSRoman Divacky OpNum+2 != Record.size()) 2251f22ef01cSRoman Divacky return Error("Invalid STORE record"); 2252f22ef01cSRoman Divacky 2253f22ef01cSRoman Divacky I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); 2254f22ef01cSRoman Divacky InstructionList.push_back(I); 2255f22ef01cSRoman Divacky break; 2256f22ef01cSRoman Divacky } 2257e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 2258e580952dSDimitry Andric case bitc::FUNC_CODE_INST_CALL: 2259e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 2260e580952dSDimitry Andric case bitc::FUNC_CODE_INST_CALL2: { 2261f22ef01cSRoman Divacky // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...] 2262f22ef01cSRoman Divacky if (Record.size() < 3) 2263f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2264f22ef01cSRoman Divacky 2265f22ef01cSRoman Divacky AttrListPtr PAL = getAttributes(Record[0]); 2266f22ef01cSRoman Divacky unsigned CCInfo = Record[1]; 2267f22ef01cSRoman Divacky 2268f22ef01cSRoman Divacky unsigned OpNum = 2; 2269f22ef01cSRoman Divacky Value *Callee; 2270f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 2271f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2272f22ef01cSRoman Divacky 2273f22ef01cSRoman Divacky const PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 2274f22ef01cSRoman Divacky const FunctionType *FTy = 0; 2275f22ef01cSRoman Divacky if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType()); 2276f22ef01cSRoman Divacky if (!FTy || Record.size() < FTy->getNumParams()+OpNum) 2277f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2278f22ef01cSRoman Divacky 2279f22ef01cSRoman Divacky SmallVector<Value*, 16> Args; 2280f22ef01cSRoman Divacky // Read the fixed params. 2281f22ef01cSRoman Divacky for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 2282f22ef01cSRoman Divacky if (FTy->getParamType(i)->getTypeID()==Type::LabelTyID) 2283f22ef01cSRoman Divacky Args.push_back(getBasicBlock(Record[OpNum])); 2284f22ef01cSRoman Divacky else 2285f22ef01cSRoman Divacky Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); 2286f22ef01cSRoman Divacky if (Args.back() == 0) return Error("Invalid CALL record"); 2287f22ef01cSRoman Divacky } 2288f22ef01cSRoman Divacky 2289f22ef01cSRoman Divacky // Read type/value pairs for varargs params. 2290f22ef01cSRoman Divacky if (!FTy->isVarArg()) { 2291f22ef01cSRoman Divacky if (OpNum != Record.size()) 2292f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2293f22ef01cSRoman Divacky } else { 2294f22ef01cSRoman Divacky while (OpNum != Record.size()) { 2295f22ef01cSRoman Divacky Value *Op; 2296f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 2297f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2298f22ef01cSRoman Divacky Args.push_back(Op); 2299f22ef01cSRoman Divacky } 2300f22ef01cSRoman Divacky } 2301f22ef01cSRoman Divacky 2302f22ef01cSRoman Divacky I = CallInst::Create(Callee, Args.begin(), Args.end()); 2303f22ef01cSRoman Divacky InstructionList.push_back(I); 2304f22ef01cSRoman Divacky cast<CallInst>(I)->setCallingConv( 2305f22ef01cSRoman Divacky static_cast<CallingConv::ID>(CCInfo>>1)); 2306f22ef01cSRoman Divacky cast<CallInst>(I)->setTailCall(CCInfo & 1); 2307f22ef01cSRoman Divacky cast<CallInst>(I)->setAttributes(PAL); 2308f22ef01cSRoman Divacky break; 2309f22ef01cSRoman Divacky } 2310f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] 2311f22ef01cSRoman Divacky if (Record.size() < 3) 2312f22ef01cSRoman Divacky return Error("Invalid VAARG record"); 2313f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[0]); 2314f22ef01cSRoman Divacky Value *Op = getFnValueByID(Record[1], OpTy); 2315f22ef01cSRoman Divacky const Type *ResTy = getTypeByID(Record[2]); 2316f22ef01cSRoman Divacky if (!OpTy || !Op || !ResTy) 2317f22ef01cSRoman Divacky return Error("Invalid VAARG record"); 2318f22ef01cSRoman Divacky I = new VAArgInst(Op, ResTy); 2319f22ef01cSRoman Divacky InstructionList.push_back(I); 2320f22ef01cSRoman Divacky break; 2321f22ef01cSRoman Divacky } 2322f22ef01cSRoman Divacky } 2323f22ef01cSRoman Divacky 2324f22ef01cSRoman Divacky // Add instruction to end of current BB. If there is no current BB, reject 2325f22ef01cSRoman Divacky // this file. 2326f22ef01cSRoman Divacky if (CurBB == 0) { 2327f22ef01cSRoman Divacky delete I; 2328f22ef01cSRoman Divacky return Error("Invalid instruction with no BB"); 2329f22ef01cSRoman Divacky } 2330f22ef01cSRoman Divacky CurBB->getInstList().push_back(I); 2331f22ef01cSRoman Divacky 2332f22ef01cSRoman Divacky // If this was a terminator instruction, move to the next block. 2333f22ef01cSRoman Divacky if (isa<TerminatorInst>(I)) { 2334f22ef01cSRoman Divacky ++CurBBNo; 2335f22ef01cSRoman Divacky CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0; 2336f22ef01cSRoman Divacky } 2337f22ef01cSRoman Divacky 2338f22ef01cSRoman Divacky // Non-void values get registered in the value table for future use. 2339f22ef01cSRoman Divacky if (I && !I->getType()->isVoidTy()) 2340f22ef01cSRoman Divacky ValueList.AssignValue(I, NextValueNo++); 2341f22ef01cSRoman Divacky } 2342f22ef01cSRoman Divacky 2343f22ef01cSRoman Divacky // Check the function list for unresolved values. 2344f22ef01cSRoman Divacky if (Argument *A = dyn_cast<Argument>(ValueList.back())) { 2345f22ef01cSRoman Divacky if (A->getParent() == 0) { 2346f22ef01cSRoman Divacky // We found at least one unresolved value. Nuke them all to avoid leaks. 2347f22ef01cSRoman Divacky for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ 2348e580952dSDimitry Andric if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) { 2349f22ef01cSRoman Divacky A->replaceAllUsesWith(UndefValue::get(A->getType())); 2350f22ef01cSRoman Divacky delete A; 2351f22ef01cSRoman Divacky } 2352f22ef01cSRoman Divacky } 2353f22ef01cSRoman Divacky return Error("Never resolved value found in function!"); 2354f22ef01cSRoman Divacky } 2355f22ef01cSRoman Divacky } 2356f22ef01cSRoman Divacky 2357e580952dSDimitry Andric // FIXME: Check for unresolved forward-declared metadata references 2358e580952dSDimitry Andric // and clean up leaks. 2359e580952dSDimitry Andric 2360f22ef01cSRoman Divacky // See if anything took the address of blocks in this function. If so, 2361f22ef01cSRoman Divacky // resolve them now. 2362f22ef01cSRoman Divacky DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI = 2363f22ef01cSRoman Divacky BlockAddrFwdRefs.find(F); 2364f22ef01cSRoman Divacky if (BAFRI != BlockAddrFwdRefs.end()) { 2365f22ef01cSRoman Divacky std::vector<BlockAddrRefTy> &RefList = BAFRI->second; 2366f22ef01cSRoman Divacky for (unsigned i = 0, e = RefList.size(); i != e; ++i) { 2367f22ef01cSRoman Divacky unsigned BlockIdx = RefList[i].first; 2368f22ef01cSRoman Divacky if (BlockIdx >= FunctionBBs.size()) 2369f22ef01cSRoman Divacky return Error("Invalid blockaddress block #"); 2370f22ef01cSRoman Divacky 2371f22ef01cSRoman Divacky GlobalVariable *FwdRef = RefList[i].second; 2372f22ef01cSRoman Divacky FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx])); 2373f22ef01cSRoman Divacky FwdRef->eraseFromParent(); 2374f22ef01cSRoman Divacky } 2375f22ef01cSRoman Divacky 2376f22ef01cSRoman Divacky BlockAddrFwdRefs.erase(BAFRI); 2377f22ef01cSRoman Divacky } 2378f22ef01cSRoman Divacky 2379e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 2380e580952dSDimitry Andric unsigned NewMDValueListSize = MDValueList.size(); 2381e580952dSDimitry Andric 2382f22ef01cSRoman Divacky // Trim the value list down to the size it was before we parsed this function. 2383f22ef01cSRoman Divacky ValueList.shrinkTo(ModuleValueListSize); 2384e580952dSDimitry Andric MDValueList.shrinkTo(ModuleMDValueListSize); 2385e580952dSDimitry Andric 2386e580952dSDimitry Andric // Backwards compatibility hack: Function-local metadata numbers 2387e580952dSDimitry Andric // were previously not reset between functions. This is now fixed, 2388e580952dSDimitry Andric // however we still need to understand the old numbering in order 2389e580952dSDimitry Andric // to be able to read old bitcode files. 2390e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 2391e580952dSDimitry Andric if (LLVM2_7MetadataDetected) 2392e580952dSDimitry Andric MDValueList.resize(NewMDValueListSize); 2393e580952dSDimitry Andric 2394f22ef01cSRoman Divacky std::vector<BasicBlock*>().swap(FunctionBBs); 2395f22ef01cSRoman Divacky 2396f22ef01cSRoman Divacky return false; 2397f22ef01cSRoman Divacky } 2398f22ef01cSRoman Divacky 2399f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2400f22ef01cSRoman Divacky // GVMaterializer implementation 2401f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2402f22ef01cSRoman Divacky 2403f22ef01cSRoman Divacky 2404f22ef01cSRoman Divacky bool BitcodeReader::isMaterializable(const GlobalValue *GV) const { 2405f22ef01cSRoman Divacky if (const Function *F = dyn_cast<Function>(GV)) { 2406f22ef01cSRoman Divacky return F->isDeclaration() && 2407f22ef01cSRoman Divacky DeferredFunctionInfo.count(const_cast<Function*>(F)); 2408f22ef01cSRoman Divacky } 2409f22ef01cSRoman Divacky return false; 2410f22ef01cSRoman Divacky } 2411f22ef01cSRoman Divacky 2412f22ef01cSRoman Divacky bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) { 2413f22ef01cSRoman Divacky Function *F = dyn_cast<Function>(GV); 2414f22ef01cSRoman Divacky // If it's not a function or is already material, ignore the request. 2415f22ef01cSRoman Divacky if (!F || !F->isMaterializable()) return false; 2416f22ef01cSRoman Divacky 2417f22ef01cSRoman Divacky DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 2418f22ef01cSRoman Divacky assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 2419f22ef01cSRoman Divacky 2420f22ef01cSRoman Divacky // Move the bit stream to the saved position of the deferred function body. 2421f22ef01cSRoman Divacky Stream.JumpToBit(DFII->second); 2422f22ef01cSRoman Divacky 2423f22ef01cSRoman Divacky if (ParseFunctionBody(F)) { 2424f22ef01cSRoman Divacky if (ErrInfo) *ErrInfo = ErrorString; 2425f22ef01cSRoman Divacky return true; 2426f22ef01cSRoman Divacky } 2427f22ef01cSRoman Divacky 2428f22ef01cSRoman Divacky // Upgrade any old intrinsic calls in the function. 2429f22ef01cSRoman Divacky for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(), 2430f22ef01cSRoman Divacky E = UpgradedIntrinsics.end(); I != E; ++I) { 2431f22ef01cSRoman Divacky if (I->first != I->second) { 2432f22ef01cSRoman Divacky for (Value::use_iterator UI = I->first->use_begin(), 2433f22ef01cSRoman Divacky UE = I->first->use_end(); UI != UE; ) { 2434f22ef01cSRoman Divacky if (CallInst* CI = dyn_cast<CallInst>(*UI++)) 2435f22ef01cSRoman Divacky UpgradeIntrinsicCall(CI, I->second); 2436f22ef01cSRoman Divacky } 2437f22ef01cSRoman Divacky } 2438f22ef01cSRoman Divacky } 2439f22ef01cSRoman Divacky 2440f22ef01cSRoman Divacky return false; 2441f22ef01cSRoman Divacky } 2442f22ef01cSRoman Divacky 2443f22ef01cSRoman Divacky bool BitcodeReader::isDematerializable(const GlobalValue *GV) const { 2444f22ef01cSRoman Divacky const Function *F = dyn_cast<Function>(GV); 2445f22ef01cSRoman Divacky if (!F || F->isDeclaration()) 2446f22ef01cSRoman Divacky return false; 2447f22ef01cSRoman Divacky return DeferredFunctionInfo.count(const_cast<Function*>(F)); 2448f22ef01cSRoman Divacky } 2449f22ef01cSRoman Divacky 2450f22ef01cSRoman Divacky void BitcodeReader::Dematerialize(GlobalValue *GV) { 2451f22ef01cSRoman Divacky Function *F = dyn_cast<Function>(GV); 2452f22ef01cSRoman Divacky // If this function isn't dematerializable, this is a noop. 2453f22ef01cSRoman Divacky if (!F || !isDematerializable(F)) 2454f22ef01cSRoman Divacky return; 2455f22ef01cSRoman Divacky 2456f22ef01cSRoman Divacky assert(DeferredFunctionInfo.count(F) && "No info to read function later?"); 2457f22ef01cSRoman Divacky 2458f22ef01cSRoman Divacky // Just forget the function body, we can remat it later. 2459f22ef01cSRoman Divacky F->deleteBody(); 2460f22ef01cSRoman Divacky } 2461f22ef01cSRoman Divacky 2462f22ef01cSRoman Divacky 2463f22ef01cSRoman Divacky bool BitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) { 2464f22ef01cSRoman Divacky assert(M == TheModule && 2465f22ef01cSRoman Divacky "Can only Materialize the Module this BitcodeReader is attached to."); 2466f22ef01cSRoman Divacky // Iterate over the module, deserializing any functions that are still on 2467f22ef01cSRoman Divacky // disk. 2468f22ef01cSRoman Divacky for (Module::iterator F = TheModule->begin(), E = TheModule->end(); 2469f22ef01cSRoman Divacky F != E; ++F) 2470f22ef01cSRoman Divacky if (F->isMaterializable() && 2471f22ef01cSRoman Divacky Materialize(F, ErrInfo)) 2472f22ef01cSRoman Divacky return true; 2473f22ef01cSRoman Divacky 2474f22ef01cSRoman Divacky // Upgrade any intrinsic calls that slipped through (should not happen!) and 2475f22ef01cSRoman Divacky // delete the old functions to clean up. We can't do this unless the entire 2476f22ef01cSRoman Divacky // module is materialized because there could always be another function body 2477f22ef01cSRoman Divacky // with calls to the old function. 2478f22ef01cSRoman Divacky for (std::vector<std::pair<Function*, Function*> >::iterator I = 2479f22ef01cSRoman Divacky UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) { 2480f22ef01cSRoman Divacky if (I->first != I->second) { 2481f22ef01cSRoman Divacky for (Value::use_iterator UI = I->first->use_begin(), 2482f22ef01cSRoman Divacky UE = I->first->use_end(); UI != UE; ) { 2483f22ef01cSRoman Divacky if (CallInst* CI = dyn_cast<CallInst>(*UI++)) 2484f22ef01cSRoman Divacky UpgradeIntrinsicCall(CI, I->second); 2485f22ef01cSRoman Divacky } 2486f22ef01cSRoman Divacky if (!I->first->use_empty()) 2487f22ef01cSRoman Divacky I->first->replaceAllUsesWith(I->second); 2488f22ef01cSRoman Divacky I->first->eraseFromParent(); 2489f22ef01cSRoman Divacky } 2490f22ef01cSRoman Divacky } 2491f22ef01cSRoman Divacky std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics); 2492f22ef01cSRoman Divacky 2493f22ef01cSRoman Divacky // Check debug info intrinsics. 2494f22ef01cSRoman Divacky CheckDebugInfoIntrinsics(TheModule); 2495f22ef01cSRoman Divacky 2496f22ef01cSRoman Divacky return false; 2497f22ef01cSRoman Divacky } 2498f22ef01cSRoman Divacky 2499f22ef01cSRoman Divacky 2500f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2501f22ef01cSRoman Divacky // External interface 2502f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2503f22ef01cSRoman Divacky 2504f22ef01cSRoman Divacky /// getLazyBitcodeModule - lazy function-at-a-time loading from a file. 2505f22ef01cSRoman Divacky /// 2506f22ef01cSRoman Divacky Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer, 2507f22ef01cSRoman Divacky LLVMContext& Context, 2508f22ef01cSRoman Divacky std::string *ErrMsg) { 2509f22ef01cSRoman Divacky Module *M = new Module(Buffer->getBufferIdentifier(), Context); 2510f22ef01cSRoman Divacky BitcodeReader *R = new BitcodeReader(Buffer, Context); 2511f22ef01cSRoman Divacky M->setMaterializer(R); 2512f22ef01cSRoman Divacky if (R->ParseBitcodeInto(M)) { 2513f22ef01cSRoman Divacky if (ErrMsg) 2514f22ef01cSRoman Divacky *ErrMsg = R->getErrorString(); 2515f22ef01cSRoman Divacky 2516f22ef01cSRoman Divacky delete M; // Also deletes R. 2517f22ef01cSRoman Divacky return 0; 2518f22ef01cSRoman Divacky } 2519f22ef01cSRoman Divacky // Have the BitcodeReader dtor delete 'Buffer'. 2520f22ef01cSRoman Divacky R->setBufferOwned(true); 2521f22ef01cSRoman Divacky return M; 2522f22ef01cSRoman Divacky } 2523f22ef01cSRoman Divacky 2524f22ef01cSRoman Divacky /// ParseBitcodeFile - Read the specified bitcode file, returning the module. 2525f22ef01cSRoman Divacky /// If an error occurs, return null and fill in *ErrMsg if non-null. 2526f22ef01cSRoman Divacky Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context, 2527f22ef01cSRoman Divacky std::string *ErrMsg){ 2528f22ef01cSRoman Divacky Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg); 2529f22ef01cSRoman Divacky if (!M) return 0; 2530f22ef01cSRoman Divacky 2531f22ef01cSRoman Divacky // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether 2532f22ef01cSRoman Divacky // there was an error. 2533f22ef01cSRoman Divacky static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false); 2534f22ef01cSRoman Divacky 2535f22ef01cSRoman Divacky // Read in the entire module, and destroy the BitcodeReader. 2536f22ef01cSRoman Divacky if (M->MaterializeAllPermanently(ErrMsg)) { 2537f22ef01cSRoman Divacky delete M; 2538f22ef01cSRoman Divacky return NULL; 2539f22ef01cSRoman Divacky } 2540f22ef01cSRoman Divacky return M; 2541f22ef01cSRoman Divacky } 2542