blob: a3db96aee1f5c34e8fa82c94f12a3a155e1f1f06 [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 Wendling0310d762009-05-15 09:23:25 +000020#include "llvm/Module.h"
Devang Patele449d1f2011-01-20 00:02:16 +000021#include "llvm/Instructions.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000022#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000024#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000025#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000026#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000027#include "llvm/MC/MCSymbol.h"
Bill Wendling0310d762009-05-15 09:23:25 +000028#include "llvm/Target/TargetData.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000029#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000031#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000034#include "llvm/Analysis/DebugInfo.h"
Devang Patel0eea95d2011-02-24 18:49:30 +000035#include "llvm/Analysis/DIBuilder.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"
Devang Pateleac9c072010-04-27 19:46:33 +000039#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000042#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000043#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000044#include "llvm/Support/Timer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000045#include "llvm/Support/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000046using namespace llvm;
47
Jim Grosbach1e20b962010-07-21 21:21:52 +000048static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000049 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000050 cl::desc("Disable debug info printing"));
51
Dan Gohman281d65d2010-05-07 01:08:53 +000052static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner7a2bdde2011-04-15 05:18:47 +000053 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman281d65d2010-05-07 01:08:53 +000054 cl::init(false));
55
Eric Christopher09ac3d82011-11-07 09:24:32 +000056static cl::opt<bool> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
57 cl::desc("Output prototype dwarf accelerator tables."),
58 cl::init(false));
59
Bill Wendling5f017e82010-04-07 09:28:04 +000060namespace {
61 const char *DWARFGroupName = "DWARF Emission";
62 const char *DbgTimerName = "DWARF Debug Writer";
63} // end anonymous namespace
64
Bill Wendling0310d762009-05-15 09:23:25 +000065//===----------------------------------------------------------------------===//
66
67/// Configuration values for initial hash set sizes (log2).
68///
Bill Wendling0310d762009-05-15 09:23:25 +000069static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000070
71namespace llvm {
72
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000073DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000074 DIType Ty = Var.getType();
75 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
76 // addresses instead.
77 if (Var.isBlockByrefVariable()) {
78 /* Byref variables, in Blocks, are declared by the programmer as
79 "SomeType VarName;", but the compiler creates a
80 __Block_byref_x_VarName struct, and gives the variable VarName
81 either the struct, or a pointer to the struct, as its type. This
82 is necessary for various behind-the-scenes things the compiler
83 needs to do with by-reference variables in blocks.
84
85 However, as far as the original *programmer* is concerned, the
86 variable should still have type 'SomeType', as originally declared.
87
88 The following function dives into the __Block_byref_x_VarName
89 struct to find the original type of the variable. This will be
90 passed back to the code generating the type for the Debug
91 Information Entry for the variable 'VarName'. 'VarName' will then
92 have the original type 'SomeType' in its debug information.
93
94 The original type 'SomeType' will be the type of the field named
95 'VarName' inside the __Block_byref_x_VarName struct.
96
97 NOTE: In order for this to not completely fail on the debugger
98 side, the Debug Information Entry for the variable VarName needs to
99 have a DW_AT_location that tells the debugger how to unwind through
100 the pointers and __Block_byref_x_VarName struct to find the actual
101 value of the variable. The function addBlockByrefType does this. */
102 DIType subType = Ty;
103 unsigned tag = Ty.getTag();
104
105 if (tag == dwarf::DW_TAG_pointer_type) {
106 DIDerivedType DTy = DIDerivedType(Ty);
107 subType = DTy.getTypeDerivedFrom();
108 }
109
110 DICompositeType blockStruct = DICompositeType(subType);
111 DIArray Elements = blockStruct.getTypeArray();
112
113 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
114 DIDescriptor Element = Elements.getElement(i);
115 DIDerivedType DT = DIDerivedType(Element);
116 if (getName() == DT.getName())
117 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000118 }
119 return Ty;
120 }
Devang Patel3cbee302011-04-12 22:53:02 +0000121 return Ty;
122}
Bill Wendling0310d762009-05-15 09:23:25 +0000123
Chris Lattnerea761862010-04-05 04:09:20 +0000124} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000125
Chris Lattner49cd6642010-04-05 05:11:15 +0000126DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000127 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000128 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelbf47fdb2011-08-10 20:55:27 +0000129 PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000130 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000131
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000132 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000133 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000134 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000135 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman03c3dc72010-06-18 15:56:31 +0000136 {
137 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
138 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000139 }
Bill Wendling0310d762009-05-15 09:23:25 +0000140}
141DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000142}
143
Eric Christopherd8a87522011-11-07 09:18:38 +0000144/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
145/// temporary label to it if SymbolStem is specified.
146static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
147 const char *SymbolStem = 0) {
148 Asm->OutStreamer.SwitchSection(Section);
149 if (!SymbolStem) return 0;
150
151 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
152 Asm->OutStreamer.EmitLabel(TmpSym);
153 return TmpSym;
154}
155
Nick Lewycky390c40d2011-10-27 06:44:11 +0000156MCSymbol *DwarfDebug::getStringPool() {
157 return Asm->GetTempSymbol("section_str");
158}
159
Chris Lattnerbc733f52010-03-13 02:17:42 +0000160MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
161 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
162 if (Entry.first) return Entry.first;
163
164 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000165 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000166}
167
Devang Patel2c4ceb12009-11-21 02:48:08 +0000168/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000169///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000170void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000171 // Profile the node so that we can make it unique.
172 FoldingSetNodeID ID;
173 Abbrev.Profile(ID);
174
175 // Check the set for priors.
176 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
177
178 // If it's newly added.
179 if (InSet == &Abbrev) {
180 // Add to abbreviation list.
181 Abbreviations.push_back(&Abbrev);
182
183 // Assign the vector position + 1 as its number.
184 Abbrev.setNumber(Abbreviations.size());
185 } else {
186 // Assign existing abbreviation number.
187 Abbrev.setNumber(InSet->getNumber());
188 }
189}
190
Jim Grosbach1e20b962010-07-21 21:21:52 +0000191/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000192/// printer to not emit usual symbol prefix before the symbol name is used then
193/// return linkage name after skipping this special LLVM prefix.
194static StringRef getRealLinkageName(StringRef LinkageName) {
195 char One = '\1';
196 if (LinkageName.startswith(StringRef(&One, 1)))
197 return LinkageName.substr(1);
198 return LinkageName;
199}
200
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000201static bool isObjCClass(StringRef Name) {
202 return Name.startswith("+") || Name.startswith("-");
203}
204
205static bool hasObjCCategory(StringRef Name) {
206 if (!isObjCClass(Name)) return false;
207
208 size_t pos = Name.find(')');
209 if (pos != std::string::npos) {
210 if (Name[pos+1] != ' ') return false;
211 return true;
212 }
213 return false;
214}
215
216static void getObjCClassCategory(StringRef In, StringRef &Class,
217 StringRef &Category) {
218 if (!hasObjCCategory(In)) {
219 Class = In.slice(In.find('[') + 1, In.find(' '));
220 Category = "";
221 return;
222 }
223
224 Class = In.slice(In.find('[') + 1, In.find('('));
225 Category = In.slice(In.find('[') + 1, In.find(' '));
226 return;
227}
228
229static StringRef getObjCMethodName(StringRef In) {
230 return In.slice(In.find(' ') + 1, In.find(']'));
231}
232
233// Add the various names to the Dwarf accelerator table names.
234static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
235 DIE* Die) {
236 if (!SP.isDefinition()) return;
237
238 TheCU->addAccelName(SP.getName(), Die);
239
240 // If the linkage name is different than the name, go ahead and output
241 // that as well into the name table.
242 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
243 TheCU->addAccelName(SP.getLinkageName(), Die);
244
245 // If this is an Objective-C selector name add it to the ObjC accelerator
246 // too.
247 if (isObjCClass(SP.getName())) {
248 StringRef Class, Category;
249 getObjCClassCategory(SP.getName(), Class, Category);
250 TheCU->addAccelObjC(Class, Die);
251 if (Category != "")
252 TheCU->addAccelObjC(Category, Die);
253 // Also add the base method name to the name table.
254 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
255 }
256}
257
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000258/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000259/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
260/// If there are global variables in this scope then create and insert
261/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000262DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
263 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000264 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000265
Chris Lattnerd38fee82010-04-05 00:13:49 +0000266 assert(SPDie && "Unable to find subprogram DIE!");
267 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000268
Devang Patel5e06bb82011-04-22 23:10:17 +0000269 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindolab0527282011-11-04 19:00:29 +0000270 if (!SPDecl.isSubprogram()) {
Devang Patel5e06bb82011-04-22 23:10:17 +0000271 // There is not any need to generate specification DIE for a function
272 // defined at compile unit level. If a function is defined inside another
273 // function then gdb prefers the definition at top level and but does not
274 // expect specification DIE in parent function. So avoid creating
275 // specification DIE for a function defined inside a function.
276 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
277 !SP.getContext().isFile() &&
278 !isSubprogramContext(SP.getContext())) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000279 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel5e06bb82011-04-22 23:10:17 +0000280
281 // Add arguments.
282 DICompositeType SPTy = SP.getType();
283 DIArray Args = SPTy.getTypeArray();
284 unsigned SPTag = SPTy.getTag();
285 if (SPTag == dwarf::DW_TAG_subroutine_type)
286 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
287 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
288 DIType ATy = DIType(DIType(Args.getElement(i)));
289 SPCU->addType(Arg, ATy);
290 if (ATy.isArtificial())
291 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
292 SPDie->addChild(Arg);
293 }
294 DIE *SPDeclDie = SPDie;
295 SPDie = new DIE(dwarf::DW_TAG_subprogram);
296 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
297 SPDeclDie);
298 SPCU->addDie(SPDie);
299 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000300 }
Devang Patel8aa61472010-07-07 22:20:57 +0000301 // Pick up abstract subprogram DIE.
302 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
303 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000304 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
305 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000306 SPCU->addDie(SPDie);
307 }
308
Devang Patel3cbee302011-04-12 22:53:02 +0000309 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
310 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
311 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
312 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000313 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
314 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000315 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000316
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000317 // Add name to the name table, we do this here because we're guaranteed
318 // to have concrete versions of our DW_TAG_subprogram nodes.
319 addSubprogramNames(SPCU, SP, SPDie);
320
Chris Lattnerd38fee82010-04-05 00:13:49 +0000321 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000322}
323
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000324/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000325/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000326DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
327 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000328 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
329 if (Scope->isAbstractScope())
330 return ScopeDIE;
331
Devang Patelbf47fdb2011-08-10 20:55:27 +0000332 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000333 if (Ranges.empty())
334 return 0;
335
Devang Patelbf47fdb2011-08-10 20:55:27 +0000336 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000337 if (Ranges.size() > 1) {
338 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000339 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000340 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000341 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000342 DebugRangeSymbols.size()
343 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000344 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000345 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000346 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
347 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000348 }
349 DebugRangeSymbols.push_back(NULL);
350 DebugRangeSymbols.push_back(NULL);
351 return ScopeDIE;
352 }
353
Devang Patelc3f5f782010-05-25 23:40:22 +0000354 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
355 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000356
Devang Patelc3f5f782010-05-25 23:40:22 +0000357 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000358
Chris Lattnerb7db7332010-03-09 01:58:53 +0000359 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
360 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000361
Devang Patel3cbee302011-04-12 22:53:02 +0000362 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
363 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000364
365 return ScopeDIE;
366}
367
Devang Patel2c4ceb12009-11-21 02:48:08 +0000368/// constructInlinedScopeDIE - This scope represents inlined body of
369/// a function. Construct DIE to represent this concrete inlined copy
370/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000371DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
372 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000373 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000374 assert(Ranges.empty() == false &&
375 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000376
Devang Patel26a92002011-07-27 00:34:13 +0000377 if (!Scope->getScopeNode())
378 return NULL;
379 DIScope DS(Scope->getScopeNode());
380 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000381 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
382 if (!OriginDIE) {
383 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
384 return NULL;
385 }
386
Devang Patelbf47fdb2011-08-10 20:55:27 +0000387 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000388 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
389 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000390
Devang Patel0afbf232010-07-08 22:39:20 +0000391 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000392 assert(0 && "Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000393 return 0;
394 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000395 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000396 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000397 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000398 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000399
Devang Pateld96efb82011-05-05 17:54:26 +0000400 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000401 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
402 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000403
Devang Patel26a92002011-07-27 00:34:13 +0000404 if (Ranges.size() > 1) {
405 // .debug_range section has not been laid out yet. Emit offset in
406 // .debug_range as a uint, size 4, for now. emitDIE will handle
407 // DW_AT_ranges appropriately.
408 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000409 DebugRangeSymbols.size()
410 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000411 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000412 RE = Ranges.end(); RI != RE; ++RI) {
413 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
414 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
415 }
416 DebugRangeSymbols.push_back(NULL);
417 DebugRangeSymbols.push_back(NULL);
418 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000419 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
420 StartLabel);
421 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
422 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000423 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000424
425 InlinedSubprogramDIEs.insert(OriginDIE);
426
427 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000428 //.debug_inlined section specification does not clearly state how
429 // to emit inlined scope that is split into multiple instruction ranges.
430 // For now, use first instruction range and emit low_pc/high_pc pair and
431 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000432 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000433 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000434
435 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000436 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000437 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000438 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000439 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000440
Devang Patel53bb5c92009-11-10 23:06:00 +0000441 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000442 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
443 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000444
Eric Christopher309bedd2011-12-04 06:02:38 +0000445 // Add name to the name table, we do this here because we're guaranteed
446 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
447 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
448
Devang Patel53bb5c92009-11-10 23:06:00 +0000449 return ScopeDIE;
450}
451
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000452
453
Devang Patel2c4ceb12009-11-21 02:48:08 +0000454/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000455DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000456 if (!Scope || !Scope->getScopeNode())
457 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000458
Nick Lewycky746cb672011-10-26 22:55:33 +0000459 SmallVector<DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000460
461 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000462 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000463 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
464 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000465 if (DIE *Arg =
466 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000467 Children.push_back(Arg);
468
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000469 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000470 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000471 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000472 if (DIE *Variable =
473 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000474 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000475 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000476 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000477 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000478 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000479 DIScope DS(Scope->getScopeNode());
480 DIE *ScopeDIE = NULL;
481 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000482 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000483 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000484 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000485 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000486 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000487 // Note down abstract DIE.
488 if (ScopeDIE)
489 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
490 }
Devang Patel3c91b052010-03-08 20:52:55 +0000491 else
Devang Pateld3024342011-08-15 22:24:32 +0000492 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000493 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000494 else {
495 // There is no need to emit empty lexical block DIE.
496 if (Children.empty())
497 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000498 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000499 }
500
Devang Patelaead63c2010-03-29 22:59:58 +0000501 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000502
Devang Patel5bc9fec2011-02-19 01:31:27 +0000503 // Add children
504 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
505 E = Children.end(); I != E; ++I)
506 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000507
Jim Grosbach1e20b962010-07-21 21:21:52 +0000508 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000509 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000510
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000511 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000512}
513
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000514/// GetOrCreateSourceID - Look up the source id with the given directory and
515/// source file names. If none currently exists, create a new id and insert it
516/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
517/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000518unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
519 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000520 // If FE did not provide a file name, then assume stdin.
521 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000522 return GetOrCreateSourceID("<stdin>", StringRef());
523
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000524 // TODO: this might not belong here. See if we can factor this better.
525 if (DirName == CompilationDir)
526 DirName = "";
527
Nick Lewycky44d798d2011-10-17 23:05:28 +0000528 unsigned SrcId = SourceIdMap.size()+1;
529 std::pair<std::string, std::string> SourceName =
530 std::make_pair(FileName, DirName);
531 std::pair<std::pair<std::string, std::string>, unsigned> Entry =
532 make_pair(SourceName, SrcId);
Devang Patel1905a182010-09-16 20:57:49 +0000533
Nick Lewycky44d798d2011-10-17 23:05:28 +0000534 std::map<std::pair<std::string, std::string>, unsigned>::iterator I;
535 bool NewlyInserted;
536 tie(I, NewlyInserted) = SourceIdMap.insert(Entry);
537 if (!NewlyInserted)
538 return I->second;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000539
Rafael Espindola5c055632010-11-18 02:04:25 +0000540 // Print out a .file directive to specify files for .loc directives.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000541 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.first.second,
542 Entry.first.first);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000543
544 return SrcId;
545}
546
Jim Grosbach1e20b962010-07-21 21:21:52 +0000547/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000548/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000549CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000550 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000551 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000552 CompilationDir = DIUnit.getDirectory();
553 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000554
555 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000556 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000557 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000558 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
559 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000560 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000561 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
562 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000563 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000564 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000565 // compile unit in debug_line section.
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000566 if (Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000567 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000568 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000569 else
Devang Patel3cbee302011-04-12 22:53:02 +0000570 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000571
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000572 if (!CompilationDir.empty())
573 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000574 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000575 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000576
Devang Patel65dbc902009-11-25 17:36:49 +0000577 StringRef Flags = DIUnit.getFlags();
578 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000579 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000580
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000581 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000582 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000583 dwarf::DW_FORM_data1, RVer);
584
Devang Patel163a9f72010-05-10 22:49:55 +0000585 if (!FirstCU)
586 FirstCU = NewCU;
587 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000588 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000589}
590
Devang Patel163a9f72010-05-10 22:49:55 +0000591/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000592void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
593 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000594 CompileUnit *&CURef = SPMap[N];
595 if (CURef)
596 return;
597 CURef = TheCU;
598
Devang Patele4b27562009-08-28 23:24:31 +0000599 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000600 if (!SP.isDefinition())
601 // This is a method declaration which will be handled while constructing
602 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000603 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000604
Devang Pateldbc64af2011-08-15 17:24:54 +0000605 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000606
607 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000608 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000609
610 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000611 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000612
Devang Patel13e16b62009-06-26 01:49:18 +0000613 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000614}
615
Devang Patel02e603f2011-08-15 23:47:24 +0000616/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
617/// as llvm.dbg.enum and llvm.dbg.ty
618void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000619 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
620 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
621 const MDNode *N = NMD->getOperand(i);
622 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
623 constructSubprogramDIE(CU, N);
624 }
625
626 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
627 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
628 const MDNode *N = NMD->getOperand(i);
629 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000630 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000631 }
632
Devang Patel02e603f2011-08-15 23:47:24 +0000633 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
634 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
635 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000636 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
637 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000638 }
639
640 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
641 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
642 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000643 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
644 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000645 }
646}
647
648/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
649/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
650bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
651 DebugInfoFinder DbgFinder;
652 DbgFinder.processModule(*M);
653
654 bool HasDebugInfo = false;
655 // Scan all the compile-units to see if there are any marked as the main
656 // unit. If not, we do not generate debug info.
657 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
658 E = DbgFinder.compile_unit_end(); I != E; ++I) {
659 if (DICompileUnit(*I).isMain()) {
660 HasDebugInfo = true;
661 break;
662 }
663 }
664 if (!HasDebugInfo) return false;
665
666 // Create all the compile unit DIEs.
667 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
668 E = DbgFinder.compile_unit_end(); I != E; ++I)
669 constructCompileUnit(*I);
670
671 // Create DIEs for each global variable.
672 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
673 E = DbgFinder.global_variable_end(); I != E; ++I) {
674 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000675 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000676 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000677 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000678
Devang Patel02e603f2011-08-15 23:47:24 +0000679 // Create DIEs for each subprogram.
680 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
681 E = DbgFinder.subprogram_end(); I != E; ++I) {
682 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000683 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
684 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000685 }
686
687 return HasDebugInfo;
688}
689
Devang Patel2c4ceb12009-11-21 02:48:08 +0000690/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000691/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000692/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000693void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000694 if (DisableDebugInfoPrinting)
695 return;
696
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000697 // If module has named metadata anchors then use them, otherwise scan the
698 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000699 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
700 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000701 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
702 DICompileUnit CUNode(CU_Nodes->getOperand(i));
703 CompileUnit *CU = constructCompileUnit(CUNode);
704 DIArray GVs = CUNode.getGlobalVariables();
705 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000706 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000707 DIArray SPs = CUNode.getSubprograms();
708 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
709 constructSubprogramDIE(CU, SPs.getElement(i));
710 DIArray EnumTypes = CUNode.getEnumTypes();
711 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
712 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
713 DIArray RetainedTypes = CUNode.getRetainedTypes();
714 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
715 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
716 }
Devang Patel02e603f2011-08-15 23:47:24 +0000717 } else if (!collectLegacyDebugInfo(M))
718 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000719
Devang Patel02e603f2011-08-15 23:47:24 +0000720 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000721
Chris Lattnerd850ac72010-04-05 02:19:28 +0000722 // Tell MMI that we have debug info.
723 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000724
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000725 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000726 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000727
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000728 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000729 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000730}
731
Devang Patel2c4ceb12009-11-21 02:48:08 +0000732/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000733///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000734void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000735 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000736 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000737 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000738
Devang Patel94c7ddb2011-08-16 22:09:43 +0000739 // Collect info for variables that were optimized out.
740 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
741 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
742 DICompileUnit TheCU(CU_Nodes->getOperand(i));
743 DIArray Subprograms = TheCU.getSubprograms();
744 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
745 DISubprogram SP(Subprograms.getElement(i));
746 if (ProcessedSPNodes.count(SP) != 0) continue;
747 if (!SP.Verify()) continue;
748 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000749 DIArray Variables = SP.getVariables();
750 if (Variables.getNumElements() == 0) continue;
751
Devang Patel94c7ddb2011-08-16 22:09:43 +0000752 LexicalScope *Scope =
753 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
754 DeadFnScopeMap[SP] = Scope;
755
756 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000757 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000758 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000759 constructSubprogramDIE(SPCU, SP);
760 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000761 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
762 DIVariable DV(Variables.getElement(vi));
763 if (!DV.Verify()) continue;
764 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000765 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000766 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000767 ScopeDIE->addChild(VariableDIE);
768 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000769 }
770 }
771 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000772
Devang Patel53bb5c92009-11-10 23:06:00 +0000773 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
774 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
775 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
776 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000777 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000778 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000779 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
780 AE = AbstractSPDies.end(); AI != AE; ++AI) {
781 DIE *ISP = AI->second;
782 if (InlinedSubprogramDIEs.count(ISP))
783 continue;
784 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
785 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000786
Devang Pateldbc64af2011-08-15 17:24:54 +0000787 // Emit DW_AT_containing_type attribute to connect types with their
788 // vtable holding type.
789 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
790 CUE = CUMap.end(); CUI != CUE; ++CUI) {
791 CompileUnit *TheCU = CUI->second;
792 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000793 }
794
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000795 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000796 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000797 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000798 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000799 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000800
801 // End text sections.
802 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000803 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000804 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000805 }
806
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000807 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000808 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000809
810 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000811 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000812
813 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000814 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000815
Eric Christopher09ac3d82011-11-07 09:24:32 +0000816 // Emit info into a dwarf accelerator table sections.
817 if (DwarfAccelTables) {
818 emitAccelNames();
819 emitAccelObjC();
820 emitAccelNamespaces();
821 emitAccelTypes();
822 }
823
Devang Patel193f7202009-11-24 01:14:22 +0000824 // Emit info into a debug pubtypes section.
825 emitDebugPubTypes();
826
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000827 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000828 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000829
830 // Emit info into a debug aranges section.
831 EmitDebugARanges();
832
833 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000834 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000835
836 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000837 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000838
839 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000840 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000841
Chris Lattnerbc733f52010-03-13 02:17:42 +0000842 // Emit info into a debug str section.
843 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000844
Devang Patele9a1cca2010-08-02 17:32:15 +0000845 // clean up.
846 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000847 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000848 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
849 E = CUMap.end(); I != E; ++I)
850 delete I->second;
851 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000852}
853
Devang Patel53bb5c92009-11-10 23:06:00 +0000854/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000855DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000856 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000857 LLVMContext &Ctx = DV->getContext();
858 // More then one inlined variable corresponds to one abstract variable.
859 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000860 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000861 if (AbsDbgVariable)
862 return AbsDbgVariable;
863
Devang Patelbf47fdb2011-08-10 20:55:27 +0000864 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000865 if (!Scope)
866 return NULL;
867
Devang Patel5a1a67c2011-08-15 19:01:20 +0000868 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000869 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000870 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000871 return AbsDbgVariable;
872}
873
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000874/// addCurrentFnArgument - If Var is a current function argument then add
875/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000876bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000877 DbgVariable *Var, LexicalScope *Scope) {
878 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000879 return false;
880 DIVariable DV = Var->getVariable();
881 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
882 return false;
883 unsigned ArgNo = DV.getArgNumber();
884 if (ArgNo == 0)
885 return false;
886
Devang Patelcb3a6572011-03-03 20:02:02 +0000887 size_t Size = CurrentFnArguments.size();
888 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000889 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000890 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000891 // arguments does the function have at source level.
892 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000893 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000894 CurrentFnArguments[ArgNo - 1] = Var;
895 return true;
896}
897
Devang Patelee432862010-05-20 19:57:06 +0000898/// collectVariableInfoFromMMITable - Collect variable information from
899/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000900void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000901DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000902 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000903 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
904 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
905 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000906 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000907 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000908 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000909 DIVariable DV(Var);
910 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000911
Devang Patelbf47fdb2011-08-10 20:55:27 +0000912 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000913
Devang Patelfb0ee432009-11-10 23:20:04 +0000914 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000915 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000916 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000917
Devang Patel26c1e562010-05-20 16:36:41 +0000918 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000919 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000920 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000921 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000922 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000923 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000924 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000925 }
Devang Patelee432862010-05-20 19:57:06 +0000926}
Devang Patel90a48ad2010-03-15 18:33:46 +0000927
Jim Grosbach1e20b962010-07-21 21:21:52 +0000928/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000929/// DBG_VALUE instruction, is in a defined reg.
930static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000931 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000932 return MI->getNumOperands() == 3 &&
933 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
934 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000935}
936
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000937/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000938/// at MI.
939static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
940 const MCSymbol *FLabel,
941 const MCSymbol *SLabel,
942 const MachineInstr *MI) {
943 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
944
945 if (MI->getNumOperands() != 3) {
946 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
947 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
948 }
949 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
950 MachineLocation MLoc;
951 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
952 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
953 }
954 if (MI->getOperand(0).isImm())
955 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
956 if (MI->getOperand(0).isFPImm())
957 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
958 if (MI->getOperand(0).isCImm())
959 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
960
Nick Lewycky746cb672011-10-26 22:55:33 +0000961 assert(0 && "Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +0000962 return DotDebugLocEntry();
963}
964
Devang Patelbf47fdb2011-08-10 20:55:27 +0000965/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000966void
Devang Patel78e127d2010-06-25 22:07:34 +0000967DwarfDebug::collectVariableInfo(const MachineFunction *MF,
968 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000969
Devang Patelee432862010-05-20 19:57:06 +0000970 /// collection info from MMI table.
971 collectVariableInfoFromMMITable(MF, Processed);
972
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000973 for (SmallVectorImpl<const MDNode*>::const_iterator
974 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
975 ++UVI) {
976 const MDNode *Var = *UVI;
977 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +0000978 continue;
979
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000980 // History contains relevant DBG_VALUE instructions for Var and instructions
981 // clobbering it.
982 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
983 if (History.empty())
984 continue;
985 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +0000986
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000987 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000988 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +0000989 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
990 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000991 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +0000992 else {
993 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +0000994 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +0000995 else {
996 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +0000997 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +0000998 else
Devang Patelbf47fdb2011-08-10 20:55:27 +0000999 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001000 }
1001 }
Devang Patelee432862010-05-20 19:57:06 +00001002 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001003 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001004 continue;
1005
1006 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001007 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001008 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1009 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001010 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001011 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001012 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001013 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001014
1015 // Simple ranges that are fully coalesced.
1016 if (History.size() <= 1 || (History.size() == 2 &&
1017 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001018 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001019 continue;
1020 }
1021
1022 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001023 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001024
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001025 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1026 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1027 const MachineInstr *Begin = *HI;
1028 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001029
Devang Patel4ada1d72011-06-01 23:00:17 +00001030 // Check if DBG_VALUE is truncating a range.
1031 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1032 && !Begin->getOperand(0).getReg())
1033 continue;
1034
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001035 // Compute the range for a register location.
1036 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1037 const MCSymbol *SLabel = 0;
1038
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001039 if (HI + 1 == HE)
1040 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001041 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001042 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001043 else {
1044 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001045 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1046 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001047 if (End->isDebugValue())
1048 SLabel = getLabelBeforeInsn(End);
1049 else {
1050 // End is a normal instruction clobbering the range.
1051 SLabel = getLabelAfterInsn(End);
1052 assert(SLabel && "Forgot label after clobber instruction");
1053 ++HI;
1054 }
1055 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001056
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001057 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +00001058 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001059 }
1060 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001061 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001062
1063 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001064 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1065 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1066 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1067 DIVariable DV(Variables.getElement(i));
1068 if (!DV || !DV.Verify() || !Processed.insert(DV))
1069 continue;
1070 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1071 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001072 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001073}
Devang Patel98e1cac2010-05-14 21:01:35 +00001074
Devang Patelc3f5f782010-05-25 23:40:22 +00001075/// getLabelBeforeInsn - Return Label preceding the instruction.
1076const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001077 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1078 assert(Label && "Didn't insert label before instruction");
1079 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001080}
1081
1082/// getLabelAfterInsn - Return Label immediately following the instruction.
1083const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001084 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001085}
1086
Devang Patelcbbe2872010-10-26 17:49:02 +00001087/// beginInstruction - Process beginning of an instruction.
1088void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001089 // Check if source location changes, but ignore DBG_VALUE locations.
1090 if (!MI->isDebugValue()) {
1091 DebugLoc DL = MI->getDebugLoc();
1092 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +00001093 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001094 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001095 if (DL == PrologEndLoc) {
1096 Flags |= DWARF2_FLAG_PROLOGUE_END;
1097 PrologEndLoc = DebugLoc();
1098 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001099 if (!DL.isUnknown()) {
1100 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001101 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001102 } else
Devang Patel4243e672011-05-11 19:22:19 +00001103 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001104 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001105 }
Devang Patelaead63c2010-03-29 22:59:58 +00001106
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001107 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001108 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1109 LabelsBeforeInsn.find(MI);
1110
1111 // No label needed.
1112 if (I == LabelsBeforeInsn.end())
1113 return;
1114
1115 // Label already assigned.
1116 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001117 return;
Devang Patel553881b2010-03-29 17:20:31 +00001118
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001119 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001120 PrevLabel = MMI->getContext().CreateTempSymbol();
1121 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001122 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001123 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001124}
1125
Devang Patelcbbe2872010-10-26 17:49:02 +00001126/// endInstruction - Process end of an instruction.
1127void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001128 // Don't create a new label after DBG_VALUE instructions.
1129 // They don't generate code.
1130 if (!MI->isDebugValue())
1131 PrevLabel = 0;
1132
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001133 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1134 LabelsAfterInsn.find(MI);
1135
1136 // No label needed.
1137 if (I == LabelsAfterInsn.end())
1138 return;
1139
1140 // Label already assigned.
1141 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001142 return;
1143
1144 // We need a label after this instruction.
1145 if (!PrevLabel) {
1146 PrevLabel = MMI->getContext().CreateTempSymbol();
1147 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001148 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001149 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001150}
1151
Jim Grosbach1e20b962010-07-21 21:21:52 +00001152/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001153/// Each LexicalScope has first instruction and last instruction to mark
1154/// beginning and end of a scope respectively. Create an inverse map that list
1155/// scopes starts (and ends) with an instruction. One instruction may start (or
1156/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001157void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001158 SmallVector<LexicalScope *, 4> WorkList;
1159 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001160 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001161 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001162
Devang Patelbf47fdb2011-08-10 20:55:27 +00001163 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001164 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001165 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001166 SE = Children.end(); SI != SE; ++SI)
1167 WorkList.push_back(*SI);
1168
Devang Patel53bb5c92009-11-10 23:06:00 +00001169 if (S->isAbstractScope())
1170 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001171
Devang Patelbf47fdb2011-08-10 20:55:27 +00001172 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001173 if (Ranges.empty())
1174 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001175 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001176 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001177 assert(RI->first && "InsnRange does not have first instruction!");
1178 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001179 requestLabelBeforeInsn(RI->first);
1180 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001181 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001182 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001183}
1184
Devang Patela3f48672011-05-09 22:14:49 +00001185/// getScopeNode - Get MDNode for DebugLoc's scope.
1186static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1187 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1188 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1189 return DL.getScope(Ctx);
1190}
1191
Devang Patel4243e672011-05-11 19:22:19 +00001192/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1193/// line number info for the function.
1194static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1195 const MDNode *Scope = getScopeNode(DL, Ctx);
1196 DISubprogram SP = getDISubprogram(Scope);
1197 if (SP.Verify())
1198 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1199 return DebugLoc();
1200}
1201
Devang Patel2c4ceb12009-11-21 02:48:08 +00001202/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001203/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001204void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001205 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001206 LScopes.initialize(*MF);
1207 if (LScopes.empty()) return;
1208 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001209
Devang Pateleac9c072010-04-27 19:46:33 +00001210 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1211 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001212 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001213 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001214
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001215 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1216
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001217 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001218 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1219 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1220
Devang Patelb2b31a62010-05-26 19:37:24 +00001221 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001222 I != E; ++I) {
1223 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001224 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1225 II != IE; ++II) {
1226 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001227
Devang Patelb2b31a62010-05-26 19:37:24 +00001228 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001229 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001230
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001231 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001232 const MDNode *Var =
1233 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001234
1235 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001236 if (isDbgValueInDefinedReg(MI))
1237 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1238
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001239 // Check the history of this variable.
1240 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1241 if (History.empty()) {
1242 UserVariables.push_back(Var);
1243 // The first mention of a function argument gets the FunctionBeginSym
1244 // label, so arguments are visible when breaking at function entry.
1245 DIVariable DV(Var);
1246 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1247 DISubprogram(getDISubprogram(DV.getContext()))
1248 .describes(MF->getFunction()))
1249 LabelsBeforeInsn[MI] = FunctionBeginSym;
1250 } else {
1251 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1252 const MachineInstr *Prev = History.back();
1253 if (Prev->isDebugValue()) {
1254 // Coalesce identical entries at the end of History.
1255 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001256 Prev->isIdenticalTo(History[History.size() - 2])) {
1257 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1258 << "\t" << *Prev
1259 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001260 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001261 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001262
1263 // Terminate old register assignments that don't reach MI;
1264 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1265 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1266 isDbgValueInDefinedReg(Prev)) {
1267 // Previous register assignment needs to terminate at the end of
1268 // its basic block.
1269 MachineBasicBlock::const_iterator LastMI =
1270 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001271 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001272 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001273 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1274 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001275 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001276 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001277 else {
1278 // Terminate after LastMI.
1279 History.push_back(LastMI);
1280 }
1281 }
1282 }
1283 }
1284 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001285 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001286 // Not a DBG_VALUE instruction.
1287 if (!MI->isLabel())
1288 AtBlockEntry = false;
1289
Devang Patel4243e672011-05-11 19:22:19 +00001290 // First known non DBG_VALUE location marks beginning of function
1291 // body.
1292 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1293 PrologEndLoc = MI->getDebugLoc();
1294
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001295 // Check if the instruction clobbers any registers with debug vars.
1296 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1297 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1298 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1299 continue;
1300 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1301 unsigned Reg = *AI; ++AI) {
1302 const MDNode *Var = LiveUserVar[Reg];
1303 if (!Var)
1304 continue;
1305 // Reg is now clobbered.
1306 LiveUserVar[Reg] = 0;
1307
1308 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001309 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1310 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001311 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001312 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1313 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001314 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001315 const MachineInstr *Prev = History.back();
1316 // Sanity-check: Register assignments are terminated at the end of
1317 // their block.
1318 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1319 continue;
1320 // Is the variable still in Reg?
1321 if (!isDbgValueInDefinedReg(Prev) ||
1322 Prev->getOperand(0).getReg() != Reg)
1323 continue;
1324 // Var is clobbered. Make sure the next instruction gets a label.
1325 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001326 }
1327 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001328 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001329 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001330 }
1331
1332 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1333 I != E; ++I) {
1334 SmallVectorImpl<const MachineInstr*> &History = I->second;
1335 if (History.empty())
1336 continue;
1337
1338 // Make sure the final register assignments are terminated.
1339 const MachineInstr *Prev = History.back();
1340 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1341 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001342 MachineBasicBlock::const_iterator LastMI =
1343 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001344 if (LastMI == PrevMBB->end())
1345 // Drop DBG_VALUE for empty range.
1346 History.pop_back();
1347 else {
1348 // Terminate after LastMI.
1349 History.push_back(LastMI);
1350 }
1351 }
1352 // Request labels for the full history.
1353 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1354 const MachineInstr *MI = History[i];
1355 if (MI->isDebugValue())
1356 requestLabelBeforeInsn(MI);
1357 else
1358 requestLabelAfterInsn(MI);
1359 }
1360 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001361
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001362 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001363 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001364
1365 // Record beginning of function.
1366 if (!PrologEndLoc.isUnknown()) {
1367 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1368 MF->getFunction()->getContext());
1369 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1370 FnStartDL.getScope(MF->getFunction()->getContext()),
1371 DWARF2_FLAG_IS_STMT);
1372 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001373}
1374
Devang Patelbf47fdb2011-08-10 20:55:27 +00001375void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1376// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1377 ScopeVariables[LS].push_back(Var);
1378// Vars.push_back(Var);
1379}
1380
Devang Patel2c4ceb12009-11-21 02:48:08 +00001381/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001382///
Chris Lattnereec791a2010-01-26 23:18:02 +00001383void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001384 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001385
Devang Patelbf47fdb2011-08-10 20:55:27 +00001386 // Define end label for subprogram.
1387 FunctionEndSym = Asm->GetTempSymbol("func_end",
1388 Asm->getFunctionNumber());
1389 // Assumes in correct section after the entry point.
1390 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1391
1392 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1393 collectVariableInfo(MF, ProcessedVars);
1394
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001395 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001396 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001397 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001398
Devang Patelbf47fdb2011-08-10 20:55:27 +00001399 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001400 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1401 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1402 LexicalScope *AScope = AList[i];
1403 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001404 if (SP.Verify()) {
1405 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001406 DIArray Variables = SP.getVariables();
1407 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1408 DIVariable DV(Variables.getElement(i));
1409 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1410 continue;
1411 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1412 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001413 }
1414 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001415 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001416 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001417 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001418
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001419 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001420
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001421 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Devang Patel5bc942c2011-08-10 23:58:09 +00001422 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1423 dwarf::DW_FORM_flag, 1);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001424
Devang Patelbf47fdb2011-08-10 20:55:27 +00001425 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1426 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001427
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001428 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001429 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1430 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1431 DeleteContainerPointers(I->second);
1432 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001433 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001434 UserVariables.clear();
1435 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001436 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001437 LabelsBeforeInsn.clear();
1438 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001439 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001440}
1441
Chris Lattnerc6087842010-03-09 04:54:43 +00001442/// recordSourceLine - Register a source line with debug info. Returns the
1443/// unique label that was emitted and which provides correspondence to
1444/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001445void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1446 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001447 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001448 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001449 unsigned Src = 1;
1450 if (S) {
1451 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001452
Dan Gohman1cc0d622010-05-05 23:41:32 +00001453 if (Scope.isCompileUnit()) {
1454 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001455 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001456 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001457 } else if (Scope.isFile()) {
1458 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001459 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001460 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001461 } else if (Scope.isSubprogram()) {
1462 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001463 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001464 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001465 } else if (Scope.isLexicalBlockFile()) {
1466 DILexicalBlockFile DBF(S);
1467 Fn = DBF.getFilename();
1468 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001469 } else if (Scope.isLexicalBlock()) {
1470 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001471 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001472 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001473 } else
1474 assert(0 && "Unexpected scope info");
1475
Devang Patel23670e52011-03-24 20:30:50 +00001476 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001477 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001478 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001479}
1480
Bill Wendling829e67b2009-05-20 23:22:40 +00001481//===----------------------------------------------------------------------===//
1482// Emit Methods
1483//===----------------------------------------------------------------------===//
1484
Devang Patel2c4ceb12009-11-21 02:48:08 +00001485/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001486///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001487unsigned
1488DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001489 // Get the children.
1490 const std::vector<DIE *> &Children = Die->getChildren();
1491
1492 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001493 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001494 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001495
1496 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001497 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001498
1499 // Get the abbreviation for this DIE.
1500 unsigned AbbrevNumber = Die->getAbbrevNumber();
1501 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1502
1503 // Set DIE offset
1504 Die->setOffset(Offset);
1505
1506 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001507 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001508
1509 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1510 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1511
1512 // Size the DIE attribute values.
1513 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1514 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001515 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001516
1517 // Size the DIE children if any.
1518 if (!Children.empty()) {
1519 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1520 "Children flag not set");
1521
1522 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001523 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001524
1525 // End of children marker.
1526 Offset += sizeof(int8_t);
1527 }
1528
1529 Die->setSize(Offset - Die->getOffset());
1530 return Offset;
1531}
1532
Devang Patel2c4ceb12009-11-21 02:48:08 +00001533/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001534///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001535void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001536 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1537 E = CUMap.end(); I != E; ++I) {
1538 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001539 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001540 sizeof(int32_t) + // Length of Compilation Unit Info
1541 sizeof(int16_t) + // DWARF version number
1542 sizeof(int32_t) + // Offset Into Abbrev. Section
1543 sizeof(int8_t); // Pointer Size (in bytes)
1544 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001545 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001546}
1547
Chris Lattner9c69e285532010-04-04 22:59:04 +00001548/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1549/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001550void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001551 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001552
Bill Wendling94d04b82009-05-20 23:21:38 +00001553 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001554 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001555 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001556 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001557 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001558 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001559
Chris Lattner9c69e285532010-04-04 22:59:04 +00001560 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001561 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001562
Devang Patelaf608bd2010-08-24 00:06:12 +00001563 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001564 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner11b8f302010-04-04 23:02:02 +00001565 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001566 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001567 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001568 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1569 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001570
Devang Patelc3f5f782010-05-25 23:40:22 +00001571 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1572 "section_debug_loc");
1573
Chris Lattner9c69e285532010-04-04 22:59:04 +00001574 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001575 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001576}
1577
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001578/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001579///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001580void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001581 // Get the abbreviation for this DIE.
1582 unsigned AbbrevNumber = Die->getAbbrevNumber();
1583 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1584
Bill Wendling94d04b82009-05-20 23:21:38 +00001585 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001586 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001587 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1588 Twine::utohexstr(Die->getOffset()) + ":0x" +
1589 Twine::utohexstr(Die->getSize()) + " " +
1590 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001591 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001592
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001593 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001594 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1595
1596 // Emit the DIE attribute values.
1597 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1598 unsigned Attr = AbbrevData[i].getAttribute();
1599 unsigned Form = AbbrevData[i].getForm();
1600 assert(Form && "Too many attributes for DIE (check abbreviation)");
1601
Chris Lattner3f53c832010-04-04 18:52:31 +00001602 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001603 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001604
Bill Wendling94d04b82009-05-20 23:21:38 +00001605 switch (Attr) {
1606 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001607 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001608 break;
1609 case dwarf::DW_AT_abstract_origin: {
1610 DIEEntry *E = cast<DIEEntry>(Values[i]);
1611 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001612 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001613 Asm->EmitInt32(Addr);
1614 break;
1615 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001616 case dwarf::DW_AT_ranges: {
1617 // DW_AT_range Value encodes offset in debug_range section.
1618 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001619
1620 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1621 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1622 V->getValue(),
1623 4);
1624 } else {
1625 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1626 V->getValue(),
1627 DwarfDebugRangeSectionSym,
1628 4);
1629 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001630 break;
1631 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001632 case dwarf::DW_AT_location: {
Devang Patel7a328272011-08-15 21:43:21 +00001633 if (DIELabel *L = dyn_cast<DIELabel>(Values[i]))
Daniel Dunbar83320a02011-03-16 22:16:39 +00001634 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel7a328272011-08-15 21:43:21 +00001635 else
Devang Patelc3f5f782010-05-25 23:40:22 +00001636 Values[i]->EmitValue(Asm, Form);
1637 break;
1638 }
Devang Patel2a361602010-09-29 19:08:08 +00001639 case dwarf::DW_AT_accessibility: {
1640 if (Asm->isVerbose()) {
1641 DIEInteger *V = cast<DIEInteger>(Values[i]);
1642 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1643 }
1644 Values[i]->EmitValue(Asm, Form);
1645 break;
1646 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001647 default:
1648 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001649 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001650 break;
1651 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001652 }
1653
1654 // Emit the DIE children if any.
1655 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1656 const std::vector<DIE *> &Children = Die->getChildren();
1657
1658 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001659 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001660
Chris Lattner3f53c832010-04-04 18:52:31 +00001661 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001662 Asm->OutStreamer.AddComment("End Of Children Mark");
1663 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001664 }
1665}
1666
Devang Patel8a241142009-12-09 18:24:21 +00001667/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001668///
Devang Patel8a241142009-12-09 18:24:21 +00001669void DwarfDebug::emitDebugInfo() {
1670 // Start debug info section.
1671 Asm->OutStreamer.SwitchSection(
1672 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001673 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1674 E = CUMap.end(); I != E; ++I) {
1675 CompileUnit *TheCU = I->second;
1676 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001677
Devang Patel163a9f72010-05-10 22:49:55 +00001678 // Emit the compile units header.
1679 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1680 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001681
Devang Patel163a9f72010-05-10 22:49:55 +00001682 // Emit size of content not including length itself
1683 unsigned ContentSize = Die->getSize() +
1684 sizeof(int16_t) + // DWARF version number
1685 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001686 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001687
Devang Patel163a9f72010-05-10 22:49:55 +00001688 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1689 Asm->EmitInt32(ContentSize);
1690 Asm->OutStreamer.AddComment("DWARF version number");
1691 Asm->EmitInt16(dwarf::DWARF_VERSION);
1692 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1693 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1694 DwarfAbbrevSectionSym);
1695 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1696 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001697
Devang Patel163a9f72010-05-10 22:49:55 +00001698 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001699 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1700 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001701}
1702
Devang Patel2c4ceb12009-11-21 02:48:08 +00001703/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001704///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001705void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001706 // Check to see if it is worth the effort.
1707 if (!Abbreviations.empty()) {
1708 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001709 Asm->OutStreamer.SwitchSection(
1710 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001711
Chris Lattnerc0215722010-04-04 19:25:43 +00001712 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001713
1714 // For each abbrevation.
1715 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1716 // Get abbreviation data
1717 const DIEAbbrev *Abbrev = Abbreviations[i];
1718
1719 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001720 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001721
1722 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001723 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001724 }
1725
1726 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001727 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001728
Chris Lattnerc0215722010-04-04 19:25:43 +00001729 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001730 }
1731}
1732
Devang Patel2c4ceb12009-11-21 02:48:08 +00001733/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001734/// the line matrix.
1735///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001736void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001737 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001738 Asm->OutStreamer.AddComment("Extended Op");
1739 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001740
Chris Lattner233f52b2010-03-09 23:52:58 +00001741 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001742 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001743 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1744 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1745
1746 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001747
Chris Lattnerc0215722010-04-04 19:25:43 +00001748 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001749 Asm->getTargetData().getPointerSize(),
1750 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001751
1752 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001753 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1754 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001755 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001756 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001757}
1758
Eric Christopher09ac3d82011-11-07 09:24:32 +00001759/// emitAccelNames - Emit visible names into a hashed accelerator table
1760/// section.
1761void DwarfDebug::emitAccelNames() {
1762 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1763 dwarf::DW_FORM_data4));
1764 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1765 E = CUMap.end(); I != E; ++I) {
1766 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001767 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1768 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001769 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1770 const char *Name = GI->getKeyData();
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001771 std::vector<DIE *> Entities = GI->second;
1772 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1773 DE = Entities.end(); DI != DE; ++DI)
1774 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001775 }
1776 }
1777
1778 AT.FinalizeTable(Asm, "Names");
1779 Asm->OutStreamer.SwitchSection(
1780 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1781 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1782 Asm->OutStreamer.EmitLabel(SectionBegin);
1783
1784 // Emit the full data.
1785 AT.Emit(Asm, SectionBegin, this);
1786}
1787
1788/// emitAccelObjC - Emit objective C classes and categories into a hashed
1789/// accelerator table section.
1790void DwarfDebug::emitAccelObjC() {
1791 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1792 dwarf::DW_FORM_data4));
1793 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1794 E = CUMap.end(); I != E; ++I) {
1795 CompileUnit *TheCU = I->second;
1796 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1797 for (StringMap<std::vector<DIE*> >::const_iterator
1798 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1799 const char *Name = GI->getKeyData();
1800 std::vector<DIE *> Entities = GI->second;
1801 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1802 DE = Entities.end(); DI != DE; ++DI)
1803 AT.AddName(Name, (*DI));
1804 }
1805 }
1806
1807 AT.FinalizeTable(Asm, "ObjC");
1808 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1809 .getDwarfAccelObjCSection());
1810 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1811 Asm->OutStreamer.EmitLabel(SectionBegin);
1812
1813 // Emit the full data.
1814 AT.Emit(Asm, SectionBegin, this);
1815}
1816
1817/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1818/// table.
1819void DwarfDebug::emitAccelNamespaces() {
1820 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1821 dwarf::DW_FORM_data4));
1822 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1823 E = CUMap.end(); I != E; ++I) {
1824 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001825 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1826 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001827 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1828 const char *Name = GI->getKeyData();
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001829 std::vector<DIE *> Entities = GI->second;
1830 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1831 DE = Entities.end(); DI != DE; ++DI)
1832 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001833 }
1834 }
1835
1836 AT.FinalizeTable(Asm, "namespac");
1837 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1838 .getDwarfAccelNamespaceSection());
1839 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1840 Asm->OutStreamer.EmitLabel(SectionBegin);
1841
1842 // Emit the full data.
1843 AT.Emit(Asm, SectionBegin, this);
1844}
1845
1846/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1847void DwarfDebug::emitAccelTypes() {
1848 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1849 dwarf::DW_FORM_data4));
1850 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1851 E = CUMap.end(); I != E; ++I) {
1852 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001853 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelTypes();
1854 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001855 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1856 const char *Name = GI->getKeyData();
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001857 std::vector<DIE *> Entities = GI->second;
1858 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1859 DE= Entities.end(); DI !=DE; ++DI)
1860 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001861 }
1862 }
1863
1864 AT.FinalizeTable(Asm, "types");
1865 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1866 .getDwarfAccelTypesSection());
1867 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1868 Asm->OutStreamer.EmitLabel(SectionBegin);
1869
1870 // Emit the full data.
1871 AT.Emit(Asm, SectionBegin, this);
1872}
1873
Devang Patel193f7202009-11-24 01:14:22 +00001874void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001875 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1876 E = CUMap.end(); I != E; ++I) {
1877 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00001878 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00001879 Asm->OutStreamer.SwitchSection(
1880 Asm->getObjFileLowering().getDwarfPubTypesSection());
1881 Asm->OutStreamer.AddComment("Length of Public Types Info");
1882 Asm->EmitLabelDifference(
1883 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1884 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001885
Devang Patel163a9f72010-05-10 22:49:55 +00001886 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1887 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001888
Devang Patel163a9f72010-05-10 22:49:55 +00001889 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1890 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001891
Devang Patel163a9f72010-05-10 22:49:55 +00001892 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1893 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1894 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001895
Devang Patel163a9f72010-05-10 22:49:55 +00001896 Asm->OutStreamer.AddComment("Compilation Unit Length");
1897 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1898 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1899 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001900
Devang Patel163a9f72010-05-10 22:49:55 +00001901 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1902 for (StringMap<DIE*>::const_iterator
1903 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1904 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001905 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001906
Devang Patel163a9f72010-05-10 22:49:55 +00001907 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1908 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001909
Devang Patel163a9f72010-05-10 22:49:55 +00001910 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00001911 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00001912 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1913 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001914
Devang Patel163a9f72010-05-10 22:49:55 +00001915 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001916 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001917 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1918 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001919 }
Devang Patel193f7202009-11-24 01:14:22 +00001920}
1921
Devang Patel2c4ceb12009-11-21 02:48:08 +00001922/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001923///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001924void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001925 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001926 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001927
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001928 // Start the dwarf str section.
1929 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001930 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001931
Chris Lattnerbc733f52010-03-13 02:17:42 +00001932 // Get all of the string pool entries and put them in an array by their ID so
1933 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001934 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001935 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001936
Chris Lattnerbc733f52010-03-13 02:17:42 +00001937 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1938 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1939 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001940
Chris Lattnerbc733f52010-03-13 02:17:42 +00001941 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001942
Chris Lattnerbc733f52010-03-13 02:17:42 +00001943 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001944 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001945 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001946
Benjamin Kramer983c4572011-11-09 18:16:11 +00001947 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00001948 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
1949 Entries[i].second->getKeyLength()+1),
1950 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001951 }
1952}
1953
Devang Patel2c4ceb12009-11-21 02:48:08 +00001954/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001955///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001956void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001957 if (DotDebugLocEntries.empty())
1958 return;
1959
Devang Patel6c3ea902011-02-04 22:57:18 +00001960 for (SmallVector<DotDebugLocEntry, 4>::iterator
1961 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1962 I != E; ++I) {
1963 DotDebugLocEntry &Entry = *I;
1964 if (I + 1 != DotDebugLocEntries.end())
1965 Entry.Merge(I+1);
1966 }
1967
Daniel Dunbar83320a02011-03-16 22:16:39 +00001968 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001969 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00001970 Asm->getObjFileLowering().getDwarfLocSection());
1971 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00001972 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1973 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001974 for (SmallVector<DotDebugLocEntry, 4>::iterator
1975 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00001976 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00001977 DotDebugLocEntry &Entry = *I;
1978 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00001979 if (Entry.isEmpty()) {
1980 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1981 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00001982 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00001983 } else {
1984 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1985 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00001986 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001987 Asm->OutStreamer.AddComment("Loc expr size");
1988 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1989 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1990 Asm->EmitLabelDifference(end, begin, 2);
1991 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00001992 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00001993 DIBasicType BTy(DV.getType());
1994 if (BTy.Verify() &&
1995 (BTy.getEncoding() == dwarf::DW_ATE_signed
1996 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1997 Asm->OutStreamer.AddComment("DW_OP_consts");
1998 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00001999 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002000 } else {
2001 Asm->OutStreamer.AddComment("DW_OP_constu");
2002 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002003 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002004 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002005 } else if (Entry.isLocation()) {
2006 if (!DV.hasComplexAddress())
2007 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002008 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002009 else {
2010 // Complex address entry.
2011 unsigned N = DV.getNumAddrElements();
2012 unsigned i = 0;
2013 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2014 if (Entry.Loc.getOffset()) {
2015 i = 2;
2016 Asm->EmitDwarfRegOp(Entry.Loc);
2017 Asm->OutStreamer.AddComment("DW_OP_deref");
2018 Asm->EmitInt8(dwarf::DW_OP_deref);
2019 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2020 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2021 Asm->EmitSLEB128(DV.getAddrElement(1));
2022 } else {
2023 // If first address element is OpPlus then emit
2024 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2025 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2026 Asm->EmitDwarfRegOp(Loc);
2027 i = 2;
2028 }
2029 } else {
2030 Asm->EmitDwarfRegOp(Entry.Loc);
2031 }
2032
2033 // Emit remaining complex address elements.
2034 for (; i < N; ++i) {
2035 uint64_t Element = DV.getAddrElement(i);
2036 if (Element == DIBuilder::OpPlus) {
2037 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2038 Asm->EmitULEB128(DV.getAddrElement(++i));
2039 } else if (Element == DIBuilder::OpDeref)
2040 Asm->EmitInt8(dwarf::DW_OP_deref);
2041 else llvm_unreachable("unknown Opcode found in complex address");
2042 }
Devang Patelc26f5442011-04-28 02:22:40 +00002043 }
Devang Patelc26f5442011-04-28 02:22:40 +00002044 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002045 // else ... ignore constant fp. There is not any good way to
2046 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002047 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002048 }
2049 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002050}
2051
2052/// EmitDebugARanges - Emit visible names into a debug aranges section.
2053///
2054void DwarfDebug::EmitDebugARanges() {
2055 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002056 Asm->OutStreamer.SwitchSection(
2057 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002058}
2059
Devang Patel2c4ceb12009-11-21 02:48:08 +00002060/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002061///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002062void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002063 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002064 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002065 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002066 unsigned char Size = Asm->getTargetData().getPointerSize();
2067 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002068 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002069 I != E; ++I) {
2070 if (*I)
2071 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002072 else
Devang Pateleac9c072010-04-27 19:46:33 +00002073 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002074 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002075}
2076
Devang Patel2c4ceb12009-11-21 02:48:08 +00002077/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002078///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002079void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002080 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002081 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002082 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002083 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002084 }
2085}
2086
Devang Patel2c4ceb12009-11-21 02:48:08 +00002087/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002088/// Section Header:
2089/// 1. length of section
2090/// 2. Dwarf version number
2091/// 3. address size.
2092///
2093/// Entries (one "entry" for each function that was inlined):
2094///
2095/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2096/// otherwise offset into __debug_str for regular function name.
2097/// 2. offset into __debug_str section for regular function name.
2098/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2099/// instances for the function.
2100///
2101/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2102/// inlined instance; the die_offset points to the inlined_subroutine die in the
2103/// __debug_info section, and the low_pc is the starting address for the
2104/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002105void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002106 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002107 return;
2108
Devang Patel163a9f72010-05-10 22:49:55 +00002109 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002110 return;
2111
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002112 Asm->OutStreamer.SwitchSection(
2113 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002114
Chris Lattner233f52b2010-03-09 23:52:58 +00002115 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002116 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2117 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002118
Chris Lattnerc0215722010-04-04 19:25:43 +00002119 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002120
Chris Lattner233f52b2010-03-09 23:52:58 +00002121 Asm->OutStreamer.AddComment("Dwarf Version");
2122 Asm->EmitInt16(dwarf::DWARF_VERSION);
2123 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002124 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002125
Devang Patele9f8f5e2010-05-07 20:54:48 +00002126 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002127 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002128
Devang Patele9f8f5e2010-05-07 20:54:48 +00002129 const MDNode *Node = *I;
2130 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002131 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002132 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002133 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002134 StringRef LName = SP.getLinkageName();
2135 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002136
Chris Lattner233f52b2010-03-09 23:52:58 +00002137 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002138 if (LName.empty()) {
2139 Asm->OutStreamer.EmitBytes(Name, 0);
2140 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002141 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00002142 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2143 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002144
Chris Lattner233f52b2010-03-09 23:52:58 +00002145 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002146 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002147 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002148
Devang Patel53bb5c92009-11-10 23:06:00 +00002149 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002150 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002151 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002152 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002153
Chris Lattner3f53c832010-04-04 18:52:31 +00002154 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002155 Asm->OutStreamer.EmitSymbolValue(LI->first,
2156 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002157 }
2158 }
2159
Chris Lattnerc0215722010-04-04 19:25:43 +00002160 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002161}