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