David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 1 | #include "DwarfCompileUnit.h" |
| 2 | |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 3 | #include "llvm/CodeGen/MachineFunction.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 4 | #include "llvm/IR/DataLayout.h" |
| 5 | #include "llvm/IR/GlobalValue.h" |
| 6 | #include "llvm/IR/GlobalVariable.h" |
| 7 | #include "llvm/IR/Instruction.h" |
| 8 | #include "llvm/MC/MCAsmInfo.h" |
| 9 | #include "llvm/MC/MCStreamer.h" |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 10 | #include "llvm/Target/TargetFrameLowering.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 11 | #include "llvm/Target/TargetLoweringObjectFile.h" |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 12 | #include "llvm/Target/TargetMachine.h" |
| 13 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 14 | #include "llvm/Target/TargetRegisterInfo.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 15 | |
| 16 | namespace llvm { |
| 17 | |
| 18 | DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node, |
| 19 | AsmPrinter *A, DwarfDebug *DW, |
| 20 | DwarfFile *DWU) |
| 21 | : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) { |
| 22 | insertDIE(Node, &getUnitDie()); |
| 23 | } |
| 24 | |
| 25 | /// addLabelAddress - Add a dwarf label attribute data and value using |
| 26 | /// DW_FORM_addr or DW_FORM_GNU_addr_index. |
| 27 | /// |
| 28 | void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, |
| 29 | const MCSymbol *Label) { |
| 30 | |
| 31 | // Don't use the address pool in non-fission or in the skeleton unit itself. |
| 32 | // FIXME: Once GDB supports this, it's probably worthwhile using the address |
| 33 | // pool from the skeleton - maybe even in non-fission (possibly fewer |
| 34 | // relocations by sharing them in the pool, but we have other ideas about how |
| 35 | // to reduce the number of relocations as well/instead). |
| 36 | if (!DD->useSplitDwarf() || !Skeleton) |
| 37 | return addLocalLabelAddress(Die, Attribute, Label); |
| 38 | |
| 39 | if (Label) |
| 40 | DD->addArangeLabel(SymbolCU(this, Label)); |
| 41 | |
| 42 | unsigned idx = DD->getAddressPool().getIndex(Label); |
| 43 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); |
| 44 | Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value); |
| 45 | } |
| 46 | |
| 47 | void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, |
| 48 | dwarf::Attribute Attribute, |
| 49 | const MCSymbol *Label) { |
| 50 | if (Label) |
| 51 | DD->addArangeLabel(SymbolCU(this, Label)); |
| 52 | |
| 53 | Die.addValue(Attribute, dwarf::DW_FORM_addr, |
| 54 | Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label) |
| 55 | : new (DIEValueAllocator) DIEInteger(0)); |
| 56 | } |
| 57 | |
David Blaikie | 8945219 | 2014-10-04 16:00:26 +0000 | [diff] [blame] | 58 | unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, |
| 59 | StringRef DirName) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 60 | // If we print assembly, we can't separate .file entries according to |
| 61 | // compile units. Thus all files will belong to the default compile unit. |
| 62 | |
| 63 | // FIXME: add a better feature test than hasRawTextSupport. Even better, |
| 64 | // extend .file to support this. |
| 65 | return Asm->OutStreamer.EmitDwarfFileDirective( |
| 66 | 0, DirName, FileName, |
| 67 | Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID()); |
| 68 | } |
| 69 | |
| 70 | // Return const expression if value is a GEP to access merged global |
| 71 | // constant. e.g. |
| 72 | // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) |
| 73 | static const ConstantExpr *getMergedGlobalExpr(const Value *V) { |
| 74 | const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); |
| 75 | if (!CE || CE->getNumOperands() != 3 || |
| 76 | CE->getOpcode() != Instruction::GetElementPtr) |
| 77 | return nullptr; |
| 78 | |
| 79 | // First operand points to a global struct. |
| 80 | Value *Ptr = CE->getOperand(0); |
| 81 | if (!isa<GlobalValue>(Ptr) || |
| 82 | !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) |
| 83 | return nullptr; |
| 84 | |
| 85 | // Second operand is zero. |
| 86 | const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); |
| 87 | if (!CI || !CI->isZero()) |
| 88 | return nullptr; |
| 89 | |
| 90 | // Third operand is offset. |
| 91 | if (!isa<ConstantInt>(CE->getOperand(2))) |
| 92 | return nullptr; |
| 93 | |
| 94 | return CE; |
| 95 | } |
| 96 | |
| 97 | /// getOrCreateGlobalVariableDIE - get or create global variable DIE. |
| 98 | DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) { |
| 99 | // Check for pre-existence. |
| 100 | if (DIE *Die = getDIE(GV)) |
| 101 | return Die; |
| 102 | |
| 103 | assert(GV.isGlobalVariable()); |
| 104 | |
| 105 | DIScope GVContext = GV.getContext(); |
| 106 | DIType GTy = DD->resolve(GV.getType()); |
| 107 | |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 108 | // Construct the context before querying for the existence of the DIE in |
| 109 | // case such construction creates the DIE. |
| 110 | DIE *ContextDIE = getOrCreateContextDIE(GVContext); |
| 111 | |
| 112 | // Add to map. |
| 113 | DIE *VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV); |
| 114 | DIScope DeclContext; |
| 115 | |
| 116 | if (DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration()) { |
| 117 | DeclContext = resolve(SDMDecl.getContext()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 118 | assert(SDMDecl.isStaticMember() && "Expected static member decl"); |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 119 | assert(GV.isDefinition()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 120 | // 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] | 121 | DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); |
| 122 | addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); |
| 123 | } else { |
| 124 | DeclContext = GV.getContext(); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 125 | // Add name and type. |
| 126 | addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); |
| 127 | addType(*VariableDIE, GTy); |
| 128 | |
| 129 | // Add scoping info. |
| 130 | if (!GV.isLocalToUnit()) |
| 131 | addFlag(*VariableDIE, dwarf::DW_AT_external); |
| 132 | |
| 133 | // Add line number info. |
| 134 | addSourceLine(*VariableDIE, GV); |
| 135 | } |
| 136 | |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 137 | if (!GV.isDefinition()) |
| 138 | addFlag(*VariableDIE, dwarf::DW_AT_declaration); |
| 139 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 140 | // Add location. |
| 141 | bool addToAccelTable = false; |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 142 | bool isGlobalVariable = GV.getGlobal() != nullptr; |
| 143 | if (isGlobalVariable) { |
| 144 | addToAccelTable = true; |
| 145 | DIELoc *Loc = new (DIEValueAllocator) DIELoc(); |
| 146 | const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal()); |
| 147 | if (GV.getGlobal()->isThreadLocal()) { |
| 148 | // FIXME: Make this work with -gsplit-dwarf. |
| 149 | unsigned PointerSize = Asm->getDataLayout().getPointerSize(); |
| 150 | assert((PointerSize == 4 || PointerSize == 8) && |
| 151 | "Add support for other sizes if necessary"); |
| 152 | // Based on GCC's support for TLS: |
| 153 | if (!DD->useSplitDwarf()) { |
| 154 | // 1) Start with a constNu of the appropriate pointer size |
| 155 | addUInt(*Loc, dwarf::DW_FORM_data1, |
| 156 | PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u); |
| 157 | // 2) containing the (relocated) offset of the TLS variable |
| 158 | // within the module's TLS block. |
| 159 | addExpr(*Loc, dwarf::DW_FORM_udata, |
| 160 | Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); |
| 161 | } else { |
| 162 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); |
| 163 | addUInt(*Loc, dwarf::DW_FORM_udata, |
| 164 | DD->getAddressPool().getIndex(Sym, /* TLS */ true)); |
| 165 | } |
| 166 | // 3) followed by a custom OP to make the debugger do a TLS lookup. |
| 167 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address); |
| 168 | } else { |
| 169 | DD->addArangeLabel(SymbolCU(this, Sym)); |
| 170 | addOpAddress(*Loc, Sym); |
| 171 | } |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 172 | |
| 173 | addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 174 | // Add the linkage name. |
| 175 | StringRef LinkageName = GV.getLinkageName(); |
| 176 | if (!LinkageName.empty()) |
| 177 | // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: |
| 178 | // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and |
| 179 | // TAG_variable. |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 180 | addString(*VariableDIE, |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 181 | DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name |
| 182 | : dwarf::DW_AT_MIPS_linkage_name, |
| 183 | GlobalValue::getRealLinkageName(LinkageName)); |
| 184 | } else if (const ConstantInt *CI = |
| 185 | dyn_cast_or_null<ConstantInt>(GV.getConstant())) { |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 186 | addConstantValue(*VariableDIE, CI, GTy); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 187 | } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV.getConstant())) { |
| 188 | addToAccelTable = true; |
| 189 | // GV is a merged global. |
| 190 | DIELoc *Loc = new (DIEValueAllocator) DIELoc(); |
| 191 | Value *Ptr = CE->getOperand(0); |
| 192 | MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr)); |
| 193 | DD->addArangeLabel(SymbolCU(this, Sym)); |
| 194 | addOpAddress(*Loc, Sym); |
| 195 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
| 196 | SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end()); |
| 197 | addUInt(*Loc, dwarf::DW_FORM_udata, |
| 198 | Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); |
| 199 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); |
| 200 | addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); |
| 201 | } |
| 202 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 203 | if (addToAccelTable) { |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 204 | DD->addAccelName(GV.getName(), *VariableDIE); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 205 | |
| 206 | // If the linkage name is different than the name, go ahead and output |
| 207 | // that as well into the name table. |
| 208 | if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 209 | DD->addAccelName(GV.getLinkageName(), *VariableDIE); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 210 | } |
| 211 | |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 212 | addGlobalName(GV.getName(), *VariableDIE, DeclContext); |
| 213 | return VariableDIE; |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void DwarfCompileUnit::addRange(RangeSpan Range) { |
| 217 | bool SameAsPrevCU = this == DD->getPrevCU(); |
| 218 | DD->setPrevCU(this); |
| 219 | // If we have no current ranges just add the range and return, otherwise, |
| 220 | // check the current section and CU against the previous section and CU we |
| 221 | // emitted into and the subprogram was contained within. If these are the |
| 222 | // same then extend our current range, otherwise add this as a new range. |
| 223 | if (CURanges.empty() || !SameAsPrevCU || |
| 224 | (&CURanges.back().getEnd()->getSection() != |
| 225 | &Range.getEnd()->getSection())) { |
| 226 | CURanges.push_back(Range); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | CURanges.back().setEnd(Range.getEnd()); |
| 231 | } |
| 232 | |
David Blaikie | 6c0ee4e | 2014-10-08 22:46:27 +0000 | [diff] [blame] | 233 | void DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, |
| 234 | const MCSymbol *Label, |
| 235 | const MCSymbol *Sec) { |
| 236 | if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) |
| 237 | addLabel(Die, Attribute, |
| 238 | DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset |
| 239 | : dwarf::DW_FORM_data4, |
| 240 | Label); |
| 241 | else |
| 242 | addSectionDelta(Die, Attribute, Label, Sec); |
| 243 | } |
| 244 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 245 | void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) { |
| 246 | // Define start line table label for each Compile Unit. |
| 247 | MCSymbol *LineTableStartSym = |
| 248 | Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID()); |
| 249 | |
| 250 | stmtListIndex = UnitDie.getValues().size(); |
| 251 | |
| 252 | // DW_AT_stmt_list is a offset of line number information for this |
| 253 | // compile unit in debug_line section. For split dwarf this is |
| 254 | // left in the skeleton CU and so not included. |
| 255 | // The line table entries are not always emitted in assembly, so it |
| 256 | // is not okay to use line_table_start here. |
David Blaikie | 6c0ee4e | 2014-10-08 22:46:27 +0000 | [diff] [blame] | 257 | addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym, |
| 258 | DwarfLineSectionSym); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void DwarfCompileUnit::applyStmtList(DIE &D) { |
| 262 | D.addValue(dwarf::DW_AT_stmt_list, |
| 263 | UnitDie.getAbbrev().getData()[stmtListIndex].getForm(), |
| 264 | UnitDie.getValues()[stmtListIndex]); |
| 265 | } |
| 266 | |
David Blaikie | 14499a7 | 2014-10-04 15:58:47 +0000 | [diff] [blame] | 267 | void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, |
| 268 | const MCSymbol *End) { |
| 269 | assert(Begin && "Begin label should not be null!"); |
| 270 | assert(End && "End label should not be null!"); |
| 271 | assert(Begin->isDefined() && "Invalid starting label"); |
| 272 | assert(End->isDefined() && "Invalid end label"); |
| 273 | |
| 274 | addLabelAddress(D, dwarf::DW_AT_low_pc, Begin); |
| 275 | if (DD->getDwarfVersion() < 4) |
| 276 | addLabelAddress(D, dwarf::DW_AT_high_pc, End); |
| 277 | else |
| 278 | addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin); |
| 279 | } |
| 280 | |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 281 | // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc |
| 282 | // and DW_AT_high_pc attributes. If there are global variables in this |
| 283 | // scope then create and insert DIEs for these variables. |
| 284 | DIE &DwarfCompileUnit::updateSubprogramScopeDIE(DISubprogram SP) { |
| 285 | DIE *SPDie = getOrCreateSubprogramDIE(SP); |
| 286 | |
| 287 | attachLowHighPC(*SPDie, DD->getFunctionBeginSym(), DD->getFunctionEndSym()); |
| 288 | if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim( |
| 289 | *DD->getCurrentFunction())) |
| 290 | addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr); |
| 291 | |
| 292 | // Only include DW_AT_frame_base in full debug info |
| 293 | if (getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) { |
| 294 | const TargetRegisterInfo *RI = |
| 295 | Asm->TM.getSubtargetImpl()->getRegisterInfo(); |
| 296 | MachineLocation Location(RI->getFrameRegister(*Asm->MF)); |
| 297 | addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); |
| 298 | } |
| 299 | |
| 300 | // Add name to the name table, we do this here because we're guaranteed |
| 301 | // to have concrete versions of our DW_TAG_subprogram nodes. |
| 302 | DD->addSubprogramNames(SP, *SPDie); |
| 303 | |
| 304 | return *SPDie; |
| 305 | } |
| 306 | |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 307 | // Construct a DIE for this scope. |
| 308 | void DwarfCompileUnit::constructScopeDIE( |
| 309 | LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &FinalChildren) { |
| 310 | if (!Scope || !Scope->getScopeNode()) |
| 311 | return; |
| 312 | |
| 313 | DIScope DS(Scope->getScopeNode()); |
| 314 | |
| 315 | assert((Scope->getInlinedAt() || !DS.isSubprogram()) && |
| 316 | "Only handle inlined subprograms here, use " |
| 317 | "constructSubprogramScopeDIE for non-inlined " |
| 318 | "subprograms"); |
| 319 | |
| 320 | SmallVector<std::unique_ptr<DIE>, 8> Children; |
| 321 | |
| 322 | // We try to create the scope DIE first, then the children DIEs. This will |
| 323 | // avoid creating un-used children then removing them later when we find out |
| 324 | // the scope DIE is null. |
| 325 | std::unique_ptr<DIE> ScopeDIE; |
| 326 | if (Scope->getParent() && DS.isSubprogram()) { |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 327 | ScopeDIE = constructInlinedScopeDIE(Scope); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 328 | if (!ScopeDIE) |
| 329 | return; |
| 330 | // We create children when the scope DIE is not null. |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 331 | createScopeChildrenDIE(Scope, Children); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 332 | } else { |
| 333 | // Early exit when we know the scope DIE is going to be null. |
| 334 | if (DD->isLexicalScopeDIENull(Scope)) |
| 335 | return; |
| 336 | |
| 337 | unsigned ChildScopeCount; |
| 338 | |
| 339 | // We create children here when we know the scope DIE is not going to be |
| 340 | // null and the children will be added to the scope DIE. |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 341 | createScopeChildrenDIE(Scope, Children, &ChildScopeCount); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 342 | |
| 343 | // There is no need to emit empty lexical block DIE. |
| 344 | for (const auto &E : DD->findImportedEntitiesForScope(DS)) |
| 345 | Children.push_back( |
| 346 | constructImportedEntityDIE(DIImportedEntity(E.second))); |
| 347 | // If there are only other scopes as children, put them directly in the |
| 348 | // parent instead, as this scope would serve no purpose. |
| 349 | if (Children.size() == ChildScopeCount) { |
| 350 | FinalChildren.insert(FinalChildren.end(), |
| 351 | std::make_move_iterator(Children.begin()), |
| 352 | std::make_move_iterator(Children.end())); |
| 353 | return; |
| 354 | } |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 355 | ScopeDIE = constructLexicalScopeDIE(Scope); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 356 | assert(ScopeDIE && "Scope DIE should not be null."); |
| 357 | } |
| 358 | |
| 359 | // Add children |
| 360 | for (auto &I : Children) |
| 361 | ScopeDIE->addChild(std::move(I)); |
| 362 | |
| 363 | FinalChildren.push_back(std::move(ScopeDIE)); |
| 364 | } |
| 365 | |
David Blaikie | e5feec5 | 2014-10-08 23:30:05 +0000 | [diff] [blame] | 366 | void DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, |
| 367 | const MCSymbol *Hi, const MCSymbol *Lo) { |
| 368 | DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); |
| 369 | Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset |
| 370 | : dwarf::DW_FORM_data4, |
| 371 | Value); |
| 372 | } |
| 373 | |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 374 | void |
| 375 | DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, |
| 376 | const SmallVectorImpl<InsnRange> &Range) { |
| 377 | // Emit offset in .debug_range as a relocatable label. emitDIE will handle |
| 378 | // emitting it appropriately. |
| 379 | MCSymbol *RangeSym = |
| 380 | Asm->GetTempSymbol("debug_ranges", DD->getNextRangeNumber()); |
| 381 | |
| 382 | auto *RangeSectionSym = DD->getRangeSectionSym(); |
| 383 | |
| 384 | // Under fission, ranges are specified by constant offsets relative to the |
| 385 | // CU's DW_AT_GNU_ranges_base. |
| 386 | if (DD->useSplitDwarf()) |
| 387 | addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, RangeSym, RangeSectionSym); |
| 388 | else |
| 389 | addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, RangeSym, RangeSectionSym); |
| 390 | |
| 391 | RangeSpanList List(RangeSym); |
| 392 | for (const InsnRange &R : Range) |
| 393 | List.addRange(RangeSpan(DD->getLabelBeforeInsn(R.first), |
| 394 | DD->getLabelAfterInsn(R.second))); |
| 395 | |
| 396 | // Add the range list to the set of ranges to be emitted. |
| 397 | addRangeList(std::move(List)); |
| 398 | } |
| 399 | |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 400 | void DwarfCompileUnit::attachRangesOrLowHighPC( |
| 401 | DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { |
| 402 | assert(!Ranges.empty()); |
| 403 | if (Ranges.size() == 1) |
| 404 | attachLowHighPC(Die, DD->getLabelBeforeInsn(Ranges.front().first), |
| 405 | DD->getLabelAfterInsn(Ranges.front().second)); |
| 406 | else |
| 407 | addScopeRangeList(Die, Ranges); |
| 408 | } |
| 409 | |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 410 | // This scope represents inlined body of a function. Construct DIE to |
| 411 | // represent this concrete inlined copy of the function. |
| 412 | std::unique_ptr<DIE> |
| 413 | DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { |
| 414 | assert(Scope->getScopeNode()); |
| 415 | DIScope DS(Scope->getScopeNode()); |
| 416 | DISubprogram InlinedSP = getDISubprogram(DS); |
| 417 | // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram |
| 418 | // was inlined from another compile unit. |
| 419 | DIE *OriginDIE = DD->getAbstractSPDies()[InlinedSP]; |
| 420 | assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); |
| 421 | |
| 422 | auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_inlined_subroutine); |
| 423 | addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); |
| 424 | |
| 425 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); |
| 426 | |
| 427 | // Add the call site information to the DIE. |
| 428 | DILocation DL(Scope->getInlinedAt()); |
| 429 | addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, |
David Blaikie | a09bd0a | 2014-10-09 17:08:38 +0000 | [diff] [blame] | 430 | getOrCreateSourceID(DL.getFilename(), DL.getDirectory())); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 431 | addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber()); |
| 432 | |
| 433 | // Add name to the name table, we do this here because we're guaranteed |
| 434 | // to have concrete versions of our DW_TAG_inlined_subprogram nodes. |
| 435 | DD->addSubprogramNames(InlinedSP, *ScopeDIE); |
| 436 | |
| 437 | return ScopeDIE; |
| 438 | } |
| 439 | |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 440 | // Construct new DW_TAG_lexical_block for this scope and attach |
| 441 | // DW_AT_low_pc/DW_AT_high_pc labels. |
| 442 | std::unique_ptr<DIE> |
| 443 | DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { |
| 444 | if (DD->isLexicalScopeDIENull(Scope)) |
| 445 | return nullptr; |
| 446 | |
| 447 | auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_lexical_block); |
| 448 | if (Scope->isAbstractScope()) |
| 449 | return ScopeDIE; |
| 450 | |
| 451 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); |
| 452 | |
| 453 | return ScopeDIE; |
| 454 | } |
| 455 | |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 456 | /// constructVariableDIE - Construct a DIE for the given DbgVariable. |
| 457 | std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, |
| 458 | bool Abstract) { |
| 459 | auto D = constructVariableDIEImpl(DV, Abstract); |
| 460 | DV.setDIE(*D); |
| 461 | return D; |
| 462 | } |
| 463 | |
| 464 | std::unique_ptr<DIE> |
| 465 | DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, |
| 466 | bool Abstract) { |
| 467 | // Define variable debug information entry. |
| 468 | auto VariableDie = make_unique<DIE>(DV.getTag()); |
| 469 | |
| 470 | if (Abstract) { |
| 471 | applyVariableAttributes(DV, *VariableDie); |
| 472 | return VariableDie; |
| 473 | } |
| 474 | |
| 475 | // Add variable address. |
| 476 | |
| 477 | unsigned Offset = DV.getDotDebugLocOffset(); |
| 478 | if (Offset != ~0U) { |
| 479 | addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); |
| 480 | return VariableDie; |
| 481 | } |
| 482 | |
| 483 | // Check if variable is described by a DBG_VALUE instruction. |
| 484 | if (const MachineInstr *DVInsn = DV.getMInsn()) { |
| 485 | assert(DVInsn->getNumOperands() == 4); |
| 486 | if (DVInsn->getOperand(0).isReg()) { |
| 487 | const MachineOperand RegOp = DVInsn->getOperand(0); |
| 488 | // If the second operand is an immediate, this is an indirect value. |
| 489 | if (DVInsn->getOperand(1).isImm()) { |
| 490 | MachineLocation Location(RegOp.getReg(), |
| 491 | DVInsn->getOperand(1).getImm()); |
| 492 | addVariableAddress(DV, *VariableDie, Location); |
| 493 | } else if (RegOp.getReg()) |
| 494 | addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg())); |
| 495 | } else if (DVInsn->getOperand(0).isImm()) |
| 496 | addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType()); |
| 497 | else if (DVInsn->getOperand(0).isFPImm()) |
| 498 | addConstantFPValue(*VariableDie, DVInsn->getOperand(0)); |
| 499 | else if (DVInsn->getOperand(0).isCImm()) |
| 500 | addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(), |
| 501 | DV.getType()); |
| 502 | |
| 503 | return VariableDie; |
| 504 | } |
| 505 | |
| 506 | // .. else use frame index. |
| 507 | int FI = DV.getFrameIndex(); |
| 508 | if (FI != ~0) { |
| 509 | unsigned FrameReg = 0; |
| 510 | const TargetFrameLowering *TFI = |
| 511 | Asm->TM.getSubtargetImpl()->getFrameLowering(); |
| 512 | int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); |
| 513 | MachineLocation Location(FrameReg, Offset); |
| 514 | addVariableAddress(DV, *VariableDie, Location); |
| 515 | } |
| 516 | |
| 517 | return VariableDie; |
| 518 | } |
| 519 | |
David Blaikie | 4a1a44e | 2014-10-09 17:56:39 +0000 | [diff] [blame] | 520 | std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE( |
| 521 | DbgVariable &DV, const LexicalScope &Scope, DIE *&ObjectPointer) { |
| 522 | auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); |
| 523 | if (DV.isObjectPointer()) |
| 524 | ObjectPointer = Var.get(); |
| 525 | return Var; |
| 526 | } |
| 527 | |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 528 | DIE *DwarfCompileUnit::createScopeChildrenDIE( |
| 529 | LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &Children, |
| 530 | unsigned *ChildScopeCount) { |
| 531 | DIE *ObjectPointer = nullptr; |
| 532 | |
| 533 | for (DbgVariable *DV : DD->getScopeVariables().lookup(Scope)) |
| 534 | Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); |
| 535 | |
| 536 | unsigned ChildCountWithoutScopes = Children.size(); |
| 537 | |
| 538 | for (LexicalScope *LS : Scope->getChildren()) |
| 539 | constructScopeDIE(LS, Children); |
| 540 | |
| 541 | if (ChildScopeCount) |
| 542 | *ChildScopeCount = Children.size() - ChildCountWithoutScopes; |
| 543 | |
| 544 | return ObjectPointer; |
| 545 | } |
| 546 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 547 | void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) { |
| 548 | assert(Scope && Scope->getScopeNode()); |
| 549 | assert(!Scope->getInlinedAt()); |
| 550 | assert(!Scope->isAbstractScope()); |
| 551 | DISubprogram Sub(Scope->getScopeNode()); |
| 552 | |
| 553 | assert(Sub.isSubprogram()); |
| 554 | |
| 555 | DD->getProcessedSPNodes().insert(Sub); |
| 556 | |
| 557 | DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); |
| 558 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 559 | // If this is a variadic function, add an unspecified parameter. |
| 560 | DITypeArray FnArgs = Sub.getType().getTypeArray(); |
David Blaikie | 1dd573d | 2014-10-23 22:27:50 +0000 | [diff] [blame^] | 561 | |
| 562 | // Collect lexical scope children first. |
| 563 | // ObjectPointer might be a local (non-argument) local variable if it's a |
| 564 | // block's synthetic this pointer. |
| 565 | if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE)) |
| 566 | addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); |
| 567 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 568 | // If we have a single element of null, it is a function that returns void. |
| 569 | // If we have more than one elements and the last one is null, it is a |
| 570 | // variadic function. |
| 571 | if (FnArgs.getNumElements() > 1 && |
| 572 | !FnArgs.getElement(FnArgs.getNumElements() - 1)) |
| 573 | ScopeDIE.addChild(make_unique<DIE>(dwarf::DW_TAG_unspecified_parameters)); |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 574 | } |
| 575 | |
David Blaikie | 78b65b6 | 2014-10-09 20:26:15 +0000 | [diff] [blame] | 576 | DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, |
| 577 | DIE &ScopeDIE) { |
| 578 | // We create children when the scope DIE is not null. |
| 579 | SmallVector<std::unique_ptr<DIE>, 8> Children; |
| 580 | DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); |
| 581 | |
| 582 | // Add children |
| 583 | for (auto &I : Children) |
| 584 | ScopeDIE.addChild(std::move(I)); |
| 585 | |
| 586 | return ObjectPointer; |
| 587 | } |
| 588 | |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 589 | DIE & |
| 590 | DwarfCompileUnit::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) { |
| 591 | DISubprogram SP(Scope->getScopeNode()); |
| 592 | |
| 593 | DIE *ContextDIE; |
| 594 | |
| 595 | // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with |
| 596 | // the important distinction that the DIDescriptor is not associated with the |
| 597 | // DIE (since the DIDescriptor will be associated with the concrete DIE, if |
| 598 | // any). It could be refactored to some common utility function. |
| 599 | if (DISubprogram SPDecl = SP.getFunctionDeclaration()) { |
| 600 | ContextDIE = &getUnitDie(); |
| 601 | getOrCreateSubprogramDIE(SPDecl); |
| 602 | } else |
| 603 | ContextDIE = getOrCreateContextDIE(resolve(SP.getContext())); |
| 604 | |
| 605 | // Passing null as the associated DIDescriptor because the abstract definition |
| 606 | // shouldn't be found by lookup. |
| 607 | DIE &AbsDef = |
| 608 | createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, DIDescriptor()); |
| 609 | applySubprogramAttributesToDefinition(SP, AbsDef); |
| 610 | |
| 611 | if (getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) |
| 612 | addUInt(AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); |
| 613 | if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, AbsDef)) |
| 614 | addDIEEntry(AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); |
| 615 | return AbsDef; |
| 616 | } |
| 617 | |
David Blaikie | 4191cbc | 2014-10-10 06:39:29 +0000 | [diff] [blame] | 618 | void DwarfCompileUnit::finishSubprogramDefinition(DISubprogram SP) { |
| 619 | DIE *D = getDIE(SP); |
| 620 | if (DIE *AbsSPDIE = DD->getAbstractSPDies().lookup(SP)) { |
| 621 | if (D) |
| 622 | // If this subprogram has an abstract definition, reference that |
| 623 | addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); |
| 624 | } else { |
| 625 | if (!D && getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) |
| 626 | // Lazily construct the subprogram if we didn't see either concrete or |
| 627 | // inlined versions during codegen. (except in -gmlt ^ where we want |
| 628 | // to omit these entirely) |
| 629 | D = getOrCreateSubprogramDIE(SP); |
| 630 | if (D) |
| 631 | // And attach the attributes |
| 632 | applySubprogramAttributesToDefinition(SP, *D); |
| 633 | } |
| 634 | } |
| 635 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 636 | } // end llvm namespace |