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