Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Units ------------===// |
Paul Robinson | 3443575 | 2017-03-01 00:14:42 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Paul Robinson | 3443575 | 2017-03-01 00:14:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains support for constructing a dwarf compile unit. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 13 | #include "DwarfCompileUnit.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 14 | #include "AddressPool.h" |
| 15 | #include "DwarfDebug.h" |
Adrian Prantl | 7813d9c | 2015-01-14 01:01:30 +0000 | [diff] [blame] | 16 | #include "DwarfExpression.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 17 | #include "DwarfUnit.h" |
| 18 | #include "llvm/ADT/None.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
Markus Lavin | b86ce21 | 2019-03-19 13:16:28 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
| 23 | #include "llvm/BinaryFormat/Dwarf.h" |
| 24 | #include "llvm/CodeGen/AsmPrinter.h" |
| 25 | #include "llvm/CodeGen/DIE.h" |
| 26 | #include "llvm/CodeGen/LexicalScopes.h" |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineInstr.h" |
| 29 | #include "llvm/CodeGen/MachineOperand.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/TargetFrameLowering.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 32 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 34 | #include "llvm/IR/DebugInfo.h" |
| 35 | #include "llvm/IR/DebugInfoMetadata.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 36 | #include "llvm/IR/GlobalVariable.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCSection.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCSymbol.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MachineLocation.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Casting.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetLoweringObjectFile.h" |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetOptions.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 45 | #include <algorithm> |
| 46 | #include <cassert> |
| 47 | #include <cstdint> |
| 48 | #include <iterator> |
| 49 | #include <memory> |
| 50 | #include <string> |
| 51 | #include <utility> |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 52 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 53 | using namespace llvm; |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 54 | |
Alexey Lapshin | 789e257 | 2019-11-30 23:48:32 +0300 | [diff] [blame^] | 55 | static dwarf::Tag GetCompileUnitType(UnitKind Kind, DwarfDebug *DW) { |
| 56 | |
| 57 | // According to DWARF Debugging Information Format Version 5, |
| 58 | // 3.1.2 Skeleton Compilation Unit Entries: |
| 59 | // "When generating a split DWARF object file (see Section 7.3.2 |
| 60 | // on page 187), the compilation unit in the .debug_info section |
| 61 | // is a "skeleton" compilation unit with the tag DW_TAG_skeleton_unit" |
| 62 | if (DW->getDwarfVersion() >= 5 && Kind == UnitKind::Skeleton) |
| 63 | return dwarf::DW_TAG_skeleton_unit; |
| 64 | |
| 65 | return dwarf::DW_TAG_compile_unit; |
| 66 | } |
| 67 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 68 | DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 69 | AsmPrinter *A, DwarfDebug *DW, |
Alexey Lapshin | 789e257 | 2019-11-30 23:48:32 +0300 | [diff] [blame^] | 70 | DwarfFile *DWU, UnitKind Kind) |
| 71 | : DwarfUnit(GetCompileUnitType(Kind, DW), Node, A, DW, DWU), UniqueID(UID) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 72 | insertDIE(Node, &getUnitDie()); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 73 | MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin"); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /// addLabelAddress - Add a dwarf label attribute data and value using |
| 77 | /// DW_FORM_addr or DW_FORM_GNU_addr_index. |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 78 | void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, |
| 79 | const MCSymbol *Label) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 80 | // Don't use the address pool in non-fission or in the skeleton unit itself. |
| 81 | // FIXME: Once GDB supports this, it's probably worthwhile using the address |
| 82 | // pool from the skeleton - maybe even in non-fission (possibly fewer |
| 83 | // relocations by sharing them in the pool, but we have other ideas about how |
| 84 | // to reduce the number of relocations as well/instead). |
David Blaikie | 161dd3c | 2018-10-20 06:02:15 +0000 | [diff] [blame] | 85 | if ((!DD->useSplitDwarf() || !Skeleton) && DD->getDwarfVersion() < 5) |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 86 | return addLocalLabelAddress(Die, Attribute, Label); |
| 87 | |
| 88 | if (Label) |
| 89 | DD->addArangeLabel(SymbolCU(this, Label)); |
| 90 | |
| 91 | unsigned idx = DD->getAddressPool().getIndex(Label); |
David Blaikie | 161dd3c | 2018-10-20 06:02:15 +0000 | [diff] [blame] | 92 | Die.addValue(DIEValueAllocator, Attribute, |
| 93 | DD->getDwarfVersion() >= 5 ? dwarf::DW_FORM_addrx |
| 94 | : dwarf::DW_FORM_GNU_addr_index, |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 95 | DIEInteger(idx)); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, |
| 99 | dwarf::Attribute Attribute, |
| 100 | const MCSymbol *Label) { |
| 101 | if (Label) |
| 102 | DD->addArangeLabel(SymbolCU(this, Label)); |
| 103 | |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 104 | if (Label) |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 105 | Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, |
| 106 | DIELabel(Label)); |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 107 | else |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 108 | Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, |
| 109 | DIEInteger(0)); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 112 | unsigned DwarfCompileUnit::getOrCreateSourceID(const DIFile *File) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 113 | // If we print assembly, we can't separate .file entries according to |
| 114 | // compile units. Thus all files will belong to the default compile unit. |
| 115 | |
| 116 | // FIXME: add a better feature test than hasRawTextSupport. Even better, |
| 117 | // extend .file to support this. |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 118 | unsigned CUID = Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID(); |
| 119 | if (!File) |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 120 | return Asm->OutStreamer->EmitDwarfFileDirective(0, "", "", None, None, CUID); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 121 | return Asm->OutStreamer->EmitDwarfFileDirective( |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 122 | 0, File->getDirectory(), File->getFilename(), getMD5AsBytes(File), |
| 123 | File->getSource(), CUID); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 126 | DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 127 | const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 128 | // Check for pre-existence. |
| 129 | if (DIE *Die = getDIE(GV)) |
| 130 | return Die; |
| 131 | |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 132 | assert(GV); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 133 | |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 134 | auto *GVContext = GV->getScope(); |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 135 | const DIType *GTy = GV->getType(); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 136 | |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 137 | // Construct the context before querying for the existence of the DIE in |
| 138 | // case such construction creates the DIE. |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 139 | auto *CB = GVContext ? dyn_cast<DICommonBlock>(GVContext) : nullptr; |
| 140 | DIE *ContextDIE = CB ? getOrCreateCommonBlock(CB, GlobalExprs) |
| 141 | : getOrCreateContextDIE(GVContext); |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 142 | |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 143 | // Add to map. |
| 144 | DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 145 | DIScope *DeclContext; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 146 | if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) { |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 147 | DeclContext = SDMDecl->getScope(); |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 148 | assert(SDMDecl->isStaticMember() && "Expected static member decl"); |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 149 | assert(GV->isDefinition()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 150 | // We need the declaration DIE that is in the static member's class. |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 151 | DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); |
| 152 | addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); |
Adrian Prantl | 3502f20 | 2016-11-08 22:11:38 +0000 | [diff] [blame] | 153 | // If the global variable's type is different from the one in the class |
| 154 | // member type, assume that it's more specific and also emit it. |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 155 | if (GTy != SDMDecl->getBaseType()) |
Adrian Prantl | 3502f20 | 2016-11-08 22:11:38 +0000 | [diff] [blame] | 156 | addType(*VariableDIE, GTy); |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 157 | } else { |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 158 | DeclContext = GV->getScope(); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 159 | // Add name and type. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 160 | addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 161 | addType(*VariableDIE, GTy); |
| 162 | |
| 163 | // Add scoping info. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 164 | if (!GV->isLocalToUnit()) |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 165 | addFlag(*VariableDIE, dwarf::DW_AT_external); |
| 166 | |
| 167 | // Add line number info. |
| 168 | addSourceLine(*VariableDIE, GV); |
| 169 | } |
| 170 | |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 171 | if (!GV->isDefinition()) |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 172 | addFlag(*VariableDIE, dwarf::DW_AT_declaration); |
David Blaikie | 877354a | 2015-04-14 18:08:25 +0000 | [diff] [blame] | 173 | else |
| 174 | addGlobalName(GV->getName(), *VariableDIE, DeclContext); |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 175 | |
Victor Leschuk | 3c98998 | 2016-10-26 11:59:03 +0000 | [diff] [blame] | 176 | if (uint32_t AlignInBytes = GV->getAlignInBytes()) |
| 177 | addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, |
| 178 | AlignInBytes); |
| 179 | |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 180 | if (MDTuple *TP = GV->getTemplateParams()) |
| 181 | addTemplateParams(*VariableDIE, DINodeArray(TP)); |
| 182 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 183 | // Add location. |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 184 | addLocationAttribute(VariableDIE, GV, GlobalExprs); |
| 185 | |
| 186 | return VariableDIE; |
| 187 | } |
| 188 | |
| 189 | void DwarfCompileUnit::addLocationAttribute( |
| 190 | DIE *VariableDIE, const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 191 | bool addToAccelTable = false; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 192 | DIELoc *Loc = nullptr; |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 193 | Optional<unsigned> NVPTXAddressSpace; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 194 | std::unique_ptr<DIEDwarfExpression> DwarfExpr; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 195 | for (const auto &GE : GlobalExprs) { |
| 196 | const GlobalVariable *Global = GE.Var; |
| 197 | const DIExpression *Expr = GE.Expr; |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 198 | |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 199 | // For compatibility with DWARF 3 and earlier, |
| 200 | // DW_AT_location(DW_OP_constu, X, DW_OP_stack_value) becomes |
| 201 | // DW_AT_const_value(X). |
| 202 | if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) { |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 203 | addToAccelTable = true; |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 204 | addConstantValue(*VariableDIE, /*Unsigned=*/true, Expr->getElement(1)); |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | // We cannot describe the location of dllimport'd variables: the |
| 209 | // computation of their address requires loads from the IAT. |
| 210 | if (Global && Global->hasDLLImportStorageClass()) |
| 211 | continue; |
| 212 | |
| 213 | // Nothing to describe without address or constant. |
| 214 | if (!Global && (!Expr || !Expr->isConstant())) |
| 215 | continue; |
| 216 | |
Lei Liu | 8e422b8 | 2018-08-01 23:46:49 +0000 | [diff] [blame] | 217 | if (Global && Global->isThreadLocal() && |
| 218 | !Asm->getObjFileLowering().supportDebugThreadLocalLocation()) |
| 219 | continue; |
| 220 | |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 221 | if (!Loc) { |
| 222 | addToAccelTable = true; |
| 223 | Loc = new (DIEValueAllocator) DIELoc; |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 224 | DwarfExpr = std::make_unique<DIEDwarfExpression>(*Asm, *this, *Loc); |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 227 | if (Expr) { |
| 228 | // According to |
| 229 | // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf |
| 230 | // cuda-gdb requires DW_AT_address_class for all variables to be able to |
| 231 | // correctly interpret address space of the variable address. |
| 232 | // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef |
| 233 | // sequence for the NVPTX + gdb target. |
| 234 | unsigned LocalNVPTXAddressSpace; |
| 235 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { |
| 236 | const DIExpression *NewExpr = |
| 237 | DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace); |
| 238 | if (NewExpr != Expr) { |
| 239 | Expr = NewExpr; |
| 240 | NVPTXAddressSpace = LocalNVPTXAddressSpace; |
| 241 | } |
| 242 | } |
Mikael Holmen | a9e3153 | 2018-01-29 12:37:30 +0000 | [diff] [blame] | 243 | DwarfExpr->addFragmentOffset(Expr); |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 244 | } |
Mikael Holmen | a9e3153 | 2018-01-29 12:37:30 +0000 | [diff] [blame] | 245 | |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 246 | if (Global) { |
| 247 | const MCSymbol *Sym = Asm->getSymbol(Global); |
| 248 | if (Global->isThreadLocal()) { |
Chih-Hung Hsieh | 9f9e468 | 2018-02-28 17:48:55 +0000 | [diff] [blame] | 249 | if (Asm->TM.useEmulatedTLS()) { |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 250 | // TODO: add debug info for emulated thread local mode. |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 251 | } else { |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 252 | // FIXME: Make this work with -gsplit-dwarf. |
| 253 | unsigned PointerSize = Asm->getDataLayout().getPointerSize(); |
| 254 | assert((PointerSize == 4 || PointerSize == 8) && |
| 255 | "Add support for other sizes if necessary"); |
| 256 | // Based on GCC's support for TLS: |
| 257 | if (!DD->useSplitDwarf()) { |
| 258 | // 1) Start with a constNu of the appropriate pointer size |
| 259 | addUInt(*Loc, dwarf::DW_FORM_data1, |
| 260 | PointerSize == 4 ? dwarf::DW_OP_const4u |
| 261 | : dwarf::DW_OP_const8u); |
| 262 | // 2) containing the (relocated) offset of the TLS variable |
| 263 | // within the module's TLS block. |
| 264 | addExpr(*Loc, dwarf::DW_FORM_udata, |
| 265 | Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); |
| 266 | } else { |
| 267 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); |
| 268 | addUInt(*Loc, dwarf::DW_FORM_udata, |
| 269 | DD->getAddressPool().getIndex(Sym, /* TLS */ true)); |
| 270 | } |
| 271 | // 3) followed by an OP to make the debugger do a TLS lookup. |
| 272 | addUInt(*Loc, dwarf::DW_FORM_data1, |
| 273 | DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address |
| 274 | : dwarf::DW_OP_form_tls_address); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 275 | } |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 276 | } else { |
| 277 | DD->addArangeLabel(SymbolCU(this, Sym)); |
| 278 | addOpAddress(*Loc, Sym); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 279 | } |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 280 | } |
Adrian Prantl | 4b542c6 | 2018-04-27 22:05:31 +0000 | [diff] [blame] | 281 | // Global variables attached to symbols are memory locations. |
| 282 | // It would be better if this were unconditional, but malformed input that |
| 283 | // mixes non-fragments and fragments for the same variable is too expensive |
| 284 | // to detect in the verifier. |
Adrian Prantl | 3edc63a | 2018-05-04 16:10:43 +0000 | [diff] [blame] | 285 | if (DwarfExpr->isUnknownLocation()) |
Adrian Prantl | 4b542c6 | 2018-04-27 22:05:31 +0000 | [diff] [blame] | 286 | DwarfExpr->setMemoryLocationKind(); |
| 287 | DwarfExpr->addExpression(Expr); |
Adrian Prantl | 73ec065 | 2016-12-16 19:39:01 +0000 | [diff] [blame] | 288 | } |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 289 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { |
| 290 | // According to |
| 291 | // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf |
| 292 | // cuda-gdb requires DW_AT_address_class for all variables to be able to |
| 293 | // correctly interpret address space of the variable address. |
| 294 | const unsigned NVPTX_ADDR_global_space = 5; |
| 295 | addUInt(*VariableDIE, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, |
| 296 | NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_global_space); |
| 297 | } |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 298 | if (Loc) |
| 299 | addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize()); |
| 300 | |
| 301 | if (DD->useAllLinkageNames()) |
| 302 | addLinkageName(*VariableDIE, GV->getLinkageName()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 303 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 304 | if (addToAccelTable) { |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 305 | DD->addAccelName(*CUNode, GV->getName(), *VariableDIE); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 306 | |
| 307 | // If the linkage name is different than the name, go ahead and output |
| 308 | // that as well into the name table. |
Pavel Labath | 2a6afe5 | 2018-05-14 14:13:20 +0000 | [diff] [blame] | 309 | if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName() && |
| 310 | DD->useAllLinkageNames()) |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 311 | DD->addAccelName(*CUNode, GV->getLinkageName(), *VariableDIE); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 312 | } |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 313 | } |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 314 | |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 315 | DIE *DwarfCompileUnit::getOrCreateCommonBlock( |
| 316 | const DICommonBlock *CB, ArrayRef<GlobalExpr> GlobalExprs) { |
| 317 | // Construct the context before querying for the existence of the DIE in case |
| 318 | // such construction creates the DIE. |
| 319 | DIE *ContextDIE = getOrCreateContextDIE(CB->getScope()); |
| 320 | |
| 321 | if (DIE *NDie = getDIE(CB)) |
| 322 | return NDie; |
| 323 | DIE &NDie = createAndAddDIE(dwarf::DW_TAG_common_block, *ContextDIE, CB); |
| 324 | StringRef Name = CB->getName().empty() ? "_BLNK_" : CB->getName(); |
| 325 | addString(NDie, dwarf::DW_AT_name, Name); |
| 326 | addGlobalName(Name, NDie, CB->getScope()); |
| 327 | if (CB->getFile()) |
| 328 | addSourceLine(NDie, CB->getLineNo(), CB->getFile()); |
| 329 | if (DIGlobalVariable *V = CB->getDecl()) |
| 330 | getCU().addLocationAttribute(&NDie, V, GlobalExprs); |
| 331 | return &NDie; |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void DwarfCompileUnit::addRange(RangeSpan Range) { |
| 335 | bool SameAsPrevCU = this == DD->getPrevCU(); |
| 336 | DD->setPrevCU(this); |
| 337 | // If we have no current ranges just add the range and return, otherwise, |
| 338 | // check the current section and CU against the previous section and CU we |
| 339 | // emitted into and the subprogram was contained within. If these are the |
| 340 | // same then extend our current range, otherwise add this as a new range. |
| 341 | if (CURanges.empty() || !SameAsPrevCU || |
David Blaikie | b677cb8d | 2019-10-02 22:27:24 +0000 | [diff] [blame] | 342 | (&CURanges.back().End->getSection() != |
| 343 | &Range.End->getSection())) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 344 | CURanges.push_back(Range); |
| 345 | return; |
| 346 | } |
| 347 | |
David Blaikie | b677cb8d | 2019-10-02 22:27:24 +0000 | [diff] [blame] | 348 | CURanges.back().End = Range.End; |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 351 | void DwarfCompileUnit::initStmtList() { |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 352 | if (CUNode->isDebugDirectivesOnly()) |
| 353 | return; |
| 354 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 355 | // Define start line table label for each Compile Unit. |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 356 | MCSymbol *LineTableStartSym; |
| 357 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 358 | if (DD->useSectionsAsReferences()) { |
| 359 | LineTableStartSym = TLOF.getDwarfLineSection()->getBeginSymbol(); |
| 360 | } else { |
| 361 | LineTableStartSym = |
| 362 | Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID()); |
| 363 | } |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 364 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 365 | // DW_AT_stmt_list is a offset of line number information for this |
| 366 | // compile unit in debug_line section. For split dwarf this is |
| 367 | // left in the skeleton CU and so not included. |
| 368 | // The line table entries are not always emitted in assembly, so it |
| 369 | // is not okay to use line_table_start here. |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 370 | StmtListValue = |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 371 | addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym, |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 372 | TLOF.getDwarfLineSection()->getBeginSymbol()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void DwarfCompileUnit::applyStmtList(DIE &D) { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 376 | D.addValue(DIEValueAllocator, *StmtListValue); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 377 | } |
| 378 | |
David Blaikie | 14499a7 | 2014-10-04 15:58:47 +0000 | [diff] [blame] | 379 | void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, |
| 380 | const MCSymbol *End) { |
| 381 | assert(Begin && "Begin label should not be null!"); |
| 382 | assert(End && "End label should not be null!"); |
| 383 | assert(Begin->isDefined() && "Invalid starting label"); |
| 384 | assert(End->isDefined() && "Invalid end label"); |
| 385 | |
| 386 | addLabelAddress(D, dwarf::DW_AT_low_pc, Begin); |
| 387 | if (DD->getDwarfVersion() < 4) |
| 388 | addLabelAddress(D, dwarf::DW_AT_high_pc, End); |
| 389 | else |
| 390 | addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin); |
| 391 | } |
| 392 | |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 393 | // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc |
| 394 | // and DW_AT_high_pc attributes. If there are global variables in this |
| 395 | // scope then create and insert DIEs for these variables. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 396 | DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) { |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 397 | DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes()); |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 398 | |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 399 | attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd()); |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 400 | if (DD->useAppleExtensionAttributes() && |
| 401 | !DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim( |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 402 | *DD->getCurrentFunction())) |
| 403 | addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr); |
| 404 | |
| 405 | // Only include DW_AT_frame_base in full debug info |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 406 | if (!includeMinimalInlineScopes()) { |
Alexey Bataev | 7ae86fe | 2018-07-26 16:10:05 +0000 | [diff] [blame] | 407 | if (Asm->MF->getTarget().getTargetTriple().isNVPTX()) { |
| 408 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
| 409 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_call_frame_cfa); |
| 410 | addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc); |
| 411 | } else { |
| 412 | const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo(); |
| 413 | MachineLocation Location(RI->getFrameRegister(*Asm->MF)); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 414 | if (Register::isPhysicalRegister(Location.getReg())) |
Alexey Bataev | 7ae86fe | 2018-07-26 16:10:05 +0000 | [diff] [blame] | 415 | addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); |
| 416 | } |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // Add name to the name table, we do this here because we're guaranteed |
| 420 | // to have concrete versions of our DW_TAG_subprogram nodes. |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 421 | DD->addSubprogramNames(*CUNode, SP, *SPDie); |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 422 | |
| 423 | return *SPDie; |
| 424 | } |
| 425 | |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 426 | // Construct a DIE for this scope. |
| 427 | void DwarfCompileUnit::constructScopeDIE( |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 428 | LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) { |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 429 | if (!Scope || !Scope->getScopeNode()) |
| 430 | return; |
| 431 | |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 432 | auto *DS = Scope->getScopeNode(); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 433 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 434 | assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) && |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 435 | "Only handle inlined subprograms here, use " |
| 436 | "constructSubprogramScopeDIE for non-inlined " |
| 437 | "subprograms"); |
| 438 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 439 | SmallVector<DIE *, 8> Children; |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 440 | |
| 441 | // We try to create the scope DIE first, then the children DIEs. This will |
| 442 | // avoid creating un-used children then removing them later when we find out |
| 443 | // the scope DIE is null. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 444 | DIE *ScopeDIE; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 445 | if (Scope->getParent() && isa<DISubprogram>(DS)) { |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 446 | ScopeDIE = constructInlinedScopeDIE(Scope); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 447 | if (!ScopeDIE) |
| 448 | return; |
| 449 | // We create children when the scope DIE is not null. |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 450 | createScopeChildrenDIE(Scope, Children); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 451 | } else { |
| 452 | // Early exit when we know the scope DIE is going to be null. |
| 453 | if (DD->isLexicalScopeDIENull(Scope)) |
| 454 | return; |
| 455 | |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 456 | bool HasNonScopeChildren = false; |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 457 | |
| 458 | // We create children here when we know the scope DIE is not going to be |
| 459 | // null and the children will be added to the scope DIE. |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 460 | createScopeChildrenDIE(Scope, Children, &HasNonScopeChildren); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 461 | |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 462 | // If there are only other scopes as children, put them directly in the |
| 463 | // parent instead, as this scope would serve no purpose. |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 464 | if (!HasNonScopeChildren) { |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 465 | FinalChildren.insert(FinalChildren.end(), |
| 466 | std::make_move_iterator(Children.begin()), |
| 467 | std::make_move_iterator(Children.end())); |
| 468 | return; |
| 469 | } |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 470 | ScopeDIE = constructLexicalScopeDIE(Scope); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 471 | assert(ScopeDIE && "Scope DIE should not be null."); |
| 472 | } |
| 473 | |
| 474 | // Add children |
| 475 | for (auto &I : Children) |
| 476 | ScopeDIE->addChild(std::move(I)); |
| 477 | |
| 478 | FinalChildren.push_back(std::move(ScopeDIE)); |
| 479 | } |
| 480 | |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 481 | void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, |
| 482 | SmallVector<RangeSpan, 2> Range) { |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 483 | |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 484 | HasRangeLists = true; |
| 485 | |
| 486 | // Add the range list to the set of ranges to be emitted. |
| 487 | auto IndexAndList = |
| 488 | (DD->getDwarfVersion() < 5 && Skeleton ? Skeleton->DU : DU) |
David Blaikie | c8f7e6c | 2018-11-08 00:35:54 +0000 | [diff] [blame] | 489 | ->addRange(*(Skeleton ? Skeleton : this), std::move(Range)); |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 490 | |
| 491 | uint32_t Index = IndexAndList.first; |
| 492 | auto &List = *IndexAndList.second; |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 493 | |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 494 | // Under fission, ranges are specified by constant offsets relative to the |
| 495 | // CU's DW_AT_GNU_ranges_base. |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 496 | // FIXME: For DWARF v5, do not generate the DW_AT_ranges attribute under |
| 497 | // fission until we support the forms using the .debug_addr section |
| 498 | // (DW_RLE_startx_endx etc.). |
David Blaikie | 32e09de | 2018-10-20 08:12:36 +0000 | [diff] [blame] | 499 | if (DD->getDwarfVersion() >= 5) |
| 500 | addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_rnglistx, Index); |
David Blaikie | de9aa37 | 2019-10-11 23:51:24 +0000 | [diff] [blame] | 501 | else { |
| 502 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 503 | const MCSymbol *RangeSectionSym = |
| 504 | TLOF.getDwarfRangesSection()->getBeginSymbol(); |
| 505 | if (isDwoUnit()) |
| 506 | addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), |
| 507 | RangeSectionSym); |
| 508 | else |
| 509 | addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), |
| 510 | RangeSectionSym); |
| 511 | } |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 512 | } |
| 513 | |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 514 | void DwarfCompileUnit::attachRangesOrLowHighPC( |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 515 | DIE &Die, SmallVector<RangeSpan, 2> Ranges) { |
Alexey Bataev | 858a7dd | 2018-03-20 20:21:38 +0000 | [diff] [blame] | 516 | if (Ranges.size() == 1 || !DD->useRangesSection()) { |
| 517 | const RangeSpan &Front = Ranges.front(); |
| 518 | const RangeSpan &Back = Ranges.back(); |
David Blaikie | b677cb8d | 2019-10-02 22:27:24 +0000 | [diff] [blame] | 519 | attachLowHighPC(Die, Front.Begin, Back.End); |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 520 | } else |
| 521 | addScopeRangeList(Die, std::move(Ranges)); |
| 522 | } |
| 523 | |
| 524 | void DwarfCompileUnit::attachRangesOrLowHighPC( |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 525 | DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 526 | SmallVector<RangeSpan, 2> List; |
| 527 | List.reserve(Ranges.size()); |
| 528 | for (const InsnRange &R : Ranges) |
David Blaikie | b677cb8d | 2019-10-02 22:27:24 +0000 | [diff] [blame] | 529 | List.push_back( |
| 530 | {DD->getLabelBeforeInsn(R.first), DD->getLabelAfterInsn(R.second)}); |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 531 | attachRangesOrLowHighPC(Die, std::move(List)); |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 532 | } |
| 533 | |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 534 | // This scope represents inlined body of a function. Construct DIE to |
| 535 | // represent this concrete inlined copy of the function. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 536 | DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 537 | assert(Scope->getScopeNode()); |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 538 | auto *DS = Scope->getScopeNode(); |
| 539 | auto *InlinedSP = getDISubprogram(DS); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 540 | // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram |
| 541 | // was inlined from another compile unit. |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 542 | DIE *OriginDIE = getAbstractSPDies()[InlinedSP]; |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 543 | assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); |
| 544 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 545 | auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 546 | addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); |
| 547 | |
| 548 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); |
| 549 | |
| 550 | // Add the call site information to the DIE. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 551 | const DILocation *IA = Scope->getInlinedAt(); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 552 | addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 553 | getOrCreateSourceID(IA->getFile())); |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 554 | addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine()); |
David Blaikie | 40580d3 | 2019-07-16 21:15:19 +0000 | [diff] [blame] | 555 | if (IA->getColumn()) |
| 556 | addUInt(*ScopeDIE, dwarf::DW_AT_call_column, None, IA->getColumn()); |
Dehao Chen | 6e0c844 | 2016-10-07 15:21:31 +0000 | [diff] [blame] | 557 | if (IA->getDiscriminator() && DD->getDwarfVersion() >= 4) |
Dehao Chen | 5451135 | 2015-11-11 18:09:47 +0000 | [diff] [blame] | 558 | addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None, |
| 559 | IA->getDiscriminator()); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 560 | |
| 561 | // Add name to the name table, we do this here because we're guaranteed |
| 562 | // to have concrete versions of our DW_TAG_inlined_subprogram nodes. |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 563 | DD->addSubprogramNames(*CUNode, InlinedSP, *ScopeDIE); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 564 | |
| 565 | return ScopeDIE; |
| 566 | } |
| 567 | |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 568 | // Construct new DW_TAG_lexical_block for this scope and attach |
| 569 | // DW_AT_low_pc/DW_AT_high_pc labels. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 570 | DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 571 | if (DD->isLexicalScopeDIENull(Scope)) |
| 572 | return nullptr; |
| 573 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 574 | auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block); |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 575 | if (Scope->isAbstractScope()) |
| 576 | return ScopeDIE; |
| 577 | |
| 578 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); |
| 579 | |
| 580 | return ScopeDIE; |
| 581 | } |
| 582 | |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 583 | /// constructVariableDIE - Construct a DIE for the given DbgVariable. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 584 | DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) { |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 585 | auto D = constructVariableDIEImpl(DV, Abstract); |
| 586 | DV.setDIE(*D); |
| 587 | return D; |
| 588 | } |
| 589 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 590 | DIE *DwarfCompileUnit::constructLabelDIE(DbgLabel &DL, |
| 591 | const LexicalScope &Scope) { |
| 592 | auto LabelDie = DIE::get(DIEValueAllocator, DL.getTag()); |
| 593 | insertDIE(DL.getLabel(), LabelDie); |
| 594 | DL.setDIE(*LabelDie); |
| 595 | |
| 596 | if (Scope.isAbstractScope()) |
| 597 | applyLabelAttributes(DL, *LabelDie); |
| 598 | |
| 599 | return LabelDie; |
| 600 | } |
| 601 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 602 | DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, |
| 603 | bool Abstract) { |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 604 | // Define variable debug information entry. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 605 | auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag()); |
Sander de Smalen | dc00bec | 2018-01-24 13:35:54 +0000 | [diff] [blame] | 606 | insertDIE(DV.getVariable(), VariableDie); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 607 | |
| 608 | if (Abstract) { |
| 609 | applyVariableAttributes(DV, *VariableDie); |
| 610 | return VariableDie; |
| 611 | } |
| 612 | |
| 613 | // Add variable address. |
| 614 | |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 615 | unsigned Offset = DV.getDebugLocListIndex(); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 616 | if (Offset != ~0U) { |
| 617 | addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); |
| 618 | return VariableDie; |
| 619 | } |
| 620 | |
Nikola Prica | abc1dff | 2019-06-10 08:41:06 +0000 | [diff] [blame] | 621 | // Check if variable has a single location description. |
| 622 | if (auto *DVal = DV.getValueLoc()) { |
| 623 | if (DVal->isLocation()) |
| 624 | addVariableAddress(DV, *VariableDie, DVal->getLoc()); |
| 625 | else if (DVal->isInt()) { |
Adrian Prantl | 3b89e66 | 2016-02-17 22:20:08 +0000 | [diff] [blame] | 626 | auto *Expr = DV.getSingleExpression(); |
| 627 | if (Expr && Expr->getNumElements()) { |
| 628 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
| 629 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
| 630 | // If there is an expression, emit raw unsigned bytes. |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 631 | DwarfExpr.addFragmentOffset(Expr); |
Nikola Prica | abc1dff | 2019-06-10 08:41:06 +0000 | [diff] [blame] | 632 | DwarfExpr.addUnsignedConstant(DVal->getInt()); |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 633 | DwarfExpr.addExpression(Expr); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 634 | addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); |
Adrian Prantl | 3b89e66 | 2016-02-17 22:20:08 +0000 | [diff] [blame] | 635 | } else |
Nikola Prica | abc1dff | 2019-06-10 08:41:06 +0000 | [diff] [blame] | 636 | addConstantValue(*VariableDie, DVal->getInt(), DV.getType()); |
| 637 | } else if (DVal->isConstantFP()) { |
| 638 | addConstantFPValue(*VariableDie, DVal->getConstantFP()); |
| 639 | } else if (DVal->isConstantInt()) { |
| 640 | addConstantValue(*VariableDie, DVal->getConstantInt(), DV.getType()); |
| 641 | } |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 642 | return VariableDie; |
| 643 | } |
| 644 | |
| 645 | // .. else use frame index. |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 646 | if (!DV.hasFrameIndexExprs()) |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 647 | return VariableDie; |
| 648 | |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 649 | Optional<unsigned> NVPTXAddressSpace; |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 650 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 651 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 652 | for (auto &Fragment : DV.getFrameIndexExprs()) { |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 653 | unsigned FrameReg = 0; |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 654 | const DIExpression *Expr = Fragment.Expr; |
Eric Christopher | d4e723f | 2015-02-20 22:36:11 +0000 | [diff] [blame] | 655 | const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 656 | int Offset = TFI->getFrameIndexReference(*Asm->MF, Fragment.FI, FrameReg); |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 657 | DwarfExpr.addFragmentOffset(Expr); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 658 | SmallVector<uint64_t, 8> Ops; |
Bjorn Pettersson | 2b698a1 | 2019-09-07 11:40:10 +0000 | [diff] [blame] | 659 | DIExpression::appendOffset(Ops, Offset); |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 660 | // According to |
| 661 | // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf |
| 662 | // cuda-gdb requires DW_AT_address_class for all variables to be able to |
| 663 | // correctly interpret address space of the variable address. |
| 664 | // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef |
| 665 | // sequence for the NVPTX + gdb target. |
| 666 | unsigned LocalNVPTXAddressSpace; |
| 667 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { |
| 668 | const DIExpression *NewExpr = |
| 669 | DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace); |
| 670 | if (NewExpr != Expr) { |
| 671 | Expr = NewExpr; |
| 672 | NVPTXAddressSpace = LocalNVPTXAddressSpace; |
| 673 | } |
| 674 | } |
| 675 | if (Expr) |
| 676 | Ops.append(Expr->elements_begin(), Expr->elements_end()); |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 677 | DIExpressionCursor Cursor(Ops); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 678 | DwarfExpr.setMemoryLocationKind(); |
Alexey Bataev | 4dd7558 | 2018-07-26 16:29:52 +0000 | [diff] [blame] | 679 | if (const MCSymbol *FrameSymbol = Asm->getFunctionFrameSymbol()) |
| 680 | addOpAddress(*Loc, FrameSymbol); |
| 681 | else |
| 682 | DwarfExpr.addMachineRegExpression( |
| 683 | *Asm->MF->getSubtarget().getRegisterInfo(), Cursor, FrameReg); |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 684 | DwarfExpr.addExpression(std::move(Cursor)); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 685 | } |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 686 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { |
| 687 | // According to |
| 688 | // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf |
| 689 | // cuda-gdb requires DW_AT_address_class for all variables to be able to |
| 690 | // correctly interpret address space of the variable address. |
| 691 | const unsigned NVPTX_ADDR_local_space = 6; |
| 692 | addUInt(*VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, |
| 693 | NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_local_space); |
| 694 | } |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 695 | addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); |
Peter Collingbourne | fb9ce10 | 2019-06-17 23:39:41 +0000 | [diff] [blame] | 696 | if (DwarfExpr.TagOffset) |
| 697 | addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, |
| 698 | *DwarfExpr.TagOffset); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 699 | |
| 700 | return VariableDie; |
| 701 | } |
| 702 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 703 | DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, |
| 704 | const LexicalScope &Scope, |
| 705 | DIE *&ObjectPointer) { |
David Blaikie | 4a1a44e | 2014-10-09 17:56:39 +0000 | [diff] [blame] | 706 | auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); |
| 707 | if (DV.isObjectPointer()) |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 708 | ObjectPointer = Var; |
David Blaikie | 4a1a44e | 2014-10-09 17:56:39 +0000 | [diff] [blame] | 709 | return Var; |
| 710 | } |
| 711 | |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 712 | /// Return all DIVariables that appear in count: expressions. |
| 713 | static SmallVector<const DIVariable *, 2> dependencies(DbgVariable *Var) { |
| 714 | SmallVector<const DIVariable *, 2> Result; |
| 715 | auto *Array = dyn_cast<DICompositeType>(Var->getType()); |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 716 | if (!Array || Array->getTag() != dwarf::DW_TAG_array_type) |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 717 | return Result; |
| 718 | for (auto *El : Array->getElements()) { |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 719 | if (auto *Subrange = dyn_cast<DISubrange>(El)) { |
| 720 | auto Count = Subrange->getCount(); |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 721 | if (auto *Dependency = Count.dyn_cast<DIVariable *>()) |
| 722 | Result.push_back(Dependency); |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 723 | } |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 724 | } |
| 725 | return Result; |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /// Sort local variables so that variables appearing inside of helper |
| 729 | /// expressions come first. |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 730 | static SmallVector<DbgVariable *, 8> |
| 731 | sortLocalVars(SmallVectorImpl<DbgVariable *> &Input) { |
| 732 | SmallVector<DbgVariable *, 8> Result; |
| 733 | SmallVector<PointerIntPair<DbgVariable *, 1>, 8> WorkList; |
| 734 | // Map back from a DIVariable to its containing DbgVariable. |
| 735 | SmallDenseMap<const DILocalVariable *, DbgVariable *> DbgVar; |
| 736 | // Set of DbgVariables in Result. |
| 737 | SmallDenseSet<DbgVariable *, 8> Visited; |
| 738 | // For cycle detection. |
| 739 | SmallDenseSet<DbgVariable *, 8> Visiting; |
| 740 | |
| 741 | // Initialize the worklist and the DIVariable lookup table. |
| 742 | for (auto Var : reverse(Input)) { |
| 743 | DbgVar.insert({Var->getVariable(), Var}); |
| 744 | WorkList.push_back({Var, 0}); |
| 745 | } |
| 746 | |
| 747 | // Perform a stable topological sort by doing a DFS. |
| 748 | while (!WorkList.empty()) { |
| 749 | auto Item = WorkList.back(); |
| 750 | DbgVariable *Var = Item.getPointer(); |
| 751 | bool visitedAllDependencies = Item.getInt(); |
| 752 | WorkList.pop_back(); |
| 753 | |
| 754 | // Dependency is in a different lexical scope or a global. |
| 755 | if (!Var) |
| 756 | continue; |
| 757 | |
| 758 | // Already handled. |
| 759 | if (Visited.count(Var)) |
| 760 | continue; |
| 761 | |
| 762 | // Add to Result if all dependencies are visited. |
| 763 | if (visitedAllDependencies) { |
| 764 | Visited.insert(Var); |
| 765 | Result.push_back(Var); |
| 766 | continue; |
| 767 | } |
| 768 | |
| 769 | // Detect cycles. |
| 770 | auto Res = Visiting.insert(Var); |
| 771 | if (!Res.second) { |
| 772 | assert(false && "dependency cycle in local variables"); |
| 773 | return Result; |
| 774 | } |
| 775 | |
| 776 | // Push dependencies and this node onto the worklist, so that this node is |
| 777 | // visited again after all of its dependencies are handled. |
| 778 | WorkList.push_back({Var, 1}); |
| 779 | for (auto *Dependency : dependencies(Var)) { |
| 780 | auto Dep = dyn_cast_or_null<const DILocalVariable>(Dependency); |
| 781 | WorkList.push_back({DbgVar[Dep], 0}); |
| 782 | } |
| 783 | } |
| 784 | return Result; |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 787 | DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, |
| 788 | SmallVectorImpl<DIE *> &Children, |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 789 | bool *HasNonScopeChildren) { |
| 790 | assert(Children.empty()); |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 791 | DIE *ObjectPointer = nullptr; |
| 792 | |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 793 | // Emit function arguments (order is significant). |
| 794 | auto Vars = DU->getScopeVariables().lookup(Scope); |
| 795 | for (auto &DV : Vars.Args) |
| 796 | Children.push_back(constructVariableDIE(*DV.second, *Scope, ObjectPointer)); |
| 797 | |
| 798 | // Emit local variables. |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 799 | auto Locals = sortLocalVars(Vars.Locals); |
| 800 | for (DbgVariable *DV : Locals) |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 801 | Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); |
| 802 | |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 803 | // Skip imported directives in gmlt-like data. |
| 804 | if (!includeMinimalInlineScopes()) { |
| 805 | // There is no need to emit empty lexical block DIE. |
| 806 | for (const auto *IE : ImportedEntities[Scope->getScopeNode()]) |
| 807 | Children.push_back( |
| 808 | constructImportedEntityDIE(cast<DIImportedEntity>(IE))); |
| 809 | } |
| 810 | |
| 811 | if (HasNonScopeChildren) |
| 812 | *HasNonScopeChildren = !Children.empty(); |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 813 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 814 | for (DbgLabel *DL : DU->getScopeLabels().lookup(Scope)) |
| 815 | Children.push_back(constructLabelDIE(*DL, *Scope)); |
| 816 | |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 817 | for (LexicalScope *LS : Scope->getChildren()) |
| 818 | constructScopeDIE(LS, Children); |
| 819 | |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 820 | return ObjectPointer; |
| 821 | } |
| 822 | |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 823 | DIE &DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub, |
| 824 | LexicalScope *Scope) { |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 825 | DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); |
| 826 | |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 827 | if (Scope) { |
| 828 | assert(!Scope->getInlinedAt()); |
| 829 | assert(!Scope->isAbstractScope()); |
| 830 | // Collect lexical scope children first. |
| 831 | // ObjectPointer might be a local (non-argument) local variable if it's a |
| 832 | // block's synthetic this pointer. |
| 833 | if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE)) |
| 834 | addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); |
| 835 | } |
| 836 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 837 | // If this is a variadic function, add an unspecified parameter. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 838 | DITypeRefArray FnArgs = Sub->getType()->getTypeArray(); |
David Blaikie | 1dd573d | 2014-10-23 22:27:50 +0000 | [diff] [blame] | 839 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 840 | // If we have a single element of null, it is a function that returns void. |
| 841 | // If we have more than one elements and the last one is null, it is a |
| 842 | // variadic function. |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 843 | if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] && |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 844 | !includeMinimalInlineScopes()) |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 845 | ScopeDIE.addChild( |
| 846 | DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters)); |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 847 | |
| 848 | return ScopeDIE; |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 849 | } |
| 850 | |
David Blaikie | 78b65b6 | 2014-10-09 20:26:15 +0000 | [diff] [blame] | 851 | DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, |
| 852 | DIE &ScopeDIE) { |
| 853 | // We create children when the scope DIE is not null. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 854 | SmallVector<DIE *, 8> Children; |
David Blaikie | 78b65b6 | 2014-10-09 20:26:15 +0000 | [diff] [blame] | 855 | DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); |
| 856 | |
| 857 | // Add children |
| 858 | for (auto &I : Children) |
| 859 | ScopeDIE.addChild(std::move(I)); |
| 860 | |
| 861 | return ObjectPointer; |
| 862 | } |
| 863 | |
Dehao Chen | 5451135 | 2015-11-11 18:09:47 +0000 | [diff] [blame] | 864 | void DwarfCompileUnit::constructAbstractSubprogramScopeDIE( |
| 865 | LexicalScope *Scope) { |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 866 | DIE *&AbsDef = getAbstractSPDies()[Scope->getScopeNode()]; |
David Blaikie | 49be5b3 | 2014-10-31 21:57:02 +0000 | [diff] [blame] | 867 | if (AbsDef) |
| 868 | return; |
| 869 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 870 | auto *SP = cast<DISubprogram>(Scope->getScopeNode()); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 871 | |
| 872 | DIE *ContextDIE; |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 873 | DwarfCompileUnit *ContextCU = this; |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 874 | |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 875 | if (includeMinimalInlineScopes()) |
| 876 | ContextDIE = &getUnitDie(); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 877 | // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with |
Duncan P. N. Exon Smith | 7c60f20 | 2015-04-18 00:35:36 +0000 | [diff] [blame] | 878 | // the important distinction that the debug node is not associated with the |
| 879 | // DIE (since the debug node will be associated with the concrete DIE, if |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 880 | // any). It could be refactored to some common utility function. |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 881 | else if (auto *SPDecl = SP->getDeclaration()) { |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 882 | ContextDIE = &getUnitDie(); |
| 883 | getOrCreateSubprogramDIE(SPDecl); |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 884 | } else { |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 885 | ContextDIE = getOrCreateContextDIE(SP->getScope()); |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 886 | // The scope may be shared with a subprogram that has already been |
| 887 | // constructed in another CU, in which case we need to construct this |
| 888 | // subprogram in the same CU. |
| 889 | ContextCU = DD->lookupCU(ContextDIE->getUnitDie()); |
| 890 | } |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 891 | |
Duncan P. N. Exon Smith | 7c60f20 | 2015-04-18 00:35:36 +0000 | [diff] [blame] | 892 | // Passing null as the associated node because the abstract definition |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 893 | // shouldn't be found by lookup. |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 894 | AbsDef = &ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr); |
| 895 | ContextCU->applySubprogramAttributesToDefinition(SP, *AbsDef); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 896 | |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 897 | if (!ContextCU->includeMinimalInlineScopes()) |
| 898 | ContextCU->addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); |
| 899 | if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren(Scope, *AbsDef)) |
| 900 | ContextCU->addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Vedant Kumar | 533dd02 | 2019-08-26 20:53:12 +0000 | [diff] [blame] | 903 | /// Whether to use the GNU analog for a DWARF5 tag, attribute, or location atom. |
| 904 | static bool useGNUAnalogForDwarf5Feature(DwarfDebug *DD) { |
| 905 | return DD->getDwarfVersion() == 4 && DD->tuneForGDB(); |
| 906 | } |
| 907 | |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 908 | dwarf::Tag DwarfCompileUnit::getDwarf5OrGNUTag(dwarf::Tag Tag) const { |
Vedant Kumar | 533dd02 | 2019-08-26 20:53:12 +0000 | [diff] [blame] | 909 | if (!useGNUAnalogForDwarf5Feature(DD)) |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 910 | return Tag; |
| 911 | switch (Tag) { |
| 912 | case dwarf::DW_TAG_call_site: |
| 913 | return dwarf::DW_TAG_GNU_call_site; |
| 914 | case dwarf::DW_TAG_call_site_parameter: |
| 915 | return dwarf::DW_TAG_GNU_call_site_parameter; |
| 916 | default: |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 917 | llvm_unreachable("DWARF5 tag with no GNU analog"); |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
| 921 | dwarf::Attribute |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 922 | DwarfCompileUnit::getDwarf5OrGNUAttr(dwarf::Attribute Attr) const { |
Vedant Kumar | 533dd02 | 2019-08-26 20:53:12 +0000 | [diff] [blame] | 923 | if (!useGNUAnalogForDwarf5Feature(DD)) |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 924 | return Attr; |
| 925 | switch (Attr) { |
| 926 | case dwarf::DW_AT_call_all_calls: |
| 927 | return dwarf::DW_AT_GNU_all_call_sites; |
| 928 | case dwarf::DW_AT_call_target: |
| 929 | return dwarf::DW_AT_GNU_call_site_target; |
| 930 | case dwarf::DW_AT_call_origin: |
| 931 | return dwarf::DW_AT_abstract_origin; |
| 932 | case dwarf::DW_AT_call_pc: |
| 933 | return dwarf::DW_AT_low_pc; |
| 934 | case dwarf::DW_AT_call_value: |
| 935 | return dwarf::DW_AT_GNU_call_site_value; |
| 936 | case dwarf::DW_AT_call_tail_call: |
| 937 | return dwarf::DW_AT_GNU_tail_call; |
| 938 | default: |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 939 | llvm_unreachable("DWARF5 attribute with no GNU analog"); |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
Vedant Kumar | 533dd02 | 2019-08-26 20:53:12 +0000 | [diff] [blame] | 943 | dwarf::LocationAtom |
| 944 | DwarfCompileUnit::getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const { |
| 945 | if (!useGNUAnalogForDwarf5Feature(DD)) |
| 946 | return Loc; |
| 947 | switch (Loc) { |
| 948 | case dwarf::DW_OP_entry_value: |
| 949 | return dwarf::DW_OP_GNU_entry_value; |
| 950 | default: |
| 951 | llvm_unreachable("DWARF5 location atom with no GNU analog"); |
| 952 | } |
| 953 | } |
| 954 | |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 955 | DIE &DwarfCompileUnit::constructCallSiteEntryDIE( |
| 956 | DIE &ScopeDIE, const DISubprogram *CalleeSP, bool IsTail, |
| 957 | const MCSymbol *PCAddr, const MCExpr *PCOffset, unsigned CallReg) { |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 958 | // Insert a call site entry DIE within ScopeDIE. |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 959 | DIE &CallSiteDIE = createAndAddDIE(getDwarf5OrGNUTag(dwarf::DW_TAG_call_site), |
| 960 | ScopeDIE, nullptr); |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 961 | |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 962 | if (CallReg) { |
| 963 | // Indirect call. |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 964 | addAddress(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_target), |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 965 | MachineLocation(CallReg)); |
Djordje Todorovic | 0739ccd | 2019-07-12 09:45:12 +0000 | [diff] [blame] | 966 | } else { |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 967 | DIE *CalleeDIE = getOrCreateSubprogramDIE(CalleeSP); |
| 968 | assert(CalleeDIE && "Could not create DIE for call site entry origin"); |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 969 | addDIEEntry(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_origin), |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 970 | *CalleeDIE); |
| 971 | } |
| 972 | |
| 973 | if (IsTail) |
| 974 | // Attach DW_AT_call_tail_call to tail calls for standards compliance. |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 975 | addFlag(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_tail_call)); |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 976 | |
| 977 | // Attach the return PC to allow the debugger to disambiguate call paths |
| 978 | // from one function to another. |
| 979 | if (DD->getDwarfVersion() == 4 && DD->tuneForGDB()) { |
| 980 | assert(PCAddr && "Missing PC information for a call"); |
| 981 | addLabelAddress(CallSiteDIE, dwarf::DW_AT_low_pc, PCAddr); |
| 982 | } else if (!IsTail || DD->tuneForGDB()) { |
Vedant Kumar | 74533bd | 2018-10-22 21:44:21 +0000 | [diff] [blame] | 983 | assert(PCOffset && "Missing return PC information for a call"); |
| 984 | addAddressExpr(CallSiteDIE, dwarf::DW_AT_call_return_pc, PCOffset); |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 985 | } |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 986 | |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 987 | return CallSiteDIE; |
| 988 | } |
| 989 | |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 990 | void DwarfCompileUnit::constructCallSiteParmEntryDIEs( |
| 991 | DIE &CallSiteDIE, SmallVector<DbgCallSiteParam, 4> &Params) { |
| 992 | for (const auto &Param : Params) { |
| 993 | unsigned Register = Param.getRegister(); |
| 994 | auto CallSiteDieParam = |
| 995 | DIE::get(DIEValueAllocator, |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 996 | getDwarf5OrGNUTag(dwarf::DW_TAG_call_site_parameter)); |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 997 | insertDIE(CallSiteDieParam); |
| 998 | addAddress(*CallSiteDieParam, dwarf::DW_AT_location, |
| 999 | MachineLocation(Register)); |
| 1000 | |
| 1001 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
| 1002 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
| 1003 | DwarfExpr.setCallSiteParamValueFlag(); |
| 1004 | |
| 1005 | DwarfDebug::emitDebugLocValue(*Asm, nullptr, Param.getValue(), DwarfExpr); |
| 1006 | |
Vedant Kumar | 58a0714 | 2019-08-26 20:53:34 +0000 | [diff] [blame] | 1007 | addBlock(*CallSiteDieParam, getDwarf5OrGNUAttr(dwarf::DW_AT_call_value), |
Djordje Todorovic | b9973f8 | 2019-07-31 16:51:28 +0000 | [diff] [blame] | 1008 | DwarfExpr.finalize()); |
| 1009 | |
| 1010 | CallSiteDIE.addChild(CallSiteDieParam); |
| 1011 | } |
| 1012 | } |
| 1013 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 1014 | DIE *DwarfCompileUnit::constructImportedEntityDIE( |
| 1015 | const DIImportedEntity *Module) { |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 1016 | DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag()); |
| 1017 | insertDIE(Module, IMDie); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 1018 | DIE *EntityDie; |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 1019 | auto *Entity = Module->getEntity(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1020 | if (auto *NS = dyn_cast<DINamespace>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 1021 | EntityDie = getOrCreateNameSpace(NS); |
Adrian Prantl | 08a388b | 2015-06-30 02:13:04 +0000 | [diff] [blame] | 1022 | else if (auto *M = dyn_cast<DIModule>(Entity)) |
| 1023 | EntityDie = getOrCreateModule(M); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1024 | else if (auto *SP = dyn_cast<DISubprogram>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 1025 | EntityDie = getOrCreateSubprogramDIE(SP); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1026 | else if (auto *T = dyn_cast<DIType>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 1027 | EntityDie = getOrCreateTypeDIE(T); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1028 | else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity)) |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 1029 | EntityDie = getOrCreateGlobalVariableDIE(GV, {}); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 1030 | else |
| 1031 | EntityDie = getDIE(Entity); |
| 1032 | assert(EntityDie); |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 1033 | addSourceLine(*IMDie, Module->getLine(), Module->getFile()); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 1034 | addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie); |
Duncan P. N. Exon Smith | de8e427 | 2015-04-14 01:46:44 +0000 | [diff] [blame] | 1035 | StringRef Name = Module->getName(); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 1036 | if (!Name.empty()) |
| 1037 | addString(*IMDie, dwarf::DW_AT_name, Name); |
| 1038 | |
| 1039 | return IMDie; |
| 1040 | } |
| 1041 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1042 | void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) { |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 1043 | DIE *D = getDIE(SP); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1044 | if (DIE *AbsSPDIE = getAbstractSPDies().lookup(SP)) { |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 1045 | if (D) |
David Blaikie | 4191cbc | 2014-10-10 06:39:29 +0000 | [diff] [blame] | 1046 | // If this subprogram has an abstract definition, reference that |
| 1047 | addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 1048 | } else { |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 1049 | assert(D || includeMinimalInlineScopes()); |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 1050 | if (D) |
David Blaikie | 4191cbc | 2014-10-10 06:39:29 +0000 | [diff] [blame] | 1051 | // And attach the attributes |
| 1052 | applySubprogramAttributesToDefinition(SP, *D); |
| 1053 | } |
| 1054 | } |
| 1055 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1056 | void DwarfCompileUnit::finishEntityDefinition(const DbgEntity *Entity) { |
| 1057 | DbgEntity *AbsEntity = getExistingAbstractEntity(Entity->getEntity()); |
| 1058 | |
| 1059 | auto *Die = Entity->getDIE(); |
| 1060 | /// Label may be used to generate DW_AT_low_pc, so put it outside |
| 1061 | /// if/else block. |
| 1062 | const DbgLabel *Label = nullptr; |
| 1063 | if (AbsEntity && AbsEntity->getDIE()) { |
| 1064 | addDIEEntry(*Die, dwarf::DW_AT_abstract_origin, *AbsEntity->getDIE()); |
| 1065 | Label = dyn_cast<const DbgLabel>(Entity); |
| 1066 | } else { |
| 1067 | if (const DbgVariable *Var = dyn_cast<const DbgVariable>(Entity)) |
| 1068 | applyVariableAttributes(*Var, *Die); |
| 1069 | else if ((Label = dyn_cast<const DbgLabel>(Entity))) |
| 1070 | applyLabelAttributes(*Label, *Die); |
| 1071 | else |
| 1072 | llvm_unreachable("DbgEntity must be DbgVariable or DbgLabel."); |
| 1073 | } |
| 1074 | |
Hsiangkai Wang | 55321d8 | 2018-09-26 04:19:23 +0000 | [diff] [blame] | 1075 | if (Label) |
| 1076 | if (const auto *Sym = Label->getSymbol()) |
| 1077 | addLabelAddress(*Die, dwarf::DW_AT_low_pc, Sym); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1080 | DbgEntity *DwarfCompileUnit::getExistingAbstractEntity(const DINode *Node) { |
| 1081 | auto &AbstractEntities = getAbstractEntities(); |
| 1082 | auto I = AbstractEntities.find(Node); |
| 1083 | if (I != AbstractEntities.end()) |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1084 | return I->second.get(); |
| 1085 | return nullptr; |
| 1086 | } |
| 1087 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1088 | void DwarfCompileUnit::createAbstractEntity(const DINode *Node, |
| 1089 | LexicalScope *Scope) { |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1090 | assert(Scope && Scope->isAbstractScope()); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1091 | auto &Entity = getAbstractEntities()[Node]; |
| 1092 | if (isa<const DILocalVariable>(Node)) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 1093 | Entity = std::make_unique<DbgVariable>( |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1094 | cast<const DILocalVariable>(Node), nullptr /* IA */);; |
| 1095 | DU->addScopeVariable(Scope, cast<DbgVariable>(Entity.get())); |
| 1096 | } else if (isa<const DILabel>(Node)) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 1097 | Entity = std::make_unique<DbgLabel>( |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1098 | cast<const DILabel>(Node), nullptr /* IA */); |
| 1099 | DU->addScopeLabel(Scope, cast<DbgLabel>(Entity.get())); |
| 1100 | } |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 1103 | void DwarfCompileUnit::emitHeader(bool UseOffsets) { |
David Blaikie | b6726a9 | 2014-11-02 02:26:24 +0000 | [diff] [blame] | 1104 | // Don't bother labeling the .dwo unit, as its offset isn't used. |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 1105 | if (!Skeleton && !DD->useSectionsAsReferences()) { |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 1106 | LabelBegin = Asm->createTempSymbol("cu_begin"); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1107 | Asm->OutStreamer->EmitLabel(LabelBegin); |
Rafael Espindola | 3c31114 | 2015-03-10 03:11:11 +0000 | [diff] [blame] | 1108 | } |
David Blaikie | ae57e66 | 2014-11-01 23:59:23 +0000 | [diff] [blame] | 1109 | |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 1110 | dwarf::UnitType UT = Skeleton ? dwarf::DW_UT_split_compile |
| 1111 | : DD->useSplitDwarf() ? dwarf::DW_UT_skeleton |
| 1112 | : dwarf::DW_UT_compile; |
| 1113 | DwarfUnit::emitCommonHeader(UseOffsets, UT); |
Paul Robinson | 543c0e1 | 2018-05-22 17:27:31 +0000 | [diff] [blame] | 1114 | if (DD->getDwarfVersion() >= 5 && UT != dwarf::DW_UT_compile) |
| 1115 | Asm->emitInt64(getDWOId()); |
David Blaikie | ae57e66 | 2014-11-01 23:59:23 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1118 | bool DwarfCompileUnit::hasDwarfPubSections() const { |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 1119 | switch (CUNode->getNameTableKind()) { |
| 1120 | case DICompileUnit::DebugNameTableKind::None: |
| 1121 | return false; |
| 1122 | // Opting in to GNU Pubnames/types overrides the default to ensure these are |
| 1123 | // generated for things like Gold's gdb_index generation. |
| 1124 | case DICompileUnit::DebugNameTableKind::GNU: |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1125 | return true; |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 1126 | case DICompileUnit::DebugNameTableKind::Default: |
David Blaikie | 0e03047 | 2018-08-16 23:57:15 +0000 | [diff] [blame] | 1127 | return DD->tuneForGDB() && !includeMinimalInlineScopes() && |
David Blaikie | 2f51176 | 2019-04-23 19:00:45 +0000 | [diff] [blame] | 1128 | !CUNode->isDebugDirectivesOnly() && |
| 1129 | DD->getAccelTableKind() != AccelTableKind::Apple && |
| 1130 | DD->getDwarfVersion() < 5; |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 1131 | } |
Simon Pilgrim | 22d580f2 | 2018-08-17 09:47:52 +0000 | [diff] [blame] | 1132 | llvm_unreachable("Unhandled DICompileUnit::DebugNameTableKind enum"); |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1135 | /// addGlobalName - Add a new global name to the compile unit. |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1136 | void DwarfCompileUnit::addGlobalName(StringRef Name, const DIE &Die, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1137 | const DIScope *Context) { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1138 | if (!hasDwarfPubSections()) |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1139 | return; |
| 1140 | std::string FullName = getParentContextString(Context) + Name.str(); |
| 1141 | GlobalNames[FullName] = &Die; |
| 1142 | } |
| 1143 | |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1144 | void DwarfCompileUnit::addGlobalNameForTypeUnit(StringRef Name, |
| 1145 | const DIScope *Context) { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1146 | if (!hasDwarfPubSections()) |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1147 | return; |
| 1148 | std::string FullName = getParentContextString(Context) + Name.str(); |
| 1149 | // Insert, allowing the entry to remain as-is if it's already present |
| 1150 | // This way the CU-level type DIE is preferred over the "can't describe this |
| 1151 | // type as a unit offset because it's not really in the CU at all, it's only |
| 1152 | // in a type unit" |
| 1153 | GlobalNames.insert(std::make_pair(std::move(FullName), &getUnitDie())); |
| 1154 | } |
| 1155 | |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1156 | /// Add a new global type to the unit. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1157 | void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die, |
| 1158 | const DIScope *Context) { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1159 | if (!hasDwarfPubSections()) |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1160 | return; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 1161 | std::string FullName = getParentContextString(Context) + Ty->getName().str(); |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1162 | GlobalTypes[FullName] = &Die; |
| 1163 | } |
David Blaikie | ae57e66 | 2014-11-01 23:59:23 +0000 | [diff] [blame] | 1164 | |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1165 | void DwarfCompileUnit::addGlobalTypeUnitType(const DIType *Ty, |
| 1166 | const DIScope *Context) { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1167 | if (!hasDwarfPubSections()) |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1168 | return; |
| 1169 | std::string FullName = getParentContextString(Context) + Ty->getName().str(); |
| 1170 | // Insert, allowing the entry to remain as-is if it's already present |
| 1171 | // This way the CU-level type DIE is preferred over the "can't describe this |
| 1172 | // type as a unit offset because it's not really in the CU at all, it's only |
| 1173 | // in a type unit" |
| 1174 | GlobalTypes.insert(std::make_pair(std::move(FullName), &getUnitDie())); |
| 1175 | } |
| 1176 | |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 1177 | void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, |
| 1178 | MachineLocation Location) { |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1179 | if (DV.hasComplexAddress()) |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 1180 | addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 1181 | else |
Adrian Prantl | 5883af3 | 2015-01-19 17:57:29 +0000 | [diff] [blame] | 1182 | addAddress(Die, dwarf::DW_AT_location, Location); |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 1183 | } |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 1184 | |
| 1185 | /// Add an address attribute to a die based on the location provided. |
| 1186 | void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, |
Adrian Prantl | 5883af3 | 2015-01-19 17:57:29 +0000 | [diff] [blame] | 1187 | const MachineLocation &Location) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 1188 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1189 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1190 | if (Location.isIndirect()) |
| 1191 | DwarfExpr.setMemoryLocationKind(); |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 1192 | |
Adrian Prantl | 2049c0d | 2017-08-02 15:22:17 +0000 | [diff] [blame] | 1193 | DIExpressionCursor Cursor({}); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1194 | const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1195 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) |
Adrian Prantl | ab255fc | 2014-12-05 01:02:46 +0000 | [diff] [blame] | 1196 | return; |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1197 | DwarfExpr.addExpression(std::move(Cursor)); |
Adrian Prantl | ab255fc | 2014-12-05 01:02:46 +0000 | [diff] [blame] | 1198 | |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 1199 | // Now attach the location information to the DIE. |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1200 | addBlock(Die, Attribute, DwarfExpr.finalize()); |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 1201 | } |
David Blaikie | 77895fb | 2014-11-02 06:58:44 +0000 | [diff] [blame] | 1202 | |
| 1203 | /// Start with the address based on the location provided, and generate the |
| 1204 | /// DWARF information necessary to find the actual variable given the extra |
| 1205 | /// address information encoded in the DbgVariable, starting from the starting |
| 1206 | /// location. Add the DWARF information to the die. |
| 1207 | void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, |
| 1208 | dwarf::Attribute Attribute, |
| 1209 | const MachineLocation &Location) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 1210 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
Adrian Prantl | 7813d9c | 2015-01-14 01:01:30 +0000 | [diff] [blame] | 1211 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1212 | const DIExpression *DIExpr = DV.getSingleExpression(); |
| 1213 | DwarfExpr.addFragmentOffset(DIExpr); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1214 | if (Location.isIndirect()) |
| 1215 | DwarfExpr.setMemoryLocationKind(); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1216 | |
Adrian Prantl | 2049c0d | 2017-08-02 15:22:17 +0000 | [diff] [blame] | 1217 | DIExpressionCursor Cursor(DIExpr); |
Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 1218 | |
| 1219 | if (DIExpr->isEntryValue()) { |
| 1220 | DwarfExpr.setEntryValueFlag(); |
David Stenberg | 1ae2d9a | 2019-10-15 11:31:21 +0000 | [diff] [blame] | 1221 | DwarfExpr.beginEntryValueExpression(Cursor); |
Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 1224 | const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1225 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 1226 | return; |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1227 | DwarfExpr.addExpression(std::move(Cursor)); |
David Blaikie | 77895fb | 2014-11-02 06:58:44 +0000 | [diff] [blame] | 1228 | |
| 1229 | // Now attach the location information to the DIE. |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1230 | addBlock(Die, Attribute, DwarfExpr.finalize()); |
David Blaikie | 77895fb | 2014-11-02 06:58:44 +0000 | [diff] [blame] | 1231 | } |
David Blaikie | 4bc0881 | 2014-11-02 07:03:19 +0000 | [diff] [blame] | 1232 | |
| 1233 | /// Add a Dwarf loclistptr attribute data and value. |
| 1234 | void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, |
| 1235 | unsigned Index) { |
David Blaikie | 77cfcd7 | 2019-11-12 14:15:37 -0800 | [diff] [blame] | 1236 | dwarf::Form Form = dwarf::DW_FORM_data4; |
| 1237 | if (DD->getDwarfVersion() == 4) |
| 1238 | Form =dwarf::DW_FORM_sec_offset; |
| 1239 | if (DD->getDwarfVersion() >= 5) |
| 1240 | Form =dwarf::DW_FORM_loclistx; |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 1241 | Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index)); |
David Blaikie | 4bc0881 | 2014-11-02 07:03:19 +0000 | [diff] [blame] | 1242 | } |
David Blaikie | 02a6333 | 2014-11-02 07:06:51 +0000 | [diff] [blame] | 1243 | |
David Blaikie | 8c485b5 | 2014-11-02 07:08:12 +0000 | [diff] [blame] | 1244 | void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var, |
| 1245 | DIE &VariableDie) { |
David Blaikie | 02a6333 | 2014-11-02 07:06:51 +0000 | [diff] [blame] | 1246 | StringRef Name = Var.getName(); |
| 1247 | if (!Name.empty()) |
| 1248 | addString(VariableDie, dwarf::DW_AT_name, Name); |
Victor Leschuk | 3c98998 | 2016-10-26 11:59:03 +0000 | [diff] [blame] | 1249 | const auto *DIVar = Var.getVariable(); |
| 1250 | if (DIVar) |
| 1251 | if (uint32_t AlignInBytes = DIVar->getAlignInBytes()) |
| 1252 | addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, |
| 1253 | AlignInBytes); |
| 1254 | |
| 1255 | addSourceLine(VariableDie, DIVar); |
David Blaikie | 02a6333 | 2014-11-02 07:06:51 +0000 | [diff] [blame] | 1256 | addType(VariableDie, Var.getType()); |
| 1257 | if (Var.isArtificial()) |
| 1258 | addFlag(VariableDie, dwarf::DW_AT_artificial); |
| 1259 | } |
David Blaikie | 9780208 | 2014-11-02 07:11:55 +0000 | [diff] [blame] | 1260 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1261 | void DwarfCompileUnit::applyLabelAttributes(const DbgLabel &Label, |
| 1262 | DIE &LabelDie) { |
| 1263 | StringRef Name = Label.getName(); |
| 1264 | if (!Name.empty()) |
| 1265 | addString(LabelDie, dwarf::DW_AT_name, Name); |
| 1266 | const auto *DILabel = Label.getLabel(); |
| 1267 | addSourceLine(LabelDie, DILabel); |
| 1268 | } |
| 1269 | |
David Blaikie | 9780208 | 2014-11-02 07:11:55 +0000 | [diff] [blame] | 1270 | /// Add a Dwarf expression attribute data and value. |
| 1271 | void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form, |
| 1272 | const MCExpr *Expr) { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 1273 | Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr)); |
David Blaikie | 9780208 | 2014-11-02 07:11:55 +0000 | [diff] [blame] | 1274 | } |
David Blaikie | 3363a57 | 2014-11-02 08:09:09 +0000 | [diff] [blame] | 1275 | |
Vedant Kumar | 74533bd | 2018-10-22 21:44:21 +0000 | [diff] [blame] | 1276 | void DwarfCompileUnit::addAddressExpr(DIE &Die, dwarf::Attribute Attribute, |
| 1277 | const MCExpr *Expr) { |
| 1278 | Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, |
| 1279 | DIEExpr(Expr)); |
| 1280 | } |
| 1281 | |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 1282 | void DwarfCompileUnit::applySubprogramAttributesToDefinition( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1283 | const DISubprogram *SP, DIE &SPDie) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 1284 | auto *SPDecl = SP->getDeclaration(); |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 1285 | auto *Context = SPDecl ? SPDecl->getScope() : SP->getScope(); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1286 | applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes()); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 1287 | addGlobalName(SP->getName(), SPDie, Context); |
David Blaikie | 3363a57 | 2014-11-02 08:09:09 +0000 | [diff] [blame] | 1288 | } |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 1289 | |
| 1290 | bool DwarfCompileUnit::isDwoUnit() const { |
| 1291 | return DD->useSplitDwarf() && Skeleton; |
| 1292 | } |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1293 | |
David Blaikie | 832c7d9 | 2019-04-24 18:09:44 +0000 | [diff] [blame] | 1294 | void DwarfCompileUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) { |
| 1295 | constructTypeDIE(D, CTy); |
| 1296 | } |
| 1297 | |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1298 | bool DwarfCompileUnit::includeMinimalInlineScopes() const { |
Adrian Prantl | b939a25 | 2016-03-31 23:56:58 +0000 | [diff] [blame] | 1299 | return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly || |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1300 | (DD->useSplitDwarf() && !Skeleton); |
| 1301 | } |
David Blaikie | 61c127c | 2018-12-14 22:34:03 +0000 | [diff] [blame] | 1302 | |
| 1303 | void DwarfCompileUnit::addAddrTableBase() { |
| 1304 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 1305 | MCSymbol *Label = DD->getAddressPool().getLabel(); |
| 1306 | addSectionLabel(getUnitDie(), |
| 1307 | getDwarfVersion() >= 5 ? dwarf::DW_AT_addr_base |
| 1308 | : dwarf::DW_AT_GNU_addr_base, |
| 1309 | Label, TLOF.getDwarfAddrSection()->getBeginSymbol()); |
| 1310 | } |
Markus Lavin | b86ce21 | 2019-03-19 13:16:28 +0000 | [diff] [blame] | 1311 | |
| 1312 | void DwarfCompileUnit::addBaseTypeRef(DIEValueList &Die, int64_t Idx) { |
| 1313 | Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, dwarf::DW_FORM_udata, |
| 1314 | new (DIEValueAllocator) DIEBaseTypeRef(this, Idx)); |
| 1315 | } |
| 1316 | |
| 1317 | void DwarfCompileUnit::createBaseTypeDIEs() { |
| 1318 | // Insert the base_type DIEs directly after the CU so that their offsets will |
| 1319 | // fit in the fixed size ULEB128 used inside the location expressions. |
| 1320 | // Maintain order by iterating backwards and inserting to the front of CU |
| 1321 | // child list. |
| 1322 | for (auto &Btr : reverse(ExprRefedBaseTypes)) { |
| 1323 | DIE &Die = getUnitDie().addChildFront( |
| 1324 | DIE::get(DIEValueAllocator, dwarf::DW_TAG_base_type)); |
| 1325 | SmallString<32> Str; |
| 1326 | addString(Die, dwarf::DW_AT_name, |
| 1327 | Twine(dwarf::AttributeEncodingString(Btr.Encoding) + |
| 1328 | "_" + Twine(Btr.BitSize)).toStringRef(Str)); |
| 1329 | addUInt(Die, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Btr.Encoding); |
| 1330 | addUInt(Die, dwarf::DW_AT_byte_size, None, Btr.BitSize / 8); |
| 1331 | |
| 1332 | Btr.Die = &Die; |
| 1333 | } |
| 1334 | } |