blob: 1991785b119028f8216180f3d1ddc1b22e9c6b47 [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"
Eric Christopher4996c702011-11-07 09:24:32 +000017#include "DwarfAccelTable.h"
Devang Patel5eb43192011-04-12 17:40:32 +000018#include "DwarfCompileUnit.h"
Bill Wendling30346342010-04-05 22:59:21 +000019#include "llvm/Constants.h"
Bill Wendlinge38859d2012-06-28 00:05:13 +000020#include "llvm/DebugInfo.h"
Bill Wendlingf799efd2012-06-29 08:32:07 +000021#include "llvm/DIBuilder.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000022#include "llvm/Module.h"
Devang Patel2d9e5322011-01-20 00:02:16 +000023#include "llvm/Instructions.h"
David Greene829b3e82009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000026#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000027#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000028#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000029#include "llvm/MC/MCSymbol.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000030#include "llvm/Target/TargetData.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000031#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000033#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000035#include "llvm/Target/TargetOptions.h"
Devang Patela86114b2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Bill Wendlingf799efd2012-06-29 08:32:07 +000039#include "llvm/ADT/Triple.h"
Devang Patel6c74a872010-04-27 19:46:33 +000040#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000043#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000044#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000045#include "llvm/Support/Timer.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000046#include "llvm/Support/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000047using namespace llvm;
48
Jim Grosbacha8683bb2010-07-21 21:21:52 +000049static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000050 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000051 cl::desc("Disable debug info printing"));
52
Dan Gohman7421ae42010-05-07 01:08:53 +000053static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000054 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman7421ae42010-05-07 01:08:53 +000055 cl::init(false));
56
Eric Christopher4996c702011-11-07 09:24:32 +000057static cl::opt<bool> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
58 cl::desc("Output prototype dwarf accelerator tables."),
59 cl::init(false));
60
Eric Christopher978fbff2012-08-23 07:10:46 +000061static cl::opt<bool> DarwinGDBCompat("darwin-gdb-compat", cl::Hidden,
62 cl::desc("Compatibility with Darwin gdb."),
63 cl::init(false));
64
Bill Wendlingfcc14142010-04-07 09:28:04 +000065namespace {
66 const char *DWARFGroupName = "DWARF Emission";
67 const char *DbgTimerName = "DWARF Debug Writer";
68} // end anonymous namespace
69
Bill Wendling2f921f82009-05-15 09:23:25 +000070//===----------------------------------------------------------------------===//
71
72/// Configuration values for initial hash set sizes (log2).
73///
Bill Wendling2f921f82009-05-15 09:23:25 +000074static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000075
76namespace llvm {
77
Nick Lewycky019d2552011-07-29 03:49:23 +000078DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +000079 DIType Ty = Var.getType();
80 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
81 // addresses instead.
82 if (Var.isBlockByrefVariable()) {
83 /* Byref variables, in Blocks, are declared by the programmer as
84 "SomeType VarName;", but the compiler creates a
85 __Block_byref_x_VarName struct, and gives the variable VarName
86 either the struct, or a pointer to the struct, as its type. This
87 is necessary for various behind-the-scenes things the compiler
88 needs to do with by-reference variables in blocks.
89
90 However, as far as the original *programmer* is concerned, the
91 variable should still have type 'SomeType', as originally declared.
92
93 The following function dives into the __Block_byref_x_VarName
94 struct to find the original type of the variable. This will be
95 passed back to the code generating the type for the Debug
96 Information Entry for the variable 'VarName'. 'VarName' will then
97 have the original type 'SomeType' in its debug information.
98
99 The original type 'SomeType' will be the type of the field named
100 'VarName' inside the __Block_byref_x_VarName struct.
101
102 NOTE: In order for this to not completely fail on the debugger
103 side, the Debug Information Entry for the variable VarName needs to
104 have a DW_AT_location that tells the debugger how to unwind through
105 the pointers and __Block_byref_x_VarName struct to find the actual
106 value of the variable. The function addBlockByrefType does this. */
107 DIType subType = Ty;
108 unsigned tag = Ty.getTag();
109
110 if (tag == dwarf::DW_TAG_pointer_type) {
111 DIDerivedType DTy = DIDerivedType(Ty);
112 subType = DTy.getTypeDerivedFrom();
113 }
114
115 DICompositeType blockStruct = DICompositeType(subType);
116 DIArray Elements = blockStruct.getTypeArray();
117
118 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
119 DIDescriptor Element = Elements.getElement(i);
120 DIDerivedType DT = DIDerivedType(Element);
121 if (getName() == DT.getName())
122 return (DT.getTypeDerivedFrom());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000123 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000124 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000125 return Ty;
126}
Bill Wendling2f921f82009-05-15 09:23:25 +0000127
Chris Lattnerf5d06362010-04-05 04:09:20 +0000128} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000129
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000130DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000131 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000132 AbbreviationsSet(InitAbbreviationsSetSize),
Benjamin Kramer07480082012-06-09 10:34:15 +0000133 SourceIdMap(DIEValueAllocator), StringPool(DIEValueAllocator),
Eric Christopherb1b94512012-08-01 18:19:01 +0000134 PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000135 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000136
Rafael Espindolaa7160962011-05-06 14:56:22 +0000137 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000138 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000139 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000140 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000141
Eric Christopher978fbff2012-08-23 07:10:46 +0000142 // Turn on accelerator tables and older gdb compatibility
143 // for Darwin.
144 if (Triple(M->getTargetTriple()).isOSDarwin()) {
Eric Christopherad9fe892012-04-02 17:58:52 +0000145 DwarfAccelTables = true;
Eric Christopher978fbff2012-08-23 07:10:46 +0000146 DarwinGDBCompat = true;
147 }
Eric Christopher4977f212012-08-23 22:36:36 +0000148
149 isDarwinGDBCompat = DarwinGDBCompat;
Eric Christopherad9fe892012-04-02 17:58:52 +0000150
Dan Gohman6e681a52010-06-18 15:56:31 +0000151 {
152 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
153 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000154 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000155}
156DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000157}
158
Eric Christophera7b61892011-11-07 09:18:38 +0000159/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
160/// temporary label to it if SymbolStem is specified.
161static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
162 const char *SymbolStem = 0) {
163 Asm->OutStreamer.SwitchSection(Section);
164 if (!SymbolStem) return 0;
165
166 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
167 Asm->OutStreamer.EmitLabel(TmpSym);
168 return TmpSym;
169}
170
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000171MCSymbol *DwarfDebug::getStringPool() {
172 return Asm->GetTempSymbol("section_str");
173}
174
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000175MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
176 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
177 if (Entry.first) return Entry.first;
178
179 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000180 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000181}
182
Devang Patel930143b2009-11-21 02:48:08 +0000183/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000184///
Devang Patel930143b2009-11-21 02:48:08 +0000185void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000186 // Profile the node so that we can make it unique.
187 FoldingSetNodeID ID;
188 Abbrev.Profile(ID);
189
190 // Check the set for priors.
191 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
192
193 // If it's newly added.
194 if (InSet == &Abbrev) {
195 // Add to abbreviation list.
196 Abbreviations.push_back(&Abbrev);
197
198 // Assign the vector position + 1 as its number.
199 Abbrev.setNumber(Abbreviations.size());
200 } else {
201 // Assign existing abbreviation number.
202 Abbrev.setNumber(InSet->getNumber());
203 }
204}
205
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000206/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-01-05 01:46:14 +0000207/// printer to not emit usual symbol prefix before the symbol name is used then
208/// return linkage name after skipping this special LLVM prefix.
209static StringRef getRealLinkageName(StringRef LinkageName) {
210 char One = '\1';
211 if (LinkageName.startswith(StringRef(&One, 1)))
212 return LinkageName.substr(1);
213 return LinkageName;
214}
215
Eric Christopherd9843b32011-11-10 19:25:34 +0000216static bool isObjCClass(StringRef Name) {
217 return Name.startswith("+") || Name.startswith("-");
218}
219
220static bool hasObjCCategory(StringRef Name) {
221 if (!isObjCClass(Name)) return false;
222
223 size_t pos = Name.find(')');
224 if (pos != std::string::npos) {
225 if (Name[pos+1] != ' ') return false;
226 return true;
227 }
228 return false;
229}
230
231static void getObjCClassCategory(StringRef In, StringRef &Class,
232 StringRef &Category) {
233 if (!hasObjCCategory(In)) {
234 Class = In.slice(In.find('[') + 1, In.find(' '));
235 Category = "";
236 return;
237 }
238
239 Class = In.slice(In.find('[') + 1, In.find('('));
240 Category = In.slice(In.find('[') + 1, In.find(' '));
241 return;
242}
243
244static StringRef getObjCMethodName(StringRef In) {
245 return In.slice(In.find(' ') + 1, In.find(']'));
246}
247
248// Add the various names to the Dwarf accelerator table names.
249static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
250 DIE* Die) {
251 if (!SP.isDefinition()) return;
252
253 TheCU->addAccelName(SP.getName(), Die);
254
255 // If the linkage name is different than the name, go ahead and output
256 // that as well into the name table.
257 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
258 TheCU->addAccelName(SP.getLinkageName(), Die);
259
260 // If this is an Objective-C selector name add it to the ObjC accelerator
261 // too.
262 if (isObjCClass(SP.getName())) {
263 StringRef Class, Category;
264 getObjCClassCategory(SP.getName(), Class, Category);
265 TheCU->addAccelObjC(Class, Die);
266 if (Category != "")
267 TheCU->addAccelObjC(Category, Die);
268 // Also add the base method name to the name table.
269 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
270 }
271}
272
Jim Grosbach042483e2009-11-21 23:12:12 +0000273/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +0000274/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
275/// If there are global variables in this scope then create and insert
276/// DIEs for these variables.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000277DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
278 const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000279 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000280
Chris Lattner3a383cb2010-04-05 00:13:49 +0000281 assert(SPDie && "Unable to find subprogram DIE!");
282 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000283
Devang Patel1d6bbd42011-04-22 23:10:17 +0000284 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000285 if (!SPDecl.isSubprogram()) {
Devang Patel1d6bbd42011-04-22 23:10:17 +0000286 // There is not any need to generate specification DIE for a function
287 // defined at compile unit level. If a function is defined inside another
288 // function then gdb prefers the definition at top level and but does not
289 // expect specification DIE in parent function. So avoid creating
290 // specification DIE for a function defined inside a function.
291 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
292 !SP.getContext().isFile() &&
293 !isSubprogramContext(SP.getContext())) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000294 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel1d6bbd42011-04-22 23:10:17 +0000295
296 // Add arguments.
297 DICompositeType SPTy = SP.getType();
298 DIArray Args = SPTy.getTypeArray();
299 unsigned SPTag = SPTy.getTag();
300 if (SPTag == dwarf::DW_TAG_subroutine_type)
301 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
302 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
303 DIType ATy = DIType(DIType(Args.getElement(i)));
304 SPCU->addType(Arg, ATy);
305 if (ATy.isArtificial())
306 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
307 SPDie->addChild(Arg);
308 }
309 DIE *SPDeclDie = SPDie;
310 SPDie = new DIE(dwarf::DW_TAG_subprogram);
311 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
312 SPDeclDie);
313 SPCU->addDie(SPDie);
314 }
Chris Lattner3a383cb2010-04-05 00:13:49 +0000315 }
Devang Patela37a95e2010-07-07 22:20:57 +0000316 // Pick up abstract subprogram DIE.
317 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
318 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000319 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
320 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000321 SPCU->addDie(SPDie);
322 }
323
Devang Patelf20c4f72011-04-12 22:53:02 +0000324 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
325 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
326 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
327 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000328 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
329 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000330 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000331
Eric Christopherd9843b32011-11-10 19:25:34 +0000332 // Add name to the name table, we do this here because we're guaranteed
333 // to have concrete versions of our DW_TAG_subprogram nodes.
334 addSubprogramNames(SPCU, SP, SPDie);
335
Chris Lattner3a383cb2010-04-05 00:13:49 +0000336 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000337}
338
Jim Grosbach042483e2009-11-21 23:12:12 +0000339/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +0000340/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000341DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
342 LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000343 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
344 if (Scope->isAbstractScope())
345 return ScopeDIE;
346
Devang Patel7e623022011-08-10 20:55:27 +0000347 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000348 if (Ranges.empty())
349 return 0;
350
Devang Patel7e623022011-08-10 20:55:27 +0000351 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000352 if (Ranges.size() > 1) {
353 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000354 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000355 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000356 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000357 DebugRangeSymbols.size()
358 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000359 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000360 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000361 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
362 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000363 }
364 DebugRangeSymbols.push_back(NULL);
365 DebugRangeSymbols.push_back(NULL);
366 return ScopeDIE;
367 }
368
Devang Patel9fc11702010-05-25 23:40:22 +0000369 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
370 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000371
Devang Patel9fc11702010-05-25 23:40:22 +0000372 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000373
Chris Lattnere13c3722010-03-09 01:58:53 +0000374 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
375 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000376
Devang Patelf20c4f72011-04-12 22:53:02 +0000377 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
378 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000379
380 return ScopeDIE;
381}
382
Devang Patel930143b2009-11-21 02:48:08 +0000383/// constructInlinedScopeDIE - This scope represents inlined body of
384/// a function. Construct DIE to represent this concrete inlined copy
385/// of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000386DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
387 LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +0000388 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000389 assert(Ranges.empty() == false &&
390 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000391
Devang Patelf098ce22011-07-27 00:34:13 +0000392 if (!Scope->getScopeNode())
393 return NULL;
394 DIScope DS(Scope->getScopeNode());
395 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patelf098ce22011-07-27 00:34:13 +0000396 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
397 if (!OriginDIE) {
398 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
399 return NULL;
400 }
401
Devang Patel7e623022011-08-10 20:55:27 +0000402 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000403 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
404 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000405
Devang Patel4c6bd662010-07-08 22:39:20 +0000406 if (StartLabel == 0 || EndLabel == 0) {
Craig Topperee4dab52012-02-05 08:31:47 +0000407 llvm_unreachable("Unexpected Start and End labels for a inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000408 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000409 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000410 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000411 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000412 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000413
Devang Patel73bc1722011-05-05 17:54:26 +0000414 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000415 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
416 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000417
Devang Patelf098ce22011-07-27 00:34:13 +0000418 if (Ranges.size() > 1) {
419 // .debug_range section has not been laid out yet. Emit offset in
420 // .debug_range as a uint, size 4, for now. emitDIE will handle
421 // DW_AT_ranges appropriately.
422 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000423 DebugRangeSymbols.size()
424 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000425 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000426 RE = Ranges.end(); RI != RE; ++RI) {
427 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
428 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
429 }
430 DebugRangeSymbols.push_back(NULL);
431 DebugRangeSymbols.push_back(NULL);
432 } else {
Devang Patel784077e2011-08-10 23:58:09 +0000433 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
434 StartLabel);
435 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
436 EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000437 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000438
439 InlinedSubprogramDIEs.insert(OriginDIE);
440
441 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000442 //.debug_inlined section specification does not clearly state how
443 // to emit inlined scope that is split into multiple instruction ranges.
444 // For now, use first instruction range and emit low_pc/high_pc pair and
445 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000446 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000447 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000448
449 if (I == InlineInfo.end()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000450 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000451 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000452 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000453 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000454
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000455 DILocation DL(Scope->getInlinedAt());
Eric Christopher0925c622012-03-26 21:38:38 +0000456 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
457 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patelf20c4f72011-04-12 22:53:02 +0000458 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000459
Eric Christopher8dda5d02011-12-04 06:02:38 +0000460 // Add name to the name table, we do this here because we're guaranteed
461 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
462 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
463
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000464 return ScopeDIE;
465}
466
Devang Patel930143b2009-11-21 02:48:08 +0000467/// constructScopeDIE - Construct a DIE for this scope.
Devang Patel3acc70e2011-08-15 22:04:40 +0000468DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000469 if (!Scope || !Scope->getScopeNode())
470 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000471
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000472 SmallVector<DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +0000473
474 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000475 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000476 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
477 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Patel3acc70e2011-08-15 22:04:40 +0000478 if (DIE *Arg =
479 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel6c622ef2011-03-01 22:58:55 +0000480 Children.push_back(Arg);
481
Eric Christopherf84354b2011-10-03 15:49:16 +0000482 // Collect lexical scope children first.
Devang Patel7e623022011-08-10 20:55:27 +0000483 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000484 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Patel3acc70e2011-08-15 22:04:40 +0000485 if (DIE *Variable =
486 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000487 Children.push_back(Variable);
Devang Patel7e623022011-08-10 20:55:27 +0000488 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000489 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000490 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000491 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000492 DIScope DS(Scope->getScopeNode());
493 DIE *ScopeDIE = NULL;
494 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000495 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000496 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000497 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000498 if (Scope->isAbstractScope()) {
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000499 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000500 // Note down abstract DIE.
501 if (ScopeDIE)
502 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
503 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000504 else
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000505 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000506 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000507 else {
508 // There is no need to emit empty lexical block DIE.
509 if (Children.empty())
510 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000511 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000512 }
513
Devang Patel23b2ae62010-03-29 22:59:58 +0000514 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000515
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000516 // Add children
517 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
518 E = Children.end(); I != E; ++I)
519 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000520
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000521 if (DS.isSubprogram())
Eric Christopherd9843b32011-11-10 19:25:34 +0000522 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000523
Eric Christopherd9843b32011-11-10 19:25:34 +0000524 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000525}
526
Bill Wendling2b128d72009-05-20 23:19:06 +0000527/// GetOrCreateSourceID - Look up the source id with the given directory and
528/// source file names. If none currently exists, create a new id and insert it
529/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
530/// maps as well.
Devang Patele01b75c2011-03-24 20:30:50 +0000531unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
532 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000533 // If FE did not provide a file name, then assume stdin.
534 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000535 return GetOrCreateSourceID("<stdin>", StringRef());
536
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000537 // TODO: this might not belong here. See if we can factor this better.
538 if (DirName == CompilationDir)
539 DirName = "";
540
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000541 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel871d0b12010-09-16 20:57:49 +0000542
Benjamin Kramer71b19732012-03-11 14:56:26 +0000543 // We look up the file/dir pair by concatenating them with a zero byte.
544 SmallString<128> NamePair;
545 NamePair += DirName;
546 NamePair += '\0'; // Zero bytes are not allowed in paths.
547 NamePair += FileName;
548
549 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
550 if (Ent.getValue() != SrcId)
551 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000552
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000553 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000554 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendling2b128d72009-05-20 23:19:06 +0000555
556 return SrcId;
557}
558
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000559/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +0000560/// metadata node with tag DW_TAG_compile_unit.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000561CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000562 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000563 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000564 CompilationDir = DIUnit.getDirectory();
565 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000566
567 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher3a2656b2012-02-22 08:46:13 +0000568 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die, Asm, this);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000569 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000570 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
571 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000572 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopherb1b94512012-08-01 18:19:01 +0000573 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
574 // into an entity.
575 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000576 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000577 // compile unit in debug_line section.
Rafael Espindolad7bdaf52012-06-22 13:24:07 +0000578 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000579 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000580 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000581 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000582 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000583
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000584 if (!CompilationDir.empty())
585 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000586 if (DIUnit.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000587 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +0000588
Devang Patel2d9caf92009-11-25 17:36:49 +0000589 StringRef Flags = DIUnit.getFlags();
590 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000591 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patelf20c4f72011-04-12 22:53:02 +0000592
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000593 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000594 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000595 dwarf::DW_FORM_data1, RVer);
596
Devang Patel1a0df9a2010-05-10 22:49:55 +0000597 if (!FirstCU)
598 FirstCU = NewCU;
599 CUMap.insert(std::make_pair(N, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000600 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000601}
602
Devang Patel1a0df9a2010-05-10 22:49:55 +0000603/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel1f4f98d2011-08-15 23:36:40 +0000604void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
605 const MDNode *N) {
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000606 CompileUnit *&CURef = SPMap[N];
607 if (CURef)
608 return;
609 CURef = TheCU;
610
Devang Patel80ae3492009-08-28 23:24:31 +0000611 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000612 if (!SP.isDefinition())
613 // This is a method declaration which will be handled while constructing
614 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000615 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000616
Devang Patel89543712011-08-15 17:24:54 +0000617 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000618
619 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000620 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000621
622 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000623 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000624
Devang Patel0751a282009-06-26 01:49:18 +0000625 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000626}
627
Devang Patel07bb9ee2011-08-15 23:47:24 +0000628/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
629/// as llvm.dbg.enum and llvm.dbg.ty
630void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000631 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
632 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
633 const MDNode *N = NMD->getOperand(i);
634 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
635 constructSubprogramDIE(CU, N);
636 }
637
638 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
639 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
640 const MDNode *N = NMD->getOperand(i);
641 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000642 CU->createGlobalVariableDIE(N);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000643 }
644
Devang Patel07bb9ee2011-08-15 23:47:24 +0000645 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
646 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
647 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000648 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
649 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000650 }
651
652 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
653 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
654 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000655 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
656 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000657 }
658}
659
660/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
661/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
662bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
663 DebugInfoFinder DbgFinder;
664 DbgFinder.processModule(*M);
665
666 bool HasDebugInfo = false;
667 // Scan all the compile-units to see if there are any marked as the main
668 // unit. If not, we do not generate debug info.
669 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
670 E = DbgFinder.compile_unit_end(); I != E; ++I) {
671 if (DICompileUnit(*I).isMain()) {
672 HasDebugInfo = true;
673 break;
674 }
675 }
676 if (!HasDebugInfo) return false;
677
678 // Create all the compile unit DIEs.
679 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
680 E = DbgFinder.compile_unit_end(); I != E; ++I)
681 constructCompileUnit(*I);
682
683 // Create DIEs for each global variable.
684 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
685 E = DbgFinder.global_variable_end(); I != E; ++I) {
686 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000687 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000688 CU->createGlobalVariableDIE(N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000689 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000690
Devang Patel07bb9ee2011-08-15 23:47:24 +0000691 // Create DIEs for each subprogram.
692 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
693 E = DbgFinder.subprogram_end(); I != E; ++I) {
694 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000695 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
696 constructSubprogramDIE(CU, N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000697 }
698
699 return HasDebugInfo;
700}
701
Devang Patel930143b2009-11-21 02:48:08 +0000702/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000703/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky019d2552011-07-29 03:49:23 +0000704/// This is invoked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +0000705void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +0000706 if (DisableDebugInfoPrinting)
707 return;
708
Nick Lewycky019d2552011-07-29 03:49:23 +0000709 // If module has named metadata anchors then use them, otherwise scan the
710 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000711 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
712 if (CU_Nodes) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000713 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
714 DICompileUnit CUNode(CU_Nodes->getOperand(i));
715 CompileUnit *CU = constructCompileUnit(CUNode);
716 DIArray GVs = CUNode.getGlobalVariables();
717 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000718 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000719 DIArray SPs = CUNode.getSubprograms();
720 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
721 constructSubprogramDIE(CU, SPs.getElement(i));
722 DIArray EnumTypes = CUNode.getEnumTypes();
723 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
724 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
725 DIArray RetainedTypes = CUNode.getRetainedTypes();
726 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
727 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
728 }
Devang Patel07bb9ee2011-08-15 23:47:24 +0000729 } else if (!collectLegacyDebugInfo(M))
730 return;
Devang Patele02e5852011-05-03 16:45:22 +0000731
Devang Patel07bb9ee2011-08-15 23:47:24 +0000732 collectInfoFromNamedMDNodes(M);
Devang Patele02e5852011-05-03 16:45:22 +0000733
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000734 // Tell MMI that we have debug info.
735 MMI->setDebugInfoAvailability(true);
Devang Patele02e5852011-05-03 16:45:22 +0000736
Chris Lattnerd442aa32010-04-04 23:17:54 +0000737 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000738 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000739
Bill Wendling2b128d72009-05-20 23:19:06 +0000740 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000741 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000742}
743
Devang Patel930143b2009-11-21 02:48:08 +0000744/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +0000745///
Devang Patel930143b2009-11-21 02:48:08 +0000746void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000747 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +0000748 const Module *M = MMI->getModule();
Devang Patel7e623022011-08-10 20:55:27 +0000749 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +0000750
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000751 // Collect info for variables that were optimized out.
752 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
753 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
754 DICompileUnit TheCU(CU_Nodes->getOperand(i));
755 DIArray Subprograms = TheCU.getSubprograms();
756 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
757 DISubprogram SP(Subprograms.getElement(i));
758 if (ProcessedSPNodes.count(SP) != 0) continue;
759 if (!SP.Verify()) continue;
760 if (!SP.isDefinition()) continue;
Devang Patel59e27c52011-08-19 23:28:12 +0000761 DIArray Variables = SP.getVariables();
762 if (Variables.getNumElements() == 0) continue;
763
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000764 LexicalScope *Scope =
765 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
766 DeadFnScopeMap[SP] = Scope;
767
768 // Construct subprogram DIE and add variables DIEs.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000769 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000770 assert(SPCU && "Unable to find Compile Unit!");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000771 constructSubprogramDIE(SPCU, SP);
772 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel59e27c52011-08-19 23:28:12 +0000773 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
774 DIVariable DV(Variables.getElement(vi));
775 if (!DV.Verify()) continue;
776 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000777 if (DIE *VariableDIE =
Devang Patel59e27c52011-08-19 23:28:12 +0000778 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000779 ScopeDIE->addChild(VariableDIE);
780 }
Devang Patelf3b2db62010-06-28 18:25:03 +0000781 }
782 }
783 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000784
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000785 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
786 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
787 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
788 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000789 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000790 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000791 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
792 AE = AbstractSPDies.end(); AI != AE; ++AI) {
793 DIE *ISP = AI->second;
794 if (InlinedSubprogramDIEs.count(ISP))
795 continue;
796 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
797 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000798
Eric Christopherb1b94512012-08-01 18:19:01 +0000799 // Emit DW_AT_containing_type attribute to connect types with their
800 // vtable holding type.
Devang Patel89543712011-08-15 17:24:54 +0000801 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
802 CUE = CUMap.end(); CUI != CUE; ++CUI) {
803 CompileUnit *TheCU = CUI->second;
804 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000805 }
806
Bill Wendling2b128d72009-05-20 23:19:06 +0000807 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000808 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000809 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000810 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000811 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000812
813 // End text sections.
814 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000815 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +0000816 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +0000817 }
818
Bill Wendling2b128d72009-05-20 23:19:06 +0000819 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +0000820 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +0000821
822 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +0000823 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000824
825 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +0000826 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +0000827
Eric Christophera876b822012-08-23 07:32:06 +0000828 // Emit info into the dwarf accelerator table sections.
Eric Christopher4996c702011-11-07 09:24:32 +0000829 if (DwarfAccelTables) {
830 emitAccelNames();
831 emitAccelObjC();
832 emitAccelNamespaces();
833 emitAccelTypes();
834 }
835
Devang Patel04d2f2d2009-11-24 01:14:22 +0000836 // Emit info into a debug pubtypes section.
Eric Christopher77826182012-08-23 07:10:56 +0000837 // TODO: When we don't need the option anymore we can
838 // remove all of the code that adds to the table.
839 if (DarwinGDBCompat)
840 emitDebugPubTypes();
Devang Patel04d2f2d2009-11-24 01:14:22 +0000841
Bill Wendling2b128d72009-05-20 23:19:06 +0000842 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +0000843 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +0000844
845 // Emit info into a debug aranges section.
846 EmitDebugARanges();
847
848 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +0000849 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +0000850
851 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +0000852 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000853
854 // Emit inline info.
Eric Christopher3a47c3e2012-08-23 07:32:02 +0000855 // TODO: When we don't need the option anymore we
856 // can remove all of the code that this section
857 // depends upon.
858 if (DarwinGDBCompat)
859 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000860
Eric Christopher7524fe42012-03-02 00:30:24 +0000861 // Emit info into a debug str section.
862 emitDebugStr();
863
Devang Pateld0701282010-08-02 17:32:15 +0000864 // clean up.
865 DeleteContainerSeconds(DeadFnScopeMap);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000866 SPMap.clear();
Devang Patel1a0df9a2010-05-10 22:49:55 +0000867 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
868 E = CUMap.end(); I != E; ++I)
869 delete I->second;
870 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +0000871}
872
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000873/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +0000874DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +0000875 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +0000876 LLVMContext &Ctx = DV->getContext();
877 // More then one inlined variable corresponds to one abstract variable.
878 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000879 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000880 if (AbsDbgVariable)
881 return AbsDbgVariable;
882
Devang Patel7e623022011-08-10 20:55:27 +0000883 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000884 if (!Scope)
885 return NULL;
886
Devang Patel99819b52011-08-15 19:01:20 +0000887 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +0000888 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000889 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000890 return AbsDbgVariable;
891}
892
Nick Lewycky019d2552011-07-29 03:49:23 +0000893/// addCurrentFnArgument - If Var is a current function argument then add
894/// it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +0000895bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +0000896 DbgVariable *Var, LexicalScope *Scope) {
897 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000898 return false;
899 DIVariable DV = Var->getVariable();
900 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
901 return false;
902 unsigned ArgNo = DV.getArgNumber();
903 if (ArgNo == 0)
904 return false;
905
Devang Patel4ab660b2011-03-03 20:02:02 +0000906 size_t Size = CurrentFnArguments.size();
907 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +0000908 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +0000909 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +0000910 // arguments does the function have at source level.
911 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +0000912 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +0000913 CurrentFnArguments[ArgNo - 1] = Var;
914 return true;
915}
916
Devang Patel490c8ab2010-05-20 19:57:06 +0000917/// collectVariableInfoFromMMITable - Collect variable information from
918/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000919void
Nick Lewycky019d2552011-07-29 03:49:23 +0000920DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +0000921 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +0000922 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
923 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
924 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000925 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000926 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +0000927 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +0000928 DIVariable DV(Var);
929 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000930
Devang Patel7e623022011-08-10 20:55:27 +0000931 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000932
Devang Patelcdb7d442009-11-10 23:20:04 +0000933 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +0000934 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +0000935 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000936
Devang Patele1c53f22010-05-20 16:36:41 +0000937 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +0000938 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patel3e4a9652011-08-15 21:24:36 +0000939 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +0000940 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000941 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +0000942 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +0000943 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +0000944 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000945}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000946
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000947/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +0000948/// DBG_VALUE instruction, is in a defined reg.
949static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000950 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000951 return MI->getNumOperands() == 3 &&
952 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
953 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000954}
955
Nick Lewycky019d2552011-07-29 03:49:23 +0000956/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel2442a892011-07-08 17:09:57 +0000957/// at MI.
958static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
959 const MCSymbol *FLabel,
960 const MCSymbol *SLabel,
961 const MachineInstr *MI) {
962 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
963
964 if (MI->getNumOperands() != 3) {
965 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
966 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
967 }
968 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
969 MachineLocation MLoc;
970 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
971 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
972 }
973 if (MI->getOperand(0).isImm())
974 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
975 if (MI->getOperand(0).isFPImm())
976 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
977 if (MI->getOperand(0).isCImm())
978 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
979
Craig Topperee4dab52012-02-05 08:31:47 +0000980 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +0000981}
982
Devang Patel7e623022011-08-10 20:55:27 +0000983/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000984void
Devang Patel5c0f85c2010-06-25 22:07:34 +0000985DwarfDebug::collectVariableInfo(const MachineFunction *MF,
986 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000987
Devang Patel490c8ab2010-05-20 19:57:06 +0000988 /// collection info from MMI table.
989 collectVariableInfoFromMMITable(MF, Processed);
990
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000991 for (SmallVectorImpl<const MDNode*>::const_iterator
992 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
993 ++UVI) {
994 const MDNode *Var = *UVI;
995 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +0000996 continue;
997
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000998 // History contains relevant DBG_VALUE instructions for Var and instructions
999 // clobbering it.
1000 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1001 if (History.empty())
1002 continue;
1003 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001004
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001005 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001006 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001007 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1008 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001009 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +00001010 else {
1011 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +00001012 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +00001013 else {
1014 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +00001015 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001016 else
Devang Patel7e623022011-08-10 20:55:27 +00001017 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001018 }
1019 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001020 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001021 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001022 continue;
1023
1024 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001025 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001026 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1027 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +00001028 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001029 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001030 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001031 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001032
1033 // Simple ranges that are fully coalesced.
1034 if (History.size() <= 1 || (History.size() == 2 &&
1035 MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001036 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001037 continue;
1038 }
1039
1040 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001041 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001042
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001043 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1044 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1045 const MachineInstr *Begin = *HI;
1046 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001047
Devang Patele7181b52011-06-01 23:00:17 +00001048 // Check if DBG_VALUE is truncating a range.
1049 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1050 && !Begin->getOperand(0).getReg())
1051 continue;
1052
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001053 // Compute the range for a register location.
1054 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1055 const MCSymbol *SLabel = 0;
1056
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001057 if (HI + 1 == HE)
1058 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001059 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001060 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001061 else {
1062 const MachineInstr *End = HI[1];
Devang Patel53b050a2011-07-07 21:44:42 +00001063 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1064 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001065 if (End->isDebugValue())
1066 SLabel = getLabelBeforeInsn(End);
1067 else {
1068 // End is a normal instruction clobbering the range.
1069 SLabel = getLabelAfterInsn(End);
1070 assert(SLabel && "Forgot label after clobber instruction");
1071 ++HI;
1072 }
1073 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001074
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001075 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopher365d0832011-12-16 23:42:31 +00001076 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1077 Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001078 }
1079 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001080 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001081
1082 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001083 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1084 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1085 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1086 DIVariable DV(Variables.getElement(i));
1087 if (!DV || !DV.Verify() || !Processed.insert(DV))
1088 continue;
1089 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1090 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +00001091 }
Devang Patel9fc11702010-05-25 23:40:22 +00001092}
Devang Patele0a94bf2010-05-14 21:01:35 +00001093
Devang Patel9fc11702010-05-25 23:40:22 +00001094/// getLabelBeforeInsn - Return Label preceding the instruction.
1095const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001096 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1097 assert(Label && "Didn't insert label before instruction");
1098 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001099}
1100
1101/// getLabelAfterInsn - Return Label immediately following the instruction.
1102const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001103 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001104}
1105
Devang Patelb5694e72010-10-26 17:49:02 +00001106/// beginInstruction - Process beginning of an instruction.
1107void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001108 // Check if source location changes, but ignore DBG_VALUE locations.
1109 if (!MI->isDebugValue()) {
1110 DebugLoc DL = MI->getDebugLoc();
1111 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001112 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001113 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001114 if (DL == PrologEndLoc) {
1115 Flags |= DWARF2_FLAG_PROLOGUE_END;
1116 PrologEndLoc = DebugLoc();
1117 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001118 if (PrologEndLoc.isUnknown())
1119 Flags |= DWARF2_FLAG_IS_STMT;
1120
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001121 if (!DL.isUnknown()) {
1122 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001123 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001124 } else
Devang Patel34a66202011-05-11 19:22:19 +00001125 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001126 }
Devang Patel9fc11702010-05-25 23:40:22 +00001127 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001128
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001129 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001130 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1131 LabelsBeforeInsn.find(MI);
1132
1133 // No label needed.
1134 if (I == LabelsBeforeInsn.end())
1135 return;
1136
1137 // Label already assigned.
1138 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001139 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001140
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001141 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001142 PrevLabel = MMI->getContext().CreateTempSymbol();
1143 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001144 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001145 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001146}
1147
Devang Patelb5694e72010-10-26 17:49:02 +00001148/// endInstruction - Process end of an instruction.
1149void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001150 // Don't create a new label after DBG_VALUE instructions.
1151 // They don't generate code.
1152 if (!MI->isDebugValue())
1153 PrevLabel = 0;
1154
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001155 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1156 LabelsAfterInsn.find(MI);
1157
1158 // No label needed.
1159 if (I == LabelsAfterInsn.end())
1160 return;
1161
1162 // Label already assigned.
1163 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001164 return;
1165
1166 // We need a label after this instruction.
1167 if (!PrevLabel) {
1168 PrevLabel = MMI->getContext().CreateTempSymbol();
1169 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001170 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001171 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001172}
1173
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001174/// identifyScopeMarkers() -
Devang Patel784077e2011-08-10 23:58:09 +00001175/// Each LexicalScope has first instruction and last instruction to mark
1176/// beginning and end of a scope respectively. Create an inverse map that list
1177/// scopes starts (and ends) with an instruction. One instruction may start (or
1178/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001179void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001180 SmallVector<LexicalScope *, 4> WorkList;
1181 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001182 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001183 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001184
Devang Patel7e623022011-08-10 20:55:27 +00001185 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001186 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001187 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001188 SE = Children.end(); SI != SE; ++SI)
1189 WorkList.push_back(*SI);
1190
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001191 if (S->isAbstractScope())
1192 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001193
Devang Patel7e623022011-08-10 20:55:27 +00001194 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001195 if (Ranges.empty())
1196 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001197 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001198 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001199 assert(RI->first && "InsnRange does not have first instruction!");
1200 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001201 requestLabelBeforeInsn(RI->first);
1202 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001203 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001204 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001205}
1206
Devang Patel589845d2011-05-09 22:14:49 +00001207/// getScopeNode - Get MDNode for DebugLoc's scope.
1208static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1209 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1210 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1211 return DL.getScope(Ctx);
1212}
1213
Devang Patel34a66202011-05-11 19:22:19 +00001214/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher34164192012-04-03 00:43:49 +00001215/// line number info for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001216static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1217 const MDNode *Scope = getScopeNode(DL, Ctx);
1218 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher34164192012-04-03 00:43:49 +00001219 if (SP.Verify()) {
1220 // Check for number of operands since the compatibility is
1221 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001222 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001223 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1224 else
1225 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1226 }
1227
Devang Patel34a66202011-05-11 19:22:19 +00001228 return DebugLoc();
1229}
1230
Devang Patel930143b2009-11-21 02:48:08 +00001231/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001232/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001233void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001234 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001235 LScopes.initialize(*MF);
1236 if (LScopes.empty()) return;
1237 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001238
Devang Patel6c74a872010-04-27 19:46:33 +00001239 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1240 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001241 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001242 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001243
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001244 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1245
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001246 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001247 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1248 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1249
Devang Patel002d54d2010-05-26 19:37:24 +00001250 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001251 I != E; ++I) {
1252 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001253 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1254 II != IE; ++II) {
1255 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001256
Devang Patel002d54d2010-05-26 19:37:24 +00001257 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001258 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001259
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001260 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001261 const MDNode *Var =
1262 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001263
1264 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001265 if (isDbgValueInDefinedReg(MI))
1266 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1267
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001268 // Check the history of this variable.
1269 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1270 if (History.empty()) {
1271 UserVariables.push_back(Var);
1272 // The first mention of a function argument gets the FunctionBeginSym
1273 // label, so arguments are visible when breaking at function entry.
1274 DIVariable DV(Var);
1275 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1276 DISubprogram(getDISubprogram(DV.getContext()))
1277 .describes(MF->getFunction()))
1278 LabelsBeforeInsn[MI] = FunctionBeginSym;
1279 } else {
1280 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1281 const MachineInstr *Prev = History.back();
1282 if (Prev->isDebugValue()) {
1283 // Coalesce identical entries at the end of History.
1284 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001285 Prev->isIdenticalTo(History[History.size() - 2])) {
1286 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1287 << "\t" << *Prev
1288 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001289 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001290 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001291
1292 // Terminate old register assignments that don't reach MI;
1293 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1294 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1295 isDbgValueInDefinedReg(Prev)) {
1296 // Previous register assignment needs to terminate at the end of
1297 // its basic block.
1298 MachineBasicBlock::const_iterator LastMI =
1299 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001300 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001301 // Drop DBG_VALUE for empty range.
Devang Patelb7a328e2011-07-07 00:14:27 +00001302 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1303 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001304 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001305 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001306 else {
1307 // Terminate after LastMI.
1308 History.push_back(LastMI);
1309 }
1310 }
1311 }
1312 }
1313 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001314 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001315 // Not a DBG_VALUE instruction.
1316 if (!MI->isLabel())
1317 AtBlockEntry = false;
1318
Devang Patel34a66202011-05-11 19:22:19 +00001319 // First known non DBG_VALUE location marks beginning of function
1320 // body.
1321 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1322 PrologEndLoc = MI->getDebugLoc();
1323
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001324 // Check if the instruction clobbers any registers with debug vars.
1325 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1326 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1327 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1328 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001329 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1330 AI.isValid(); ++AI) {
1331 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001332 const MDNode *Var = LiveUserVar[Reg];
1333 if (!Var)
1334 continue;
1335 // Reg is now clobbered.
1336 LiveUserVar[Reg] = 0;
1337
1338 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001339 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1340 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001341 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001342 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1343 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001344 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001345 const MachineInstr *Prev = History.back();
1346 // Sanity-check: Register assignments are terminated at the end of
1347 // their block.
1348 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1349 continue;
1350 // Is the variable still in Reg?
1351 if (!isDbgValueInDefinedReg(Prev) ||
1352 Prev->getOperand(0).getReg() != Reg)
1353 continue;
1354 // Var is clobbered. Make sure the next instruction gets a label.
1355 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001356 }
1357 }
Devang Patel002d54d2010-05-26 19:37:24 +00001358 }
Devang Patel002d54d2010-05-26 19:37:24 +00001359 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001360 }
1361
1362 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1363 I != E; ++I) {
1364 SmallVectorImpl<const MachineInstr*> &History = I->second;
1365 if (History.empty())
1366 continue;
1367
1368 // Make sure the final register assignments are terminated.
1369 const MachineInstr *Prev = History.back();
1370 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1371 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel784077e2011-08-10 23:58:09 +00001372 MachineBasicBlock::const_iterator LastMI =
1373 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001374 if (LastMI == PrevMBB->end())
1375 // Drop DBG_VALUE for empty range.
1376 History.pop_back();
1377 else {
1378 // Terminate after LastMI.
1379 History.push_back(LastMI);
1380 }
1381 }
1382 // Request labels for the full history.
1383 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1384 const MachineInstr *MI = History[i];
1385 if (MI->isDebugValue())
1386 requestLabelBeforeInsn(MI);
1387 else
1388 requestLabelAfterInsn(MI);
1389 }
1390 }
Devang Patel002d54d2010-05-26 19:37:24 +00001391
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001392 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001393 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001394
1395 // Record beginning of function.
1396 if (!PrologEndLoc.isUnknown()) {
1397 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1398 MF->getFunction()->getContext());
1399 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1400 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopherbf570912012-07-12 23:30:25 +00001401 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0);
Devang Patel34a66202011-05-11 19:22:19 +00001402 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001403}
1404
Devang Patel7e623022011-08-10 20:55:27 +00001405void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1406// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1407 ScopeVariables[LS].push_back(Var);
1408// Vars.push_back(Var);
1409}
1410
Devang Patel930143b2009-11-21 02:48:08 +00001411/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00001412///
Chris Lattner76555b52010-01-26 23:18:02 +00001413void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001414 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001415
Devang Patel7e623022011-08-10 20:55:27 +00001416 // Define end label for subprogram.
1417 FunctionEndSym = Asm->GetTempSymbol("func_end",
1418 Asm->getFunctionNumber());
1419 // Assumes in correct section after the entry point.
1420 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1421
1422 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1423 collectVariableInfo(MF, ProcessedVars);
1424
Devang Patel3acc70e2011-08-15 22:04:40 +00001425 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001426 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001427 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001428
Devang Patel7e623022011-08-10 20:55:27 +00001429 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001430 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1431 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1432 LexicalScope *AScope = AList[i];
1433 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001434 if (SP.Verify()) {
1435 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001436 DIArray Variables = SP.getVariables();
1437 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1438 DIVariable DV(Variables.getElement(i));
1439 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1440 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001441 // Check that DbgVariable for DV wasn't created earlier, when
1442 // findAbstractVariable() was called for inlined instance of DV.
1443 LLVMContext &Ctx = DV->getContext();
1444 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1445 if (AbstractVariables.lookup(CleanDV))
1446 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001447 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1448 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001449 }
1450 }
Devang Patel44403472011-08-12 18:10:19 +00001451 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Patel3acc70e2011-08-15 22:04:40 +00001452 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001453 }
Devang Patel7e623022011-08-10 20:55:27 +00001454
Devang Patel3acc70e2011-08-15 22:04:40 +00001455 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patel7e623022011-08-10 20:55:27 +00001456
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001457 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Devang Patel784077e2011-08-10 23:58:09 +00001458 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1459 dwarf::DW_FORM_flag, 1);
Devang Patel3acc70e2011-08-15 22:04:40 +00001460
Devang Patel7e623022011-08-10 20:55:27 +00001461 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1462 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001463
Bill Wendling2b128d72009-05-20 23:19:06 +00001464 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001465 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1466 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1467 DeleteContainerPointers(I->second);
1468 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001469 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001470 UserVariables.clear();
1471 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001472 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001473 LabelsBeforeInsn.clear();
1474 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001475 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001476}
1477
Chris Lattnerba35a672010-03-09 04:54:43 +00001478/// recordSourceLine - Register a source line with debug info. Returns the
1479/// unique label that was emitted and which provides correspondence to
1480/// the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001481void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1482 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001483 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001484 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001485 unsigned Src = 1;
1486 if (S) {
1487 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001488
Dan Gohman50849c62010-05-05 23:41:32 +00001489 if (Scope.isCompileUnit()) {
1490 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001491 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001492 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001493 } else if (Scope.isFile()) {
1494 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001495 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001496 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001497 } else if (Scope.isSubprogram()) {
1498 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001499 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001500 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001501 } else if (Scope.isLexicalBlockFile()) {
1502 DILexicalBlockFile DBF(S);
1503 Fn = DBF.getFilename();
1504 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001505 } else if (Scope.isLexicalBlock()) {
1506 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001507 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001508 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001509 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001510 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001511
Devang Patele01b75c2011-03-24 20:30:50 +00001512 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001513 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001514 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001515}
1516
Bill Wendling806535f2009-05-20 23:22:40 +00001517//===----------------------------------------------------------------------===//
1518// Emit Methods
1519//===----------------------------------------------------------------------===//
1520
Devang Patel930143b2009-11-21 02:48:08 +00001521/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00001522///
Jim Grosbach00e9c612009-11-22 19:20:36 +00001523unsigned
1524DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00001525 // Get the children.
1526 const std::vector<DIE *> &Children = Die->getChildren();
1527
Bill Wendling480ff322009-05-20 23:21:38 +00001528 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001529 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001530
1531 // Get the abbreviation for this DIE.
1532 unsigned AbbrevNumber = Die->getAbbrevNumber();
1533 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1534
1535 // Set DIE offset
1536 Die->setOffset(Offset);
1537
1538 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001539 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001540
1541 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1542 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1543
1544 // Size the DIE attribute values.
1545 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1546 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001547 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001548
1549 // Size the DIE children if any.
1550 if (!Children.empty()) {
1551 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1552 "Children flag not set");
1553
1554 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001555 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00001556
1557 // End of children marker.
1558 Offset += sizeof(int8_t);
1559 }
1560
1561 Die->setSize(Offset - Die->getOffset());
1562 return Offset;
1563}
1564
Devang Patel930143b2009-11-21 02:48:08 +00001565/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00001566///
Devang Patel930143b2009-11-21 02:48:08 +00001567void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001568 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1569 E = CUMap.end(); I != E; ++I) {
1570 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00001571 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00001572 sizeof(int32_t) + // Length of Compilation Unit Info
1573 sizeof(int16_t) + // DWARF version number
1574 sizeof(int32_t) + // Offset Into Abbrev. Section
1575 sizeof(int8_t); // Pointer Size (in bytes)
1576 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001577 }
Bill Wendling480ff322009-05-20 23:21:38 +00001578}
1579
Chris Lattner6629ca92010-04-04 22:59:04 +00001580/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1581/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00001582void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001583 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001584
Bill Wendling480ff322009-05-20 23:21:38 +00001585 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001586 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001587 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001588 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001589 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001590 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001591
Chris Lattner6629ca92010-04-04 22:59:04 +00001592 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001593 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001594
Devang Patel4a213872010-08-24 00:06:12 +00001595 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001596 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001597 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001598 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001599 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00001600 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1601 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001602
Devang Patel9fc11702010-05-25 23:40:22 +00001603 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1604 "section_debug_loc");
1605
Chris Lattner6629ca92010-04-04 22:59:04 +00001606 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00001607 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001608}
1609
Nick Lewycky019d2552011-07-29 03:49:23 +00001610/// emitDIE - Recursively emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00001611///
Devang Patel930143b2009-11-21 02:48:08 +00001612void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001613 // Get the abbreviation for this DIE.
1614 unsigned AbbrevNumber = Die->getAbbrevNumber();
1615 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1616
Bill Wendling480ff322009-05-20 23:21:38 +00001617 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001618 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001619 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1620 Twine::utohexstr(Die->getOffset()) + ":0x" +
1621 Twine::utohexstr(Die->getSize()) + " " +
1622 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001623 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001624
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001625 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001626 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1627
1628 // Emit the DIE attribute values.
1629 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1630 unsigned Attr = AbbrevData[i].getAttribute();
1631 unsigned Form = AbbrevData[i].getForm();
1632 assert(Form && "Too many attributes for DIE (check abbreviation)");
1633
Chris Lattner7bde8c02010-04-04 18:52:31 +00001634 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001635 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001636
Bill Wendling480ff322009-05-20 23:21:38 +00001637 switch (Attr) {
Bill Wendling480ff322009-05-20 23:21:38 +00001638 case dwarf::DW_AT_abstract_origin: {
1639 DIEEntry *E = cast<DIEEntry>(Values[i]);
1640 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001641 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001642 Asm->EmitInt32(Addr);
1643 break;
1644 }
Devang Patel12563b32010-04-16 23:33:45 +00001645 case dwarf::DW_AT_ranges: {
1646 // DW_AT_range Value encodes offset in debug_range section.
1647 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001648
Nick Lewycky33da3362012-06-22 01:25:12 +00001649 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelda3ef852010-09-02 16:43:44 +00001650 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1651 V->getValue(),
1652 4);
1653 } else {
1654 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1655 V->getValue(),
1656 DwarfDebugRangeSectionSym,
1657 4);
1658 }
Devang Patel12563b32010-04-16 23:33:45 +00001659 break;
1660 }
Devang Patel9fc11702010-05-25 23:40:22 +00001661 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00001662 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1663 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1664 Asm->EmitLabelReference(L->getValue(), 4);
1665 else
1666 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1667 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00001668 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00001669 }
Devang Patel9fc11702010-05-25 23:40:22 +00001670 break;
1671 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001672 case dwarf::DW_AT_accessibility: {
1673 if (Asm->isVerbose()) {
1674 DIEInteger *V = cast<DIEInteger>(Values[i]);
1675 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1676 }
1677 Values[i]->EmitValue(Asm, Form);
1678 break;
1679 }
Bill Wendling480ff322009-05-20 23:21:38 +00001680 default:
1681 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001682 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001683 break;
1684 }
Bill Wendling480ff322009-05-20 23:21:38 +00001685 }
1686
1687 // Emit the DIE children if any.
1688 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1689 const std::vector<DIE *> &Children = Die->getChildren();
1690
1691 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001692 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00001693
Chris Lattner7bde8c02010-04-04 18:52:31 +00001694 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001695 Asm->OutStreamer.AddComment("End Of Children Mark");
1696 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001697 }
1698}
1699
Devang Patel9ccfb642009-12-09 18:24:21 +00001700/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00001701///
Devang Patel9ccfb642009-12-09 18:24:21 +00001702void DwarfDebug::emitDebugInfo() {
1703 // Start debug info section.
1704 Asm->OutStreamer.SwitchSection(
1705 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001706 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1707 E = CUMap.end(); I != E; ++I) {
1708 CompileUnit *TheCU = I->second;
1709 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001710
Devang Patel1a0df9a2010-05-10 22:49:55 +00001711 // Emit the compile units header.
1712 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1713 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001714
Devang Patel1a0df9a2010-05-10 22:49:55 +00001715 // Emit size of content not including length itself
1716 unsigned ContentSize = Die->getSize() +
1717 sizeof(int16_t) + // DWARF version number
1718 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001719 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001720
Devang Patel1a0df9a2010-05-10 22:49:55 +00001721 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1722 Asm->EmitInt32(ContentSize);
1723 Asm->OutStreamer.AddComment("DWARF version number");
1724 Asm->EmitInt16(dwarf::DWARF_VERSION);
1725 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1726 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1727 DwarfAbbrevSectionSym);
1728 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1729 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001730
Devang Patel1a0df9a2010-05-10 22:49:55 +00001731 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001732 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1733 }
Bill Wendling480ff322009-05-20 23:21:38 +00001734}
1735
Devang Patel930143b2009-11-21 02:48:08 +00001736/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00001737///
Devang Patel930143b2009-11-21 02:48:08 +00001738void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00001739 // Check to see if it is worth the effort.
1740 if (!Abbreviations.empty()) {
1741 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001742 Asm->OutStreamer.SwitchSection(
1743 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001744
Chris Lattnera179b522010-04-04 19:25:43 +00001745 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00001746
1747 // For each abbrevation.
1748 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1749 // Get abbreviation data
1750 const DIEAbbrev *Abbrev = Abbreviations[i];
1751
1752 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001753 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001754
1755 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001756 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001757 }
1758
1759 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001760 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00001761
Chris Lattnera179b522010-04-04 19:25:43 +00001762 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00001763 }
1764}
1765
Devang Patel930143b2009-11-21 02:48:08 +00001766/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00001767/// the line matrix.
1768///
Devang Patel930143b2009-11-21 02:48:08 +00001769void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00001770 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00001771 Asm->OutStreamer.AddComment("Extended Op");
1772 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001773
Chris Lattner566cae92010-03-09 23:52:58 +00001774 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001775 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00001776 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1777 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1778
1779 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00001780
Chris Lattnera179b522010-04-04 19:25:43 +00001781 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00001782 Asm->getTargetData().getPointerSize(),
1783 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001784
1785 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00001786 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1787 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001788 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00001789 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00001790}
1791
Eric Christopher4996c702011-11-07 09:24:32 +00001792/// emitAccelNames - Emit visible names into a hashed accelerator table
1793/// section.
1794void DwarfDebug::emitAccelNames() {
1795 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1796 dwarf::DW_FORM_data4));
1797 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1798 E = CUMap.end(); I != E; ++I) {
1799 CompileUnit *TheCU = I->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00001800 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1801 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001802 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1803 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001804 const std::vector<DIE *> &Entities = GI->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00001805 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1806 DE = Entities.end(); DI != DE; ++DI)
1807 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00001808 }
1809 }
1810
1811 AT.FinalizeTable(Asm, "Names");
1812 Asm->OutStreamer.SwitchSection(
1813 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1814 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1815 Asm->OutStreamer.EmitLabel(SectionBegin);
1816
1817 // Emit the full data.
1818 AT.Emit(Asm, SectionBegin, this);
1819}
1820
1821/// emitAccelObjC - Emit objective C classes and categories into a hashed
1822/// accelerator table section.
1823void DwarfDebug::emitAccelObjC() {
1824 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1825 dwarf::DW_FORM_data4));
1826 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1827 E = CUMap.end(); I != E; ++I) {
1828 CompileUnit *TheCU = I->second;
1829 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1830 for (StringMap<std::vector<DIE*> >::const_iterator
1831 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1832 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001833 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher4996c702011-11-07 09:24:32 +00001834 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1835 DE = Entities.end(); DI != DE; ++DI)
1836 AT.AddName(Name, (*DI));
1837 }
1838 }
1839
1840 AT.FinalizeTable(Asm, "ObjC");
1841 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1842 .getDwarfAccelObjCSection());
1843 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1844 Asm->OutStreamer.EmitLabel(SectionBegin);
1845
1846 // Emit the full data.
1847 AT.Emit(Asm, SectionBegin, this);
1848}
1849
1850/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1851/// table.
1852void DwarfDebug::emitAccelNamespaces() {
1853 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1854 dwarf::DW_FORM_data4));
1855 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1856 E = CUMap.end(); I != E; ++I) {
1857 CompileUnit *TheCU = I->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00001858 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1859 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001860 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1861 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001862 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00001863 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1864 DE = Entities.end(); DI != DE; ++DI)
1865 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00001866 }
1867 }
1868
1869 AT.FinalizeTable(Asm, "namespac");
1870 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1871 .getDwarfAccelNamespaceSection());
1872 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1873 Asm->OutStreamer.EmitLabel(SectionBegin);
1874
1875 // Emit the full data.
1876 AT.Emit(Asm, SectionBegin, this);
1877}
1878
1879/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1880void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00001881 std::vector<DwarfAccelTable::Atom> Atoms;
1882 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1883 dwarf::DW_FORM_data4));
1884 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1885 dwarf::DW_FORM_data2));
1886 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1887 dwarf::DW_FORM_data1));
1888 DwarfAccelTable AT(Atoms);
Eric Christopher4996c702011-11-07 09:24:32 +00001889 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1890 E = CUMap.end(); I != E; ++I) {
1891 CompileUnit *TheCU = I->second;
Eric Christopher21bde872012-01-06 04:35:23 +00001892 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1893 = TheCU->getAccelTypes();
1894 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001895 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1896 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001897 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopher21bde872012-01-06 04:35:23 +00001898 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1899 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1900 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher4996c702011-11-07 09:24:32 +00001901 }
1902 }
1903
1904 AT.FinalizeTable(Asm, "types");
1905 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1906 .getDwarfAccelTypesSection());
1907 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1908 Asm->OutStreamer.EmitLabel(SectionBegin);
1909
1910 // Emit the full data.
1911 AT.Emit(Asm, SectionBegin, this);
1912}
1913
Devang Patel04d2f2d2009-11-24 01:14:22 +00001914void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001915 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1916 E = CUMap.end(); I != E; ++I) {
1917 CompileUnit *TheCU = I->second;
Eric Christopher6abc9c52011-11-07 09:18:35 +00001918 // Start the dwarf pubtypes section.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001919 Asm->OutStreamer.SwitchSection(
1920 Asm->getObjFileLowering().getDwarfPubTypesSection());
1921 Asm->OutStreamer.AddComment("Length of Public Types Info");
1922 Asm->EmitLabelDifference(
1923 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1924 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001925
Devang Patel1a0df9a2010-05-10 22:49:55 +00001926 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1927 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001928
Devang Patel1a0df9a2010-05-10 22:49:55 +00001929 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1930 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001931
Devang Patel1a0df9a2010-05-10 22:49:55 +00001932 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1933 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1934 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001935
Devang Patel1a0df9a2010-05-10 22:49:55 +00001936 Asm->OutStreamer.AddComment("Compilation Unit Length");
1937 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1938 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1939 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001940
Devang Patel1a0df9a2010-05-10 22:49:55 +00001941 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1942 for (StringMap<DIE*>::const_iterator
1943 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1944 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00001945 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001946
Devang Patel1a0df9a2010-05-10 22:49:55 +00001947 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1948 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001949
Devang Patel1a0df9a2010-05-10 22:49:55 +00001950 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00001951 // Emit the name with a terminating null byte.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001952 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1953 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001954
Devang Patel1a0df9a2010-05-10 22:49:55 +00001955 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001956 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001957 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1958 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001959 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001960}
1961
Devang Patel930143b2009-11-21 02:48:08 +00001962/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00001963///
Devang Patel930143b2009-11-21 02:48:08 +00001964void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00001965 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00001966 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001967
Chris Lattner3d72a672010-03-09 23:38:23 +00001968 // Start the dwarf str section.
1969 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001970 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001971
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001972 // Get all of the string pool entries and put them in an array by their ID so
1973 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001974 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001975 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001976
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001977 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1978 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1979 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001980
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001981 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001982
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001983 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00001984 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001985 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001986
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00001987 // Emit the string itself with a terminating null byte.
Benjamin Kramer148db362011-11-09 12:12:04 +00001988 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
1989 Entries[i].second->getKeyLength()+1),
1990 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001991 }
1992}
1993
Devang Patel930143b2009-11-21 02:48:08 +00001994/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00001995///
Devang Patel930143b2009-11-21 02:48:08 +00001996void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001997 if (DotDebugLocEntries.empty())
1998 return;
1999
Devang Patel116a9d72011-02-04 22:57:18 +00002000 for (SmallVector<DotDebugLocEntry, 4>::iterator
2001 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2002 I != E; ++I) {
2003 DotDebugLocEntry &Entry = *I;
2004 if (I + 1 != DotDebugLocEntries.end())
2005 Entry.Merge(I+1);
2006 }
2007
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002008 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002009 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00002010 Asm->getObjFileLowering().getDwarfLocSection());
2011 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002012 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2013 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002014 for (SmallVector<DotDebugLocEntry, 4>::iterator
2015 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002016 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002017 DotDebugLocEntry &Entry = *I;
2018 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002019 if (Entry.isEmpty()) {
2020 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2021 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002022 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002023 } else {
2024 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2025 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00002026 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002027 Asm->OutStreamer.AddComment("Loc expr size");
2028 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2029 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2030 Asm->EmitLabelDifference(end, begin, 2);
2031 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002032 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002033 DIBasicType BTy(DV.getType());
2034 if (BTy.Verify() &&
2035 (BTy.getEncoding() == dwarf::DW_ATE_signed
2036 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2037 Asm->OutStreamer.AddComment("DW_OP_consts");
2038 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002039 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002040 } else {
2041 Asm->OutStreamer.AddComment("DW_OP_constu");
2042 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002043 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002044 }
Devang Pateled9fd452011-07-08 16:49:43 +00002045 } else if (Entry.isLocation()) {
2046 if (!DV.hasComplexAddress())
2047 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00002048 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00002049 else {
2050 // Complex address entry.
2051 unsigned N = DV.getNumAddrElements();
2052 unsigned i = 0;
2053 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2054 if (Entry.Loc.getOffset()) {
2055 i = 2;
2056 Asm->EmitDwarfRegOp(Entry.Loc);
2057 Asm->OutStreamer.AddComment("DW_OP_deref");
2058 Asm->EmitInt8(dwarf::DW_OP_deref);
2059 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2060 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2061 Asm->EmitSLEB128(DV.getAddrElement(1));
2062 } else {
2063 // If first address element is OpPlus then emit
2064 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2065 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2066 Asm->EmitDwarfRegOp(Loc);
2067 i = 2;
2068 }
2069 } else {
2070 Asm->EmitDwarfRegOp(Entry.Loc);
2071 }
2072
2073 // Emit remaining complex address elements.
2074 for (; i < N; ++i) {
2075 uint64_t Element = DV.getAddrElement(i);
2076 if (Element == DIBuilder::OpPlus) {
2077 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2078 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002079 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher8d2a77d2012-05-08 21:24:39 +00002080 if (!Entry.Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002081 Asm->EmitInt8(dwarf::DW_OP_deref);
2082 } else
2083 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002084 }
Devang Patel3e021532011-04-28 02:22:40 +00002085 }
Devang Patel3e021532011-04-28 02:22:40 +00002086 }
Devang Pateled9fd452011-07-08 16:49:43 +00002087 // else ... ignore constant fp. There is not any good way to
2088 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002089 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002090 }
2091 }
Bill Wendling480ff322009-05-20 23:21:38 +00002092}
2093
2094/// EmitDebugARanges - Emit visible names into a debug aranges section.
2095///
2096void DwarfDebug::EmitDebugARanges() {
2097 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002098 Asm->OutStreamer.SwitchSection(
2099 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002100}
2101
Devang Patel930143b2009-11-21 02:48:08 +00002102/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002103///
Devang Patel930143b2009-11-21 02:48:08 +00002104void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002105 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002106 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002107 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00002108 unsigned char Size = Asm->getTargetData().getPointerSize();
2109 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002110 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002111 I != E; ++I) {
2112 if (*I)
2113 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00002114 else
Devang Patel6c74a872010-04-27 19:46:33 +00002115 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00002116 }
Bill Wendling480ff322009-05-20 23:21:38 +00002117}
2118
Devang Patel930143b2009-11-21 02:48:08 +00002119/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002120///
Devang Patel930143b2009-11-21 02:48:08 +00002121void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002122 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002123 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002124 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002125 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002126 }
2127}
2128
Devang Patel930143b2009-11-21 02:48:08 +00002129/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002130/// Section Header:
2131/// 1. length of section
2132/// 2. Dwarf version number
2133/// 3. address size.
2134///
2135/// Entries (one "entry" for each function that was inlined):
2136///
2137/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2138/// otherwise offset into __debug_str for regular function name.
2139/// 2. offset into __debug_str section for regular function name.
2140/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2141/// instances for the function.
2142///
2143/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2144/// inlined instance; the die_offset points to the inlined_subroutine die in the
2145/// __debug_info section, and the low_pc is the starting address for the
2146/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002147void DwarfDebug::emitDebugInlineInfo() {
Eric Christopher1df94bf2012-03-02 02:11:47 +00002148 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002149 return;
2150
Devang Patel1a0df9a2010-05-10 22:49:55 +00002151 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002152 return;
2153
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002154 Asm->OutStreamer.SwitchSection(
2155 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002156
Chris Lattner566cae92010-03-09 23:52:58 +00002157 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002158 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2159 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002160
Chris Lattnera179b522010-04-04 19:25:43 +00002161 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002162
Chris Lattner566cae92010-03-09 23:52:58 +00002163 Asm->OutStreamer.AddComment("Dwarf Version");
2164 Asm->EmitInt16(dwarf::DWARF_VERSION);
2165 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002166 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002167
Devang Patel32cc43c2010-05-07 20:54:48 +00002168 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002169 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002170
Devang Patel32cc43c2010-05-07 20:54:48 +00002171 const MDNode *Node = *I;
2172 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002173 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002174 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002175 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002176 StringRef LName = SP.getLinkageName();
2177 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002178
Chris Lattner566cae92010-03-09 23:52:58 +00002179 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher77725312012-03-02 01:57:52 +00002180 if (LName.empty())
2181 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2182 else
Chris Lattner70a4fce2010-04-04 23:25:33 +00002183 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2184 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002185
Chris Lattner566cae92010-03-09 23:52:58 +00002186 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00002187 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002188 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002189
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002190 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002191 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002192 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002193 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002194
Chris Lattner7bde8c02010-04-04 18:52:31 +00002195 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002196 Asm->OutStreamer.EmitSymbolValue(LI->first,
2197 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002198 }
2199 }
2200
Chris Lattnera179b522010-04-04 19:25:43 +00002201 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002202}