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