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" |
| 18 | #include "llvm/Module.h" |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 24 | BitcodeReader::~BitcodeReader() { |
| 25 | delete Buffer; |
| 26 | } |
| 27 | |
| 28 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 29 | /// ConvertToString - Convert a string from a record into an std::string, return |
| 30 | /// true on failure. |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 31 | template<typename StrTy> |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 32 | static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx, |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 33 | StrTy &Result) { |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 34 | if (Record.size() < Idx+1 || Record.size() < Record[Idx]+Idx+1) |
| 35 | return true; |
| 36 | |
| 37 | for (unsigned i = 0, e = Record[Idx]; i != e; ++i) |
| 38 | Result += (char)Record[Idx+i+1]; |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { |
| 43 | switch (Val) { |
| 44 | default: // Map unknown/new linkages to external |
| 45 | case 0: return GlobalValue::ExternalLinkage; |
| 46 | case 1: return GlobalValue::WeakLinkage; |
| 47 | case 2: return GlobalValue::AppendingLinkage; |
| 48 | case 3: return GlobalValue::InternalLinkage; |
| 49 | case 4: return GlobalValue::LinkOnceLinkage; |
| 50 | case 5: return GlobalValue::DLLImportLinkage; |
| 51 | case 6: return GlobalValue::DLLExportLinkage; |
| 52 | case 7: return GlobalValue::ExternalWeakLinkage; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { |
| 57 | switch (Val) { |
| 58 | default: // Map unknown visibilities to default. |
| 59 | case 0: return GlobalValue::DefaultVisibility; |
| 60 | case 1: return GlobalValue::HiddenVisibility; |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 61 | case 2: return GlobalValue::ProtectedVisibility; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 65 | static int GetDecodedCastOpcode(unsigned Val) { |
| 66 | switch (Val) { |
| 67 | default: return -1; |
| 68 | case bitc::CAST_TRUNC : return Instruction::Trunc; |
| 69 | case bitc::CAST_ZEXT : return Instruction::ZExt; |
| 70 | case bitc::CAST_SEXT : return Instruction::SExt; |
| 71 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
| 72 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
| 73 | case bitc::CAST_UITOFP : return Instruction::UIToFP; |
| 74 | case bitc::CAST_SITOFP : return Instruction::SIToFP; |
| 75 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
| 76 | case bitc::CAST_FPEXT : return Instruction::FPExt; |
| 77 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
| 78 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
| 79 | case bitc::CAST_BITCAST : return Instruction::BitCast; |
| 80 | } |
| 81 | } |
| 82 | static int GetDecodedBinaryOpcode(unsigned Val, const Type *Ty) { |
| 83 | switch (Val) { |
| 84 | default: return -1; |
| 85 | case bitc::BINOP_ADD: return Instruction::Add; |
| 86 | case bitc::BINOP_SUB: return Instruction::Sub; |
| 87 | case bitc::BINOP_MUL: return Instruction::Mul; |
| 88 | case bitc::BINOP_UDIV: return Instruction::UDiv; |
| 89 | case bitc::BINOP_SDIV: |
| 90 | return Ty->isFPOrFPVector() ? Instruction::FDiv : Instruction::SDiv; |
| 91 | case bitc::BINOP_UREM: return Instruction::URem; |
| 92 | case bitc::BINOP_SREM: |
| 93 | return Ty->isFPOrFPVector() ? Instruction::FRem : Instruction::SRem; |
| 94 | case bitc::BINOP_SHL: return Instruction::Shl; |
| 95 | case bitc::BINOP_LSHR: return Instruction::LShr; |
| 96 | case bitc::BINOP_ASHR: return Instruction::AShr; |
| 97 | case bitc::BINOP_AND: return Instruction::And; |
| 98 | case bitc::BINOP_OR: return Instruction::Or; |
| 99 | case bitc::BINOP_XOR: return Instruction::Xor; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 104 | namespace { |
| 105 | /// @brief A class for maintaining the slot number definition |
| 106 | /// as a placeholder for the actual definition for forward constants defs. |
| 107 | class ConstantPlaceHolder : public ConstantExpr { |
| 108 | ConstantPlaceHolder(); // DO NOT IMPLEMENT |
| 109 | void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 110 | public: |
| 111 | Use Op; |
| 112 | ConstantPlaceHolder(const Type *Ty) |
| 113 | : ConstantExpr(Ty, Instruction::UserOp1, &Op, 1), |
| 114 | Op(UndefValue::get(Type::Int32Ty), this) { |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 115 | } |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, |
| 120 | const Type *Ty) { |
| 121 | if (Idx >= size()) { |
| 122 | // Insert a bunch of null values. |
| 123 | Uses.resize(Idx+1); |
| 124 | OperandList = &Uses[0]; |
| 125 | NumOperands = Idx+1; |
| 126 | } |
| 127 | |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 128 | if (Uses[Idx]) { |
| 129 | assert(Ty == getOperand(Idx)->getType() && |
| 130 | "Type mismatch in constant table!"); |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 131 | return cast<Constant>(getOperand(Idx)); |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 132 | } |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 133 | |
| 134 | // Create and return a placeholder, which will later be RAUW'd. |
| 135 | Constant *C = new ConstantPlaceHolder(Ty); |
| 136 | Uses[Idx].init(C, this); |
| 137 | return C; |
| 138 | } |
| 139 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 140 | |
| 141 | const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) { |
| 142 | // If the TypeID is in range, return it. |
| 143 | if (ID < TypeList.size()) |
| 144 | return TypeList[ID].get(); |
| 145 | if (!isTypeTable) return 0; |
| 146 | |
| 147 | // The type table allows forward references. Push as many Opaque types as |
| 148 | // needed to get up to ID. |
| 149 | while (TypeList.size() <= ID) |
| 150 | TypeList.push_back(OpaqueType::get()); |
| 151 | return TypeList.back().get(); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | bool BitcodeReader::ParseTypeTable(BitstreamReader &Stream) { |
| 156 | if (Stream.EnterSubBlock()) |
| 157 | return Error("Malformed block record"); |
| 158 | |
| 159 | if (!TypeList.empty()) |
| 160 | return Error("Multiple TYPE_BLOCKs found!"); |
| 161 | |
| 162 | SmallVector<uint64_t, 64> Record; |
| 163 | unsigned NumRecords = 0; |
| 164 | |
| 165 | // Read all the records for this type table. |
| 166 | while (1) { |
| 167 | unsigned Code = Stream.ReadCode(); |
| 168 | if (Code == bitc::END_BLOCK) { |
| 169 | if (NumRecords != TypeList.size()) |
| 170 | return Error("Invalid type forward reference in TYPE_BLOCK"); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 171 | if (Stream.ReadBlockEnd()) |
| 172 | return Error("Error at end of type table block"); |
| 173 | return false; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 177 | // No known subblocks, always skip them. |
| 178 | Stream.ReadSubBlockID(); |
| 179 | if (Stream.SkipBlock()) |
| 180 | return Error("Malformed block record"); |
| 181 | continue; |
| 182 | } |
| 183 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 184 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 185 | Stream.ReadAbbrevRecord(); |
| 186 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Read a record. |
| 190 | Record.clear(); |
| 191 | const Type *ResultTy = 0; |
| 192 | switch (Stream.ReadRecord(Code, Record)) { |
| 193 | default: // Default behavior: unknown type. |
| 194 | ResultTy = 0; |
| 195 | break; |
| 196 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
| 197 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
| 198 | // type list. This allows us to reserve space. |
| 199 | if (Record.size() < 1) |
| 200 | return Error("Invalid TYPE_CODE_NUMENTRY record"); |
| 201 | TypeList.reserve(Record[0]); |
| 202 | continue; |
| 203 | case bitc::TYPE_CODE_META: // TYPE_CODE_META: [metacode]... |
| 204 | // No metadata supported yet. |
| 205 | if (Record.size() < 1) |
| 206 | return Error("Invalid TYPE_CODE_META record"); |
| 207 | continue; |
| 208 | |
| 209 | case bitc::TYPE_CODE_VOID: // VOID |
| 210 | ResultTy = Type::VoidTy; |
| 211 | break; |
| 212 | case bitc::TYPE_CODE_FLOAT: // FLOAT |
| 213 | ResultTy = Type::FloatTy; |
| 214 | break; |
| 215 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
| 216 | ResultTy = Type::DoubleTy; |
| 217 | break; |
| 218 | case bitc::TYPE_CODE_LABEL: // LABEL |
| 219 | ResultTy = Type::LabelTy; |
| 220 | break; |
| 221 | case bitc::TYPE_CODE_OPAQUE: // OPAQUE |
| 222 | ResultTy = 0; |
| 223 | break; |
| 224 | case bitc::TYPE_CODE_INTEGER: // INTEGER: [width] |
| 225 | if (Record.size() < 1) |
| 226 | return Error("Invalid Integer type record"); |
| 227 | |
| 228 | ResultTy = IntegerType::get(Record[0]); |
| 229 | break; |
| 230 | case bitc::TYPE_CODE_POINTER: // POINTER: [pointee type] |
| 231 | if (Record.size() < 1) |
| 232 | return Error("Invalid POINTER type record"); |
| 233 | ResultTy = PointerType::get(getTypeByID(Record[0], true)); |
| 234 | break; |
| 235 | case bitc::TYPE_CODE_FUNCTION: { |
| 236 | // FUNCTION: [vararg, retty, #pararms, paramty N] |
| 237 | if (Record.size() < 3 || Record.size() < Record[2]+3) |
| 238 | return Error("Invalid FUNCTION type record"); |
| 239 | std::vector<const Type*> ArgTys; |
| 240 | for (unsigned i = 0, e = Record[2]; i != e; ++i) |
| 241 | ArgTys.push_back(getTypeByID(Record[3+i], true)); |
| 242 | |
| 243 | // FIXME: PARAM TYS. |
| 244 | ResultTy = FunctionType::get(getTypeByID(Record[1], true), ArgTys, |
| 245 | Record[0]); |
| 246 | break; |
| 247 | } |
| 248 | case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, #elts, eltty x N] |
| 249 | if (Record.size() < 2 || Record.size() < Record[1]+2) |
| 250 | return Error("Invalid STRUCT type record"); |
| 251 | std::vector<const Type*> EltTys; |
| 252 | for (unsigned i = 0, e = Record[1]; i != e; ++i) |
| 253 | EltTys.push_back(getTypeByID(Record[2+i], true)); |
| 254 | ResultTy = StructType::get(EltTys, Record[0]); |
| 255 | break; |
| 256 | } |
| 257 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
| 258 | if (Record.size() < 2) |
| 259 | return Error("Invalid ARRAY type record"); |
| 260 | ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]); |
| 261 | break; |
| 262 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] |
| 263 | if (Record.size() < 2) |
| 264 | return Error("Invalid VECTOR type record"); |
| 265 | ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]); |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | if (NumRecords == TypeList.size()) { |
| 270 | // If this is a new type slot, just append it. |
| 271 | TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get()); |
| 272 | ++NumRecords; |
| 273 | } else if (ResultTy == 0) { |
| 274 | // Otherwise, this was forward referenced, so an opaque type was created, |
| 275 | // but the result type is actually just an opaque. Leave the one we |
| 276 | // created previously. |
| 277 | ++NumRecords; |
| 278 | } else { |
| 279 | // Otherwise, this was forward referenced, so an opaque type was created. |
| 280 | // Resolve the opaque type to the real type now. |
| 281 | assert(NumRecords < TypeList.size() && "Typelist imbalance"); |
| 282 | const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get()); |
| 283 | |
| 284 | // Don't directly push the new type on the Tab. Instead we want to replace |
| 285 | // the opaque type we previously inserted with the new concrete value. The |
| 286 | // refinement from the abstract (opaque) type to the new type causes all |
| 287 | // uses of the abstract type to use the concrete type (NewTy). This will |
| 288 | // also cause the opaque type to be deleted. |
| 289 | const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy); |
| 290 | |
| 291 | // 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] | 292 | // value table... or with a preexisting type that was already in the |
| 293 | // system. Let's just make sure it did. |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 294 | assert(TypeList[NumRecords-1].get() != OldTy && |
| 295 | "refineAbstractType didn't work!"); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | bool BitcodeReader::ParseTypeSymbolTable(BitstreamReader &Stream) { |
| 302 | if (Stream.EnterSubBlock()) |
| 303 | return Error("Malformed block record"); |
| 304 | |
| 305 | SmallVector<uint64_t, 64> Record; |
| 306 | |
| 307 | // Read all the records for this type table. |
| 308 | std::string TypeName; |
| 309 | while (1) { |
| 310 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 311 | if (Code == bitc::END_BLOCK) { |
| 312 | if (Stream.ReadBlockEnd()) |
| 313 | return Error("Error at end of type symbol table block"); |
| 314 | return false; |
| 315 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 316 | |
| 317 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 318 | // No known subblocks, always skip them. |
| 319 | Stream.ReadSubBlockID(); |
| 320 | if (Stream.SkipBlock()) |
| 321 | return Error("Malformed block record"); |
| 322 | continue; |
| 323 | } |
| 324 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 325 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 326 | Stream.ReadAbbrevRecord(); |
| 327 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // Read a record. |
| 331 | Record.clear(); |
| 332 | switch (Stream.ReadRecord(Code, Record)) { |
| 333 | default: // Default behavior: unknown type. |
| 334 | break; |
Chris Lattner | 6dbfd7b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 335 | case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namelen, namechar x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 336 | if (ConvertToString(Record, 1, TypeName)) |
| 337 | return Error("Invalid TST_ENTRY record"); |
| 338 | unsigned TypeID = Record[0]; |
| 339 | if (TypeID >= TypeList.size()) |
| 340 | return Error("Invalid Type ID in TST_ENTRY record"); |
| 341 | |
| 342 | TheModule->addTypeName(TypeName, TypeList[TypeID].get()); |
| 343 | TypeName.clear(); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 349 | bool BitcodeReader::ParseValueSymbolTable(BitstreamReader &Stream) { |
| 350 | if (Stream.EnterSubBlock()) |
| 351 | return Error("Malformed block record"); |
| 352 | |
| 353 | SmallVector<uint64_t, 64> Record; |
| 354 | |
| 355 | // Read all the records for this value table. |
| 356 | SmallString<128> ValueName; |
| 357 | while (1) { |
| 358 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 359 | if (Code == bitc::END_BLOCK) { |
| 360 | if (Stream.ReadBlockEnd()) |
| 361 | return Error("Error at end of value symbol table block"); |
| 362 | return false; |
| 363 | } |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 364 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 365 | // No known subblocks, always skip them. |
| 366 | Stream.ReadSubBlockID(); |
| 367 | if (Stream.SkipBlock()) |
| 368 | return Error("Malformed block record"); |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | if (Code == bitc::DEFINE_ABBREV) { |
| 373 | Stream.ReadAbbrevRecord(); |
| 374 | continue; |
| 375 | } |
| 376 | |
| 377 | // Read a record. |
| 378 | Record.clear(); |
| 379 | switch (Stream.ReadRecord(Code, Record)) { |
| 380 | default: // Default behavior: unknown type. |
| 381 | break; |
Chris Lattner | 6dbfd7b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 382 | case bitc::TST_CODE_ENTRY: // VST_ENTRY: [valueid, namelen, namechar x N] |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 383 | if (ConvertToString(Record, 1, ValueName)) |
| 384 | return Error("Invalid TST_ENTRY record"); |
| 385 | unsigned ValueID = Record[0]; |
| 386 | if (ValueID >= ValueList.size()) |
| 387 | return Error("Invalid Value ID in VST_ENTRY record"); |
| 388 | Value *V = ValueList[ValueID]; |
| 389 | |
| 390 | V->setName(&ValueName[0], ValueName.size()); |
| 391 | ValueName.clear(); |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 397 | /// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in |
| 398 | /// the LSB for dense VBR encoding. |
| 399 | static uint64_t DecodeSignRotatedValue(uint64_t V) { |
| 400 | if ((V & 1) == 0) |
| 401 | return V >> 1; |
| 402 | if (V != 1) |
| 403 | return -(V >> 1); |
| 404 | // There is no such thing as -0 with integers. "-0" really means MININT. |
| 405 | return 1ULL << 63; |
| 406 | } |
| 407 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 408 | /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global |
| 409 | /// values and aliases that we can. |
| 410 | bool BitcodeReader::ResolveGlobalAndAliasInits() { |
| 411 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; |
| 412 | std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; |
| 413 | |
| 414 | GlobalInitWorklist.swap(GlobalInits); |
| 415 | AliasInitWorklist.swap(AliasInits); |
| 416 | |
| 417 | while (!GlobalInitWorklist.empty()) { |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 418 | unsigned ValID = GlobalInitWorklist.back().second; |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 419 | if (ValID >= ValueList.size()) { |
| 420 | // 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] | 421 | GlobalInits.push_back(GlobalInitWorklist.back()); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 422 | } else { |
| 423 | if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) |
| 424 | GlobalInitWorklist.back().first->setInitializer(C); |
| 425 | else |
| 426 | return Error("Global variable initializer is not a constant!"); |
| 427 | } |
| 428 | GlobalInitWorklist.pop_back(); |
| 429 | } |
| 430 | |
| 431 | while (!AliasInitWorklist.empty()) { |
| 432 | unsigned ValID = AliasInitWorklist.back().second; |
| 433 | if (ValID >= ValueList.size()) { |
| 434 | AliasInits.push_back(AliasInitWorklist.back()); |
| 435 | } else { |
| 436 | if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) |
Anton Korobeynikov | 7dde0ff | 2007-04-28 14:57:59 +0000 | [diff] [blame] | 437 | AliasInitWorklist.back().first->setAliasee(C); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 438 | else |
| 439 | return Error("Alias initializer is not a constant!"); |
| 440 | } |
| 441 | AliasInitWorklist.pop_back(); |
| 442 | } |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 447 | bool BitcodeReader::ParseConstants(BitstreamReader &Stream) { |
| 448 | if (Stream.EnterSubBlock()) |
| 449 | return Error("Malformed block record"); |
| 450 | |
| 451 | SmallVector<uint64_t, 64> Record; |
| 452 | |
| 453 | // Read all the records for this value table. |
| 454 | const Type *CurTy = Type::Int32Ty; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 455 | unsigned NextCstNo = ValueList.size(); |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 456 | while (1) { |
| 457 | unsigned Code = Stream.ReadCode(); |
| 458 | if (Code == bitc::END_BLOCK) { |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 459 | if (NextCstNo != ValueList.size()) |
| 460 | return Error("Invalid constant reference!"); |
| 461 | |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 462 | if (Stream.ReadBlockEnd()) |
| 463 | return Error("Error at end of constants block"); |
| 464 | return false; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 468 | // No known subblocks, always skip them. |
| 469 | Stream.ReadSubBlockID(); |
| 470 | if (Stream.SkipBlock()) |
| 471 | return Error("Malformed block record"); |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | if (Code == bitc::DEFINE_ABBREV) { |
| 476 | Stream.ReadAbbrevRecord(); |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | // Read a record. |
| 481 | Record.clear(); |
| 482 | Value *V = 0; |
| 483 | switch (Stream.ReadRecord(Code, Record)) { |
| 484 | default: // Default behavior: unknown constant |
| 485 | case bitc::CST_CODE_UNDEF: // UNDEF |
| 486 | V = UndefValue::get(CurTy); |
| 487 | break; |
| 488 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
| 489 | if (Record.empty()) |
| 490 | return Error("Malformed CST_SETTYPE record"); |
| 491 | if (Record[0] >= TypeList.size()) |
| 492 | return Error("Invalid Type ID in CST_SETTYPE record"); |
| 493 | CurTy = TypeList[Record[0]]; |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 494 | continue; // Skip the ValueList manipulation. |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 495 | case bitc::CST_CODE_NULL: // NULL |
| 496 | V = Constant::getNullValue(CurTy); |
| 497 | break; |
| 498 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 499 | if (!isa<IntegerType>(CurTy) || Record.empty()) |
| 500 | return Error("Invalid CST_INTEGER record"); |
| 501 | V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0])); |
| 502 | break; |
| 503 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n, n x intval] |
| 504 | if (!isa<IntegerType>(CurTy) || Record.empty() || |
| 505 | Record.size() < Record[0]+1) |
| 506 | return Error("Invalid WIDE_INTEGER record"); |
| 507 | |
| 508 | unsigned NumWords = Record[0]; |
Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 509 | SmallVector<uint64_t, 8> Words; |
| 510 | Words.resize(NumWords); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 511 | for (unsigned i = 0; i != NumWords; ++i) |
Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 512 | Words[i] = DecodeSignRotatedValue(Record[i+1]); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 513 | V = ConstantInt::get(APInt(cast<IntegerType>(CurTy)->getBitWidth(), |
Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 514 | NumWords, &Words[0])); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 515 | break; |
| 516 | } |
| 517 | case bitc::CST_CODE_FLOAT: // FLOAT: [fpval] |
| 518 | if (Record.empty()) |
| 519 | return Error("Invalid FLOAT record"); |
| 520 | if (CurTy == Type::FloatTy) |
| 521 | V = ConstantFP::get(CurTy, BitsToFloat(Record[0])); |
| 522 | else if (CurTy == Type::DoubleTy) |
| 523 | V = ConstantFP::get(CurTy, BitsToDouble(Record[0])); |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 524 | else |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 525 | V = UndefValue::get(CurTy); |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 526 | break; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 527 | |
| 528 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n, n x value number] |
| 529 | if (Record.empty() || Record.size() < Record[0]+1) |
| 530 | return Error("Invalid CST_AGGREGATE record"); |
| 531 | |
| 532 | unsigned Size = Record[0]; |
| 533 | std::vector<Constant*> Elts; |
| 534 | |
| 535 | if (const StructType *STy = dyn_cast<StructType>(CurTy)) { |
| 536 | for (unsigned i = 0; i != Size; ++i) |
| 537 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], |
| 538 | STy->getElementType(i))); |
| 539 | V = ConstantStruct::get(STy, Elts); |
| 540 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { |
| 541 | const Type *EltTy = ATy->getElementType(); |
| 542 | for (unsigned i = 0; i != Size; ++i) |
| 543 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], EltTy)); |
| 544 | V = ConstantArray::get(ATy, Elts); |
| 545 | } else if (const VectorType *VTy = dyn_cast<VectorType>(CurTy)) { |
| 546 | const Type *EltTy = VTy->getElementType(); |
| 547 | for (unsigned i = 0; i != Size; ++i) |
| 548 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], EltTy)); |
| 549 | V = ConstantVector::get(Elts); |
| 550 | } else { |
| 551 | V = UndefValue::get(CurTy); |
| 552 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 553 | break; |
| 554 | } |
| 555 | |
| 556 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
| 557 | if (Record.size() < 3) return Error("Invalid CE_BINOP record"); |
| 558 | int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 559 | if (Opc < 0) { |
| 560 | V = UndefValue::get(CurTy); // Unknown binop. |
| 561 | } else { |
| 562 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); |
| 563 | Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); |
| 564 | V = ConstantExpr::get(Opc, LHS, RHS); |
| 565 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
| 569 | if (Record.size() < 3) return Error("Invalid CE_CAST record"); |
| 570 | int Opc = GetDecodedCastOpcode(Record[0]); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 571 | if (Opc < 0) { |
| 572 | V = UndefValue::get(CurTy); // Unknown cast. |
| 573 | } else { |
| 574 | const Type *OpTy = getTypeByID(Record[1]); |
| 575 | Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 576 | V = ConstantExpr::getCast(Opc, Op, CurTy); |
| 577 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 578 | break; |
| 579 | } |
| 580 | case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] |
| 581 | if ((Record.size() & 1) == 0) return Error("Invalid CE_GEP record"); |
| 582 | SmallVector<Constant*, 16> Elts; |
| 583 | for (unsigned i = 1, e = Record.size(); i != e; i += 2) { |
| 584 | const Type *ElTy = getTypeByID(Record[i]); |
| 585 | if (!ElTy) return Error("Invalid CE_GEP record"); |
| 586 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); |
| 587 | } |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 588 | V = ConstantExpr::getGetElementPtr(Elts[0], &Elts[1], Elts.size()-1); |
| 589 | break; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 590 | } |
| 591 | case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#] |
| 592 | if (Record.size() < 3) return Error("Invalid CE_SELECT record"); |
| 593 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], |
| 594 | Type::Int1Ty), |
| 595 | ValueList.getConstantFwdRef(Record[1],CurTy), |
| 596 | ValueList.getConstantFwdRef(Record[2],CurTy)); |
| 597 | break; |
| 598 | case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval] |
| 599 | if (Record.size() < 3) return Error("Invalid CE_EXTRACTELT record"); |
| 600 | const VectorType *OpTy = |
| 601 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
| 602 | if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record"); |
| 603 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 604 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], |
| 605 | OpTy->getElementType()); |
| 606 | V = ConstantExpr::getExtractElement(Op0, Op1); |
| 607 | break; |
| 608 | } |
| 609 | case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval] |
| 610 | const VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
| 611 | if (Record.size() < 3 || OpTy == 0) |
| 612 | return Error("Invalid CE_INSERTELT record"); |
| 613 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 614 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], |
| 615 | OpTy->getElementType()); |
| 616 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::Int32Ty); |
| 617 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 618 | break; |
| 619 | } |
| 620 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
| 621 | const VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
| 622 | if (Record.size() < 3 || OpTy == 0) |
| 623 | return Error("Invalid CE_INSERTELT record"); |
| 624 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 625 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 626 | const Type *ShufTy=VectorType::get(Type::Int32Ty, OpTy->getNumElements()); |
| 627 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); |
| 628 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
| 629 | break; |
| 630 | } |
| 631 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
| 632 | if (Record.size() < 4) return Error("Invalid CE_CMP record"); |
| 633 | const Type *OpTy = getTypeByID(Record[0]); |
| 634 | if (OpTy == 0) return Error("Invalid CE_CMP record"); |
| 635 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 636 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 637 | |
| 638 | if (OpTy->isFloatingPoint()) |
| 639 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); |
| 640 | else |
| 641 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); |
| 642 | break; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 643 | } |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 646 | if (NextCstNo == ValueList.size()) |
| 647 | ValueList.push_back(V); |
| 648 | else if (ValueList[NextCstNo] == 0) |
| 649 | ValueList.initVal(NextCstNo, V); |
| 650 | else { |
| 651 | // If there was a forward reference to this constant, |
| 652 | Value *OldV = ValueList[NextCstNo]; |
| 653 | ValueList.setOperand(NextCstNo, V); |
| 654 | OldV->replaceAllUsesWith(V); |
| 655 | delete OldV; |
| 656 | } |
| 657 | |
| 658 | ++NextCstNo; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame^] | 662 | /// ParseFunction - When we see the block for a function body, remember where it |
| 663 | /// is and then skip it. This lets us lazily deserialize the functions. |
| 664 | bool BitcodeReader::ParseFunction(BitstreamReader &Stream) { |
| 665 | // Get the function we are talking about. |
| 666 | if (FunctionsWithBodies.empty()) |
| 667 | return Error("Insufficient function protos"); |
| 668 | |
| 669 | Function *Fn = FunctionsWithBodies.back(); |
| 670 | FunctionsWithBodies.pop_back(); |
| 671 | |
| 672 | // Save the current stream state. |
| 673 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
| 674 | DeferredFunctionInfo[Fn] = std::make_pair(CurBit, Fn->getLinkage()); |
| 675 | |
| 676 | // Set the functions linkage to GhostLinkage so we know it is lazily |
| 677 | // deserialized. |
| 678 | Fn->setLinkage(GlobalValue::GhostLinkage); |
| 679 | |
| 680 | // Skip over the function block for now. |
| 681 | if (Stream.SkipBlock()) |
| 682 | return Error("Malformed block record"); |
| 683 | return false; |
| 684 | } |
| 685 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 686 | bool BitcodeReader::ParseModule(BitstreamReader &Stream, |
| 687 | const std::string &ModuleID) { |
| 688 | // Reject multiple MODULE_BLOCK's in a single bitstream. |
| 689 | if (TheModule) |
| 690 | return Error("Multiple MODULE_BLOCKs in same stream"); |
| 691 | |
| 692 | if (Stream.EnterSubBlock()) |
| 693 | return Error("Malformed block record"); |
| 694 | |
| 695 | // Otherwise, create the module. |
| 696 | TheModule = new Module(ModuleID); |
| 697 | |
| 698 | SmallVector<uint64_t, 64> Record; |
| 699 | std::vector<std::string> SectionTable; |
| 700 | |
| 701 | // Read all the records for this module. |
| 702 | while (!Stream.AtEndOfStream()) { |
| 703 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 704 | if (Code == bitc::END_BLOCK) { |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 705 | ResolveGlobalAndAliasInits(); |
| 706 | if (!GlobalInits.empty() || !AliasInits.empty()) |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 707 | return Error("Malformed global initializer set"); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame^] | 708 | if (!FunctionsWithBodies.empty()) |
| 709 | return Error("Too few function bodies found"); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 710 | if (Stream.ReadBlockEnd()) |
| 711 | return Error("Error at end of module block"); |
| 712 | return false; |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 713 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 714 | |
| 715 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 716 | switch (Stream.ReadSubBlockID()) { |
| 717 | default: // Skip unknown content. |
| 718 | if (Stream.SkipBlock()) |
| 719 | return Error("Malformed block record"); |
| 720 | break; |
| 721 | case bitc::TYPE_BLOCK_ID: |
| 722 | if (ParseTypeTable(Stream)) |
| 723 | return true; |
| 724 | break; |
| 725 | case bitc::TYPE_SYMTAB_BLOCK_ID: |
| 726 | if (ParseTypeSymbolTable(Stream)) |
| 727 | return true; |
| 728 | break; |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 729 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
| 730 | if (ParseValueSymbolTable(Stream)) |
| 731 | return true; |
| 732 | break; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 733 | case bitc::CONSTANTS_BLOCK_ID: |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 734 | if (ParseConstants(Stream) || ResolveGlobalAndAliasInits()) |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 735 | return true; |
| 736 | break; |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame^] | 737 | case bitc::FUNCTION_BLOCK_ID: |
| 738 | // If this is the first function body we've seen, reverse the |
| 739 | // FunctionsWithBodies list. |
| 740 | if (!HasReversedFunctionsWithBodies) { |
| 741 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); |
| 742 | HasReversedFunctionsWithBodies = true; |
| 743 | } |
| 744 | |
| 745 | if (ParseFunction(Stream)) |
| 746 | return true; |
| 747 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 748 | } |
| 749 | continue; |
| 750 | } |
| 751 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 752 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 753 | Stream.ReadAbbrevRecord(); |
| 754 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // Read a record. |
| 758 | switch (Stream.ReadRecord(Code, Record)) { |
| 759 | default: break; // Default behavior, ignore unknown content. |
| 760 | case bitc::MODULE_CODE_VERSION: // VERSION: [version#] |
| 761 | if (Record.size() < 1) |
| 762 | return Error("Malformed MODULE_CODE_VERSION"); |
| 763 | // Only version #0 is supported so far. |
| 764 | if (Record[0] != 0) |
| 765 | return Error("Unknown bitstream version!"); |
| 766 | break; |
| 767 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strlen, strchr x N] |
| 768 | std::string S; |
| 769 | if (ConvertToString(Record, 0, S)) |
| 770 | return Error("Invalid MODULE_CODE_TRIPLE record"); |
| 771 | TheModule->setTargetTriple(S); |
| 772 | break; |
| 773 | } |
| 774 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strlen, strchr x N] |
| 775 | std::string S; |
| 776 | if (ConvertToString(Record, 0, S)) |
| 777 | return Error("Invalid MODULE_CODE_DATALAYOUT record"); |
| 778 | TheModule->setDataLayout(S); |
| 779 | break; |
| 780 | } |
| 781 | case bitc::MODULE_CODE_ASM: { // ASM: [strlen, strchr x N] |
| 782 | std::string S; |
| 783 | if (ConvertToString(Record, 0, S)) |
| 784 | return Error("Invalid MODULE_CODE_ASM record"); |
| 785 | TheModule->setModuleInlineAsm(S); |
| 786 | break; |
| 787 | } |
| 788 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strlen, strchr x N] |
| 789 | std::string S; |
| 790 | if (ConvertToString(Record, 0, S)) |
| 791 | return Error("Invalid MODULE_CODE_DEPLIB record"); |
| 792 | TheModule->addLibrary(S); |
| 793 | break; |
| 794 | } |
| 795 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strlen, strchr x N] |
| 796 | std::string S; |
| 797 | if (ConvertToString(Record, 0, S)) |
| 798 | return Error("Invalid MODULE_CODE_SECTIONNAME record"); |
| 799 | SectionTable.push_back(S); |
| 800 | break; |
| 801 | } |
| 802 | // GLOBALVAR: [type, isconst, initid, |
| 803 | // linkage, alignment, section, visibility, threadlocal] |
| 804 | case bitc::MODULE_CODE_GLOBALVAR: { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 805 | if (Record.size() < 6) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 806 | return Error("Invalid MODULE_CODE_GLOBALVAR record"); |
| 807 | const Type *Ty = getTypeByID(Record[0]); |
| 808 | if (!isa<PointerType>(Ty)) |
| 809 | return Error("Global not a pointer type!"); |
| 810 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 811 | |
| 812 | bool isConstant = Record[1]; |
| 813 | GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]); |
| 814 | unsigned Alignment = (1 << Record[4]) >> 1; |
| 815 | std::string Section; |
| 816 | if (Record[5]) { |
| 817 | if (Record[5]-1 >= SectionTable.size()) |
| 818 | return Error("Invalid section ID"); |
| 819 | Section = SectionTable[Record[5]-1]; |
| 820 | } |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 821 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
| 822 | if (Record.size() >= 6) Visibility = GetDecodedVisibility(Record[6]); |
| 823 | bool isThreadLocal = false; |
| 824 | if (Record.size() >= 7) isThreadLocal = Record[7]; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 825 | |
| 826 | GlobalVariable *NewGV = |
| 827 | new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule); |
| 828 | NewGV->setAlignment(Alignment); |
| 829 | if (!Section.empty()) |
| 830 | NewGV->setSection(Section); |
| 831 | NewGV->setVisibility(Visibility); |
| 832 | NewGV->setThreadLocal(isThreadLocal); |
| 833 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 834 | ValueList.push_back(NewGV); |
| 835 | |
Chris Lattner | 6dbfd7b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 836 | // Remember which value to use for the global initializer. |
| 837 | if (unsigned InitID = Record[2]) |
| 838 | GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 839 | break; |
| 840 | } |
| 841 | // FUNCTION: [type, callingconv, isproto, linkage, alignment, section, |
| 842 | // visibility] |
| 843 | case bitc::MODULE_CODE_FUNCTION: { |
| 844 | if (Record.size() < 7) |
| 845 | return Error("Invalid MODULE_CODE_FUNCTION record"); |
| 846 | const Type *Ty = getTypeByID(Record[0]); |
| 847 | if (!isa<PointerType>(Ty)) |
| 848 | return Error("Function not a pointer type!"); |
| 849 | const FunctionType *FTy = |
| 850 | dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); |
| 851 | if (!FTy) |
| 852 | return Error("Function not a pointer to function type!"); |
| 853 | |
| 854 | Function *Func = new Function(FTy, GlobalValue::ExternalLinkage, |
| 855 | "", TheModule); |
| 856 | |
| 857 | Func->setCallingConv(Record[1]); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame^] | 858 | bool isProto = Record[2]; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 859 | Func->setLinkage(GetDecodedLinkage(Record[3])); |
| 860 | Func->setAlignment((1 << Record[4]) >> 1); |
| 861 | if (Record[5]) { |
| 862 | if (Record[5]-1 >= SectionTable.size()) |
| 863 | return Error("Invalid section ID"); |
| 864 | Func->setSection(SectionTable[Record[5]-1]); |
| 865 | } |
| 866 | Func->setVisibility(GetDecodedVisibility(Record[6])); |
| 867 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 868 | ValueList.push_back(Func); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame^] | 869 | |
| 870 | // If this is a function with a body, remember the prototype we are |
| 871 | // creating now, so that we can match up the body with them later. |
| 872 | if (!isProto) |
| 873 | FunctionsWithBodies.push_back(Func); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 874 | break; |
| 875 | } |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 876 | // ALIAS: [alias type, aliasee val#, linkage] |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 877 | case bitc::MODULE_CODE_ALIAS: { |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 878 | if (Record.size() < 3) |
| 879 | return Error("Invalid MODULE_ALIAS record"); |
| 880 | const Type *Ty = getTypeByID(Record[0]); |
| 881 | if (!isa<PointerType>(Ty)) |
| 882 | return Error("Function not a pointer type!"); |
| 883 | |
| 884 | GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]), |
| 885 | "", 0, TheModule); |
| 886 | ValueList.push_back(NewGA); |
| 887 | AliasInits.push_back(std::make_pair(NewGA, Record[1])); |
| 888 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 889 | } |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 890 | /// MODULE_CODE_PURGEVALS: [numvals] |
| 891 | case bitc::MODULE_CODE_PURGEVALS: |
| 892 | // Trim down the value list to the specified size. |
| 893 | if (Record.size() < 1 || Record[0] > ValueList.size()) |
| 894 | return Error("Invalid MODULE_PURGEVALS record"); |
| 895 | ValueList.shrinkTo(Record[0]); |
| 896 | break; |
| 897 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 898 | Record.clear(); |
| 899 | } |
| 900 | |
| 901 | return Error("Premature end of bitstream"); |
| 902 | } |
| 903 | |
| 904 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 905 | bool BitcodeReader::ParseBitcode() { |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 906 | TheModule = 0; |
| 907 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 908 | if (Buffer->getBufferSize() & 3) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 909 | return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
| 910 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 911 | unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame^] | 912 | Stream.init(BufPtr, BufPtr+Buffer->getBufferSize()); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 913 | |
| 914 | // Sniff for the signature. |
| 915 | if (Stream.Read(8) != 'B' || |
| 916 | Stream.Read(8) != 'C' || |
| 917 | Stream.Read(4) != 0x0 || |
| 918 | Stream.Read(4) != 0xC || |
| 919 | Stream.Read(4) != 0xE || |
| 920 | Stream.Read(4) != 0xD) |
| 921 | return Error("Invalid bitcode signature"); |
| 922 | |
| 923 | // We expect a number of well-defined blocks, though we don't necessarily |
| 924 | // need to understand them all. |
| 925 | while (!Stream.AtEndOfStream()) { |
| 926 | unsigned Code = Stream.ReadCode(); |
| 927 | |
| 928 | if (Code != bitc::ENTER_SUBBLOCK) |
| 929 | return Error("Invalid record at top-level"); |
| 930 | |
| 931 | unsigned BlockID = Stream.ReadSubBlockID(); |
| 932 | |
| 933 | // We only know the MODULE subblock ID. |
| 934 | if (BlockID == bitc::MODULE_BLOCK_ID) { |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 935 | if (ParseModule(Stream, Buffer->getBufferIdentifier())) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 936 | return true; |
| 937 | } else if (Stream.SkipBlock()) { |
| 938 | return Error("Malformed block record"); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | return false; |
| 943 | } |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 944 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame^] | 945 | |
| 946 | bool BitcodeReader::materializeFunction(Function *F, std::string *ErrInfo) { |
| 947 | // If it already is material, ignore the request. |
| 948 | if (!F->hasNotBeenReadFromBytecode()) return false; |
| 949 | |
| 950 | DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator DFII = |
| 951 | DeferredFunctionInfo.find(F); |
| 952 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); |
| 953 | |
| 954 | // Move the bit stream to the saved position of the deferred function body and |
| 955 | // restore the real linkage type for the function. |
| 956 | Stream.JumpToBit(DFII->second.first); |
| 957 | F->setLinkage((GlobalValue::LinkageTypes)DFII->second.second); |
| 958 | DeferredFunctionInfo.erase(DFII); |
| 959 | |
| 960 | return false; |
| 961 | } |
| 962 | |
| 963 | |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 964 | //===----------------------------------------------------------------------===// |
| 965 | // External interface |
| 966 | //===----------------------------------------------------------------------===// |
| 967 | |
| 968 | /// getBitcodeModuleProvider - lazy function-at-a-time loading from a file. |
| 969 | /// |
| 970 | ModuleProvider *llvm::getBitcodeModuleProvider(MemoryBuffer *Buffer, |
| 971 | std::string *ErrMsg) { |
| 972 | BitcodeReader *R = new BitcodeReader(Buffer); |
| 973 | if (R->ParseBitcode()) { |
| 974 | if (ErrMsg) |
| 975 | *ErrMsg = R->getErrorString(); |
| 976 | |
| 977 | // Don't let the BitcodeReader dtor delete 'Buffer'. |
| 978 | R->releaseMemoryBuffer(); |
| 979 | delete R; |
| 980 | return 0; |
| 981 | } |
| 982 | return R; |
| 983 | } |
| 984 | |
| 985 | /// ParseBitcodeFile - Read the specified bitcode file, returning the module. |
| 986 | /// If an error occurs, return null and fill in *ErrMsg if non-null. |
| 987 | Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg){ |
| 988 | BitcodeReader *R; |
| 989 | R = static_cast<BitcodeReader*>(getBitcodeModuleProvider(Buffer, ErrMsg)); |
| 990 | if (!R) return 0; |
| 991 | |
| 992 | // Read the whole module, get a pointer to it, tell ModuleProvider not to |
| 993 | // delete it when its dtor is run. |
| 994 | Module *M = R->releaseModule(ErrMsg); |
| 995 | |
| 996 | // Don't let the BitcodeReader dtor delete 'Buffer'. |
| 997 | R->releaseMemoryBuffer(); |
| 998 | delete R; |
| 999 | return M; |
| 1000 | } |