blob: 0a284fefaa7cc71bd8faf0befad64cca803aec86 [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
Chris Lattnerbc733f52010-03-13 02:17:42 +0000139MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
140 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
141 if (Entry.first) return Entry.first;
142
143 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000144 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000145}
146
147
Devang Patel2c4ceb12009-11-21 02:48:08 +0000148/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000149///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000150void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000151 // Profile the node so that we can make it unique.
152 FoldingSetNodeID ID;
153 Abbrev.Profile(ID);
154
155 // Check the set for priors.
156 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
157
158 // If it's newly added.
159 if (InSet == &Abbrev) {
160 // Add to abbreviation list.
161 Abbreviations.push_back(&Abbrev);
162
163 // Assign the vector position + 1 as its number.
164 Abbrev.setNumber(Abbreviations.size());
165 } else {
166 // Assign existing abbreviation number.
167 Abbrev.setNumber(InSet->getNumber());
168 }
169}
170
Jim Grosbach1e20b962010-07-21 21:21:52 +0000171/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000172/// printer to not emit usual symbol prefix before the symbol name is used then
173/// return linkage name after skipping this special LLVM prefix.
174static StringRef getRealLinkageName(StringRef LinkageName) {
175 char One = '\1';
176 if (LinkageName.startswith(StringRef(&One, 1)))
177 return LinkageName.substr(1);
178 return LinkageName;
179}
180
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000181/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000182/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
183/// If there are global variables in this scope then create and insert
184/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000185DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
186 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000187 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000188
Chris Lattnerd38fee82010-04-05 00:13:49 +0000189 assert(SPDie && "Unable to find subprogram DIE!");
190 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000191
Devang Patel5e06bb82011-04-22 23:10:17 +0000192 DISubprogram SPDecl = SP.getFunctionDeclaration();
193 if (SPDecl.isSubprogram())
194 // Refer function declaration directly.
Devang Patel3cbee302011-04-12 22:53:02 +0000195 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Pateldbc64af2011-08-15 17:24:54 +0000196 SPCU->getOrCreateSubprogramDIE(SPDecl));
Devang Patel5e06bb82011-04-22 23:10:17 +0000197 else {
198 // There is not any need to generate specification DIE for a function
199 // defined at compile unit level. If a function is defined inside another
200 // function then gdb prefers the definition at top level and but does not
201 // expect specification DIE in parent function. So avoid creating
202 // specification DIE for a function defined inside a function.
203 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
204 !SP.getContext().isFile() &&
205 !isSubprogramContext(SP.getContext())) {
206 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
207
208 // Add arguments.
209 DICompositeType SPTy = SP.getType();
210 DIArray Args = SPTy.getTypeArray();
211 unsigned SPTag = SPTy.getTag();
212 if (SPTag == dwarf::DW_TAG_subroutine_type)
213 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
214 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
215 DIType ATy = DIType(DIType(Args.getElement(i)));
216 SPCU->addType(Arg, ATy);
217 if (ATy.isArtificial())
218 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
219 SPDie->addChild(Arg);
220 }
221 DIE *SPDeclDie = SPDie;
222 SPDie = new DIE(dwarf::DW_TAG_subprogram);
223 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
224 SPDeclDie);
225 SPCU->addDie(SPDie);
226 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000227 }
Devang Patel8aa61472010-07-07 22:20:57 +0000228 // Pick up abstract subprogram DIE.
229 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
230 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000231 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
232 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000233 SPCU->addDie(SPDie);
234 }
235
Devang Patel3cbee302011-04-12 22:53:02 +0000236 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
237 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
238 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
239 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000240 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
241 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000242 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000243
Chris Lattnerd38fee82010-04-05 00:13:49 +0000244 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000245}
246
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000247/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000248/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000249DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
250 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000251
252 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
253 if (Scope->isAbstractScope())
254 return ScopeDIE;
255
Devang Patelbf47fdb2011-08-10 20:55:27 +0000256 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000257 if (Ranges.empty())
258 return 0;
259
Devang Patelbf47fdb2011-08-10 20:55:27 +0000260 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000261 if (Ranges.size() > 1) {
262 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000263 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000264 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000265 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000266 DebugRangeSymbols.size()
267 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000268 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000269 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000270 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
271 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000272 }
273 DebugRangeSymbols.push_back(NULL);
274 DebugRangeSymbols.push_back(NULL);
275 return ScopeDIE;
276 }
277
Devang Patelc3f5f782010-05-25 23:40:22 +0000278 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
279 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000280
Devang Patelc3f5f782010-05-25 23:40:22 +0000281 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000282
Chris Lattnerb7db7332010-03-09 01:58:53 +0000283 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
284 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000285
Devang Patel3cbee302011-04-12 22:53:02 +0000286 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
287 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000288
289 return ScopeDIE;
290}
291
Devang Patel2c4ceb12009-11-21 02:48:08 +0000292/// constructInlinedScopeDIE - This scope represents inlined body of
293/// a function. Construct DIE to represent this concrete inlined copy
294/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000295DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
296 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000297
Devang Patelbf47fdb2011-08-10 20:55:27 +0000298 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000299 assert (Ranges.empty() == false
Devang Patelbf47fdb2011-08-10 20:55:27 +0000300 && "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000301
Devang Patel26a92002011-07-27 00:34:13 +0000302 if (!Scope->getScopeNode())
303 return NULL;
304 DIScope DS(Scope->getScopeNode());
305 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000306 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
307 if (!OriginDIE) {
308 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
309 return NULL;
310 }
311
Devang Patelbf47fdb2011-08-10 20:55:27 +0000312 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000313 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
314 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000315
Devang Patel0afbf232010-07-08 22:39:20 +0000316 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000317 assert (0 && "Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000318 return 0;
319 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000320 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000321 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000322 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000323 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000324
Devang Pateld96efb82011-05-05 17:54:26 +0000325 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000326 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
327 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000328
Devang Patel26a92002011-07-27 00:34:13 +0000329 if (Ranges.size() > 1) {
330 // .debug_range section has not been laid out yet. Emit offset in
331 // .debug_range as a uint, size 4, for now. emitDIE will handle
332 // DW_AT_ranges appropriately.
333 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000334 DebugRangeSymbols.size()
335 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000336 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000337 RE = Ranges.end(); RI != RE; ++RI) {
338 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
339 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
340 }
341 DebugRangeSymbols.push_back(NULL);
342 DebugRangeSymbols.push_back(NULL);
343 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000344 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
345 StartLabel);
346 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
347 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000348 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000349
350 InlinedSubprogramDIEs.insert(OriginDIE);
351
352 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000353 //.debug_inlined section specification does not clearly state how
354 // to emit inlined scope that is split into multiple instruction ranges.
355 // For now, use first instruction range and emit low_pc/high_pc pair and
356 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000357 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000358 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000359
360 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000361 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000362 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000363 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000364 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000365 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000366
Devang Patel53bb5c92009-11-10 23:06:00 +0000367 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000368 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
369 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000370
371 return ScopeDIE;
372}
373
Devang Patel2c4ceb12009-11-21 02:48:08 +0000374/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000375DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000376 if (!Scope || !Scope->getScopeNode())
377 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000378
Devang Patel5bc9fec2011-02-19 01:31:27 +0000379 SmallVector <DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000380
381 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000382 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000383 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
384 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000385 if (DIE *Arg =
386 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000387 Children.push_back(Arg);
388
Devang Patel5bc9fec2011-02-19 01:31:27 +0000389 // Collect lexical scope childrens first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000390 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000391 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000392 if (DIE *Variable =
393 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000394 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000395 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000396 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000397 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000398 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000399 DIScope DS(Scope->getScopeNode());
400 DIE *ScopeDIE = NULL;
401 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000402 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000403 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000404 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000405 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000406 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000407 // Note down abstract DIE.
408 if (ScopeDIE)
409 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
410 }
Devang Patel3c91b052010-03-08 20:52:55 +0000411 else
Devang Pateld3024342011-08-15 22:24:32 +0000412 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000413 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000414 else {
415 // There is no need to emit empty lexical block DIE.
416 if (Children.empty())
417 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000418 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000419 }
420
Devang Patelaead63c2010-03-29 22:59:58 +0000421 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000422
Devang Patel5bc9fec2011-02-19 01:31:27 +0000423 // Add children
424 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
425 E = Children.end(); I != E; ++I)
426 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000427
Jim Grosbach1e20b962010-07-21 21:21:52 +0000428 if (DS.isSubprogram())
Devang Pateld3024342011-08-15 22:24:32 +0000429 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000430
Devang Patel193f7202009-11-24 01:14:22 +0000431 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000432}
433
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000434/// GetOrCreateSourceID - Look up the source id with the given directory and
435/// source file names. If none currently exists, create a new id and insert it
436/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
437/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +0000438
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
445 // MCStream expects full path name as filename.
Benjamin Kramerab5f7882011-06-05 14:36:47 +0000446 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
447 SmallString<128> FullPathName = DirName;
448 sys::path::append(FullPathName, FileName);
Devang Patel23670e52011-03-24 20:30:50 +0000449 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
450 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
451 }
Devang Patel1905a182010-09-16 20:57:49 +0000452
Rafael Espindola5c055632010-11-18 02:04:25 +0000453 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
454 if (Entry.getValue())
455 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000456
Rafael Espindola5c055632010-11-18 02:04:25 +0000457 unsigned SrcId = SourceIdMap.size();
458 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000459
Rafael Espindola5c055632010-11-18 02:04:25 +0000460 // Print out a .file directive to specify files for .loc directives.
Devang Patel23670e52011-03-24 20:30:50 +0000461 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000462
463 return SrcId;
464}
465
Jim Grosbach1e20b962010-07-21 21:21:52 +0000466/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000467/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000468CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000469 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000470 StringRef FN = DIUnit.getFilename();
471 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000472 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000473
474 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000475 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
476 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
477 DIUnit.getProducer());
478 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
479 DIUnit.getLanguage());
480 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000481 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
482 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000483 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000484 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000485 // compile unit in debug_line section.
Rafael Espindoladc52ecf2011-05-10 20:35:05 +0000486 if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000487 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000488 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000489 else
Devang Patel3cbee302011-04-12 22:53:02 +0000490 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000491
Devang Patel65dbc902009-11-25 17:36:49 +0000492 if (!Dir.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000493 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000494 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000495 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000496
Devang Patel65dbc902009-11-25 17:36:49 +0000497 StringRef Flags = DIUnit.getFlags();
498 if (!Flags.empty())
Devang Patel5bc942c2011-08-10 23:58:09 +0000499 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
500 Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000501
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000502 unsigned RVer = DIUnit.getRunTimeVersion();
503 if (RVer)
Devang Patel3cbee302011-04-12 22:53:02 +0000504 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000505 dwarf::DW_FORM_data1, RVer);
506
Devang Patel163a9f72010-05-10 22:49:55 +0000507 if (!FirstCU)
508 FirstCU = NewCU;
509 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000510 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000511}
512
Devang Patel163a9f72010-05-10 22:49:55 +0000513/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000514void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
515 const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000516 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000517 if (!SP.isDefinition())
518 // This is a method declaration which will be handled while constructing
519 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000520 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000521
Devang Pateldbc64af2011-08-15 17:24:54 +0000522 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000523
524 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000525 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000526
527 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000528 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000529
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000530 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +0000531 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +0000532
Devang Patel94c7ddb2011-08-16 22:09:43 +0000533 SPMap[N] = TheCU;
Devang Patel13e16b62009-06-26 01:49:18 +0000534 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000535}
536
Devang Patel02e603f2011-08-15 23:47:24 +0000537/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
538/// as llvm.dbg.enum and llvm.dbg.ty
539void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000540 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
541 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
542 const MDNode *N = NMD->getOperand(i);
543 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
544 constructSubprogramDIE(CU, N);
545 }
546
547 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
548 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
549 const MDNode *N = NMD->getOperand(i);
550 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000551 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000552 }
553
Devang Patel02e603f2011-08-15 23:47:24 +0000554 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
555 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
556 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000557 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
558 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000559 }
560
561 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
562 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
563 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000564 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
565 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000566 }
567}
568
569/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
570/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
571bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
572 DebugInfoFinder DbgFinder;
573 DbgFinder.processModule(*M);
574
575 bool HasDebugInfo = false;
576 // Scan all the compile-units to see if there are any marked as the main
577 // unit. If not, we do not generate debug info.
578 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
579 E = DbgFinder.compile_unit_end(); I != E; ++I) {
580 if (DICompileUnit(*I).isMain()) {
581 HasDebugInfo = true;
582 break;
583 }
584 }
585 if (!HasDebugInfo) return false;
586
587 // Create all the compile unit DIEs.
588 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
589 E = DbgFinder.compile_unit_end(); I != E; ++I)
590 constructCompileUnit(*I);
591
592 // Create DIEs for each global variable.
593 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
594 E = DbgFinder.global_variable_end(); I != E; ++I) {
595 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000596 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000597 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000598 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000599
Devang Patel02e603f2011-08-15 23:47:24 +0000600 // Create DIEs for each subprogram.
601 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
602 E = DbgFinder.subprogram_end(); I != E; ++I) {
603 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000604 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
605 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000606 }
607
608 return HasDebugInfo;
609}
610
Devang Patel2c4ceb12009-11-21 02:48:08 +0000611/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000612/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000613/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000614void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000615 if (DisableDebugInfoPrinting)
616 return;
617
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000618 // If module has named metadata anchors then use them, otherwise scan the
619 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000620 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
621 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000622 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
623 DICompileUnit CUNode(CU_Nodes->getOperand(i));
624 CompileUnit *CU = constructCompileUnit(CUNode);
625 DIArray GVs = CUNode.getGlobalVariables();
626 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000627 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000628 DIArray SPs = CUNode.getSubprograms();
629 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
630 constructSubprogramDIE(CU, SPs.getElement(i));
631 DIArray EnumTypes = CUNode.getEnumTypes();
632 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
633 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
634 DIArray RetainedTypes = CUNode.getRetainedTypes();
635 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
636 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
637 }
Devang Patel02e603f2011-08-15 23:47:24 +0000638 } else if (!collectLegacyDebugInfo(M))
639 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000640
Devang Patel02e603f2011-08-15 23:47:24 +0000641 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000642
Chris Lattnerd850ac72010-04-05 02:19:28 +0000643 // Tell MMI that we have debug info.
644 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000645
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000646 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000647 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000648
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000649 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000650 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000651}
652
Devang Patel2c4ceb12009-11-21 02:48:08 +0000653/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000654///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000655void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000656 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000657 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000658 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000659
Devang Patel94c7ddb2011-08-16 22:09:43 +0000660 // Collect info for variables that were optimized out.
661 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
662 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
663 DICompileUnit TheCU(CU_Nodes->getOperand(i));
664 DIArray Subprograms = TheCU.getSubprograms();
665 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
666 DISubprogram SP(Subprograms.getElement(i));
667 if (ProcessedSPNodes.count(SP) != 0) continue;
668 if (!SP.Verify()) continue;
669 if (!SP.isDefinition()) continue;
670 StringRef FName = SP.getLinkageName();
671 if (FName.empty())
672 FName = SP.getName();
673 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
674 if (!NMD) continue;
675 unsigned E = NMD->getNumOperands();
676 if (!E) continue;
677 LexicalScope *Scope =
678 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
679 DeadFnScopeMap[SP] = Scope;
680
681 // Construct subprogram DIE and add variables DIEs.
682 SmallVector<DbgVariable, 8> Variables;
683 for (unsigned I = 0; I != E; ++I) {
684 DIVariable DV(NMD->getOperand(I));
685 if (!DV.Verify()) continue;
686 Variables.push_back(DbgVariable(DV, NULL));
687 }
688 CompileUnit *SPCU = CUMap.lookup(TheCU);
689 assert (SPCU && "Unable to find Compile Unit!");
690 constructSubprogramDIE(SPCU, SP);
691 DIE *ScopeDIE = SPCU->getDIE(SP);
692 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
693 if (DIE *VariableDIE =
694 SPCU->constructVariableDIE(&Variables[i], Scope->isAbstractScope()))
695 ScopeDIE->addChild(VariableDIE);
696 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000697 }
698 }
699 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000700
Devang Patel53bb5c92009-11-10 23:06:00 +0000701 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
702 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
703 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
704 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000705 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000706 }
707
Devang Pateldbc64af2011-08-15 17:24:54 +0000708 // Emit DW_AT_containing_type attribute to connect types with their
709 // vtable holding type.
710 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
711 CUE = CUMap.end(); CUI != CUE; ++CUI) {
712 CompileUnit *TheCU = CUI->second;
713 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000714 }
715
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000716 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000717 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000718 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000719 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000720 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000721
722 // End text sections.
723 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000724 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000725 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000726 }
727
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000728 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000729 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000730
731 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000732 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000733
734 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000735 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000736
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000737 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000738 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000739
Devang Patel193f7202009-11-24 01:14:22 +0000740 // Emit info into a debug pubtypes section.
741 emitDebugPubTypes();
742
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000743 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000744 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000745
746 // Emit info into a debug aranges section.
747 EmitDebugARanges();
748
749 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000750 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000751
752 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000753 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000754
755 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000756 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000757
Chris Lattnerbc733f52010-03-13 02:17:42 +0000758 // Emit info into a debug str section.
759 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000760
Devang Patele9a1cca2010-08-02 17:32:15 +0000761 // clean up.
762 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000763 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000764 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
765 E = CUMap.end(); I != E; ++I)
766 delete I->second;
767 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000768}
769
Devang Patel53bb5c92009-11-10 23:06:00 +0000770/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000771DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000772 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000773 LLVMContext &Ctx = DV->getContext();
774 // More then one inlined variable corresponds to one abstract variable.
775 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000776 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000777 if (AbsDbgVariable)
778 return AbsDbgVariable;
779
Devang Patelbf47fdb2011-08-10 20:55:27 +0000780 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000781 if (!Scope)
782 return NULL;
783
Devang Patel5a1a67c2011-08-15 19:01:20 +0000784 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000785 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000786 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000787 return AbsDbgVariable;
788}
789
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000790/// addCurrentFnArgument - If Var is a current function argument then add
791/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000792bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000793 DbgVariable *Var, LexicalScope *Scope) {
794 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000795 return false;
796 DIVariable DV = Var->getVariable();
797 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
798 return false;
799 unsigned ArgNo = DV.getArgNumber();
800 if (ArgNo == 0)
801 return false;
802
Devang Patelcb3a6572011-03-03 20:02:02 +0000803 size_t Size = CurrentFnArguments.size();
804 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000805 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000806 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000807 // arguments does the function have at source level.
808 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000809 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000810 CurrentFnArguments[ArgNo - 1] = Var;
811 return true;
812}
813
Devang Patelee432862010-05-20 19:57:06 +0000814/// collectVariableInfoFromMMITable - Collect variable information from
815/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000816void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000817DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000818 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000819 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
820 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
821 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000822 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000823 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000824 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000825 DIVariable DV(Var);
826 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000827
Devang Patelbf47fdb2011-08-10 20:55:27 +0000828 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000829
Devang Patelfb0ee432009-11-10 23:20:04 +0000830 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000831 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000832 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000833
Devang Patel26c1e562010-05-20 16:36:41 +0000834 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000835 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000836 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000837 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000838 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000839 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000840 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000841 }
Devang Patelee432862010-05-20 19:57:06 +0000842}
Devang Patel90a48ad2010-03-15 18:33:46 +0000843
Jim Grosbach1e20b962010-07-21 21:21:52 +0000844/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000845/// DBG_VALUE instruction, is in a defined reg.
846static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
847 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000848 return MI->getNumOperands() == 3 &&
849 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
850 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000851}
852
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000853/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000854/// at MI.
855static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
856 const MCSymbol *FLabel,
857 const MCSymbol *SLabel,
858 const MachineInstr *MI) {
859 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
860
861 if (MI->getNumOperands() != 3) {
862 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
863 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
864 }
865 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
866 MachineLocation MLoc;
867 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
868 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
869 }
870 if (MI->getOperand(0).isImm())
871 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
872 if (MI->getOperand(0).isFPImm())
873 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
874 if (MI->getOperand(0).isCImm())
875 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
876
877 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
878 return DotDebugLocEntry();
879}
880
Devang Patelbf47fdb2011-08-10 20:55:27 +0000881/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000882void
Devang Patel78e127d2010-06-25 22:07:34 +0000883DwarfDebug::collectVariableInfo(const MachineFunction *MF,
884 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000885
Devang Patelee432862010-05-20 19:57:06 +0000886 /// collection info from MMI table.
887 collectVariableInfoFromMMITable(MF, Processed);
888
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000889 for (SmallVectorImpl<const MDNode*>::const_iterator
890 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
891 ++UVI) {
892 const MDNode *Var = *UVI;
893 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +0000894 continue;
895
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000896 // History contains relevant DBG_VALUE instructions for Var and instructions
897 // clobbering it.
898 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
899 if (History.empty())
900 continue;
901 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +0000902
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000903 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000904 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +0000905 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
906 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000907 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +0000908 else {
909 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +0000910 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +0000911 else {
912 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +0000913 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +0000914 else
Devang Patelbf47fdb2011-08-10 20:55:27 +0000915 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +0000916 }
917 }
Devang Patelee432862010-05-20 19:57:06 +0000918 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +0000919 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +0000920 continue;
921
922 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000923 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +0000924 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
925 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +0000926 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000927 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000928 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000929 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000930
931 // Simple ranges that are fully coalesced.
932 if (History.size() <= 1 || (History.size() == 2 &&
933 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +0000934 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +0000935 continue;
936 }
937
938 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000939 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000940
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000941 for (SmallVectorImpl<const MachineInstr*>::const_iterator
942 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
943 const MachineInstr *Begin = *HI;
944 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +0000945
Devang Patel4ada1d72011-06-01 23:00:17 +0000946 // Check if DBG_VALUE is truncating a range.
947 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
948 && !Begin->getOperand(0).getReg())
949 continue;
950
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000951 // Compute the range for a register location.
952 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
953 const MCSymbol *SLabel = 0;
954
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000955 if (HI + 1 == HE)
956 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000957 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000958 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000959 else {
960 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +0000961 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
962 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000963 if (End->isDebugValue())
964 SLabel = getLabelBeforeInsn(End);
965 else {
966 // End is a normal instruction clobbering the range.
967 SLabel = getLabelAfterInsn(End);
968 assert(SLabel && "Forgot label after clobber instruction");
969 ++HI;
970 }
971 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000972
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000973 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +0000974 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +0000975 }
976 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +0000977 }
Devang Patel98e1cac2010-05-14 21:01:35 +0000978
979 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +0000980 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +0000981 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +0000982 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +0000983 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +0000984 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +0000985 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +0000986 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Devang Patel5a1a67c2011-08-15 19:01:20 +0000987 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +0000988 }
989 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000990}
Devang Patel98e1cac2010-05-14 21:01:35 +0000991
Devang Patelc3f5f782010-05-25 23:40:22 +0000992/// getLabelBeforeInsn - Return Label preceding the instruction.
993const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000994 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
995 assert(Label && "Didn't insert label before instruction");
996 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +0000997}
998
999/// getLabelAfterInsn - Return Label immediately following the instruction.
1000const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001001 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001002}
1003
Devang Patelcbbe2872010-10-26 17:49:02 +00001004/// beginInstruction - Process beginning of an instruction.
1005void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001006 // Check if source location changes, but ignore DBG_VALUE locations.
1007 if (!MI->isDebugValue()) {
1008 DebugLoc DL = MI->getDebugLoc();
1009 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +00001010 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001011 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001012 if (DL == PrologEndLoc) {
1013 Flags |= DWARF2_FLAG_PROLOGUE_END;
1014 PrologEndLoc = DebugLoc();
1015 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001016 if (!DL.isUnknown()) {
1017 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001018 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001019 } else
Devang Patel4243e672011-05-11 19:22:19 +00001020 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001021 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001022 }
Devang Patelaead63c2010-03-29 22:59:58 +00001023
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001024 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001025 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1026 LabelsBeforeInsn.find(MI);
1027
1028 // No label needed.
1029 if (I == LabelsBeforeInsn.end())
1030 return;
1031
1032 // Label already assigned.
1033 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001034 return;
Devang Patel553881b2010-03-29 17:20:31 +00001035
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001036 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001037 PrevLabel = MMI->getContext().CreateTempSymbol();
1038 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001039 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001040 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001041}
1042
Devang Patelcbbe2872010-10-26 17:49:02 +00001043/// endInstruction - Process end of an instruction.
1044void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001045 // Don't create a new label after DBG_VALUE instructions.
1046 // They don't generate code.
1047 if (!MI->isDebugValue())
1048 PrevLabel = 0;
1049
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001050 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1051 LabelsAfterInsn.find(MI);
1052
1053 // No label needed.
1054 if (I == LabelsAfterInsn.end())
1055 return;
1056
1057 // Label already assigned.
1058 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001059 return;
1060
1061 // We need a label after this instruction.
1062 if (!PrevLabel) {
1063 PrevLabel = MMI->getContext().CreateTempSymbol();
1064 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001065 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001066 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001067}
1068
Jim Grosbach1e20b962010-07-21 21:21:52 +00001069/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001070/// Each LexicalScope has first instruction and last instruction to mark
1071/// beginning and end of a scope respectively. Create an inverse map that list
1072/// scopes starts (and ends) with an instruction. One instruction may start (or
1073/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001074void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001075 SmallVector<LexicalScope *, 4> WorkList;
1076 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001077 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001078 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001079
Devang Patelbf47fdb2011-08-10 20:55:27 +00001080 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001081 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001082 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001083 SE = Children.end(); SI != SE; ++SI)
1084 WorkList.push_back(*SI);
1085
Devang Patel53bb5c92009-11-10 23:06:00 +00001086 if (S->isAbstractScope())
1087 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001088
Devang Patelbf47fdb2011-08-10 20:55:27 +00001089 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001090 if (Ranges.empty())
1091 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001092 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001093 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001094 assert(RI->first && "InsnRange does not have first instruction!");
1095 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001096 requestLabelBeforeInsn(RI->first);
1097 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001098 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001099 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001100}
1101
Devang Patela3f48672011-05-09 22:14:49 +00001102/// getScopeNode - Get MDNode for DebugLoc's scope.
1103static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1104 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1105 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1106 return DL.getScope(Ctx);
1107}
1108
Devang Patel4243e672011-05-11 19:22:19 +00001109/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1110/// line number info for the function.
1111static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1112 const MDNode *Scope = getScopeNode(DL, Ctx);
1113 DISubprogram SP = getDISubprogram(Scope);
1114 if (SP.Verify())
1115 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1116 return DebugLoc();
1117}
1118
Devang Patel2c4ceb12009-11-21 02:48:08 +00001119/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001120/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001121void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001122 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001123 LScopes.initialize(*MF);
1124 if (LScopes.empty()) return;
1125 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001126
Devang Pateleac9c072010-04-27 19:46:33 +00001127 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1128 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001129 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001130 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001131
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001132 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1133
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001134 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001135 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1136 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1137
Devang Patelb2b31a62010-05-26 19:37:24 +00001138 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001139 I != E; ++I) {
1140 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001141 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1142 II != IE; ++II) {
1143 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001144
Devang Patelb2b31a62010-05-26 19:37:24 +00001145 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00001146 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001147
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001148 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001149 const MDNode *Var =
1150 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001151
1152 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001153 if (isDbgValueInDefinedReg(MI))
1154 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1155
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001156 // Check the history of this variable.
1157 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1158 if (History.empty()) {
1159 UserVariables.push_back(Var);
1160 // The first mention of a function argument gets the FunctionBeginSym
1161 // label, so arguments are visible when breaking at function entry.
1162 DIVariable DV(Var);
1163 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1164 DISubprogram(getDISubprogram(DV.getContext()))
1165 .describes(MF->getFunction()))
1166 LabelsBeforeInsn[MI] = FunctionBeginSym;
1167 } else {
1168 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1169 const MachineInstr *Prev = History.back();
1170 if (Prev->isDebugValue()) {
1171 // Coalesce identical entries at the end of History.
1172 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001173 Prev->isIdenticalTo(History[History.size() - 2])) {
1174 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1175 << "\t" << *Prev
1176 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001177 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001178 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001179
1180 // Terminate old register assignments that don't reach MI;
1181 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1182 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1183 isDbgValueInDefinedReg(Prev)) {
1184 // Previous register assignment needs to terminate at the end of
1185 // its basic block.
1186 MachineBasicBlock::const_iterator LastMI =
1187 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001188 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001189 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001190 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1191 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001192 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001193 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001194 else {
1195 // Terminate after LastMI.
1196 History.push_back(LastMI);
1197 }
1198 }
1199 }
1200 }
1201 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001202 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001203 // Not a DBG_VALUE instruction.
1204 if (!MI->isLabel())
1205 AtBlockEntry = false;
1206
Devang Patel4243e672011-05-11 19:22:19 +00001207 // First known non DBG_VALUE location marks beginning of function
1208 // body.
1209 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1210 PrologEndLoc = MI->getDebugLoc();
1211
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001212 // Check if the instruction clobbers any registers with debug vars.
1213 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1214 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1215 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1216 continue;
1217 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1218 unsigned Reg = *AI; ++AI) {
1219 const MDNode *Var = LiveUserVar[Reg];
1220 if (!Var)
1221 continue;
1222 // Reg is now clobbered.
1223 LiveUserVar[Reg] = 0;
1224
1225 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001226 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1227 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001228 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001229 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1230 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001231 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001232 const MachineInstr *Prev = History.back();
1233 // Sanity-check: Register assignments are terminated at the end of
1234 // their block.
1235 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1236 continue;
1237 // Is the variable still in Reg?
1238 if (!isDbgValueInDefinedReg(Prev) ||
1239 Prev->getOperand(0).getReg() != Reg)
1240 continue;
1241 // Var is clobbered. Make sure the next instruction gets a label.
1242 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001243 }
1244 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001245 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001246 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001247 }
1248
1249 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1250 I != E; ++I) {
1251 SmallVectorImpl<const MachineInstr*> &History = I->second;
1252 if (History.empty())
1253 continue;
1254
1255 // Make sure the final register assignments are terminated.
1256 const MachineInstr *Prev = History.back();
1257 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1258 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001259 MachineBasicBlock::const_iterator LastMI =
1260 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001261 if (LastMI == PrevMBB->end())
1262 // Drop DBG_VALUE for empty range.
1263 History.pop_back();
1264 else {
1265 // Terminate after LastMI.
1266 History.push_back(LastMI);
1267 }
1268 }
1269 // Request labels for the full history.
1270 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1271 const MachineInstr *MI = History[i];
1272 if (MI->isDebugValue())
1273 requestLabelBeforeInsn(MI);
1274 else
1275 requestLabelAfterInsn(MI);
1276 }
1277 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001278
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001279 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001280 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001281
1282 // Record beginning of function.
1283 if (!PrologEndLoc.isUnknown()) {
1284 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1285 MF->getFunction()->getContext());
1286 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1287 FnStartDL.getScope(MF->getFunction()->getContext()),
1288 DWARF2_FLAG_IS_STMT);
1289 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001290}
1291
Devang Patelbf47fdb2011-08-10 20:55:27 +00001292void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1293// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1294 ScopeVariables[LS].push_back(Var);
1295// Vars.push_back(Var);
1296}
1297
Devang Patel2c4ceb12009-11-21 02:48:08 +00001298/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001299///
Chris Lattnereec791a2010-01-26 23:18:02 +00001300void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001301 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001302
Devang Patelbf47fdb2011-08-10 20:55:27 +00001303 // Define end label for subprogram.
1304 FunctionEndSym = Asm->GetTempSymbol("func_end",
1305 Asm->getFunctionNumber());
1306 // Assumes in correct section after the entry point.
1307 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1308
1309 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1310 collectVariableInfo(MF, ProcessedVars);
1311
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001312 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001313 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1314 assert (TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001315
Devang Patelbf47fdb2011-08-10 20:55:27 +00001316 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001317 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1318 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1319 LexicalScope *AScope = AList[i];
1320 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001321 if (SP.Verify()) {
1322 // Collect info for variables that were optimized out.
1323 StringRef FName = SP.getLinkageName();
1324 if (FName.empty())
1325 FName = SP.getName();
1326 if (NamedMDNode *NMD =
1327 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
1328 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001329 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00001330 if (!DV || !ProcessedVars.insert(DV))
1331 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001332 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Devang Patel5a1a67c2011-08-15 19:01:20 +00001333 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001334 }
1335 }
1336 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001337 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001338 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001339 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001340
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001341 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001342
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001343 if (!DisableFramePointerElim(*MF))
Devang Patel5bc942c2011-08-10 23:58:09 +00001344 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1345 dwarf::DW_FORM_flag, 1);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001346
Devang Patelbf47fdb2011-08-10 20:55:27 +00001347 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1348 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001349
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001350 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001351 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1352 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1353 DeleteContainerPointers(I->second);
1354 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001355 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001356 UserVariables.clear();
1357 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001358 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001359 LabelsBeforeInsn.clear();
1360 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001361 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001362}
1363
Chris Lattnerc6087842010-03-09 04:54:43 +00001364/// recordSourceLine - Register a source line with debug info. Returns the
1365/// unique label that was emitted and which provides correspondence to
1366/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001367void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1368 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001369 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001370 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001371 unsigned Src = 1;
1372 if (S) {
1373 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001374
Dan Gohman1cc0d622010-05-05 23:41:32 +00001375 if (Scope.isCompileUnit()) {
1376 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001377 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001378 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001379 } else if (Scope.isFile()) {
1380 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001381 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001382 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001383 } else if (Scope.isSubprogram()) {
1384 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001385 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001386 Dir = SP.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001387 } else if (Scope.isLexicalBlock()) {
1388 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001389 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001390 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001391 } else
1392 assert(0 && "Unexpected scope info");
1393
Devang Patel23670e52011-03-24 20:30:50 +00001394 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001395 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001396 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001397}
1398
Bill Wendling829e67b2009-05-20 23:22:40 +00001399//===----------------------------------------------------------------------===//
1400// Emit Methods
1401//===----------------------------------------------------------------------===//
1402
Devang Patel2c4ceb12009-11-21 02:48:08 +00001403/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001404///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001405unsigned
1406DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001407 // Get the children.
1408 const std::vector<DIE *> &Children = Die->getChildren();
1409
1410 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001411 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001412 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001413
1414 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001415 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001416
1417 // Get the abbreviation for this DIE.
1418 unsigned AbbrevNumber = Die->getAbbrevNumber();
1419 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1420
1421 // Set DIE offset
1422 Die->setOffset(Offset);
1423
1424 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001425 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001426
1427 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1428 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1429
1430 // Size the DIE attribute values.
1431 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1432 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001433 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001434
1435 // Size the DIE children if any.
1436 if (!Children.empty()) {
1437 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1438 "Children flag not set");
1439
1440 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001441 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001442
1443 // End of children marker.
1444 Offset += sizeof(int8_t);
1445 }
1446
1447 Die->setSize(Offset - Die->getOffset());
1448 return Offset;
1449}
1450
Devang Patel2c4ceb12009-11-21 02:48:08 +00001451/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001452///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001453void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001454 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1455 E = CUMap.end(); I != E; ++I) {
1456 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001457 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001458 sizeof(int32_t) + // Length of Compilation Unit Info
1459 sizeof(int16_t) + // DWARF version number
1460 sizeof(int32_t) + // Offset Into Abbrev. Section
1461 sizeof(int8_t); // Pointer Size (in bytes)
1462 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001463 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001464}
1465
Chris Lattner11b8f302010-04-04 23:02:02 +00001466/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1467/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00001468static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00001469 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00001470 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00001471 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001472
Chris Lattner9c69e285532010-04-04 22:59:04 +00001473 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1474 Asm->OutStreamer.EmitLabel(TmpSym);
1475 return TmpSym;
1476}
1477
1478/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1479/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001480void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001481 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001482
Bill Wendling94d04b82009-05-20 23:21:38 +00001483 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001484 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001485 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001486 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001487 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001488 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001489
Chris Lattner9c69e285532010-04-04 22:59:04 +00001490 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001491 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001492
Devang Patelaf608bd2010-08-24 00:06:12 +00001493 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001494 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1495 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1496 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001497 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001498 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001499 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1500 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001501
Devang Patelc3f5f782010-05-25 23:40:22 +00001502 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1503 "section_debug_loc");
1504
Chris Lattner9c69e285532010-04-04 22:59:04 +00001505 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001506 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001507}
1508
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001509/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001510///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001511void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001512 // Get the abbreviation for this DIE.
1513 unsigned AbbrevNumber = Die->getAbbrevNumber();
1514 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1515
Bill Wendling94d04b82009-05-20 23:21:38 +00001516 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001517 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001518 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1519 Twine::utohexstr(Die->getOffset()) + ":0x" +
1520 Twine::utohexstr(Die->getSize()) + " " +
1521 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001522 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001523
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001524 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001525 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1526
1527 // Emit the DIE attribute values.
1528 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1529 unsigned Attr = AbbrevData[i].getAttribute();
1530 unsigned Form = AbbrevData[i].getForm();
1531 assert(Form && "Too many attributes for DIE (check abbreviation)");
1532
Chris Lattner3f53c832010-04-04 18:52:31 +00001533 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001534 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001535
Bill Wendling94d04b82009-05-20 23:21:38 +00001536 switch (Attr) {
1537 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001538 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001539 break;
1540 case dwarf::DW_AT_abstract_origin: {
1541 DIEEntry *E = cast<DIEEntry>(Values[i]);
1542 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001543 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001544 Asm->EmitInt32(Addr);
1545 break;
1546 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001547 case dwarf::DW_AT_ranges: {
1548 // DW_AT_range Value encodes offset in debug_range section.
1549 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001550
1551 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1552 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1553 V->getValue(),
1554 4);
1555 } else {
1556 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1557 V->getValue(),
1558 DwarfDebugRangeSectionSym,
1559 4);
1560 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001561 break;
1562 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001563 case dwarf::DW_AT_location: {
Devang Patel7a328272011-08-15 21:43:21 +00001564 if (DIELabel *L = dyn_cast<DIELabel>(Values[i]))
Daniel Dunbar83320a02011-03-16 22:16:39 +00001565 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel7a328272011-08-15 21:43:21 +00001566 else
Devang Patelc3f5f782010-05-25 23:40:22 +00001567 Values[i]->EmitValue(Asm, Form);
1568 break;
1569 }
Devang Patel2a361602010-09-29 19:08:08 +00001570 case dwarf::DW_AT_accessibility: {
1571 if (Asm->isVerbose()) {
1572 DIEInteger *V = cast<DIEInteger>(Values[i]);
1573 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1574 }
1575 Values[i]->EmitValue(Asm, Form);
1576 break;
1577 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001578 default:
1579 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001580 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001581 break;
1582 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001583 }
1584
1585 // Emit the DIE children if any.
1586 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1587 const std::vector<DIE *> &Children = Die->getChildren();
1588
1589 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001590 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001591
Chris Lattner3f53c832010-04-04 18:52:31 +00001592 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001593 Asm->OutStreamer.AddComment("End Of Children Mark");
1594 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001595 }
1596}
1597
Devang Patel8a241142009-12-09 18:24:21 +00001598/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001599///
Devang Patel8a241142009-12-09 18:24:21 +00001600void DwarfDebug::emitDebugInfo() {
1601 // Start debug info section.
1602 Asm->OutStreamer.SwitchSection(
1603 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001604 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1605 E = CUMap.end(); I != E; ++I) {
1606 CompileUnit *TheCU = I->second;
1607 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001608
Devang Patel163a9f72010-05-10 22:49:55 +00001609 // Emit the compile units header.
1610 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1611 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001612
Devang Patel163a9f72010-05-10 22:49:55 +00001613 // Emit size of content not including length itself
1614 unsigned ContentSize = Die->getSize() +
1615 sizeof(int16_t) + // DWARF version number
1616 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001617 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001618
Devang Patel163a9f72010-05-10 22:49:55 +00001619 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1620 Asm->EmitInt32(ContentSize);
1621 Asm->OutStreamer.AddComment("DWARF version number");
1622 Asm->EmitInt16(dwarf::DWARF_VERSION);
1623 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1624 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1625 DwarfAbbrevSectionSym);
1626 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1627 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001628
Devang Patel163a9f72010-05-10 22:49:55 +00001629 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001630 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1631 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001632}
1633
Devang Patel2c4ceb12009-11-21 02:48:08 +00001634/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001635///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001636void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001637 // Check to see if it is worth the effort.
1638 if (!Abbreviations.empty()) {
1639 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001640 Asm->OutStreamer.SwitchSection(
1641 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001642
Chris Lattnerc0215722010-04-04 19:25:43 +00001643 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001644
1645 // For each abbrevation.
1646 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1647 // Get abbreviation data
1648 const DIEAbbrev *Abbrev = Abbreviations[i];
1649
1650 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001651 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001652
1653 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001654 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001655 }
1656
1657 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001658 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001659
Chris Lattnerc0215722010-04-04 19:25:43 +00001660 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001661 }
1662}
1663
Devang Patel2c4ceb12009-11-21 02:48:08 +00001664/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001665/// the line matrix.
1666///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001667void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001668 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001669 Asm->OutStreamer.AddComment("Extended Op");
1670 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001671
Chris Lattner233f52b2010-03-09 23:52:58 +00001672 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001673 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001674 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1675 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1676
1677 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001678
Chris Lattnerc0215722010-04-04 19:25:43 +00001679 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001680 Asm->getTargetData().getPointerSize(),
1681 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001682
1683 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001684 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1685 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001686 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001687 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001688}
1689
Devang Patel8a241142009-12-09 18:24:21 +00001690/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1691///
1692void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00001693 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1694 E = CUMap.end(); I != E; ++I) {
1695 CompileUnit *TheCU = I->second;
1696 // Start the dwarf pubnames section.
1697 Asm->OutStreamer.SwitchSection(
1698 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001699
Devang Patel163a9f72010-05-10 22:49:55 +00001700 Asm->OutStreamer.AddComment("Length of Public Names Info");
1701 Asm->EmitLabelDifference(
1702 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1703 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001704
Devang Patel163a9f72010-05-10 22:49:55 +00001705 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1706 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001707
Devang Patel163a9f72010-05-10 22:49:55 +00001708 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001709 Asm->EmitInt16(dwarf::DWARF_VERSION);
1710
Devang Patel163a9f72010-05-10 22:49:55 +00001711 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001712 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00001713 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001714
Devang Patel163a9f72010-05-10 22:49:55 +00001715 Asm->OutStreamer.AddComment("Compilation Unit Length");
1716 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1717 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1718 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001719
Devang Patel163a9f72010-05-10 22:49:55 +00001720 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1721 for (StringMap<DIE*>::const_iterator
1722 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1723 const char *Name = GI->getKeyData();
1724 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001725
Devang Patel163a9f72010-05-10 22:49:55 +00001726 Asm->OutStreamer.AddComment("DIE offset");
1727 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001728
Devang Patel163a9f72010-05-10 22:49:55 +00001729 if (Asm->isVerbose())
1730 Asm->OutStreamer.AddComment("External Name");
1731 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1732 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001733
Devang Patel163a9f72010-05-10 22:49:55 +00001734 Asm->OutStreamer.AddComment("End Mark");
1735 Asm->EmitInt32(0);
1736 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001737 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00001738 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001739}
1740
Devang Patel193f7202009-11-24 01:14:22 +00001741void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001742 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1743 E = CUMap.end(); I != E; ++I) {
1744 CompileUnit *TheCU = I->second;
1745 // Start the dwarf pubnames section.
1746 Asm->OutStreamer.SwitchSection(
1747 Asm->getObjFileLowering().getDwarfPubTypesSection());
1748 Asm->OutStreamer.AddComment("Length of Public Types Info");
1749 Asm->EmitLabelDifference(
1750 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1751 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001752
Devang Patel163a9f72010-05-10 22:49:55 +00001753 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1754 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001755
Devang Patel163a9f72010-05-10 22:49:55 +00001756 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1757 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001758
Devang Patel163a9f72010-05-10 22:49:55 +00001759 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1760 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1761 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001762
Devang Patel163a9f72010-05-10 22:49:55 +00001763 Asm->OutStreamer.AddComment("Compilation Unit Length");
1764 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1765 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1766 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001767
Devang Patel163a9f72010-05-10 22:49:55 +00001768 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1769 for (StringMap<DIE*>::const_iterator
1770 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1771 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001772 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001773
Devang Patel163a9f72010-05-10 22:49:55 +00001774 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1775 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001776
Devang Patel163a9f72010-05-10 22:49:55 +00001777 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1778 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1779 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001780
Devang Patel163a9f72010-05-10 22:49:55 +00001781 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001782 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001783 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1784 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001785 }
Devang Patel193f7202009-11-24 01:14:22 +00001786}
1787
Devang Patel2c4ceb12009-11-21 02:48:08 +00001788/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001789///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001790void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001791 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001792 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001793
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001794 // Start the dwarf str section.
1795 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001796 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001797
Chris Lattnerbc733f52010-03-13 02:17:42 +00001798 // Get all of the string pool entries and put them in an array by their ID so
1799 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001800 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001801 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001802
Chris Lattnerbc733f52010-03-13 02:17:42 +00001803 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1804 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1805 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001806
Chris Lattnerbc733f52010-03-13 02:17:42 +00001807 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001808
Chris Lattnerbc733f52010-03-13 02:17:42 +00001809 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001810 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001811 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001812
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001813 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001814 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001815 }
1816}
1817
Devang Patel2c4ceb12009-11-21 02:48:08 +00001818/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001819///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001820void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001821 if (DotDebugLocEntries.empty())
1822 return;
1823
Devang Patel6c3ea902011-02-04 22:57:18 +00001824 for (SmallVector<DotDebugLocEntry, 4>::iterator
1825 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1826 I != E; ++I) {
1827 DotDebugLocEntry &Entry = *I;
1828 if (I + 1 != DotDebugLocEntries.end())
1829 Entry.Merge(I+1);
1830 }
1831
Daniel Dunbar83320a02011-03-16 22:16:39 +00001832 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001833 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00001834 Asm->getObjFileLowering().getDwarfLocSection());
1835 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00001836 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1837 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001838 for (SmallVector<DotDebugLocEntry, 4>::iterator
1839 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00001840 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00001841 DotDebugLocEntry &Entry = *I;
1842 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00001843 if (Entry.isEmpty()) {
1844 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1845 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00001846 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00001847 } else {
1848 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1849 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00001850 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001851 Asm->OutStreamer.AddComment("Loc expr size");
1852 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1853 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1854 Asm->EmitLabelDifference(end, begin, 2);
1855 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00001856 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00001857 DIBasicType BTy(DV.getType());
1858 if (BTy.Verify() &&
1859 (BTy.getEncoding() == dwarf::DW_ATE_signed
1860 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1861 Asm->OutStreamer.AddComment("DW_OP_consts");
1862 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00001863 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001864 } else {
1865 Asm->OutStreamer.AddComment("DW_OP_constu");
1866 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00001867 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001868 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001869 } else if (Entry.isLocation()) {
1870 if (!DV.hasComplexAddress())
1871 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00001872 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00001873 else {
1874 // Complex address entry.
1875 unsigned N = DV.getNumAddrElements();
1876 unsigned i = 0;
1877 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
1878 if (Entry.Loc.getOffset()) {
1879 i = 2;
1880 Asm->EmitDwarfRegOp(Entry.Loc);
1881 Asm->OutStreamer.AddComment("DW_OP_deref");
1882 Asm->EmitInt8(dwarf::DW_OP_deref);
1883 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
1884 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1885 Asm->EmitSLEB128(DV.getAddrElement(1));
1886 } else {
1887 // If first address element is OpPlus then emit
1888 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
1889 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
1890 Asm->EmitDwarfRegOp(Loc);
1891 i = 2;
1892 }
1893 } else {
1894 Asm->EmitDwarfRegOp(Entry.Loc);
1895 }
1896
1897 // Emit remaining complex address elements.
1898 for (; i < N; ++i) {
1899 uint64_t Element = DV.getAddrElement(i);
1900 if (Element == DIBuilder::OpPlus) {
1901 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1902 Asm->EmitULEB128(DV.getAddrElement(++i));
1903 } else if (Element == DIBuilder::OpDeref)
1904 Asm->EmitInt8(dwarf::DW_OP_deref);
1905 else llvm_unreachable("unknown Opcode found in complex address");
1906 }
Devang Patelc26f5442011-04-28 02:22:40 +00001907 }
Devang Patelc26f5442011-04-28 02:22:40 +00001908 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001909 // else ... ignore constant fp. There is not any good way to
1910 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001911 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00001912 }
1913 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001914}
1915
1916/// EmitDebugARanges - Emit visible names into a debug aranges section.
1917///
1918void DwarfDebug::EmitDebugARanges() {
1919 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001920 Asm->OutStreamer.SwitchSection(
1921 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001922}
1923
Devang Patel2c4ceb12009-11-21 02:48:08 +00001924/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001925///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001926void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001927 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001928 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00001929 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00001930 unsigned char Size = Asm->getTargetData().getPointerSize();
1931 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00001932 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00001933 I != E; ++I) {
1934 if (*I)
1935 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001936 else
Devang Pateleac9c072010-04-27 19:46:33 +00001937 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001938 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001939}
1940
Devang Patel2c4ceb12009-11-21 02:48:08 +00001941/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001942///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001943void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001944 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00001945 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001946 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001947 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001948 }
1949}
1950
Devang Patel2c4ceb12009-11-21 02:48:08 +00001951/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00001952/// Section Header:
1953/// 1. length of section
1954/// 2. Dwarf version number
1955/// 3. address size.
1956///
1957/// Entries (one "entry" for each function that was inlined):
1958///
1959/// 1. offset into __debug_str section for MIPS linkage name, if exists;
1960/// otherwise offset into __debug_str for regular function name.
1961/// 2. offset into __debug_str section for regular function name.
1962/// 3. an unsigned LEB128 number indicating the number of distinct inlining
1963/// instances for the function.
1964///
1965/// The rest of the entry consists of a {die_offset, low_pc} pair for each
1966/// inlined instance; the die_offset points to the inlined_subroutine die in the
1967/// __debug_info section, and the low_pc is the starting address for the
1968/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001969void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001970 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00001971 return;
1972
Devang Patel163a9f72010-05-10 22:49:55 +00001973 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00001974 return;
1975
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001976 Asm->OutStreamer.SwitchSection(
1977 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00001978
Chris Lattner233f52b2010-03-09 23:52:58 +00001979 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00001980 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
1981 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00001982
Chris Lattnerc0215722010-04-04 19:25:43 +00001983 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00001984
Chris Lattner233f52b2010-03-09 23:52:58 +00001985 Asm->OutStreamer.AddComment("Dwarf Version");
1986 Asm->EmitInt16(dwarf::DWARF_VERSION);
1987 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001988 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00001989
Devang Patele9f8f5e2010-05-07 20:54:48 +00001990 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00001991 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001992
Devang Patele9f8f5e2010-05-07 20:54:48 +00001993 const MDNode *Node = *I;
1994 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001995 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00001996 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00001997 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00001998 StringRef LName = SP.getLinkageName();
1999 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002000
Chris Lattner233f52b2010-03-09 23:52:58 +00002001 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002002 if (LName.empty()) {
2003 Asm->OutStreamer.EmitBytes(Name, 0);
2004 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002005 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00002006 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2007 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002008
Chris Lattner233f52b2010-03-09 23:52:58 +00002009 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002010 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002011 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002012
Devang Patel53bb5c92009-11-10 23:06:00 +00002013 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002014 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002015 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002016 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002017
Chris Lattner3f53c832010-04-04 18:52:31 +00002018 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002019 Asm->OutStreamer.EmitSymbolValue(LI->first,
2020 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002021 }
2022 }
2023
Chris Lattnerc0215722010-04-04 19:25:43 +00002024 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002025}