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