blob: e8c807ac5f1718733f1368a5f5315946f9e07280 [file] [log] [blame]
Bill Wendling0310d762009-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 Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher09ac3d82011-11-07 09:24:32 +000017#include "DwarfAccelTable.h"
Devang Patel8b9df622011-04-12 17:40:32 +000018#include "DwarfCompileUnit.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000019#include "llvm/Constants.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000020#include "llvm/DebugInfo.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000021#include "llvm/DIBuilder.h"
Bill Wendling0310d762009-05-15 09:23:25 +000022#include "llvm/Module.h"
Devang Patele449d1f2011-01-20 00:02:16 +000023#include "llvm/Instructions.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000026#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000027#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000028#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000029#include "llvm/MC/MCSymbol.h"
Micah Villmow3574eca2012-10-08 16:38:25 +000030#include "llvm/DataLayout.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000031#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000033#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000035#include "llvm/Target/TargetOptions.h"
Devang Patel9a31f0f2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000039#include "llvm/ADT/Triple.h"
Devang Pateleac9c072010-04-27 19:46:33 +000040#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000043#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000044#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000045#include "llvm/Support/Timer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000046#include "llvm/Support/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000047using namespace llvm;
48
Jim Grosbach1e20b962010-07-21 21:21:52 +000049static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000050 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000051 cl::desc("Disable debug info printing"));
52
Dan Gohman281d65d2010-05-07 01:08:53 +000053static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner7a2bdde2011-04-15 05:18:47 +000054 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman281d65d2010-05-07 01:08:53 +000055 cl::init(false));
56
Eric Christopher20f47ab2012-08-23 22:36:40 +000057namespace {
58 enum DefaultOnOff {
59 Default, Enable, Disable
60 };
61}
Eric Christopher09ac3d82011-11-07 09:24:32 +000062
Eric Christopher20f47ab2012-08-23 22:36:40 +000063static cl::opt<DefaultOnOff> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
64 cl::desc("Output prototype dwarf accelerator tables."),
65 cl::values(
66 clEnumVal(Default, "Default for platform"),
67 clEnumVal(Enable, "Enabled"),
68 clEnumVal(Disable, "Disabled"),
69 clEnumValEnd),
70 cl::init(Default));
71
72static cl::opt<DefaultOnOff> DarwinGDBCompat("darwin-gdb-compat", cl::Hidden,
Eric Christopher10cb7442012-08-23 07:10:46 +000073 cl::desc("Compatibility with Darwin gdb."),
Eric Christopher20f47ab2012-08-23 22:36:40 +000074 cl::values(
75 clEnumVal(Default, "Default for platform"),
76 clEnumVal(Enable, "Enabled"),
77 clEnumVal(Disable, "Disabled"),
78 clEnumValEnd),
79 cl::init(Default));
Eric Christopher10cb7442012-08-23 07:10:46 +000080
Bill Wendling5f017e82010-04-07 09:28:04 +000081namespace {
82 const char *DWARFGroupName = "DWARF Emission";
83 const char *DbgTimerName = "DWARF Debug Writer";
84} // end anonymous namespace
85
Bill Wendling0310d762009-05-15 09:23:25 +000086//===----------------------------------------------------------------------===//
87
88/// Configuration values for initial hash set sizes (log2).
89///
Bill Wendling0310d762009-05-15 09:23:25 +000090static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000091
92namespace llvm {
93
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000094DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000095 DIType Ty = Var.getType();
96 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
97 // addresses instead.
98 if (Var.isBlockByrefVariable()) {
99 /* Byref variables, in Blocks, are declared by the programmer as
100 "SomeType VarName;", but the compiler creates a
101 __Block_byref_x_VarName struct, and gives the variable VarName
102 either the struct, or a pointer to the struct, as its type. This
103 is necessary for various behind-the-scenes things the compiler
104 needs to do with by-reference variables in blocks.
105
106 However, as far as the original *programmer* is concerned, the
107 variable should still have type 'SomeType', as originally declared.
108
109 The following function dives into the __Block_byref_x_VarName
110 struct to find the original type of the variable. This will be
111 passed back to the code generating the type for the Debug
112 Information Entry for the variable 'VarName'. 'VarName' will then
113 have the original type 'SomeType' in its debug information.
114
115 The original type 'SomeType' will be the type of the field named
116 'VarName' inside the __Block_byref_x_VarName struct.
117
118 NOTE: In order for this to not completely fail on the debugger
119 side, the Debug Information Entry for the variable VarName needs to
120 have a DW_AT_location that tells the debugger how to unwind through
121 the pointers and __Block_byref_x_VarName struct to find the actual
122 value of the variable. The function addBlockByrefType does this. */
123 DIType subType = Ty;
124 unsigned tag = Ty.getTag();
125
126 if (tag == dwarf::DW_TAG_pointer_type) {
127 DIDerivedType DTy = DIDerivedType(Ty);
128 subType = DTy.getTypeDerivedFrom();
129 }
130
131 DICompositeType blockStruct = DICompositeType(subType);
132 DIArray Elements = blockStruct.getTypeArray();
133
134 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
135 DIDescriptor Element = Elements.getElement(i);
136 DIDerivedType DT = DIDerivedType(Element);
137 if (getName() == DT.getName())
138 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000139 }
Devang Patel8bd11de2010-08-09 21:01:39 +0000140 }
Devang Patel3cbee302011-04-12 22:53:02 +0000141 return Ty;
142}
Bill Wendling0310d762009-05-15 09:23:25 +0000143
Chris Lattnerea761862010-04-05 04:09:20 +0000144} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000145
Chris Lattner49cd6642010-04-05 05:11:15 +0000146DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000147 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000148 AbbreviationsSet(InitAbbreviationsSetSize),
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000149 SourceIdMap(DIEValueAllocator), StringPool(DIEValueAllocator),
Eric Christopher6635cad2012-08-01 18:19:01 +0000150 PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000151 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000152
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000153 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000154 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000155 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000156 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000157
Eric Christopher10cb7442012-08-23 07:10:46 +0000158 // Turn on accelerator tables and older gdb compatibility
159 // for Darwin.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000160 bool isDarwin = Triple(M->getTargetTriple()).isOSDarwin();
161 if (DarwinGDBCompat == Default) {
162 if (isDarwin)
163 isDarwinGDBCompat = true;
164 else
165 isDarwinGDBCompat = false;
166 } else
167 isDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
Eric Christopherc1610fa2012-08-23 22:36:36 +0000168
Eric Christopher20f47ab2012-08-23 22:36:40 +0000169 if (DwarfAccelTables == Default) {
170 if (isDarwin)
171 hasDwarfAccelTables = true;
172 else
173 hasDwarfAccelTables = false;
174 } else
175 hasDwarfAccelTables = DwarfAccelTables == Enable ? true : false;
176
Dan Gohman03c3dc72010-06-18 15:56:31 +0000177 {
178 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
179 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000180 }
Bill Wendling0310d762009-05-15 09:23:25 +0000181}
182DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000183}
184
Eric Christopherd8a87522011-11-07 09:18:38 +0000185/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
186/// temporary label to it if SymbolStem is specified.
187static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
188 const char *SymbolStem = 0) {
189 Asm->OutStreamer.SwitchSection(Section);
190 if (!SymbolStem) return 0;
191
192 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
193 Asm->OutStreamer.EmitLabel(TmpSym);
194 return TmpSym;
195}
196
Nick Lewycky390c40d2011-10-27 06:44:11 +0000197MCSymbol *DwarfDebug::getStringPool() {
198 return Asm->GetTempSymbol("section_str");
199}
200
Chris Lattnerbc733f52010-03-13 02:17:42 +0000201MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
202 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
203 if (Entry.first) return Entry.first;
204
205 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000206 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000207}
208
Devang Patel2c4ceb12009-11-21 02:48:08 +0000209/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000210///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000211void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000212 // Profile the node so that we can make it unique.
213 FoldingSetNodeID ID;
214 Abbrev.Profile(ID);
215
216 // Check the set for priors.
217 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
218
219 // If it's newly added.
220 if (InSet == &Abbrev) {
221 // Add to abbreviation list.
222 Abbreviations.push_back(&Abbrev);
223
224 // Assign the vector position + 1 as its number.
225 Abbrev.setNumber(Abbreviations.size());
226 } else {
227 // Assign existing abbreviation number.
228 Abbrev.setNumber(InSet->getNumber());
229 }
230}
231
Jim Grosbach1e20b962010-07-21 21:21:52 +0000232/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000233/// printer to not emit usual symbol prefix before the symbol name is used then
234/// return linkage name after skipping this special LLVM prefix.
235static StringRef getRealLinkageName(StringRef LinkageName) {
236 char One = '\1';
237 if (LinkageName.startswith(StringRef(&One, 1)))
238 return LinkageName.substr(1);
239 return LinkageName;
240}
241
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000242static bool isObjCClass(StringRef Name) {
243 return Name.startswith("+") || Name.startswith("-");
244}
245
246static bool hasObjCCategory(StringRef Name) {
247 if (!isObjCClass(Name)) return false;
248
249 size_t pos = Name.find(')');
250 if (pos != std::string::npos) {
251 if (Name[pos+1] != ' ') return false;
252 return true;
253 }
254 return false;
255}
256
257static void getObjCClassCategory(StringRef In, StringRef &Class,
258 StringRef &Category) {
259 if (!hasObjCCategory(In)) {
260 Class = In.slice(In.find('[') + 1, In.find(' '));
261 Category = "";
262 return;
263 }
264
265 Class = In.slice(In.find('[') + 1, In.find('('));
266 Category = In.slice(In.find('[') + 1, In.find(' '));
267 return;
268}
269
270static StringRef getObjCMethodName(StringRef In) {
271 return In.slice(In.find(' ') + 1, In.find(']'));
272}
273
274// Add the various names to the Dwarf accelerator table names.
275static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
276 DIE* Die) {
277 if (!SP.isDefinition()) return;
278
279 TheCU->addAccelName(SP.getName(), Die);
280
281 // If the linkage name is different than the name, go ahead and output
282 // that as well into the name table.
283 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
284 TheCU->addAccelName(SP.getLinkageName(), Die);
285
286 // If this is an Objective-C selector name add it to the ObjC accelerator
287 // too.
288 if (isObjCClass(SP.getName())) {
289 StringRef Class, Category;
290 getObjCClassCategory(SP.getName(), Class, Category);
291 TheCU->addAccelObjC(Class, Die);
292 if (Category != "")
293 TheCU->addAccelObjC(Category, Die);
294 // Also add the base method name to the name table.
295 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
296 }
297}
298
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000299/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000300/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
301/// If there are global variables in this scope then create and insert
302/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000303DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
304 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000305 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000306
Chris Lattnerd38fee82010-04-05 00:13:49 +0000307 assert(SPDie && "Unable to find subprogram DIE!");
308 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000309
Devang Patel8aa61472010-07-07 22:20:57 +0000310 // Pick up abstract subprogram DIE.
311 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
312 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000313 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
314 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000315 SPCU->addDie(SPDie);
Bill Wendlinga4c76932012-11-07 04:42:18 +0000316 } else {
317 DISubprogram SPDecl = SP.getFunctionDeclaration();
318 if (!SPDecl.isSubprogram()) {
319 // There is not any need to generate specification DIE for a function
320 // defined at compile unit level. If a function is defined inside another
321 // function then gdb prefers the definition at top level and but does not
322 // expect specification DIE in parent function. So avoid creating
323 // specification DIE for a function defined inside a function.
324 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
325 !SP.getContext().isFile() &&
326 !isSubprogramContext(SP.getContext())) {
327 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
328
329 // Add arguments.
330 DICompositeType SPTy = SP.getType();
331 DIArray Args = SPTy.getTypeArray();
332 unsigned SPTag = SPTy.getTag();
333 if (SPTag == dwarf::DW_TAG_subroutine_type)
334 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
335 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
336 DIType ATy = DIType(Args.getElement(i));
337 SPCU->addType(Arg, ATy);
338 if (ATy.isArtificial())
339 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
340 if (ATy.isObjectPointer())
341 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer,
342 dwarf::DW_FORM_ref4, Arg);
343 SPDie->addChild(Arg);
344 }
345 DIE *SPDeclDie = SPDie;
346 SPDie = new DIE(dwarf::DW_TAG_subprogram);
347 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
348 SPDeclDie);
349 SPCU->addDie(SPDie);
350 }
351 }
Devang Patel8aa61472010-07-07 22:20:57 +0000352 }
353
Devang Patel3cbee302011-04-12 22:53:02 +0000354 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
355 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
356 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
357 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000358 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
359 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000360 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000361
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000362 // Add name to the name table, we do this here because we're guaranteed
363 // to have concrete versions of our DW_TAG_subprogram nodes.
364 addSubprogramNames(SPCU, SP, SPDie);
365
Chris Lattnerd38fee82010-04-05 00:13:49 +0000366 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000367}
368
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000369/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000370/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000371DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
372 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000373 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
374 if (Scope->isAbstractScope())
375 return ScopeDIE;
376
Devang Patelbf47fdb2011-08-10 20:55:27 +0000377 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000378 if (Ranges.empty())
379 return 0;
380
Devang Patelbf47fdb2011-08-10 20:55:27 +0000381 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000382 if (Ranges.size() > 1) {
383 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000384 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000385 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000386 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000387 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000388 * Asm->getDataLayout().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000389 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000390 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000391 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
392 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000393 }
394 DebugRangeSymbols.push_back(NULL);
395 DebugRangeSymbols.push_back(NULL);
396 return ScopeDIE;
397 }
398
Devang Patelc3f5f782010-05-25 23:40:22 +0000399 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
400 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000401
Devang Patelc3f5f782010-05-25 23:40:22 +0000402 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000403
Chris Lattnerb7db7332010-03-09 01:58:53 +0000404 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
405 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000406
Devang Patel3cbee302011-04-12 22:53:02 +0000407 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
408 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000409
410 return ScopeDIE;
411}
412
Devang Patel2c4ceb12009-11-21 02:48:08 +0000413/// constructInlinedScopeDIE - This scope represents inlined body of
414/// a function. Construct DIE to represent this concrete inlined copy
415/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000416DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
417 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000418 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000419 assert(Ranges.empty() == false &&
420 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000421
Devang Patel26a92002011-07-27 00:34:13 +0000422 if (!Scope->getScopeNode())
423 return NULL;
424 DIScope DS(Scope->getScopeNode());
425 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000426 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
427 if (!OriginDIE) {
Bill Wendlinge0aae5b2012-10-30 17:51:02 +0000428 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patel26a92002011-07-27 00:34:13 +0000429 return NULL;
430 }
431
Devang Patelbf47fdb2011-08-10 20:55:27 +0000432 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000433 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
434 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000435
Devang Patel0afbf232010-07-08 22:39:20 +0000436 if (StartLabel == 0 || EndLabel == 0) {
Bill Wendlinge0aae5b2012-10-30 17:51:02 +0000437 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000438 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000439 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000440 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000441 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000442 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000443
Devang Pateld96efb82011-05-05 17:54:26 +0000444 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000445 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
446 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000447
Devang Patel26a92002011-07-27 00:34:13 +0000448 if (Ranges.size() > 1) {
449 // .debug_range section has not been laid out yet. Emit offset in
450 // .debug_range as a uint, size 4, for now. emitDIE will handle
451 // DW_AT_ranges appropriately.
452 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000453 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000454 * Asm->getDataLayout().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000455 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000456 RE = Ranges.end(); RI != RE; ++RI) {
457 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
458 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
459 }
460 DebugRangeSymbols.push_back(NULL);
461 DebugRangeSymbols.push_back(NULL);
462 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000463 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
464 StartLabel);
465 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
466 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000467 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000468
469 InlinedSubprogramDIEs.insert(OriginDIE);
470
471 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000472 //.debug_inlined section specification does not clearly state how
473 // to emit inlined scope that is split into multiple instruction ranges.
474 // For now, use first instruction range and emit low_pc/high_pc pair and
475 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000476 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000477 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000478
479 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000480 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000481 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000482 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000483 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000484
Devang Patel53bb5c92009-11-10 23:06:00 +0000485 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000486 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
487 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000488 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000489
Eric Christopher309bedd2011-12-04 06:02:38 +0000490 // Add name to the name table, we do this here because we're guaranteed
491 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
492 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
493
Devang Patel53bb5c92009-11-10 23:06:00 +0000494 return ScopeDIE;
495}
496
Devang Patel2c4ceb12009-11-21 02:48:08 +0000497/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000498DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000499 if (!Scope || !Scope->getScopeNode())
500 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000501
Nick Lewycky746cb672011-10-26 22:55:33 +0000502 SmallVector<DIE *, 8> Children;
Eric Christophere5212782012-09-12 23:36:19 +0000503 DIE *ObjectPointer = NULL;
Devang Patel0478c152011-03-01 22:58:55 +0000504
505 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000506 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000507 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
508 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000509 if (DIE *Arg =
Eric Christophere5212782012-09-12 23:36:19 +0000510 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel0478c152011-03-01 22:58:55 +0000511 Children.push_back(Arg);
Eric Christophere5212782012-09-12 23:36:19 +0000512 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
513 }
Devang Patel0478c152011-03-01 22:58:55 +0000514
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000515 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000516 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000517 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000518 if (DIE *Variable =
Eric Christopher7b451cf2012-09-21 22:18:52 +0000519 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) {
Devang Patel5bc9fec2011-02-19 01:31:27 +0000520 Children.push_back(Variable);
Eric Christopher7b451cf2012-09-21 22:18:52 +0000521 if (Variables[i]->isObjectPointer()) ObjectPointer = Variable;
522 }
Devang Patelbf47fdb2011-08-10 20:55:27 +0000523 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000524 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000525 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000526 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000527 DIScope DS(Scope->getScopeNode());
528 DIE *ScopeDIE = NULL;
529 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000530 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000531 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000532 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000533 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000534 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000535 // Note down abstract DIE.
536 if (ScopeDIE)
537 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
538 }
Devang Patel3c91b052010-03-08 20:52:55 +0000539 else
Devang Pateld3024342011-08-15 22:24:32 +0000540 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000541 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000542 else {
543 // There is no need to emit empty lexical block DIE.
544 if (Children.empty())
545 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000546 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000547 }
548
Devang Patelaead63c2010-03-29 22:59:58 +0000549 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000550
Devang Patel5bc9fec2011-02-19 01:31:27 +0000551 // Add children
552 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
553 E = Children.end(); I != E; ++I)
554 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000555
Eric Christophere5212782012-09-12 23:36:19 +0000556 if (DS.isSubprogram() && ObjectPointer != NULL)
557 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
558 dwarf::DW_FORM_ref4, ObjectPointer);
559
Jim Grosbach1e20b962010-07-21 21:21:52 +0000560 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000561 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000562
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000563 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000564}
565
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000566/// GetOrCreateSourceID - Look up the source id with the given directory and
567/// source file names. If none currently exists, create a new id and insert it
568/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
569/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000570unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
571 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000572 // If FE did not provide a file name, then assume stdin.
573 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000574 return GetOrCreateSourceID("<stdin>", StringRef());
575
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000576 // TODO: this might not belong here. See if we can factor this better.
577 if (DirName == CompilationDir)
578 DirName = "";
579
Nick Lewycky44d798d2011-10-17 23:05:28 +0000580 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000581
Benjamin Kramer74612c22012-03-11 14:56:26 +0000582 // We look up the file/dir pair by concatenating them with a zero byte.
583 SmallString<128> NamePair;
584 NamePair += DirName;
585 NamePair += '\0'; // Zero bytes are not allowed in paths.
586 NamePair += FileName;
587
588 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
589 if (Ent.getValue() != SrcId)
590 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000591
Rafael Espindola5c055632010-11-18 02:04:25 +0000592 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000593 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000594
595 return SrcId;
596}
597
Jim Grosbach1e20b962010-07-21 21:21:52 +0000598/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000599/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000600CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000601 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000602 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000603 CompilationDir = DIUnit.getDirectory();
604 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000605
606 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopheredf8bc82012-09-10 23:34:00 +0000607 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die,
608 Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000609 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000610 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
611 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000612 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000613 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
614 // into an entity.
615 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000616 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000617 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000618 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000619 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000620 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000621 else
Devang Patel3cbee302011-04-12 22:53:02 +0000622 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000623
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000624 if (!CompilationDir.empty())
625 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000626 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000627 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000628
Devang Patel65dbc902009-11-25 17:36:49 +0000629 StringRef Flags = DIUnit.getFlags();
630 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000631 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000632
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000633 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000634 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000635 dwarf::DW_FORM_data1, RVer);
636
Devang Patel163a9f72010-05-10 22:49:55 +0000637 if (!FirstCU)
638 FirstCU = NewCU;
639 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000640 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000641}
642
Devang Patel163a9f72010-05-10 22:49:55 +0000643/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000644void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
645 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000646 CompileUnit *&CURef = SPMap[N];
647 if (CURef)
648 return;
649 CURef = TheCU;
650
Devang Patele4b27562009-08-28 23:24:31 +0000651 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000652 if (!SP.isDefinition())
653 // This is a method declaration which will be handled while constructing
654 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000655 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000656
Devang Pateldbc64af2011-08-15 17:24:54 +0000657 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000658
659 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000660 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000661
662 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000663 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000664
Devang Patel13e16b62009-06-26 01:49:18 +0000665 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000666}
667
Devang Patel02e603f2011-08-15 23:47:24 +0000668/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
669/// as llvm.dbg.enum and llvm.dbg.ty
670void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000671 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
672 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
673 const MDNode *N = NMD->getOperand(i);
674 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
675 constructSubprogramDIE(CU, N);
676 }
677
678 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
679 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
680 const MDNode *N = NMD->getOperand(i);
681 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000682 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000683 }
684
Devang Patel02e603f2011-08-15 23:47:24 +0000685 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
686 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
687 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000688 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
689 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000690 }
691
692 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
693 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
694 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000695 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
696 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000697 }
698}
699
700/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
701/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
702bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
703 DebugInfoFinder DbgFinder;
704 DbgFinder.processModule(*M);
705
706 bool HasDebugInfo = false;
707 // Scan all the compile-units to see if there are any marked as the main
708 // unit. If not, we do not generate debug info.
709 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
710 E = DbgFinder.compile_unit_end(); I != E; ++I) {
711 if (DICompileUnit(*I).isMain()) {
712 HasDebugInfo = true;
713 break;
714 }
715 }
716 if (!HasDebugInfo) return false;
717
718 // Create all the compile unit DIEs.
719 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
720 E = DbgFinder.compile_unit_end(); I != E; ++I)
721 constructCompileUnit(*I);
722
723 // Create DIEs for each global variable.
724 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
725 E = DbgFinder.global_variable_end(); I != E; ++I) {
726 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000727 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000728 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000729 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000730
Devang Patel02e603f2011-08-15 23:47:24 +0000731 // Create DIEs for each subprogram.
732 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
733 E = DbgFinder.subprogram_end(); I != E; ++I) {
734 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000735 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
736 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000737 }
738
739 return HasDebugInfo;
740}
741
Devang Patel2c4ceb12009-11-21 02:48:08 +0000742/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000743/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000744/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000745void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000746 if (DisableDebugInfoPrinting)
747 return;
748
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000749 // If module has named metadata anchors then use them, otherwise scan the
750 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000751 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
752 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000753 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
754 DICompileUnit CUNode(CU_Nodes->getOperand(i));
755 CompileUnit *CU = constructCompileUnit(CUNode);
756 DIArray GVs = CUNode.getGlobalVariables();
757 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000758 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000759 DIArray SPs = CUNode.getSubprograms();
760 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
761 constructSubprogramDIE(CU, SPs.getElement(i));
762 DIArray EnumTypes = CUNode.getEnumTypes();
763 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
764 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
765 DIArray RetainedTypes = CUNode.getRetainedTypes();
766 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
767 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
768 }
Devang Patel02e603f2011-08-15 23:47:24 +0000769 } else if (!collectLegacyDebugInfo(M))
770 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000771
Devang Patel02e603f2011-08-15 23:47:24 +0000772 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000773
Chris Lattnerd850ac72010-04-05 02:19:28 +0000774 // Tell MMI that we have debug info.
775 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000776
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000777 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000778 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000779
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000780 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000781 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000782}
783
Devang Patel2c4ceb12009-11-21 02:48:08 +0000784/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000785///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000786void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000787 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000788 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000789 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000790
Devang Patel94c7ddb2011-08-16 22:09:43 +0000791 // Collect info for variables that were optimized out.
792 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
793 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
794 DICompileUnit TheCU(CU_Nodes->getOperand(i));
795 DIArray Subprograms = TheCU.getSubprograms();
796 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
797 DISubprogram SP(Subprograms.getElement(i));
798 if (ProcessedSPNodes.count(SP) != 0) continue;
799 if (!SP.Verify()) continue;
800 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000801 DIArray Variables = SP.getVariables();
802 if (Variables.getNumElements() == 0) continue;
803
Devang Patel94c7ddb2011-08-16 22:09:43 +0000804 LexicalScope *Scope =
805 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
806 DeadFnScopeMap[SP] = Scope;
Bill Wendlinga4c76932012-11-07 04:42:18 +0000807
Devang Patel94c7ddb2011-08-16 22:09:43 +0000808 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000809 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000810 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000811 constructSubprogramDIE(SPCU, SP);
812 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000813 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
814 DIVariable DV(Variables.getElement(vi));
815 if (!DV.Verify()) continue;
816 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000817 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000818 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000819 ScopeDIE->addChild(VariableDIE);
820 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000821 }
822 }
823 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000824
Devang Patel53bb5c92009-11-10 23:06:00 +0000825 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
826 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
827 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
828 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000829 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000830 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000831 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
832 AE = AbstractSPDies.end(); AI != AE; ++AI) {
833 DIE *ISP = AI->second;
834 if (InlinedSubprogramDIEs.count(ISP))
835 continue;
836 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
837 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000838
Eric Christopher6635cad2012-08-01 18:19:01 +0000839 // Emit DW_AT_containing_type attribute to connect types with their
840 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000841 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
842 CUE = CUMap.end(); CUI != CUE; ++CUI) {
843 CompileUnit *TheCU = CUI->second;
844 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000845 }
846
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000847 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000848 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000849 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000850 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000851 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000852
853 // End text sections.
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000854 for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) {
855 Asm->OutStreamer.SwitchSection(SectionMap[I]);
856 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000857 }
858
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000859 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000860 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000861
862 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000863 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000864
865 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000866 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000867
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000868 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000869 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000870 emitAccelNames();
871 emitAccelObjC();
872 emitAccelNamespaces();
873 emitAccelTypes();
874 }
875
Devang Patel193f7202009-11-24 01:14:22 +0000876 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000877 // TODO: When we don't need the option anymore we can
878 // remove all of the code that adds to the table.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000879 if (useDarwinGDBCompat())
Eric Christopher360f0062012-08-23 07:10:56 +0000880 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000881
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000882 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000883 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000884
885 // Emit info into a debug aranges section.
886 EmitDebugARanges();
887
888 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000889 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000890
891 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000892 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000893
894 // Emit inline info.
Eric Christopher9eb1a942012-08-23 07:32:02 +0000895 // TODO: When we don't need the option anymore we
896 // can remove all of the code that this section
897 // depends upon.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000898 if (useDarwinGDBCompat())
Eric Christopher9eb1a942012-08-23 07:32:02 +0000899 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000900
Eric Christopher7550f7d2012-03-02 00:30:24 +0000901 // Emit info into a debug str section.
902 emitDebugStr();
903
Devang Patele9a1cca2010-08-02 17:32:15 +0000904 // clean up.
905 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000906 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000907 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
908 E = CUMap.end(); I != E; ++I)
909 delete I->second;
910 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000911}
912
Devang Patel53bb5c92009-11-10 23:06:00 +0000913/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000914DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000915 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000916 LLVMContext &Ctx = DV->getContext();
917 // More then one inlined variable corresponds to one abstract variable.
918 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000919 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000920 if (AbsDbgVariable)
921 return AbsDbgVariable;
922
Devang Patelbf47fdb2011-08-10 20:55:27 +0000923 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000924 if (!Scope)
925 return NULL;
926
Devang Patel5a1a67c2011-08-15 19:01:20 +0000927 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000928 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000929 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000930 return AbsDbgVariable;
931}
932
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000933/// addCurrentFnArgument - If Var is a current function argument then add
934/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000935bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000936 DbgVariable *Var, LexicalScope *Scope) {
937 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000938 return false;
939 DIVariable DV = Var->getVariable();
940 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
941 return false;
942 unsigned ArgNo = DV.getArgNumber();
943 if (ArgNo == 0)
944 return false;
945
Devang Patelcb3a6572011-03-03 20:02:02 +0000946 size_t Size = CurrentFnArguments.size();
947 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000948 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000949 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000950 // arguments does the function have at source level.
951 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000952 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000953 CurrentFnArguments[ArgNo - 1] = Var;
954 return true;
955}
956
Devang Patelee432862010-05-20 19:57:06 +0000957/// collectVariableInfoFromMMITable - Collect variable information from
958/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000959void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000960DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000961 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000962 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
963 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
964 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000965 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000966 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000967 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000968 DIVariable DV(Var);
969 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000970
Devang Patelbf47fdb2011-08-10 20:55:27 +0000971 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000972
Devang Patelfb0ee432009-11-10 23:20:04 +0000973 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000974 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000975 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000976
Devang Patel26c1e562010-05-20 16:36:41 +0000977 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000978 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000979 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000980 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000981 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000982 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000983 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000984 }
Devang Patelee432862010-05-20 19:57:06 +0000985}
Devang Patel90a48ad2010-03-15 18:33:46 +0000986
Jim Grosbach1e20b962010-07-21 21:21:52 +0000987/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000988/// DBG_VALUE instruction, is in a defined reg.
989static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000990 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000991 return MI->getNumOperands() == 3 &&
992 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
993 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000994}
995
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000996/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000997/// at MI.
998static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
999 const MCSymbol *FLabel,
1000 const MCSymbol *SLabel,
1001 const MachineInstr *MI) {
1002 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1003
1004 if (MI->getNumOperands() != 3) {
1005 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1006 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1007 }
1008 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1009 MachineLocation MLoc;
1010 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1011 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1012 }
1013 if (MI->getOperand(0).isImm())
1014 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1015 if (MI->getOperand(0).isFPImm())
1016 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1017 if (MI->getOperand(0).isCImm())
1018 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1019
Craig Topper5e25ee82012-02-05 08:31:47 +00001020 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001021}
1022
Devang Patelbf47fdb2011-08-10 20:55:27 +00001023/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001024void
Devang Patel78e127d2010-06-25 22:07:34 +00001025DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1026 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001027
Devang Patelee432862010-05-20 19:57:06 +00001028 /// collection info from MMI table.
1029 collectVariableInfoFromMMITable(MF, Processed);
1030
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001031 for (SmallVectorImpl<const MDNode*>::const_iterator
1032 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1033 ++UVI) {
1034 const MDNode *Var = *UVI;
1035 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001036 continue;
1037
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001038 // History contains relevant DBG_VALUE instructions for Var and instructions
1039 // clobbering it.
1040 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1041 if (History.empty())
1042 continue;
1043 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001044
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001045 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001046 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001047 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1048 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001049 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001050 else {
1051 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001052 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001053 else {
1054 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001055 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001056 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001057 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001058 }
1059 }
Devang Patelee432862010-05-20 19:57:06 +00001060 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001061 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001062 continue;
1063
1064 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001065 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001066 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1067 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001068 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001069 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001070 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001071 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001072
Eric Christopherc56e3f02012-10-08 20:48:54 +00001073 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001074 if (History.size() <= 1 || (History.size() == 2 &&
1075 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001076 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001077 continue;
1078 }
1079
1080 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001081 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001082
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001083 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1084 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1085 const MachineInstr *Begin = *HI;
1086 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001087
Devang Patel4ada1d72011-06-01 23:00:17 +00001088 // Check if DBG_VALUE is truncating a range.
1089 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1090 && !Begin->getOperand(0).getReg())
1091 continue;
1092
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001093 // Compute the range for a register location.
1094 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1095 const MCSymbol *SLabel = 0;
1096
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001097 if (HI + 1 == HE)
1098 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001099 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001100 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001101 else {
1102 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001103 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1104 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001105 if (End->isDebugValue())
1106 SLabel = getLabelBeforeInsn(End);
1107 else {
1108 // End is a normal instruction clobbering the range.
1109 SLabel = getLabelAfterInsn(End);
1110 assert(SLabel && "Forgot label after clobber instruction");
1111 ++HI;
1112 }
1113 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001114
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001115 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001116 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1117 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001118 }
1119 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001120 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001121
1122 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001123 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1124 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1125 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1126 DIVariable DV(Variables.getElement(i));
1127 if (!DV || !DV.Verify() || !Processed.insert(DV))
1128 continue;
1129 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1130 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001131 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001132}
Devang Patel98e1cac2010-05-14 21:01:35 +00001133
Devang Patelc3f5f782010-05-25 23:40:22 +00001134/// getLabelBeforeInsn - Return Label preceding the instruction.
1135const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001136 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1137 assert(Label && "Didn't insert label before instruction");
1138 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001139}
1140
1141/// getLabelAfterInsn - Return Label immediately following the instruction.
1142const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001143 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001144}
1145
Devang Patelcbbe2872010-10-26 17:49:02 +00001146/// beginInstruction - Process beginning of an instruction.
1147void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001148 // Check if source location changes, but ignore DBG_VALUE locations.
1149 if (!MI->isDebugValue()) {
1150 DebugLoc DL = MI->getDebugLoc();
1151 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001152 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001153 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001154 if (DL == PrologEndLoc) {
1155 Flags |= DWARF2_FLAG_PROLOGUE_END;
1156 PrologEndLoc = DebugLoc();
1157 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001158 if (PrologEndLoc.isUnknown())
1159 Flags |= DWARF2_FLAG_IS_STMT;
1160
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001161 if (!DL.isUnknown()) {
1162 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001163 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001164 } else
Devang Patel4243e672011-05-11 19:22:19 +00001165 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001166 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001167 }
Devang Patelaead63c2010-03-29 22:59:58 +00001168
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001169 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001170 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1171 LabelsBeforeInsn.find(MI);
1172
1173 // No label needed.
1174 if (I == LabelsBeforeInsn.end())
1175 return;
1176
1177 // Label already assigned.
1178 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001179 return;
Devang Patel553881b2010-03-29 17:20:31 +00001180
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001181 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001182 PrevLabel = MMI->getContext().CreateTempSymbol();
1183 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001184 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001185 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001186}
1187
Devang Patelcbbe2872010-10-26 17:49:02 +00001188/// endInstruction - Process end of an instruction.
1189void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001190 // Don't create a new label after DBG_VALUE instructions.
1191 // They don't generate code.
1192 if (!MI->isDebugValue())
1193 PrevLabel = 0;
1194
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001195 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1196 LabelsAfterInsn.find(MI);
1197
1198 // No label needed.
1199 if (I == LabelsAfterInsn.end())
1200 return;
1201
1202 // Label already assigned.
1203 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001204 return;
1205
1206 // We need a label after this instruction.
1207 if (!PrevLabel) {
1208 PrevLabel = MMI->getContext().CreateTempSymbol();
1209 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001210 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001211 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001212}
1213
Jim Grosbach1e20b962010-07-21 21:21:52 +00001214/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001215/// Each LexicalScope has first instruction and last instruction to mark
1216/// beginning and end of a scope respectively. Create an inverse map that list
1217/// scopes starts (and ends) with an instruction. One instruction may start (or
1218/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001219void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001220 SmallVector<LexicalScope *, 4> WorkList;
1221 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001222 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001223 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001224
Devang Patelbf47fdb2011-08-10 20:55:27 +00001225 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001226 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001227 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001228 SE = Children.end(); SI != SE; ++SI)
1229 WorkList.push_back(*SI);
1230
Devang Patel53bb5c92009-11-10 23:06:00 +00001231 if (S->isAbstractScope())
1232 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001233
Devang Patelbf47fdb2011-08-10 20:55:27 +00001234 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001235 if (Ranges.empty())
1236 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001237 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001238 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001239 assert(RI->first && "InsnRange does not have first instruction!");
1240 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001241 requestLabelBeforeInsn(RI->first);
1242 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001243 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001244 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001245}
1246
Devang Patela3f48672011-05-09 22:14:49 +00001247/// getScopeNode - Get MDNode for DebugLoc's scope.
1248static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1249 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1250 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1251 return DL.getScope(Ctx);
1252}
1253
Devang Patel4243e672011-05-11 19:22:19 +00001254/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher6126a1e2012-04-03 00:43:49 +00001255/// line number info for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001256static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1257 const MDNode *Scope = getScopeNode(DL, Ctx);
1258 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001259 if (SP.Verify()) {
1260 // Check for number of operands since the compatibility is
1261 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001262 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001263 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1264 else
1265 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1266 }
1267
Devang Patel4243e672011-05-11 19:22:19 +00001268 return DebugLoc();
1269}
1270
Devang Patel2c4ceb12009-11-21 02:48:08 +00001271/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001272/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001273void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001274 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001275 LScopes.initialize(*MF);
1276 if (LScopes.empty()) return;
1277 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001278
Devang Pateleac9c072010-04-27 19:46:33 +00001279 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1280 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001281 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001282 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001283
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001284 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1285
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001286 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001287 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1288 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1289
Devang Patelb2b31a62010-05-26 19:37:24 +00001290 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001291 I != E; ++I) {
1292 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001293 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1294 II != IE; ++II) {
1295 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001296
Devang Patelb2b31a62010-05-26 19:37:24 +00001297 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001298 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001299
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001300 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001301 const MDNode *Var =
1302 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001303
1304 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001305 if (isDbgValueInDefinedReg(MI))
1306 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1307
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001308 // Check the history of this variable.
1309 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1310 if (History.empty()) {
1311 UserVariables.push_back(Var);
1312 // The first mention of a function argument gets the FunctionBeginSym
1313 // label, so arguments are visible when breaking at function entry.
1314 DIVariable DV(Var);
1315 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1316 DISubprogram(getDISubprogram(DV.getContext()))
1317 .describes(MF->getFunction()))
1318 LabelsBeforeInsn[MI] = FunctionBeginSym;
1319 } else {
1320 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1321 const MachineInstr *Prev = History.back();
1322 if (Prev->isDebugValue()) {
1323 // Coalesce identical entries at the end of History.
1324 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001325 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher02c1a642012-10-08 20:48:49 +00001326 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Devang Patel79862892011-07-07 00:14:27 +00001327 << "\t" << *Prev
1328 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001329 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001330 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001331
1332 // Terminate old register assignments that don't reach MI;
1333 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1334 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1335 isDbgValueInDefinedReg(Prev)) {
1336 // Previous register assignment needs to terminate at the end of
1337 // its basic block.
1338 MachineBasicBlock::const_iterator LastMI =
1339 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001340 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001341 // Drop DBG_VALUE for empty range.
Eric Christopher02c1a642012-10-08 20:48:49 +00001342 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patel79862892011-07-07 00:14:27 +00001343 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001344 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001345 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001346 else {
1347 // Terminate after LastMI.
1348 History.push_back(LastMI);
1349 }
1350 }
1351 }
1352 }
1353 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001354 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001355 // Not a DBG_VALUE instruction.
1356 if (!MI->isLabel())
1357 AtBlockEntry = false;
1358
Eric Christopher0313ced2012-10-04 20:46:14 +00001359 // First known non-DBG_VALUE and non-frame setup location marks
1360 // the beginning of the function body.
1361 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1362 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel4243e672011-05-11 19:22:19 +00001363 PrologEndLoc = MI->getDebugLoc();
1364
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001365 // Check if the instruction clobbers any registers with debug vars.
1366 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1367 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1368 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1369 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001370 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1371 AI.isValid(); ++AI) {
1372 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001373 const MDNode *Var = LiveUserVar[Reg];
1374 if (!Var)
1375 continue;
1376 // Reg is now clobbered.
1377 LiveUserVar[Reg] = 0;
1378
1379 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001380 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1381 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001382 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001383 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1384 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001385 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001386 const MachineInstr *Prev = History.back();
1387 // Sanity-check: Register assignments are terminated at the end of
1388 // their block.
1389 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1390 continue;
1391 // Is the variable still in Reg?
1392 if (!isDbgValueInDefinedReg(Prev) ||
1393 Prev->getOperand(0).getReg() != Reg)
1394 continue;
1395 // Var is clobbered. Make sure the next instruction gets a label.
1396 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001397 }
1398 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001399 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001400 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001401 }
1402
1403 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1404 I != E; ++I) {
1405 SmallVectorImpl<const MachineInstr*> &History = I->second;
1406 if (History.empty())
1407 continue;
1408
1409 // Make sure the final register assignments are terminated.
1410 const MachineInstr *Prev = History.back();
1411 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1412 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001413 MachineBasicBlock::const_iterator LastMI =
1414 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001415 if (LastMI == PrevMBB->end())
1416 // Drop DBG_VALUE for empty range.
1417 History.pop_back();
1418 else {
1419 // Terminate after LastMI.
1420 History.push_back(LastMI);
1421 }
1422 }
1423 // Request labels for the full history.
1424 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1425 const MachineInstr *MI = History[i];
1426 if (MI->isDebugValue())
1427 requestLabelBeforeInsn(MI);
1428 else
1429 requestLabelAfterInsn(MI);
1430 }
1431 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001432
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001433 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001434 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001435
1436 // Record beginning of function.
1437 if (!PrologEndLoc.isUnknown()) {
1438 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1439 MF->getFunction()->getContext());
1440 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1441 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopher09e47502012-09-10 23:34:06 +00001442 0);
Devang Patel4243e672011-05-11 19:22:19 +00001443 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001444}
1445
Devang Patelbf47fdb2011-08-10 20:55:27 +00001446void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1447// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1448 ScopeVariables[LS].push_back(Var);
1449// Vars.push_back(Var);
1450}
1451
Devang Patel2c4ceb12009-11-21 02:48:08 +00001452/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001453///
Chris Lattnereec791a2010-01-26 23:18:02 +00001454void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001455 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001456
Devang Patelbf47fdb2011-08-10 20:55:27 +00001457 // Define end label for subprogram.
1458 FunctionEndSym = Asm->GetTempSymbol("func_end",
1459 Asm->getFunctionNumber());
1460 // Assumes in correct section after the entry point.
1461 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1462
1463 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1464 collectVariableInfo(MF, ProcessedVars);
1465
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001466 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001467 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001468 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001469
Devang Patelbf47fdb2011-08-10 20:55:27 +00001470 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001471 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1472 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1473 LexicalScope *AScope = AList[i];
1474 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001475 if (SP.Verify()) {
1476 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001477 DIArray Variables = SP.getVariables();
1478 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1479 DIVariable DV(Variables.getElement(i));
1480 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1481 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001482 // Check that DbgVariable for DV wasn't created earlier, when
1483 // findAbstractVariable() was called for inlined instance of DV.
1484 LLVMContext &Ctx = DV->getContext();
1485 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1486 if (AbstractVariables.lookup(CleanDV))
1487 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001488 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1489 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001490 }
1491 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001492 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001493 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001494 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001495
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001496 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001497
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001498 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001499 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001500
Devang Patelbf47fdb2011-08-10 20:55:27 +00001501 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1502 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001503
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001504 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001505 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1506 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1507 DeleteContainerPointers(I->second);
1508 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001509 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001510 UserVariables.clear();
1511 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001512 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001513 LabelsBeforeInsn.clear();
1514 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001515 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001516}
1517
Chris Lattnerc6087842010-03-09 04:54:43 +00001518/// recordSourceLine - Register a source line with debug info. Returns the
1519/// unique label that was emitted and which provides correspondence to
1520/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001521void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1522 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001523 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001524 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001525 unsigned Src = 1;
1526 if (S) {
1527 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001528
Dan Gohman1cc0d622010-05-05 23:41:32 +00001529 if (Scope.isCompileUnit()) {
1530 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001531 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001532 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001533 } else if (Scope.isFile()) {
1534 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001535 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001536 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001537 } else if (Scope.isSubprogram()) {
1538 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001539 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001540 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001541 } else if (Scope.isLexicalBlockFile()) {
1542 DILexicalBlockFile DBF(S);
1543 Fn = DBF.getFilename();
1544 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001545 } else if (Scope.isLexicalBlock()) {
1546 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001547 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001548 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001549 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001550 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001551
Devang Patel23670e52011-03-24 20:30:50 +00001552 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001553 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001554 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001555}
1556
Bill Wendling829e67b2009-05-20 23:22:40 +00001557//===----------------------------------------------------------------------===//
1558// Emit Methods
1559//===----------------------------------------------------------------------===//
1560
Devang Patel2c4ceb12009-11-21 02:48:08 +00001561/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001562///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001563unsigned
1564DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001565 // Get the children.
1566 const std::vector<DIE *> &Children = Die->getChildren();
1567
Bill Wendling94d04b82009-05-20 23:21:38 +00001568 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001569 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001570
1571 // Get the abbreviation for this DIE.
1572 unsigned AbbrevNumber = Die->getAbbrevNumber();
1573 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1574
1575 // Set DIE offset
1576 Die->setOffset(Offset);
1577
1578 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001579 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001580
1581 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1582 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1583
1584 // Size the DIE attribute values.
1585 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1586 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001587 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001588
1589 // Size the DIE children if any.
1590 if (!Children.empty()) {
1591 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1592 "Children flag not set");
1593
1594 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001595 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001596
1597 // End of children marker.
1598 Offset += sizeof(int8_t);
1599 }
1600
1601 Die->setSize(Offset - Die->getOffset());
1602 return Offset;
1603}
1604
Devang Patel2c4ceb12009-11-21 02:48:08 +00001605/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001606///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001607void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001608 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1609 E = CUMap.end(); I != E; ++I) {
1610 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001611 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001612 sizeof(int32_t) + // Length of Compilation Unit Info
1613 sizeof(int16_t) + // DWARF version number
1614 sizeof(int32_t) + // Offset Into Abbrev. Section
1615 sizeof(int8_t); // Pointer Size (in bytes)
1616 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001617 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001618}
1619
Chris Lattner9c69e285532010-04-04 22:59:04 +00001620/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1621/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001622void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001623 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001624
Bill Wendling94d04b82009-05-20 23:21:38 +00001625 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001626 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001627 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001628 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001629 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001630 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001631
Chris Lattner9c69e285532010-04-04 22:59:04 +00001632 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001633 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001634
Devang Patelaf608bd2010-08-24 00:06:12 +00001635 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001636 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner11b8f302010-04-04 23:02:02 +00001637 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001638 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001639 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001640 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1641 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001642
Devang Patelc3f5f782010-05-25 23:40:22 +00001643 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1644 "section_debug_loc");
1645
Chris Lattner9c69e285532010-04-04 22:59:04 +00001646 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001647 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001648}
1649
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001650/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001651///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001652void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001653 // Get the abbreviation for this DIE.
1654 unsigned AbbrevNumber = Die->getAbbrevNumber();
1655 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1656
Bill Wendling94d04b82009-05-20 23:21:38 +00001657 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001658 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001659 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1660 Twine::utohexstr(Die->getOffset()) + ":0x" +
1661 Twine::utohexstr(Die->getSize()) + " " +
1662 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001663 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001664
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001665 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001666 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1667
1668 // Emit the DIE attribute values.
1669 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1670 unsigned Attr = AbbrevData[i].getAttribute();
1671 unsigned Form = AbbrevData[i].getForm();
1672 assert(Form && "Too many attributes for DIE (check abbreviation)");
1673
Chris Lattner3f53c832010-04-04 18:52:31 +00001674 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001675 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001676
Bill Wendling94d04b82009-05-20 23:21:38 +00001677 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001678 case dwarf::DW_AT_abstract_origin: {
1679 DIEEntry *E = cast<DIEEntry>(Values[i]);
1680 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001681 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001682 Asm->EmitInt32(Addr);
1683 break;
1684 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001685 case dwarf::DW_AT_ranges: {
1686 // DW_AT_range Value encodes offset in debug_range section.
1687 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001688
Nick Lewyckyffccd922012-06-22 01:25:12 +00001689 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001690 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1691 V->getValue(),
1692 4);
1693 } else {
1694 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1695 V->getValue(),
1696 DwarfDebugRangeSectionSym,
1697 4);
1698 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001699 break;
1700 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001701 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001702 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1703 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1704 Asm->EmitLabelReference(L->getValue(), 4);
1705 else
1706 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1707 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001708 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001709 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001710 break;
1711 }
Devang Patel2a361602010-09-29 19:08:08 +00001712 case dwarf::DW_AT_accessibility: {
1713 if (Asm->isVerbose()) {
1714 DIEInteger *V = cast<DIEInteger>(Values[i]);
1715 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1716 }
1717 Values[i]->EmitValue(Asm, Form);
1718 break;
1719 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001720 default:
1721 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001722 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001723 break;
1724 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001725 }
1726
1727 // Emit the DIE children if any.
1728 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1729 const std::vector<DIE *> &Children = Die->getChildren();
1730
1731 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001732 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001733
Chris Lattner3f53c832010-04-04 18:52:31 +00001734 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001735 Asm->OutStreamer.AddComment("End Of Children Mark");
1736 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001737 }
1738}
1739
Devang Patel8a241142009-12-09 18:24:21 +00001740/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001741///
Devang Patel8a241142009-12-09 18:24:21 +00001742void DwarfDebug::emitDebugInfo() {
1743 // Start debug info section.
1744 Asm->OutStreamer.SwitchSection(
1745 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001746 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1747 E = CUMap.end(); I != E; ++I) {
1748 CompileUnit *TheCU = I->second;
1749 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001750
Devang Patel163a9f72010-05-10 22:49:55 +00001751 // Emit the compile units header.
1752 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1753 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001754
Devang Patel163a9f72010-05-10 22:49:55 +00001755 // Emit size of content not including length itself
1756 unsigned ContentSize = Die->getSize() +
1757 sizeof(int16_t) + // DWARF version number
1758 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001759 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001760
Devang Patel163a9f72010-05-10 22:49:55 +00001761 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1762 Asm->EmitInt32(ContentSize);
1763 Asm->OutStreamer.AddComment("DWARF version number");
1764 Asm->EmitInt16(dwarf::DWARF_VERSION);
1765 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1766 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1767 DwarfAbbrevSectionSym);
1768 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001769 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001770
Devang Patel163a9f72010-05-10 22:49:55 +00001771 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001772 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1773 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001774}
1775
Devang Patel2c4ceb12009-11-21 02:48:08 +00001776/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001777///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001778void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001779 // Check to see if it is worth the effort.
1780 if (!Abbreviations.empty()) {
1781 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001782 Asm->OutStreamer.SwitchSection(
1783 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001784
Chris Lattnerc0215722010-04-04 19:25:43 +00001785 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001786
1787 // For each abbrevation.
1788 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1789 // Get abbreviation data
1790 const DIEAbbrev *Abbrev = Abbreviations[i];
1791
1792 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001793 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001794
1795 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001796 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001797 }
1798
1799 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001800 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001801
Chris Lattnerc0215722010-04-04 19:25:43 +00001802 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001803 }
1804}
1805
Devang Patel2c4ceb12009-11-21 02:48:08 +00001806/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001807/// the line matrix.
1808///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001809void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001810 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001811 Asm->OutStreamer.AddComment("Extended Op");
1812 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001813
Chris Lattner233f52b2010-03-09 23:52:58 +00001814 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001815 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001816 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1817 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1818
1819 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001820
Chris Lattnerc0215722010-04-04 19:25:43 +00001821 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001822 Asm->getDataLayout().getPointerSize(),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001823 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001824
1825 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001826 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1827 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001828 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001829 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001830}
1831
Eric Christopher09ac3d82011-11-07 09:24:32 +00001832/// emitAccelNames - Emit visible names into a hashed accelerator table
1833/// section.
1834void DwarfDebug::emitAccelNames() {
1835 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1836 dwarf::DW_FORM_data4));
1837 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1838 E = CUMap.end(); I != E; ++I) {
1839 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001840 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1841 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001842 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1843 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001844 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001845 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1846 DE = Entities.end(); DI != DE; ++DI)
1847 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001848 }
1849 }
1850
1851 AT.FinalizeTable(Asm, "Names");
1852 Asm->OutStreamer.SwitchSection(
1853 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1854 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1855 Asm->OutStreamer.EmitLabel(SectionBegin);
1856
1857 // Emit the full data.
1858 AT.Emit(Asm, SectionBegin, this);
1859}
1860
1861/// emitAccelObjC - Emit objective C classes and categories into a hashed
1862/// accelerator table section.
1863void DwarfDebug::emitAccelObjC() {
1864 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1865 dwarf::DW_FORM_data4));
1866 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1867 E = CUMap.end(); I != E; ++I) {
1868 CompileUnit *TheCU = I->second;
1869 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1870 for (StringMap<std::vector<DIE*> >::const_iterator
1871 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1872 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001873 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001874 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1875 DE = Entities.end(); DI != DE; ++DI)
1876 AT.AddName(Name, (*DI));
1877 }
1878 }
1879
1880 AT.FinalizeTable(Asm, "ObjC");
1881 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1882 .getDwarfAccelObjCSection());
1883 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1884 Asm->OutStreamer.EmitLabel(SectionBegin);
1885
1886 // Emit the full data.
1887 AT.Emit(Asm, SectionBegin, this);
1888}
1889
1890/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1891/// table.
1892void DwarfDebug::emitAccelNamespaces() {
1893 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1894 dwarf::DW_FORM_data4));
1895 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1896 E = CUMap.end(); I != E; ++I) {
1897 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001898 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1899 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001900 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1901 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001902 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001903 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1904 DE = Entities.end(); DI != DE; ++DI)
1905 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001906 }
1907 }
1908
1909 AT.FinalizeTable(Asm, "namespac");
1910 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1911 .getDwarfAccelNamespaceSection());
1912 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1913 Asm->OutStreamer.EmitLabel(SectionBegin);
1914
1915 // Emit the full data.
1916 AT.Emit(Asm, SectionBegin, this);
1917}
1918
1919/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1920void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00001921 std::vector<DwarfAccelTable::Atom> Atoms;
1922 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1923 dwarf::DW_FORM_data4));
1924 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1925 dwarf::DW_FORM_data2));
1926 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1927 dwarf::DW_FORM_data1));
1928 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001929 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1930 E = CUMap.end(); I != E; ++I) {
1931 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001932 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1933 = TheCU->getAccelTypes();
1934 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001935 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1936 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001937 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001938 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1939 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1940 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001941 }
1942 }
1943
1944 AT.FinalizeTable(Asm, "types");
1945 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1946 .getDwarfAccelTypesSection());
1947 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1948 Asm->OutStreamer.EmitLabel(SectionBegin);
1949
1950 // Emit the full data.
1951 AT.Emit(Asm, SectionBegin, this);
1952}
1953
Devang Patel193f7202009-11-24 01:14:22 +00001954void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001955 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1956 E = CUMap.end(); I != E; ++I) {
1957 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00001958 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00001959 Asm->OutStreamer.SwitchSection(
1960 Asm->getObjFileLowering().getDwarfPubTypesSection());
1961 Asm->OutStreamer.AddComment("Length of Public Types Info");
1962 Asm->EmitLabelDifference(
1963 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1964 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001965
Devang Patel163a9f72010-05-10 22:49:55 +00001966 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1967 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001968
Devang Patel163a9f72010-05-10 22:49:55 +00001969 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1970 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001971
Devang Patel163a9f72010-05-10 22:49:55 +00001972 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1973 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1974 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001975
Devang Patel163a9f72010-05-10 22:49:55 +00001976 Asm->OutStreamer.AddComment("Compilation Unit Length");
1977 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1978 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1979 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001980
Devang Patel163a9f72010-05-10 22:49:55 +00001981 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1982 for (StringMap<DIE*>::const_iterator
1983 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1984 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001985 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001986
Devang Patel163a9f72010-05-10 22:49:55 +00001987 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1988 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001989
Devang Patel163a9f72010-05-10 22:49:55 +00001990 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00001991 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00001992 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1993 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001994
Devang Patel163a9f72010-05-10 22:49:55 +00001995 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001996 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001997 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1998 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001999 }
Devang Patel193f7202009-11-24 01:14:22 +00002000}
2001
Devang Patel2c4ceb12009-11-21 02:48:08 +00002002/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002003///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002004void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002005 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002006 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002007
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002008 // Start the dwarf str section.
2009 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002010 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002011
Chris Lattnerbc733f52010-03-13 02:17:42 +00002012 // Get all of the string pool entries and put them in an array by their ID so
2013 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002014 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002015 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002016
Chris Lattnerbc733f52010-03-13 02:17:42 +00002017 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2018 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2019 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002020
Chris Lattnerbc733f52010-03-13 02:17:42 +00002021 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002022
Chris Lattnerbc733f52010-03-13 02:17:42 +00002023 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002024 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002025 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002026
Benjamin Kramer983c4572011-11-09 18:16:11 +00002027 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002028 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2029 Entries[i].second->getKeyLength()+1),
2030 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002031 }
2032}
2033
Devang Patel2c4ceb12009-11-21 02:48:08 +00002034/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002035///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002036void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002037 if (DotDebugLocEntries.empty())
2038 return;
2039
Devang Patel6c3ea902011-02-04 22:57:18 +00002040 for (SmallVector<DotDebugLocEntry, 4>::iterator
2041 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2042 I != E; ++I) {
2043 DotDebugLocEntry &Entry = *I;
2044 if (I + 1 != DotDebugLocEntries.end())
2045 Entry.Merge(I+1);
2046 }
2047
Daniel Dunbar83320a02011-03-16 22:16:39 +00002048 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002049 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002050 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002051 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002052 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2053 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002054 for (SmallVector<DotDebugLocEntry, 4>::iterator
2055 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002056 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002057 DotDebugLocEntry &Entry = *I;
2058 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002059 if (Entry.isEmpty()) {
2060 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2061 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002062 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002063 } else {
2064 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2065 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002066 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002067 Asm->OutStreamer.AddComment("Loc expr size");
2068 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2069 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2070 Asm->EmitLabelDifference(end, begin, 2);
2071 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002072 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002073 DIBasicType BTy(DV.getType());
2074 if (BTy.Verify() &&
2075 (BTy.getEncoding() == dwarf::DW_ATE_signed
2076 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2077 Asm->OutStreamer.AddComment("DW_OP_consts");
2078 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002079 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002080 } else {
2081 Asm->OutStreamer.AddComment("DW_OP_constu");
2082 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002083 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002084 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002085 } else if (Entry.isLocation()) {
2086 if (!DV.hasComplexAddress())
2087 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002088 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002089 else {
2090 // Complex address entry.
2091 unsigned N = DV.getNumAddrElements();
2092 unsigned i = 0;
2093 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2094 if (Entry.Loc.getOffset()) {
2095 i = 2;
2096 Asm->EmitDwarfRegOp(Entry.Loc);
2097 Asm->OutStreamer.AddComment("DW_OP_deref");
2098 Asm->EmitInt8(dwarf::DW_OP_deref);
2099 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2100 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2101 Asm->EmitSLEB128(DV.getAddrElement(1));
2102 } else {
2103 // If first address element is OpPlus then emit
2104 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2105 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2106 Asm->EmitDwarfRegOp(Loc);
2107 i = 2;
2108 }
2109 } else {
2110 Asm->EmitDwarfRegOp(Entry.Loc);
2111 }
2112
2113 // Emit remaining complex address elements.
2114 for (; i < N; ++i) {
2115 uint64_t Element = DV.getAddrElement(i);
2116 if (Element == DIBuilder::OpPlus) {
2117 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2118 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002119 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002120 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002121 Asm->EmitInt8(dwarf::DW_OP_deref);
2122 } else
2123 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002124 }
Devang Patelc26f5442011-04-28 02:22:40 +00002125 }
Devang Patelc26f5442011-04-28 02:22:40 +00002126 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002127 // else ... ignore constant fp. There is not any good way to
2128 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002129 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002130 }
2131 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002132}
2133
2134/// EmitDebugARanges - Emit visible names into a debug aranges section.
2135///
2136void DwarfDebug::EmitDebugARanges() {
2137 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002138 Asm->OutStreamer.SwitchSection(
2139 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002140}
2141
Devang Patel2c4ceb12009-11-21 02:48:08 +00002142/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002143///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002144void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002145 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002146 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002147 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002148 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Pateleac9c072010-04-27 19:46:33 +00002149 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002150 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002151 I != E; ++I) {
2152 if (*I)
2153 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002154 else
Devang Pateleac9c072010-04-27 19:46:33 +00002155 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002156 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002157}
2158
Devang Patel2c4ceb12009-11-21 02:48:08 +00002159/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002160///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002161void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002162 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002163 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002164 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002165 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002166 }
2167}
2168
Devang Patel2c4ceb12009-11-21 02:48:08 +00002169/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002170/// Section Header:
2171/// 1. length of section
2172/// 2. Dwarf version number
2173/// 3. address size.
2174///
2175/// Entries (one "entry" for each function that was inlined):
2176///
2177/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2178/// otherwise offset into __debug_str for regular function name.
2179/// 2. offset into __debug_str section for regular function name.
2180/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2181/// instances for the function.
2182///
2183/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2184/// inlined instance; the die_offset points to the inlined_subroutine die in the
2185/// __debug_info section, and the low_pc is the starting address for the
2186/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002187void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002188 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002189 return;
2190
Devang Patel163a9f72010-05-10 22:49:55 +00002191 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002192 return;
2193
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002194 Asm->OutStreamer.SwitchSection(
2195 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002196
Chris Lattner233f52b2010-03-09 23:52:58 +00002197 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002198 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2199 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002200
Chris Lattnerc0215722010-04-04 19:25:43 +00002201 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002202
Chris Lattner233f52b2010-03-09 23:52:58 +00002203 Asm->OutStreamer.AddComment("Dwarf Version");
2204 Asm->EmitInt16(dwarf::DWARF_VERSION);
2205 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002206 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002207
Devang Patele9f8f5e2010-05-07 20:54:48 +00002208 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002209 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002210
Devang Patele9f8f5e2010-05-07 20:54:48 +00002211 const MDNode *Node = *I;
2212 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002213 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002214 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002215 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002216 StringRef LName = SP.getLinkageName();
2217 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002218
Chris Lattner233f52b2010-03-09 23:52:58 +00002219 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002220 if (LName.empty())
2221 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2222 else
Chris Lattner6189ed12010-04-04 23:25:33 +00002223 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2224 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002225
Chris Lattner233f52b2010-03-09 23:52:58 +00002226 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002227 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002228 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002229
Devang Patel53bb5c92009-11-10 23:06:00 +00002230 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002231 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002232 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002233 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002234
Chris Lattner3f53c832010-04-04 18:52:31 +00002235 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002236 Asm->OutStreamer.EmitSymbolValue(LI->first,
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002237 Asm->getDataLayout().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002238 }
2239 }
2240
Chris Lattnerc0215722010-04-04 19:25:43 +00002241 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002242}