blob: 54c0419ab4a42ba267e9c9aa97e8346d025cf9c0 [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Devang Patel5eb43192011-04-12 17:40:32 +000017#include "DwarfCompileUnit.h"
Bill Wendling30346342010-04-05 22:59:21 +000018#include "llvm/Constants.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000019#include "llvm/Module.h"
Devang Patel2d9e5322011-01-20 00:02:16 +000020#include "llvm/Instructions.h"
David Greene829b3e82009-08-19 21:52:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000023#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000024#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000025#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000026#include "llvm/MC/MCSymbol.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000027#include "llvm/Target/TargetData.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000028#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000033#include "llvm/Analysis/DebugInfo.h"
Devang Patela5d93242011-02-24 18:49:30 +000034#include "llvm/Analysis/DIBuilder.h"
Devang Patela86114b2010-10-25 20:45:32 +000035#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000036#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000037#include "llvm/ADT/StringExtras.h"
Devang Patel6c74a872010-04-27 19:46:33 +000038#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000039#include "llvm/Support/Debug.h"
40#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000041#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000042#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000043#include "llvm/Support/Timer.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000044#include "llvm/Support/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000045using namespace llvm;
46
Jim Grosbacha8683bb2010-07-21 21:21:52 +000047static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000048 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000049 cl::desc("Disable debug info printing"));
50
Dan Gohman7421ae42010-05-07 01:08:53 +000051static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000052 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman7421ae42010-05-07 01:08:53 +000053 cl::init(false));
54
Bill Wendlingfcc14142010-04-07 09:28:04 +000055namespace {
56 const char *DWARFGroupName = "DWARF Emission";
57 const char *DbgTimerName = "DWARF Debug Writer";
58} // end anonymous namespace
59
Bill Wendling2f921f82009-05-15 09:23:25 +000060//===----------------------------------------------------------------------===//
61
62/// Configuration values for initial hash set sizes (log2).
63///
Bill Wendling2f921f82009-05-15 09:23:25 +000064static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000065
66namespace llvm {
67
Nick Lewycky019d2552011-07-29 03:49:23 +000068DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +000069 DIType Ty = Var.getType();
70 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
71 // addresses instead.
72 if (Var.isBlockByrefVariable()) {
73 /* Byref variables, in Blocks, are declared by the programmer as
74 "SomeType VarName;", but the compiler creates a
75 __Block_byref_x_VarName struct, and gives the variable VarName
76 either the struct, or a pointer to the struct, as its type. This
77 is necessary for various behind-the-scenes things the compiler
78 needs to do with by-reference variables in blocks.
79
80 However, as far as the original *programmer* is concerned, the
81 variable should still have type 'SomeType', as originally declared.
82
83 The following function dives into the __Block_byref_x_VarName
84 struct to find the original type of the variable. This will be
85 passed back to the code generating the type for the Debug
86 Information Entry for the variable 'VarName'. 'VarName' will then
87 have the original type 'SomeType' in its debug information.
88
89 The original type 'SomeType' will be the type of the field named
90 'VarName' inside the __Block_byref_x_VarName struct.
91
92 NOTE: In order for this to not completely fail on the debugger
93 side, the Debug Information Entry for the variable VarName needs to
94 have a DW_AT_location that tells the debugger how to unwind through
95 the pointers and __Block_byref_x_VarName struct to find the actual
96 value of the variable. The function addBlockByrefType does this. */
97 DIType subType = Ty;
98 unsigned tag = Ty.getTag();
99
100 if (tag == dwarf::DW_TAG_pointer_type) {
101 DIDerivedType DTy = DIDerivedType(Ty);
102 subType = DTy.getTypeDerivedFrom();
103 }
104
105 DICompositeType blockStruct = DICompositeType(subType);
106 DIArray Elements = blockStruct.getTypeArray();
107
108 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
109 DIDescriptor Element = Elements.getElement(i);
110 DIDerivedType DT = DIDerivedType(Element);
111 if (getName() == DT.getName())
112 return (DT.getTypeDerivedFrom());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000113 }
114 return Ty;
115 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000116 return Ty;
117}
Bill Wendling2f921f82009-05-15 09:23:25 +0000118
Chris Lattnerf5d06362010-04-05 04:09:20 +0000119} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000120
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000121DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000122 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000123 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel7e623022011-08-10 20:55:27 +0000124 PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000125 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000126
Rafael Espindolaa7160962011-05-06 14:56:22 +0000127 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000128 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000129 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000130 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman6e681a52010-06-18 15:56:31 +0000131 {
132 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
133 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000134 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000135}
136DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000137}
138
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000139MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
140 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
141 if (Entry.first) return Entry.first;
142
143 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000144 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000145}
146
147
Devang Patel930143b2009-11-21 02:48:08 +0000148/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000149///
Devang Patel930143b2009-11-21 02:48:08 +0000150void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000151 // Profile the node so that we can make it unique.
152 FoldingSetNodeID ID;
153 Abbrev.Profile(ID);
154
155 // Check the set for priors.
156 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
157
158 // If it's newly added.
159 if (InSet == &Abbrev) {
160 // Add to abbreviation list.
161 Abbreviations.push_back(&Abbrev);
162
163 // Assign the vector position + 1 as its number.
164 Abbrev.setNumber(Abbreviations.size());
165 } else {
166 // Assign existing abbreviation number.
167 Abbrev.setNumber(InSet->getNumber());
168 }
169}
170
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000171/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-01-05 01:46:14 +0000172/// printer to not emit usual symbol prefix before the symbol name is used then
173/// return linkage name after skipping this special LLVM prefix.
174static StringRef getRealLinkageName(StringRef LinkageName) {
175 char One = '\1';
176 if (LinkageName.startswith(StringRef(&One, 1)))
177 return LinkageName.substr(1);
178 return LinkageName;
179}
180
Jim Grosbach042483e2009-11-21 23:12:12 +0000181/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +0000182/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
183/// If there are global variables in this scope then create and insert
184/// DIEs for these variables.
Devang Patel32cc43c2010-05-07 20:54:48 +0000185DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000186 CompileUnit *SPCU = getCompileUnit(SPNode);
187 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000188
Chris Lattner3a383cb2010-04-05 00:13:49 +0000189 assert(SPDie && "Unable to find subprogram DIE!");
190 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000191
Devang Patel1d6bbd42011-04-22 23:10:17 +0000192 DISubprogram SPDecl = SP.getFunctionDeclaration();
193 if (SPDecl.isSubprogram())
194 // Refer function declaration directly.
Devang Patelf20c4f72011-04-12 22:53:02 +0000195 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Patel89543712011-08-15 17:24:54 +0000196 SPCU->getOrCreateSubprogramDIE(SPDecl));
Devang Patel1d6bbd42011-04-22 23:10:17 +0000197 else {
198 // There is not any need to generate specification DIE for a function
199 // defined at compile unit level. If a function is defined inside another
200 // function then gdb prefers the definition at top level and but does not
201 // expect specification DIE in parent function. So avoid creating
202 // specification DIE for a function defined inside a function.
203 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
204 !SP.getContext().isFile() &&
205 !isSubprogramContext(SP.getContext())) {
206 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
207
208 // Add arguments.
209 DICompositeType SPTy = SP.getType();
210 DIArray Args = SPTy.getTypeArray();
211 unsigned SPTag = SPTy.getTag();
212 if (SPTag == dwarf::DW_TAG_subroutine_type)
213 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
214 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
215 DIType ATy = DIType(DIType(Args.getElement(i)));
216 SPCU->addType(Arg, ATy);
217 if (ATy.isArtificial())
218 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
219 SPDie->addChild(Arg);
220 }
221 DIE *SPDeclDie = SPDie;
222 SPDie = new DIE(dwarf::DW_TAG_subprogram);
223 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
224 SPDeclDie);
225 SPCU->addDie(SPDie);
226 }
Chris Lattner3a383cb2010-04-05 00:13:49 +0000227 }
Devang Patela37a95e2010-07-07 22:20:57 +0000228 // Pick up abstract subprogram DIE.
229 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
230 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000231 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
232 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000233 SPCU->addDie(SPDie);
234 }
235
Devang Patelf20c4f72011-04-12 22:53:02 +0000236 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
237 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
238 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
239 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000240 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
241 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000242 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000243
Chris Lattner3a383cb2010-04-05 00:13:49 +0000244 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000245}
246
Jim Grosbach042483e2009-11-21 23:12:12 +0000247/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +0000248/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Patel7e623022011-08-10 20:55:27 +0000249DIE *DwarfDebug::constructLexicalScopeDIE(LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000250
251 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
252 if (Scope->isAbstractScope())
253 return ScopeDIE;
254
Devang Patel7e623022011-08-10 20:55:27 +0000255 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000256 if (Ranges.empty())
257 return 0;
258
Devang Patelf20c4f72011-04-12 22:53:02 +0000259 CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +0000260 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000261 if (Ranges.size() > 1) {
262 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000263 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000264 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000265 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000266 DebugRangeSymbols.size()
267 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000268 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000269 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000270 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
271 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000272 }
273 DebugRangeSymbols.push_back(NULL);
274 DebugRangeSymbols.push_back(NULL);
275 return ScopeDIE;
276 }
277
Devang Patel9fc11702010-05-25 23:40:22 +0000278 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
279 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000280
Devang Patel9fc11702010-05-25 23:40:22 +0000281 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000282
Chris Lattnere13c3722010-03-09 01:58:53 +0000283 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
284 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000285
Devang Patelf20c4f72011-04-12 22:53:02 +0000286 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
287 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000288
289 return ScopeDIE;
290}
291
Devang Patel930143b2009-11-21 02:48:08 +0000292/// constructInlinedScopeDIE - This scope represents inlined body of
293/// a function. Construct DIE to represent this concrete inlined copy
294/// of the function.
Devang Patel7e623022011-08-10 20:55:27 +0000295DIE *DwarfDebug::constructInlinedScopeDIE(LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000296
Devang Patel7e623022011-08-10 20:55:27 +0000297 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000298 assert (Ranges.empty() == false
Devang Patel7e623022011-08-10 20:55:27 +0000299 && "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000300
Devang Patelf098ce22011-07-27 00:34:13 +0000301 if (!Scope->getScopeNode())
302 return NULL;
303 DIScope DS(Scope->getScopeNode());
304 DISubprogram InlinedSP = getDISubprogram(DS);
305 CompileUnit *TheCU = getCompileUnit(InlinedSP);
306 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
307 if (!OriginDIE) {
308 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
309 return NULL;
310 }
311
Devang Patel7e623022011-08-10 20:55:27 +0000312 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000313 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
314 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000315
Devang Patel4c6bd662010-07-08 22:39:20 +0000316 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky019d2552011-07-29 03:49:23 +0000317 assert (0 && "Unexpected Start and End labels for a inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000318 return 0;
319 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000320 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000321 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000322 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000323 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000324
Devang Patel73bc1722011-05-05 17:54:26 +0000325 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000326 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
327 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000328
Devang Patelf098ce22011-07-27 00:34:13 +0000329 if (Ranges.size() > 1) {
330 // .debug_range section has not been laid out yet. Emit offset in
331 // .debug_range as a uint, size 4, for now. emitDIE will handle
332 // DW_AT_ranges appropriately.
333 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000334 DebugRangeSymbols.size()
335 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000336 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000337 RE = Ranges.end(); RI != RE; ++RI) {
338 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
339 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
340 }
341 DebugRangeSymbols.push_back(NULL);
342 DebugRangeSymbols.push_back(NULL);
343 } else {
Devang Patel784077e2011-08-10 23:58:09 +0000344 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
345 StartLabel);
346 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
347 EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000348 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000349
350 InlinedSubprogramDIEs.insert(OriginDIE);
351
352 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000353 //.debug_inlined section specification does not clearly state how
354 // to emit inlined scope that is split into multiple instruction ranges.
355 // For now, use first instruction range and emit low_pc/high_pc pair and
356 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000357 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000358 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000359
360 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000361 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +0000362 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000363 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000364 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000365 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000366
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000367 DILocation DL(Scope->getInlinedAt());
Devang Patelf20c4f72011-04-12 22:53:02 +0000368 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
369 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000370
371 return ScopeDIE;
372}
373
Devang Patel930143b2009-11-21 02:48:08 +0000374/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel7e623022011-08-10 20:55:27 +0000375DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, LexicalScope *Scope) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000376 StringRef Name = DV->getName();
Devang Patel2d9caf92009-11-25 17:36:49 +0000377 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +0000378 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000379
Devang Patel6e4d2c92011-08-15 18:35:42 +0000380 // Translate tag to proper Dwarf tag.
381 unsigned Tag = DV->getTag();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000382
383 // Define variable debug information entry.
384 DIE *VariableDie = new DIE(Tag);
Devang Patel2606f4d2011-04-25 23:05:21 +0000385 CompileUnit *VariableCU = getCompileUnit(DV->getVariable());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000386 DIE *AbsDIE = NULL;
Devang Patele1c53f22010-05-20 16:36:41 +0000387 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
388 V2AVI = VarToAbstractVarMap.find(DV);
389 if (V2AVI != VarToAbstractVarMap.end())
390 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +0000391
Devang Patele1c53f22010-05-20 16:36:41 +0000392 if (AbsDIE)
Devang Patel2606f4d2011-04-25 23:05:21 +0000393 VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf20c4f72011-04-12 22:53:02 +0000394 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000395 else {
Devang Patel2606f4d2011-04-25 23:05:21 +0000396 VariableCU->addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
397 Name);
398 VariableCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000399
400 // Add variable type.
Devang Patel2606f4d2011-04-25 23:05:21 +0000401 VariableCU->addType(VariableDie, DV->getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000402 }
403
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000404 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel2606f4d2011-04-25 23:05:21 +0000405 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
406 dwarf::DW_FORM_flag, 1);
Devang Patelbea08d12010-09-29 23:07:21 +0000407 else if (DIVariable(DV->getVariable()).isArtificial())
Devang Patel2606f4d2011-04-25 23:05:21 +0000408 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
409 dwarf::DW_FORM_flag, 1);
Devang Patel9fc11702010-05-25 23:40:22 +0000410
411 if (Scope->isAbstractScope()) {
412 DV->setDIE(VariableDie);
413 return VariableDie;
414 }
415
416 // Add variable address.
417
418 unsigned Offset = DV->getDotDebugLocOffset();
419 if (Offset != ~0U) {
Devang Patel784077e2011-08-10 23:58:09 +0000420 VariableCU->addLabel(VariableDie, dwarf::DW_AT_location,
421 dwarf::DW_FORM_data4,
422 Asm->GetTempSymbol("debug_loc", Offset));
Devang Patel9fc11702010-05-25 23:40:22 +0000423 DV->setDIE(VariableDie);
424 UseDotDebugLocEntry.insert(VariableDie);
425 return VariableDie;
426 }
427
428 // Check if variable is described by a DBG_VALUE instruction.
429 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
430 DbgVariableToDbgInstMap.find(DV);
431 if (DVI != DbgVariableToDbgInstMap.end()) {
432 const MachineInstr *DVInsn = DVI->second;
Devang Patel9fc11702010-05-25 23:40:22 +0000433 bool updated = false;
Devang Patel9fc11702010-05-25 23:40:22 +0000434 if (DVInsn->getNumOperands() == 3) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000435 if (DVInsn->getOperand(0).isReg()) {
436 const MachineOperand RegOp = DVInsn->getOperand(0);
437 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
438 if (DVInsn->getOperand(1).isImm() &&
439 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patel77dc5412011-04-27 22:45:24 +0000440 unsigned FrameReg = 0;
441 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
442 int Offset =
443 TFI->getFrameIndexReference(*Asm->MF,
444 DVInsn->getOperand(1).getImm(),
445 FrameReg);
446 MachineLocation Location(FrameReg, Offset);
447 VariableCU->addVariableAddress(DV, VariableDie, Location);
448
449 } else if (RegOp.getReg())
450 VariableCU->addVariableAddress(DV, VariableDie,
451 MachineLocation(RegOp.getReg()));
452 updated = true;
Devang Patel86ec8b32010-08-31 22:22:42 +0000453 }
Devang Patel9fc11702010-05-25 23:40:22 +0000454 else if (DVInsn->getOperand(0).isImm())
Devang Patel3c6aed22011-05-27 16:45:18 +0000455 updated =
456 VariableCU->addConstantValue(VariableDie, DVInsn->getOperand(0),
457 DV->getType());
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000458 else if (DVInsn->getOperand(0).isFPImm())
459 updated =
Devang Patel2606f4d2011-04-25 23:05:21 +0000460 VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patelf071d722011-06-24 20:46:11 +0000461 else if (DVInsn->getOperand(0).isCImm())
462 updated =
463 VariableCU->addConstantValue(VariableDie,
464 DVInsn->getOperand(0).getCImm(),
Devang Pateldfd6ec32011-08-15 17:57:41 +0000465 DV->getType().isUnsignedDIType());
Devang Patel9fc11702010-05-25 23:40:22 +0000466 } else {
Devang Patel77dc5412011-04-27 22:45:24 +0000467 VariableCU->addVariableAddress(DV, VariableDie,
468 Asm->getDebugValueLocation(DVInsn));
469 updated = true;
Devang Patel9fc11702010-05-25 23:40:22 +0000470 }
471 if (!updated) {
472 // If variableDie is not updated then DBG_VALUE instruction does not
473 // have valid variable info.
474 delete VariableDie;
475 return NULL;
476 }
477 DV->setDIE(VariableDie);
478 return VariableDie;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000479 }
Devang Patel9fc11702010-05-25 23:40:22 +0000480
481 // .. else use frame index, if available.
Devang Patel9fc11702010-05-25 23:40:22 +0000482 int FI = 0;
Devang Patel77dc5412011-04-27 22:45:24 +0000483 if (findVariableFrameIndex(DV, &FI)) {
484 unsigned FrameReg = 0;
485 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
486 int Offset =
487 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
488 MachineLocation Location(FrameReg, Offset);
489 VariableCU->addVariableAddress(DV, VariableDie, Location);
490 }
491
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000492 DV->setDIE(VariableDie);
493 return VariableDie;
494
495}
Devang Patel930143b2009-11-21 02:48:08 +0000496
497/// constructScopeDIE - Construct a DIE for this scope.
Devang Patel7e623022011-08-10 20:55:27 +0000498DIE *DwarfDebug::constructScopeDIE(LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000499 if (!Scope || !Scope->getScopeNode())
500 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000501
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000502 SmallVector <DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +0000503
504 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000505 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000506 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
507 if (DbgVariable *ArgDV = CurrentFnArguments[i])
508 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
509 Children.push_back(Arg);
510
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000511 // Collect lexical scope childrens first.
Devang Patel7e623022011-08-10 20:55:27 +0000512 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000513 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
514 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
515 Children.push_back(Variable);
Devang Patel7e623022011-08-10 20:55:27 +0000516 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000517 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
518 if (DIE *Nested = constructScopeDIE(Scopes[j]))
519 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000520 DIScope DS(Scope->getScopeNode());
521 DIE *ScopeDIE = NULL;
522 if (Scope->getInlinedAt())
523 ScopeDIE = constructInlinedScopeDIE(Scope);
524 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000525 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000526 if (Scope->isAbstractScope()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000527 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000528 // Note down abstract DIE.
529 if (ScopeDIE)
530 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
531 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000532 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000533 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000534 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000535 else {
536 // There is no need to emit empty lexical block DIE.
537 if (Children.empty())
538 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +0000539 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000540 }
541
Devang Patel23b2ae62010-03-29 22:59:58 +0000542 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000543
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000544 // Add children
545 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
546 E = Children.end(); I != E; ++I)
547 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000548
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000549 if (DS.isSubprogram())
Devang Patelf20c4f72011-04-12 22:53:02 +0000550 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000551
Devang Patel04d2f2d2009-11-24 01:14:22 +0000552 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000553}
554
Bill Wendling2b128d72009-05-20 23:19:06 +0000555/// GetOrCreateSourceID - Look up the source id with the given directory and
556/// source file names. If none currently exists, create a new id and insert it
557/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
558/// maps as well.
Devang Patel498877d2010-07-24 00:53:22 +0000559
Devang Patele01b75c2011-03-24 20:30:50 +0000560unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
561 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000562 // If FE did not provide a file name, then assume stdin.
563 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000564 return GetOrCreateSourceID("<stdin>", StringRef());
565
566 // MCStream expects full path name as filename.
Benjamin Kramer440c3b72011-06-05 14:36:47 +0000567 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
568 SmallString<128> FullPathName = DirName;
569 sys::path::append(FullPathName, FileName);
Devang Patele01b75c2011-03-24 20:30:50 +0000570 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
571 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
572 }
Devang Patel871d0b12010-09-16 20:57:49 +0000573
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000574 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
575 if (Entry.getValue())
576 return Entry.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000577
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000578 unsigned SrcId = SourceIdMap.size();
579 Entry.setValue(SrcId);
Bill Wendling2b128d72009-05-20 23:19:06 +0000580
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000581 // Print out a .file directive to specify files for .loc directives.
Devang Patele01b75c2011-03-24 20:30:50 +0000582 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendling2b128d72009-05-20 23:19:06 +0000583
584 return SrcId;
585}
586
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000587/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +0000588/// metadata node with tag DW_TAG_compile_unit.
Devang Patel32cc43c2010-05-07 20:54:48 +0000589void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000590 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000591 StringRef FN = DIUnit.getFilename();
592 StringRef Dir = DIUnit.getDirectory();
Devang Patele01b75c2011-03-24 20:30:50 +0000593 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000594
595 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelf20c4f72011-04-12 22:53:02 +0000596 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
597 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
598 DIUnit.getProducer());
599 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
600 DIUnit.getLanguage());
601 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +0000602 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
603 // simplifies debug range entries.
Devang Patelf20c4f72011-04-12 22:53:02 +0000604 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000605 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000606 // compile unit in debug_line section.
Rafael Espindola2fe0ee12011-05-10 20:35:05 +0000607 if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000608 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000609 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000610 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000611 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000612
Devang Patel2d9caf92009-11-25 17:36:49 +0000613 if (!Dir.empty())
Devang Patelf20c4f72011-04-12 22:53:02 +0000614 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000615 if (DIUnit.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000616 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +0000617
Devang Patel2d9caf92009-11-25 17:36:49 +0000618 StringRef Flags = DIUnit.getFlags();
619 if (!Flags.empty())
Devang Patel784077e2011-08-10 23:58:09 +0000620 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
621 Flags);
Devang Patelf20c4f72011-04-12 22:53:02 +0000622
Bill Wendling2b128d72009-05-20 23:19:06 +0000623 unsigned RVer = DIUnit.getRunTimeVersion();
624 if (RVer)
Devang Patelf20c4f72011-04-12 22:53:02 +0000625 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000626 dwarf::DW_FORM_data1, RVer);
627
Devang Patel1a0df9a2010-05-10 22:49:55 +0000628 if (!FirstCU)
629 FirstCU = NewCU;
630 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendling2b128d72009-05-20 23:19:06 +0000631}
632
Nick Lewycky019d2552011-07-29 03:49:23 +0000633/// getCompileUnit - Get CompileUnit DIE.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000634CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
635 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
636 DIDescriptor D(N);
637 const MDNode *CUNode = NULL;
638 if (D.isCompileUnit())
639 CUNode = N;
640 else if (D.isSubprogram())
641 CUNode = DISubprogram(N).getCompileUnit();
642 else if (D.isType())
643 CUNode = DIType(N).getCompileUnit();
644 else if (D.isGlobalVariable())
645 CUNode = DIGlobalVariable(N).getCompileUnit();
646 else if (D.isVariable())
647 CUNode = DIVariable(N).getCompileUnit();
648 else if (D.isNameSpace())
649 CUNode = DINameSpace(N).getCompileUnit();
650 else if (D.isFile())
651 CUNode = DIFile(N).getCompileUnit();
652 else
653 return FirstCU;
654
655 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
656 = CUMap.find(CUNode);
657 if (I == CUMap.end())
658 return FirstCU;
659 return I->second;
660}
661
Devang Patel1a0df9a2010-05-10 22:49:55 +0000662/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +0000663void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel469c12d22010-08-10 01:37:23 +0000664 DIGlobalVariable GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000665
Devang Patelf5d53602009-09-04 23:59:07 +0000666 // If debug information is malformed then ignore it.
Devang Patel469c12d22010-08-10 01:37:23 +0000667 if (GV.Verify() == false)
Devang Patelf5d53602009-09-04 23:59:07 +0000668 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000669
670 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000671 CompileUnit *TheCU = getCompileUnit(N);
Devang Pateldfd6ec32011-08-15 17:57:41 +0000672 TheCU->createGlobalVariableDIE(N);
Devang Patel0751a282009-06-26 01:49:18 +0000673 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000674}
675
Devang Patel1a0df9a2010-05-10 22:49:55 +0000676/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +0000677void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000678 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000679
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000680 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000681 CompileUnit *TheCU = getCompileUnit(N);
682 if (TheCU->getDIE(N))
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000683 return;
684
Bill Wendling2b128d72009-05-20 23:19:06 +0000685 if (!SP.isDefinition())
686 // This is a method declaration which will be handled while constructing
687 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000688 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000689
Devang Patel89543712011-08-15 17:24:54 +0000690 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000691
692 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000693 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000694
695 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000696 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000697
Bill Wendling2b128d72009-05-20 23:19:06 +0000698 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000699 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000700
Devang Patel0751a282009-06-26 01:49:18 +0000701 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000702}
703
Devang Patel930143b2009-11-21 02:48:08 +0000704/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000705/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky019d2552011-07-29 03:49:23 +0000706/// This is invoked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +0000707void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +0000708 if (DisableDebugInfoPrinting)
709 return;
710
Nick Lewycky019d2552011-07-29 03:49:23 +0000711 // If module has named metadata anchors then use them, otherwise scan the
712 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000713 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
714 if (CU_Nodes) {
Devang Patelc981f622011-05-04 16:34:02 +0000715
716 NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
717 NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
718 if (!GV_Nodes && !SP_Nodes)
719 // If there are not any global variables or any functions then
720 // there is not any debug info in this module.
Devang Patele02e5852011-05-03 16:45:22 +0000721 return;
722
Devang Patelc981f622011-05-04 16:34:02 +0000723 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
724 constructCompileUnit(CU_Nodes->getOperand(i));
Devang Patele02e5852011-05-03 16:45:22 +0000725
Devang Patelc981f622011-05-04 16:34:02 +0000726 if (GV_Nodes)
727 for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i)
728 constructGlobalVariableDIE(GV_Nodes->getOperand(i));
729
730 if (SP_Nodes)
731 for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i)
732 constructSubprogramDIE(SP_Nodes->getOperand(i));
Devang Patele02e5852011-05-03 16:45:22 +0000733
734 } else {
735
736 DebugInfoFinder DbgFinder;
737 DbgFinder.processModule(*M);
738
Devang Patelc981f622011-05-04 16:34:02 +0000739 bool HasDebugInfo = false;
Nick Lewycky019d2552011-07-29 03:49:23 +0000740 // Scan all the compile-units to see if there are any marked as the main
741 // unit. If not, we do not generate debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000742 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
743 E = DbgFinder.compile_unit_end(); I != E; ++I) {
744 if (DICompileUnit(*I).isMain()) {
745 HasDebugInfo = true;
746 break;
747 }
748 }
Devang Patelc981f622011-05-04 16:34:02 +0000749 if (!HasDebugInfo) return;
Devang Patele02e5852011-05-03 16:45:22 +0000750
751 // Create all the compile unit DIEs.
752 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
753 E = DbgFinder.compile_unit_end(); I != E; ++I)
754 constructCompileUnit(*I);
755
756 // Create DIEs for each global variable.
757 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
758 E = DbgFinder.global_variable_end(); I != E; ++I)
759 constructGlobalVariableDIE(*I);
760
761 // Create DIEs for each subprogram.
762 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
763 E = DbgFinder.subprogram_end(); I != E; ++I)
764 constructSubprogramDIE(*I);
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000765 }
Devang Patele02e5852011-05-03 16:45:22 +0000766
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000767 // Tell MMI that we have debug info.
768 MMI->setDebugInfoAvailability(true);
Devang Patele02e5852011-05-03 16:45:22 +0000769
Chris Lattnerd442aa32010-04-04 23:17:54 +0000770 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000771 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000772
Devang Patel8e06a5e2010-08-10 20:01:20 +0000773 //getOrCreateTypeDIE
774 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patelf20c4f72011-04-12 22:53:02 +0000775 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
776 DIType Ty(NMD->getOperand(i));
777 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
778 }
Devang Patel8e06a5e2010-08-10 20:01:20 +0000779
Devang Patel7a554812010-09-28 18:08:20 +0000780 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patelf20c4f72011-04-12 22:53:02 +0000781 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
782 DIType Ty(NMD->getOperand(i));
783 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
784 }
Devang Patel7a554812010-09-28 18:08:20 +0000785
Bill Wendling2b128d72009-05-20 23:19:06 +0000786 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000787 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000788}
789
Devang Patel930143b2009-11-21 02:48:08 +0000790/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +0000791///
Devang Patel930143b2009-11-21 02:48:08 +0000792void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000793 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +0000794 const Module *M = MMI->getModule();
Devang Patel7e623022011-08-10 20:55:27 +0000795 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +0000796 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
797 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
798 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
799 DISubprogram SP(AllSPs->getOperand(SI));
800 if (!SP.Verify()) continue;
801
802 // Collect info for variables that were optimized out.
Devang Patel18efced2010-07-19 17:53:55 +0000803 if (!SP.isDefinition()) continue;
Devang Patelf3b2db62010-06-28 18:25:03 +0000804 StringRef FName = SP.getLinkageName();
805 if (FName.empty())
806 FName = SP.getName();
Devang Patel364bf042010-11-10 22:19:21 +0000807 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patelf3b2db62010-06-28 18:25:03 +0000808 if (!NMD) continue;
809 unsigned E = NMD->getNumOperands();
810 if (!E) continue;
Devang Patel784077e2011-08-10 23:58:09 +0000811 LexicalScope *Scope = new LexicalScope(NULL, DIDescriptor(SP), NULL,
812 false);
Devang Pateld0701282010-08-02 17:32:15 +0000813 DeadFnScopeMap[SP] = Scope;
Benjamin Kramerfa7e6a52011-08-11 18:39:28 +0000814 SmallVector<DbgVariable, 8> Variables;
Devang Patelf3b2db62010-06-28 18:25:03 +0000815 for (unsigned I = 0; I != E; ++I) {
816 DIVariable DV(NMD->getOperand(I));
817 if (!DV.Verify()) continue;
Benjamin Kramerfa7e6a52011-08-11 18:39:28 +0000818 Variables.push_back(DbgVariable(DV));
Devang Patelf3b2db62010-06-28 18:25:03 +0000819 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000820
Devang Patelf3b2db62010-06-28 18:25:03 +0000821 // Construct subprogram DIE and add variables DIEs.
822 constructSubprogramDIE(SP);
823 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patelf3b2db62010-06-28 18:25:03 +0000824 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Benjamin Kramerfa7e6a52011-08-11 18:39:28 +0000825 if (DIE *VariableDIE = constructVariableDIE(&Variables[i], Scope))
Devang Patelf3b2db62010-06-28 18:25:03 +0000826 ScopeDIE->addChild(VariableDIE);
827 }
828 }
829 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000830
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000831 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
832 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
833 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
834 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000835 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000836 }
837
Devang Patel89543712011-08-15 17:24:54 +0000838 // Emit DW_AT_containing_type attribute to connect types with their
839 // vtable holding type.
840 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
841 CUE = CUMap.end(); CUI != CUE; ++CUI) {
842 CompileUnit *TheCU = CUI->second;
843 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000844 }
845
Bill Wendling2b128d72009-05-20 23:19:06 +0000846 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000847 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000848 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000849 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000850 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000851
852 // End text sections.
853 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000854 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +0000855 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +0000856 }
857
Bill Wendling2b128d72009-05-20 23:19:06 +0000858 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +0000859 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +0000860
861 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +0000862 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000863
864 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +0000865 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +0000866
Bill Wendling2b128d72009-05-20 23:19:06 +0000867 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +0000868 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +0000869
Devang Patel04d2f2d2009-11-24 01:14:22 +0000870 // Emit info into a debug pubtypes section.
871 emitDebugPubTypes();
872
Bill Wendling2b128d72009-05-20 23:19:06 +0000873 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +0000874 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +0000875
876 // Emit info into a debug aranges section.
877 EmitDebugARanges();
878
879 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +0000880 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +0000881
882 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +0000883 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000884
885 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +0000886 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000887
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000888 // Emit info into a debug str section.
889 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000890
Devang Pateld0701282010-08-02 17:32:15 +0000891 // clean up.
892 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000893 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
894 E = CUMap.end(); I != E; ++I)
895 delete I->second;
896 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +0000897}
898
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000899/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +0000900DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +0000901 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +0000902 LLVMContext &Ctx = DV->getContext();
903 // More then one inlined variable corresponds to one abstract variable.
904 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000905 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000906 if (AbsDbgVariable)
907 return AbsDbgVariable;
908
Devang Patel7e623022011-08-10 20:55:27 +0000909 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000910 if (!Scope)
911 return NULL;
912
Devang Patele1c53f22010-05-20 16:36:41 +0000913 AbsDbgVariable = new DbgVariable(Var);
Devang Patel7e623022011-08-10 20:55:27 +0000914 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000915 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000916 return AbsDbgVariable;
917}
918
Nick Lewycky019d2552011-07-29 03:49:23 +0000919/// addCurrentFnArgument - If Var is a current function argument then add
920/// it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +0000921bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +0000922 DbgVariable *Var, LexicalScope *Scope) {
923 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000924 return false;
925 DIVariable DV = Var->getVariable();
926 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
927 return false;
928 unsigned ArgNo = DV.getArgNumber();
929 if (ArgNo == 0)
930 return false;
931
Devang Patel4ab660b2011-03-03 20:02:02 +0000932 size_t Size = CurrentFnArguments.size();
933 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +0000934 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +0000935 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +0000936 // arguments does the function have at source level.
937 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +0000938 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +0000939 CurrentFnArguments[ArgNo - 1] = Var;
940 return true;
941}
942
Devang Patel490c8ab2010-05-20 19:57:06 +0000943/// collectVariableInfoFromMMITable - Collect variable information from
944/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000945void
Nick Lewycky019d2552011-07-29 03:49:23 +0000946DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +0000947 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +0000948 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
949 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
950 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000951 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000952 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +0000953 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +0000954 DIVariable DV(Var);
955 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000956
Devang Patel7e623022011-08-10 20:55:27 +0000957 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000958
Devang Patelcdb7d442009-11-10 23:20:04 +0000959 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +0000960 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +0000961 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000962
Devang Patele1c53f22010-05-20 16:36:41 +0000963 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
964 DbgVariable *RegVar = new DbgVariable(DV);
965 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +0000966 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000967 addScopeVariable(Scope, RegVar);
Devang Patele1c53f22010-05-20 16:36:41 +0000968 if (AbsDbgVariable) {
969 recordVariableFrameIndex(AbsDbgVariable, VP.first);
970 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
971 }
Devang Patel475d32a2009-10-06 01:26:37 +0000972 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000973}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000974
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000975/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +0000976/// DBG_VALUE instruction, is in a defined reg.
977static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
978 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000979 return MI->getNumOperands() == 3 &&
980 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
981 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000982}
983
Nick Lewycky019d2552011-07-29 03:49:23 +0000984/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel2442a892011-07-08 17:09:57 +0000985/// at MI.
986static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
987 const MCSymbol *FLabel,
988 const MCSymbol *SLabel,
989 const MachineInstr *MI) {
990 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
991
992 if (MI->getNumOperands() != 3) {
993 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
994 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
995 }
996 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
997 MachineLocation MLoc;
998 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
999 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1000 }
1001 if (MI->getOperand(0).isImm())
1002 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1003 if (MI->getOperand(0).isFPImm())
1004 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1005 if (MI->getOperand(0).isCImm())
1006 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1007
1008 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
1009 return DotDebugLocEntry();
1010}
1011
Devang Patel7e623022011-08-10 20:55:27 +00001012/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001013void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001014DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1015 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001016
Devang Patel490c8ab2010-05-20 19:57:06 +00001017 /// collection info from MMI table.
1018 collectVariableInfoFromMMITable(MF, Processed);
1019
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001020 for (SmallVectorImpl<const MDNode*>::const_iterator
1021 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1022 ++UVI) {
1023 const MDNode *Var = *UVI;
1024 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001025 continue;
1026
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001027 // History contains relevant DBG_VALUE instructions for Var and instructions
1028 // clobbering it.
1029 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1030 if (History.empty())
1031 continue;
1032 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001033
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001034 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001035 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001036 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1037 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001038 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +00001039 else {
1040 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +00001041 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +00001042 else {
1043 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +00001044 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001045 else
Devang Patel7e623022011-08-10 20:55:27 +00001046 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001047 }
1048 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001049 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001050 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001051 continue;
1052
1053 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001054 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel490c8ab2010-05-20 19:57:06 +00001055 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel6c622ef2011-03-01 22:58:55 +00001056 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001057 addScopeVariable(Scope, RegVar);
Devang Patelfbd6c452010-05-21 00:10:20 +00001058 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
1059 DbgVariableToDbgInstMap[AbsVar] = MInsn;
1060 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patela3e9c9c2010-03-15 18:33:46 +00001061 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001062
1063 // Simple ranges that are fully coalesced.
1064 if (History.size() <= 1 || (History.size() == 2 &&
1065 MInsn->isIdenticalTo(History.back()))) {
Devang Patel9fc11702010-05-25 23:40:22 +00001066 DbgVariableToDbgInstMap[RegVar] = MInsn;
1067 continue;
1068 }
1069
1070 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001071 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001072
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001073 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1074 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1075 const MachineInstr *Begin = *HI;
1076 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001077
Devang Patele7181b52011-06-01 23:00:17 +00001078 // Check if DBG_VALUE is truncating a range.
1079 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1080 && !Begin->getOperand(0).getReg())
1081 continue;
1082
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001083 // Compute the range for a register location.
1084 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1085 const MCSymbol *SLabel = 0;
1086
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001087 if (HI + 1 == HE)
1088 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001089 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001090 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001091 else {
1092 const MachineInstr *End = HI[1];
Devang Patel53b050a2011-07-07 21:44:42 +00001093 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1094 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001095 if (End->isDebugValue())
1096 SLabel = getLabelBeforeInsn(End);
1097 else {
1098 // End is a normal instruction clobbering the range.
1099 SLabel = getLabelAfterInsn(End);
1100 assert(SLabel && "Forgot label after clobber instruction");
1101 ++HI;
1102 }
1103 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001104
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001105 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel2442a892011-07-08 17:09:57 +00001106 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001107 }
1108 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001109 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001110
1111 // Collect info for variables that were optimized out.
Devang Patelad517352010-06-22 01:01:58 +00001112 const Function *F = MF->getFunction();
Devang Patel364bf042010-11-10 22:19:21 +00001113 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patele0a94bf2010-05-14 21:01:35 +00001114 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00001115 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001116 if (!DV || !Processed.insert(DV))
Devang Patele0a94bf2010-05-14 21:01:35 +00001117 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001118 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1119 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patele0a94bf2010-05-14 21:01:35 +00001120 }
1121 }
Devang Patel9fc11702010-05-25 23:40:22 +00001122}
Devang Patele0a94bf2010-05-14 21:01:35 +00001123
Devang Patel9fc11702010-05-25 23:40:22 +00001124/// getLabelBeforeInsn - Return Label preceding the instruction.
1125const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001126 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1127 assert(Label && "Didn't insert label before instruction");
1128 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001129}
1130
1131/// getLabelAfterInsn - Return Label immediately following the instruction.
1132const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001133 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001134}
1135
Devang Patelb5694e72010-10-26 17:49:02 +00001136/// beginInstruction - Process beginning of an instruction.
1137void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001138 // Check if source location changes, but ignore DBG_VALUE locations.
1139 if (!MI->isDebugValue()) {
1140 DebugLoc DL = MI->getDebugLoc();
1141 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel34a66202011-05-11 19:22:19 +00001142 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001143 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001144 if (DL == PrologEndLoc) {
1145 Flags |= DWARF2_FLAG_PROLOGUE_END;
1146 PrologEndLoc = DebugLoc();
1147 }
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001148 if (!DL.isUnknown()) {
1149 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001150 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001151 } else
Devang Patel34a66202011-05-11 19:22:19 +00001152 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001153 }
Devang Patel9fc11702010-05-25 23:40:22 +00001154 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001155
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001156 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001157 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1158 LabelsBeforeInsn.find(MI);
1159
1160 // No label needed.
1161 if (I == LabelsBeforeInsn.end())
1162 return;
1163
1164 // Label already assigned.
1165 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001166 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001167
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001168 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001169 PrevLabel = MMI->getContext().CreateTempSymbol();
1170 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001171 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001172 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001173}
1174
Devang Patelb5694e72010-10-26 17:49:02 +00001175/// endInstruction - Process end of an instruction.
1176void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001177 // Don't create a new label after DBG_VALUE instructions.
1178 // They don't generate code.
1179 if (!MI->isDebugValue())
1180 PrevLabel = 0;
1181
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001182 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1183 LabelsAfterInsn.find(MI);
1184
1185 // No label needed.
1186 if (I == LabelsAfterInsn.end())
1187 return;
1188
1189 // Label already assigned.
1190 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001191 return;
1192
1193 // We need a label after this instruction.
1194 if (!PrevLabel) {
1195 PrevLabel = MMI->getContext().CreateTempSymbol();
1196 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001197 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001198 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001199}
1200
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001201/// identifyScopeMarkers() -
Devang Patel784077e2011-08-10 23:58:09 +00001202/// Each LexicalScope has first instruction and last instruction to mark
1203/// beginning and end of a scope respectively. Create an inverse map that list
1204/// scopes starts (and ends) with an instruction. One instruction may start (or
1205/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001206void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001207 SmallVector<LexicalScope *, 4> WorkList;
1208 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001209 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001210 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001211
Devang Patel7e623022011-08-10 20:55:27 +00001212 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001213 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001214 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001215 SE = Children.end(); SI != SE; ++SI)
1216 WorkList.push_back(*SI);
1217
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001218 if (S->isAbstractScope())
1219 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001220
Devang Patel7e623022011-08-10 20:55:27 +00001221 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001222 if (Ranges.empty())
1223 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001224 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001225 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001226 assert(RI->first && "InsnRange does not have first instruction!");
1227 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001228 requestLabelBeforeInsn(RI->first);
1229 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001230 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001231 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001232}
1233
Devang Patel589845d2011-05-09 22:14:49 +00001234/// getScopeNode - Get MDNode for DebugLoc's scope.
1235static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1236 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1237 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1238 return DL.getScope(Ctx);
1239}
1240
Devang Patel34a66202011-05-11 19:22:19 +00001241/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1242/// line number info for the function.
1243static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1244 const MDNode *Scope = getScopeNode(DL, Ctx);
1245 DISubprogram SP = getDISubprogram(Scope);
1246 if (SP.Verify())
1247 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1248 return DebugLoc();
1249}
1250
Devang Patel930143b2009-11-21 02:48:08 +00001251/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001252/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001253void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001254 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001255 LScopes.initialize(*MF);
1256 if (LScopes.empty()) return;
1257 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001258
Devang Patel6c74a872010-04-27 19:46:33 +00001259 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1260 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001261 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001262 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001263
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001264 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1265
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001266 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001267 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1268 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1269
Devang Patel002d54d2010-05-26 19:37:24 +00001270 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001271 I != E; ++I) {
1272 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001273 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1274 II != IE; ++II) {
1275 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001276
Devang Patel002d54d2010-05-26 19:37:24 +00001277 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00001278 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001279
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001280 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001281 const MDNode *Var =
1282 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001283
1284 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001285 if (isDbgValueInDefinedReg(MI))
1286 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1287
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001288 // Check the history of this variable.
1289 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1290 if (History.empty()) {
1291 UserVariables.push_back(Var);
1292 // The first mention of a function argument gets the FunctionBeginSym
1293 // label, so arguments are visible when breaking at function entry.
1294 DIVariable DV(Var);
1295 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1296 DISubprogram(getDISubprogram(DV.getContext()))
1297 .describes(MF->getFunction()))
1298 LabelsBeforeInsn[MI] = FunctionBeginSym;
1299 } else {
1300 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1301 const MachineInstr *Prev = History.back();
1302 if (Prev->isDebugValue()) {
1303 // Coalesce identical entries at the end of History.
1304 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001305 Prev->isIdenticalTo(History[History.size() - 2])) {
1306 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1307 << "\t" << *Prev
1308 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001309 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001310 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001311
1312 // Terminate old register assignments that don't reach MI;
1313 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1314 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1315 isDbgValueInDefinedReg(Prev)) {
1316 // Previous register assignment needs to terminate at the end of
1317 // its basic block.
1318 MachineBasicBlock::const_iterator LastMI =
1319 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001320 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001321 // Drop DBG_VALUE for empty range.
Devang Patelb7a328e2011-07-07 00:14:27 +00001322 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1323 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001324 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001325 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001326 else {
1327 // Terminate after LastMI.
1328 History.push_back(LastMI);
1329 }
1330 }
1331 }
1332 }
1333 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001334 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001335 // Not a DBG_VALUE instruction.
1336 if (!MI->isLabel())
1337 AtBlockEntry = false;
1338
Devang Patel34a66202011-05-11 19:22:19 +00001339 // First known non DBG_VALUE location marks beginning of function
1340 // body.
1341 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1342 PrologEndLoc = MI->getDebugLoc();
1343
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001344 // Check if the instruction clobbers any registers with debug vars.
1345 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1346 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1347 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1348 continue;
1349 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1350 unsigned Reg = *AI; ++AI) {
1351 const MDNode *Var = LiveUserVar[Reg];
1352 if (!Var)
1353 continue;
1354 // Reg is now clobbered.
1355 LiveUserVar[Reg] = 0;
1356
1357 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001358 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1359 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001360 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001361 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1362 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001363 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001364 const MachineInstr *Prev = History.back();
1365 // Sanity-check: Register assignments are terminated at the end of
1366 // their block.
1367 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1368 continue;
1369 // Is the variable still in Reg?
1370 if (!isDbgValueInDefinedReg(Prev) ||
1371 Prev->getOperand(0).getReg() != Reg)
1372 continue;
1373 // Var is clobbered. Make sure the next instruction gets a label.
1374 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001375 }
1376 }
Devang Patel002d54d2010-05-26 19:37:24 +00001377 }
Devang Patel002d54d2010-05-26 19:37:24 +00001378 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001379 }
1380
1381 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1382 I != E; ++I) {
1383 SmallVectorImpl<const MachineInstr*> &History = I->second;
1384 if (History.empty())
1385 continue;
1386
1387 // Make sure the final register assignments are terminated.
1388 const MachineInstr *Prev = History.back();
1389 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1390 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel784077e2011-08-10 23:58:09 +00001391 MachineBasicBlock::const_iterator LastMI =
1392 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001393 if (LastMI == PrevMBB->end())
1394 // Drop DBG_VALUE for empty range.
1395 History.pop_back();
1396 else {
1397 // Terminate after LastMI.
1398 History.push_back(LastMI);
1399 }
1400 }
1401 // Request labels for the full history.
1402 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1403 const MachineInstr *MI = History[i];
1404 if (MI->isDebugValue())
1405 requestLabelBeforeInsn(MI);
1406 else
1407 requestLabelAfterInsn(MI);
1408 }
1409 }
Devang Patel002d54d2010-05-26 19:37:24 +00001410
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001411 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001412 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001413
1414 // Record beginning of function.
1415 if (!PrologEndLoc.isUnknown()) {
1416 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1417 MF->getFunction()->getContext());
1418 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1419 FnStartDL.getScope(MF->getFunction()->getContext()),
1420 DWARF2_FLAG_IS_STMT);
1421 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001422}
1423
Devang Patel7e623022011-08-10 20:55:27 +00001424void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1425// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1426 ScopeVariables[LS].push_back(Var);
1427// Vars.push_back(Var);
1428}
1429
Devang Patel930143b2009-11-21 02:48:08 +00001430/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00001431///
Chris Lattner76555b52010-01-26 23:18:02 +00001432void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001433 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001434
Devang Patel7e623022011-08-10 20:55:27 +00001435 // Define end label for subprogram.
1436 FunctionEndSym = Asm->GetTempSymbol("func_end",
1437 Asm->getFunctionNumber());
1438 // Assumes in correct section after the entry point.
1439 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1440
1441 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1442 collectVariableInfo(MF, ProcessedVars);
1443
1444 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001445 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1446 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1447 LexicalScope *AScope = AList[i];
1448 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001449 if (SP.Verify()) {
1450 // Collect info for variables that were optimized out.
1451 StringRef FName = SP.getLinkageName();
1452 if (FName.empty())
1453 FName = SP.getName();
1454 if (NamedMDNode *NMD =
1455 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
1456 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00001457 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001458 if (!DV || !ProcessedVars.insert(DV))
1459 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001460 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1461 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001462 }
1463 }
1464 }
Devang Patel44403472011-08-12 18:10:19 +00001465 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
1466 constructScopeDIE(AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001467 }
Devang Patel7e623022011-08-10 20:55:27 +00001468
1469 DIE *CurFnDIE = constructScopeDIE(LScopes.getCurrentFunctionScope());
1470
1471 if (!DisableFramePointerElim(*MF)) {
1472 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel784077e2011-08-10 23:58:09 +00001473 CompileUnit *TheCU = getCompileUnit(FnScope->getScopeNode());
1474 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1475 dwarf::DW_FORM_flag, 1);
Devang Patel7e623022011-08-10 20:55:27 +00001476 }
1477 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1478 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001479
Bill Wendling2b128d72009-05-20 23:19:06 +00001480 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001481 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1482 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1483 DeleteContainerPointers(I->second);
1484 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001485 DeleteContainerPointers(CurrentFnArguments);
Devang Patele1c53f22010-05-20 16:36:41 +00001486 DbgVariableToFrameIndexMap.clear();
1487 VarToAbstractVarMap.clear();
1488 DbgVariableToDbgInstMap.clear();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001489 UserVariables.clear();
1490 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001491 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001492 LabelsBeforeInsn.clear();
1493 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001494 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001495}
1496
Devang Patele1c53f22010-05-20 16:36:41 +00001497/// recordVariableFrameIndex - Record a variable's index.
1498void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
1499 assert (V && "Invalid DbgVariable!");
1500 DbgVariableToFrameIndexMap[V] = Index;
1501}
1502
1503/// findVariableFrameIndex - Return true if frame index for the variable
1504/// is found. Update FI to hold value of the index.
1505bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
1506 assert (V && "Invalid DbgVariable!");
1507 DenseMap<const DbgVariable *, int>::iterator I =
1508 DbgVariableToFrameIndexMap.find(V);
1509 if (I == DbgVariableToFrameIndexMap.end())
1510 return false;
1511 *FI = I->second;
1512 return true;
1513}
1514
Chris Lattnerba35a672010-03-09 04:54:43 +00001515/// recordSourceLine - Register a source line with debug info. Returns the
1516/// unique label that was emitted and which provides correspondence to
1517/// the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001518void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1519 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001520 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001521 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001522 unsigned Src = 1;
1523 if (S) {
1524 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001525
Dan Gohman50849c62010-05-05 23:41:32 +00001526 if (Scope.isCompileUnit()) {
1527 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001528 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001529 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001530 } else if (Scope.isFile()) {
1531 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001532 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001533 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001534 } else if (Scope.isSubprogram()) {
1535 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001536 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001537 Dir = SP.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001538 } else if (Scope.isLexicalBlock()) {
1539 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001540 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001541 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001542 } else
1543 assert(0 && "Unexpected scope info");
1544
Devang Patele01b75c2011-03-24 20:30:50 +00001545 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001546 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001547 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001548}
1549
Bill Wendling806535f2009-05-20 23:22:40 +00001550//===----------------------------------------------------------------------===//
1551// Emit Methods
1552//===----------------------------------------------------------------------===//
1553
Devang Patel930143b2009-11-21 02:48:08 +00001554/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00001555///
Jim Grosbach00e9c612009-11-22 19:20:36 +00001556unsigned
1557DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00001558 // Get the children.
1559 const std::vector<DIE *> &Children = Die->getChildren();
1560
1561 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001562 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001563 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00001564
1565 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001566 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001567
1568 // Get the abbreviation for this DIE.
1569 unsigned AbbrevNumber = Die->getAbbrevNumber();
1570 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1571
1572 // Set DIE offset
1573 Die->setOffset(Offset);
1574
1575 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001576 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001577
1578 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1579 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1580
1581 // Size the DIE attribute values.
1582 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1583 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001584 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001585
1586 // Size the DIE children if any.
1587 if (!Children.empty()) {
1588 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1589 "Children flag not set");
1590
1591 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001592 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00001593
1594 // End of children marker.
1595 Offset += sizeof(int8_t);
1596 }
1597
1598 Die->setSize(Offset - Die->getOffset());
1599 return Offset;
1600}
1601
Devang Patel930143b2009-11-21 02:48:08 +00001602/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00001603///
Devang Patel930143b2009-11-21 02:48:08 +00001604void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001605 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1606 E = CUMap.end(); I != E; ++I) {
1607 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00001608 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00001609 sizeof(int32_t) + // Length of Compilation Unit Info
1610 sizeof(int16_t) + // DWARF version number
1611 sizeof(int32_t) + // Offset Into Abbrev. Section
1612 sizeof(int8_t); // Pointer Size (in bytes)
1613 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001614 }
Bill Wendling480ff322009-05-20 23:21:38 +00001615}
1616
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001617/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1618/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00001619static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001620 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00001621 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001622 if (!SymbolStem) return 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001623
Chris Lattner6629ca92010-04-04 22:59:04 +00001624 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1625 Asm->OutStreamer.EmitLabel(TmpSym);
1626 return TmpSym;
1627}
1628
1629/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1630/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00001631void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001632 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001633
Bill Wendling480ff322009-05-20 23:21:38 +00001634 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001635 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001636 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001637 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001638 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001639 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001640
Chris Lattner6629ca92010-04-04 22:59:04 +00001641 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001642 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001643
Devang Patel4a213872010-08-24 00:06:12 +00001644 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001645 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1646 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1647 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001648 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001649 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00001650 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1651 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001652
Devang Patel9fc11702010-05-25 23:40:22 +00001653 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1654 "section_debug_loc");
1655
Chris Lattner6629ca92010-04-04 22:59:04 +00001656 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00001657 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001658}
1659
Nick Lewycky019d2552011-07-29 03:49:23 +00001660/// emitDIE - Recursively emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00001661///
Devang Patel930143b2009-11-21 02:48:08 +00001662void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001663 // Get the abbreviation for this DIE.
1664 unsigned AbbrevNumber = Die->getAbbrevNumber();
1665 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1666
Bill Wendling480ff322009-05-20 23:21:38 +00001667 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001668 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001669 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1670 Twine::utohexstr(Die->getOffset()) + ":0x" +
1671 Twine::utohexstr(Die->getSize()) + " " +
1672 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001673 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001674
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001675 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001676 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1677
1678 // Emit the DIE attribute values.
1679 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1680 unsigned Attr = AbbrevData[i].getAttribute();
1681 unsigned Form = AbbrevData[i].getForm();
1682 assert(Form && "Too many attributes for DIE (check abbreviation)");
1683
Chris Lattner7bde8c02010-04-04 18:52:31 +00001684 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001685 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001686
Bill Wendling480ff322009-05-20 23:21:38 +00001687 switch (Attr) {
1688 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00001689 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00001690 break;
1691 case dwarf::DW_AT_abstract_origin: {
1692 DIEEntry *E = cast<DIEEntry>(Values[i]);
1693 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001694 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001695 Asm->EmitInt32(Addr);
1696 break;
1697 }
Devang Patel12563b32010-04-16 23:33:45 +00001698 case dwarf::DW_AT_ranges: {
1699 // DW_AT_range Value encodes offset in debug_range section.
1700 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001701
1702 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1703 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1704 V->getValue(),
1705 4);
1706 } else {
1707 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1708 V->getValue(),
1709 DwarfDebugRangeSectionSym,
1710 4);
1711 }
Devang Patel12563b32010-04-16 23:33:45 +00001712 break;
1713 }
Devang Patel9fc11702010-05-25 23:40:22 +00001714 case dwarf::DW_AT_location: {
1715 if (UseDotDebugLocEntry.count(Die) != 0) {
1716 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001717 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel9fc11702010-05-25 23:40:22 +00001718 } else
1719 Values[i]->EmitValue(Asm, Form);
1720 break;
1721 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001722 case dwarf::DW_AT_accessibility: {
1723 if (Asm->isVerbose()) {
1724 DIEInteger *V = cast<DIEInteger>(Values[i]);
1725 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1726 }
1727 Values[i]->EmitValue(Asm, Form);
1728 break;
1729 }
Bill Wendling480ff322009-05-20 23:21:38 +00001730 default:
1731 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001732 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001733 break;
1734 }
Bill Wendling480ff322009-05-20 23:21:38 +00001735 }
1736
1737 // Emit the DIE children if any.
1738 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1739 const std::vector<DIE *> &Children = Die->getChildren();
1740
1741 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001742 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00001743
Chris Lattner7bde8c02010-04-04 18:52:31 +00001744 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001745 Asm->OutStreamer.AddComment("End Of Children Mark");
1746 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001747 }
1748}
1749
Devang Patel9ccfb642009-12-09 18:24:21 +00001750/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00001751///
Devang Patel9ccfb642009-12-09 18:24:21 +00001752void DwarfDebug::emitDebugInfo() {
1753 // Start debug info section.
1754 Asm->OutStreamer.SwitchSection(
1755 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001756 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1757 E = CUMap.end(); I != E; ++I) {
1758 CompileUnit *TheCU = I->second;
1759 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001760
Devang Patel1a0df9a2010-05-10 22:49:55 +00001761 // Emit the compile units header.
1762 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1763 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001764
Devang Patel1a0df9a2010-05-10 22:49:55 +00001765 // Emit size of content not including length itself
1766 unsigned ContentSize = Die->getSize() +
1767 sizeof(int16_t) + // DWARF version number
1768 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001769 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001770
Devang Patel1a0df9a2010-05-10 22:49:55 +00001771 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1772 Asm->EmitInt32(ContentSize);
1773 Asm->OutStreamer.AddComment("DWARF version number");
1774 Asm->EmitInt16(dwarf::DWARF_VERSION);
1775 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1776 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1777 DwarfAbbrevSectionSym);
1778 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1779 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001780
Devang Patel1a0df9a2010-05-10 22:49:55 +00001781 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001782 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1783 }
Bill Wendling480ff322009-05-20 23:21:38 +00001784}
1785
Devang Patel930143b2009-11-21 02:48:08 +00001786/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00001787///
Devang Patel930143b2009-11-21 02:48:08 +00001788void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00001789 // Check to see if it is worth the effort.
1790 if (!Abbreviations.empty()) {
1791 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001792 Asm->OutStreamer.SwitchSection(
1793 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001794
Chris Lattnera179b522010-04-04 19:25:43 +00001795 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00001796
1797 // For each abbrevation.
1798 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1799 // Get abbreviation data
1800 const DIEAbbrev *Abbrev = Abbreviations[i];
1801
1802 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001803 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001804
1805 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001806 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001807 }
1808
1809 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001810 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00001811
Chris Lattnera179b522010-04-04 19:25:43 +00001812 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00001813 }
1814}
1815
Devang Patel930143b2009-11-21 02:48:08 +00001816/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00001817/// the line matrix.
1818///
Devang Patel930143b2009-11-21 02:48:08 +00001819void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00001820 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00001821 Asm->OutStreamer.AddComment("Extended Op");
1822 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001823
Chris Lattner566cae92010-03-09 23:52:58 +00001824 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001825 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00001826 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1827 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1828
1829 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00001830
Chris Lattnera179b522010-04-04 19:25:43 +00001831 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00001832 Asm->getTargetData().getPointerSize(),
1833 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001834
1835 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00001836 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1837 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001838 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00001839 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00001840}
1841
Devang Patel9ccfb642009-12-09 18:24:21 +00001842/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1843///
1844void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001845 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1846 E = CUMap.end(); I != E; ++I) {
1847 CompileUnit *TheCU = I->second;
1848 // Start the dwarf pubnames section.
1849 Asm->OutStreamer.SwitchSection(
1850 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001851
Devang Patel1a0df9a2010-05-10 22:49:55 +00001852 Asm->OutStreamer.AddComment("Length of Public Names Info");
1853 Asm->EmitLabelDifference(
1854 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1855 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001856
Devang Patel1a0df9a2010-05-10 22:49:55 +00001857 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1858 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001859
Devang Patel1a0df9a2010-05-10 22:49:55 +00001860 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001861 Asm->EmitInt16(dwarf::DWARF_VERSION);
1862
Devang Patel1a0df9a2010-05-10 22:49:55 +00001863 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001864 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00001865 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001866
Devang Patel1a0df9a2010-05-10 22:49:55 +00001867 Asm->OutStreamer.AddComment("Compilation Unit Length");
1868 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1869 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1870 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001871
Devang Patel1a0df9a2010-05-10 22:49:55 +00001872 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1873 for (StringMap<DIE*>::const_iterator
1874 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1875 const char *Name = GI->getKeyData();
1876 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001877
Devang Patel1a0df9a2010-05-10 22:49:55 +00001878 Asm->OutStreamer.AddComment("DIE offset");
1879 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001880
Devang Patel1a0df9a2010-05-10 22:49:55 +00001881 if (Asm->isVerbose())
1882 Asm->OutStreamer.AddComment("External Name");
1883 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1884 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001885
Devang Patel1a0df9a2010-05-10 22:49:55 +00001886 Asm->OutStreamer.AddComment("End Mark");
1887 Asm->EmitInt32(0);
1888 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky019d2552011-07-29 03:49:23 +00001889 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00001890 }
Bill Wendling480ff322009-05-20 23:21:38 +00001891}
1892
Devang Patel04d2f2d2009-11-24 01:14:22 +00001893void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001894 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1895 E = CUMap.end(); I != E; ++I) {
1896 CompileUnit *TheCU = I->second;
1897 // Start the dwarf pubnames section.
1898 Asm->OutStreamer.SwitchSection(
1899 Asm->getObjFileLowering().getDwarfPubTypesSection());
1900 Asm->OutStreamer.AddComment("Length of Public Types Info");
1901 Asm->EmitLabelDifference(
1902 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1903 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001904
Devang Patel1a0df9a2010-05-10 22:49:55 +00001905 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1906 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001907
Devang Patel1a0df9a2010-05-10 22:49:55 +00001908 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1909 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001910
Devang Patel1a0df9a2010-05-10 22:49:55 +00001911 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1912 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1913 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001914
Devang Patel1a0df9a2010-05-10 22:49:55 +00001915 Asm->OutStreamer.AddComment("Compilation Unit Length");
1916 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1917 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1918 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001919
Devang Patel1a0df9a2010-05-10 22:49:55 +00001920 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1921 for (StringMap<DIE*>::const_iterator
1922 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1923 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00001924 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001925
Devang Patel1a0df9a2010-05-10 22:49:55 +00001926 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1927 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001928
Devang Patel1a0df9a2010-05-10 22:49:55 +00001929 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1930 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1931 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001932
Devang Patel1a0df9a2010-05-10 22:49:55 +00001933 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001934 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001935 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1936 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001937 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001938}
1939
Devang Patel930143b2009-11-21 02:48:08 +00001940/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00001941///
Devang Patel930143b2009-11-21 02:48:08 +00001942void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00001943 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00001944 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001945
Chris Lattner3d72a672010-03-09 23:38:23 +00001946 // Start the dwarf str section.
1947 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001948 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001949
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001950 // Get all of the string pool entries and put them in an array by their ID so
1951 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001952 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001953 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001954
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001955 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1956 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1957 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001958
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001959 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001960
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001961 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00001962 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001963 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001964
Chris Lattner3d72a672010-03-09 23:38:23 +00001965 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001966 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001967 }
1968}
1969
Devang Patel930143b2009-11-21 02:48:08 +00001970/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00001971///
Devang Patel930143b2009-11-21 02:48:08 +00001972void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001973 if (DotDebugLocEntries.empty())
1974 return;
1975
Devang Patel116a9d72011-02-04 22:57:18 +00001976 for (SmallVector<DotDebugLocEntry, 4>::iterator
1977 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1978 I != E; ++I) {
1979 DotDebugLocEntry &Entry = *I;
1980 if (I + 1 != DotDebugLocEntries.end())
1981 Entry.Merge(I+1);
1982 }
1983
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001984 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001985 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00001986 Asm->getObjFileLowering().getDwarfLocSection());
1987 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001988 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1989 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001990 for (SmallVector<DotDebugLocEntry, 4>::iterator
1991 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00001992 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00001993 DotDebugLocEntry &Entry = *I;
1994 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00001995 if (Entry.isEmpty()) {
1996 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1997 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001998 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00001999 } else {
2000 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2001 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00002002 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002003 Asm->OutStreamer.AddComment("Loc expr size");
2004 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2005 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2006 Asm->EmitLabelDifference(end, begin, 2);
2007 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002008 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002009 DIBasicType BTy(DV.getType());
2010 if (BTy.Verify() &&
2011 (BTy.getEncoding() == dwarf::DW_ATE_signed
2012 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2013 Asm->OutStreamer.AddComment("DW_OP_consts");
2014 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002015 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002016 } else {
2017 Asm->OutStreamer.AddComment("DW_OP_constu");
2018 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002019 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002020 }
Devang Pateled9fd452011-07-08 16:49:43 +00002021 } else if (Entry.isLocation()) {
2022 if (!DV.hasComplexAddress())
2023 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00002024 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00002025 else {
2026 // Complex address entry.
2027 unsigned N = DV.getNumAddrElements();
2028 unsigned i = 0;
2029 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2030 if (Entry.Loc.getOffset()) {
2031 i = 2;
2032 Asm->EmitDwarfRegOp(Entry.Loc);
2033 Asm->OutStreamer.AddComment("DW_OP_deref");
2034 Asm->EmitInt8(dwarf::DW_OP_deref);
2035 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2036 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2037 Asm->EmitSLEB128(DV.getAddrElement(1));
2038 } else {
2039 // If first address element is OpPlus then emit
2040 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2041 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2042 Asm->EmitDwarfRegOp(Loc);
2043 i = 2;
2044 }
2045 } else {
2046 Asm->EmitDwarfRegOp(Entry.Loc);
2047 }
2048
2049 // Emit remaining complex address elements.
2050 for (; i < N; ++i) {
2051 uint64_t Element = DV.getAddrElement(i);
2052 if (Element == DIBuilder::OpPlus) {
2053 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2054 Asm->EmitULEB128(DV.getAddrElement(++i));
2055 } else if (Element == DIBuilder::OpDeref)
2056 Asm->EmitInt8(dwarf::DW_OP_deref);
2057 else llvm_unreachable("unknown Opcode found in complex address");
2058 }
Devang Patel3e021532011-04-28 02:22:40 +00002059 }
Devang Patel3e021532011-04-28 02:22:40 +00002060 }
Devang Pateled9fd452011-07-08 16:49:43 +00002061 // else ... ignore constant fp. There is not any good way to
2062 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002063 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002064 }
2065 }
Bill Wendling480ff322009-05-20 23:21:38 +00002066}
2067
2068/// EmitDebugARanges - Emit visible names into a debug aranges section.
2069///
2070void DwarfDebug::EmitDebugARanges() {
2071 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002072 Asm->OutStreamer.SwitchSection(
2073 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002074}
2075
Devang Patel930143b2009-11-21 02:48:08 +00002076/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002077///
Devang Patel930143b2009-11-21 02:48:08 +00002078void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002079 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002080 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002081 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00002082 unsigned char Size = Asm->getTargetData().getPointerSize();
2083 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002084 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002085 I != E; ++I) {
2086 if (*I)
2087 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00002088 else
Devang Patel6c74a872010-04-27 19:46:33 +00002089 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00002090 }
Bill Wendling480ff322009-05-20 23:21:38 +00002091}
2092
Devang Patel930143b2009-11-21 02:48:08 +00002093/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002094///
Devang Patel930143b2009-11-21 02:48:08 +00002095void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002096 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002097 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002098 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002099 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002100 }
2101}
2102
Devang Patel930143b2009-11-21 02:48:08 +00002103/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002104/// Section Header:
2105/// 1. length of section
2106/// 2. Dwarf version number
2107/// 3. address size.
2108///
2109/// Entries (one "entry" for each function that was inlined):
2110///
2111/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2112/// otherwise offset into __debug_str for regular function name.
2113/// 2. offset into __debug_str section for regular function name.
2114/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2115/// instances for the function.
2116///
2117/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2118/// inlined instance; the die_offset points to the inlined_subroutine die in the
2119/// __debug_info section, and the low_pc is the starting address for the
2120/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002121void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002122 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002123 return;
2124
Devang Patel1a0df9a2010-05-10 22:49:55 +00002125 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002126 return;
2127
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002128 Asm->OutStreamer.SwitchSection(
2129 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002130
Chris Lattner566cae92010-03-09 23:52:58 +00002131 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002132 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2133 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002134
Chris Lattnera179b522010-04-04 19:25:43 +00002135 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002136
Chris Lattner566cae92010-03-09 23:52:58 +00002137 Asm->OutStreamer.AddComment("Dwarf Version");
2138 Asm->EmitInt16(dwarf::DWARF_VERSION);
2139 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002140 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002141
Devang Patel32cc43c2010-05-07 20:54:48 +00002142 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002143 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002144
Devang Patel32cc43c2010-05-07 20:54:48 +00002145 const MDNode *Node = *I;
2146 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002147 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002148 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002149 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002150 StringRef LName = SP.getLinkageName();
2151 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002152
Chris Lattner566cae92010-03-09 23:52:58 +00002153 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002154 if (LName.empty()) {
2155 Asm->OutStreamer.EmitBytes(Name, 0);
2156 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002157 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00002158 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2159 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002160
Chris Lattner566cae92010-03-09 23:52:58 +00002161 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00002162 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002163 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002164
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002165 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002166 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002167 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002168 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002169
Chris Lattner7bde8c02010-04-04 18:52:31 +00002170 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002171 Asm->OutStreamer.EmitSymbolValue(LI->first,
2172 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002173 }
2174 }
2175
Chris Lattnera179b522010-04-04 19:25:43 +00002176 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002177}