David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 1 | #include "DwarfCompileUnit.h" |
Adrian Prantl | 7813d9c | 2015-01-14 01:01:30 +0000 | [diff] [blame] | 2 | #include "DwarfExpression.h" |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 3 | #include "llvm/CodeGen/MachineFunction.h" |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 4 | #include "llvm/IR/Constants.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 5 | #include "llvm/IR/DataLayout.h" |
| 6 | #include "llvm/IR/GlobalValue.h" |
| 7 | #include "llvm/IR/GlobalVariable.h" |
| 8 | #include "llvm/IR/Instruction.h" |
| 9 | #include "llvm/MC/MCAsmInfo.h" |
| 10 | #include "llvm/MC/MCStreamer.h" |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 11 | #include "llvm/Target/TargetFrameLowering.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 12 | #include "llvm/Target/TargetLoweringObjectFile.h" |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 13 | #include "llvm/Target/TargetMachine.h" |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 14 | #include "llvm/Target/TargetRegisterInfo.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetSubtargetInfo.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 16 | |
| 17 | namespace llvm { |
| 18 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 19 | DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 20 | AsmPrinter *A, DwarfDebug *DW, |
| 21 | DwarfFile *DWU) |
David Blaikie | 7cbf58a | 2014-11-01 18:18:07 +0000 | [diff] [blame] | 22 | : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU), |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 23 | Skeleton(nullptr), BaseAddress(nullptr) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 24 | insertDIE(Node, &getUnitDie()); |
| 25 | } |
| 26 | |
| 27 | /// addLabelAddress - Add a dwarf label attribute data and value using |
| 28 | /// DW_FORM_addr or DW_FORM_GNU_addr_index. |
| 29 | /// |
| 30 | void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, |
| 31 | const MCSymbol *Label) { |
| 32 | |
| 33 | // Don't use the address pool in non-fission or in the skeleton unit itself. |
| 34 | // FIXME: Once GDB supports this, it's probably worthwhile using the address |
| 35 | // pool from the skeleton - maybe even in non-fission (possibly fewer |
| 36 | // relocations by sharing them in the pool, but we have other ideas about how |
| 37 | // to reduce the number of relocations as well/instead). |
| 38 | if (!DD->useSplitDwarf() || !Skeleton) |
| 39 | return addLocalLabelAddress(Die, Attribute, Label); |
| 40 | |
| 41 | if (Label) |
| 42 | DD->addArangeLabel(SymbolCU(this, Label)); |
| 43 | |
| 44 | unsigned idx = DD->getAddressPool().getIndex(Label); |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 45 | Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_GNU_addr_index, |
| 46 | DIEInteger(idx)); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, |
| 50 | dwarf::Attribute Attribute, |
| 51 | const MCSymbol *Label) { |
| 52 | if (Label) |
| 53 | DD->addArangeLabel(SymbolCU(this, Label)); |
| 54 | |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 55 | if (Label) |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 56 | Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, |
| 57 | DIELabel(Label)); |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 58 | else |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 59 | Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, |
| 60 | DIEInteger(0)); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 61 | } |
| 62 | |
David Blaikie | 8945219 | 2014-10-04 16:00:26 +0000 | [diff] [blame] | 63 | unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, |
| 64 | StringRef DirName) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 65 | // If we print assembly, we can't separate .file entries according to |
| 66 | // compile units. Thus all files will belong to the default compile unit. |
| 67 | |
| 68 | // FIXME: add a better feature test than hasRawTextSupport. Even better, |
| 69 | // extend .file to support this. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 70 | return Asm->OutStreamer->EmitDwarfFileDirective( |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 71 | 0, DirName, FileName, |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 72 | Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Return const expression if value is a GEP to access merged global |
| 76 | // constant. e.g. |
| 77 | // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) |
| 78 | static const ConstantExpr *getMergedGlobalExpr(const Value *V) { |
| 79 | const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); |
| 80 | if (!CE || CE->getNumOperands() != 3 || |
| 81 | CE->getOpcode() != Instruction::GetElementPtr) |
| 82 | return nullptr; |
| 83 | |
| 84 | // First operand points to a global struct. |
| 85 | Value *Ptr = CE->getOperand(0); |
| 86 | if (!isa<GlobalValue>(Ptr) || |
| 87 | !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) |
| 88 | return nullptr; |
| 89 | |
| 90 | // Second operand is zero. |
| 91 | const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); |
| 92 | if (!CI || !CI->isZero()) |
| 93 | return nullptr; |
| 94 | |
| 95 | // Third operand is offset. |
| 96 | if (!isa<ConstantInt>(CE->getOperand(2))) |
| 97 | return nullptr; |
| 98 | |
| 99 | return CE; |
| 100 | } |
| 101 | |
| 102 | /// getOrCreateGlobalVariableDIE - get or create global variable DIE. |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 103 | DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( |
David Blaikie | d51dea6 | 2015-07-01 18:07:16 +0000 | [diff] [blame] | 104 | const DIGlobalVariable *GV) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 105 | // Check for pre-existence. |
| 106 | if (DIE *Die = getDIE(GV)) |
| 107 | return Die; |
| 108 | |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 109 | assert(GV); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 110 | |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 111 | auto *GVContext = GV->getScope(); |
| 112 | auto *GTy = DD->resolve(GV->getType()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 113 | |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 114 | // Construct the context before querying for the existence of the DIE in |
| 115 | // case such construction creates the DIE. |
David Blaikie | d51dea6 | 2015-07-01 18:07:16 +0000 | [diff] [blame] | 116 | DIE *ContextDIE = getOrCreateContextDIE(GVContext); |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 117 | |
| 118 | // Add to map. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 119 | DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 120 | DIScope *DeclContext; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 121 | if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) { |
| 122 | DeclContext = resolve(SDMDecl->getScope()); |
| 123 | assert(SDMDecl->isStaticMember() && "Expected static member decl"); |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 124 | assert(GV->isDefinition()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 125 | // 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] | 126 | DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); |
| 127 | addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); |
| 128 | } else { |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 129 | DeclContext = GV->getScope(); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 130 | // Add name and type. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 131 | addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 132 | addType(*VariableDIE, GTy); |
| 133 | |
| 134 | // Add scoping info. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 135 | if (!GV->isLocalToUnit()) |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 136 | addFlag(*VariableDIE, dwarf::DW_AT_external); |
| 137 | |
| 138 | // Add line number info. |
| 139 | addSourceLine(*VariableDIE, GV); |
| 140 | } |
| 141 | |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 142 | if (!GV->isDefinition()) |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 143 | addFlag(*VariableDIE, dwarf::DW_AT_declaration); |
David Blaikie | 877354a | 2015-04-14 18:08:25 +0000 | [diff] [blame] | 144 | else |
| 145 | addGlobalName(GV->getName(), *VariableDIE, DeclContext); |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 146 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 147 | // Add location. |
| 148 | bool addToAccelTable = false; |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 149 | if (auto *Global = dyn_cast_or_null<GlobalVariable>(GV->getVariable())) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 150 | addToAccelTable = true; |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 151 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
Duncan P. N. Exon Smith | cca5f68 | 2015-04-13 20:39:25 +0000 | [diff] [blame] | 152 | const MCSymbol *Sym = Asm->getSymbol(Global); |
| 153 | if (Global->isThreadLocal()) { |
Chih-Hung Hsieh | 1e85958 | 2015-07-28 16:24:05 +0000 | [diff] [blame] | 154 | if (Asm->TM.Options.EmulatedTLS) { |
| 155 | // TODO: add debug info for emulated thread local mode. |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 156 | } else { |
Chih-Hung Hsieh | 1e85958 | 2015-07-28 16:24:05 +0000 | [diff] [blame] | 157 | // FIXME: Make this work with -gsplit-dwarf. |
| 158 | unsigned PointerSize = Asm->getDataLayout().getPointerSize(); |
| 159 | assert((PointerSize == 4 || PointerSize == 8) && |
| 160 | "Add support for other sizes if necessary"); |
| 161 | // Based on GCC's support for TLS: |
| 162 | if (!DD->useSplitDwarf()) { |
| 163 | // 1) Start with a constNu of the appropriate pointer size |
Dehao Chen | 5451135 | 2015-11-11 18:09:47 +0000 | [diff] [blame] | 164 | addUInt(*Loc, dwarf::DW_FORM_data1, PointerSize == 4 |
| 165 | ? dwarf::DW_OP_const4u |
| 166 | : dwarf::DW_OP_const8u); |
Chih-Hung Hsieh | 1e85958 | 2015-07-28 16:24:05 +0000 | [diff] [blame] | 167 | // 2) containing the (relocated) offset of the TLS variable |
| 168 | // within the module's TLS block. |
| 169 | addExpr(*Loc, dwarf::DW_FORM_udata, |
| 170 | Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); |
| 171 | } else { |
| 172 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); |
| 173 | addUInt(*Loc, dwarf::DW_FORM_udata, |
| 174 | DD->getAddressPool().getIndex(Sym, /* TLS */ true)); |
| 175 | } |
| 176 | // 3) followed by an OP to make the debugger do a TLS lookup. |
| 177 | addUInt(*Loc, dwarf::DW_FORM_data1, |
| 178 | DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address |
| 179 | : dwarf::DW_OP_form_tls_address); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 180 | } |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 181 | } else { |
| 182 | DD->addArangeLabel(SymbolCU(this, Sym)); |
| 183 | addOpAddress(*Loc, Sym); |
| 184 | } |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 185 | |
| 186 | addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 187 | addLinkageName(*VariableDIE, GV->getLinkageName()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 188 | } else if (const ConstantInt *CI = |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 189 | dyn_cast_or_null<ConstantInt>(GV->getVariable())) { |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 190 | addConstantValue(*VariableDIE, CI, GTy); |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 191 | } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getVariable())) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 192 | addToAccelTable = true; |
| 193 | // GV is a merged global. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 194 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 195 | Value *Ptr = CE->getOperand(0); |
| 196 | MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr)); |
| 197 | DD->addArangeLabel(SymbolCU(this, Sym)); |
| 198 | addOpAddress(*Loc, Sym); |
| 199 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
| 200 | SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end()); |
| 201 | addUInt(*Loc, dwarf::DW_FORM_udata, |
| 202 | Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); |
| 203 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); |
| 204 | addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); |
| 205 | } |
| 206 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 207 | if (addToAccelTable) { |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 208 | DD->addAccelName(GV->getName(), *VariableDIE); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 209 | |
| 210 | // If the linkage name is different than the name, go ahead and output |
| 211 | // that as well into the name table. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 212 | if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName()) |
| 213 | DD->addAccelName(GV->getLinkageName(), *VariableDIE); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 214 | } |
| 215 | |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 216 | return VariableDIE; |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void DwarfCompileUnit::addRange(RangeSpan Range) { |
| 220 | bool SameAsPrevCU = this == DD->getPrevCU(); |
| 221 | DD->setPrevCU(this); |
| 222 | // If we have no current ranges just add the range and return, otherwise, |
| 223 | // check the current section and CU against the previous section and CU we |
| 224 | // emitted into and the subprogram was contained within. If these are the |
| 225 | // same then extend our current range, otherwise add this as a new range. |
| 226 | if (CURanges.empty() || !SameAsPrevCU || |
| 227 | (&CURanges.back().getEnd()->getSection() != |
| 228 | &Range.getEnd()->getSection())) { |
| 229 | CURanges.push_back(Range); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | CURanges.back().setEnd(Range.getEnd()); |
| 234 | } |
| 235 | |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 236 | DIE::value_iterator |
| 237 | DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, |
| 238 | const MCSymbol *Label, const MCSymbol *Sec) { |
David Blaikie | 6c0ee4e | 2014-10-08 22:46:27 +0000 | [diff] [blame] | 239 | if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 240 | return addLabel(Die, Attribute, |
| 241 | DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset |
| 242 | : dwarf::DW_FORM_data4, |
| 243 | Label); |
| 244 | return addSectionDelta(Die, Attribute, Label, Sec); |
David Blaikie | 6c0ee4e | 2014-10-08 22:46:27 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 247 | void DwarfCompileUnit::initStmtList() { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 248 | // Define start line table label for each Compile Unit. |
| 249 | MCSymbol *LineTableStartSym = |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 250 | Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 251 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 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. |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 257 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 258 | StmtListValue = |
| 259 | addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym, |
| 260 | TLOF.getDwarfLineSection()->getBeginSymbol()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void DwarfCompileUnit::applyStmtList(DIE &D) { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 264 | D.addValue(DIEValueAllocator, *StmtListValue); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 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. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 284 | DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) { |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 285 | DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes()); |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 286 | |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 287 | attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd()); |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 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 |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 293 | if (!includeMinimalInlineScopes()) { |
Eric Christopher | d4e723f | 2015-02-20 22:36:11 +0000 | [diff] [blame] | 294 | const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo(); |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 295 | MachineLocation Location(RI->getFrameRegister(*Asm->MF)); |
Adrian Prantl | 8efadbf | 2015-01-14 00:15:16 +0000 | [diff] [blame] | 296 | if (RI->isPhysicalRegister(Location.getReg())) |
| 297 | addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 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( |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 309 | LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) { |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 310 | if (!Scope || !Scope->getScopeNode()) |
| 311 | return; |
| 312 | |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 313 | auto *DS = Scope->getScopeNode(); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 314 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 315 | assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) && |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 316 | "Only handle inlined subprograms here, use " |
| 317 | "constructSubprogramScopeDIE for non-inlined " |
| 318 | "subprograms"); |
| 319 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 320 | SmallVector<DIE *, 8> Children; |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 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. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 325 | DIE *ScopeDIE; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 326 | if (Scope->getParent() && isa<DISubprogram>(DS)) { |
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 | |
David Blaikie | d51dea6 | 2015-07-01 18:07:16 +0000 | [diff] [blame] | 343 | // Skip imported directives in gmlt-like data. |
| 344 | if (!includeMinimalInlineScopes()) { |
| 345 | // There is no need to emit empty lexical block DIE. |
Ivan Krasin | 298639a | 2015-10-26 21:36:35 +0000 | [diff] [blame] | 346 | for (const auto *IE : ImportedEntities[DS]) |
Ivan Krasin | 465fbe2 | 2015-10-26 22:35:40 +0000 | [diff] [blame] | 347 | Children.push_back( |
| 348 | constructImportedEntityDIE(cast<DIImportedEntity>(IE))); |
David Blaikie | d51dea6 | 2015-07-01 18:07:16 +0000 | [diff] [blame] | 349 | } |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 350 | |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 351 | // If there are only other scopes as children, put them directly in the |
| 352 | // parent instead, as this scope would serve no purpose. |
David Blaikie | d51dea6 | 2015-07-01 18:07:16 +0000 | [diff] [blame] | 353 | if (Children.size() == ChildScopeCount) { |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 354 | FinalChildren.insert(FinalChildren.end(), |
| 355 | std::make_move_iterator(Children.begin()), |
| 356 | std::make_move_iterator(Children.end())); |
| 357 | return; |
| 358 | } |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 359 | ScopeDIE = constructLexicalScopeDIE(Scope); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 360 | assert(ScopeDIE && "Scope DIE should not be null."); |
| 361 | } |
| 362 | |
| 363 | // Add children |
| 364 | for (auto &I : Children) |
| 365 | ScopeDIE->addChild(std::move(I)); |
| 366 | |
| 367 | FinalChildren.push_back(std::move(ScopeDIE)); |
| 368 | } |
| 369 | |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 370 | DIE::value_iterator |
| 371 | DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, |
| 372 | const MCSymbol *Hi, const MCSymbol *Lo) { |
| 373 | return Die.addValue(DIEValueAllocator, Attribute, |
| 374 | DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset |
| 375 | : dwarf::DW_FORM_data4, |
| 376 | new (DIEValueAllocator) DIEDelta(Hi, Lo)); |
David Blaikie | e5feec5 | 2014-10-08 23:30:05 +0000 | [diff] [blame] | 377 | } |
| 378 | |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 379 | void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, |
| 380 | SmallVector<RangeSpan, 2> Range) { |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 381 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 382 | |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 383 | // Emit offset in .debug_range as a relocatable label. emitDIE will handle |
| 384 | // emitting it appropriately. |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 385 | const MCSymbol *RangeSectionSym = |
| 386 | TLOF.getDwarfRangesSection()->getBeginSymbol(); |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 387 | |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 388 | RangeSpanList List(Asm->createTempSymbol("debug_ranges"), std::move(Range)); |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 389 | |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 390 | // Under fission, ranges are specified by constant offsets relative to the |
| 391 | // CU's DW_AT_GNU_ranges_base. |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 392 | if (isDwoUnit()) |
| 393 | addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), |
| 394 | RangeSectionSym); |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 395 | else |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 396 | addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), |
| 397 | RangeSectionSym); |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 398 | |
| 399 | // Add the range list to the set of ranges to be emitted. |
David Blaikie | 542616d | 2014-11-03 21:52:56 +0000 | [diff] [blame] | 400 | (Skeleton ? Skeleton : this)->CURangeLists.push_back(std::move(List)); |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 401 | } |
| 402 | |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 403 | void DwarfCompileUnit::attachRangesOrLowHighPC( |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 404 | DIE &Die, SmallVector<RangeSpan, 2> Ranges) { |
| 405 | if (Ranges.size() == 1) { |
| 406 | const auto &single = Ranges.front(); |
| 407 | attachLowHighPC(Die, single.getStart(), single.getEnd()); |
| 408 | } else |
| 409 | addScopeRangeList(Die, std::move(Ranges)); |
| 410 | } |
| 411 | |
| 412 | void DwarfCompileUnit::attachRangesOrLowHighPC( |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 413 | DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 414 | SmallVector<RangeSpan, 2> List; |
| 415 | List.reserve(Ranges.size()); |
| 416 | for (const InsnRange &R : Ranges) |
| 417 | List.push_back(RangeSpan(DD->getLabelBeforeInsn(R.first), |
| 418 | DD->getLabelAfterInsn(R.second))); |
| 419 | attachRangesOrLowHighPC(Die, std::move(List)); |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 420 | } |
| 421 | |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 422 | // This scope represents inlined body of a function. Construct DIE to |
| 423 | // represent this concrete inlined copy of the function. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 424 | DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 425 | assert(Scope->getScopeNode()); |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 426 | auto *DS = Scope->getScopeNode(); |
| 427 | auto *InlinedSP = getDISubprogram(DS); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 428 | // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram |
| 429 | // was inlined from another compile unit. |
David Blaikie | f6dac29 | 2014-11-01 17:21:26 +0000 | [diff] [blame] | 430 | DIE *OriginDIE = DU->getAbstractSPDies()[InlinedSP]; |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 431 | assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); |
| 432 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 433 | auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 434 | addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); |
| 435 | |
| 436 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); |
| 437 | |
| 438 | // Add the call site information to the DIE. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 439 | const DILocation *IA = Scope->getInlinedAt(); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 440 | addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 441 | getOrCreateSourceID(IA->getFilename(), IA->getDirectory())); |
| 442 | addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine()); |
Dehao Chen | 72fdf44 | 2015-11-11 18:08:18 +0000 | [diff] [blame] | 443 | if (IA->getDiscriminator()) |
Dehao Chen | 5451135 | 2015-11-11 18:09:47 +0000 | [diff] [blame] | 444 | addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None, |
| 445 | IA->getDiscriminator()); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 446 | |
| 447 | // Add name to the name table, we do this here because we're guaranteed |
| 448 | // to have concrete versions of our DW_TAG_inlined_subprogram nodes. |
| 449 | DD->addSubprogramNames(InlinedSP, *ScopeDIE); |
| 450 | |
| 451 | return ScopeDIE; |
| 452 | } |
| 453 | |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 454 | // Construct new DW_TAG_lexical_block for this scope and attach |
| 455 | // DW_AT_low_pc/DW_AT_high_pc labels. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 456 | DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 457 | if (DD->isLexicalScopeDIENull(Scope)) |
| 458 | return nullptr; |
| 459 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 460 | auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block); |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 461 | if (Scope->isAbstractScope()) |
| 462 | return ScopeDIE; |
| 463 | |
| 464 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); |
| 465 | |
| 466 | return ScopeDIE; |
| 467 | } |
| 468 | |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 469 | /// constructVariableDIE - Construct a DIE for the given DbgVariable. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 470 | DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) { |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 471 | auto D = constructVariableDIEImpl(DV, Abstract); |
| 472 | DV.setDIE(*D); |
| 473 | return D; |
| 474 | } |
| 475 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 476 | DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, |
| 477 | bool Abstract) { |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 478 | // Define variable debug information entry. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 479 | auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag()); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 480 | |
| 481 | if (Abstract) { |
| 482 | applyVariableAttributes(DV, *VariableDie); |
| 483 | return VariableDie; |
| 484 | } |
| 485 | |
| 486 | // Add variable address. |
| 487 | |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 488 | unsigned Offset = DV.getDebugLocListIndex(); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 489 | if (Offset != ~0U) { |
| 490 | addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); |
| 491 | return VariableDie; |
| 492 | } |
| 493 | |
| 494 | // Check if variable is described by a DBG_VALUE instruction. |
| 495 | if (const MachineInstr *DVInsn = DV.getMInsn()) { |
| 496 | assert(DVInsn->getNumOperands() == 4); |
| 497 | if (DVInsn->getOperand(0).isReg()) { |
| 498 | const MachineOperand RegOp = DVInsn->getOperand(0); |
| 499 | // If the second operand is an immediate, this is an indirect value. |
| 500 | if (DVInsn->getOperand(1).isImm()) { |
| 501 | MachineLocation Location(RegOp.getReg(), |
| 502 | DVInsn->getOperand(1).getImm()); |
| 503 | addVariableAddress(DV, *VariableDie, Location); |
| 504 | } else if (RegOp.getReg()) |
| 505 | addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg())); |
| 506 | } else if (DVInsn->getOperand(0).isImm()) |
| 507 | addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType()); |
| 508 | else if (DVInsn->getOperand(0).isFPImm()) |
| 509 | addConstantFPValue(*VariableDie, DVInsn->getOperand(0)); |
| 510 | else if (DVInsn->getOperand(0).isCImm()) |
| 511 | addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(), |
| 512 | DV.getType()); |
| 513 | |
| 514 | return VariableDie; |
| 515 | } |
| 516 | |
| 517 | // .. else use frame index. |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 518 | if (DV.getFrameIndex().empty()) |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 519 | return VariableDie; |
| 520 | |
| 521 | auto Expr = DV.getExpression().begin(); |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 522 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 523 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
| 524 | for (auto FI : DV.getFrameIndex()) { |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 525 | unsigned FrameReg = 0; |
Eric Christopher | d4e723f | 2015-02-20 22:36:11 +0000 | [diff] [blame] | 526 | const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 527 | int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); |
Dehao Chen | 5451135 | 2015-11-11 18:09:47 +0000 | [diff] [blame] | 528 | assert(Expr != DV.getExpression().end() && "Wrong number of expressions"); |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 529 | DwarfExpr.AddMachineRegIndirect(FrameReg, Offset); |
Duncan P. N. Exon Smith | 76c9184 | 2015-04-07 03:45:57 +0000 | [diff] [blame] | 530 | DwarfExpr.AddExpression((*Expr)->expr_op_begin(), (*Expr)->expr_op_end()); |
Duncan P. N. Exon Smith | 57bab0b | 2015-02-17 22:30:56 +0000 | [diff] [blame] | 531 | ++Expr; |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 532 | } |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 533 | addBlock(*VariableDie, dwarf::DW_AT_location, Loc); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 534 | |
| 535 | return VariableDie; |
| 536 | } |
| 537 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 538 | DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, |
| 539 | const LexicalScope &Scope, |
| 540 | DIE *&ObjectPointer) { |
David Blaikie | 4a1a44e | 2014-10-09 17:56:39 +0000 | [diff] [blame] | 541 | auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); |
| 542 | if (DV.isObjectPointer()) |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 543 | ObjectPointer = Var; |
David Blaikie | 4a1a44e | 2014-10-09 17:56:39 +0000 | [diff] [blame] | 544 | return Var; |
| 545 | } |
| 546 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 547 | DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, |
| 548 | SmallVectorImpl<DIE *> &Children, |
| 549 | unsigned *ChildScopeCount) { |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 550 | DIE *ObjectPointer = nullptr; |
| 551 | |
David Blaikie | 80e5b1e | 2014-10-24 17:57:34 +0000 | [diff] [blame] | 552 | for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope)) |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 553 | Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); |
| 554 | |
| 555 | unsigned ChildCountWithoutScopes = Children.size(); |
| 556 | |
| 557 | for (LexicalScope *LS : Scope->getChildren()) |
| 558 | constructScopeDIE(LS, Children); |
| 559 | |
| 560 | if (ChildScopeCount) |
| 561 | *ChildScopeCount = Children.size() - ChildCountWithoutScopes; |
| 562 | |
| 563 | return ObjectPointer; |
| 564 | } |
| 565 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 566 | void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) { |
| 567 | assert(Scope && Scope->getScopeNode()); |
| 568 | assert(!Scope->getInlinedAt()); |
| 569 | assert(!Scope->isAbstractScope()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 570 | auto *Sub = cast<DISubprogram>(Scope->getScopeNode()); |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 571 | |
| 572 | DD->getProcessedSPNodes().insert(Sub); |
| 573 | |
| 574 | DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); |
| 575 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 576 | // 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] | 577 | DITypeRefArray FnArgs = Sub->getType()->getTypeArray(); |
David Blaikie | 1dd573d | 2014-10-23 22:27:50 +0000 | [diff] [blame] | 578 | |
| 579 | // Collect lexical scope children first. |
| 580 | // ObjectPointer might be a local (non-argument) local variable if it's a |
| 581 | // block's synthetic this pointer. |
| 582 | if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE)) |
| 583 | addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); |
| 584 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 585 | // If we have a single element of null, it is a function that returns void. |
| 586 | // If we have more than one elements and the last one is null, it is a |
| 587 | // variadic function. |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 588 | if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] && |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 589 | !includeMinimalInlineScopes()) |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 590 | ScopeDIE.addChild( |
| 591 | DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters)); |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 592 | } |
| 593 | |
David Blaikie | 78b65b6 | 2014-10-09 20:26:15 +0000 | [diff] [blame] | 594 | DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, |
| 595 | DIE &ScopeDIE) { |
| 596 | // 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] | 597 | SmallVector<DIE *, 8> Children; |
David Blaikie | 78b65b6 | 2014-10-09 20:26:15 +0000 | [diff] [blame] | 598 | DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); |
| 599 | |
| 600 | // Add children |
| 601 | for (auto &I : Children) |
| 602 | ScopeDIE.addChild(std::move(I)); |
| 603 | |
| 604 | return ObjectPointer; |
| 605 | } |
| 606 | |
Dehao Chen | 5451135 | 2015-11-11 18:09:47 +0000 | [diff] [blame] | 607 | void DwarfCompileUnit::constructAbstractSubprogramScopeDIE( |
| 608 | LexicalScope *Scope) { |
David Blaikie | f6dac29 | 2014-11-01 17:21:26 +0000 | [diff] [blame] | 609 | DIE *&AbsDef = DU->getAbstractSPDies()[Scope->getScopeNode()]; |
David Blaikie | 49be5b3 | 2014-10-31 21:57:02 +0000 | [diff] [blame] | 610 | if (AbsDef) |
| 611 | return; |
| 612 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 613 | auto *SP = cast<DISubprogram>(Scope->getScopeNode()); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 614 | |
| 615 | DIE *ContextDIE; |
| 616 | |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 617 | if (includeMinimalInlineScopes()) |
| 618 | ContextDIE = &getUnitDie(); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 619 | // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with |
Duncan P. N. Exon Smith | 7c60f20 | 2015-04-18 00:35:36 +0000 | [diff] [blame] | 620 | // the important distinction that the debug node is not associated with the |
| 621 | // 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] | 622 | // 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] | 623 | else if (auto *SPDecl = SP->getDeclaration()) { |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 624 | ContextDIE = &getUnitDie(); |
| 625 | getOrCreateSubprogramDIE(SPDecl); |
| 626 | } else |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 627 | ContextDIE = getOrCreateContextDIE(resolve(SP->getScope())); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 628 | |
Duncan P. N. Exon Smith | 7c60f20 | 2015-04-18 00:35:36 +0000 | [diff] [blame] | 629 | // Passing null as the associated node because the abstract definition |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 630 | // shouldn't be found by lookup. |
Duncan P. N. Exon Smith | 7c60f20 | 2015-04-18 00:35:36 +0000 | [diff] [blame] | 631 | AbsDef = &createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr); |
David Blaikie | 49be5b3 | 2014-10-31 21:57:02 +0000 | [diff] [blame] | 632 | applySubprogramAttributesToDefinition(SP, *AbsDef); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 633 | |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 634 | if (!includeMinimalInlineScopes()) |
David Blaikie | 49be5b3 | 2014-10-31 21:57:02 +0000 | [diff] [blame] | 635 | addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); |
| 636 | if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, *AbsDef)) |
| 637 | addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 640 | DIE *DwarfCompileUnit::constructImportedEntityDIE( |
| 641 | const DIImportedEntity *Module) { |
| 642 | DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag()); |
| 643 | insertDIE(Module, IMDie); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 644 | DIE *EntityDie; |
Duncan P. N. Exon Smith | de8e427 | 2015-04-14 01:46:44 +0000 | [diff] [blame] | 645 | auto *Entity = resolve(Module->getEntity()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 646 | if (auto *NS = dyn_cast<DINamespace>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 647 | EntityDie = getOrCreateNameSpace(NS); |
Adrian Prantl | 08a388b | 2015-06-30 02:13:04 +0000 | [diff] [blame] | 648 | else if (auto *M = dyn_cast<DIModule>(Entity)) |
| 649 | EntityDie = getOrCreateModule(M); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 650 | else if (auto *SP = dyn_cast<DISubprogram>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 651 | EntityDie = getOrCreateSubprogramDIE(SP); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 652 | else if (auto *T = dyn_cast<DIType>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 653 | EntityDie = getOrCreateTypeDIE(T); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 654 | else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 655 | EntityDie = getOrCreateGlobalVariableDIE(GV); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 656 | else |
| 657 | EntityDie = getDIE(Entity); |
| 658 | assert(EntityDie); |
Duncan P. N. Exon Smith | de8e427 | 2015-04-14 01:46:44 +0000 | [diff] [blame] | 659 | addSourceLine(*IMDie, Module->getLine(), Module->getScope()->getFilename(), |
| 660 | Module->getScope()->getDirectory()); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 661 | addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie); |
Duncan P. N. Exon Smith | de8e427 | 2015-04-14 01:46:44 +0000 | [diff] [blame] | 662 | StringRef Name = Module->getName(); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 663 | if (!Name.empty()) |
| 664 | addString(*IMDie, dwarf::DW_AT_name, Name); |
| 665 | |
| 666 | return IMDie; |
| 667 | } |
| 668 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 669 | void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) { |
David Blaikie | 4191cbc | 2014-10-10 06:39:29 +0000 | [diff] [blame] | 670 | DIE *D = getDIE(SP); |
David Blaikie | f6dac29 | 2014-11-01 17:21:26 +0000 | [diff] [blame] | 671 | if (DIE *AbsSPDIE = DU->getAbstractSPDies().lookup(SP)) { |
David Blaikie | 4191cbc | 2014-10-10 06:39:29 +0000 | [diff] [blame] | 672 | if (D) |
| 673 | // If this subprogram has an abstract definition, reference that |
| 674 | addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); |
| 675 | } else { |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 676 | if (!D && !includeMinimalInlineScopes()) |
David Blaikie | 4191cbc | 2014-10-10 06:39:29 +0000 | [diff] [blame] | 677 | // Lazily construct the subprogram if we didn't see either concrete or |
| 678 | // inlined versions during codegen. (except in -gmlt ^ where we want |
| 679 | // to omit these entirely) |
| 680 | D = getOrCreateSubprogramDIE(SP); |
| 681 | if (D) |
| 682 | // And attach the attributes |
| 683 | applySubprogramAttributesToDefinition(SP, *D); |
| 684 | } |
| 685 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 686 | void DwarfCompileUnit::collectDeadVariables(const DISubprogram *SP) { |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 687 | assert(SP && "CU's subprogram list contains a non-subprogram"); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 688 | assert(SP->isDefinition() && |
David Blaikie | 1d96cc2 | 2014-10-31 22:30:30 +0000 | [diff] [blame] | 689 | "CU's subprogram list contains a subprogram declaration"); |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 690 | auto Variables = SP->getVariables(); |
| 691 | if (Variables.size() == 0) |
David Blaikie | 1d96cc2 | 2014-10-31 22:30:30 +0000 | [diff] [blame] | 692 | return; |
| 693 | |
David Blaikie | f6dac29 | 2014-11-01 17:21:26 +0000 | [diff] [blame] | 694 | DIE *SPDIE = DU->getAbstractSPDies().lookup(SP); |
David Blaikie | 1d96cc2 | 2014-10-31 22:30:30 +0000 | [diff] [blame] | 695 | if (!SPDIE) |
| 696 | SPDIE = getDIE(SP); |
| 697 | assert(SPDIE); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 698 | for (const DILocalVariable *DV : Variables) { |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 699 | DbgVariable NewVar(DV, /* IA */ nullptr, DD); |
David Blaikie | 1d96cc2 | 2014-10-31 22:30:30 +0000 | [diff] [blame] | 700 | auto VariableDie = constructVariableDIE(NewVar); |
| 701 | applyVariableAttributes(NewVar, *VariableDie); |
| 702 | SPDIE->addChild(std::move(VariableDie)); |
| 703 | } |
| 704 | } |
David Blaikie | 4191cbc | 2014-10-10 06:39:29 +0000 | [diff] [blame] | 705 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 706 | void DwarfCompileUnit::emitHeader(bool UseOffsets) { |
David Blaikie | b6726a9 | 2014-11-02 02:26:24 +0000 | [diff] [blame] | 707 | // Don't bother labeling the .dwo unit, as its offset isn't used. |
Rafael Espindola | 3c31114 | 2015-03-10 03:11:11 +0000 | [diff] [blame] | 708 | if (!Skeleton) { |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 709 | LabelBegin = Asm->createTempSymbol("cu_begin"); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 710 | Asm->OutStreamer->EmitLabel(LabelBegin); |
Rafael Espindola | 3c31114 | 2015-03-10 03:11:11 +0000 | [diff] [blame] | 711 | } |
David Blaikie | ae57e66 | 2014-11-01 23:59:23 +0000 | [diff] [blame] | 712 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 713 | DwarfUnit::emitHeader(UseOffsets); |
David Blaikie | ae57e66 | 2014-11-01 23:59:23 +0000 | [diff] [blame] | 714 | } |
| 715 | |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 716 | /// addGlobalName - Add a new global name to the compile unit. |
| 717 | void DwarfCompileUnit::addGlobalName(StringRef Name, DIE &Die, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 718 | const DIScope *Context) { |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 719 | if (includeMinimalInlineScopes()) |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 720 | return; |
| 721 | std::string FullName = getParentContextString(Context) + Name.str(); |
| 722 | GlobalNames[FullName] = &Die; |
| 723 | } |
| 724 | |
| 725 | /// Add a new global type to the unit. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 726 | void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die, |
| 727 | const DIScope *Context) { |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 728 | if (includeMinimalInlineScopes()) |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 729 | return; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 730 | std::string FullName = getParentContextString(Context) + Ty->getName().str(); |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 731 | GlobalTypes[FullName] = &Die; |
| 732 | } |
David Blaikie | ae57e66 | 2014-11-01 23:59:23 +0000 | [diff] [blame] | 733 | |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 734 | /// addVariableAddress - Add DW_AT_location attribute for a |
| 735 | /// DbgVariable based on provided MachineLocation. |
| 736 | void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, |
| 737 | MachineLocation Location) { |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 738 | if (DV.hasComplexAddress()) |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 739 | addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); |
| 740 | else if (DV.isBlockByrefVariable()) |
| 741 | addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); |
| 742 | else |
Adrian Prantl | 5883af3 | 2015-01-19 17:57:29 +0000 | [diff] [blame] | 743 | addAddress(Die, dwarf::DW_AT_location, Location); |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 744 | } |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 745 | |
| 746 | /// Add an address attribute to a die based on the location provided. |
| 747 | void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, |
Adrian Prantl | 5883af3 | 2015-01-19 17:57:29 +0000 | [diff] [blame] | 748 | const MachineLocation &Location) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 749 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 750 | |
Adrian Prantl | ab255fc | 2014-12-05 01:02:46 +0000 | [diff] [blame] | 751 | bool validReg; |
Adrian Prantl | 5883af3 | 2015-01-19 17:57:29 +0000 | [diff] [blame] | 752 | if (Location.isReg()) |
Adrian Prantl | ab255fc | 2014-12-05 01:02:46 +0000 | [diff] [blame] | 753 | validReg = addRegisterOpPiece(*Loc, Location.getReg()); |
| 754 | else |
| 755 | validReg = addRegisterOffset(*Loc, Location.getReg(), Location.getOffset()); |
| 756 | |
| 757 | if (!validReg) |
| 758 | return; |
| 759 | |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 760 | // Now attach the location information to the DIE. |
| 761 | addBlock(Die, Attribute, Loc); |
| 762 | } |
David Blaikie | 77895fb | 2014-11-02 06:58:44 +0000 | [diff] [blame] | 763 | |
| 764 | /// Start with the address based on the location provided, and generate the |
| 765 | /// DWARF information necessary to find the actual variable given the extra |
| 766 | /// address information encoded in the DbgVariable, starting from the starting |
| 767 | /// location. Add the DWARF information to the die. |
| 768 | void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, |
| 769 | dwarf::Attribute Attribute, |
| 770 | const MachineLocation &Location) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 771 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
Adrian Prantl | 7813d9c | 2015-01-14 01:01:30 +0000 | [diff] [blame] | 772 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 773 | assert(DV.getExpression().size() == 1); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 774 | const DIExpression *Expr = DV.getExpression().back(); |
Adrian Prantl | 40cb819 | 2015-01-25 19:04:08 +0000 | [diff] [blame] | 775 | bool ValidReg; |
Adrian Prantl | 7813d9c | 2015-01-14 01:01:30 +0000 | [diff] [blame] | 776 | if (Location.getOffset()) { |
Adrian Prantl | 40cb819 | 2015-01-25 19:04:08 +0000 | [diff] [blame] | 777 | ValidReg = DwarfExpr.AddMachineRegIndirect(Location.getReg(), |
| 778 | Location.getOffset()); |
| 779 | if (ValidReg) |
Duncan P. N. Exon Smith | 76c9184 | 2015-04-07 03:45:57 +0000 | [diff] [blame] | 780 | DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end()); |
Adrian Prantl | 5883af3 | 2015-01-19 17:57:29 +0000 | [diff] [blame] | 781 | } else |
Adrian Prantl | 40cb819 | 2015-01-25 19:04:08 +0000 | [diff] [blame] | 782 | ValidReg = DwarfExpr.AddMachineRegExpression(Expr, Location.getReg()); |
David Blaikie | 77895fb | 2014-11-02 06:58:44 +0000 | [diff] [blame] | 783 | |
| 784 | // Now attach the location information to the DIE. |
Adrian Prantl | 40cb819 | 2015-01-25 19:04:08 +0000 | [diff] [blame] | 785 | if (ValidReg) |
| 786 | addBlock(Die, Attribute, Loc); |
David Blaikie | 77895fb | 2014-11-02 06:58:44 +0000 | [diff] [blame] | 787 | } |
David Blaikie | 4bc0881 | 2014-11-02 07:03:19 +0000 | [diff] [blame] | 788 | |
| 789 | /// Add a Dwarf loclistptr attribute data and value. |
| 790 | void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, |
| 791 | unsigned Index) { |
David Blaikie | 4bc0881 | 2014-11-02 07:03:19 +0000 | [diff] [blame] | 792 | dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset |
| 793 | : dwarf::DW_FORM_data4; |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 794 | Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index)); |
David Blaikie | 4bc0881 | 2014-11-02 07:03:19 +0000 | [diff] [blame] | 795 | } |
David Blaikie | 02a6333 | 2014-11-02 07:06:51 +0000 | [diff] [blame] | 796 | |
David Blaikie | 8c485b5 | 2014-11-02 07:08:12 +0000 | [diff] [blame] | 797 | void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var, |
| 798 | DIE &VariableDie) { |
David Blaikie | 02a6333 | 2014-11-02 07:06:51 +0000 | [diff] [blame] | 799 | StringRef Name = Var.getName(); |
| 800 | if (!Name.empty()) |
| 801 | addString(VariableDie, dwarf::DW_AT_name, Name); |
| 802 | addSourceLine(VariableDie, Var.getVariable()); |
| 803 | addType(VariableDie, Var.getType()); |
| 804 | if (Var.isArtificial()) |
| 805 | addFlag(VariableDie, dwarf::DW_AT_artificial); |
| 806 | } |
David Blaikie | 9780208 | 2014-11-02 07:11:55 +0000 | [diff] [blame] | 807 | |
| 808 | /// Add a Dwarf expression attribute data and value. |
| 809 | void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form, |
| 810 | const MCExpr *Expr) { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 811 | Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr)); |
David Blaikie | 9780208 | 2014-11-02 07:11:55 +0000 | [diff] [blame] | 812 | } |
David Blaikie | 3363a57 | 2014-11-02 08:09:09 +0000 | [diff] [blame] | 813 | |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 814 | void DwarfCompileUnit::applySubprogramAttributesToDefinition( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 815 | const DISubprogram *SP, DIE &SPDie) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 816 | auto *SPDecl = SP->getDeclaration(); |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 817 | auto *Context = resolve(SPDecl ? SPDecl->getScope() : SP->getScope()); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 818 | applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes()); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 819 | addGlobalName(SP->getName(), SPDie, Context); |
David Blaikie | 3363a57 | 2014-11-02 08:09:09 +0000 | [diff] [blame] | 820 | } |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 821 | |
| 822 | bool DwarfCompileUnit::isDwoUnit() const { |
| 823 | return DD->useSplitDwarf() && Skeleton; |
| 824 | } |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 825 | |
| 826 | bool DwarfCompileUnit::includeMinimalInlineScopes() const { |
Duncan P. N. Exon Smith | 35ef22c | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 827 | return getCUNode()->getEmissionKind() == DIBuilder::LineTablesOnly || |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 828 | (DD->useSplitDwarf() && !Skeleton); |
| 829 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 830 | } // end llvm namespace |