blob: 0f4ea051a23668690fa57a44acc3ae2f8ee59ff7 [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"
Devang Patel8b9df622011-04-12 17:40:32 +000017#include "DwarfCompileUnit.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000018#include "llvm/Constants.h"
Bill Wendling0310d762009-05-15 09:23:25 +000019#include "llvm/Module.h"
Devang Patele449d1f2011-01-20 00:02:16 +000020#include "llvm/Instructions.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000023#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000024#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000025#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000026#include "llvm/MC/MCSymbol.h"
Bill Wendling0310d762009-05-15 09:23:25 +000027#include "llvm/Target/TargetData.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000028#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000033#include "llvm/Analysis/DebugInfo.h"
Devang Patel0eea95d2011-02-24 18:49:30 +000034#include "llvm/Analysis/DIBuilder.h"
Devang Patel9a31f0f2010-10-25 20:45:32 +000035#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000036#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000037#include "llvm/ADT/StringExtras.h"
Devang Pateleac9c072010-04-27 19:46:33 +000038#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000039#include "llvm/Support/Debug.h"
40#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000041#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000042#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000043#include "llvm/Support/Timer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000044#include "llvm/Support/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000045using namespace llvm;
46
Jim Grosbach1e20b962010-07-21 21:21:52 +000047static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000048 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000049 cl::desc("Disable debug info printing"));
50
Dan Gohman281d65d2010-05-07 01:08:53 +000051static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner7a2bdde2011-04-15 05:18:47 +000052 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman281d65d2010-05-07 01:08:53 +000053 cl::init(false));
54
Bill Wendling5f017e82010-04-07 09:28:04 +000055namespace {
56 const char *DWARFGroupName = "DWARF Emission";
57 const char *DbgTimerName = "DWARF Debug Writer";
58} // end anonymous namespace
59
Bill Wendling0310d762009-05-15 09:23:25 +000060//===----------------------------------------------------------------------===//
61
62/// Configuration values for initial hash set sizes (log2).
63///
Bill Wendling0310d762009-05-15 09:23:25 +000064static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000065
66namespace llvm {
67
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000068DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000069 DIType Ty = Var.getType();
70 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
71 // addresses instead.
72 if (Var.isBlockByrefVariable()) {
73 /* Byref variables, in Blocks, are declared by the programmer as
74 "SomeType VarName;", but the compiler creates a
75 __Block_byref_x_VarName struct, and gives the variable VarName
76 either the struct, or a pointer to the struct, as its type. This
77 is necessary for various behind-the-scenes things the compiler
78 needs to do with by-reference variables in blocks.
79
80 However, as far as the original *programmer* is concerned, the
81 variable should still have type 'SomeType', as originally declared.
82
83 The following function dives into the __Block_byref_x_VarName
84 struct to find the original type of the variable. This will be
85 passed back to the code generating the type for the Debug
86 Information Entry for the variable 'VarName'. 'VarName' will then
87 have the original type 'SomeType' in its debug information.
88
89 The original type 'SomeType' will be the type of the field named
90 'VarName' inside the __Block_byref_x_VarName struct.
91
92 NOTE: In order for this to not completely fail on the debugger
93 side, the Debug Information Entry for the variable VarName needs to
94 have a DW_AT_location that tells the debugger how to unwind through
95 the pointers and __Block_byref_x_VarName struct to find the actual
96 value of the variable. The function addBlockByrefType does this. */
97 DIType subType = Ty;
98 unsigned tag = Ty.getTag();
99
100 if (tag == dwarf::DW_TAG_pointer_type) {
101 DIDerivedType DTy = DIDerivedType(Ty);
102 subType = DTy.getTypeDerivedFrom();
103 }
104
105 DICompositeType blockStruct = DICompositeType(subType);
106 DIArray Elements = blockStruct.getTypeArray();
107
108 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
109 DIDescriptor Element = Elements.getElement(i);
110 DIDerivedType DT = DIDerivedType(Element);
111 if (getName() == DT.getName())
112 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000113 }
114 return Ty;
115 }
Devang Patel3cbee302011-04-12 22:53:02 +0000116 return Ty;
117}
Bill Wendling0310d762009-05-15 09:23:25 +0000118
Chris Lattnerea761862010-04-05 04:09:20 +0000119} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000120
Chris Lattner49cd6642010-04-05 05:11:15 +0000121DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000122 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000123 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelbf47fdb2011-08-10 20:55:27 +0000124 PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000125 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000126
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000127 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000128 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000129 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000130 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman03c3dc72010-06-18 15:56:31 +0000131 {
132 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
133 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000134 }
Bill Wendling0310d762009-05-15 09:23:25 +0000135}
136DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000137}
138
Nick Lewycky390c40d2011-10-27 06:44:11 +0000139MCSymbol *DwarfDebug::getStringPool() {
140 return Asm->GetTempSymbol("section_str");
141}
142
Chris Lattnerbc733f52010-03-13 02:17:42 +0000143MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
144 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
145 if (Entry.first) return Entry.first;
146
147 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000148 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000149}
150
151
Devang Patel2c4ceb12009-11-21 02:48:08 +0000152/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000153///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000154void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000155 // Profile the node so that we can make it unique.
156 FoldingSetNodeID ID;
157 Abbrev.Profile(ID);
158
159 // Check the set for priors.
160 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
161
162 // If it's newly added.
163 if (InSet == &Abbrev) {
164 // Add to abbreviation list.
165 Abbreviations.push_back(&Abbrev);
166
167 // Assign the vector position + 1 as its number.
168 Abbrev.setNumber(Abbreviations.size());
169 } else {
170 // Assign existing abbreviation number.
171 Abbrev.setNumber(InSet->getNumber());
172 }
173}
174
Jim Grosbach1e20b962010-07-21 21:21:52 +0000175/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000176/// printer to not emit usual symbol prefix before the symbol name is used then
177/// return linkage name after skipping this special LLVM prefix.
178static StringRef getRealLinkageName(StringRef LinkageName) {
179 char One = '\1';
180 if (LinkageName.startswith(StringRef(&One, 1)))
181 return LinkageName.substr(1);
182 return LinkageName;
183}
184
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000185/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000186/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
187/// If there are global variables in this scope then create and insert
188/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000189DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
190 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000191 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000192
Chris Lattnerd38fee82010-04-05 00:13:49 +0000193 assert(SPDie && "Unable to find subprogram DIE!");
194 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000195
Devang Patel5e06bb82011-04-22 23:10:17 +0000196 DISubprogram SPDecl = SP.getFunctionDeclaration();
197 if (SPDecl.isSubprogram())
198 // Refer function declaration directly.
Devang Patel3cbee302011-04-12 22:53:02 +0000199 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Pateldbc64af2011-08-15 17:24:54 +0000200 SPCU->getOrCreateSubprogramDIE(SPDecl));
Devang Patel5e06bb82011-04-22 23:10:17 +0000201 else {
202 // There is not any need to generate specification DIE for a function
203 // defined at compile unit level. If a function is defined inside another
204 // function then gdb prefers the definition at top level and but does not
205 // expect specification DIE in parent function. So avoid creating
206 // specification DIE for a function defined inside a function.
207 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
208 !SP.getContext().isFile() &&
209 !isSubprogramContext(SP.getContext())) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000210 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel5e06bb82011-04-22 23:10:17 +0000211
212 // Add arguments.
213 DICompositeType SPTy = SP.getType();
214 DIArray Args = SPTy.getTypeArray();
215 unsigned SPTag = SPTy.getTag();
216 if (SPTag == dwarf::DW_TAG_subroutine_type)
217 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
218 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
219 DIType ATy = DIType(DIType(Args.getElement(i)));
220 SPCU->addType(Arg, ATy);
221 if (ATy.isArtificial())
222 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
223 SPDie->addChild(Arg);
224 }
225 DIE *SPDeclDie = SPDie;
226 SPDie = new DIE(dwarf::DW_TAG_subprogram);
227 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
228 SPDeclDie);
229 SPCU->addDie(SPDie);
230 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000231 }
Devang Patel8aa61472010-07-07 22:20:57 +0000232 // Pick up abstract subprogram DIE.
233 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
234 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000235 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
236 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000237 SPCU->addDie(SPDie);
238 }
239
Devang Patel3cbee302011-04-12 22:53:02 +0000240 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
241 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
242 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
243 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000244 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
245 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000246 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000247
Chris Lattnerd38fee82010-04-05 00:13:49 +0000248 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000249}
250
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000251/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000252/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000253DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
254 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000255 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
256 if (Scope->isAbstractScope())
257 return ScopeDIE;
258
Devang Patelbf47fdb2011-08-10 20:55:27 +0000259 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000260 if (Ranges.empty())
261 return 0;
262
Devang Patelbf47fdb2011-08-10 20:55:27 +0000263 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000264 if (Ranges.size() > 1) {
265 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000266 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000267 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000268 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000269 DebugRangeSymbols.size()
270 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000271 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000272 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000273 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
274 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000275 }
276 DebugRangeSymbols.push_back(NULL);
277 DebugRangeSymbols.push_back(NULL);
278 return ScopeDIE;
279 }
280
Devang Patelc3f5f782010-05-25 23:40:22 +0000281 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
282 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000283
Devang Patelc3f5f782010-05-25 23:40:22 +0000284 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000285
Chris Lattnerb7db7332010-03-09 01:58:53 +0000286 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
287 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000288
Devang Patel3cbee302011-04-12 22:53:02 +0000289 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
290 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000291
292 return ScopeDIE;
293}
294
Devang Patel2c4ceb12009-11-21 02:48:08 +0000295/// constructInlinedScopeDIE - This scope represents inlined body of
296/// a function. Construct DIE to represent this concrete inlined copy
297/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000298DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
299 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000300 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000301 assert(Ranges.empty() == false &&
302 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000303
Devang Patel26a92002011-07-27 00:34:13 +0000304 if (!Scope->getScopeNode())
305 return NULL;
306 DIScope DS(Scope->getScopeNode());
307 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000308 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
309 if (!OriginDIE) {
310 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
311 return NULL;
312 }
313
Devang Patelbf47fdb2011-08-10 20:55:27 +0000314 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000315 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
316 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000317
Devang Patel0afbf232010-07-08 22:39:20 +0000318 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000319 assert(0 && "Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000320 return 0;
321 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000322 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000323 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000324 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000325 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000326
Devang Pateld96efb82011-05-05 17:54:26 +0000327 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000328 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
329 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000330
Devang Patel26a92002011-07-27 00:34:13 +0000331 if (Ranges.size() > 1) {
332 // .debug_range section has not been laid out yet. Emit offset in
333 // .debug_range as a uint, size 4, for now. emitDIE will handle
334 // DW_AT_ranges appropriately.
335 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000336 DebugRangeSymbols.size()
337 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000338 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000339 RE = Ranges.end(); RI != RE; ++RI) {
340 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
341 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
342 }
343 DebugRangeSymbols.push_back(NULL);
344 DebugRangeSymbols.push_back(NULL);
345 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000346 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
347 StartLabel);
348 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
349 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000350 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000351
352 InlinedSubprogramDIEs.insert(OriginDIE);
353
354 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000355 //.debug_inlined section specification does not clearly state how
356 // to emit inlined scope that is split into multiple instruction ranges.
357 // For now, use first instruction range and emit low_pc/high_pc pair and
358 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000359 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000360 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000361
362 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000363 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000364 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000365 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000366 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000367
Devang Patel53bb5c92009-11-10 23:06:00 +0000368 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000369 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
370 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000371
372 return ScopeDIE;
373}
374
Devang Patel2c4ceb12009-11-21 02:48:08 +0000375/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000376DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000377 if (!Scope || !Scope->getScopeNode())
378 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000379
Nick Lewycky746cb672011-10-26 22:55:33 +0000380 SmallVector<DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000381
382 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000383 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000384 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
385 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000386 if (DIE *Arg =
387 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000388 Children.push_back(Arg);
389
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000390 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000391 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000392 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000393 if (DIE *Variable =
394 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000395 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000396 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000397 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000398 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000399 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000400 DIScope DS(Scope->getScopeNode());
401 DIE *ScopeDIE = NULL;
402 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000403 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000404 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000405 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000406 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000407 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000408 // Note down abstract DIE.
409 if (ScopeDIE)
410 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
411 }
Devang Patel3c91b052010-03-08 20:52:55 +0000412 else
Devang Pateld3024342011-08-15 22:24:32 +0000413 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000414 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000415 else {
416 // There is no need to emit empty lexical block DIE.
417 if (Children.empty())
418 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000419 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000420 }
421
Devang Patelaead63c2010-03-29 22:59:58 +0000422 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000423
Devang Patel5bc9fec2011-02-19 01:31:27 +0000424 // Add children
425 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
426 E = Children.end(); I != E; ++I)
427 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000428
Jim Grosbach1e20b962010-07-21 21:21:52 +0000429 if (DS.isSubprogram())
Devang Pateld3024342011-08-15 22:24:32 +0000430 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000431
Devang Patel193f7202009-11-24 01:14:22 +0000432 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000433}
434
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000435/// GetOrCreateSourceID - Look up the source id with the given directory and
436/// source file names. If none currently exists, create a new id and insert it
437/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
438/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000439unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
440 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000441 // If FE did not provide a file name, then assume stdin.
442 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000443 return GetOrCreateSourceID("<stdin>", StringRef());
444
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000445 // TODO: this might not belong here. See if we can factor this better.
446 if (DirName == CompilationDir)
447 DirName = "";
448
Nick Lewycky44d798d2011-10-17 23:05:28 +0000449 unsigned SrcId = SourceIdMap.size()+1;
450 std::pair<std::string, std::string> SourceName =
451 std::make_pair(FileName, DirName);
452 std::pair<std::pair<std::string, std::string>, unsigned> Entry =
453 make_pair(SourceName, SrcId);
Devang Patel1905a182010-09-16 20:57:49 +0000454
Nick Lewycky44d798d2011-10-17 23:05:28 +0000455 std::map<std::pair<std::string, std::string>, unsigned>::iterator I;
456 bool NewlyInserted;
457 tie(I, NewlyInserted) = SourceIdMap.insert(Entry);
458 if (!NewlyInserted)
459 return I->second;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000460
Rafael Espindola5c055632010-11-18 02:04:25 +0000461 // Print out a .file directive to specify files for .loc directives.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000462 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.first.second,
463 Entry.first.first);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000464
465 return SrcId;
466}
467
Jim Grosbach1e20b962010-07-21 21:21:52 +0000468/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000469/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000470CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000471 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000472 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000473 CompilationDir = DIUnit.getDirectory();
474 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000475
476 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000477 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000478 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000479 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
480 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000481 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000482 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
483 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000484 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000485 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000486 // compile unit in debug_line section.
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000487 if (Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000488 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000489 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000490 else
Devang Patel3cbee302011-04-12 22:53:02 +0000491 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000492
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000493 if (!CompilationDir.empty())
494 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000495 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000496 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000497
Devang Patel65dbc902009-11-25 17:36:49 +0000498 StringRef Flags = DIUnit.getFlags();
499 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000500 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000501
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000502 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000503 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000504 dwarf::DW_FORM_data1, RVer);
505
Devang Patel163a9f72010-05-10 22:49:55 +0000506 if (!FirstCU)
507 FirstCU = NewCU;
508 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000509 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000510}
511
Devang Patel163a9f72010-05-10 22:49:55 +0000512/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000513void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
514 const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000515 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000516 if (!SP.isDefinition())
517 // This is a method declaration which will be handled while constructing
518 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000519 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000520
Devang Pateldbc64af2011-08-15 17:24:54 +0000521 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000522
523 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000524 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000525
526 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000527 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000528
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000529 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +0000530 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +0000531
Devang Patel94c7ddb2011-08-16 22:09:43 +0000532 SPMap[N] = TheCU;
Devang Patel13e16b62009-06-26 01:49:18 +0000533 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000534}
535
Devang Patel02e603f2011-08-15 23:47:24 +0000536/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
537/// as llvm.dbg.enum and llvm.dbg.ty
538void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000539 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
540 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
541 const MDNode *N = NMD->getOperand(i);
542 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
543 constructSubprogramDIE(CU, N);
544 }
545
546 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
547 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
548 const MDNode *N = NMD->getOperand(i);
549 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000550 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000551 }
552
Devang Patel02e603f2011-08-15 23:47:24 +0000553 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
554 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
555 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000556 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
557 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000558 }
559
560 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
561 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
562 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000563 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
564 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000565 }
566}
567
568/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
569/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
570bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
571 DebugInfoFinder DbgFinder;
572 DbgFinder.processModule(*M);
573
574 bool HasDebugInfo = false;
575 // Scan all the compile-units to see if there are any marked as the main
576 // unit. If not, we do not generate debug info.
577 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
578 E = DbgFinder.compile_unit_end(); I != E; ++I) {
579 if (DICompileUnit(*I).isMain()) {
580 HasDebugInfo = true;
581 break;
582 }
583 }
584 if (!HasDebugInfo) return false;
585
586 // Create all the compile unit DIEs.
587 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
588 E = DbgFinder.compile_unit_end(); I != E; ++I)
589 constructCompileUnit(*I);
590
591 // Create DIEs for each global variable.
592 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
593 E = DbgFinder.global_variable_end(); I != E; ++I) {
594 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000595 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000596 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000597 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000598
Devang Patel02e603f2011-08-15 23:47:24 +0000599 // Create DIEs for each subprogram.
600 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
601 E = DbgFinder.subprogram_end(); I != E; ++I) {
602 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000603 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
604 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000605 }
606
607 return HasDebugInfo;
608}
609
Devang Patel2c4ceb12009-11-21 02:48:08 +0000610/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000611/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000612/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000613void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000614 if (DisableDebugInfoPrinting)
615 return;
616
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000617 // If module has named metadata anchors then use them, otherwise scan the
618 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000619 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
620 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000621 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
622 DICompileUnit CUNode(CU_Nodes->getOperand(i));
623 CompileUnit *CU = constructCompileUnit(CUNode);
624 DIArray GVs = CUNode.getGlobalVariables();
625 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000626 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000627 DIArray SPs = CUNode.getSubprograms();
628 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
629 constructSubprogramDIE(CU, SPs.getElement(i));
630 DIArray EnumTypes = CUNode.getEnumTypes();
631 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
632 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
633 DIArray RetainedTypes = CUNode.getRetainedTypes();
634 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
635 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
636 }
Devang Patel02e603f2011-08-15 23:47:24 +0000637 } else if (!collectLegacyDebugInfo(M))
638 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000639
Devang Patel02e603f2011-08-15 23:47:24 +0000640 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000641
Chris Lattnerd850ac72010-04-05 02:19:28 +0000642 // Tell MMI that we have debug info.
643 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000644
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000645 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000646 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000647
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000648 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000649 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000650}
651
Devang Patel2c4ceb12009-11-21 02:48:08 +0000652/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000653///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000654void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000655 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000656 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000657 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000658
Devang Patel94c7ddb2011-08-16 22:09:43 +0000659 // Collect info for variables that were optimized out.
660 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
661 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
662 DICompileUnit TheCU(CU_Nodes->getOperand(i));
663 DIArray Subprograms = TheCU.getSubprograms();
664 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
665 DISubprogram SP(Subprograms.getElement(i));
666 if (ProcessedSPNodes.count(SP) != 0) continue;
667 if (!SP.Verify()) continue;
668 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000669 DIArray Variables = SP.getVariables();
670 if (Variables.getNumElements() == 0) continue;
671
Devang Patel94c7ddb2011-08-16 22:09:43 +0000672 LexicalScope *Scope =
673 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
674 DeadFnScopeMap[SP] = Scope;
675
676 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000677 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000678 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000679 constructSubprogramDIE(SPCU, SP);
680 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000681 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
682 DIVariable DV(Variables.getElement(vi));
683 if (!DV.Verify()) continue;
684 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000685 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000686 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000687 ScopeDIE->addChild(VariableDIE);
688 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000689 }
690 }
691 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000692
Devang Patel53bb5c92009-11-10 23:06:00 +0000693 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
694 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
695 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
696 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000697 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000698 }
699
Devang Pateldbc64af2011-08-15 17:24:54 +0000700 // Emit DW_AT_containing_type attribute to connect types with their
701 // vtable holding type.
702 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
703 CUE = CUMap.end(); CUI != CUE; ++CUI) {
704 CompileUnit *TheCU = CUI->second;
705 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000706 }
707
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000708 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000709 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000710 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000711 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000712 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000713
714 // End text sections.
715 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000716 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000717 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000718 }
719
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000720 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000721 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000722
723 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000724 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000725
726 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000727 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000728
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000729 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000730 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000731
Devang Patel193f7202009-11-24 01:14:22 +0000732 // Emit info into a debug pubtypes section.
733 emitDebugPubTypes();
734
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000735 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000736 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000737
738 // Emit info into a debug aranges section.
739 EmitDebugARanges();
740
741 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000742 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000743
744 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000745 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000746
747 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000748 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000749
Chris Lattnerbc733f52010-03-13 02:17:42 +0000750 // Emit info into a debug str section.
751 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000752
Devang Patele9a1cca2010-08-02 17:32:15 +0000753 // clean up.
754 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000755 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000756 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
757 E = CUMap.end(); I != E; ++I)
758 delete I->second;
759 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000760}
761
Devang Patel53bb5c92009-11-10 23:06:00 +0000762/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000763DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000764 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000765 LLVMContext &Ctx = DV->getContext();
766 // More then one inlined variable corresponds to one abstract variable.
767 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000768 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000769 if (AbsDbgVariable)
770 return AbsDbgVariable;
771
Devang Patelbf47fdb2011-08-10 20:55:27 +0000772 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000773 if (!Scope)
774 return NULL;
775
Devang Patel5a1a67c2011-08-15 19:01:20 +0000776 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000777 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000778 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000779 return AbsDbgVariable;
780}
781
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000782/// addCurrentFnArgument - If Var is a current function argument then add
783/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000784bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000785 DbgVariable *Var, LexicalScope *Scope) {
786 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000787 return false;
788 DIVariable DV = Var->getVariable();
789 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
790 return false;
791 unsigned ArgNo = DV.getArgNumber();
792 if (ArgNo == 0)
793 return false;
794
Devang Patelcb3a6572011-03-03 20:02:02 +0000795 size_t Size = CurrentFnArguments.size();
796 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000797 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000798 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000799 // arguments does the function have at source level.
800 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000801 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000802 CurrentFnArguments[ArgNo - 1] = Var;
803 return true;
804}
805
Devang Patelee432862010-05-20 19:57:06 +0000806/// collectVariableInfoFromMMITable - Collect variable information from
807/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000808void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000809DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000810 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000811 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
812 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
813 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000814 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000815 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000816 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000817 DIVariable DV(Var);
818 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000819
Devang Patelbf47fdb2011-08-10 20:55:27 +0000820 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000821
Devang Patelfb0ee432009-11-10 23:20:04 +0000822 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000823 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000824 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000825
Devang Patel26c1e562010-05-20 16:36:41 +0000826 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000827 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000828 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000829 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000830 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000831 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000832 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000833 }
Devang Patelee432862010-05-20 19:57:06 +0000834}
Devang Patel90a48ad2010-03-15 18:33:46 +0000835
Jim Grosbach1e20b962010-07-21 21:21:52 +0000836/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000837/// DBG_VALUE instruction, is in a defined reg.
838static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000839 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000840 return MI->getNumOperands() == 3 &&
841 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
842 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000843}
844
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000845/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000846/// at MI.
847static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
848 const MCSymbol *FLabel,
849 const MCSymbol *SLabel,
850 const MachineInstr *MI) {
851 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
852
853 if (MI->getNumOperands() != 3) {
854 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
855 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
856 }
857 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
858 MachineLocation MLoc;
859 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
860 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
861 }
862 if (MI->getOperand(0).isImm())
863 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
864 if (MI->getOperand(0).isFPImm())
865 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
866 if (MI->getOperand(0).isCImm())
867 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
868
Nick Lewycky746cb672011-10-26 22:55:33 +0000869 assert(0 && "Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +0000870 return DotDebugLocEntry();
871}
872
Devang Patelbf47fdb2011-08-10 20:55:27 +0000873/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000874void
Devang Patel78e127d2010-06-25 22:07:34 +0000875DwarfDebug::collectVariableInfo(const MachineFunction *MF,
876 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000877
Devang Patelee432862010-05-20 19:57:06 +0000878 /// collection info from MMI table.
879 collectVariableInfoFromMMITable(MF, Processed);
880
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000881 for (SmallVectorImpl<const MDNode*>::const_iterator
882 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
883 ++UVI) {
884 const MDNode *Var = *UVI;
885 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +0000886 continue;
887
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000888 // History contains relevant DBG_VALUE instructions for Var and instructions
889 // clobbering it.
890 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
891 if (History.empty())
892 continue;
893 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +0000894
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000895 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000896 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +0000897 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
898 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000899 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +0000900 else {
901 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +0000902 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +0000903 else {
904 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +0000905 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +0000906 else
Devang Patelbf47fdb2011-08-10 20:55:27 +0000907 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +0000908 }
909 }
Devang Patelee432862010-05-20 19:57:06 +0000910 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +0000911 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +0000912 continue;
913
914 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000915 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +0000916 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
917 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +0000918 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000919 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000920 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000921 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000922
923 // Simple ranges that are fully coalesced.
924 if (History.size() <= 1 || (History.size() == 2 &&
925 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +0000926 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +0000927 continue;
928 }
929
930 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000931 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000932
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000933 for (SmallVectorImpl<const MachineInstr*>::const_iterator
934 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
935 const MachineInstr *Begin = *HI;
936 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +0000937
Devang Patel4ada1d72011-06-01 23:00:17 +0000938 // Check if DBG_VALUE is truncating a range.
939 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
940 && !Begin->getOperand(0).getReg())
941 continue;
942
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000943 // Compute the range for a register location.
944 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
945 const MCSymbol *SLabel = 0;
946
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000947 if (HI + 1 == HE)
948 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000949 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000950 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000951 else {
952 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +0000953 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
954 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000955 if (End->isDebugValue())
956 SLabel = getLabelBeforeInsn(End);
957 else {
958 // End is a normal instruction clobbering the range.
959 SLabel = getLabelAfterInsn(End);
960 assert(SLabel && "Forgot label after clobber instruction");
961 ++HI;
962 }
963 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000964
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000965 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +0000966 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +0000967 }
968 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +0000969 }
Devang Patel98e1cac2010-05-14 21:01:35 +0000970
971 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +0000972 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
973 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
974 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
975 DIVariable DV(Variables.getElement(i));
976 if (!DV || !DV.Verify() || !Processed.insert(DV))
977 continue;
978 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
979 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +0000980 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000981}
Devang Patel98e1cac2010-05-14 21:01:35 +0000982
Devang Patelc3f5f782010-05-25 23:40:22 +0000983/// getLabelBeforeInsn - Return Label preceding the instruction.
984const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000985 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
986 assert(Label && "Didn't insert label before instruction");
987 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +0000988}
989
990/// getLabelAfterInsn - Return Label immediately following the instruction.
991const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000992 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +0000993}
994
Devang Patelcbbe2872010-10-26 17:49:02 +0000995/// beginInstruction - Process beginning of an instruction.
996void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000997 // Check if source location changes, but ignore DBG_VALUE locations.
998 if (!MI->isDebugValue()) {
999 DebugLoc DL = MI->getDebugLoc();
1000 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +00001001 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001002 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001003 if (DL == PrologEndLoc) {
1004 Flags |= DWARF2_FLAG_PROLOGUE_END;
1005 PrologEndLoc = DebugLoc();
1006 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001007 if (!DL.isUnknown()) {
1008 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001009 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001010 } else
Devang Patel4243e672011-05-11 19:22:19 +00001011 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001012 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001013 }
Devang Patelaead63c2010-03-29 22:59:58 +00001014
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001015 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001016 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1017 LabelsBeforeInsn.find(MI);
1018
1019 // No label needed.
1020 if (I == LabelsBeforeInsn.end())
1021 return;
1022
1023 // Label already assigned.
1024 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001025 return;
Devang Patel553881b2010-03-29 17:20:31 +00001026
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001027 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001028 PrevLabel = MMI->getContext().CreateTempSymbol();
1029 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001030 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001031 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001032}
1033
Devang Patelcbbe2872010-10-26 17:49:02 +00001034/// endInstruction - Process end of an instruction.
1035void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001036 // Don't create a new label after DBG_VALUE instructions.
1037 // They don't generate code.
1038 if (!MI->isDebugValue())
1039 PrevLabel = 0;
1040
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001041 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1042 LabelsAfterInsn.find(MI);
1043
1044 // No label needed.
1045 if (I == LabelsAfterInsn.end())
1046 return;
1047
1048 // Label already assigned.
1049 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001050 return;
1051
1052 // We need a label after this instruction.
1053 if (!PrevLabel) {
1054 PrevLabel = MMI->getContext().CreateTempSymbol();
1055 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001056 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001057 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001058}
1059
Jim Grosbach1e20b962010-07-21 21:21:52 +00001060/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001061/// Each LexicalScope has first instruction and last instruction to mark
1062/// beginning and end of a scope respectively. Create an inverse map that list
1063/// scopes starts (and ends) with an instruction. One instruction may start (or
1064/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001065void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001066 SmallVector<LexicalScope *, 4> WorkList;
1067 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001068 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001069 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001070
Devang Patelbf47fdb2011-08-10 20:55:27 +00001071 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001072 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001073 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001074 SE = Children.end(); SI != SE; ++SI)
1075 WorkList.push_back(*SI);
1076
Devang Patel53bb5c92009-11-10 23:06:00 +00001077 if (S->isAbstractScope())
1078 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001079
Devang Patelbf47fdb2011-08-10 20:55:27 +00001080 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001081 if (Ranges.empty())
1082 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001083 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001084 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001085 assert(RI->first && "InsnRange does not have first instruction!");
1086 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001087 requestLabelBeforeInsn(RI->first);
1088 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001089 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001090 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001091}
1092
Devang Patela3f48672011-05-09 22:14:49 +00001093/// getScopeNode - Get MDNode for DebugLoc's scope.
1094static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1095 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1096 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1097 return DL.getScope(Ctx);
1098}
1099
Devang Patel4243e672011-05-11 19:22:19 +00001100/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1101/// line number info for the function.
1102static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1103 const MDNode *Scope = getScopeNode(DL, Ctx);
1104 DISubprogram SP = getDISubprogram(Scope);
1105 if (SP.Verify())
1106 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1107 return DebugLoc();
1108}
1109
Devang Patel2c4ceb12009-11-21 02:48:08 +00001110/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001111/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001112void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001113 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001114 LScopes.initialize(*MF);
1115 if (LScopes.empty()) return;
1116 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001117
Devang Pateleac9c072010-04-27 19:46:33 +00001118 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1119 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001120 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001121 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001122
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001123 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1124
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001125 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001126 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1127 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1128
Devang Patelb2b31a62010-05-26 19:37:24 +00001129 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001130 I != E; ++I) {
1131 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001132 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1133 II != IE; ++II) {
1134 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001135
Devang Patelb2b31a62010-05-26 19:37:24 +00001136 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001137 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001138
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001139 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001140 const MDNode *Var =
1141 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001142
1143 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001144 if (isDbgValueInDefinedReg(MI))
1145 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1146
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001147 // Check the history of this variable.
1148 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1149 if (History.empty()) {
1150 UserVariables.push_back(Var);
1151 // The first mention of a function argument gets the FunctionBeginSym
1152 // label, so arguments are visible when breaking at function entry.
1153 DIVariable DV(Var);
1154 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1155 DISubprogram(getDISubprogram(DV.getContext()))
1156 .describes(MF->getFunction()))
1157 LabelsBeforeInsn[MI] = FunctionBeginSym;
1158 } else {
1159 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1160 const MachineInstr *Prev = History.back();
1161 if (Prev->isDebugValue()) {
1162 // Coalesce identical entries at the end of History.
1163 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001164 Prev->isIdenticalTo(History[History.size() - 2])) {
1165 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1166 << "\t" << *Prev
1167 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001168 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001169 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001170
1171 // Terminate old register assignments that don't reach MI;
1172 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1173 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1174 isDbgValueInDefinedReg(Prev)) {
1175 // Previous register assignment needs to terminate at the end of
1176 // its basic block.
1177 MachineBasicBlock::const_iterator LastMI =
1178 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001179 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001180 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001181 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1182 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001183 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001184 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001185 else {
1186 // Terminate after LastMI.
1187 History.push_back(LastMI);
1188 }
1189 }
1190 }
1191 }
1192 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001193 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001194 // Not a DBG_VALUE instruction.
1195 if (!MI->isLabel())
1196 AtBlockEntry = false;
1197
Devang Patel4243e672011-05-11 19:22:19 +00001198 // First known non DBG_VALUE location marks beginning of function
1199 // body.
1200 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1201 PrologEndLoc = MI->getDebugLoc();
1202
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001203 // Check if the instruction clobbers any registers with debug vars.
1204 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1205 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1206 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1207 continue;
1208 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1209 unsigned Reg = *AI; ++AI) {
1210 const MDNode *Var = LiveUserVar[Reg];
1211 if (!Var)
1212 continue;
1213 // Reg is now clobbered.
1214 LiveUserVar[Reg] = 0;
1215
1216 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001217 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1218 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001219 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001220 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1221 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001222 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001223 const MachineInstr *Prev = History.back();
1224 // Sanity-check: Register assignments are terminated at the end of
1225 // their block.
1226 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1227 continue;
1228 // Is the variable still in Reg?
1229 if (!isDbgValueInDefinedReg(Prev) ||
1230 Prev->getOperand(0).getReg() != Reg)
1231 continue;
1232 // Var is clobbered. Make sure the next instruction gets a label.
1233 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001234 }
1235 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001236 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001237 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001238 }
1239
1240 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1241 I != E; ++I) {
1242 SmallVectorImpl<const MachineInstr*> &History = I->second;
1243 if (History.empty())
1244 continue;
1245
1246 // Make sure the final register assignments are terminated.
1247 const MachineInstr *Prev = History.back();
1248 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1249 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001250 MachineBasicBlock::const_iterator LastMI =
1251 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001252 if (LastMI == PrevMBB->end())
1253 // Drop DBG_VALUE for empty range.
1254 History.pop_back();
1255 else {
1256 // Terminate after LastMI.
1257 History.push_back(LastMI);
1258 }
1259 }
1260 // Request labels for the full history.
1261 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1262 const MachineInstr *MI = History[i];
1263 if (MI->isDebugValue())
1264 requestLabelBeforeInsn(MI);
1265 else
1266 requestLabelAfterInsn(MI);
1267 }
1268 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001269
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001270 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001271 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001272
1273 // Record beginning of function.
1274 if (!PrologEndLoc.isUnknown()) {
1275 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1276 MF->getFunction()->getContext());
1277 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1278 FnStartDL.getScope(MF->getFunction()->getContext()),
1279 DWARF2_FLAG_IS_STMT);
1280 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001281}
1282
Devang Patelbf47fdb2011-08-10 20:55:27 +00001283void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1284// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1285 ScopeVariables[LS].push_back(Var);
1286// Vars.push_back(Var);
1287}
1288
Devang Patel2c4ceb12009-11-21 02:48:08 +00001289/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001290///
Chris Lattnereec791a2010-01-26 23:18:02 +00001291void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001292 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001293
Devang Patelbf47fdb2011-08-10 20:55:27 +00001294 // Define end label for subprogram.
1295 FunctionEndSym = Asm->GetTempSymbol("func_end",
1296 Asm->getFunctionNumber());
1297 // Assumes in correct section after the entry point.
1298 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1299
1300 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1301 collectVariableInfo(MF, ProcessedVars);
1302
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001303 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001304 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001305 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001306
Devang Patelbf47fdb2011-08-10 20:55:27 +00001307 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001308 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1309 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1310 LexicalScope *AScope = AList[i];
1311 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001312 if (SP.Verify()) {
1313 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001314 DIArray Variables = SP.getVariables();
1315 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1316 DIVariable DV(Variables.getElement(i));
1317 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1318 continue;
1319 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1320 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001321 }
1322 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001323 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001324 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001325 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001326
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001327 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001328
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001329 if (!DisableFramePointerElim(*MF))
Devang Patel5bc942c2011-08-10 23:58:09 +00001330 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1331 dwarf::DW_FORM_flag, 1);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001332
Devang Patelbf47fdb2011-08-10 20:55:27 +00001333 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1334 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001335
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001336 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001337 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1338 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1339 DeleteContainerPointers(I->second);
1340 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001341 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001342 UserVariables.clear();
1343 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001344 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001345 LabelsBeforeInsn.clear();
1346 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001347 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001348}
1349
Chris Lattnerc6087842010-03-09 04:54:43 +00001350/// recordSourceLine - Register a source line with debug info. Returns the
1351/// unique label that was emitted and which provides correspondence to
1352/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001353void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1354 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001355 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001356 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001357 unsigned Src = 1;
1358 if (S) {
1359 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001360
Dan Gohman1cc0d622010-05-05 23:41:32 +00001361 if (Scope.isCompileUnit()) {
1362 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001363 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001364 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001365 } else if (Scope.isFile()) {
1366 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001367 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001368 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001369 } else if (Scope.isSubprogram()) {
1370 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001371 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001372 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001373 } else if (Scope.isLexicalBlockFile()) {
1374 DILexicalBlockFile DBF(S);
1375 Fn = DBF.getFilename();
1376 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001377 } else if (Scope.isLexicalBlock()) {
1378 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001379 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001380 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001381 } else
1382 assert(0 && "Unexpected scope info");
1383
Devang Patel23670e52011-03-24 20:30:50 +00001384 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001385 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001386 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001387}
1388
Bill Wendling829e67b2009-05-20 23:22:40 +00001389//===----------------------------------------------------------------------===//
1390// Emit Methods
1391//===----------------------------------------------------------------------===//
1392
Devang Patel2c4ceb12009-11-21 02:48:08 +00001393/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001394///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001395unsigned
1396DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001397 // Get the children.
1398 const std::vector<DIE *> &Children = Die->getChildren();
1399
1400 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001401 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001402 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001403
1404 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001405 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001406
1407 // Get the abbreviation for this DIE.
1408 unsigned AbbrevNumber = Die->getAbbrevNumber();
1409 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1410
1411 // Set DIE offset
1412 Die->setOffset(Offset);
1413
1414 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001415 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001416
1417 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1418 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1419
1420 // Size the DIE attribute values.
1421 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1422 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001423 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001424
1425 // Size the DIE children if any.
1426 if (!Children.empty()) {
1427 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1428 "Children flag not set");
1429
1430 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001431 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001432
1433 // End of children marker.
1434 Offset += sizeof(int8_t);
1435 }
1436
1437 Die->setSize(Offset - Die->getOffset());
1438 return Offset;
1439}
1440
Devang Patel2c4ceb12009-11-21 02:48:08 +00001441/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001442///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001443void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001444 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1445 E = CUMap.end(); I != E; ++I) {
1446 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001447 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001448 sizeof(int32_t) + // Length of Compilation Unit Info
1449 sizeof(int16_t) + // DWARF version number
1450 sizeof(int32_t) + // Offset Into Abbrev. Section
1451 sizeof(int8_t); // Pointer Size (in bytes)
1452 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001453 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001454}
1455
Chris Lattner11b8f302010-04-04 23:02:02 +00001456/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1457/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00001458static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00001459 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00001460 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00001461 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001462
Chris Lattner9c69e285532010-04-04 22:59:04 +00001463 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1464 Asm->OutStreamer.EmitLabel(TmpSym);
1465 return TmpSym;
1466}
1467
1468/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1469/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001470void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001471 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001472
Bill Wendling94d04b82009-05-20 23:21:38 +00001473 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001474 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001475 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001476 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001477 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001478 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001479
Chris Lattner9c69e285532010-04-04 22:59:04 +00001480 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001481 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001482
Devang Patelaf608bd2010-08-24 00:06:12 +00001483 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001484 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1485 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1486 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001487 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001488 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001489 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1490 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001491
Devang Patelc3f5f782010-05-25 23:40:22 +00001492 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1493 "section_debug_loc");
1494
Chris Lattner9c69e285532010-04-04 22:59:04 +00001495 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001496 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001497}
1498
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001499/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001500///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001501void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001502 // Get the abbreviation for this DIE.
1503 unsigned AbbrevNumber = Die->getAbbrevNumber();
1504 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1505
Bill Wendling94d04b82009-05-20 23:21:38 +00001506 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001507 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001508 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1509 Twine::utohexstr(Die->getOffset()) + ":0x" +
1510 Twine::utohexstr(Die->getSize()) + " " +
1511 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001512 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001513
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001514 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001515 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1516
1517 // Emit the DIE attribute values.
1518 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1519 unsigned Attr = AbbrevData[i].getAttribute();
1520 unsigned Form = AbbrevData[i].getForm();
1521 assert(Form && "Too many attributes for DIE (check abbreviation)");
1522
Chris Lattner3f53c832010-04-04 18:52:31 +00001523 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001524 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001525
Bill Wendling94d04b82009-05-20 23:21:38 +00001526 switch (Attr) {
1527 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001528 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001529 break;
1530 case dwarf::DW_AT_abstract_origin: {
1531 DIEEntry *E = cast<DIEEntry>(Values[i]);
1532 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001533 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001534 Asm->EmitInt32(Addr);
1535 break;
1536 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001537 case dwarf::DW_AT_ranges: {
1538 // DW_AT_range Value encodes offset in debug_range section.
1539 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001540
1541 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1542 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1543 V->getValue(),
1544 4);
1545 } else {
1546 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1547 V->getValue(),
1548 DwarfDebugRangeSectionSym,
1549 4);
1550 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001551 break;
1552 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001553 case dwarf::DW_AT_location: {
Devang Patel7a328272011-08-15 21:43:21 +00001554 if (DIELabel *L = dyn_cast<DIELabel>(Values[i]))
Daniel Dunbar83320a02011-03-16 22:16:39 +00001555 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel7a328272011-08-15 21:43:21 +00001556 else
Devang Patelc3f5f782010-05-25 23:40:22 +00001557 Values[i]->EmitValue(Asm, Form);
1558 break;
1559 }
Devang Patel2a361602010-09-29 19:08:08 +00001560 case dwarf::DW_AT_accessibility: {
1561 if (Asm->isVerbose()) {
1562 DIEInteger *V = cast<DIEInteger>(Values[i]);
1563 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1564 }
1565 Values[i]->EmitValue(Asm, Form);
1566 break;
1567 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001568 default:
1569 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001570 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001571 break;
1572 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001573 }
1574
1575 // Emit the DIE children if any.
1576 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1577 const std::vector<DIE *> &Children = Die->getChildren();
1578
1579 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001580 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001581
Chris Lattner3f53c832010-04-04 18:52:31 +00001582 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001583 Asm->OutStreamer.AddComment("End Of Children Mark");
1584 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001585 }
1586}
1587
Devang Patel8a241142009-12-09 18:24:21 +00001588/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001589///
Devang Patel8a241142009-12-09 18:24:21 +00001590void DwarfDebug::emitDebugInfo() {
1591 // Start debug info section.
1592 Asm->OutStreamer.SwitchSection(
1593 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001594 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1595 E = CUMap.end(); I != E; ++I) {
1596 CompileUnit *TheCU = I->second;
1597 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001598
Devang Patel163a9f72010-05-10 22:49:55 +00001599 // Emit the compile units header.
1600 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1601 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001602
Devang Patel163a9f72010-05-10 22:49:55 +00001603 // Emit size of content not including length itself
1604 unsigned ContentSize = Die->getSize() +
1605 sizeof(int16_t) + // DWARF version number
1606 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001607 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001608
Devang Patel163a9f72010-05-10 22:49:55 +00001609 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1610 Asm->EmitInt32(ContentSize);
1611 Asm->OutStreamer.AddComment("DWARF version number");
1612 Asm->EmitInt16(dwarf::DWARF_VERSION);
1613 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1614 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1615 DwarfAbbrevSectionSym);
1616 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1617 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001618
Devang Patel163a9f72010-05-10 22:49:55 +00001619 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001620 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1621 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001622}
1623
Devang Patel2c4ceb12009-11-21 02:48:08 +00001624/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001625///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001626void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001627 // Check to see if it is worth the effort.
1628 if (!Abbreviations.empty()) {
1629 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001630 Asm->OutStreamer.SwitchSection(
1631 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001632
Chris Lattnerc0215722010-04-04 19:25:43 +00001633 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001634
1635 // For each abbrevation.
1636 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1637 // Get abbreviation data
1638 const DIEAbbrev *Abbrev = Abbreviations[i];
1639
1640 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001641 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001642
1643 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001644 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001645 }
1646
1647 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001648 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001649
Chris Lattnerc0215722010-04-04 19:25:43 +00001650 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001651 }
1652}
1653
Devang Patel2c4ceb12009-11-21 02:48:08 +00001654/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001655/// the line matrix.
1656///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001657void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001658 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001659 Asm->OutStreamer.AddComment("Extended Op");
1660 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001661
Chris Lattner233f52b2010-03-09 23:52:58 +00001662 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001663 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001664 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1665 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1666
1667 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001668
Chris Lattnerc0215722010-04-04 19:25:43 +00001669 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001670 Asm->getTargetData().getPointerSize(),
1671 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001672
1673 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001674 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1675 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001676 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001677 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001678}
1679
Devang Patel8a241142009-12-09 18:24:21 +00001680/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1681///
1682void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00001683 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1684 E = CUMap.end(); I != E; ++I) {
1685 CompileUnit *TheCU = I->second;
1686 // Start the dwarf pubnames section.
1687 Asm->OutStreamer.SwitchSection(
1688 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001689
Devang Patel163a9f72010-05-10 22:49:55 +00001690 Asm->OutStreamer.AddComment("Length of Public Names Info");
1691 Asm->EmitLabelDifference(
1692 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1693 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001694
Devang Patel163a9f72010-05-10 22:49:55 +00001695 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1696 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001697
Devang Patel163a9f72010-05-10 22:49:55 +00001698 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001699 Asm->EmitInt16(dwarf::DWARF_VERSION);
1700
Devang Patel163a9f72010-05-10 22:49:55 +00001701 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001702 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00001703 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001704
Devang Patel163a9f72010-05-10 22:49:55 +00001705 Asm->OutStreamer.AddComment("Compilation Unit Length");
1706 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1707 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1708 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001709
Devang Patel163a9f72010-05-10 22:49:55 +00001710 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1711 for (StringMap<DIE*>::const_iterator
1712 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1713 const char *Name = GI->getKeyData();
1714 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001715
Devang Patel163a9f72010-05-10 22:49:55 +00001716 Asm->OutStreamer.AddComment("DIE offset");
1717 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001718
Devang Patel163a9f72010-05-10 22:49:55 +00001719 if (Asm->isVerbose())
1720 Asm->OutStreamer.AddComment("External Name");
1721 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1722 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001723
Devang Patel163a9f72010-05-10 22:49:55 +00001724 Asm->OutStreamer.AddComment("End Mark");
1725 Asm->EmitInt32(0);
1726 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001727 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00001728 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001729}
1730
Devang Patel193f7202009-11-24 01:14:22 +00001731void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001732 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1733 E = CUMap.end(); I != E; ++I) {
1734 CompileUnit *TheCU = I->second;
1735 // Start the dwarf pubnames section.
1736 Asm->OutStreamer.SwitchSection(
1737 Asm->getObjFileLowering().getDwarfPubTypesSection());
1738 Asm->OutStreamer.AddComment("Length of Public Types Info");
1739 Asm->EmitLabelDifference(
1740 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1741 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001742
Devang Patel163a9f72010-05-10 22:49:55 +00001743 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1744 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001745
Devang Patel163a9f72010-05-10 22:49:55 +00001746 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1747 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001748
Devang Patel163a9f72010-05-10 22:49:55 +00001749 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1750 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1751 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001752
Devang Patel163a9f72010-05-10 22:49:55 +00001753 Asm->OutStreamer.AddComment("Compilation Unit Length");
1754 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1755 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1756 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001757
Devang Patel163a9f72010-05-10 22:49:55 +00001758 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1759 for (StringMap<DIE*>::const_iterator
1760 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1761 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001762 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001763
Devang Patel163a9f72010-05-10 22:49:55 +00001764 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1765 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001766
Devang Patel163a9f72010-05-10 22:49:55 +00001767 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1768 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1769 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001770
Devang Patel163a9f72010-05-10 22:49:55 +00001771 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001772 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001773 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1774 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001775 }
Devang Patel193f7202009-11-24 01:14:22 +00001776}
1777
Devang Patel2c4ceb12009-11-21 02:48:08 +00001778/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001779///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001780void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001781 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001782 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001783
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001784 // Start the dwarf str section.
1785 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001786 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001787
Chris Lattnerbc733f52010-03-13 02:17:42 +00001788 // Get all of the string pool entries and put them in an array by their ID so
1789 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001790 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001791 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001792
Chris Lattnerbc733f52010-03-13 02:17:42 +00001793 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1794 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1795 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001796
Chris Lattnerbc733f52010-03-13 02:17:42 +00001797 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001798
Chris Lattnerbc733f52010-03-13 02:17:42 +00001799 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001800 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001801 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001802
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001803 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001804 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Nick Lewycky390c40d2011-10-27 06:44:11 +00001805 Asm->OutStreamer.EmitZeros(1, 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001806 }
1807}
1808
Devang Patel2c4ceb12009-11-21 02:48:08 +00001809/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001810///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001811void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001812 if (DotDebugLocEntries.empty())
1813 return;
1814
Devang Patel6c3ea902011-02-04 22:57:18 +00001815 for (SmallVector<DotDebugLocEntry, 4>::iterator
1816 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1817 I != E; ++I) {
1818 DotDebugLocEntry &Entry = *I;
1819 if (I + 1 != DotDebugLocEntries.end())
1820 Entry.Merge(I+1);
1821 }
1822
Daniel Dunbar83320a02011-03-16 22:16:39 +00001823 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001824 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00001825 Asm->getObjFileLowering().getDwarfLocSection());
1826 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00001827 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1828 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001829 for (SmallVector<DotDebugLocEntry, 4>::iterator
1830 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00001831 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00001832 DotDebugLocEntry &Entry = *I;
1833 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00001834 if (Entry.isEmpty()) {
1835 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1836 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00001837 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00001838 } else {
1839 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1840 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00001841 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001842 Asm->OutStreamer.AddComment("Loc expr size");
1843 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1844 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1845 Asm->EmitLabelDifference(end, begin, 2);
1846 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00001847 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00001848 DIBasicType BTy(DV.getType());
1849 if (BTy.Verify() &&
1850 (BTy.getEncoding() == dwarf::DW_ATE_signed
1851 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1852 Asm->OutStreamer.AddComment("DW_OP_consts");
1853 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00001854 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001855 } else {
1856 Asm->OutStreamer.AddComment("DW_OP_constu");
1857 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00001858 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001859 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001860 } else if (Entry.isLocation()) {
1861 if (!DV.hasComplexAddress())
1862 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00001863 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00001864 else {
1865 // Complex address entry.
1866 unsigned N = DV.getNumAddrElements();
1867 unsigned i = 0;
1868 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
1869 if (Entry.Loc.getOffset()) {
1870 i = 2;
1871 Asm->EmitDwarfRegOp(Entry.Loc);
1872 Asm->OutStreamer.AddComment("DW_OP_deref");
1873 Asm->EmitInt8(dwarf::DW_OP_deref);
1874 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
1875 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1876 Asm->EmitSLEB128(DV.getAddrElement(1));
1877 } else {
1878 // If first address element is OpPlus then emit
1879 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
1880 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
1881 Asm->EmitDwarfRegOp(Loc);
1882 i = 2;
1883 }
1884 } else {
1885 Asm->EmitDwarfRegOp(Entry.Loc);
1886 }
1887
1888 // Emit remaining complex address elements.
1889 for (; i < N; ++i) {
1890 uint64_t Element = DV.getAddrElement(i);
1891 if (Element == DIBuilder::OpPlus) {
1892 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1893 Asm->EmitULEB128(DV.getAddrElement(++i));
1894 } else if (Element == DIBuilder::OpDeref)
1895 Asm->EmitInt8(dwarf::DW_OP_deref);
1896 else llvm_unreachable("unknown Opcode found in complex address");
1897 }
Devang Patelc26f5442011-04-28 02:22:40 +00001898 }
Devang Patelc26f5442011-04-28 02:22:40 +00001899 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001900 // else ... ignore constant fp. There is not any good way to
1901 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001902 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00001903 }
1904 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001905}
1906
1907/// EmitDebugARanges - Emit visible names into a debug aranges section.
1908///
1909void DwarfDebug::EmitDebugARanges() {
1910 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001911 Asm->OutStreamer.SwitchSection(
1912 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001913}
1914
Devang Patel2c4ceb12009-11-21 02:48:08 +00001915/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001916///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001917void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001918 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001919 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00001920 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00001921 unsigned char Size = Asm->getTargetData().getPointerSize();
1922 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00001923 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00001924 I != E; ++I) {
1925 if (*I)
1926 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001927 else
Devang Pateleac9c072010-04-27 19:46:33 +00001928 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001929 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001930}
1931
Devang Patel2c4ceb12009-11-21 02:48:08 +00001932/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001933///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001934void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001935 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00001936 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001937 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001938 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001939 }
1940}
1941
Devang Patel2c4ceb12009-11-21 02:48:08 +00001942/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00001943/// Section Header:
1944/// 1. length of section
1945/// 2. Dwarf version number
1946/// 3. address size.
1947///
1948/// Entries (one "entry" for each function that was inlined):
1949///
1950/// 1. offset into __debug_str section for MIPS linkage name, if exists;
1951/// otherwise offset into __debug_str for regular function name.
1952/// 2. offset into __debug_str section for regular function name.
1953/// 3. an unsigned LEB128 number indicating the number of distinct inlining
1954/// instances for the function.
1955///
1956/// The rest of the entry consists of a {die_offset, low_pc} pair for each
1957/// inlined instance; the die_offset points to the inlined_subroutine die in the
1958/// __debug_info section, and the low_pc is the starting address for the
1959/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001960void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001961 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00001962 return;
1963
Devang Patel163a9f72010-05-10 22:49:55 +00001964 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00001965 return;
1966
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001967 Asm->OutStreamer.SwitchSection(
1968 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00001969
Chris Lattner233f52b2010-03-09 23:52:58 +00001970 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00001971 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
1972 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00001973
Chris Lattnerc0215722010-04-04 19:25:43 +00001974 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00001975
Chris Lattner233f52b2010-03-09 23:52:58 +00001976 Asm->OutStreamer.AddComment("Dwarf Version");
1977 Asm->EmitInt16(dwarf::DWARF_VERSION);
1978 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001979 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00001980
Devang Patele9f8f5e2010-05-07 20:54:48 +00001981 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00001982 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001983
Devang Patele9f8f5e2010-05-07 20:54:48 +00001984 const MDNode *Node = *I;
1985 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001986 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00001987 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00001988 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00001989 StringRef LName = SP.getLinkageName();
1990 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00001991
Chris Lattner233f52b2010-03-09 23:52:58 +00001992 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00001993 if (LName.empty()) {
1994 Asm->OutStreamer.EmitBytes(Name, 0);
1995 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001996 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00001997 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
1998 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00001999
Chris Lattner233f52b2010-03-09 23:52:58 +00002000 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002001 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002002 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002003
Devang Patel53bb5c92009-11-10 23:06:00 +00002004 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002005 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002006 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002007 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002008
Chris Lattner3f53c832010-04-04 18:52:31 +00002009 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002010 Asm->OutStreamer.EmitSymbolValue(LI->first,
2011 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002012 }
2013 }
2014
Chris Lattnerc0215722010-04-04 19:25:43 +00002015 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002016}