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