blob: 2c23712a2cd2a54c572b6b3336064f3861e32ee7 [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"
Bill Wendling0310d762009-05-15 09:23:25 +000030#include "llvm/Target/TargetData.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 Christopher09ac3d82011-11-07 09:24:32 +000057static cl::opt<bool> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
58 cl::desc("Output prototype dwarf accelerator tables."),
59 cl::init(false));
60
Eric Christopher10cb7442012-08-23 07:10:46 +000061static cl::opt<bool> DarwinGDBCompat("darwin-gdb-compat", cl::Hidden,
62 cl::desc("Compatibility with Darwin gdb."),
63 cl::init(false));
64
Bill Wendling5f017e82010-04-07 09:28:04 +000065namespace {
66 const char *DWARFGroupName = "DWARF Emission";
67 const char *DbgTimerName = "DWARF Debug Writer";
68} // end anonymous namespace
69
Bill Wendling0310d762009-05-15 09:23:25 +000070//===----------------------------------------------------------------------===//
71
72/// Configuration values for initial hash set sizes (log2).
73///
Bill Wendling0310d762009-05-15 09:23:25 +000074static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000075
76namespace llvm {
77
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000078DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000079 DIType Ty = Var.getType();
80 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
81 // addresses instead.
82 if (Var.isBlockByrefVariable()) {
83 /* Byref variables, in Blocks, are declared by the programmer as
84 "SomeType VarName;", but the compiler creates a
85 __Block_byref_x_VarName struct, and gives the variable VarName
86 either the struct, or a pointer to the struct, as its type. This
87 is necessary for various behind-the-scenes things the compiler
88 needs to do with by-reference variables in blocks.
89
90 However, as far as the original *programmer* is concerned, the
91 variable should still have type 'SomeType', as originally declared.
92
93 The following function dives into the __Block_byref_x_VarName
94 struct to find the original type of the variable. This will be
95 passed back to the code generating the type for the Debug
96 Information Entry for the variable 'VarName'. 'VarName' will then
97 have the original type 'SomeType' in its debug information.
98
99 The original type 'SomeType' will be the type of the field named
100 'VarName' inside the __Block_byref_x_VarName struct.
101
102 NOTE: In order for this to not completely fail on the debugger
103 side, the Debug Information Entry for the variable VarName needs to
104 have a DW_AT_location that tells the debugger how to unwind through
105 the pointers and __Block_byref_x_VarName struct to find the actual
106 value of the variable. The function addBlockByrefType does this. */
107 DIType subType = Ty;
108 unsigned tag = Ty.getTag();
109
110 if (tag == dwarf::DW_TAG_pointer_type) {
111 DIDerivedType DTy = DIDerivedType(Ty);
112 subType = DTy.getTypeDerivedFrom();
113 }
114
115 DICompositeType blockStruct = DICompositeType(subType);
116 DIArray Elements = blockStruct.getTypeArray();
117
118 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
119 DIDescriptor Element = Elements.getElement(i);
120 DIDerivedType DT = DIDerivedType(Element);
121 if (getName() == DT.getName())
122 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000123 }
Devang Patel8bd11de2010-08-09 21:01:39 +0000124 }
Devang Patel3cbee302011-04-12 22:53:02 +0000125 return Ty;
126}
Bill Wendling0310d762009-05-15 09:23:25 +0000127
Chris Lattnerea761862010-04-05 04:09:20 +0000128} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000129
Chris Lattner49cd6642010-04-05 05:11:15 +0000130DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000131 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000132 AbbreviationsSet(InitAbbreviationsSetSize),
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000133 SourceIdMap(DIEValueAllocator), StringPool(DIEValueAllocator),
Eric Christopher6635cad2012-08-01 18:19:01 +0000134 PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000135 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000136
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000137 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000138 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000139 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000140 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000141
Eric Christopher10cb7442012-08-23 07:10:46 +0000142 // Turn on accelerator tables and older gdb compatibility
143 // for Darwin.
144 if (Triple(M->getTargetTriple()).isOSDarwin()) {
Eric Christopher60777d82012-04-02 17:58:52 +0000145 DwarfAccelTables = true;
Eric Christopher10cb7442012-08-23 07:10:46 +0000146 DarwinGDBCompat = true;
147 }
Eric Christopher60777d82012-04-02 17:58:52 +0000148
Dan Gohman03c3dc72010-06-18 15:56:31 +0000149 {
150 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
151 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000152 }
Bill Wendling0310d762009-05-15 09:23:25 +0000153}
154DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000155}
156
Eric Christopherd8a87522011-11-07 09:18:38 +0000157/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
158/// temporary label to it if SymbolStem is specified.
159static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
160 const char *SymbolStem = 0) {
161 Asm->OutStreamer.SwitchSection(Section);
162 if (!SymbolStem) return 0;
163
164 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
165 Asm->OutStreamer.EmitLabel(TmpSym);
166 return TmpSym;
167}
168
Nick Lewycky390c40d2011-10-27 06:44:11 +0000169MCSymbol *DwarfDebug::getStringPool() {
170 return Asm->GetTempSymbol("section_str");
171}
172
Chris Lattnerbc733f52010-03-13 02:17:42 +0000173MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
174 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
175 if (Entry.first) return Entry.first;
176
177 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000178 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000179}
180
Devang Patel2c4ceb12009-11-21 02:48:08 +0000181/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000182///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000183void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000184 // Profile the node so that we can make it unique.
185 FoldingSetNodeID ID;
186 Abbrev.Profile(ID);
187
188 // Check the set for priors.
189 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
190
191 // If it's newly added.
192 if (InSet == &Abbrev) {
193 // Add to abbreviation list.
194 Abbreviations.push_back(&Abbrev);
195
196 // Assign the vector position + 1 as its number.
197 Abbrev.setNumber(Abbreviations.size());
198 } else {
199 // Assign existing abbreviation number.
200 Abbrev.setNumber(InSet->getNumber());
201 }
202}
203
Jim Grosbach1e20b962010-07-21 21:21:52 +0000204/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000205/// printer to not emit usual symbol prefix before the symbol name is used then
206/// return linkage name after skipping this special LLVM prefix.
207static StringRef getRealLinkageName(StringRef LinkageName) {
208 char One = '\1';
209 if (LinkageName.startswith(StringRef(&One, 1)))
210 return LinkageName.substr(1);
211 return LinkageName;
212}
213
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000214static bool isObjCClass(StringRef Name) {
215 return Name.startswith("+") || Name.startswith("-");
216}
217
218static bool hasObjCCategory(StringRef Name) {
219 if (!isObjCClass(Name)) return false;
220
221 size_t pos = Name.find(')');
222 if (pos != std::string::npos) {
223 if (Name[pos+1] != ' ') return false;
224 return true;
225 }
226 return false;
227}
228
229static void getObjCClassCategory(StringRef In, StringRef &Class,
230 StringRef &Category) {
231 if (!hasObjCCategory(In)) {
232 Class = In.slice(In.find('[') + 1, In.find(' '));
233 Category = "";
234 return;
235 }
236
237 Class = In.slice(In.find('[') + 1, In.find('('));
238 Category = In.slice(In.find('[') + 1, In.find(' '));
239 return;
240}
241
242static StringRef getObjCMethodName(StringRef In) {
243 return In.slice(In.find(' ') + 1, In.find(']'));
244}
245
246// Add the various names to the Dwarf accelerator table names.
247static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
248 DIE* Die) {
249 if (!SP.isDefinition()) return;
250
251 TheCU->addAccelName(SP.getName(), Die);
252
253 // If the linkage name is different than the name, go ahead and output
254 // that as well into the name table.
255 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
256 TheCU->addAccelName(SP.getLinkageName(), Die);
257
258 // If this is an Objective-C selector name add it to the ObjC accelerator
259 // too.
260 if (isObjCClass(SP.getName())) {
261 StringRef Class, Category;
262 getObjCClassCategory(SP.getName(), Class, Category);
263 TheCU->addAccelObjC(Class, Die);
264 if (Category != "")
265 TheCU->addAccelObjC(Category, Die);
266 // Also add the base method name to the name table.
267 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
268 }
269}
270
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000271/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000272/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
273/// If there are global variables in this scope then create and insert
274/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000275DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
276 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000277 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000278
Chris Lattnerd38fee82010-04-05 00:13:49 +0000279 assert(SPDie && "Unable to find subprogram DIE!");
280 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000281
Devang Patel5e06bb82011-04-22 23:10:17 +0000282 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindolab0527282011-11-04 19:00:29 +0000283 if (!SPDecl.isSubprogram()) {
Devang Patel5e06bb82011-04-22 23:10:17 +0000284 // There is not any need to generate specification DIE for a function
285 // defined at compile unit level. If a function is defined inside another
286 // function then gdb prefers the definition at top level and but does not
287 // expect specification DIE in parent function. So avoid creating
288 // specification DIE for a function defined inside a function.
289 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
290 !SP.getContext().isFile() &&
291 !isSubprogramContext(SP.getContext())) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000292 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel5e06bb82011-04-22 23:10:17 +0000293
294 // Add arguments.
295 DICompositeType SPTy = SP.getType();
296 DIArray Args = SPTy.getTypeArray();
297 unsigned SPTag = SPTy.getTag();
298 if (SPTag == dwarf::DW_TAG_subroutine_type)
299 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
300 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
301 DIType ATy = DIType(DIType(Args.getElement(i)));
302 SPCU->addType(Arg, ATy);
303 if (ATy.isArtificial())
304 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
305 SPDie->addChild(Arg);
306 }
307 DIE *SPDeclDie = SPDie;
308 SPDie = new DIE(dwarf::DW_TAG_subprogram);
309 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
310 SPDeclDie);
311 SPCU->addDie(SPDie);
312 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000313 }
Devang Patel8aa61472010-07-07 22:20:57 +0000314 // Pick up abstract subprogram DIE.
315 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
316 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000317 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
318 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000319 SPCU->addDie(SPDie);
320 }
321
Devang Patel3cbee302011-04-12 22:53:02 +0000322 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
323 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
324 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
325 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000326 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
327 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000328 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000329
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000330 // Add name to the name table, we do this here because we're guaranteed
331 // to have concrete versions of our DW_TAG_subprogram nodes.
332 addSubprogramNames(SPCU, SP, SPDie);
333
Chris Lattnerd38fee82010-04-05 00:13:49 +0000334 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000335}
336
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000337/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000338/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000339DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
340 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000341 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
342 if (Scope->isAbstractScope())
343 return ScopeDIE;
344
Devang Patelbf47fdb2011-08-10 20:55:27 +0000345 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000346 if (Ranges.empty())
347 return 0;
348
Devang Patelbf47fdb2011-08-10 20:55:27 +0000349 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000350 if (Ranges.size() > 1) {
351 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000352 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000353 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000354 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000355 DebugRangeSymbols.size()
356 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000357 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000358 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000359 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
360 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000361 }
362 DebugRangeSymbols.push_back(NULL);
363 DebugRangeSymbols.push_back(NULL);
364 return ScopeDIE;
365 }
366
Devang Patelc3f5f782010-05-25 23:40:22 +0000367 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
368 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000369
Devang Patelc3f5f782010-05-25 23:40:22 +0000370 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000371
Chris Lattnerb7db7332010-03-09 01:58:53 +0000372 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
373 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000374
Devang Patel3cbee302011-04-12 22:53:02 +0000375 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
376 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000377
378 return ScopeDIE;
379}
380
Devang Patel2c4ceb12009-11-21 02:48:08 +0000381/// constructInlinedScopeDIE - This scope represents inlined body of
382/// a function. Construct DIE to represent this concrete inlined copy
383/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000384DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
385 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000386 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000387 assert(Ranges.empty() == false &&
388 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000389
Devang Patel26a92002011-07-27 00:34:13 +0000390 if (!Scope->getScopeNode())
391 return NULL;
392 DIScope DS(Scope->getScopeNode());
393 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000394 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
395 if (!OriginDIE) {
396 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
397 return NULL;
398 }
399
Devang Patelbf47fdb2011-08-10 20:55:27 +0000400 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000401 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
402 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000403
Devang Patel0afbf232010-07-08 22:39:20 +0000404 if (StartLabel == 0 || EndLabel == 0) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000405 llvm_unreachable("Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000406 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000407 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000408 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000409 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000410 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000411
Devang Pateld96efb82011-05-05 17:54:26 +0000412 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000413 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
414 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000415
Devang Patel26a92002011-07-27 00:34:13 +0000416 if (Ranges.size() > 1) {
417 // .debug_range section has not been laid out yet. Emit offset in
418 // .debug_range as a uint, size 4, for now. emitDIE will handle
419 // DW_AT_ranges appropriately.
420 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000421 DebugRangeSymbols.size()
422 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000423 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000424 RE = Ranges.end(); RI != RE; ++RI) {
425 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
426 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
427 }
428 DebugRangeSymbols.push_back(NULL);
429 DebugRangeSymbols.push_back(NULL);
430 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000431 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
432 StartLabel);
433 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
434 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000435 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000436
437 InlinedSubprogramDIEs.insert(OriginDIE);
438
439 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000440 //.debug_inlined section specification does not clearly state how
441 // to emit inlined scope that is split into multiple instruction ranges.
442 // For now, use first instruction range and emit low_pc/high_pc pair and
443 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000444 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000445 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000446
447 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000448 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000449 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000450 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000451 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000452
Devang Patel53bb5c92009-11-10 23:06:00 +0000453 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000454 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
455 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000456 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000457
Eric Christopher309bedd2011-12-04 06:02:38 +0000458 // Add name to the name table, we do this here because we're guaranteed
459 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
460 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
461
Devang Patel53bb5c92009-11-10 23:06:00 +0000462 return ScopeDIE;
463}
464
Devang Patel2c4ceb12009-11-21 02:48:08 +0000465/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000466DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000467 if (!Scope || !Scope->getScopeNode())
468 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000469
Nick Lewycky746cb672011-10-26 22:55:33 +0000470 SmallVector<DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000471
472 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000473 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000474 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
475 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000476 if (DIE *Arg =
477 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000478 Children.push_back(Arg);
479
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000480 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000481 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000482 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000483 if (DIE *Variable =
484 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000485 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000486 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000487 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000488 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000489 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000490 DIScope DS(Scope->getScopeNode());
491 DIE *ScopeDIE = NULL;
492 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000493 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000494 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000495 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000496 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000497 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000498 // Note down abstract DIE.
499 if (ScopeDIE)
500 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
501 }
Devang Patel3c91b052010-03-08 20:52:55 +0000502 else
Devang Pateld3024342011-08-15 22:24:32 +0000503 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000504 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000505 else {
506 // There is no need to emit empty lexical block DIE.
507 if (Children.empty())
508 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000509 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000510 }
511
Devang Patelaead63c2010-03-29 22:59:58 +0000512 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000513
Devang Patel5bc9fec2011-02-19 01:31:27 +0000514 // Add children
515 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
516 E = Children.end(); I != E; ++I)
517 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000518
Jim Grosbach1e20b962010-07-21 21:21:52 +0000519 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000520 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000521
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000522 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000523}
524
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000525/// GetOrCreateSourceID - Look up the source id with the given directory and
526/// source file names. If none currently exists, create a new id and insert it
527/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
528/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000529unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
530 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000531 // If FE did not provide a file name, then assume stdin.
532 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000533 return GetOrCreateSourceID("<stdin>", StringRef());
534
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000535 // TODO: this might not belong here. See if we can factor this better.
536 if (DirName == CompilationDir)
537 DirName = "";
538
Nick Lewycky44d798d2011-10-17 23:05:28 +0000539 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000540
Benjamin Kramer74612c22012-03-11 14:56:26 +0000541 // We look up the file/dir pair by concatenating them with a zero byte.
542 SmallString<128> NamePair;
543 NamePair += DirName;
544 NamePair += '\0'; // Zero bytes are not allowed in paths.
545 NamePair += FileName;
546
547 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
548 if (Ent.getValue() != SrcId)
549 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000550
Rafael Espindola5c055632010-11-18 02:04:25 +0000551 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000552 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000553
554 return SrcId;
555}
556
Jim Grosbach1e20b962010-07-21 21:21:52 +0000557/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000558/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000559CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000560 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000561 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000562 CompilationDir = DIUnit.getDirectory();
563 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000564
565 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher438b0922012-02-22 08:46:13 +0000566 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die, Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000567 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000568 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
569 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000570 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000571 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
572 // into an entity.
573 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000574 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000575 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000576 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000577 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000578 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000579 else
Devang Patel3cbee302011-04-12 22:53:02 +0000580 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000581
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000582 if (!CompilationDir.empty())
583 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000584 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000585 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000586
Devang Patel65dbc902009-11-25 17:36:49 +0000587 StringRef Flags = DIUnit.getFlags();
588 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000589 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000590
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000591 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000592 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000593 dwarf::DW_FORM_data1, RVer);
594
Devang Patel163a9f72010-05-10 22:49:55 +0000595 if (!FirstCU)
596 FirstCU = NewCU;
597 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000598 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000599}
600
Devang Patel163a9f72010-05-10 22:49:55 +0000601/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000602void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
603 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000604 CompileUnit *&CURef = SPMap[N];
605 if (CURef)
606 return;
607 CURef = TheCU;
608
Devang Patele4b27562009-08-28 23:24:31 +0000609 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000610 if (!SP.isDefinition())
611 // This is a method declaration which will be handled while constructing
612 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000613 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000614
Devang Pateldbc64af2011-08-15 17:24:54 +0000615 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000616
617 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000618 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000619
620 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000621 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000622
Devang Patel13e16b62009-06-26 01:49:18 +0000623 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000624}
625
Devang Patel02e603f2011-08-15 23:47:24 +0000626/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
627/// as llvm.dbg.enum and llvm.dbg.ty
628void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000629 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
630 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
631 const MDNode *N = NMD->getOperand(i);
632 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
633 constructSubprogramDIE(CU, N);
634 }
635
636 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
637 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
638 const MDNode *N = NMD->getOperand(i);
639 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000640 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000641 }
642
Devang Patel02e603f2011-08-15 23:47:24 +0000643 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
644 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
645 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000646 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
647 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000648 }
649
650 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
651 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
652 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000653 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
654 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000655 }
656}
657
658/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
659/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
660bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
661 DebugInfoFinder DbgFinder;
662 DbgFinder.processModule(*M);
663
664 bool HasDebugInfo = false;
665 // Scan all the compile-units to see if there are any marked as the main
666 // unit. If not, we do not generate debug info.
667 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
668 E = DbgFinder.compile_unit_end(); I != E; ++I) {
669 if (DICompileUnit(*I).isMain()) {
670 HasDebugInfo = true;
671 break;
672 }
673 }
674 if (!HasDebugInfo) return false;
675
676 // Create all the compile unit DIEs.
677 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
678 E = DbgFinder.compile_unit_end(); I != E; ++I)
679 constructCompileUnit(*I);
680
681 // Create DIEs for each global variable.
682 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
683 E = DbgFinder.global_variable_end(); I != E; ++I) {
684 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000685 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000686 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000687 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000688
Devang Patel02e603f2011-08-15 23:47:24 +0000689 // Create DIEs for each subprogram.
690 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
691 E = DbgFinder.subprogram_end(); I != E; ++I) {
692 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000693 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
694 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000695 }
696
697 return HasDebugInfo;
698}
699
Devang Patel2c4ceb12009-11-21 02:48:08 +0000700/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000701/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000702/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000703void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000704 if (DisableDebugInfoPrinting)
705 return;
706
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000707 // If module has named metadata anchors then use them, otherwise scan the
708 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000709 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
710 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000711 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
712 DICompileUnit CUNode(CU_Nodes->getOperand(i));
713 CompileUnit *CU = constructCompileUnit(CUNode);
714 DIArray GVs = CUNode.getGlobalVariables();
715 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000716 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000717 DIArray SPs = CUNode.getSubprograms();
718 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
719 constructSubprogramDIE(CU, SPs.getElement(i));
720 DIArray EnumTypes = CUNode.getEnumTypes();
721 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
722 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
723 DIArray RetainedTypes = CUNode.getRetainedTypes();
724 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
725 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
726 }
Devang Patel02e603f2011-08-15 23:47:24 +0000727 } else if (!collectLegacyDebugInfo(M))
728 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000729
Devang Patel02e603f2011-08-15 23:47:24 +0000730 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000731
Chris Lattnerd850ac72010-04-05 02:19:28 +0000732 // Tell MMI that we have debug info.
733 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000734
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000735 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000736 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000737
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000738 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000739 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000740}
741
Devang Patel2c4ceb12009-11-21 02:48:08 +0000742/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000743///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000744void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000745 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000746 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000747 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000748
Devang Patel94c7ddb2011-08-16 22:09:43 +0000749 // Collect info for variables that were optimized out.
750 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
751 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
752 DICompileUnit TheCU(CU_Nodes->getOperand(i));
753 DIArray Subprograms = TheCU.getSubprograms();
754 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
755 DISubprogram SP(Subprograms.getElement(i));
756 if (ProcessedSPNodes.count(SP) != 0) continue;
757 if (!SP.Verify()) continue;
758 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000759 DIArray Variables = SP.getVariables();
760 if (Variables.getNumElements() == 0) continue;
761
Devang Patel94c7ddb2011-08-16 22:09:43 +0000762 LexicalScope *Scope =
763 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
764 DeadFnScopeMap[SP] = Scope;
765
766 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000767 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000768 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000769 constructSubprogramDIE(SPCU, SP);
770 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000771 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
772 DIVariable DV(Variables.getElement(vi));
773 if (!DV.Verify()) continue;
774 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000775 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000776 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000777 ScopeDIE->addChild(VariableDIE);
778 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000779 }
780 }
781 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000782
Devang Patel53bb5c92009-11-10 23:06:00 +0000783 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
784 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
785 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
786 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000787 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000788 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000789 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
790 AE = AbstractSPDies.end(); AI != AE; ++AI) {
791 DIE *ISP = AI->second;
792 if (InlinedSubprogramDIEs.count(ISP))
793 continue;
794 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
795 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000796
Eric Christopher6635cad2012-08-01 18:19:01 +0000797 // Emit DW_AT_containing_type attribute to connect types with their
798 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000799 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
800 CUE = CUMap.end(); CUI != CUE; ++CUI) {
801 CompileUnit *TheCU = CUI->second;
802 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000803 }
804
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000805 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000806 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000807 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000808 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000809 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000810
811 // End text sections.
812 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000813 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000814 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000815 }
816
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000817 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000818 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000819
820 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000821 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000822
823 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000824 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000825
Eric Christopher09ac3d82011-11-07 09:24:32 +0000826 // Emit info into a dwarf accelerator table sections.
827 if (DwarfAccelTables) {
828 emitAccelNames();
829 emitAccelObjC();
830 emitAccelNamespaces();
831 emitAccelTypes();
832 }
833
Devang Patel193f7202009-11-24 01:14:22 +0000834 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000835 // TODO: When we don't need the option anymore we can
836 // remove all of the code that adds to the table.
837 if (DarwinGDBCompat)
838 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000839
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000840 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000841 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000842
843 // Emit info into a debug aranges section.
844 EmitDebugARanges();
845
846 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000847 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000848
849 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000850 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000851
852 // Emit inline info.
Eric Christopher9eb1a942012-08-23 07:32:02 +0000853 // TODO: When we don't need the option anymore we
854 // can remove all of the code that this section
855 // depends upon.
856 if (DarwinGDBCompat)
857 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000858
Eric Christopher7550f7d2012-03-02 00:30:24 +0000859 // Emit info into a debug str section.
860 emitDebugStr();
861
Devang Patele9a1cca2010-08-02 17:32:15 +0000862 // clean up.
863 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000864 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000865 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
866 E = CUMap.end(); I != E; ++I)
867 delete I->second;
868 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000869}
870
Devang Patel53bb5c92009-11-10 23:06:00 +0000871/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000872DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000873 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000874 LLVMContext &Ctx = DV->getContext();
875 // More then one inlined variable corresponds to one abstract variable.
876 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000877 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000878 if (AbsDbgVariable)
879 return AbsDbgVariable;
880
Devang Patelbf47fdb2011-08-10 20:55:27 +0000881 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000882 if (!Scope)
883 return NULL;
884
Devang Patel5a1a67c2011-08-15 19:01:20 +0000885 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000886 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000887 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000888 return AbsDbgVariable;
889}
890
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000891/// addCurrentFnArgument - If Var is a current function argument then add
892/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000893bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000894 DbgVariable *Var, LexicalScope *Scope) {
895 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000896 return false;
897 DIVariable DV = Var->getVariable();
898 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
899 return false;
900 unsigned ArgNo = DV.getArgNumber();
901 if (ArgNo == 0)
902 return false;
903
Devang Patelcb3a6572011-03-03 20:02:02 +0000904 size_t Size = CurrentFnArguments.size();
905 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000906 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000907 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000908 // arguments does the function have at source level.
909 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000910 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000911 CurrentFnArguments[ArgNo - 1] = Var;
912 return true;
913}
914
Devang Patelee432862010-05-20 19:57:06 +0000915/// collectVariableInfoFromMMITable - Collect variable information from
916/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000917void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000918DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000919 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000920 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
921 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
922 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000923 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000924 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000925 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000926 DIVariable DV(Var);
927 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000928
Devang Patelbf47fdb2011-08-10 20:55:27 +0000929 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000930
Devang Patelfb0ee432009-11-10 23:20:04 +0000931 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000932 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000933 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000934
Devang Patel26c1e562010-05-20 16:36:41 +0000935 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000936 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000937 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000938 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000939 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000940 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000941 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000942 }
Devang Patelee432862010-05-20 19:57:06 +0000943}
Devang Patel90a48ad2010-03-15 18:33:46 +0000944
Jim Grosbach1e20b962010-07-21 21:21:52 +0000945/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000946/// DBG_VALUE instruction, is in a defined reg.
947static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000948 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000949 return MI->getNumOperands() == 3 &&
950 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
951 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000952}
953
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000954/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000955/// at MI.
956static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
957 const MCSymbol *FLabel,
958 const MCSymbol *SLabel,
959 const MachineInstr *MI) {
960 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
961
962 if (MI->getNumOperands() != 3) {
963 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
964 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
965 }
966 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
967 MachineLocation MLoc;
968 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
969 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
970 }
971 if (MI->getOperand(0).isImm())
972 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
973 if (MI->getOperand(0).isFPImm())
974 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
975 if (MI->getOperand(0).isCImm())
976 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
977
Craig Topper5e25ee82012-02-05 08:31:47 +0000978 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +0000979}
980
Devang Patelbf47fdb2011-08-10 20:55:27 +0000981/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000982void
Devang Patel78e127d2010-06-25 22:07:34 +0000983DwarfDebug::collectVariableInfo(const MachineFunction *MF,
984 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000985
Devang Patelee432862010-05-20 19:57:06 +0000986 /// collection info from MMI table.
987 collectVariableInfoFromMMITable(MF, Processed);
988
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000989 for (SmallVectorImpl<const MDNode*>::const_iterator
990 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
991 ++UVI) {
992 const MDNode *Var = *UVI;
993 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +0000994 continue;
995
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000996 // History contains relevant DBG_VALUE instructions for Var and instructions
997 // clobbering it.
998 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
999 if (History.empty())
1000 continue;
1001 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001002
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001003 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001004 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001005 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1006 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001007 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001008 else {
1009 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001010 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001011 else {
1012 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001013 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001014 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001015 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001016 }
1017 }
Devang Patelee432862010-05-20 19:57:06 +00001018 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001019 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001020 continue;
1021
1022 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001023 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001024 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1025 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001026 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001027 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001028 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001029 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001030
1031 // Simple ranges that are fully coalesced.
1032 if (History.size() <= 1 || (History.size() == 2 &&
1033 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001034 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001035 continue;
1036 }
1037
1038 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001039 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001040
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001041 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1042 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1043 const MachineInstr *Begin = *HI;
1044 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001045
Devang Patel4ada1d72011-06-01 23:00:17 +00001046 // Check if DBG_VALUE is truncating a range.
1047 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1048 && !Begin->getOperand(0).getReg())
1049 continue;
1050
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001051 // Compute the range for a register location.
1052 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1053 const MCSymbol *SLabel = 0;
1054
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001055 if (HI + 1 == HE)
1056 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001057 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001058 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001059 else {
1060 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001061 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1062 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001063 if (End->isDebugValue())
1064 SLabel = getLabelBeforeInsn(End);
1065 else {
1066 // End is a normal instruction clobbering the range.
1067 SLabel = getLabelAfterInsn(End);
1068 assert(SLabel && "Forgot label after clobber instruction");
1069 ++HI;
1070 }
1071 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001072
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001073 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001074 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1075 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001076 }
1077 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001078 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001079
1080 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001081 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1082 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1083 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1084 DIVariable DV(Variables.getElement(i));
1085 if (!DV || !DV.Verify() || !Processed.insert(DV))
1086 continue;
1087 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1088 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001089 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001090}
Devang Patel98e1cac2010-05-14 21:01:35 +00001091
Devang Patelc3f5f782010-05-25 23:40:22 +00001092/// getLabelBeforeInsn - Return Label preceding the instruction.
1093const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001094 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1095 assert(Label && "Didn't insert label before instruction");
1096 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001097}
1098
1099/// getLabelAfterInsn - Return Label immediately following the instruction.
1100const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001101 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001102}
1103
Devang Patelcbbe2872010-10-26 17:49:02 +00001104/// beginInstruction - Process beginning of an instruction.
1105void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001106 // Check if source location changes, but ignore DBG_VALUE locations.
1107 if (!MI->isDebugValue()) {
1108 DebugLoc DL = MI->getDebugLoc();
1109 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001110 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001111 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001112 if (DL == PrologEndLoc) {
1113 Flags |= DWARF2_FLAG_PROLOGUE_END;
1114 PrologEndLoc = DebugLoc();
1115 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001116 if (PrologEndLoc.isUnknown())
1117 Flags |= DWARF2_FLAG_IS_STMT;
1118
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001119 if (!DL.isUnknown()) {
1120 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001121 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001122 } else
Devang Patel4243e672011-05-11 19:22:19 +00001123 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001124 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001125 }
Devang Patelaead63c2010-03-29 22:59:58 +00001126
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001127 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001128 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1129 LabelsBeforeInsn.find(MI);
1130
1131 // No label needed.
1132 if (I == LabelsBeforeInsn.end())
1133 return;
1134
1135 // Label already assigned.
1136 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001137 return;
Devang Patel553881b2010-03-29 17:20:31 +00001138
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001139 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001140 PrevLabel = MMI->getContext().CreateTempSymbol();
1141 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001142 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001143 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001144}
1145
Devang Patelcbbe2872010-10-26 17:49:02 +00001146/// endInstruction - Process end of an instruction.
1147void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001148 // Don't create a new label after DBG_VALUE instructions.
1149 // They don't generate code.
1150 if (!MI->isDebugValue())
1151 PrevLabel = 0;
1152
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001153 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1154 LabelsAfterInsn.find(MI);
1155
1156 // No label needed.
1157 if (I == LabelsAfterInsn.end())
1158 return;
1159
1160 // Label already assigned.
1161 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001162 return;
1163
1164 // We need a label after this instruction.
1165 if (!PrevLabel) {
1166 PrevLabel = MMI->getContext().CreateTempSymbol();
1167 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001168 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001169 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001170}
1171
Jim Grosbach1e20b962010-07-21 21:21:52 +00001172/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001173/// Each LexicalScope has first instruction and last instruction to mark
1174/// beginning and end of a scope respectively. Create an inverse map that list
1175/// scopes starts (and ends) with an instruction. One instruction may start (or
1176/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001177void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001178 SmallVector<LexicalScope *, 4> WorkList;
1179 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001180 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001181 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001182
Devang Patelbf47fdb2011-08-10 20:55:27 +00001183 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001184 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001185 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001186 SE = Children.end(); SI != SE; ++SI)
1187 WorkList.push_back(*SI);
1188
Devang Patel53bb5c92009-11-10 23:06:00 +00001189 if (S->isAbstractScope())
1190 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001191
Devang Patelbf47fdb2011-08-10 20:55:27 +00001192 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001193 if (Ranges.empty())
1194 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001195 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001196 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001197 assert(RI->first && "InsnRange does not have first instruction!");
1198 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001199 requestLabelBeforeInsn(RI->first);
1200 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001201 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001202 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001203}
1204
Devang Patela3f48672011-05-09 22:14:49 +00001205/// getScopeNode - Get MDNode for DebugLoc's scope.
1206static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1207 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1208 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1209 return DL.getScope(Ctx);
1210}
1211
Devang Patel4243e672011-05-11 19:22:19 +00001212/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher6126a1e2012-04-03 00:43:49 +00001213/// line number info for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001214static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1215 const MDNode *Scope = getScopeNode(DL, Ctx);
1216 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001217 if (SP.Verify()) {
1218 // Check for number of operands since the compatibility is
1219 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001220 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001221 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1222 else
1223 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1224 }
1225
Devang Patel4243e672011-05-11 19:22:19 +00001226 return DebugLoc();
1227}
1228
Devang Patel2c4ceb12009-11-21 02:48:08 +00001229/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001230/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001231void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001232 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001233 LScopes.initialize(*MF);
1234 if (LScopes.empty()) return;
1235 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001236
Devang Pateleac9c072010-04-27 19:46:33 +00001237 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1238 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001239 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001240 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001241
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001242 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1243
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001244 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001245 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1246 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1247
Devang Patelb2b31a62010-05-26 19:37:24 +00001248 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001249 I != E; ++I) {
1250 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001251 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1252 II != IE; ++II) {
1253 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001254
Devang Patelb2b31a62010-05-26 19:37:24 +00001255 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001256 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001257
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001258 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001259 const MDNode *Var =
1260 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001261
1262 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001263 if (isDbgValueInDefinedReg(MI))
1264 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1265
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001266 // Check the history of this variable.
1267 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1268 if (History.empty()) {
1269 UserVariables.push_back(Var);
1270 // The first mention of a function argument gets the FunctionBeginSym
1271 // label, so arguments are visible when breaking at function entry.
1272 DIVariable DV(Var);
1273 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1274 DISubprogram(getDISubprogram(DV.getContext()))
1275 .describes(MF->getFunction()))
1276 LabelsBeforeInsn[MI] = FunctionBeginSym;
1277 } else {
1278 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1279 const MachineInstr *Prev = History.back();
1280 if (Prev->isDebugValue()) {
1281 // Coalesce identical entries at the end of History.
1282 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001283 Prev->isIdenticalTo(History[History.size() - 2])) {
1284 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1285 << "\t" << *Prev
1286 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001287 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001288 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001289
1290 // Terminate old register assignments that don't reach MI;
1291 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1292 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1293 isDbgValueInDefinedReg(Prev)) {
1294 // Previous register assignment needs to terminate at the end of
1295 // its basic block.
1296 MachineBasicBlock::const_iterator LastMI =
1297 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001298 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001299 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001300 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1301 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001302 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001303 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001304 else {
1305 // Terminate after LastMI.
1306 History.push_back(LastMI);
1307 }
1308 }
1309 }
1310 }
1311 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001312 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001313 // Not a DBG_VALUE instruction.
1314 if (!MI->isLabel())
1315 AtBlockEntry = false;
1316
Devang Patel4243e672011-05-11 19:22:19 +00001317 // First known non DBG_VALUE location marks beginning of function
1318 // body.
1319 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1320 PrologEndLoc = MI->getDebugLoc();
1321
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001322 // Check if the instruction clobbers any registers with debug vars.
1323 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1324 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1325 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1326 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001327 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1328 AI.isValid(); ++AI) {
1329 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001330 const MDNode *Var = LiveUserVar[Reg];
1331 if (!Var)
1332 continue;
1333 // Reg is now clobbered.
1334 LiveUserVar[Reg] = 0;
1335
1336 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001337 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1338 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001339 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001340 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1341 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001342 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001343 const MachineInstr *Prev = History.back();
1344 // Sanity-check: Register assignments are terminated at the end of
1345 // their block.
1346 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1347 continue;
1348 // Is the variable still in Reg?
1349 if (!isDbgValueInDefinedReg(Prev) ||
1350 Prev->getOperand(0).getReg() != Reg)
1351 continue;
1352 // Var is clobbered. Make sure the next instruction gets a label.
1353 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001354 }
1355 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001356 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001357 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001358 }
1359
1360 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1361 I != E; ++I) {
1362 SmallVectorImpl<const MachineInstr*> &History = I->second;
1363 if (History.empty())
1364 continue;
1365
1366 // Make sure the final register assignments are terminated.
1367 const MachineInstr *Prev = History.back();
1368 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1369 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001370 MachineBasicBlock::const_iterator LastMI =
1371 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001372 if (LastMI == PrevMBB->end())
1373 // Drop DBG_VALUE for empty range.
1374 History.pop_back();
1375 else {
1376 // Terminate after LastMI.
1377 History.push_back(LastMI);
1378 }
1379 }
1380 // Request labels for the full history.
1381 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1382 const MachineInstr *MI = History[i];
1383 if (MI->isDebugValue())
1384 requestLabelBeforeInsn(MI);
1385 else
1386 requestLabelAfterInsn(MI);
1387 }
1388 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001389
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001390 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001391 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001392
1393 // Record beginning of function.
1394 if (!PrologEndLoc.isUnknown()) {
1395 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1396 MF->getFunction()->getContext());
1397 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1398 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopher5cf55e12012-07-12 23:30:25 +00001399 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0);
Devang Patel4243e672011-05-11 19:22:19 +00001400 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001401}
1402
Devang Patelbf47fdb2011-08-10 20:55:27 +00001403void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1404// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1405 ScopeVariables[LS].push_back(Var);
1406// Vars.push_back(Var);
1407}
1408
Devang Patel2c4ceb12009-11-21 02:48:08 +00001409/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001410///
Chris Lattnereec791a2010-01-26 23:18:02 +00001411void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001412 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001413
Devang Patelbf47fdb2011-08-10 20:55:27 +00001414 // Define end label for subprogram.
1415 FunctionEndSym = Asm->GetTempSymbol("func_end",
1416 Asm->getFunctionNumber());
1417 // Assumes in correct section after the entry point.
1418 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1419
1420 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1421 collectVariableInfo(MF, ProcessedVars);
1422
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001423 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001424 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001425 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001426
Devang Patelbf47fdb2011-08-10 20:55:27 +00001427 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001428 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1429 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1430 LexicalScope *AScope = AList[i];
1431 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001432 if (SP.Verify()) {
1433 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001434 DIArray Variables = SP.getVariables();
1435 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1436 DIVariable DV(Variables.getElement(i));
1437 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1438 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001439 // Check that DbgVariable for DV wasn't created earlier, when
1440 // findAbstractVariable() was called for inlined instance of DV.
1441 LLVMContext &Ctx = DV->getContext();
1442 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1443 if (AbstractVariables.lookup(CleanDV))
1444 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001445 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1446 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001447 }
1448 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001449 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001450 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001451 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001452
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001453 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001454
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001455 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Devang Patel5bc942c2011-08-10 23:58:09 +00001456 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1457 dwarf::DW_FORM_flag, 1);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001458
Devang Patelbf47fdb2011-08-10 20:55:27 +00001459 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1460 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001461
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001462 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001463 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1464 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1465 DeleteContainerPointers(I->second);
1466 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001467 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001468 UserVariables.clear();
1469 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001470 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001471 LabelsBeforeInsn.clear();
1472 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001473 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001474}
1475
Chris Lattnerc6087842010-03-09 04:54:43 +00001476/// recordSourceLine - Register a source line with debug info. Returns the
1477/// unique label that was emitted and which provides correspondence to
1478/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001479void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1480 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001481 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001482 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001483 unsigned Src = 1;
1484 if (S) {
1485 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001486
Dan Gohman1cc0d622010-05-05 23:41:32 +00001487 if (Scope.isCompileUnit()) {
1488 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001489 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001490 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001491 } else if (Scope.isFile()) {
1492 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001493 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001494 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001495 } else if (Scope.isSubprogram()) {
1496 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001497 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001498 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001499 } else if (Scope.isLexicalBlockFile()) {
1500 DILexicalBlockFile DBF(S);
1501 Fn = DBF.getFilename();
1502 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001503 } else if (Scope.isLexicalBlock()) {
1504 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001505 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001506 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001507 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001508 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001509
Devang Patel23670e52011-03-24 20:30:50 +00001510 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001511 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001512 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001513}
1514
Bill Wendling829e67b2009-05-20 23:22:40 +00001515//===----------------------------------------------------------------------===//
1516// Emit Methods
1517//===----------------------------------------------------------------------===//
1518
Devang Patel2c4ceb12009-11-21 02:48:08 +00001519/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001520///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001521unsigned
1522DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001523 // Get the children.
1524 const std::vector<DIE *> &Children = Die->getChildren();
1525
Bill Wendling94d04b82009-05-20 23:21:38 +00001526 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001527 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001528
1529 // Get the abbreviation for this DIE.
1530 unsigned AbbrevNumber = Die->getAbbrevNumber();
1531 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1532
1533 // Set DIE offset
1534 Die->setOffset(Offset);
1535
1536 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001537 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001538
1539 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1540 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1541
1542 // Size the DIE attribute values.
1543 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1544 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001545 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001546
1547 // Size the DIE children if any.
1548 if (!Children.empty()) {
1549 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1550 "Children flag not set");
1551
1552 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001553 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001554
1555 // End of children marker.
1556 Offset += sizeof(int8_t);
1557 }
1558
1559 Die->setSize(Offset - Die->getOffset());
1560 return Offset;
1561}
1562
Devang Patel2c4ceb12009-11-21 02:48:08 +00001563/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001564///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001565void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001566 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1567 E = CUMap.end(); I != E; ++I) {
1568 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001569 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001570 sizeof(int32_t) + // Length of Compilation Unit Info
1571 sizeof(int16_t) + // DWARF version number
1572 sizeof(int32_t) + // Offset Into Abbrev. Section
1573 sizeof(int8_t); // Pointer Size (in bytes)
1574 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001575 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001576}
1577
Chris Lattner9c69e285532010-04-04 22:59:04 +00001578/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1579/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001580void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001581 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001582
Bill Wendling94d04b82009-05-20 23:21:38 +00001583 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001584 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001585 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001586 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001587 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001588 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001589
Chris Lattner9c69e285532010-04-04 22:59:04 +00001590 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001591 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001592
Devang Patelaf608bd2010-08-24 00:06:12 +00001593 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001594 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner11b8f302010-04-04 23:02:02 +00001595 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001596 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001597 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001598 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1599 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001600
Devang Patelc3f5f782010-05-25 23:40:22 +00001601 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1602 "section_debug_loc");
1603
Chris Lattner9c69e285532010-04-04 22:59:04 +00001604 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001605 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001606}
1607
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001608/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001609///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001610void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001611 // Get the abbreviation for this DIE.
1612 unsigned AbbrevNumber = Die->getAbbrevNumber();
1613 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1614
Bill Wendling94d04b82009-05-20 23:21:38 +00001615 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001616 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001617 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1618 Twine::utohexstr(Die->getOffset()) + ":0x" +
1619 Twine::utohexstr(Die->getSize()) + " " +
1620 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001621 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001622
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001623 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001624 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1625
1626 // Emit the DIE attribute values.
1627 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1628 unsigned Attr = AbbrevData[i].getAttribute();
1629 unsigned Form = AbbrevData[i].getForm();
1630 assert(Form && "Too many attributes for DIE (check abbreviation)");
1631
Chris Lattner3f53c832010-04-04 18:52:31 +00001632 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001633 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001634
Bill Wendling94d04b82009-05-20 23:21:38 +00001635 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001636 case dwarf::DW_AT_abstract_origin: {
1637 DIEEntry *E = cast<DIEEntry>(Values[i]);
1638 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001639 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001640 Asm->EmitInt32(Addr);
1641 break;
1642 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001643 case dwarf::DW_AT_ranges: {
1644 // DW_AT_range Value encodes offset in debug_range section.
1645 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001646
Nick Lewyckyffccd922012-06-22 01:25:12 +00001647 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001648 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1649 V->getValue(),
1650 4);
1651 } else {
1652 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1653 V->getValue(),
1654 DwarfDebugRangeSectionSym,
1655 4);
1656 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001657 break;
1658 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001659 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001660 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1661 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1662 Asm->EmitLabelReference(L->getValue(), 4);
1663 else
1664 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1665 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001666 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001667 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001668 break;
1669 }
Devang Patel2a361602010-09-29 19:08:08 +00001670 case dwarf::DW_AT_accessibility: {
1671 if (Asm->isVerbose()) {
1672 DIEInteger *V = cast<DIEInteger>(Values[i]);
1673 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1674 }
1675 Values[i]->EmitValue(Asm, Form);
1676 break;
1677 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001678 default:
1679 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001680 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001681 break;
1682 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001683 }
1684
1685 // Emit the DIE children if any.
1686 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1687 const std::vector<DIE *> &Children = Die->getChildren();
1688
1689 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001690 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001691
Chris Lattner3f53c832010-04-04 18:52:31 +00001692 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001693 Asm->OutStreamer.AddComment("End Of Children Mark");
1694 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001695 }
1696}
1697
Devang Patel8a241142009-12-09 18:24:21 +00001698/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001699///
Devang Patel8a241142009-12-09 18:24:21 +00001700void DwarfDebug::emitDebugInfo() {
1701 // Start debug info section.
1702 Asm->OutStreamer.SwitchSection(
1703 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001704 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1705 E = CUMap.end(); I != E; ++I) {
1706 CompileUnit *TheCU = I->second;
1707 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001708
Devang Patel163a9f72010-05-10 22:49:55 +00001709 // Emit the compile units header.
1710 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1711 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001712
Devang Patel163a9f72010-05-10 22:49:55 +00001713 // Emit size of content not including length itself
1714 unsigned ContentSize = Die->getSize() +
1715 sizeof(int16_t) + // DWARF version number
1716 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001717 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001718
Devang Patel163a9f72010-05-10 22:49:55 +00001719 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1720 Asm->EmitInt32(ContentSize);
1721 Asm->OutStreamer.AddComment("DWARF version number");
1722 Asm->EmitInt16(dwarf::DWARF_VERSION);
1723 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1724 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1725 DwarfAbbrevSectionSym);
1726 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1727 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001728
Devang Patel163a9f72010-05-10 22:49:55 +00001729 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001730 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1731 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001732}
1733
Devang Patel2c4ceb12009-11-21 02:48:08 +00001734/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001735///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001736void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001737 // Check to see if it is worth the effort.
1738 if (!Abbreviations.empty()) {
1739 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001740 Asm->OutStreamer.SwitchSection(
1741 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001742
Chris Lattnerc0215722010-04-04 19:25:43 +00001743 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001744
1745 // For each abbrevation.
1746 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1747 // Get abbreviation data
1748 const DIEAbbrev *Abbrev = Abbreviations[i];
1749
1750 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001751 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001752
1753 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001754 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001755 }
1756
1757 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001758 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001759
Chris Lattnerc0215722010-04-04 19:25:43 +00001760 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001761 }
1762}
1763
Devang Patel2c4ceb12009-11-21 02:48:08 +00001764/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001765/// the line matrix.
1766///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001767void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001768 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001769 Asm->OutStreamer.AddComment("Extended Op");
1770 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001771
Chris Lattner233f52b2010-03-09 23:52:58 +00001772 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001773 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001774 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1775 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1776
1777 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001778
Chris Lattnerc0215722010-04-04 19:25:43 +00001779 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001780 Asm->getTargetData().getPointerSize(),
1781 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001782
1783 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001784 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1785 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001786 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001787 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001788}
1789
Eric Christopher09ac3d82011-11-07 09:24:32 +00001790/// emitAccelNames - Emit visible names into a hashed accelerator table
1791/// section.
1792void DwarfDebug::emitAccelNames() {
1793 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1794 dwarf::DW_FORM_data4));
1795 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1796 E = CUMap.end(); I != E; ++I) {
1797 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001798 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1799 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001800 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1801 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001802 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001803 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1804 DE = Entities.end(); DI != DE; ++DI)
1805 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001806 }
1807 }
1808
1809 AT.FinalizeTable(Asm, "Names");
1810 Asm->OutStreamer.SwitchSection(
1811 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1812 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1813 Asm->OutStreamer.EmitLabel(SectionBegin);
1814
1815 // Emit the full data.
1816 AT.Emit(Asm, SectionBegin, this);
1817}
1818
1819/// emitAccelObjC - Emit objective C classes and categories into a hashed
1820/// accelerator table section.
1821void DwarfDebug::emitAccelObjC() {
1822 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1823 dwarf::DW_FORM_data4));
1824 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1825 E = CUMap.end(); I != E; ++I) {
1826 CompileUnit *TheCU = I->second;
1827 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1828 for (StringMap<std::vector<DIE*> >::const_iterator
1829 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1830 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001831 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001832 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1833 DE = Entities.end(); DI != DE; ++DI)
1834 AT.AddName(Name, (*DI));
1835 }
1836 }
1837
1838 AT.FinalizeTable(Asm, "ObjC");
1839 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1840 .getDwarfAccelObjCSection());
1841 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1842 Asm->OutStreamer.EmitLabel(SectionBegin);
1843
1844 // Emit the full data.
1845 AT.Emit(Asm, SectionBegin, this);
1846}
1847
1848/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1849/// table.
1850void DwarfDebug::emitAccelNamespaces() {
1851 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1852 dwarf::DW_FORM_data4));
1853 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1854 E = CUMap.end(); I != E; ++I) {
1855 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001856 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1857 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001858 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1859 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001860 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001861 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1862 DE = Entities.end(); DI != DE; ++DI)
1863 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001864 }
1865 }
1866
1867 AT.FinalizeTable(Asm, "namespac");
1868 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1869 .getDwarfAccelNamespaceSection());
1870 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1871 Asm->OutStreamer.EmitLabel(SectionBegin);
1872
1873 // Emit the full data.
1874 AT.Emit(Asm, SectionBegin, this);
1875}
1876
1877/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1878void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00001879 std::vector<DwarfAccelTable::Atom> Atoms;
1880 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1881 dwarf::DW_FORM_data4));
1882 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1883 dwarf::DW_FORM_data2));
1884 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1885 dwarf::DW_FORM_data1));
1886 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001887 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1888 E = CUMap.end(); I != E; ++I) {
1889 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001890 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1891 = TheCU->getAccelTypes();
1892 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001893 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1894 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001895 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001896 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1897 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1898 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001899 }
1900 }
1901
1902 AT.FinalizeTable(Asm, "types");
1903 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1904 .getDwarfAccelTypesSection());
1905 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1906 Asm->OutStreamer.EmitLabel(SectionBegin);
1907
1908 // Emit the full data.
1909 AT.Emit(Asm, SectionBegin, this);
1910}
1911
Devang Patel193f7202009-11-24 01:14:22 +00001912void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001913 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1914 E = CUMap.end(); I != E; ++I) {
1915 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00001916 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00001917 Asm->OutStreamer.SwitchSection(
1918 Asm->getObjFileLowering().getDwarfPubTypesSection());
1919 Asm->OutStreamer.AddComment("Length of Public Types Info");
1920 Asm->EmitLabelDifference(
1921 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1922 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001923
Devang Patel163a9f72010-05-10 22:49:55 +00001924 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1925 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001926
Devang Patel163a9f72010-05-10 22:49:55 +00001927 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1928 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001929
Devang Patel163a9f72010-05-10 22:49:55 +00001930 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1931 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1932 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001933
Devang Patel163a9f72010-05-10 22:49:55 +00001934 Asm->OutStreamer.AddComment("Compilation Unit Length");
1935 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1936 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1937 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001938
Devang Patel163a9f72010-05-10 22:49:55 +00001939 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1940 for (StringMap<DIE*>::const_iterator
1941 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1942 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001943 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001944
Devang Patel163a9f72010-05-10 22:49:55 +00001945 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1946 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001947
Devang Patel163a9f72010-05-10 22:49:55 +00001948 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00001949 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00001950 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1951 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001952
Devang Patel163a9f72010-05-10 22:49:55 +00001953 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001954 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001955 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1956 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001957 }
Devang Patel193f7202009-11-24 01:14:22 +00001958}
1959
Devang Patel2c4ceb12009-11-21 02:48:08 +00001960/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001961///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001962void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001963 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001964 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001965
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001966 // Start the dwarf str section.
1967 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001968 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001969
Chris Lattnerbc733f52010-03-13 02:17:42 +00001970 // Get all of the string pool entries and put them in an array by their ID so
1971 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001972 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001973 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001974
Chris Lattnerbc733f52010-03-13 02:17:42 +00001975 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1976 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1977 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001978
Chris Lattnerbc733f52010-03-13 02:17:42 +00001979 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001980
Chris Lattnerbc733f52010-03-13 02:17:42 +00001981 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001982 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001983 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001984
Benjamin Kramer983c4572011-11-09 18:16:11 +00001985 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00001986 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
1987 Entries[i].second->getKeyLength()+1),
1988 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001989 }
1990}
1991
Devang Patel2c4ceb12009-11-21 02:48:08 +00001992/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001993///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001994void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001995 if (DotDebugLocEntries.empty())
1996 return;
1997
Devang Patel6c3ea902011-02-04 22:57:18 +00001998 for (SmallVector<DotDebugLocEntry, 4>::iterator
1999 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2000 I != E; ++I) {
2001 DotDebugLocEntry &Entry = *I;
2002 if (I + 1 != DotDebugLocEntries.end())
2003 Entry.Merge(I+1);
2004 }
2005
Daniel Dunbar83320a02011-03-16 22:16:39 +00002006 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002007 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002008 Asm->getObjFileLowering().getDwarfLocSection());
2009 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002010 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2011 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002012 for (SmallVector<DotDebugLocEntry, 4>::iterator
2013 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002014 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002015 DotDebugLocEntry &Entry = *I;
2016 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002017 if (Entry.isEmpty()) {
2018 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2019 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002020 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002021 } else {
2022 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2023 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002024 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002025 Asm->OutStreamer.AddComment("Loc expr size");
2026 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2027 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2028 Asm->EmitLabelDifference(end, begin, 2);
2029 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002030 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002031 DIBasicType BTy(DV.getType());
2032 if (BTy.Verify() &&
2033 (BTy.getEncoding() == dwarf::DW_ATE_signed
2034 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2035 Asm->OutStreamer.AddComment("DW_OP_consts");
2036 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002037 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002038 } else {
2039 Asm->OutStreamer.AddComment("DW_OP_constu");
2040 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002041 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002042 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002043 } else if (Entry.isLocation()) {
2044 if (!DV.hasComplexAddress())
2045 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002046 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002047 else {
2048 // Complex address entry.
2049 unsigned N = DV.getNumAddrElements();
2050 unsigned i = 0;
2051 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2052 if (Entry.Loc.getOffset()) {
2053 i = 2;
2054 Asm->EmitDwarfRegOp(Entry.Loc);
2055 Asm->OutStreamer.AddComment("DW_OP_deref");
2056 Asm->EmitInt8(dwarf::DW_OP_deref);
2057 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2058 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2059 Asm->EmitSLEB128(DV.getAddrElement(1));
2060 } else {
2061 // If first address element is OpPlus then emit
2062 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2063 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2064 Asm->EmitDwarfRegOp(Loc);
2065 i = 2;
2066 }
2067 } else {
2068 Asm->EmitDwarfRegOp(Entry.Loc);
2069 }
2070
2071 // Emit remaining complex address elements.
2072 for (; i < N; ++i) {
2073 uint64_t Element = DV.getAddrElement(i);
2074 if (Element == DIBuilder::OpPlus) {
2075 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2076 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002077 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002078 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002079 Asm->EmitInt8(dwarf::DW_OP_deref);
2080 } else
2081 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002082 }
Devang Patelc26f5442011-04-28 02:22:40 +00002083 }
Devang Patelc26f5442011-04-28 02:22:40 +00002084 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002085 // else ... ignore constant fp. There is not any good way to
2086 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002087 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002088 }
2089 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002090}
2091
2092/// EmitDebugARanges - Emit visible names into a debug aranges section.
2093///
2094void DwarfDebug::EmitDebugARanges() {
2095 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002096 Asm->OutStreamer.SwitchSection(
2097 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002098}
2099
Devang Patel2c4ceb12009-11-21 02:48:08 +00002100/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002101///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002102void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002103 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002104 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002105 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002106 unsigned char Size = Asm->getTargetData().getPointerSize();
2107 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002108 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002109 I != E; ++I) {
2110 if (*I)
2111 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002112 else
Devang Pateleac9c072010-04-27 19:46:33 +00002113 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002114 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002115}
2116
Devang Patel2c4ceb12009-11-21 02:48:08 +00002117/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002118///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002119void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002120 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002121 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002122 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002123 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002124 }
2125}
2126
Devang Patel2c4ceb12009-11-21 02:48:08 +00002127/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002128/// Section Header:
2129/// 1. length of section
2130/// 2. Dwarf version number
2131/// 3. address size.
2132///
2133/// Entries (one "entry" for each function that was inlined):
2134///
2135/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2136/// otherwise offset into __debug_str for regular function name.
2137/// 2. offset into __debug_str section for regular function name.
2138/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2139/// instances for the function.
2140///
2141/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2142/// inlined instance; the die_offset points to the inlined_subroutine die in the
2143/// __debug_info section, and the low_pc is the starting address for the
2144/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002145void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002146 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002147 return;
2148
Devang Patel163a9f72010-05-10 22:49:55 +00002149 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002150 return;
2151
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002152 Asm->OutStreamer.SwitchSection(
2153 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002154
Chris Lattner233f52b2010-03-09 23:52:58 +00002155 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002156 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2157 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002158
Chris Lattnerc0215722010-04-04 19:25:43 +00002159 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002160
Chris Lattner233f52b2010-03-09 23:52:58 +00002161 Asm->OutStreamer.AddComment("Dwarf Version");
2162 Asm->EmitInt16(dwarf::DWARF_VERSION);
2163 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002164 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002165
Devang Patele9f8f5e2010-05-07 20:54:48 +00002166 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002167 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002168
Devang Patele9f8f5e2010-05-07 20:54:48 +00002169 const MDNode *Node = *I;
2170 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002171 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002172 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002173 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002174 StringRef LName = SP.getLinkageName();
2175 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002176
Chris Lattner233f52b2010-03-09 23:52:58 +00002177 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002178 if (LName.empty())
2179 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2180 else
Chris Lattner6189ed12010-04-04 23:25:33 +00002181 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2182 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002183
Chris Lattner233f52b2010-03-09 23:52:58 +00002184 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002185 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002186 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002187
Devang Patel53bb5c92009-11-10 23:06:00 +00002188 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002189 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002190 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002191 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002192
Chris Lattner3f53c832010-04-04 18:52:31 +00002193 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002194 Asm->OutStreamer.EmitSymbolValue(LI->first,
2195 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002196 }
2197 }
2198
Chris Lattnerc0215722010-04-04 19:25:43 +00002199 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002200}