137c52310SDavid Blaikie #include "DwarfCompileUnit.h"
237c52310SDavid Blaikie 
3cda2aa82SDavid Blaikie #include "llvm/CodeGen/MachineFunction.h"
437c52310SDavid Blaikie #include "llvm/IR/DataLayout.h"
537c52310SDavid Blaikie #include "llvm/IR/GlobalValue.h"
637c52310SDavid Blaikie #include "llvm/IR/GlobalVariable.h"
737c52310SDavid Blaikie #include "llvm/IR/Instruction.h"
837c52310SDavid Blaikie #include "llvm/MC/MCAsmInfo.h"
937c52310SDavid Blaikie #include "llvm/MC/MCStreamer.h"
10ee7df553SDavid Blaikie #include "llvm/Target/TargetFrameLowering.h"
1137c52310SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h"
12cda2aa82SDavid Blaikie #include "llvm/Target/TargetMachine.h"
13cda2aa82SDavid Blaikie #include "llvm/Target/TargetSubtargetInfo.h"
14cda2aa82SDavid Blaikie #include "llvm/Target/TargetRegisterInfo.h"
1537c52310SDavid Blaikie 
1637c52310SDavid Blaikie namespace llvm {
1737c52310SDavid Blaikie 
1837c52310SDavid Blaikie DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node,
1937c52310SDavid Blaikie                                    AsmPrinter *A, DwarfDebug *DW,
2037c52310SDavid Blaikie                                    DwarfFile *DWU)
2137c52310SDavid Blaikie     : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) {
2237c52310SDavid Blaikie   insertDIE(Node, &getUnitDie());
2337c52310SDavid Blaikie }
2437c52310SDavid Blaikie 
2537c52310SDavid Blaikie /// addLabelAddress - Add a dwarf label attribute data and value using
2637c52310SDavid Blaikie /// DW_FORM_addr or DW_FORM_GNU_addr_index.
2737c52310SDavid Blaikie ///
2837c52310SDavid Blaikie void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
2937c52310SDavid Blaikie                                        const MCSymbol *Label) {
3037c52310SDavid Blaikie 
3137c52310SDavid Blaikie   // Don't use the address pool in non-fission or in the skeleton unit itself.
3237c52310SDavid Blaikie   // FIXME: Once GDB supports this, it's probably worthwhile using the address
3337c52310SDavid Blaikie   // pool from the skeleton - maybe even in non-fission (possibly fewer
3437c52310SDavid Blaikie   // relocations by sharing them in the pool, but we have other ideas about how
3537c52310SDavid Blaikie   // to reduce the number of relocations as well/instead).
3637c52310SDavid Blaikie   if (!DD->useSplitDwarf() || !Skeleton)
3737c52310SDavid Blaikie     return addLocalLabelAddress(Die, Attribute, Label);
3837c52310SDavid Blaikie 
3937c52310SDavid Blaikie   if (Label)
4037c52310SDavid Blaikie     DD->addArangeLabel(SymbolCU(this, Label));
4137c52310SDavid Blaikie 
4237c52310SDavid Blaikie   unsigned idx = DD->getAddressPool().getIndex(Label);
4337c52310SDavid Blaikie   DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
4437c52310SDavid Blaikie   Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
4537c52310SDavid Blaikie }
4637c52310SDavid Blaikie 
4737c52310SDavid Blaikie void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
4837c52310SDavid Blaikie                                             dwarf::Attribute Attribute,
4937c52310SDavid Blaikie                                             const MCSymbol *Label) {
5037c52310SDavid Blaikie   if (Label)
5137c52310SDavid Blaikie     DD->addArangeLabel(SymbolCU(this, Label));
5237c52310SDavid Blaikie 
5337c52310SDavid Blaikie   Die.addValue(Attribute, dwarf::DW_FORM_addr,
5437c52310SDavid Blaikie                Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label)
5537c52310SDavid Blaikie                      : new (DIEValueAllocator) DIEInteger(0));
5637c52310SDavid Blaikie }
5737c52310SDavid Blaikie 
588945219dSDavid Blaikie unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName,
598945219dSDavid Blaikie                                                StringRef DirName) {
6037c52310SDavid Blaikie   // If we print assembly, we can't separate .file entries according to
6137c52310SDavid Blaikie   // compile units. Thus all files will belong to the default compile unit.
6237c52310SDavid Blaikie 
6337c52310SDavid Blaikie   // FIXME: add a better feature test than hasRawTextSupport. Even better,
6437c52310SDavid Blaikie   // extend .file to support this.
6537c52310SDavid Blaikie   return Asm->OutStreamer.EmitDwarfFileDirective(
6637c52310SDavid Blaikie       0, DirName, FileName,
6737c52310SDavid Blaikie       Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID());
6837c52310SDavid Blaikie }
6937c52310SDavid Blaikie 
7037c52310SDavid Blaikie // Return const expression if value is a GEP to access merged global
7137c52310SDavid Blaikie // constant. e.g.
7237c52310SDavid Blaikie // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
7337c52310SDavid Blaikie static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
7437c52310SDavid Blaikie   const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
7537c52310SDavid Blaikie   if (!CE || CE->getNumOperands() != 3 ||
7637c52310SDavid Blaikie       CE->getOpcode() != Instruction::GetElementPtr)
7737c52310SDavid Blaikie     return nullptr;
7837c52310SDavid Blaikie 
7937c52310SDavid Blaikie   // First operand points to a global struct.
8037c52310SDavid Blaikie   Value *Ptr = CE->getOperand(0);
8137c52310SDavid Blaikie   if (!isa<GlobalValue>(Ptr) ||
8237c52310SDavid Blaikie       !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
8337c52310SDavid Blaikie     return nullptr;
8437c52310SDavid Blaikie 
8537c52310SDavid Blaikie   // Second operand is zero.
8637c52310SDavid Blaikie   const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
8737c52310SDavid Blaikie   if (!CI || !CI->isZero())
8837c52310SDavid Blaikie     return nullptr;
8937c52310SDavid Blaikie 
9037c52310SDavid Blaikie   // Third operand is offset.
9137c52310SDavid Blaikie   if (!isa<ConstantInt>(CE->getOperand(2)))
9237c52310SDavid Blaikie     return nullptr;
9337c52310SDavid Blaikie 
9437c52310SDavid Blaikie   return CE;
9537c52310SDavid Blaikie }
9637c52310SDavid Blaikie 
9737c52310SDavid Blaikie /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
9837c52310SDavid Blaikie DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) {
9937c52310SDavid Blaikie   // Check for pre-existence.
10037c52310SDavid Blaikie   if (DIE *Die = getDIE(GV))
10137c52310SDavid Blaikie     return Die;
10237c52310SDavid Blaikie 
10337c52310SDavid Blaikie   assert(GV.isGlobalVariable());
10437c52310SDavid Blaikie 
10537c52310SDavid Blaikie   DIScope GVContext = GV.getContext();
10637c52310SDavid Blaikie   DIType GTy = DD->resolve(GV.getType());
10737c52310SDavid Blaikie 
10837c52310SDavid Blaikie   // If this is a static data member definition, some attributes belong
10937c52310SDavid Blaikie   // to the declaration DIE.
11037c52310SDavid Blaikie   DIE *VariableDIE = nullptr;
11137c52310SDavid Blaikie   bool IsStaticMember = false;
11237c52310SDavid Blaikie   DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
11337c52310SDavid Blaikie   if (SDMDecl.Verify()) {
11437c52310SDavid Blaikie     assert(SDMDecl.isStaticMember() && "Expected static member decl");
11537c52310SDavid Blaikie     // We need the declaration DIE that is in the static member's class.
11637c52310SDavid Blaikie     VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
11737c52310SDavid Blaikie     IsStaticMember = true;
11837c52310SDavid Blaikie   }
11937c52310SDavid Blaikie 
12037c52310SDavid Blaikie   // If this is not a static data member definition, create the variable
12137c52310SDavid Blaikie   // DIE and add the initial set of attributes to it.
12237c52310SDavid Blaikie   if (!VariableDIE) {
12337c52310SDavid Blaikie     // Construct the context before querying for the existence of the DIE in
12437c52310SDavid Blaikie     // case such construction creates the DIE.
12537c52310SDavid Blaikie     DIE *ContextDIE = getOrCreateContextDIE(GVContext);
12637c52310SDavid Blaikie 
12737c52310SDavid Blaikie     // Add to map.
12837c52310SDavid Blaikie     VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV);
12937c52310SDavid Blaikie 
13037c52310SDavid Blaikie     // Add name and type.
13137c52310SDavid Blaikie     addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
13237c52310SDavid Blaikie     addType(*VariableDIE, GTy);
13337c52310SDavid Blaikie 
13437c52310SDavid Blaikie     // Add scoping info.
13537c52310SDavid Blaikie     if (!GV.isLocalToUnit())
13637c52310SDavid Blaikie       addFlag(*VariableDIE, dwarf::DW_AT_external);
13737c52310SDavid Blaikie 
13837c52310SDavid Blaikie     // Add line number info.
13937c52310SDavid Blaikie     addSourceLine(*VariableDIE, GV);
14037c52310SDavid Blaikie   }
14137c52310SDavid Blaikie 
14237c52310SDavid Blaikie   // Add location.
14337c52310SDavid Blaikie   bool addToAccelTable = false;
14437c52310SDavid Blaikie   DIE *VariableSpecDIE = nullptr;
14537c52310SDavid Blaikie   bool isGlobalVariable = GV.getGlobal() != nullptr;
14637c52310SDavid Blaikie   if (isGlobalVariable) {
14737c52310SDavid Blaikie     addToAccelTable = true;
14837c52310SDavid Blaikie     DIELoc *Loc = new (DIEValueAllocator) DIELoc();
14937c52310SDavid Blaikie     const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
15037c52310SDavid Blaikie     if (GV.getGlobal()->isThreadLocal()) {
15137c52310SDavid Blaikie       // FIXME: Make this work with -gsplit-dwarf.
15237c52310SDavid Blaikie       unsigned PointerSize = Asm->getDataLayout().getPointerSize();
15337c52310SDavid Blaikie       assert((PointerSize == 4 || PointerSize == 8) &&
15437c52310SDavid Blaikie              "Add support for other sizes if necessary");
15537c52310SDavid Blaikie       // Based on GCC's support for TLS:
15637c52310SDavid Blaikie       if (!DD->useSplitDwarf()) {
15737c52310SDavid Blaikie         // 1) Start with a constNu of the appropriate pointer size
15837c52310SDavid Blaikie         addUInt(*Loc, dwarf::DW_FORM_data1,
15937c52310SDavid Blaikie                 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
16037c52310SDavid Blaikie         // 2) containing the (relocated) offset of the TLS variable
16137c52310SDavid Blaikie         //    within the module's TLS block.
16237c52310SDavid Blaikie         addExpr(*Loc, dwarf::DW_FORM_udata,
16337c52310SDavid Blaikie                 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
16437c52310SDavid Blaikie       } else {
16537c52310SDavid Blaikie         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
16637c52310SDavid Blaikie         addUInt(*Loc, dwarf::DW_FORM_udata,
16737c52310SDavid Blaikie                 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
16837c52310SDavid Blaikie       }
16937c52310SDavid Blaikie       // 3) followed by a custom OP to make the debugger do a TLS lookup.
17037c52310SDavid Blaikie       addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
17137c52310SDavid Blaikie     } else {
17237c52310SDavid Blaikie       DD->addArangeLabel(SymbolCU(this, Sym));
17337c52310SDavid Blaikie       addOpAddress(*Loc, Sym);
17437c52310SDavid Blaikie     }
17537c52310SDavid Blaikie     // A static member's declaration is already flagged as such.
17637c52310SDavid Blaikie     if (!SDMDecl.Verify() && !GV.isDefinition())
17737c52310SDavid Blaikie       addFlag(*VariableDIE, dwarf::DW_AT_declaration);
17837c52310SDavid Blaikie     // Do not create specification DIE if context is either compile unit
17937c52310SDavid Blaikie     // or a subprogram.
18037c52310SDavid Blaikie     if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
18137c52310SDavid Blaikie         !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
18237c52310SDavid Blaikie       // Create specification DIE.
18337c52310SDavid Blaikie       VariableSpecDIE = &createAndAddDIE(dwarf::DW_TAG_variable, UnitDie);
18437c52310SDavid Blaikie       addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, *VariableDIE);
18537c52310SDavid Blaikie       addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc);
18637c52310SDavid Blaikie       // A static member's declaration is already flagged as such.
18737c52310SDavid Blaikie       if (!SDMDecl.Verify())
18837c52310SDavid Blaikie         addFlag(*VariableDIE, dwarf::DW_AT_declaration);
18937c52310SDavid Blaikie     } else {
19037c52310SDavid Blaikie       addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
19137c52310SDavid Blaikie     }
19237c52310SDavid Blaikie     // Add the linkage name.
19337c52310SDavid Blaikie     StringRef LinkageName = GV.getLinkageName();
19437c52310SDavid Blaikie     if (!LinkageName.empty())
19537c52310SDavid Blaikie       // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
19637c52310SDavid Blaikie       // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
19737c52310SDavid Blaikie       // TAG_variable.
19837c52310SDavid Blaikie       addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE
19937c52310SDavid Blaikie                                                   : *VariableDIE,
20037c52310SDavid Blaikie                 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
20137c52310SDavid Blaikie                                            : dwarf::DW_AT_MIPS_linkage_name,
20237c52310SDavid Blaikie                 GlobalValue::getRealLinkageName(LinkageName));
20337c52310SDavid Blaikie   } else if (const ConstantInt *CI =
20437c52310SDavid Blaikie                  dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
20537c52310SDavid Blaikie     // AT_const_value was added when the static member was created. To avoid
20637c52310SDavid Blaikie     // emitting AT_const_value multiple times, we only add AT_const_value when
20737c52310SDavid Blaikie     // it is not a static member.
20837c52310SDavid Blaikie     if (!IsStaticMember)
20937c52310SDavid Blaikie       addConstantValue(*VariableDIE, CI, GTy);
21037c52310SDavid Blaikie   } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV.getConstant())) {
21137c52310SDavid Blaikie     addToAccelTable = true;
21237c52310SDavid Blaikie     // GV is a merged global.
21337c52310SDavid Blaikie     DIELoc *Loc = new (DIEValueAllocator) DIELoc();
21437c52310SDavid Blaikie     Value *Ptr = CE->getOperand(0);
21537c52310SDavid Blaikie     MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
21637c52310SDavid Blaikie     DD->addArangeLabel(SymbolCU(this, Sym));
21737c52310SDavid Blaikie     addOpAddress(*Loc, Sym);
21837c52310SDavid Blaikie     addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
21937c52310SDavid Blaikie     SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
22037c52310SDavid Blaikie     addUInt(*Loc, dwarf::DW_FORM_udata,
22137c52310SDavid Blaikie             Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
22237c52310SDavid Blaikie     addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
22337c52310SDavid Blaikie     addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
22437c52310SDavid Blaikie   }
22537c52310SDavid Blaikie 
22637c52310SDavid Blaikie   DIE *ResultDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
22737c52310SDavid Blaikie 
22837c52310SDavid Blaikie   if (addToAccelTable) {
22937c52310SDavid Blaikie     DD->addAccelName(GV.getName(), *ResultDIE);
23037c52310SDavid Blaikie 
23137c52310SDavid Blaikie     // If the linkage name is different than the name, go ahead and output
23237c52310SDavid Blaikie     // that as well into the name table.
23337c52310SDavid Blaikie     if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
23437c52310SDavid Blaikie       DD->addAccelName(GV.getLinkageName(), *ResultDIE);
23537c52310SDavid Blaikie   }
23637c52310SDavid Blaikie 
23737c52310SDavid Blaikie   addGlobalName(GV.getName(), *ResultDIE, GV.getContext());
23837c52310SDavid Blaikie   return ResultDIE;
23937c52310SDavid Blaikie }
24037c52310SDavid Blaikie 
24137c52310SDavid Blaikie void DwarfCompileUnit::addRange(RangeSpan Range) {
24237c52310SDavid Blaikie   bool SameAsPrevCU = this == DD->getPrevCU();
24337c52310SDavid Blaikie   DD->setPrevCU(this);
24437c52310SDavid Blaikie   // If we have no current ranges just add the range and return, otherwise,
24537c52310SDavid Blaikie   // check the current section and CU against the previous section and CU we
24637c52310SDavid Blaikie   // emitted into and the subprogram was contained within. If these are the
24737c52310SDavid Blaikie   // same then extend our current range, otherwise add this as a new range.
24837c52310SDavid Blaikie   if (CURanges.empty() || !SameAsPrevCU ||
24937c52310SDavid Blaikie       (&CURanges.back().getEnd()->getSection() !=
25037c52310SDavid Blaikie        &Range.getEnd()->getSection())) {
25137c52310SDavid Blaikie     CURanges.push_back(Range);
25237c52310SDavid Blaikie     return;
25337c52310SDavid Blaikie   }
25437c52310SDavid Blaikie 
25537c52310SDavid Blaikie   CURanges.back().setEnd(Range.getEnd());
25637c52310SDavid Blaikie }
25737c52310SDavid Blaikie 
2586c0ee4ecSDavid Blaikie void DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
2596c0ee4ecSDavid Blaikie                                        const MCSymbol *Label,
2606c0ee4ecSDavid Blaikie                                        const MCSymbol *Sec) {
2616c0ee4ecSDavid Blaikie   if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2626c0ee4ecSDavid Blaikie     addLabel(Die, Attribute,
2636c0ee4ecSDavid Blaikie              DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
2646c0ee4ecSDavid Blaikie                                         : dwarf::DW_FORM_data4,
2656c0ee4ecSDavid Blaikie              Label);
2666c0ee4ecSDavid Blaikie   else
2676c0ee4ecSDavid Blaikie     addSectionDelta(Die, Attribute, Label, Sec);
2686c0ee4ecSDavid Blaikie }
2696c0ee4ecSDavid Blaikie 
27037c52310SDavid Blaikie void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
27137c52310SDavid Blaikie   // Define start line table label for each Compile Unit.
27237c52310SDavid Blaikie   MCSymbol *LineTableStartSym =
27337c52310SDavid Blaikie       Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID());
27437c52310SDavid Blaikie 
27537c52310SDavid Blaikie   stmtListIndex = UnitDie.getValues().size();
27637c52310SDavid Blaikie 
27737c52310SDavid Blaikie   // DW_AT_stmt_list is a offset of line number information for this
27837c52310SDavid Blaikie   // compile unit in debug_line section. For split dwarf this is
27937c52310SDavid Blaikie   // left in the skeleton CU and so not included.
28037c52310SDavid Blaikie   // The line table entries are not always emitted in assembly, so it
28137c52310SDavid Blaikie   // is not okay to use line_table_start here.
2826c0ee4ecSDavid Blaikie   addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
28337c52310SDavid Blaikie                   DwarfLineSectionSym);
28437c52310SDavid Blaikie }
28537c52310SDavid Blaikie 
28637c52310SDavid Blaikie void DwarfCompileUnit::applyStmtList(DIE &D) {
28737c52310SDavid Blaikie   D.addValue(dwarf::DW_AT_stmt_list,
28837c52310SDavid Blaikie              UnitDie.getAbbrev().getData()[stmtListIndex].getForm(),
28937c52310SDavid Blaikie              UnitDie.getValues()[stmtListIndex]);
29037c52310SDavid Blaikie }
29137c52310SDavid Blaikie 
29214499a7dSDavid Blaikie void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
29314499a7dSDavid Blaikie                                        const MCSymbol *End) {
29414499a7dSDavid Blaikie   assert(Begin && "Begin label should not be null!");
29514499a7dSDavid Blaikie   assert(End && "End label should not be null!");
29614499a7dSDavid Blaikie   assert(Begin->isDefined() && "Invalid starting label");
29714499a7dSDavid Blaikie   assert(End->isDefined() && "Invalid end label");
29814499a7dSDavid Blaikie 
29914499a7dSDavid Blaikie   addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
30014499a7dSDavid Blaikie   if (DD->getDwarfVersion() < 4)
30114499a7dSDavid Blaikie     addLabelAddress(D, dwarf::DW_AT_high_pc, End);
30214499a7dSDavid Blaikie   else
30314499a7dSDavid Blaikie     addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
30414499a7dSDavid Blaikie }
30514499a7dSDavid Blaikie 
306cda2aa82SDavid Blaikie // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
307cda2aa82SDavid Blaikie // and DW_AT_high_pc attributes. If there are global variables in this
308cda2aa82SDavid Blaikie // scope then create and insert DIEs for these variables.
309cda2aa82SDavid Blaikie DIE &DwarfCompileUnit::updateSubprogramScopeDIE(DISubprogram SP) {
310cda2aa82SDavid Blaikie   DIE *SPDie = getOrCreateSubprogramDIE(SP);
311cda2aa82SDavid Blaikie 
312cda2aa82SDavid Blaikie   attachLowHighPC(*SPDie, DD->getFunctionBeginSym(), DD->getFunctionEndSym());
313cda2aa82SDavid Blaikie   if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
314cda2aa82SDavid Blaikie           *DD->getCurrentFunction()))
315cda2aa82SDavid Blaikie     addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
316cda2aa82SDavid Blaikie 
317cda2aa82SDavid Blaikie   // Only include DW_AT_frame_base in full debug info
318cda2aa82SDavid Blaikie   if (getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) {
319cda2aa82SDavid Blaikie     const TargetRegisterInfo *RI =
320cda2aa82SDavid Blaikie         Asm->TM.getSubtargetImpl()->getRegisterInfo();
321cda2aa82SDavid Blaikie     MachineLocation Location(RI->getFrameRegister(*Asm->MF));
322cda2aa82SDavid Blaikie     addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
323cda2aa82SDavid Blaikie   }
324cda2aa82SDavid Blaikie 
325cda2aa82SDavid Blaikie   // Add name to the name table, we do this here because we're guaranteed
326cda2aa82SDavid Blaikie   // to have concrete versions of our DW_TAG_subprogram nodes.
327cda2aa82SDavid Blaikie   DD->addSubprogramNames(SP, *SPDie);
328cda2aa82SDavid Blaikie 
329cda2aa82SDavid Blaikie   return *SPDie;
330cda2aa82SDavid Blaikie }
331cda2aa82SDavid Blaikie 
3329c65b135SDavid Blaikie // Construct a DIE for this scope.
3339c65b135SDavid Blaikie void DwarfCompileUnit::constructScopeDIE(
3349c65b135SDavid Blaikie     LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &FinalChildren) {
3359c65b135SDavid Blaikie   if (!Scope || !Scope->getScopeNode())
3369c65b135SDavid Blaikie     return;
3379c65b135SDavid Blaikie 
3389c65b135SDavid Blaikie   DIScope DS(Scope->getScopeNode());
3399c65b135SDavid Blaikie 
3409c65b135SDavid Blaikie   assert((Scope->getInlinedAt() || !DS.isSubprogram()) &&
3419c65b135SDavid Blaikie          "Only handle inlined subprograms here, use "
3429c65b135SDavid Blaikie          "constructSubprogramScopeDIE for non-inlined "
3439c65b135SDavid Blaikie          "subprograms");
3449c65b135SDavid Blaikie 
3459c65b135SDavid Blaikie   SmallVector<std::unique_ptr<DIE>, 8> Children;
3469c65b135SDavid Blaikie 
3479c65b135SDavid Blaikie   // We try to create the scope DIE first, then the children DIEs. This will
3489c65b135SDavid Blaikie   // avoid creating un-used children then removing them later when we find out
3499c65b135SDavid Blaikie   // the scope DIE is null.
3509c65b135SDavid Blaikie   std::unique_ptr<DIE> ScopeDIE;
3519c65b135SDavid Blaikie   if (Scope->getParent() && DS.isSubprogram()) {
35201b48a84SDavid Blaikie     ScopeDIE = constructInlinedScopeDIE(Scope);
3539c65b135SDavid Blaikie     if (!ScopeDIE)
3549c65b135SDavid Blaikie       return;
3559c65b135SDavid Blaikie     // We create children when the scope DIE is not null.
356*8b2fdb83SDavid Blaikie     createScopeChildrenDIE(Scope, Children);
3579c65b135SDavid Blaikie   } else {
3589c65b135SDavid Blaikie     // Early exit when we know the scope DIE is going to be null.
3599c65b135SDavid Blaikie     if (DD->isLexicalScopeDIENull(Scope))
3609c65b135SDavid Blaikie       return;
3619c65b135SDavid Blaikie 
3629c65b135SDavid Blaikie     unsigned ChildScopeCount;
3639c65b135SDavid Blaikie 
3649c65b135SDavid Blaikie     // We create children here when we know the scope DIE is not going to be
3659c65b135SDavid Blaikie     // null and the children will be added to the scope DIE.
366*8b2fdb83SDavid Blaikie     createScopeChildrenDIE(Scope, Children, &ChildScopeCount);
3679c65b135SDavid Blaikie 
3689c65b135SDavid Blaikie     // There is no need to emit empty lexical block DIE.
3699c65b135SDavid Blaikie     for (const auto &E : DD->findImportedEntitiesForScope(DS))
3709c65b135SDavid Blaikie       Children.push_back(
3719c65b135SDavid Blaikie           constructImportedEntityDIE(DIImportedEntity(E.second)));
3729c65b135SDavid Blaikie     // If there are only other scopes as children, put them directly in the
3739c65b135SDavid Blaikie     // parent instead, as this scope would serve no purpose.
3749c65b135SDavid Blaikie     if (Children.size() == ChildScopeCount) {
3759c65b135SDavid Blaikie       FinalChildren.insert(FinalChildren.end(),
3769c65b135SDavid Blaikie                            std::make_move_iterator(Children.begin()),
3779c65b135SDavid Blaikie                            std::make_move_iterator(Children.end()));
3789c65b135SDavid Blaikie       return;
3799c65b135SDavid Blaikie     }
3800fbf8bdbSDavid Blaikie     ScopeDIE = constructLexicalScopeDIE(Scope);
3819c65b135SDavid Blaikie     assert(ScopeDIE && "Scope DIE should not be null.");
3829c65b135SDavid Blaikie   }
3839c65b135SDavid Blaikie 
3849c65b135SDavid Blaikie   // Add children
3859c65b135SDavid Blaikie   for (auto &I : Children)
3869c65b135SDavid Blaikie     ScopeDIE->addChild(std::move(I));
3879c65b135SDavid Blaikie 
3889c65b135SDavid Blaikie   FinalChildren.push_back(std::move(ScopeDIE));
3899c65b135SDavid Blaikie }
3909c65b135SDavid Blaikie 
391e5feec50SDavid Blaikie void DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
392e5feec50SDavid Blaikie                                        const MCSymbol *Hi, const MCSymbol *Lo) {
393e5feec50SDavid Blaikie   DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
394e5feec50SDavid Blaikie   Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
395e5feec50SDavid Blaikie                                                      : dwarf::DW_FORM_data4,
396e5feec50SDavid Blaikie                Value);
397e5feec50SDavid Blaikie }
398e5feec50SDavid Blaikie 
39952400200SDavid Blaikie void
40052400200SDavid Blaikie DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
40152400200SDavid Blaikie                                     const SmallVectorImpl<InsnRange> &Range) {
40252400200SDavid Blaikie   // Emit offset in .debug_range as a relocatable label. emitDIE will handle
40352400200SDavid Blaikie   // emitting it appropriately.
40452400200SDavid Blaikie   MCSymbol *RangeSym =
40552400200SDavid Blaikie       Asm->GetTempSymbol("debug_ranges", DD->getNextRangeNumber());
40652400200SDavid Blaikie 
40752400200SDavid Blaikie   auto *RangeSectionSym = DD->getRangeSectionSym();
40852400200SDavid Blaikie 
40952400200SDavid Blaikie   // Under fission, ranges are specified by constant offsets relative to the
41052400200SDavid Blaikie   // CU's DW_AT_GNU_ranges_base.
41152400200SDavid Blaikie   if (DD->useSplitDwarf())
41252400200SDavid Blaikie     addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, RangeSym, RangeSectionSym);
41352400200SDavid Blaikie   else
41452400200SDavid Blaikie     addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, RangeSym, RangeSectionSym);
41552400200SDavid Blaikie 
41652400200SDavid Blaikie   RangeSpanList List(RangeSym);
41752400200SDavid Blaikie   for (const InsnRange &R : Range)
41852400200SDavid Blaikie     List.addRange(RangeSpan(DD->getLabelBeforeInsn(R.first),
41952400200SDavid Blaikie                             DD->getLabelAfterInsn(R.second)));
42052400200SDavid Blaikie 
42152400200SDavid Blaikie   // Add the range list to the set of ranges to be emitted.
42252400200SDavid Blaikie   addRangeList(std::move(List));
42352400200SDavid Blaikie }
42452400200SDavid Blaikie 
425de12375cSDavid Blaikie void DwarfCompileUnit::attachRangesOrLowHighPC(
426de12375cSDavid Blaikie     DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) {
427de12375cSDavid Blaikie   assert(!Ranges.empty());
428de12375cSDavid Blaikie   if (Ranges.size() == 1)
429de12375cSDavid Blaikie     attachLowHighPC(Die, DD->getLabelBeforeInsn(Ranges.front().first),
430de12375cSDavid Blaikie                     DD->getLabelAfterInsn(Ranges.front().second));
431de12375cSDavid Blaikie   else
432de12375cSDavid Blaikie     addScopeRangeList(Die, Ranges);
433de12375cSDavid Blaikie }
434de12375cSDavid Blaikie 
43501b48a84SDavid Blaikie // This scope represents inlined body of a function. Construct DIE to
43601b48a84SDavid Blaikie // represent this concrete inlined copy of the function.
43701b48a84SDavid Blaikie std::unique_ptr<DIE>
43801b48a84SDavid Blaikie DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) {
43901b48a84SDavid Blaikie   assert(Scope->getScopeNode());
44001b48a84SDavid Blaikie   DIScope DS(Scope->getScopeNode());
44101b48a84SDavid Blaikie   DISubprogram InlinedSP = getDISubprogram(DS);
44201b48a84SDavid Blaikie   // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
44301b48a84SDavid Blaikie   // was inlined from another compile unit.
44401b48a84SDavid Blaikie   DIE *OriginDIE = DD->getAbstractSPDies()[InlinedSP];
44501b48a84SDavid Blaikie   assert(OriginDIE && "Unable to find original DIE for an inlined subprogram.");
44601b48a84SDavid Blaikie 
44701b48a84SDavid Blaikie   auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_inlined_subroutine);
44801b48a84SDavid Blaikie   addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE);
44901b48a84SDavid Blaikie 
45001b48a84SDavid Blaikie   attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
45101b48a84SDavid Blaikie 
45201b48a84SDavid Blaikie   // Add the call site information to the DIE.
45301b48a84SDavid Blaikie   DILocation DL(Scope->getInlinedAt());
45401b48a84SDavid Blaikie   addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None,
45501b48a84SDavid Blaikie           getOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
45601b48a84SDavid Blaikie   addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
45701b48a84SDavid Blaikie 
45801b48a84SDavid Blaikie   // Add name to the name table, we do this here because we're guaranteed
45901b48a84SDavid Blaikie   // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
46001b48a84SDavid Blaikie   DD->addSubprogramNames(InlinedSP, *ScopeDIE);
46101b48a84SDavid Blaikie 
46201b48a84SDavid Blaikie   return ScopeDIE;
46301b48a84SDavid Blaikie }
46401b48a84SDavid Blaikie 
4650fbf8bdbSDavid Blaikie // Construct new DW_TAG_lexical_block for this scope and attach
4660fbf8bdbSDavid Blaikie // DW_AT_low_pc/DW_AT_high_pc labels.
4670fbf8bdbSDavid Blaikie std::unique_ptr<DIE>
4680fbf8bdbSDavid Blaikie DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
4690fbf8bdbSDavid Blaikie   if (DD->isLexicalScopeDIENull(Scope))
4700fbf8bdbSDavid Blaikie     return nullptr;
4710fbf8bdbSDavid Blaikie 
4720fbf8bdbSDavid Blaikie   auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_lexical_block);
4730fbf8bdbSDavid Blaikie   if (Scope->isAbstractScope())
4740fbf8bdbSDavid Blaikie     return ScopeDIE;
4750fbf8bdbSDavid Blaikie 
4760fbf8bdbSDavid Blaikie   attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
4770fbf8bdbSDavid Blaikie 
4780fbf8bdbSDavid Blaikie   return ScopeDIE;
4790fbf8bdbSDavid Blaikie }
4800fbf8bdbSDavid Blaikie 
481ee7df553SDavid Blaikie /// constructVariableDIE - Construct a DIE for the given DbgVariable.
482ee7df553SDavid Blaikie std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE(DbgVariable &DV,
483ee7df553SDavid Blaikie                                                             bool Abstract) {
484ee7df553SDavid Blaikie   auto D = constructVariableDIEImpl(DV, Abstract);
485ee7df553SDavid Blaikie   DV.setDIE(*D);
486ee7df553SDavid Blaikie   return D;
487ee7df553SDavid Blaikie }
488ee7df553SDavid Blaikie 
489ee7df553SDavid Blaikie std::unique_ptr<DIE>
490ee7df553SDavid Blaikie DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
491ee7df553SDavid Blaikie                                            bool Abstract) {
492ee7df553SDavid Blaikie   // Define variable debug information entry.
493ee7df553SDavid Blaikie   auto VariableDie = make_unique<DIE>(DV.getTag());
494ee7df553SDavid Blaikie 
495ee7df553SDavid Blaikie   if (Abstract) {
496ee7df553SDavid Blaikie     applyVariableAttributes(DV, *VariableDie);
497ee7df553SDavid Blaikie     return VariableDie;
498ee7df553SDavid Blaikie   }
499ee7df553SDavid Blaikie 
500ee7df553SDavid Blaikie   // Add variable address.
501ee7df553SDavid Blaikie 
502ee7df553SDavid Blaikie   unsigned Offset = DV.getDotDebugLocOffset();
503ee7df553SDavid Blaikie   if (Offset != ~0U) {
504ee7df553SDavid Blaikie     addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
505ee7df553SDavid Blaikie     return VariableDie;
506ee7df553SDavid Blaikie   }
507ee7df553SDavid Blaikie 
508ee7df553SDavid Blaikie   // Check if variable is described by a DBG_VALUE instruction.
509ee7df553SDavid Blaikie   if (const MachineInstr *DVInsn = DV.getMInsn()) {
510ee7df553SDavid Blaikie     assert(DVInsn->getNumOperands() == 4);
511ee7df553SDavid Blaikie     if (DVInsn->getOperand(0).isReg()) {
512ee7df553SDavid Blaikie       const MachineOperand RegOp = DVInsn->getOperand(0);
513ee7df553SDavid Blaikie       // If the second operand is an immediate, this is an indirect value.
514ee7df553SDavid Blaikie       if (DVInsn->getOperand(1).isImm()) {
515ee7df553SDavid Blaikie         MachineLocation Location(RegOp.getReg(),
516ee7df553SDavid Blaikie                                  DVInsn->getOperand(1).getImm());
517ee7df553SDavid Blaikie         addVariableAddress(DV, *VariableDie, Location);
518ee7df553SDavid Blaikie       } else if (RegOp.getReg())
519ee7df553SDavid Blaikie         addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg()));
520ee7df553SDavid Blaikie     } else if (DVInsn->getOperand(0).isImm())
521ee7df553SDavid Blaikie       addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType());
522ee7df553SDavid Blaikie     else if (DVInsn->getOperand(0).isFPImm())
523ee7df553SDavid Blaikie       addConstantFPValue(*VariableDie, DVInsn->getOperand(0));
524ee7df553SDavid Blaikie     else if (DVInsn->getOperand(0).isCImm())
525ee7df553SDavid Blaikie       addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(),
526ee7df553SDavid Blaikie                        DV.getType());
527ee7df553SDavid Blaikie 
528ee7df553SDavid Blaikie     return VariableDie;
529ee7df553SDavid Blaikie   }
530ee7df553SDavid Blaikie 
531ee7df553SDavid Blaikie   // .. else use frame index.
532ee7df553SDavid Blaikie   int FI = DV.getFrameIndex();
533ee7df553SDavid Blaikie   if (FI != ~0) {
534ee7df553SDavid Blaikie     unsigned FrameReg = 0;
535ee7df553SDavid Blaikie     const TargetFrameLowering *TFI =
536ee7df553SDavid Blaikie         Asm->TM.getSubtargetImpl()->getFrameLowering();
537ee7df553SDavid Blaikie     int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
538ee7df553SDavid Blaikie     MachineLocation Location(FrameReg, Offset);
539ee7df553SDavid Blaikie     addVariableAddress(DV, *VariableDie, Location);
540ee7df553SDavid Blaikie   }
541ee7df553SDavid Blaikie 
542ee7df553SDavid Blaikie   return VariableDie;
543ee7df553SDavid Blaikie }
544ee7df553SDavid Blaikie 
5454a1a44e3SDavid Blaikie std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE(
5464a1a44e3SDavid Blaikie     DbgVariable &DV, const LexicalScope &Scope, DIE *&ObjectPointer) {
5474a1a44e3SDavid Blaikie   auto Var = constructVariableDIE(DV, Scope.isAbstractScope());
5484a1a44e3SDavid Blaikie   if (DV.isObjectPointer())
5494a1a44e3SDavid Blaikie     ObjectPointer = Var.get();
5504a1a44e3SDavid Blaikie   return Var;
5514a1a44e3SDavid Blaikie }
5524a1a44e3SDavid Blaikie 
553*8b2fdb83SDavid Blaikie DIE *DwarfCompileUnit::createScopeChildrenDIE(
554*8b2fdb83SDavid Blaikie     LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &Children,
555*8b2fdb83SDavid Blaikie     unsigned *ChildScopeCount) {
556*8b2fdb83SDavid Blaikie   DIE *ObjectPointer = nullptr;
557*8b2fdb83SDavid Blaikie 
558*8b2fdb83SDavid Blaikie   for (DbgVariable *DV : DD->getScopeVariables().lookup(Scope))
559*8b2fdb83SDavid Blaikie     Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer));
560*8b2fdb83SDavid Blaikie 
561*8b2fdb83SDavid Blaikie   unsigned ChildCountWithoutScopes = Children.size();
562*8b2fdb83SDavid Blaikie 
563*8b2fdb83SDavid Blaikie   for (LexicalScope *LS : Scope->getChildren())
564*8b2fdb83SDavid Blaikie     constructScopeDIE(LS, Children);
565*8b2fdb83SDavid Blaikie 
566*8b2fdb83SDavid Blaikie   if (ChildScopeCount)
567*8b2fdb83SDavid Blaikie     *ChildScopeCount = Children.size() - ChildCountWithoutScopes;
568*8b2fdb83SDavid Blaikie 
569*8b2fdb83SDavid Blaikie   return ObjectPointer;
570*8b2fdb83SDavid Blaikie }
571*8b2fdb83SDavid Blaikie 
57237c52310SDavid Blaikie } // end llvm namespace
573