diff options
Diffstat (limited to 'lib/CodeGen/AsmPrinter/DwarfUnit.cpp')
-rw-r--r-- | lib/CodeGen/AsmPrinter/DwarfUnit.cpp | 197 |
1 files changed, 119 insertions, 78 deletions
diff --git a/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/lib/CodeGen/AsmPrinter/DwarfUnit.cpp index 80b365f1aa43..991ab94b50ab 100644 --- a/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ b/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -1,9 +1,8 @@ //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -47,21 +46,30 @@ using namespace llvm; #define DEBUG_TYPE "dwarfdebug" -DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, +DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP, + DwarfCompileUnit &CU, DIELoc &DIE) - : DwarfExpression(AP.getDwarfVersion()), AP(AP), DU(DU), + : DwarfExpression(AP.getDwarfVersion(), CU), AP(AP), DIE(DIE) {} void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) { - DU.addUInt(DIE, dwarf::DW_FORM_data1, Op); + CU.addUInt(DIE, dwarf::DW_FORM_data1, Op); } void DIEDwarfExpression::emitSigned(int64_t Value) { - DU.addSInt(DIE, dwarf::DW_FORM_sdata, Value); + CU.addSInt(DIE, dwarf::DW_FORM_sdata, Value); } void DIEDwarfExpression::emitUnsigned(uint64_t Value) { - DU.addUInt(DIE, dwarf::DW_FORM_udata, Value); + CU.addUInt(DIE, dwarf::DW_FORM_udata, Value); +} + +void DIEDwarfExpression::emitData1(uint8_t Value) { + CU.addUInt(DIE, dwarf::DW_FORM_data1, Value); +} + +void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx) { + CU.addBaseTypeRef(DIE, Idx); } bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI, @@ -285,21 +293,21 @@ void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute, addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer); } -MD5::MD5Result *DwarfUnit::getMD5AsBytes(const DIFile *File) const { +Optional<MD5::MD5Result> DwarfUnit::getMD5AsBytes(const DIFile *File) const { assert(File); if (DD->getDwarfVersion() < 5) - return nullptr; + return None; Optional<DIFile::ChecksumInfo<StringRef>> Checksum = File->getChecksum(); if (!Checksum || Checksum->Kind != DIFile::CSK_MD5) - return nullptr; + return None; // Convert the string checksum to an MD5Result for the streamer. // The verifier validates the checksum so we assume it's okay. // An MD5 checksum is 16 bytes. std::string ChecksumString = fromHex(Checksum->Value); - void *CKMem = Asm->OutStreamer->getContext().allocate(16, 1); - memcpy(CKMem, ChecksumString.data(), 16); - return reinterpret_cast<MD5::MD5Result *>(CKMem); + MD5::MD5Result CKMem; + std::copy(ChecksumString.begin(), ChecksumString.end(), CKMem.Bytes.data()); + return CKMem; } unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) { @@ -311,7 +319,9 @@ unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) { addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0); } return SplitLineTable->getFile(File->getDirectory(), File->getFilename(), - getMD5AsBytes(File), File->getSource()); + getMD5AsBytes(File), + Asm->OutContext.getDwarfVersion(), + File->getSource()); } void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) { @@ -393,7 +403,6 @@ void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, const DIFile *File) { return; unsigned FileID = getOrCreateSourceID(File); - assert(FileID && "Invalid file id"); addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); addUInt(Die, dwarf::DW_AT_decl_line, None, Line); } @@ -462,9 +471,8 @@ static bool isUnsignedDIType(DwarfDebug *DD, const DIType *Ty) { assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type || T == dwarf::DW_TAG_volatile_type || T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type); - DITypeRef Deriv = DTy->getBaseType(); - assert(Deriv && "Expected valid base type"); - return isUnsignedDIType(DD, DD->resolve(Deriv)); + assert(DTy->getBaseType() && "Expected valid base type"); + return isUnsignedDIType(DD, DTy->getBaseType()); } auto *BTy = cast<DIBasicType>(Ty); @@ -523,6 +531,10 @@ void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO, addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm()); } +void DwarfUnit::addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty) { + addConstantValue(Die, isUnsignedDIType(DD, Ty), Val); +} + void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) { // FIXME: This is a bit conservative/simple - it emits negative values always // sign extended to 64 bits rather than minimizing the number of bytes. @@ -603,8 +615,8 @@ DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) { return getDIE(Context); } -DIE *DwarfTypeUnit::createTypeDIE(const DICompositeType *Ty) { - auto *Context = resolve(Ty->getScope()); +DIE *DwarfUnit::createTypeDIE(const DICompositeType *Ty) { + auto *Context = Ty->getScope(); DIE *ContextDIE = getOrCreateContextDIE(Context); if (DIE *TyDIE = getDIE(Ty)) @@ -619,6 +631,37 @@ DIE *DwarfTypeUnit::createTypeDIE(const DICompositeType *Ty) { return &TyDIE; } +DIE *DwarfUnit::createTypeDIE(const DIScope *Context, DIE &ContextDIE, + const DIType *Ty) { + // Create new type. + DIE &TyDIE = createAndAddDIE(Ty->getTag(), ContextDIE, Ty); + + updateAcceleratorTables(Context, Ty, TyDIE); + + if (auto *BT = dyn_cast<DIBasicType>(Ty)) + constructTypeDIE(TyDIE, BT); + else if (auto *STy = dyn_cast<DISubroutineType>(Ty)) + constructTypeDIE(TyDIE, STy); + else if (auto *CTy = dyn_cast<DICompositeType>(Ty)) { + if (DD->generateTypeUnits() && !Ty->isForwardDecl() && + (Ty->getRawName() || CTy->getRawIdentifier())) { + // Skip updating the accelerator tables since this is not the full type. + if (MDString *TypeId = CTy->getRawIdentifier()) + DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy); + else { + auto X = DD->enterNonTypeUnitContext(); + finishNonUnitTypeDIE(TyDIE, CTy); + } + return &TyDIE; + } + constructTypeDIE(TyDIE, CTy); + } else { + constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty)); + } + + return &TyDIE; +} + DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) { if (!TyNode) return nullptr; @@ -627,43 +670,23 @@ DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) { // DW_TAG_restrict_type is not supported in DWARF2 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2) - return getOrCreateTypeDIE(resolve(cast<DIDerivedType>(Ty)->getBaseType())); + return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType()); // DW_TAG_atomic_type is not supported in DWARF < 5 if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5) - return getOrCreateTypeDIE(resolve(cast<DIDerivedType>(Ty)->getBaseType())); + return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType()); // Construct the context before querying for the existence of the DIE in case // such construction creates the DIE. - auto *Context = resolve(Ty->getScope()); + auto *Context = Ty->getScope(); DIE *ContextDIE = getOrCreateContextDIE(Context); assert(ContextDIE); if (DIE *TyDIE = getDIE(Ty)) return TyDIE; - // Create new type. - DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty); - - updateAcceleratorTables(Context, Ty, TyDIE); - - if (auto *BT = dyn_cast<DIBasicType>(Ty)) - constructTypeDIE(TyDIE, BT); - else if (auto *STy = dyn_cast<DISubroutineType>(Ty)) - constructTypeDIE(TyDIE, STy); - else if (auto *CTy = dyn_cast<DICompositeType>(Ty)) { - if (DD->generateTypeUnits() && !Ty->isForwardDecl()) - if (MDString *TypeId = CTy->getRawIdentifier()) { - DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy); - // Skip updating the accelerator tables since this is not the full type. - return &TyDIE; - } - constructTypeDIE(TyDIE, CTy); - } else { - constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty)); - } - - return &TyDIE; + return static_cast<DwarfUnit *>(ContextDIE->getUnit()) + ->createTypeDIE(Context, *ContextDIE, Ty); } void DwarfUnit::updateAcceleratorTables(const DIScope *Context, @@ -679,7 +702,7 @@ void DwarfUnit::updateAcceleratorTables(const DIScope *Context, DD->addAccelType(*CUNode, Ty->getName(), TyDIE, Flags); if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) || - isa<DINamespace>(Context)) + isa<DINamespace>(Context) || isa<DICommonBlock>(Context)) addGlobalType(Ty, TyDIE, Context); } } @@ -702,8 +725,8 @@ std::string DwarfUnit::getParentContextString(const DIScope *Context) const { SmallVector<const DIScope *, 1> Parents; while (!isa<DICompileUnit>(Context)) { Parents.push_back(Context); - if (Context->getScope()) - Context = resolve(Context->getScope()); + if (const DIScope *S = Context->getScope()) + Context = S; else // Structure, etc types will have a NULL context if they're at the top // level. @@ -754,7 +777,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) { uint16_t Tag = Buffer.getTag(); // Map to main type, void will not have a type. - const DIType *FromTy = resolve(DTy->getBaseType()); + const DIType *FromTy = DTy->getBaseType(); if (FromTy) addType(Buffer, FromTy); @@ -770,24 +793,23 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) { addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); if (Tag == dwarf::DW_TAG_ptr_to_member_type) - addDIEEntry( - Buffer, dwarf::DW_AT_containing_type, - *getOrCreateTypeDIE(resolve(cast<DIDerivedType>(DTy)->getClassType()))); + addDIEEntry(Buffer, dwarf::DW_AT_containing_type, + *getOrCreateTypeDIE(cast<DIDerivedType>(DTy)->getClassType())); // Add source line info if available and TyDesc is not a forward declaration. if (!DTy->isForwardDecl()) addSourceLine(Buffer, DTy); - // If DWARF address space value is other than None, add it for pointer and - // reference types as DW_AT_address_class. - if (DTy->getDWARFAddressSpace() && (Tag == dwarf::DW_TAG_pointer_type || - Tag == dwarf::DW_TAG_reference_type)) + // If DWARF address space value is other than None, add it. The IR + // verifier checks that DWARF address space only exists for pointer + // or reference types. + if (DTy->getDWARFAddressSpace()) addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4, DTy->getDWARFAddressSpace().getValue()); } void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) { for (unsigned i = 1, N = Args.size(); i < N; ++i) { - const DIType *Ty = resolve(Args[i]); + const DIType *Ty = Args[i]; if (!Ty) { assert(i == N-1 && "Unspecified parameter must be the last argument"); createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); @@ -804,7 +826,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) { // Add return type. A void return won't have a type. auto Elements = cast<DISubroutineType>(CTy)->getTypeArray(); if (Elements.size()) - if (auto RTy = resolve(Elements[0])) + if (auto RTy = Elements[0]) addType(Buffer, RTy); bool isPrototyped = true; @@ -875,7 +897,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) { else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) { if (DDTy->getTag() == dwarf::DW_TAG_friend) { DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); - addType(ElemDie, resolve(DDTy->getBaseType()), dwarf::DW_AT_friend); + addType(ElemDie, DDTy->getBaseType(), dwarf::DW_AT_friend); } else if (DDTy->isStaticMember()) { getOrCreateStaticMemberDIE(DDTy); } else if (Tag == dwarf::DW_TAG_variant_part) { @@ -884,7 +906,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) { DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer); if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DDTy->getDiscriminantValue())) { - if (isUnsignedDIType(DD, resolve(Discriminator->getBaseType()))) + if (isUnsignedDIType(DD, Discriminator->getBaseType())) addUInt(Variant, dwarf::DW_AT_discr_value, None, CI->getZExtValue()); else addSInt(Variant, dwarf::DW_AT_discr_value, None, CI->getSExtValue()); @@ -898,7 +920,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) { StringRef PropertyName = Property->getName(); addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); if (Property->getType()) - addType(ElemDie, resolve(Property->getType())); + addType(ElemDie, Property->getType()); addSourceLine(ElemDie, Property); StringRef GetterName = Property->getGetterName(); if (!GetterName.empty()) @@ -924,7 +946,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) { // inside C++ composite types to point to the base class with the vtable. // Rust uses DW_AT_containing_type to link a vtable to the type // for which it was created. - if (auto *ContainingType = resolve(CTy->getVTableHolder())) + if (auto *ContainingType = CTy->getVTableHolder()) addDIEEntry(Buffer, dwarf::DW_AT_containing_type, *getOrCreateTypeDIE(ContainingType)); @@ -994,7 +1016,7 @@ void DwarfUnit::constructTemplateTypeParameterDIE( createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); // Add the type if it exists, it could be void and therefore no type. if (TP->getType()) - addType(ParamDIE, resolve(TP->getType())); + addType(ParamDIE, TP->getType()); if (!TP->getName().empty()) addString(ParamDIE, dwarf::DW_AT_name, TP->getName()); } @@ -1006,12 +1028,12 @@ void DwarfUnit::constructTemplateValueParameterDIE( // Add the type if there is one, template template and template parameter // packs will not have a type. if (VP->getTag() == dwarf::DW_TAG_template_value_parameter) - addType(ParamDIE, resolve(VP->getType())); + addType(ParamDIE, VP->getType()); if (!VP->getName().empty()) addString(ParamDIE, dwarf::DW_AT_name, VP->getName()); if (Metadata *Val = VP->getValue()) { if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val)) - addConstantValue(ParamDIE, CI, resolve(VP->getType())); + addConstantValue(ParamDIE, CI, VP->getType()); else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) { // We cannot describe the location of dllimport'd entities: the // computation of their address requires loads from the IAT. @@ -1085,7 +1107,7 @@ DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) { // such construction creates the DIE (as is the case for member function // declarations). DIE *ContextDIE = - Minimal ? &getUnitDie() : getOrCreateContextDIE(resolve(SP->getScope())); + Minimal ? &getUnitDie() : getOrCreateContextDIE(SP->getScope()); if (DIE *SPDie = getDIE(SP)) return SPDie; @@ -1107,7 +1129,8 @@ DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) { if (SP->isDefinition()) return &SPDie; - applySubprogramAttributes(SP, SPDie); + static_cast<DwarfUnit *>(SPDie.getUnit()) + ->applySubprogramAttributes(SP, SPDie); return &SPDie; } @@ -1197,7 +1220,7 @@ void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie, // Add a return type. If this is a type like a C/C++ void type we don't add a // return type. if (Args.size()) - if (auto Ty = resolve(Args[0])) + if (auto Ty = Args[0]) addType(SPDie, Ty); unsigned VK = SP->getVirtuality(); @@ -1209,8 +1232,7 @@ void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie, addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex()); addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); } - ContainingTypeMap.insert( - std::make_pair(&SPDie, resolve(SP->getContainingType()))); + ContainingTypeMap.insert(std::make_pair(&SPDie, SP->getContainingType())); } if (!SP->isDefinition()) { @@ -1261,6 +1283,12 @@ void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie, if (SP->isMainSubprogram()) addFlag(SPDie, dwarf::DW_AT_main_subprogram); + if (SP->isPure()) + addFlag(SPDie, dwarf::DW_AT_pure); + if (SP->isElemental()) + addFlag(SPDie, dwarf::DW_AT_elemental); + if (SP->isRecursive()) + addFlag(SPDie, dwarf::DW_AT_recursive); } void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, @@ -1310,7 +1338,7 @@ static bool hasVectorBeenPadded(const DICompositeType *CTy) { const uint64_t ActualSize = CTy->getSizeInBits(); // Obtain the size of each element in the vector. - DIType *BaseTy = CTy->getBaseType().resolve(); + DIType *BaseTy = CTy->getBaseType(); assert(BaseTy && "Unknown vector element type."); const uint64_t ElementSize = BaseTy->getSizeInBits(); @@ -1338,7 +1366,7 @@ void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) { } // Emit the element type. - addType(Buffer, resolve(CTy->getBaseType())); + addType(Buffer, CTy->getBaseType()); // Get an anonymous type for index type. // FIXME: This type should be passed down from the front end @@ -1356,7 +1384,7 @@ void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) { } void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) { - const DIType *DTy = resolve(CTy->getBaseType()); + const DIType *DTy = CTy->getBaseType(); bool IsUnsigned = DTy && isUnsignedDIType(DD, DTy); if (DTy) { if (DD->getDwarfVersion() >= 3) @@ -1365,6 +1393,9 @@ void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) { addFlag(Buffer, dwarf::DW_AT_enum_class); } + auto *Context = CTy->getScope(); + bool IndexEnumerators = !Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) || + isa<DINamespace>(Context) || isa<DICommonBlock>(Context); DINodeArray Elements = CTy->getElements(); // Add enumerators to enumeration type. @@ -1376,6 +1407,8 @@ void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) { addString(Enumerator, dwarf::DW_AT_name, Name); auto Value = static_cast<uint64_t>(Enum->getValue()); addConstantValue(Enumerator, IsUnsigned, Value); + if (IndexEnumerators) + addGlobalName(Name, Enumerator, Context); } } } @@ -1400,7 +1433,7 @@ DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) { if (!Name.empty()) addString(MemberDie, dwarf::DW_AT_name, Name); - if (DIType *Resolved = resolve(DT->getBaseType())) + if (DIType *Resolved = DT->getBaseType()) addType(MemberDie, Resolved); addSourceLine(MemberDie, DT); @@ -1509,7 +1542,7 @@ DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) { // Construct the context before querying for the existence of the DIE in case // such construction creates the DIE. - DIE *ContextDIE = getOrCreateContextDIE(resolve(DT->getScope())); + DIE *ContextDIE = getOrCreateContextDIE(DT->getScope()); assert(dwarf::isType(ContextDIE->getTag()) && "Static member should belong to a type."); @@ -1518,7 +1551,7 @@ DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) { DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT); - const DIType *Ty = resolve(DT->getBaseType()); + const DIType *Ty = DT->getBaseType(); addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName()); addType(StaticMemberDIE, Ty); @@ -1671,3 +1704,11 @@ void DwarfUnit::addLoclistsBase() { DU->getLoclistsTableBaseSym(), TLOF.getDwarfLoclistsSection()->getBeginSymbol()); } + +void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) { + addFlag(D, dwarf::DW_AT_declaration); + StringRef Name = CTy->getName(); + if (!Name.empty()) + addString(D, dwarf::DW_AT_name, Name); + getCU().createTypeDIE(CTy); +} |