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 | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 183 | OperandList[Idx] = C; |
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 | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 204 | OperandList[Idx] = V; |
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 | } |
| 773 | case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#] |
| 774 | if (Record.size() < 3) return Error("Invalid CE_SELECT record"); |
| 775 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], |
| 776 | Type::Int1Ty), |
| 777 | ValueList.getConstantFwdRef(Record[1],CurTy), |
| 778 | ValueList.getConstantFwdRef(Record[2],CurTy)); |
| 779 | break; |
| 780 | case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval] |
| 781 | if (Record.size() < 3) return Error("Invalid CE_EXTRACTELT record"); |
| 782 | const VectorType *OpTy = |
| 783 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
| 784 | if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record"); |
| 785 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 786 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], |
| 787 | OpTy->getElementType()); |
| 788 | V = ConstantExpr::getExtractElement(Op0, Op1); |
| 789 | break; |
| 790 | } |
| 791 | case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval] |
| 792 | const VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
| 793 | if (Record.size() < 3 || OpTy == 0) |
| 794 | return Error("Invalid CE_INSERTELT record"); |
| 795 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 796 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], |
| 797 | OpTy->getElementType()); |
| 798 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::Int32Ty); |
| 799 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 800 | break; |
| 801 | } |
| 802 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
| 803 | const VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
| 804 | if (Record.size() < 3 || OpTy == 0) |
| 805 | return Error("Invalid CE_INSERTELT record"); |
| 806 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 807 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 808 | const Type *ShufTy=VectorType::get(Type::Int32Ty, OpTy->getNumElements()); |
| 809 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); |
| 810 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
| 811 | break; |
| 812 | } |
| 813 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
| 814 | if (Record.size() < 4) return Error("Invalid CE_CMP record"); |
| 815 | const Type *OpTy = getTypeByID(Record[0]); |
| 816 | if (OpTy == 0) return Error("Invalid CE_CMP record"); |
| 817 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 818 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 819 | |
| 820 | if (OpTy->isFloatingPoint()) |
| 821 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); |
Nate Begeman | baa64eb | 2008-05-12 20:33:52 +0000 | [diff] [blame] | 822 | else if (!isa<VectorType>(OpTy)) |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 823 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 824 | else if (OpTy->isFPOrFPVector()) |
| 825 | V = ConstantExpr::getVFCmp(Record[3], Op0, Op1); |
| 826 | else |
| 827 | V = ConstantExpr::getVICmp(Record[3], Op0, Op1); |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 828 | break; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 829 | } |
Chris Lattner | 2bce93a | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 830 | case bitc::CST_CODE_INLINEASM: { |
| 831 | if (Record.size() < 2) return Error("Invalid INLINEASM record"); |
| 832 | std::string AsmStr, ConstrStr; |
| 833 | bool HasSideEffects = Record[0]; |
| 834 | unsigned AsmStrSize = Record[1]; |
| 835 | if (2+AsmStrSize >= Record.size()) |
| 836 | return Error("Invalid INLINEASM record"); |
| 837 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 838 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
| 839 | return Error("Invalid INLINEASM record"); |
| 840 | |
| 841 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 842 | AsmStr += (char)Record[2+i]; |
| 843 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 844 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
| 845 | const PointerType *PTy = cast<PointerType>(CurTy); |
| 846 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
| 847 | AsmStr, ConstrStr, HasSideEffects); |
| 848 | break; |
| 849 | } |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 852 | ValueList.AssignValue(V, NextCstNo); |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 853 | ++NextCstNo; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 856 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 857 | /// RememberAndSkipFunctionBody - When we see the block for a function body, |
| 858 | /// remember where it is and then skip it. This lets us lazily deserialize the |
| 859 | /// functions. |
| 860 | bool BitcodeReader::RememberAndSkipFunctionBody() { |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 861 | // Get the function we are talking about. |
| 862 | if (FunctionsWithBodies.empty()) |
| 863 | return Error("Insufficient function protos"); |
| 864 | |
| 865 | Function *Fn = FunctionsWithBodies.back(); |
| 866 | FunctionsWithBodies.pop_back(); |
| 867 | |
| 868 | // Save the current stream state. |
| 869 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
| 870 | DeferredFunctionInfo[Fn] = std::make_pair(CurBit, Fn->getLinkage()); |
| 871 | |
| 872 | // Set the functions linkage to GhostLinkage so we know it is lazily |
| 873 | // deserialized. |
| 874 | Fn->setLinkage(GlobalValue::GhostLinkage); |
| 875 | |
| 876 | // Skip over the function block for now. |
| 877 | if (Stream.SkipBlock()) |
| 878 | return Error("Malformed block record"); |
| 879 | return false; |
| 880 | } |
| 881 | |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 882 | bool BitcodeReader::ParseModule(const std::string &ModuleID) { |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 883 | // Reject multiple MODULE_BLOCK's in a single bitstream. |
| 884 | if (TheModule) |
| 885 | return Error("Multiple MODULE_BLOCKs in same stream"); |
| 886 | |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 887 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 888 | return Error("Malformed block record"); |
| 889 | |
| 890 | // Otherwise, create the module. |
| 891 | TheModule = new Module(ModuleID); |
| 892 | |
| 893 | SmallVector<uint64_t, 64> Record; |
| 894 | std::vector<std::string> SectionTable; |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame^] | 895 | std::vector<std::string> GCTable; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 896 | |
| 897 | // Read all the records for this module. |
| 898 | while (!Stream.AtEndOfStream()) { |
| 899 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 900 | if (Code == bitc::END_BLOCK) { |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 901 | if (Stream.ReadBlockEnd()) |
| 902 | return Error("Error at end of module block"); |
| 903 | |
| 904 | // Patch the initializers for globals and aliases up. |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 905 | ResolveGlobalAndAliasInits(); |
| 906 | if (!GlobalInits.empty() || !AliasInits.empty()) |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 907 | return Error("Malformed global initializer set"); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 908 | if (!FunctionsWithBodies.empty()) |
| 909 | return Error("Too few function bodies found"); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 910 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 911 | // Look for intrinsic functions which need to be upgraded at some point |
| 912 | for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 913 | FI != FE; ++FI) { |
Evan Cheng | f9b83fc | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 914 | Function* NewFn; |
| 915 | if (UpgradeIntrinsicFunction(FI, NewFn)) |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 916 | UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn)); |
| 917 | } |
| 918 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 919 | // Force deallocation of memory for these vectors to favor the client that |
| 920 | // want lazy deserialization. |
| 921 | std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); |
| 922 | std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); |
| 923 | std::vector<Function*>().swap(FunctionsWithBodies); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 924 | return false; |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 925 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 926 | |
| 927 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 928 | switch (Stream.ReadSubBlockID()) { |
| 929 | default: // Skip unknown content. |
| 930 | if (Stream.SkipBlock()) |
| 931 | return Error("Malformed block record"); |
| 932 | break; |
Chris Lattner | 3f79980 | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 933 | case bitc::BLOCKINFO_BLOCK_ID: |
| 934 | if (Stream.ReadBlockInfoBlock()) |
| 935 | return Error("Malformed BlockInfoBlock"); |
| 936 | break; |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 937 | case bitc::PARAMATTR_BLOCK_ID: |
| 938 | if (ParseParamAttrBlock()) |
| 939 | return true; |
| 940 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 941 | case bitc::TYPE_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 942 | if (ParseTypeTable()) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 943 | return true; |
| 944 | break; |
| 945 | case bitc::TYPE_SYMTAB_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 946 | if (ParseTypeSymbolTable()) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 947 | return true; |
| 948 | break; |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 949 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 950 | if (ParseValueSymbolTable()) |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 951 | return true; |
| 952 | break; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 953 | case bitc::CONSTANTS_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 954 | if (ParseConstants() || ResolveGlobalAndAliasInits()) |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 955 | return true; |
| 956 | break; |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 957 | case bitc::FUNCTION_BLOCK_ID: |
| 958 | // If this is the first function body we've seen, reverse the |
| 959 | // FunctionsWithBodies list. |
| 960 | if (!HasReversedFunctionsWithBodies) { |
| 961 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); |
| 962 | HasReversedFunctionsWithBodies = true; |
| 963 | } |
| 964 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 965 | if (RememberAndSkipFunctionBody()) |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 966 | return true; |
| 967 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 968 | } |
| 969 | continue; |
| 970 | } |
| 971 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 972 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 973 | Stream.ReadAbbrevRecord(); |
| 974 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | // Read a record. |
| 978 | switch (Stream.ReadRecord(Code, Record)) { |
| 979 | default: break; // Default behavior, ignore unknown content. |
| 980 | case bitc::MODULE_CODE_VERSION: // VERSION: [version#] |
| 981 | if (Record.size() < 1) |
| 982 | return Error("Malformed MODULE_CODE_VERSION"); |
| 983 | // Only version #0 is supported so far. |
| 984 | if (Record[0] != 0) |
| 985 | return Error("Unknown bitstream version!"); |
| 986 | break; |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 987 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 988 | std::string S; |
| 989 | if (ConvertToString(Record, 0, S)) |
| 990 | return Error("Invalid MODULE_CODE_TRIPLE record"); |
| 991 | TheModule->setTargetTriple(S); |
| 992 | break; |
| 993 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 994 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 995 | std::string S; |
| 996 | if (ConvertToString(Record, 0, S)) |
| 997 | return Error("Invalid MODULE_CODE_DATALAYOUT record"); |
| 998 | TheModule->setDataLayout(S); |
| 999 | break; |
| 1000 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1001 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1002 | std::string S; |
| 1003 | if (ConvertToString(Record, 0, S)) |
| 1004 | return Error("Invalid MODULE_CODE_ASM record"); |
| 1005 | TheModule->setModuleInlineAsm(S); |
| 1006 | break; |
| 1007 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1008 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1009 | std::string S; |
| 1010 | if (ConvertToString(Record, 0, S)) |
| 1011 | return Error("Invalid MODULE_CODE_DEPLIB record"); |
| 1012 | TheModule->addLibrary(S); |
| 1013 | break; |
| 1014 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1015 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1016 | std::string S; |
| 1017 | if (ConvertToString(Record, 0, S)) |
| 1018 | return Error("Invalid MODULE_CODE_SECTIONNAME record"); |
| 1019 | SectionTable.push_back(S); |
| 1020 | break; |
| 1021 | } |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame^] | 1022 | case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1023 | std::string S; |
| 1024 | if (ConvertToString(Record, 0, S)) |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame^] | 1025 | return Error("Invalid MODULE_CODE_GCNAME record"); |
| 1026 | GCTable.push_back(S); |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1027 | break; |
| 1028 | } |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1029 | // GLOBALVAR: [pointer type, isconst, initid, |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1030 | // linkage, alignment, section, visibility, threadlocal] |
| 1031 | case bitc::MODULE_CODE_GLOBALVAR: { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 1032 | if (Record.size() < 6) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1033 | return Error("Invalid MODULE_CODE_GLOBALVAR record"); |
| 1034 | const Type *Ty = getTypeByID(Record[0]); |
| 1035 | if (!isa<PointerType>(Ty)) |
| 1036 | return Error("Global not a pointer type!"); |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1037 | unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1038 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 1039 | |
| 1040 | bool isConstant = Record[1]; |
| 1041 | GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]); |
| 1042 | unsigned Alignment = (1 << Record[4]) >> 1; |
| 1043 | std::string Section; |
| 1044 | if (Record[5]) { |
| 1045 | if (Record[5]-1 >= SectionTable.size()) |
| 1046 | return Error("Invalid section ID"); |
| 1047 | Section = SectionTable[Record[5]-1]; |
| 1048 | } |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 1049 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
Chris Lattner | 5f32c01 | 2007-05-06 19:27:46 +0000 | [diff] [blame] | 1050 | if (Record.size() > 6) |
| 1051 | Visibility = GetDecodedVisibility(Record[6]); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 1052 | bool isThreadLocal = false; |
Chris Lattner | 5f32c01 | 2007-05-06 19:27:46 +0000 | [diff] [blame] | 1053 | if (Record.size() > 7) |
| 1054 | isThreadLocal = Record[7]; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1055 | |
| 1056 | GlobalVariable *NewGV = |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1057 | new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule, |
| 1058 | isThreadLocal, AddressSpace); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1059 | NewGV->setAlignment(Alignment); |
| 1060 | if (!Section.empty()) |
| 1061 | NewGV->setSection(Section); |
| 1062 | NewGV->setVisibility(Visibility); |
| 1063 | NewGV->setThreadLocal(isThreadLocal); |
| 1064 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1065 | ValueList.push_back(NewGV); |
| 1066 | |
Chris Lattner | 6dbfd7b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 1067 | // Remember which value to use for the global initializer. |
| 1068 | if (unsigned InitID = Record[2]) |
| 1069 | GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1070 | break; |
| 1071 | } |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1072 | // FUNCTION: [type, callingconv, isproto, linkage, paramattr, |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame^] | 1073 | // alignment, section, visibility, gc] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1074 | case bitc::MODULE_CODE_FUNCTION: { |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1075 | if (Record.size() < 8) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1076 | return Error("Invalid MODULE_CODE_FUNCTION record"); |
| 1077 | const Type *Ty = getTypeByID(Record[0]); |
| 1078 | if (!isa<PointerType>(Ty)) |
| 1079 | return Error("Function not a pointer type!"); |
| 1080 | const FunctionType *FTy = |
| 1081 | dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); |
| 1082 | if (!FTy) |
| 1083 | return Error("Function not a pointer to function type!"); |
| 1084 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1085 | Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, |
| 1086 | "", TheModule); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1087 | |
| 1088 | Func->setCallingConv(Record[1]); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1089 | bool isProto = Record[2]; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1090 | Func->setLinkage(GetDecodedLinkage(Record[3])); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1091 | Func->setParamAttrs(getParamAttrs(Record[4])); |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1092 | |
| 1093 | Func->setAlignment((1 << Record[5]) >> 1); |
| 1094 | if (Record[6]) { |
| 1095 | if (Record[6]-1 >= SectionTable.size()) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1096 | return Error("Invalid section ID"); |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1097 | Func->setSection(SectionTable[Record[6]-1]); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1098 | } |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1099 | Func->setVisibility(GetDecodedVisibility(Record[7])); |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1100 | if (Record.size() > 8 && Record[8]) { |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame^] | 1101 | if (Record[8]-1 > GCTable.size()) |
| 1102 | return Error("Invalid GC ID"); |
| 1103 | Func->setGC(GCTable[Record[8]-1].c_str()); |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1104 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1106 | ValueList.push_back(Func); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1107 | |
| 1108 | // If this is a function with a body, remember the prototype we are |
| 1109 | // creating now, so that we can match up the body with them later. |
| 1110 | if (!isProto) |
| 1111 | FunctionsWithBodies.push_back(Func); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1112 | break; |
| 1113 | } |
Anton Korobeynikov | 91342d8 | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 1114 | // ALIAS: [alias type, aliasee val#, linkage] |
Anton Korobeynikov | f8342b9 | 2008-03-11 21:40:17 +0000 | [diff] [blame] | 1115 | // ALIAS: [alias type, aliasee val#, linkage, visibility] |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1116 | case bitc::MODULE_CODE_ALIAS: { |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1117 | if (Record.size() < 3) |
| 1118 | return Error("Invalid MODULE_ALIAS record"); |
| 1119 | const Type *Ty = getTypeByID(Record[0]); |
| 1120 | if (!isa<PointerType>(Ty)) |
| 1121 | return Error("Function not a pointer type!"); |
| 1122 | |
| 1123 | GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]), |
| 1124 | "", 0, TheModule); |
Anton Korobeynikov | 91342d8 | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 1125 | // Old bitcode files didn't have visibility field. |
| 1126 | if (Record.size() > 3) |
| 1127 | NewGA->setVisibility(GetDecodedVisibility(Record[3])); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1128 | ValueList.push_back(NewGA); |
| 1129 | AliasInits.push_back(std::make_pair(NewGA, Record[1])); |
| 1130 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1131 | } |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1132 | /// MODULE_CODE_PURGEVALS: [numvals] |
| 1133 | case bitc::MODULE_CODE_PURGEVALS: |
| 1134 | // Trim down the value list to the specified size. |
| 1135 | if (Record.size() < 1 || Record[0] > ValueList.size()) |
| 1136 | return Error("Invalid MODULE_PURGEVALS record"); |
| 1137 | ValueList.shrinkTo(Record[0]); |
| 1138 | break; |
| 1139 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1140 | Record.clear(); |
| 1141 | } |
| 1142 | |
| 1143 | return Error("Premature end of bitstream"); |
| 1144 | } |
| 1145 | |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1146 | /// SkipWrapperHeader - Some systems wrap bc files with a special header for |
| 1147 | /// padding or other reasons. The format of this header is: |
| 1148 | /// |
| 1149 | /// struct bc_header { |
| 1150 | /// uint32_t Magic; // 0x0B17C0DE |
| 1151 | /// uint32_t Version; // Version, currently always 0. |
| 1152 | /// uint32_t BitcodeOffset; // Offset to traditional bitcode file. |
| 1153 | /// uint32_t BitcodeSize; // Size of traditional bitcode file. |
| 1154 | /// ... potentially other gunk ... |
| 1155 | /// }; |
| 1156 | /// |
| 1157 | /// This function is called when we find a file with a matching magic number. |
| 1158 | /// In this case, skip down to the subsection of the file that is actually a BC |
| 1159 | /// file. |
| 1160 | static bool SkipWrapperHeader(unsigned char *&BufPtr, unsigned char *&BufEnd) { |
| 1161 | enum { |
| 1162 | KnownHeaderSize = 4*4, // Size of header we read. |
| 1163 | OffsetField = 2*4, // Offset in bytes to Offset field. |
| 1164 | SizeField = 3*4 // Offset in bytes to Size field. |
| 1165 | }; |
| 1166 | |
| 1167 | |
| 1168 | // Must contain the header! |
| 1169 | if (BufEnd-BufPtr < KnownHeaderSize) return true; |
| 1170 | |
| 1171 | unsigned Offset = ( BufPtr[OffsetField ] | |
| 1172 | (BufPtr[OffsetField+1] << 8) | |
| 1173 | (BufPtr[OffsetField+2] << 16) | |
| 1174 | (BufPtr[OffsetField+3] << 24)); |
| 1175 | unsigned Size = ( BufPtr[SizeField ] | |
| 1176 | (BufPtr[SizeField +1] << 8) | |
| 1177 | (BufPtr[SizeField +2] << 16) | |
| 1178 | (BufPtr[SizeField +3] << 24)); |
| 1179 | |
| 1180 | // Verify that Offset+Size fits in the file. |
| 1181 | if (Offset+Size > unsigned(BufEnd-BufPtr)) |
| 1182 | return true; |
| 1183 | BufPtr += Offset; |
| 1184 | BufEnd = BufPtr+Size; |
| 1185 | return false; |
| 1186 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1187 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1188 | bool BitcodeReader::ParseBitcode() { |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1189 | TheModule = 0; |
| 1190 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1191 | if (Buffer->getBufferSize() & 3) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1192 | return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
| 1193 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1194 | unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1195 | unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); |
| 1196 | |
| 1197 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| 1198 | // The magic number is 0x0B17C0DE stored in little endian. |
| 1199 | if (BufPtr != BufEnd && BufPtr[0] == 0xDE && BufPtr[1] == 0xC0 && |
| 1200 | BufPtr[2] == 0x17 && BufPtr[3] == 0x0B) |
| 1201 | if (SkipWrapperHeader(BufPtr, BufEnd)) |
| 1202 | return Error("Invalid bitcode wrapper header"); |
| 1203 | |
| 1204 | Stream.init(BufPtr, BufEnd); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1205 | |
| 1206 | // Sniff for the signature. |
| 1207 | if (Stream.Read(8) != 'B' || |
| 1208 | Stream.Read(8) != 'C' || |
| 1209 | Stream.Read(4) != 0x0 || |
| 1210 | Stream.Read(4) != 0xC || |
| 1211 | Stream.Read(4) != 0xE || |
| 1212 | Stream.Read(4) != 0xD) |
| 1213 | return Error("Invalid bitcode signature"); |
| 1214 | |
| 1215 | // We expect a number of well-defined blocks, though we don't necessarily |
| 1216 | // need to understand them all. |
| 1217 | while (!Stream.AtEndOfStream()) { |
| 1218 | unsigned Code = Stream.ReadCode(); |
| 1219 | |
| 1220 | if (Code != bitc::ENTER_SUBBLOCK) |
| 1221 | return Error("Invalid record at top-level"); |
| 1222 | |
| 1223 | unsigned BlockID = Stream.ReadSubBlockID(); |
| 1224 | |
| 1225 | // We only know the MODULE subblock ID. |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1226 | switch (BlockID) { |
| 1227 | case bitc::BLOCKINFO_BLOCK_ID: |
| 1228 | if (Stream.ReadBlockInfoBlock()) |
| 1229 | return Error("Malformed BlockInfoBlock"); |
| 1230 | break; |
| 1231 | case bitc::MODULE_BLOCK_ID: |
Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 1232 | if (ParseModule(Buffer->getBufferIdentifier())) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1233 | return true; |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | default: |
| 1236 | if (Stream.SkipBlock()) |
| 1237 | return Error("Malformed block record"); |
| 1238 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | return false; |
| 1243 | } |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1244 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1245 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1246 | /// ParseFunctionBody - Lazily parse the specified function body block. |
| 1247 | bool BitcodeReader::ParseFunctionBody(Function *F) { |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1248 | if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1249 | return Error("Malformed block record"); |
| 1250 | |
| 1251 | unsigned ModuleValueListSize = ValueList.size(); |
| 1252 | |
| 1253 | // Add all the function arguments to the value table. |
| 1254 | for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
| 1255 | ValueList.push_back(I); |
| 1256 | |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1257 | unsigned NextValueNo = ValueList.size(); |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1258 | BasicBlock *CurBB = 0; |
| 1259 | unsigned CurBBNo = 0; |
| 1260 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1261 | // Read all the records. |
| 1262 | SmallVector<uint64_t, 64> Record; |
| 1263 | while (1) { |
| 1264 | unsigned Code = Stream.ReadCode(); |
| 1265 | if (Code == bitc::END_BLOCK) { |
| 1266 | if (Stream.ReadBlockEnd()) |
| 1267 | return Error("Error at end of function block"); |
| 1268 | break; |
| 1269 | } |
| 1270 | |
| 1271 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 1272 | switch (Stream.ReadSubBlockID()) { |
| 1273 | default: // Skip unknown content. |
| 1274 | if (Stream.SkipBlock()) |
| 1275 | return Error("Malformed block record"); |
| 1276 | break; |
| 1277 | case bitc::CONSTANTS_BLOCK_ID: |
| 1278 | if (ParseConstants()) return true; |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1279 | NextValueNo = ValueList.size(); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1280 | break; |
| 1281 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
| 1282 | if (ParseValueSymbolTable()) return true; |
| 1283 | break; |
| 1284 | } |
| 1285 | continue; |
| 1286 | } |
| 1287 | |
| 1288 | if (Code == bitc::DEFINE_ABBREV) { |
| 1289 | Stream.ReadAbbrevRecord(); |
| 1290 | continue; |
| 1291 | } |
| 1292 | |
| 1293 | // Read a record. |
| 1294 | Record.clear(); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1295 | Instruction *I = 0; |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1296 | switch (Stream.ReadRecord(Code, Record)) { |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1297 | default: // Default behavior: reject |
| 1298 | return Error("Unknown instruction"); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1299 | case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks] |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1300 | if (Record.size() < 1 || Record[0] == 0) |
| 1301 | return Error("Invalid DECLAREBLOCKS record"); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1302 | // Create all the basic blocks for the function. |
Chris Lattner | f61e645 | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 1303 | FunctionBBs.resize(Record[0]); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1304 | for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1305 | FunctionBBs[i] = BasicBlock::Create("", F); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1306 | CurBB = FunctionBBs[0]; |
| 1307 | continue; |
| 1308 | |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1309 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] |
| 1310 | unsigned OpNum = 0; |
| 1311 | Value *LHS, *RHS; |
| 1312 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
| 1313 | getValue(Record, OpNum, LHS->getType(), RHS) || |
| 1314 | OpNum+1 != Record.size()) |
| 1315 | return Error("Invalid BINOP record"); |
| 1316 | |
| 1317 | int Opc = GetDecodedBinaryOpcode(Record[OpNum], LHS->getType()); |
| 1318 | if (Opc == -1) return Error("Invalid BINOP record"); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1319 | I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1320 | break; |
| 1321 | } |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1322 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] |
| 1323 | unsigned OpNum = 0; |
| 1324 | Value *Op; |
| 1325 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 1326 | OpNum+2 != Record.size()) |
| 1327 | return Error("Invalid CAST record"); |
| 1328 | |
| 1329 | const Type *ResTy = getTypeByID(Record[OpNum]); |
| 1330 | int Opc = GetDecodedCastOpcode(Record[OpNum+1]); |
| 1331 | if (Opc == -1 || ResTy == 0) |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1332 | return Error("Invalid CAST record"); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1333 | I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy); |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | } |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1336 | case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1337 | unsigned OpNum = 0; |
| 1338 | Value *BasePtr; |
| 1339 | if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1340 | return Error("Invalid GEP record"); |
| 1341 | |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1342 | SmallVector<Value*, 16> GEPIdx; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1343 | while (OpNum != Record.size()) { |
| 1344 | Value *Op; |
| 1345 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1346 | return Error("Invalid GEP record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1347 | GEPIdx.push_back(Op); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1350 | I = GetElementPtrInst::Create(BasePtr, GEPIdx.begin(), GEPIdx.end()); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1351 | break; |
| 1352 | } |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1353 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1354 | case bitc::FUNC_CODE_INST_EXTRACTVAL: { |
| 1355 | // EXTRACTVAL: [opty, opval, n x indices] |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 1356 | unsigned OpNum = 0; |
| 1357 | Value *Agg; |
| 1358 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
| 1359 | return Error("Invalid EXTRACTVAL record"); |
| 1360 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1361 | SmallVector<unsigned, 4> EXTRACTVALIdx; |
| 1362 | for (unsigned RecSize = Record.size(); |
| 1363 | OpNum != RecSize; ++OpNum) { |
| 1364 | uint64_t Index = Record[OpNum]; |
| 1365 | if ((unsigned)Index != Index) |
| 1366 | return Error("Invalid EXTRACTVAL index"); |
| 1367 | EXTRACTVALIdx.push_back((unsigned)Index); |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | I = ExtractValueInst::Create(Agg, |
| 1371 | EXTRACTVALIdx.begin(), EXTRACTVALIdx.end()); |
| 1372 | break; |
| 1373 | } |
| 1374 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1375 | case bitc::FUNC_CODE_INST_INSERTVAL: { |
| 1376 | // INSERTVAL: [opty, opval, opty, opval, n x indices] |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 1377 | unsigned OpNum = 0; |
| 1378 | Value *Agg; |
| 1379 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
| 1380 | return Error("Invalid INSERTVAL record"); |
| 1381 | Value *Val; |
| 1382 | if (getValueTypePair(Record, OpNum, NextValueNo, Val)) |
| 1383 | return Error("Invalid INSERTVAL record"); |
| 1384 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1385 | SmallVector<unsigned, 4> INSERTVALIdx; |
| 1386 | for (unsigned RecSize = Record.size(); |
| 1387 | OpNum != RecSize; ++OpNum) { |
| 1388 | uint64_t Index = Record[OpNum]; |
| 1389 | if ((unsigned)Index != Index) |
| 1390 | return Error("Invalid INSERTVAL index"); |
| 1391 | INSERTVALIdx.push_back((unsigned)Index); |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | I = InsertValueInst::Create(Agg, Val, |
| 1395 | INSERTVALIdx.begin(), INSERTVALIdx.end()); |
| 1396 | break; |
| 1397 | } |
| 1398 | |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1399 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] |
| 1400 | unsigned OpNum = 0; |
| 1401 | Value *TrueVal, *FalseVal, *Cond; |
| 1402 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
| 1403 | getValue(Record, OpNum, TrueVal->getType(), FalseVal) || |
| 1404 | getValue(Record, OpNum, Type::Int1Ty, Cond)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1405 | return Error("Invalid SELECT record"); |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1406 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1407 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1408 | break; |
| 1409 | } |
| 1410 | |
| 1411 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1412 | unsigned OpNum = 0; |
| 1413 | Value *Vec, *Idx; |
| 1414 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || |
| 1415 | getValue(Record, OpNum, Type::Int32Ty, Idx)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1416 | return Error("Invalid EXTRACTELT record"); |
| 1417 | I = new ExtractElementInst(Vec, Idx); |
| 1418 | break; |
| 1419 | } |
| 1420 | |
| 1421 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1422 | unsigned OpNum = 0; |
| 1423 | Value *Vec, *Elt, *Idx; |
| 1424 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || |
| 1425 | getValue(Record, OpNum, |
| 1426 | cast<VectorType>(Vec->getType())->getElementType(), Elt) || |
| 1427 | getValue(Record, OpNum, Type::Int32Ty, Idx)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1428 | return Error("Invalid INSERTELT record"); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1429 | I = InsertElementInst::Create(Vec, Elt, Idx); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1430 | break; |
| 1431 | } |
| 1432 | |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1433 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] |
| 1434 | unsigned OpNum = 0; |
| 1435 | Value *Vec1, *Vec2, *Mask; |
| 1436 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || |
| 1437 | getValue(Record, OpNum, Vec1->getType(), Vec2)) |
| 1438 | return Error("Invalid SHUFFLEVEC record"); |
| 1439 | |
| 1440 | const Type *MaskTy = |
| 1441 | VectorType::get(Type::Int32Ty, |
| 1442 | cast<VectorType>(Vec1->getType())->getNumElements()); |
| 1443 | |
| 1444 | if (getValue(Record, OpNum, MaskTy, Mask)) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1445 | return Error("Invalid SHUFFLEVEC record"); |
| 1446 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); |
| 1447 | break; |
| 1448 | } |
| 1449 | |
| 1450 | case bitc::FUNC_CODE_INST_CMP: { // CMP: [opty, opval, opval, pred] |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1451 | unsigned OpNum = 0; |
| 1452 | Value *LHS, *RHS; |
| 1453 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
| 1454 | getValue(Record, OpNum, LHS->getType(), RHS) || |
| 1455 | OpNum+1 != Record.size()) |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1456 | return Error("Invalid CMP record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1457 | |
Nate Begeman | baa64eb | 2008-05-12 20:33:52 +0000 | [diff] [blame] | 1458 | if (LHS->getType()->isFloatingPoint()) |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1459 | I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); |
Nate Begeman | baa64eb | 2008-05-12 20:33:52 +0000 | [diff] [blame] | 1460 | else if (!isa<VectorType>(LHS->getType())) |
| 1461 | I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS); |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1462 | else if (LHS->getType()->isFPOrFPVector()) |
| 1463 | I = new VFCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); |
| 1464 | else |
| 1465 | I = new VICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS); |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1466 | break; |
| 1467 | } |
Devang Patel | 197be3d | 2008-02-22 02:49:49 +0000 | [diff] [blame] | 1468 | case bitc::FUNC_CODE_INST_GETRESULT: { // GETRESULT: [ty, val, n] |
| 1469 | if (Record.size() != 2) |
| 1470 | return Error("Invalid GETRESULT record"); |
| 1471 | unsigned OpNum = 0; |
| 1472 | Value *Op; |
| 1473 | getValueTypePair(Record, OpNum, NextValueNo, Op); |
| 1474 | unsigned Index = Record[1]; |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1475 | I = ExtractValueInst::Create(Op, Index); |
Devang Patel | 197be3d | 2008-02-22 02:49:49 +0000 | [diff] [blame] | 1476 | break; |
| 1477 | } |
Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1478 | |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1479 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] |
Devang Patel | d9d99ff | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 1480 | { |
| 1481 | unsigned Size = Record.size(); |
| 1482 | if (Size == 0) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1483 | I = ReturnInst::Create(); |
Devang Patel | d9d99ff | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 1484 | break; |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1485 | } |
Devang Patel | d9d99ff | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 1486 | |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1487 | unsigned OpNum = 0; |
| 1488 | SmallVector<Value *,4> Vs; |
| 1489 | do { |
| 1490 | Value *Op = NULL; |
| 1491 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
| 1492 | return Error("Invalid RET record"); |
| 1493 | Vs.push_back(Op); |
| 1494 | } while(OpNum != Record.size()); |
| 1495 | |
| 1496 | const Type *ReturnType = F->getReturnType(); |
| 1497 | if (Vs.size() > 1 || |
| 1498 | (isa<StructType>(ReturnType) && |
| 1499 | (Vs.empty() || Vs[0]->getType() != ReturnType))) { |
| 1500 | Value *RV = UndefValue::get(ReturnType); |
| 1501 | for (unsigned i = 0, e = Vs.size(); i != e; ++i) { |
| 1502 | I = InsertValueInst::Create(RV, Vs[i], i, "mrv"); |
| 1503 | CurBB->getInstList().push_back(I); |
| 1504 | ValueList.AssignValue(I, NextValueNo++); |
| 1505 | RV = I; |
| 1506 | } |
| 1507 | I = ReturnInst::Create(RV); |
Devang Patel | d9d99ff | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 1508 | break; |
| 1509 | } |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1510 | |
| 1511 | I = ReturnInst::Create(Vs[0]); |
| 1512 | break; |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1513 | } |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1514 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] |
Chris Lattner | f61e645 | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 1515 | if (Record.size() != 1 && Record.size() != 3) |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1516 | return Error("Invalid BR record"); |
| 1517 | BasicBlock *TrueDest = getBasicBlock(Record[0]); |
| 1518 | if (TrueDest == 0) |
| 1519 | return Error("Invalid BR record"); |
| 1520 | |
| 1521 | if (Record.size() == 1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1522 | I = BranchInst::Create(TrueDest); |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1523 | else { |
| 1524 | BasicBlock *FalseDest = getBasicBlock(Record[1]); |
| 1525 | Value *Cond = getFnValueByID(Record[2], Type::Int1Ty); |
| 1526 | if (FalseDest == 0 || Cond == 0) |
| 1527 | return Error("Invalid BR record"); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1528 | I = BranchInst::Create(TrueDest, FalseDest, Cond); |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1529 | } |
| 1530 | break; |
| 1531 | } |
| 1532 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, opval, n, n x ops] |
| 1533 | if (Record.size() < 3 || (Record.size() & 1) == 0) |
| 1534 | return Error("Invalid SWITCH record"); |
| 1535 | const Type *OpTy = getTypeByID(Record[0]); |
| 1536 | Value *Cond = getFnValueByID(Record[1], OpTy); |
| 1537 | BasicBlock *Default = getBasicBlock(Record[2]); |
| 1538 | if (OpTy == 0 || Cond == 0 || Default == 0) |
| 1539 | return Error("Invalid SWITCH record"); |
| 1540 | unsigned NumCases = (Record.size()-3)/2; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1541 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1542 | for (unsigned i = 0, e = NumCases; i != e; ++i) { |
| 1543 | ConstantInt *CaseVal = |
| 1544 | dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); |
| 1545 | BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); |
| 1546 | if (CaseVal == 0 || DestBB == 0) { |
| 1547 | delete SI; |
| 1548 | return Error("Invalid SWITCH record!"); |
| 1549 | } |
| 1550 | SI->addCase(CaseVal, DestBB); |
| 1551 | } |
| 1552 | I = SI; |
| 1553 | break; |
| 1554 | } |
| 1555 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1556 | case bitc::FUNC_CODE_INST_INVOKE: { |
| 1557 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1558 | if (Record.size() < 4) return Error("Invalid INVOKE record"); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1559 | PAListPtr PAL = getParamAttrs(Record[0]); |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1560 | unsigned CCInfo = Record[1]; |
| 1561 | BasicBlock *NormalBB = getBasicBlock(Record[2]); |
| 1562 | BasicBlock *UnwindBB = getBasicBlock(Record[3]); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1564 | unsigned OpNum = 4; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1565 | Value *Callee; |
| 1566 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1567 | return Error("Invalid INVOKE record"); |
| 1568 | |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1569 | const PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); |
| 1570 | const FunctionType *FTy = !CalleeTy ? 0 : |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1571 | dyn_cast<FunctionType>(CalleeTy->getElementType()); |
| 1572 | |
| 1573 | // Check that the right number of fixed parameters are here. |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1574 | if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 || |
| 1575 | Record.size() < OpNum+FTy->getNumParams()) |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1576 | return Error("Invalid INVOKE record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1577 | |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1578 | SmallVector<Value*, 16> Ops; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1579 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
| 1580 | Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); |
| 1581 | if (Ops.back() == 0) return Error("Invalid INVOKE record"); |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1584 | if (!FTy->isVarArg()) { |
| 1585 | if (Record.size() != OpNum) |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1586 | return Error("Invalid INVOKE record"); |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1587 | } else { |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1588 | // Read type/value pairs for varargs params. |
| 1589 | while (OpNum != Record.size()) { |
| 1590 | Value *Op; |
| 1591 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
| 1592 | return Error("Invalid INVOKE record"); |
| 1593 | Ops.push_back(Op); |
| 1594 | } |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 1597 | I = InvokeInst::Create(Callee, NormalBB, UnwindBB, |
| 1598 | Ops.begin(), Ops.end()); |
Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1599 | cast<InvokeInst>(I)->setCallingConv(CCInfo); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1600 | cast<InvokeInst>(I)->setParamAttrs(PAL); |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1601 | break; |
| 1602 | } |
Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1603 | case bitc::FUNC_CODE_INST_UNWIND: // UNWIND |
| 1604 | I = new UnwindInst(); |
| 1605 | break; |
| 1606 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE |
| 1607 | I = new UnreachableInst(); |
| 1608 | break; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1609 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1610 | if (Record.size() < 1 || ((Record.size()-1)&1)) |
Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1611 | return Error("Invalid PHI record"); |
| 1612 | const Type *Ty = getTypeByID(Record[0]); |
| 1613 | if (!Ty) return Error("Invalid PHI record"); |
| 1614 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1615 | PHINode *PN = PHINode::Create(Ty); |
Chris Lattner | 8694161 | 2008-04-13 00:14:42 +0000 | [diff] [blame] | 1616 | PN->reserveOperandSpace((Record.size()-1)/2); |
Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1617 | |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1618 | for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { |
| 1619 | Value *V = getFnValueByID(Record[1+i], Ty); |
| 1620 | BasicBlock *BB = getBasicBlock(Record[2+i]); |
Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1621 | if (!V || !BB) return Error("Invalid PHI record"); |
| 1622 | PN->addIncoming(V, BB); |
| 1623 | } |
| 1624 | I = PN; |
| 1625 | break; |
| 1626 | } |
| 1627 | |
| 1628 | case bitc::FUNC_CODE_INST_MALLOC: { // MALLOC: [instty, op, align] |
| 1629 | if (Record.size() < 3) |
| 1630 | return Error("Invalid MALLOC record"); |
| 1631 | const PointerType *Ty = |
| 1632 | dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); |
| 1633 | Value *Size = getFnValueByID(Record[1], Type::Int32Ty); |
| 1634 | unsigned Align = Record[2]; |
| 1635 | if (!Ty || !Size) return Error("Invalid MALLOC record"); |
| 1636 | I = new MallocInst(Ty->getElementType(), Size, (1 << Align) >> 1); |
| 1637 | break; |
| 1638 | } |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1639 | case bitc::FUNC_CODE_INST_FREE: { // FREE: [op, opty] |
| 1640 | unsigned OpNum = 0; |
| 1641 | Value *Op; |
| 1642 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 1643 | OpNum != Record.size()) |
Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1644 | return Error("Invalid FREE record"); |
| 1645 | I = new FreeInst(Op); |
| 1646 | break; |
| 1647 | } |
| 1648 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, op, align] |
| 1649 | if (Record.size() < 3) |
| 1650 | return Error("Invalid ALLOCA record"); |
| 1651 | const PointerType *Ty = |
| 1652 | dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); |
| 1653 | Value *Size = getFnValueByID(Record[1], Type::Int32Ty); |
| 1654 | unsigned Align = Record[2]; |
| 1655 | if (!Ty || !Size) return Error("Invalid ALLOCA record"); |
| 1656 | I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1); |
| 1657 | break; |
| 1658 | } |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1659 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1660 | unsigned OpNum = 0; |
| 1661 | Value *Op; |
| 1662 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 1663 | OpNum+2 != Record.size()) |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1664 | return Error("Invalid LOAD record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1665 | |
| 1666 | I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1667 | break; |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1668 | } |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1669 | case bitc::FUNC_CODE_INST_STORE2: { // STORE2:[ptrty, ptr, val, align, vol] |
| 1670 | unsigned OpNum = 0; |
| 1671 | Value *Val, *Ptr; |
| 1672 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
| 1673 | getValue(Record, OpNum, |
| 1674 | cast<PointerType>(Ptr->getType())->getElementType(), Val) || |
| 1675 | OpNum+2 != Record.size()) |
| 1676 | return Error("Invalid STORE record"); |
| 1677 | |
| 1678 | I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); |
| 1679 | break; |
| 1680 | } |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1681 | case bitc::FUNC_CODE_INST_STORE: { // STORE:[val, valty, ptr, align, vol] |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1682 | // 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] | 1683 | unsigned OpNum = 0; |
| 1684 | Value *Val, *Ptr; |
| 1685 | if (getValueTypePair(Record, OpNum, NextValueNo, Val) || |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1686 | getValue(Record, OpNum, PointerType::getUnqual(Val->getType()), Ptr)|| |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1687 | OpNum+2 != Record.size()) |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1688 | return Error("Invalid STORE record"); |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1689 | |
| 1690 | I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1691 | break; |
| 1692 | } |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1693 | case bitc::FUNC_CODE_INST_CALL: { |
| 1694 | // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...] |
| 1695 | if (Record.size() < 3) |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1696 | return Error("Invalid CALL record"); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1697 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1698 | PAListPtr PAL = getParamAttrs(Record[0]); |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1699 | unsigned CCInfo = Record[1]; |
| 1700 | |
| 1701 | unsigned OpNum = 2; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1702 | Value *Callee; |
| 1703 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
| 1704 | return Error("Invalid CALL record"); |
| 1705 | |
| 1706 | const PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1707 | const FunctionType *FTy = 0; |
| 1708 | if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType()); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1709 | if (!FTy || Record.size() < FTy->getNumParams()+OpNum) |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1710 | return Error("Invalid CALL record"); |
| 1711 | |
| 1712 | SmallVector<Value*, 16> Args; |
| 1713 | // Read the fixed params. |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1714 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1715 | if (FTy->getParamType(i)->getTypeID()==Type::LabelTyID) |
| 1716 | Args.push_back(getBasicBlock(Record[OpNum])); |
| 1717 | else |
| 1718 | Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1719 | if (Args.back() == 0) return Error("Invalid CALL record"); |
| 1720 | } |
| 1721 | |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1722 | // Read type/value pairs for varargs params. |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1723 | if (!FTy->isVarArg()) { |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1724 | if (OpNum != Record.size()) |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1725 | return Error("Invalid CALL record"); |
| 1726 | } else { |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1727 | while (OpNum != Record.size()) { |
| 1728 | Value *Op; |
| 1729 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
| 1730 | return Error("Invalid CALL record"); |
| 1731 | Args.push_back(Op); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1735 | I = CallInst::Create(Callee, Args.begin(), Args.end()); |
Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1736 | cast<CallInst>(I)->setCallingConv(CCInfo>>1); |
| 1737 | cast<CallInst>(I)->setTailCall(CCInfo & 1); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1738 | cast<CallInst>(I)->setParamAttrs(PAL); |
Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1739 | break; |
| 1740 | } |
| 1741 | case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] |
| 1742 | if (Record.size() < 3) |
| 1743 | return Error("Invalid VAARG record"); |
| 1744 | const Type *OpTy = getTypeByID(Record[0]); |
| 1745 | Value *Op = getFnValueByID(Record[1], OpTy); |
| 1746 | const Type *ResTy = getTypeByID(Record[2]); |
| 1747 | if (!OpTy || !Op || !ResTy) |
| 1748 | return Error("Invalid VAARG record"); |
| 1749 | I = new VAArgInst(Op, ResTy); |
| 1750 | break; |
| 1751 | } |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | // Add instruction to end of current BB. If there is no current BB, reject |
| 1755 | // this file. |
| 1756 | if (CurBB == 0) { |
| 1757 | delete I; |
| 1758 | return Error("Invalid instruction with no BB"); |
| 1759 | } |
| 1760 | CurBB->getInstList().push_back(I); |
| 1761 | |
| 1762 | // If this was a terminator instruction, move to the next block. |
| 1763 | if (isa<TerminatorInst>(I)) { |
| 1764 | ++CurBBNo; |
| 1765 | CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0; |
| 1766 | } |
| 1767 | |
| 1768 | // Non-void values get registered in the value table for future use. |
| 1769 | if (I && I->getType() != Type::VoidTy) |
| 1770 | ValueList.AssignValue(I, NextValueNo++); |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1773 | // Check the function list for unresolved values. |
| 1774 | if (Argument *A = dyn_cast<Argument>(ValueList.back())) { |
| 1775 | if (A->getParent() == 0) { |
| 1776 | // We found at least one unresolved value. Nuke them all to avoid leaks. |
| 1777 | for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ |
| 1778 | if ((A = dyn_cast<Argument>(ValueList.back())) && A->getParent() == 0) { |
| 1779 | A->replaceAllUsesWith(UndefValue::get(A->getType())); |
| 1780 | delete A; |
| 1781 | } |
| 1782 | } |
Chris Lattner | 35a0470 | 2007-05-04 03:50:29 +0000 | [diff] [blame] | 1783 | return Error("Never resolved value found in function!"); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1784 | } |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1785 | } |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1786 | |
| 1787 | // Trim the value list down to the size it was before we parsed this function. |
| 1788 | ValueList.shrinkTo(ModuleValueListSize); |
| 1789 | std::vector<BasicBlock*>().swap(FunctionBBs); |
| 1790 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1791 | return false; |
| 1792 | } |
| 1793 | |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1794 | //===----------------------------------------------------------------------===// |
| 1795 | // ModuleProvider implementation |
| 1796 | //===----------------------------------------------------------------------===// |
| 1797 | |
| 1798 | |
| 1799 | bool BitcodeReader::materializeFunction(Function *F, std::string *ErrInfo) { |
| 1800 | // If it already is material, ignore the request. |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 1801 | if (!F->hasNotBeenReadFromBitcode()) return false; |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1802 | |
| 1803 | DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator DFII = |
| 1804 | DeferredFunctionInfo.find(F); |
| 1805 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); |
| 1806 | |
| 1807 | // Move the bit stream to the saved position of the deferred function body and |
| 1808 | // restore the real linkage type for the function. |
| 1809 | Stream.JumpToBit(DFII->second.first); |
| 1810 | F->setLinkage((GlobalValue::LinkageTypes)DFII->second.second); |
| 1811 | |
| 1812 | if (ParseFunctionBody(F)) { |
| 1813 | if (ErrInfo) *ErrInfo = ErrorString; |
| 1814 | return true; |
| 1815 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1816 | |
| 1817 | // Upgrade any old intrinsic calls in the function. |
| 1818 | for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(), |
| 1819 | E = UpgradedIntrinsics.end(); I != E; ++I) { |
| 1820 | if (I->first != I->second) { |
| 1821 | for (Value::use_iterator UI = I->first->use_begin(), |
| 1822 | UE = I->first->use_end(); UI != UE; ) { |
| 1823 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 1824 | UpgradeIntrinsicCall(CI, I->second); |
| 1825 | } |
| 1826 | } |
| 1827 | } |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1828 | |
| 1829 | return false; |
| 1830 | } |
| 1831 | |
| 1832 | void BitcodeReader::dematerializeFunction(Function *F) { |
| 1833 | // 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] | 1834 | if (F->hasNotBeenReadFromBitcode() || F->isDeclaration()) |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1835 | return; |
| 1836 | |
| 1837 | assert(DeferredFunctionInfo.count(F) && "No info to read function later?"); |
| 1838 | |
| 1839 | // Just forget the function body, we can remat it later. |
| 1840 | F->deleteBody(); |
| 1841 | F->setLinkage(GlobalValue::GhostLinkage); |
| 1842 | } |
| 1843 | |
| 1844 | |
| 1845 | Module *BitcodeReader::materializeModule(std::string *ErrInfo) { |
| 1846 | for (DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator I = |
| 1847 | DeferredFunctionInfo.begin(), E = DeferredFunctionInfo.end(); I != E; |
| 1848 | ++I) { |
| 1849 | Function *F = I->first; |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 1850 | if (F->hasNotBeenReadFromBitcode() && |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1851 | materializeFunction(F, ErrInfo)) |
| 1852 | return 0; |
| 1853 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1854 | |
| 1855 | // Upgrade any intrinsic calls that slipped through (should not happen!) and |
| 1856 | // delete the old functions to clean up. We can't do this unless the entire |
| 1857 | // module is materialized because there could always be another function body |
| 1858 | // with calls to the old function. |
| 1859 | for (std::vector<std::pair<Function*, Function*> >::iterator I = |
| 1860 | UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) { |
| 1861 | if (I->first != I->second) { |
| 1862 | for (Value::use_iterator UI = I->first->use_begin(), |
| 1863 | UE = I->first->use_end(); UI != UE; ) { |
| 1864 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 1865 | UpgradeIntrinsicCall(CI, I->second); |
| 1866 | } |
| 1867 | ValueList.replaceUsesOfWith(I->first, I->second); |
| 1868 | I->first->eraseFromParent(); |
| 1869 | } |
| 1870 | } |
| 1871 | std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics); |
| 1872 | |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1873 | return TheModule; |
| 1874 | } |
| 1875 | |
| 1876 | |
| 1877 | /// This method is provided by the parent ModuleProvde class and overriden |
| 1878 | /// here. It simply releases the module from its provided and frees up our |
| 1879 | /// state. |
| 1880 | /// @brief Release our hold on the generated module |
| 1881 | Module *BitcodeReader::releaseModule(std::string *ErrInfo) { |
| 1882 | // Since we're losing control of this Module, we must hand it back complete |
| 1883 | Module *M = ModuleProvider::releaseModule(ErrInfo); |
| 1884 | FreeState(); |
| 1885 | return M; |
| 1886 | } |
| 1887 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1889 | //===----------------------------------------------------------------------===// |
| 1890 | // External interface |
| 1891 | //===----------------------------------------------------------------------===// |
| 1892 | |
| 1893 | /// getBitcodeModuleProvider - lazy function-at-a-time loading from a file. |
| 1894 | /// |
| 1895 | ModuleProvider *llvm::getBitcodeModuleProvider(MemoryBuffer *Buffer, |
| 1896 | std::string *ErrMsg) { |
| 1897 | BitcodeReader *R = new BitcodeReader(Buffer); |
| 1898 | if (R->ParseBitcode()) { |
| 1899 | if (ErrMsg) |
| 1900 | *ErrMsg = R->getErrorString(); |
| 1901 | |
| 1902 | // Don't let the BitcodeReader dtor delete 'Buffer'. |
| 1903 | R->releaseMemoryBuffer(); |
| 1904 | delete R; |
| 1905 | return 0; |
| 1906 | } |
| 1907 | return R; |
| 1908 | } |
| 1909 | |
| 1910 | /// ParseBitcodeFile - Read the specified bitcode file, returning the module. |
| 1911 | /// If an error occurs, return null and fill in *ErrMsg if non-null. |
| 1912 | Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg){ |
| 1913 | BitcodeReader *R; |
| 1914 | R = static_cast<BitcodeReader*>(getBitcodeModuleProvider(Buffer, ErrMsg)); |
| 1915 | if (!R) return 0; |
| 1916 | |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1917 | // Read in the entire module. |
| 1918 | Module *M = R->materializeModule(ErrMsg); |
| 1919 | |
| 1920 | // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether |
| 1921 | // there was an error. |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1922 | R->releaseMemoryBuffer(); |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 1923 | |
| 1924 | // If there was no error, tell ModuleProvider not to delete it when its dtor |
| 1925 | // is run. |
| 1926 | if (M) |
| 1927 | M = R->releaseModule(ErrMsg); |
| 1928 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1929 | delete R; |
| 1930 | return M; |
| 1931 | } |