LLVM API Documentation
00001 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This header defines the BitcodeReader class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Bitcode/ReaderWriter.h" 00015 #include "BitcodeReader.h" 00016 #include "llvm/Constants.h" 00017 #include "llvm/DerivedTypes.h" 00018 #include "llvm/InlineAsm.h" 00019 #include "llvm/Instructions.h" 00020 #include "llvm/Module.h" 00021 #include "llvm/AutoUpgrade.h" 00022 #include "llvm/ADT/SmallString.h" 00023 #include "llvm/ADT/SmallVector.h" 00024 #include "llvm/Support/MathExtras.h" 00025 #include "llvm/Support/MemoryBuffer.h" 00026 #include "llvm/OperandTraits.h" 00027 using namespace llvm; 00028 00029 void BitcodeReader::FreeState() { 00030 delete Buffer; 00031 Buffer = 0; 00032 std::vector<PATypeHolder>().swap(TypeList); 00033 ValueList.clear(); 00034 00035 std::vector<AttrListPtr>().swap(MAttributes); 00036 std::vector<BasicBlock*>().swap(FunctionBBs); 00037 std::vector<Function*>().swap(FunctionsWithBodies); 00038 DeferredFunctionInfo.clear(); 00039 } 00040 00041 //===----------------------------------------------------------------------===// 00042 // Helper functions to implement forward reference resolution, etc. 00043 //===----------------------------------------------------------------------===// 00044 00045 /// ConvertToString - Convert a string from a record into an std::string, return 00046 /// true on failure. 00047 template<typename StrTy> 00048 static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx, 00049 StrTy &Result) { 00050 if (Idx > Record.size()) 00051 return true; 00052 00053 for (unsigned i = Idx, e = Record.size(); i != e; ++i) 00054 Result += (char)Record[i]; 00055 return false; 00056 } 00057 00058 static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { 00059 switch (Val) { 00060 default: // Map unknown/new linkages to external 00061 case 0: return GlobalValue::ExternalLinkage; 00062 case 1: return GlobalValue::WeakLinkage; 00063 case 2: return GlobalValue::AppendingLinkage; 00064 case 3: return GlobalValue::InternalLinkage; 00065 case 4: return GlobalValue::LinkOnceLinkage; 00066 case 5: return GlobalValue::DLLImportLinkage; 00067 case 6: return GlobalValue::DLLExportLinkage; 00068 case 7: return GlobalValue::ExternalWeakLinkage; 00069 case 8: return GlobalValue::CommonLinkage; 00070 } 00071 } 00072 00073 static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { 00074 switch (Val) { 00075 default: // Map unknown visibilities to default. 00076 case 0: return GlobalValue::DefaultVisibility; 00077 case 1: return GlobalValue::HiddenVisibility; 00078 case 2: return GlobalValue::ProtectedVisibility; 00079 } 00080 } 00081 00082 static int GetDecodedCastOpcode(unsigned Val) { 00083 switch (Val) { 00084 default: return -1; 00085 case bitc::CAST_TRUNC : return Instruction::Trunc; 00086 case bitc::CAST_ZEXT : return Instruction::ZExt; 00087 case bitc::CAST_SEXT : return Instruction::SExt; 00088 case bitc::CAST_FPTOUI : return Instruction::FPToUI; 00089 case bitc::CAST_FPTOSI : return Instruction::FPToSI; 00090 case bitc::CAST_UITOFP : return Instruction::UIToFP; 00091 case bitc::CAST_SITOFP : return Instruction::SIToFP; 00092 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; 00093 case bitc::CAST_FPEXT : return Instruction::FPExt; 00094 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; 00095 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; 00096 case bitc::CAST_BITCAST : return Instruction::BitCast; 00097 } 00098 } 00099 static int GetDecodedBinaryOpcode(unsigned Val, const Type *Ty) { 00100 switch (Val) { 00101 default: return -1; 00102 case bitc::BINOP_ADD: return Instruction::Add; 00103 case bitc::BINOP_SUB: return Instruction::Sub; 00104 case bitc::BINOP_MUL: return Instruction::Mul; 00105 case bitc::BINOP_UDIV: return Instruction::UDiv; 00106 case bitc::BINOP_SDIV: 00107 return Ty->isFPOrFPVector() ? Instruction::FDiv : Instruction::SDiv; 00108 case bitc::BINOP_UREM: return Instruction::URem; 00109 case bitc::BINOP_SREM: 00110 return Ty->isFPOrFPVector() ? Instruction::FRem : Instruction::SRem; 00111 case bitc::BINOP_SHL: return Instruction::Shl; 00112 case bitc::BINOP_LSHR: return Instruction::LShr; 00113 case bitc::BINOP_ASHR: return Instruction::AShr; 00114 case bitc::BINOP_AND: return Instruction::And; 00115 case bitc::BINOP_OR: return Instruction::Or; 00116 case bitc::BINOP_XOR: return Instruction::Xor; 00117 } 00118 } 00119 00120 namespace llvm { 00121 namespace { 00122 /// @brief A class for maintaining the slot number definition 00123 /// as a placeholder for the actual definition for forward constants defs. 00124 class ConstantPlaceHolder : public ConstantExpr { 00125 ConstantPlaceHolder(); // DO NOT IMPLEMENT 00126 void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT 00127 public: 00128 // allocate space for exactly one operand 00129 void *operator new(size_t s) { 00130 return User::operator new(s, 1); 00131 } 00132 explicit ConstantPlaceHolder(const Type *Ty) 00133 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) { 00134 Op<0>() = UndefValue::get(Type::Int32Ty); 00135 } 00136 00137 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast. 00138 static inline bool classof(const ConstantPlaceHolder *) { return true; } 00139 static bool classof(const Value *V) { 00140 return isa<ConstantExpr>(V) && 00141 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1; 00142 } 00143 00144 00145 /// Provide fast operand accessors 00146 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00147 }; 00148 } 00149 00150 00151 // FIXME: can we inherit this from ConstantExpr? 00152 template <> 00153 struct OperandTraits<ConstantPlaceHolder> : FixedNumOperandTraits<1> { 00154 }; 00155 00156 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value) 00157 } 00158 00159 void BitcodeReaderValueList::resize(unsigned Desired) { 00160 if (Desired > Capacity) { 00161 // Since we expect many values to come from the bitcode file we better 00162 // allocate the double amount, so that the array size grows exponentially 00163 // at each reallocation. Also, add a small amount of 100 extra elements 00164 // each time, to reallocate less frequently when the array is still small. 00165 // 00166 Capacity = Desired * 2 + 100; 00167 Use *New = allocHungoffUses(Capacity); 00168 Use *Old = OperandList; 00169 unsigned Ops = getNumOperands(); 00170 for (int i(Ops - 1); i >= 0; --i) 00171 New[i] = Old[i].get(); 00172 OperandList = New; 00173 if (Old) Use::zap(Old, Old + Ops, true); 00174 } 00175 } 00176 00177 Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, 00178 const Type *Ty) { 00179 if (Idx >= size()) { 00180 // Insert a bunch of null values. 00181 resize(Idx + 1); 00182 NumOperands = Idx+1; 00183 } 00184 00185 if (Value *V = OperandList[Idx]) { 00186 assert(Ty == V->getType() && "Type mismatch in constant table!"); 00187 return cast<Constant>(V); 00188 } 00189 00190 // Create and return a placeholder, which will later be RAUW'd. 00191 Constant *C = new ConstantPlaceHolder(Ty); 00192 OperandList[Idx] = C; 00193 return C; 00194 } 00195 00196 Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) { 00197 if (Idx >= size()) { 00198 // Insert a bunch of null values. 00199 resize(Idx + 1); 00200 NumOperands = Idx+1; 00201 } 00202 00203 if (Value *V = OperandList[Idx]) { 00204 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!"); 00205 return V; 00206 } 00207 00208 // No type specified, must be invalid reference. 00209 if (Ty == 0) return 0; 00210 00211 // Create and return a placeholder, which will later be RAUW'd. 00212 Value *V = new Argument(Ty); 00213 OperandList[Idx] = V; 00214 return V; 00215 } 00216 00217 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk 00218 /// resolves any forward references. The idea behind this is that we sometimes 00219 /// get constants (such as large arrays) which reference *many* forward ref 00220 /// constants. Replacing each of these causes a lot of thrashing when 00221 /// building/reuniquing the constant. Instead of doing this, we look at all the 00222 /// uses and rewrite all the place holders at once for any constant that uses 00223 /// a placeholder. 00224 void BitcodeReaderValueList::ResolveConstantForwardRefs() { 00225 // Sort the values by-pointer so that they are efficient to look up with a 00226 // binary search. 00227 std::sort(ResolveConstants.begin(), ResolveConstants.end()); 00228 00229 SmallVector<Constant*, 64> NewOps; 00230 00231 while (!ResolveConstants.empty()) { 00232 Value *RealVal = getOperand(ResolveConstants.back().second); 00233 Constant *Placeholder = ResolveConstants.back().first; 00234 ResolveConstants.pop_back(); 00235 00236 // Loop over all users of the placeholder, updating them to reference the 00237 // new value. If they reference more than one placeholder, update them all 00238 // at once. 00239 while (!Placeholder->use_empty()) { 00240 Value::use_iterator UI = Placeholder->use_begin(); 00241 00242 // If the using object isn't uniqued, just update the operands. This 00243 // handles instructions and initializers for global variables. 00244 if (!isa<Constant>(*UI) || isa<GlobalValue>(*UI)) { 00245 UI.getUse().set(RealVal); 00246 continue; 00247 } 00248 00249 // Otherwise, we have a constant that uses the placeholder. Replace that 00250 // constant with a new constant that has *all* placeholder uses updated. 00251 Constant *UserC = cast<Constant>(*UI); 00252 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end(); 00253 I != E; ++I) { 00254 Value *NewOp; 00255 if (!isa<ConstantPlaceHolder>(*I)) { 00256 // Not a placeholder reference. 00257 NewOp = *I; 00258 } else if (*I == Placeholder) { 00259 // Common case is that it just references this one placeholder. 00260 NewOp = RealVal; 00261 } else { 00262 // Otherwise, look up the placeholder in ResolveConstants. 00263 ResolveConstantsTy::iterator It = 00264 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(), 00265 std::pair<Constant*, unsigned>(cast<Constant>(*I), 00266 0)); 00267 assert(It != ResolveConstants.end() && It->first == *I); 00268 NewOp = this->getOperand(It->second); 00269 } 00270 00271 NewOps.push_back(cast<Constant>(NewOp)); 00272 } 00273 00274 // Make the new constant. 00275 Constant *NewC; 00276 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) { 00277 NewC = ConstantArray::get(UserCA->getType(), &NewOps[0], NewOps.size()); 00278 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) { 00279 NewC = ConstantStruct::get(&NewOps[0], NewOps.size(), 00280 UserCS->getType()->isPacked()); 00281 } else if (isa<ConstantVector>(UserC)) { 00282 NewC = ConstantVector::get(&NewOps[0], NewOps.size()); 00283 } else { 00284 // Must be a constant expression. 00285 NewC = cast<ConstantExpr>(UserC)->getWithOperands(&NewOps[0], 00286 NewOps.size()); 00287 } 00288 00289 UserC->replaceAllUsesWith(NewC); 00290 UserC->destroyConstant(); 00291 NewOps.clear(); 00292 } 00293 00294 delete Placeholder; 00295 } 00296 } 00297 00298 00299 const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) { 00300 // If the TypeID is in range, return it. 00301 if (ID < TypeList.size()) 00302 return TypeList[ID].get(); 00303 if (!isTypeTable) return 0; 00304 00305 // The type table allows forward references. Push as many Opaque types as 00306 // needed to get up to ID. 00307 while (TypeList.size() <= ID) 00308 TypeList.push_back(OpaqueType::get()); 00309 return TypeList.back().get(); 00310 } 00311 00312 //===----------------------------------------------------------------------===// 00313 // Functions for parsing blocks from the bitcode file 00314 //===----------------------------------------------------------------------===// 00315 00316 bool BitcodeReader::ParseAttributeBlock() { 00317 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) 00318 return Error("Malformed block record"); 00319 00320 if (!MAttributes.empty()) 00321 return Error("Multiple PARAMATTR blocks found!"); 00322 00323 SmallVector<uint64_t, 64> Record; 00324 00325 SmallVector<AttributeWithIndex, 8> Attrs; 00326 00327 // Read all the records. 00328 while (1) { 00329 unsigned Code = Stream.ReadCode(); 00330 if (Code == bitc::END_BLOCK) { 00331 if (Stream.ReadBlockEnd()) 00332 return Error("Error at end of PARAMATTR block"); 00333 return false; 00334 } 00335 00336 if (Code == bitc::ENTER_SUBBLOCK) { 00337 // No known subblocks, always skip them. 00338 Stream.ReadSubBlockID(); 00339 if (Stream.SkipBlock()) 00340 return Error("Malformed block record"); 00341 continue; 00342 } 00343 00344 if (Code == bitc::DEFINE_ABBREV) { 00345 Stream.ReadAbbrevRecord(); 00346 continue; 00347 } 00348 00349 // Read a record. 00350 Record.clear(); 00351 switch (Stream.ReadRecord(Code, Record)) { 00352 default: // Default behavior: ignore. 00353 break; 00354 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [paramidx0, attr0, ...] 00355 if (Record.size() & 1) 00356 return Error("Invalid ENTRY record"); 00357 00358 // FIXME : Remove this autoupgrade code in LLVM 3.0. 00359 // If Function attributes are using index 0 then transfer them 00360 // to index ~0. Index 0 is used for return value attributes but used to be 00361 // used for function attributes. 00362 Attributes RetAttribute = Attribute::None; 00363 Attributes FnAttribute = Attribute::None; 00364 for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 00365 // FIXME: remove in LLVM 3.0 00366 // The alignment is stored as a 16-bit raw value from bits 31--16. 00367 // We shift the bits above 31 down by 11 bits. 00368 00369 unsigned Alignment = (Record[i+1] & (0xffffull << 16)) >> 16; 00370 if (Alignment && !isPowerOf2_32(Alignment)) 00371 return Error("Alignment is not a power of two."); 00372 00373 Attributes ReconstitutedAttr = Record[i+1] & 0xffff; 00374 if (Alignment) 00375 ReconstitutedAttr |= Attribute::constructAlignmentFromInt(Alignment); 00376 ReconstitutedAttr |= (Record[i+1] & (0xffffull << 32)) >> 11; 00377 Record[i+1] = ReconstitutedAttr; 00378 00379 if (Record[i] == 0) 00380 RetAttribute = Record[i+1]; 00381 else if (Record[i] == ~0U) 00382 FnAttribute = Record[i+1]; 00383 } 00384 00385 unsigned OldRetAttrs = (Attribute::NoUnwind|Attribute::NoReturn| 00386 Attribute::ReadOnly|Attribute::ReadNone); 00387 00388 if (FnAttribute == Attribute::None && RetAttribute != Attribute::None && 00389 (RetAttribute & OldRetAttrs) != 0) { 00390 if (FnAttribute == Attribute::None) { // add a slot so they get added. 00391 Record.push_back(~0U); 00392 Record.push_back(0); 00393 } 00394 00395 FnAttribute |= RetAttribute & OldRetAttrs; 00396 RetAttribute &= ~OldRetAttrs; 00397 } 00398 00399 for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 00400 if (Record[i] == 0) { 00401 if (RetAttribute != Attribute::None) 00402 Attrs.push_back(AttributeWithIndex::get(0, RetAttribute)); 00403 } else if (Record[i] == ~0U) { 00404 if (FnAttribute != Attribute::None) 00405 Attrs.push_back(AttributeWithIndex::get(~0U, FnAttribute)); 00406 } else if (Record[i+1] != Attribute::None) 00407 Attrs.push_back(AttributeWithIndex::get(Record[i], Record[i+1])); 00408 } 00409 00410 MAttributes.push_back(AttrListPtr::get(Attrs.begin(), Attrs.end())); 00411 Attrs.clear(); 00412 break; 00413 } 00414 } 00415 } 00416 } 00417 00418 00419 bool BitcodeReader::ParseTypeTable() { 00420 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID)) 00421 return Error("Malformed block record"); 00422 00423 if (!TypeList.empty()) 00424 return Error("Multiple TYPE_BLOCKs found!"); 00425 00426 SmallVector<uint64_t, 64> Record; 00427 unsigned NumRecords = 0; 00428 00429 // Read all the records for this type table. 00430 while (1) { 00431 unsigned Code = Stream.ReadCode(); 00432 if (Code == bitc::END_BLOCK) { 00433 if (NumRecords != TypeList.size()) 00434 return Error("Invalid type forward reference in TYPE_BLOCK"); 00435 if (Stream.ReadBlockEnd()) 00436 return Error("Error at end of type table block"); 00437 return false; 00438 } 00439 00440 if (Code == bitc::ENTER_SUBBLOCK) { 00441 // No known subblocks, always skip them. 00442 Stream.ReadSubBlockID(); 00443 if (Stream.SkipBlock()) 00444 return Error("Malformed block record"); 00445 continue; 00446 } 00447 00448 if (Code == bitc::DEFINE_ABBREV) { 00449 Stream.ReadAbbrevRecord(); 00450 continue; 00451 } 00452 00453 // Read a record. 00454 Record.clear(); 00455 const Type *ResultTy = 0; 00456 switch (Stream.ReadRecord(Code, Record)) { 00457 default: // Default behavior: unknown type. 00458 ResultTy = 0; 00459 break; 00460 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 00461 // TYPE_CODE_NUMENTRY contains a count of the number of types in the 00462 // type list. This allows us to reserve space. 00463 if (Record.size() < 1) 00464 return Error("Invalid TYPE_CODE_NUMENTRY record"); 00465 TypeList.reserve(Record[0]); 00466 continue; 00467 case bitc::TYPE_CODE_VOID: // VOID 00468 ResultTy = Type::VoidTy; 00469 break; 00470 case bitc::TYPE_CODE_FLOAT: // FLOAT 00471 ResultTy = Type::FloatTy; 00472 break; 00473 case bitc::TYPE_CODE_DOUBLE: // DOUBLE 00474 ResultTy = Type::DoubleTy; 00475 break; 00476 case bitc::TYPE_CODE_X86_FP80: // X86_FP80 00477 ResultTy = Type::X86_FP80Ty; 00478 break; 00479 case bitc::TYPE_CODE_FP128: // FP128 00480 ResultTy = Type::FP128Ty; 00481 break; 00482 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 00483 ResultTy = Type::PPC_FP128Ty; 00484 break; 00485 case bitc::TYPE_CODE_LABEL: // LABEL 00486 ResultTy = Type::LabelTy; 00487 break; 00488 case bitc::TYPE_CODE_OPAQUE: // OPAQUE 00489 ResultTy = 0; 00490 break; 00491 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width] 00492 if (Record.size() < 1) 00493 return Error("Invalid Integer type record"); 00494 00495 ResultTy = IntegerType::get(Record[0]); 00496 break; 00497 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 00498 // [pointee type, address space] 00499 if (Record.size() < 1) 00500 return Error("Invalid POINTER type record"); 00501 unsigned AddressSpace = 0; 00502 if (Record.size() == 2) 00503 AddressSpace = Record[1]; 00504 ResultTy = PointerType::get(getTypeByID(Record[0], true), AddressSpace); 00505 break; 00506 } 00507 case bitc::TYPE_CODE_FUNCTION: { 00508 // FIXME: attrid is dead, remove it in LLVM 3.0 00509 // FUNCTION: [vararg, attrid, retty, paramty x N] 00510 if (Record.size() < 3) 00511 return Error("Invalid FUNCTION type record"); 00512 std::vector<const Type*> ArgTys; 00513 for (unsigned i = 3, e = Record.size(); i != e; ++i) 00514 ArgTys.push_back(getTypeByID(Record[i], true)); 00515 00516 ResultTy = FunctionType::get(getTypeByID(Record[2], true), ArgTys, 00517 Record[0]); 00518 break; 00519 } 00520 case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, eltty x N] 00521 if (Record.size() < 1) 00522 return Error("Invalid STRUCT type record"); 00523 std::vector<const Type*> EltTys; 00524 for (unsigned i = 1, e = Record.size(); i != e; ++i) 00525 EltTys.push_back(getTypeByID(Record[i], true)); 00526 ResultTy = StructType::get(EltTys, Record[0]); 00527 break; 00528 } 00529 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] 00530 if (Record.size() < 2) 00531 return Error("Invalid ARRAY type record"); 00532 ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]); 00533 break; 00534 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] 00535 if (Record.size() < 2) 00536 return Error("Invalid VECTOR type record"); 00537 ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]); 00538 break; 00539 } 00540 00541 if (NumRecords == TypeList.size()) { 00542 // If this is a new type slot, just append it. 00543 TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get()); 00544 ++NumRecords; 00545 } else if (ResultTy == 0) { 00546 // Otherwise, this was forward referenced, so an opaque type was created, 00547 // but the result type is actually just an opaque. Leave the one we 00548 // created previously. 00549 ++NumRecords; 00550 } else { 00551 // Otherwise, this was forward referenced, so an opaque type was created. 00552 // Resolve the opaque type to the real type now. 00553 assert(NumRecords < TypeList.size() && "Typelist imbalance"); 00554 const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get()); 00555 00556 // Don't directly push the new type on the Tab. Instead we want to replace 00557 // the opaque type we previously inserted with the new concrete value. The 00558 // refinement from the abstract (opaque) type to the new type causes all 00559 // uses of the abstract type to use the concrete type (NewTy). This will 00560 // also cause the opaque type to be deleted. 00561 const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy); 00562 00563 // This should have replaced the old opaque type with the new type in the 00564 // value table... or with a preexisting type that was already in the 00565 // system. Let's just make sure it did. 00566 assert(TypeList[NumRecords-1].get() != OldTy && 00567 "refineAbstractType didn't work!"); 00568 } 00569 } 00570 } 00571 00572 00573 bool BitcodeReader::ParseTypeSymbolTable() { 00574 if (Stream.EnterSubBlock(bitc::TYPE_SYMTAB_BLOCK_ID)) 00575 return Error("Malformed block record"); 00576 00577 SmallVector<uint64_t, 64> Record; 00578 00579 // Read all the records for this type table. 00580 std::string TypeName; 00581 while (1) { 00582 unsigned Code = Stream.ReadCode(); 00583 if (Code == bitc::END_BLOCK) { 00584 if (Stream.ReadBlockEnd()) 00585 return Error("Error at end of type symbol table block"); 00586 return false; 00587 } 00588 00589 if (Code == bitc::ENTER_SUBBLOCK) { 00590 // No known subblocks, always skip them. 00591 Stream.ReadSubBlockID(); 00592 if (Stream.SkipBlock()) 00593 return Error("Malformed block record"); 00594 continue; 00595 } 00596 00597 if (Code == bitc::DEFINE_ABBREV) { 00598 Stream.ReadAbbrevRecord(); 00599 continue; 00600 } 00601 00602 // Read a record. 00603 Record.clear(); 00604 switch (Stream.ReadRecord(Code, Record)) { 00605 default: // Default behavior: unknown type. 00606 break; 00607 case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namechar x N] 00608 if (ConvertToString(Record, 1, TypeName)) 00609 return Error("Invalid TST_ENTRY record"); 00610 unsigned TypeID = Record[0]; 00611 if (TypeID >= TypeList.size()) 00612 return Error("Invalid Type ID in TST_ENTRY record"); 00613 00614 TheModule->addTypeName(TypeName, TypeList[TypeID].get()); 00615 TypeName.clear(); 00616 break; 00617 } 00618 } 00619 } 00620 00621 bool BitcodeReader::ParseValueSymbolTable() { 00622 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 00623 return Error("Malformed block record"); 00624 00625 SmallVector<uint64_t, 64> Record; 00626 00627 // Read all the records for this value table. 00628 SmallString<128> ValueName; 00629 while (1) { 00630 unsigned Code = Stream.ReadCode(); 00631 if (Code == bitc::END_BLOCK) { 00632 if (Stream.ReadBlockEnd()) 00633 return Error("Error at end of value symbol table block"); 00634 return false; 00635 } 00636 if (Code == bitc::ENTER_SUBBLOCK) { 00637 // No known subblocks, always skip them. 00638 Stream.ReadSubBlockID(); 00639 if (Stream.SkipBlock()) 00640 return Error("Malformed block record"); 00641 continue; 00642 } 00643 00644 if (Code == bitc::DEFINE_ABBREV) { 00645 Stream.ReadAbbrevRecord(); 00646 continue; 00647 } 00648 00649 // Read a record. 00650 Record.clear(); 00651 switch (Stream.ReadRecord(Code, Record)) { 00652 default: // Default behavior: unknown type. 00653 break; 00654 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N] 00655 if (ConvertToString(Record, 1, ValueName)) 00656 return Error("Invalid TST_ENTRY record"); 00657 unsigned ValueID = Record[0]; 00658 if (ValueID >= ValueList.size()) 00659 return Error("Invalid Value ID in VST_ENTRY record"); 00660 Value *V = ValueList[ValueID]; 00661 00662 V->setName(&ValueName[0], ValueName.size()); 00663 ValueName.clear(); 00664 break; 00665 } 00666 case bitc::VST_CODE_BBENTRY: { 00667 if (ConvertToString(Record, 1, ValueName)) 00668 return Error("Invalid VST_BBENTRY record"); 00669 BasicBlock *BB = getBasicBlock(Record[0]); 00670 if (BB == 0) 00671 return Error("Invalid BB ID in VST_BBENTRY record"); 00672 00673 BB->setName(&ValueName[0], ValueName.size()); 00674 ValueName.clear(); 00675 break; 00676 } 00677 } 00678 } 00679 } 00680 00681 /// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in 00682 /// the LSB for dense VBR encoding. 00683 static uint64_t DecodeSignRotatedValue(uint64_t V) { 00684 if ((V & 1) == 0) 00685 return V >> 1; 00686 if (V != 1) 00687 return -(V >> 1); 00688 // There is no such thing as -0 with integers. "-0" really means MININT. 00689 return 1ULL << 63; 00690 } 00691 00692 /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global 00693 /// values and aliases that we can. 00694 bool BitcodeReader::ResolveGlobalAndAliasInits() { 00695 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; 00696 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; 00697 00698 GlobalInitWorklist.swap(GlobalInits); 00699 AliasInitWorklist.swap(AliasInits); 00700 00701 while (!GlobalInitWorklist.empty()) { 00702 unsigned ValID = GlobalInitWorklist.back().second; 00703 if (ValID >= ValueList.size()) { 00704 // Not ready to resolve this yet, it requires something later in the file. 00705 GlobalInits.push_back(GlobalInitWorklist.back()); 00706 } else { 00707 if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) 00708 GlobalInitWorklist.back().first->setInitializer(C); 00709 else 00710 return Error("Global variable initializer is not a constant!"); 00711 } 00712 GlobalInitWorklist.pop_back(); 00713 } 00714 00715 while (!AliasInitWorklist.empty()) { 00716 unsigned ValID = AliasInitWorklist.back().second; 00717 if (ValID >= ValueList.size()) { 00718 AliasInits.push_back(AliasInitWorklist.back()); 00719 } else { 00720 if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) 00721 AliasInitWorklist.back().first->setAliasee(C); 00722 else 00723 return Error("Alias initializer is not a constant!"); 00724 } 00725 AliasInitWorklist.pop_back(); 00726 } 00727 return false; 00728 } 00729 00730 00731 bool BitcodeReader::ParseConstants() { 00732 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) 00733 return Error("Malformed block record"); 00734 00735 SmallVector<uint64_t, 64> Record; 00736 00737 // Read all the records for this value table. 00738 const Type *CurTy = Type::Int32Ty; 00739 unsigned NextCstNo = ValueList.size(); 00740 while (1) { 00741 unsigned Code = Stream.ReadCode(); 00742 if (Code == bitc::END_BLOCK) 00743 break; 00744 00745 if (Code == bitc::ENTER_SUBBLOCK) { 00746 // No known subblocks, always skip them. 00747 Stream.ReadSubBlockID(); 00748 if (Stream.SkipBlock()) 00749 return Error("Malformed block record"); 00750 continue; 00751 } 00752 00753 if (Code == bitc::DEFINE_ABBREV) { 00754 Stream.ReadAbbrevRecord(); 00755 continue; 00756 } 00757 00758 // Read a record. 00759 Record.clear(); 00760 Value *V = 0; 00761 switch (Stream.ReadRecord(Code, Record)) { 00762 default: // Default behavior: unknown constant 00763 case bitc::CST_CODE_UNDEF: // UNDEF 00764 V = UndefValue::get(CurTy); 00765 break; 00766 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] 00767 if (Record.empty()) 00768 return Error("Malformed CST_SETTYPE record"); 00769 if (Record[0] >= TypeList.size()) 00770 return Error("Invalid Type ID in CST_SETTYPE record"); 00771 CurTy = TypeList[Record[0]]; 00772 continue; // Skip the ValueList manipulation. 00773 case bitc::CST_CODE_NULL: // NULL 00774 V = Constant::getNullValue(CurTy); 00775 break; 00776 case bitc::CST_CODE_INTEGER: // INTEGER: [intval] 00777 if (!isa<IntegerType>(CurTy) || Record.empty()) 00778 return Error("Invalid CST_INTEGER record"); 00779 V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0])); 00780 break; 00781 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] 00782 if (!isa<IntegerType>(CurTy) || Record.empty()) 00783 return Error("Invalid WIDE_INTEGER record"); 00784 00785 unsigned NumWords = Record.size(); 00786 SmallVector<uint64_t, 8> Words; 00787 Words.resize(NumWords); 00788 for (unsigned i = 0; i != NumWords; ++i) 00789 Words[i] = DecodeSignRotatedValue(Record[i]); 00790 V = ConstantInt::get(APInt(cast<IntegerType>(CurTy)->getBitWidth(), 00791 NumWords, &Words[0])); 00792 break; 00793 } 00794 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] 00795 if (Record.empty()) 00796 return Error("Invalid FLOAT record"); 00797 if (CurTy == Type::FloatTy) 00798 V = ConstantFP::get(APFloat(APInt(32, (uint32_t)Record[0]))); 00799 else if (CurTy == Type::DoubleTy) 00800 V = ConstantFP::get(APFloat(APInt(64, Record[0]))); 00801 else if (CurTy == Type::X86_FP80Ty) 00802 V = ConstantFP::get(APFloat(APInt(80, 2, &Record[0]))); 00803 else if (CurTy == Type::FP128Ty) 00804 V = ConstantFP::get(APFloat(APInt(128, 2, &Record[0]), true)); 00805 else if (CurTy == Type::PPC_FP128Ty) 00806 V = ConstantFP::get(APFloat(APInt(128, 2, &Record[0]))); 00807 else 00808 V = UndefValue::get(CurTy); 00809 break; 00810 } 00811 00812 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] 00813 if (Record.empty()) 00814 return Error("Invalid CST_AGGREGATE record"); 00815 00816 unsigned Size = Record.size(); 00817 std::vector<Constant*> Elts; 00818 00819 if (const StructType *STy = dyn_cast<StructType>(CurTy)) { 00820 for (unsigned i = 0; i != Size; ++i) 00821 Elts.push_back(ValueList.getConstantFwdRef(Record[i], 00822 STy->getElementType(i))); 00823 V = ConstantStruct::get(STy, Elts); 00824 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { 00825 const Type *EltTy = ATy->getElementType(); 00826 for (unsigned i = 0; i != Size; ++i) 00827 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 00828 V = ConstantArray::get(ATy, Elts); 00829 } else if (const VectorType *VTy = dyn_cast<VectorType>(CurTy)) { 00830 const Type *EltTy = VTy->getElementType(); 00831 for (unsigned i = 0; i != Size; ++i) 00832 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 00833 V = ConstantVector::get(Elts); 00834 } else { 00835 V = UndefValue::get(CurTy); 00836 } 00837 break; 00838 } 00839 case bitc::CST_CODE_STRING: { // STRING: [values] 00840 if (Record.empty()) 00841 return Error("Invalid CST_AGGREGATE record"); 00842 00843 const ArrayType *ATy = cast<ArrayType>(CurTy); 00844 const Type *EltTy = ATy->getElementType(); 00845 00846 unsigned Size = Record.size(); 00847 std::vector<Constant*> Elts; 00848 for (unsigned i = 0; i != Size; ++i) 00849 Elts.push_back(ConstantInt::get(EltTy, Record[i])); 00850 V = ConstantArray::get(ATy, Elts); 00851 break; 00852 } 00853 case bitc::CST_CODE_CSTRING: { // CSTRING: [values] 00854 if (Record.empty()) 00855 return Error("Invalid CST_AGGREGATE record"); 00856 00857 const ArrayType *ATy = cast<ArrayType>(CurTy); 00858 const Type *EltTy = ATy->getElementType(); 00859 00860 unsigned Size = Record.size(); 00861 std::vector<Constant*> Elts; 00862 for (unsigned i = 0; i != Size; ++i) 00863 Elts.push_back(ConstantInt::get(EltTy, Record[i])); 00864 Elts.push_back(Constant::getNullValue(EltTy)); 00865 V = ConstantArray::get(ATy, Elts); 00866 break; 00867 } 00868 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] 00869 if (Record.size() < 3) return Error("Invalid CE_BINOP record"); 00870 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); 00871 if (Opc < 0) { 00872 V = UndefValue::get(CurTy); // Unknown binop. 00873 } else { 00874 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); 00875 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); 00876 V = ConstantExpr::get(Opc, LHS, RHS); 00877 } 00878 break; 00879 } 00880 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] 00881 if (Record.size() < 3) return Error("Invalid CE_CAST record"); 00882 int Opc = GetDecodedCastOpcode(Record[0]); 00883 if (Opc < 0) { 00884 V = UndefValue::get(CurTy); // Unknown cast. 00885 } else { 00886 const Type *OpTy = getTypeByID(Record[1]); 00887 if (!OpTy) return Error("Invalid CE_CAST record"); 00888 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); 00889 V = ConstantExpr::getCast(Opc, Op, CurTy); 00890 } 00891 break; 00892 } 00893 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] 00894 if (Record.size() & 1) return Error("Invalid CE_GEP record"); 00895 SmallVector<Constant*, 16> Elts; 00896 for<