blob: 1679a309961c7a5b654fffecbd633d7f184d2ef3 [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 Patele9f8f5e2010-05-07 20:54:48 +0000185DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000186 CompileUnit *SPCU = getCompileUnit(SPNode);
187 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 Patelbf47fdb2011-08-10 20:55:27 +0000249DIE *DwarfDebug::constructLexicalScopeDIE(LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000250
251 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
252 if (Scope->isAbstractScope())
253 return ScopeDIE;
254
Devang Patelbf47fdb2011-08-10 20:55:27 +0000255 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000256 if (Ranges.empty())
257 return 0;
258
Devang Patel3cbee302011-04-12 22:53:02 +0000259 CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
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 Patelbf47fdb2011-08-10 20:55:27 +0000295DIE *DwarfDebug::constructInlinedScopeDIE(LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000296
Devang Patelbf47fdb2011-08-10 20:55:27 +0000297 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000298 assert (Ranges.empty() == false
Devang Patelbf47fdb2011-08-10 20:55:27 +0000299 && "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000300
Devang Patel26a92002011-07-27 00:34:13 +0000301 if (!Scope->getScopeNode())
302 return NULL;
303 DIScope DS(Scope->getScopeNode());
304 DISubprogram InlinedSP = getDISubprogram(DS);
305 CompileUnit *TheCU = getCompileUnit(InlinedSP);
306 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/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000375DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, LexicalScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000376 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000377 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +0000378 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +0000379
Devang Patel59bc4092011-08-15 18:35:42 +0000380 // Translate tag to proper Dwarf tag.
381 unsigned Tag = DV->getTag();
Devang Patel53bb5c92009-11-10 23:06:00 +0000382
383 // Define variable debug information entry.
384 DIE *VariableDie = new DIE(Tag);
Devang Patel9341d102011-04-25 23:05:21 +0000385 CompileUnit *VariableCU = getCompileUnit(DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000386 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +0000387 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
388 V2AVI = VarToAbstractVarMap.find(DV);
389 if (V2AVI != VarToAbstractVarMap.end())
390 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000391
Devang Patel26c1e562010-05-20 16:36:41 +0000392 if (AbsDIE)
Devang Patel9341d102011-04-25 23:05:21 +0000393 VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel3cbee302011-04-12 22:53:02 +0000394 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000395 else {
Devang Patel9341d102011-04-25 23:05:21 +0000396 VariableCU->addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
397 Name);
398 VariableCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000399
400 // Add variable type.
Devang Patel9341d102011-04-25 23:05:21 +0000401 VariableCU->addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +0000402 }
403
Devang Patela098c502011-08-15 18:40:16 +0000404 if (DV->isArtificial())
405 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
Devang Patel9341d102011-04-25 23:05:21 +0000406 dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +0000407
408 if (Scope->isAbstractScope()) {
409 DV->setDIE(VariableDie);
410 return VariableDie;
411 }
412
413 // Add variable address.
414
415 unsigned Offset = DV->getDotDebugLocOffset();
416 if (Offset != ~0U) {
Devang Patel5bc942c2011-08-10 23:58:09 +0000417 VariableCU->addLabel(VariableDie, dwarf::DW_AT_location,
418 dwarf::DW_FORM_data4,
419 Asm->GetTempSymbol("debug_loc", Offset));
Devang Patelc3f5f782010-05-25 23:40:22 +0000420 DV->setDIE(VariableDie);
421 UseDotDebugLocEntry.insert(VariableDie);
422 return VariableDie;
423 }
424
425 // Check if variable is described by a DBG_VALUE instruction.
426 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
427 DbgVariableToDbgInstMap.find(DV);
428 if (DVI != DbgVariableToDbgInstMap.end()) {
429 const MachineInstr *DVInsn = DVI->second;
Devang Patelc3f5f782010-05-25 23:40:22 +0000430 bool updated = false;
Devang Patelc3f5f782010-05-25 23:40:22 +0000431 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +0000432 if (DVInsn->getOperand(0).isReg()) {
433 const MachineOperand RegOp = DVInsn->getOperand(0);
434 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
435 if (DVInsn->getOperand(1).isImm() &&
436 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patele1cdf842011-04-27 22:45:24 +0000437 unsigned FrameReg = 0;
438 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
439 int Offset =
440 TFI->getFrameIndexReference(*Asm->MF,
441 DVInsn->getOperand(1).getImm(),
442 FrameReg);
443 MachineLocation Location(FrameReg, Offset);
444 VariableCU->addVariableAddress(DV, VariableDie, Location);
445
446 } else if (RegOp.getReg())
447 VariableCU->addVariableAddress(DV, VariableDie,
448 MachineLocation(RegOp.getReg()));
449 updated = true;
Devang Patel0b48ead2010-08-31 22:22:42 +0000450 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000451 else if (DVInsn->getOperand(0).isImm())
Devang Patelb58128e2011-05-27 16:45:18 +0000452 updated =
453 VariableCU->addConstantValue(VariableDie, DVInsn->getOperand(0),
454 DV->getType());
Jim Grosbach1e20b962010-07-21 21:21:52 +0000455 else if (DVInsn->getOperand(0).isFPImm())
456 updated =
Devang Patel9341d102011-04-25 23:05:21 +0000457 VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patel8594d422011-06-24 20:46:11 +0000458 else if (DVInsn->getOperand(0).isCImm())
459 updated =
460 VariableCU->addConstantValue(VariableDie,
461 DVInsn->getOperand(0).getCImm(),
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000462 DV->getType().isUnsignedDIType());
Devang Patelc3f5f782010-05-25 23:40:22 +0000463 } else {
Devang Patele1cdf842011-04-27 22:45:24 +0000464 VariableCU->addVariableAddress(DV, VariableDie,
465 Asm->getDebugValueLocation(DVInsn));
466 updated = true;
Devang Patelc3f5f782010-05-25 23:40:22 +0000467 }
468 if (!updated) {
469 // If variableDie is not updated then DBG_VALUE instruction does not
470 // have valid variable info.
471 delete VariableDie;
472 return NULL;
473 }
474 DV->setDIE(VariableDie);
475 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000476 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000477
478 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +0000479 int FI = 0;
Devang Patele1cdf842011-04-27 22:45:24 +0000480 if (findVariableFrameIndex(DV, &FI)) {
481 unsigned FrameReg = 0;
482 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
483 int Offset =
484 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
485 MachineLocation Location(FrameReg, Offset);
486 VariableCU->addVariableAddress(DV, VariableDie, Location);
487 }
488
Devang Patel53bb5c92009-11-10 23:06:00 +0000489 DV->setDIE(VariableDie);
490 return VariableDie;
491
492}
Devang Patel2c4ceb12009-11-21 02:48:08 +0000493
494/// constructScopeDIE - Construct a DIE for this scope.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000495DIE *DwarfDebug::constructScopeDIE(LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000496 if (!Scope || !Scope->getScopeNode())
497 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000498
Devang Patel5bc9fec2011-02-19 01:31:27 +0000499 SmallVector <DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000500
501 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000502 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000503 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
504 if (DbgVariable *ArgDV = CurrentFnArguments[i])
505 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
506 Children.push_back(Arg);
507
Devang Patel5bc9fec2011-02-19 01:31:27 +0000508 // Collect lexical scope childrens first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000509 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000510 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
511 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
512 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000513 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000514 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
515 if (DIE *Nested = constructScopeDIE(Scopes[j]))
516 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000517 DIScope DS(Scope->getScopeNode());
518 DIE *ScopeDIE = NULL;
519 if (Scope->getInlinedAt())
520 ScopeDIE = constructInlinedScopeDIE(Scope);
521 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000522 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000523 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +0000524 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000525 // Note down abstract DIE.
526 if (ScopeDIE)
527 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
528 }
Devang Patel3c91b052010-03-08 20:52:55 +0000529 else
Devang Patel2db49d72010-05-07 18:11:54 +0000530 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000531 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000532 else {
533 // There is no need to emit empty lexical block DIE.
534 if (Children.empty())
535 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000536 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000537 }
538
Devang Patelaead63c2010-03-29 22:59:58 +0000539 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000540
Devang Patel5bc9fec2011-02-19 01:31:27 +0000541 // Add children
542 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
543 E = Children.end(); I != E; ++I)
544 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000545
Jim Grosbach1e20b962010-07-21 21:21:52 +0000546 if (DS.isSubprogram())
Devang Patel3cbee302011-04-12 22:53:02 +0000547 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000548
Devang Patel193f7202009-11-24 01:14:22 +0000549 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000550}
551
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000552/// GetOrCreateSourceID - Look up the source id with the given directory and
553/// source file names. If none currently exists, create a new id and insert it
554/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
555/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +0000556
Devang Patel23670e52011-03-24 20:30:50 +0000557unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
558 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000559 // If FE did not provide a file name, then assume stdin.
560 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000561 return GetOrCreateSourceID("<stdin>", StringRef());
562
563 // MCStream expects full path name as filename.
Benjamin Kramerab5f7882011-06-05 14:36:47 +0000564 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
565 SmallString<128> FullPathName = DirName;
566 sys::path::append(FullPathName, FileName);
Devang Patel23670e52011-03-24 20:30:50 +0000567 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
568 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
569 }
Devang Patel1905a182010-09-16 20:57:49 +0000570
Rafael Espindola5c055632010-11-18 02:04:25 +0000571 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
572 if (Entry.getValue())
573 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000574
Rafael Espindola5c055632010-11-18 02:04:25 +0000575 unsigned SrcId = SourceIdMap.size();
576 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000577
Rafael Espindola5c055632010-11-18 02:04:25 +0000578 // Print out a .file directive to specify files for .loc directives.
Devang Patel23670e52011-03-24 20:30:50 +0000579 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000580
581 return SrcId;
582}
583
Jim Grosbach1e20b962010-07-21 21:21:52 +0000584/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000585/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000586void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000587 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000588 StringRef FN = DIUnit.getFilename();
589 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000590 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000591
592 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000593 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
594 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
595 DIUnit.getProducer());
596 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
597 DIUnit.getLanguage());
598 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000599 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
600 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000601 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000602 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000603 // compile unit in debug_line section.
Rafael Espindoladc52ecf2011-05-10 20:35:05 +0000604 if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000605 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000606 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000607 else
Devang Patel3cbee302011-04-12 22:53:02 +0000608 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000609
Devang Patel65dbc902009-11-25 17:36:49 +0000610 if (!Dir.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000611 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000612 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000613 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000614
Devang Patel65dbc902009-11-25 17:36:49 +0000615 StringRef Flags = DIUnit.getFlags();
616 if (!Flags.empty())
Devang Patel5bc942c2011-08-10 23:58:09 +0000617 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
618 Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000619
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000620 unsigned RVer = DIUnit.getRunTimeVersion();
621 if (RVer)
Devang Patel3cbee302011-04-12 22:53:02 +0000622 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000623 dwarf::DW_FORM_data1, RVer);
624
Devang Patel163a9f72010-05-10 22:49:55 +0000625 if (!FirstCU)
626 FirstCU = NewCU;
627 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000628}
629
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000630/// getCompileUnit - Get CompileUnit DIE.
Devang Patel163a9f72010-05-10 22:49:55 +0000631CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
632 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
633 DIDescriptor D(N);
634 const MDNode *CUNode = NULL;
635 if (D.isCompileUnit())
636 CUNode = N;
637 else if (D.isSubprogram())
638 CUNode = DISubprogram(N).getCompileUnit();
639 else if (D.isType())
640 CUNode = DIType(N).getCompileUnit();
641 else if (D.isGlobalVariable())
642 CUNode = DIGlobalVariable(N).getCompileUnit();
643 else if (D.isVariable())
644 CUNode = DIVariable(N).getCompileUnit();
645 else if (D.isNameSpace())
646 CUNode = DINameSpace(N).getCompileUnit();
647 else if (D.isFile())
648 CUNode = DIFile(N).getCompileUnit();
649 else
650 return FirstCU;
651
652 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
653 = CUMap.find(CUNode);
654 if (I == CUMap.end())
655 return FirstCU;
656 return I->second;
657}
658
Devang Patel163a9f72010-05-10 22:49:55 +0000659/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000660void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +0000661 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000662
Devang Patel905cf5e2009-09-04 23:59:07 +0000663 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +0000664 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +0000665 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000666
667 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000668 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000669 TheCU->createGlobalVariableDIE(N);
Devang Patel13e16b62009-06-26 01:49:18 +0000670 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000671}
672
Devang Patel163a9f72010-05-10 22:49:55 +0000673/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000674void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000675 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000676
Stuart Hastings639336e2010-04-06 21:38:29 +0000677 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000678 CompileUnit *TheCU = getCompileUnit(N);
679 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +0000680 return;
681
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000682 if (!SP.isDefinition())
683 // This is a method declaration which will be handled while constructing
684 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000685 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000686
Devang Pateldbc64af2011-08-15 17:24:54 +0000687 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000688
689 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000690 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000691
692 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000693 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000694
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000695 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +0000696 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +0000697
Devang Patel13e16b62009-06-26 01:49:18 +0000698 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000699}
700
Devang Patel2c4ceb12009-11-21 02:48:08 +0000701/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000702/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000703/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000704void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000705 if (DisableDebugInfoPrinting)
706 return;
707
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000708 // If module has named metadata anchors then use them, otherwise scan the
709 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000710 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
711 if (CU_Nodes) {
Devang Patela938dd02011-05-04 16:34:02 +0000712
713 NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
714 NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
715 if (!GV_Nodes && !SP_Nodes)
716 // If there are not any global variables or any functions then
717 // there is not any debug info in this module.
Devang Patel30692ab2011-05-03 16:45:22 +0000718 return;
719
Devang Patela938dd02011-05-04 16:34:02 +0000720 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
721 constructCompileUnit(CU_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +0000722
Devang Patela938dd02011-05-04 16:34:02 +0000723 if (GV_Nodes)
724 for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i)
725 constructGlobalVariableDIE(GV_Nodes->getOperand(i));
726
727 if (SP_Nodes)
728 for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i)
729 constructSubprogramDIE(SP_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +0000730
731 } else {
732
733 DebugInfoFinder DbgFinder;
734 DbgFinder.processModule(*M);
735
Devang Patela938dd02011-05-04 16:34:02 +0000736 bool HasDebugInfo = false;
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000737 // Scan all the compile-units to see if there are any marked as the main
738 // unit. If not, we do not generate debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000739 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
740 E = DbgFinder.compile_unit_end(); I != E; ++I) {
741 if (DICompileUnit(*I).isMain()) {
742 HasDebugInfo = true;
743 break;
744 }
745 }
Devang Patela938dd02011-05-04 16:34:02 +0000746 if (!HasDebugInfo) return;
Devang Patel30692ab2011-05-03 16:45:22 +0000747
748 // Create all the compile unit DIEs.
749 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
750 E = DbgFinder.compile_unit_end(); I != E; ++I)
751 constructCompileUnit(*I);
752
753 // Create DIEs for each global variable.
754 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
755 E = DbgFinder.global_variable_end(); I != E; ++I)
756 constructGlobalVariableDIE(*I);
757
758 // Create DIEs for each subprogram.
759 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
760 E = DbgFinder.subprogram_end(); I != E; ++I)
761 constructSubprogramDIE(*I);
Chris Lattnerd850ac72010-04-05 02:19:28 +0000762 }
Devang Patel30692ab2011-05-03 16:45:22 +0000763
Chris Lattnerd850ac72010-04-05 02:19:28 +0000764 // Tell MMI that we have debug info.
765 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000766
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000767 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000768 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000769
Devang Patele7e5a0f2010-08-10 20:01:20 +0000770 //getOrCreateTypeDIE
771 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patel3cbee302011-04-12 22:53:02 +0000772 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
773 DIType Ty(NMD->getOperand(i));
774 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
775 }
Devang Patele7e5a0f2010-08-10 20:01:20 +0000776
Devang Patel1a7ca032010-09-28 18:08:20 +0000777 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patel3cbee302011-04-12 22:53:02 +0000778 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
779 DIType Ty(NMD->getOperand(i));
780 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
781 }
Devang Patel1a7ca032010-09-28 18:08:20 +0000782
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000783 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000784 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000785}
786
Devang Patel2c4ceb12009-11-21 02:48:08 +0000787/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000788///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000789void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000790 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000791 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000792 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000793 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
794 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
795 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
796 DISubprogram SP(AllSPs->getOperand(SI));
797 if (!SP.Verify()) continue;
798
799 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +0000800 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +0000801 StringRef FName = SP.getLinkageName();
802 if (FName.empty())
803 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +0000804 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +0000805 if (!NMD) continue;
806 unsigned E = NMD->getNumOperands();
807 if (!E) continue;
Devang Patel5bc942c2011-08-10 23:58:09 +0000808 LexicalScope *Scope = new LexicalScope(NULL, DIDescriptor(SP), NULL,
809 false);
Devang Patele9a1cca2010-08-02 17:32:15 +0000810 DeadFnScopeMap[SP] = Scope;
Benjamin Kramer0fa5e052011-08-11 18:39:28 +0000811 SmallVector<DbgVariable, 8> Variables;
Devang Patel4a1cad62010-06-28 18:25:03 +0000812 for (unsigned I = 0; I != E; ++I) {
813 DIVariable DV(NMD->getOperand(I));
814 if (!DV.Verify()) continue;
Benjamin Kramer0fa5e052011-08-11 18:39:28 +0000815 Variables.push_back(DbgVariable(DV));
Devang Patel4a1cad62010-06-28 18:25:03 +0000816 }
Jim Grosbach1e20b962010-07-21 21:21:52 +0000817
Devang Patel4a1cad62010-06-28 18:25:03 +0000818 // Construct subprogram DIE and add variables DIEs.
819 constructSubprogramDIE(SP);
820 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patel4a1cad62010-06-28 18:25:03 +0000821 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Benjamin Kramer0fa5e052011-08-11 18:39:28 +0000822 if (DIE *VariableDIE = constructVariableDIE(&Variables[i], Scope))
Devang Patel4a1cad62010-06-28 18:25:03 +0000823 ScopeDIE->addChild(VariableDIE);
824 }
825 }
826 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000827
Devang Patel53bb5c92009-11-10 23:06:00 +0000828 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
829 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
830 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
831 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000832 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000833 }
834
Devang Pateldbc64af2011-08-15 17:24:54 +0000835 // Emit DW_AT_containing_type attribute to connect types with their
836 // vtable holding type.
837 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
838 CUE = CUMap.end(); CUI != CUE; ++CUI) {
839 CompileUnit *TheCU = CUI->second;
840 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000841 }
842
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000843 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000844 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000845 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000846 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000847 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000848
849 // End text sections.
850 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000851 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000852 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000853 }
854
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000855 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000856 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000857
858 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000859 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000860
861 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000862 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000863
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000864 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000865 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000866
Devang Patel193f7202009-11-24 01:14:22 +0000867 // Emit info into a debug pubtypes section.
868 emitDebugPubTypes();
869
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000870 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000871 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000872
873 // Emit info into a debug aranges section.
874 EmitDebugARanges();
875
876 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000877 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000878
879 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000880 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000881
882 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000883 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000884
Chris Lattnerbc733f52010-03-13 02:17:42 +0000885 // Emit info into a debug str section.
886 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000887
Devang Patele9a1cca2010-08-02 17:32:15 +0000888 // clean up.
889 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +0000890 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
891 E = CUMap.end(); I != E; ++I)
892 delete I->second;
893 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000894}
895
Devang Patel53bb5c92009-11-10 23:06:00 +0000896/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000897DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000898 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000899 LLVMContext &Ctx = DV->getContext();
900 // More then one inlined variable corresponds to one abstract variable.
901 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000902 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000903 if (AbsDbgVariable)
904 return AbsDbgVariable;
905
Devang Patelbf47fdb2011-08-10 20:55:27 +0000906 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000907 if (!Scope)
908 return NULL;
909
Devang Patel26c1e562010-05-20 16:36:41 +0000910 AbsDbgVariable = new DbgVariable(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000911 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000912 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000913 return AbsDbgVariable;
914}
915
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000916/// addCurrentFnArgument - If Var is a current function argument then add
917/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000918bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000919 DbgVariable *Var, LexicalScope *Scope) {
920 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000921 return false;
922 DIVariable DV = Var->getVariable();
923 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
924 return false;
925 unsigned ArgNo = DV.getArgNumber();
926 if (ArgNo == 0)
927 return false;
928
Devang Patelcb3a6572011-03-03 20:02:02 +0000929 size_t Size = CurrentFnArguments.size();
930 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000931 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000932 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000933 // arguments does the function have at source level.
934 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000935 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000936 CurrentFnArguments[ArgNo - 1] = Var;
937 return true;
938}
939
Devang Patelee432862010-05-20 19:57:06 +0000940/// collectVariableInfoFromMMITable - Collect variable information from
941/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000942void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000943DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000944 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000945 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
946 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
947 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000948 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000949 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000950 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000951 DIVariable DV(Var);
952 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000953
Devang Patelbf47fdb2011-08-10 20:55:27 +0000954 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000955
Devang Patelfb0ee432009-11-10 23:20:04 +0000956 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000957 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000958 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000959
Devang Patel26c1e562010-05-20 16:36:41 +0000960 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
961 DbgVariable *RegVar = new DbgVariable(DV);
962 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000963 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000964 addScopeVariable(Scope, RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +0000965 if (AbsDbgVariable) {
966 recordVariableFrameIndex(AbsDbgVariable, VP.first);
967 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
968 }
Devang Patele717faa2009-10-06 01:26:37 +0000969 }
Devang Patelee432862010-05-20 19:57:06 +0000970}
Devang Patel90a48ad2010-03-15 18:33:46 +0000971
Jim Grosbach1e20b962010-07-21 21:21:52 +0000972/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000973/// DBG_VALUE instruction, is in a defined reg.
974static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
975 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000976 return MI->getNumOperands() == 3 &&
977 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
978 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000979}
980
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000981/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000982/// at MI.
983static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
984 const MCSymbol *FLabel,
985 const MCSymbol *SLabel,
986 const MachineInstr *MI) {
987 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
988
989 if (MI->getNumOperands() != 3) {
990 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
991 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
992 }
993 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
994 MachineLocation MLoc;
995 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
996 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
997 }
998 if (MI->getOperand(0).isImm())
999 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1000 if (MI->getOperand(0).isFPImm())
1001 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1002 if (MI->getOperand(0).isCImm())
1003 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1004
1005 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
1006 return DotDebugLocEntry();
1007}
1008
Devang Patelbf47fdb2011-08-10 20:55:27 +00001009/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001010void
Devang Patel78e127d2010-06-25 22:07:34 +00001011DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1012 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001013
Devang Patelee432862010-05-20 19:57:06 +00001014 /// collection info from MMI table.
1015 collectVariableInfoFromMMITable(MF, Processed);
1016
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001017 for (SmallVectorImpl<const MDNode*>::const_iterator
1018 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1019 ++UVI) {
1020 const MDNode *Var = *UVI;
1021 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001022 continue;
1023
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001024 // History contains relevant DBG_VALUE instructions for Var and instructions
1025 // clobbering it.
1026 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1027 if (History.empty())
1028 continue;
1029 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001030
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001031 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001032 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001033 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1034 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001035 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001036 else {
1037 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001038 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001039 else {
1040 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001041 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001042 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001043 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001044 }
1045 }
Devang Patelee432862010-05-20 19:57:06 +00001046 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001047 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001048 continue;
1049
1050 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001051 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patelee432862010-05-20 19:57:06 +00001052 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel0478c152011-03-01 22:58:55 +00001053 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001054 addScopeVariable(Scope, RegVar);
Devang Patelc0c5a262010-05-21 00:10:20 +00001055 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
1056 DbgVariableToDbgInstMap[AbsVar] = MInsn;
1057 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00001058 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001059
1060 // Simple ranges that are fully coalesced.
1061 if (History.size() <= 1 || (History.size() == 2 &&
1062 MInsn->isIdenticalTo(History.back()))) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001063 DbgVariableToDbgInstMap[RegVar] = MInsn;
1064 continue;
1065 }
1066
1067 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001068 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001069
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001070 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1071 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1072 const MachineInstr *Begin = *HI;
1073 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001074
Devang Patel4ada1d72011-06-01 23:00:17 +00001075 // Check if DBG_VALUE is truncating a range.
1076 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1077 && !Begin->getOperand(0).getReg())
1078 continue;
1079
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001080 // Compute the range for a register location.
1081 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1082 const MCSymbol *SLabel = 0;
1083
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001084 if (HI + 1 == HE)
1085 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001086 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001087 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001088 else {
1089 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001090 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1091 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001092 if (End->isDebugValue())
1093 SLabel = getLabelBeforeInsn(End);
1094 else {
1095 // End is a normal instruction clobbering the range.
1096 SLabel = getLabelAfterInsn(End);
1097 assert(SLabel && "Forgot label after clobber instruction");
1098 ++HI;
1099 }
1100 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001101
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001102 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +00001103 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001104 }
1105 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001106 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001107
1108 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00001109 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00001110 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00001111 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001112 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00001113 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00001114 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001115 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1116 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00001117 }
1118 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001119}
Devang Patel98e1cac2010-05-14 21:01:35 +00001120
Devang Patelc3f5f782010-05-25 23:40:22 +00001121/// getLabelBeforeInsn - Return Label preceding the instruction.
1122const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001123 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1124 assert(Label && "Didn't insert label before instruction");
1125 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001126}
1127
1128/// getLabelAfterInsn - Return Label immediately following the instruction.
1129const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001130 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001131}
1132
Devang Patelcbbe2872010-10-26 17:49:02 +00001133/// beginInstruction - Process beginning of an instruction.
1134void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001135 // Check if source location changes, but ignore DBG_VALUE locations.
1136 if (!MI->isDebugValue()) {
1137 DebugLoc DL = MI->getDebugLoc();
1138 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +00001139 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001140 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001141 if (DL == PrologEndLoc) {
1142 Flags |= DWARF2_FLAG_PROLOGUE_END;
1143 PrologEndLoc = DebugLoc();
1144 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001145 if (!DL.isUnknown()) {
1146 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001147 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001148 } else
Devang Patel4243e672011-05-11 19:22:19 +00001149 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001150 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001151 }
Devang Patelaead63c2010-03-29 22:59:58 +00001152
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001153 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001154 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1155 LabelsBeforeInsn.find(MI);
1156
1157 // No label needed.
1158 if (I == LabelsBeforeInsn.end())
1159 return;
1160
1161 // Label already assigned.
1162 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001163 return;
Devang Patel553881b2010-03-29 17:20:31 +00001164
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001165 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001166 PrevLabel = MMI->getContext().CreateTempSymbol();
1167 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001168 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001169 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001170}
1171
Devang Patelcbbe2872010-10-26 17:49:02 +00001172/// endInstruction - Process end of an instruction.
1173void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001174 // Don't create a new label after DBG_VALUE instructions.
1175 // They don't generate code.
1176 if (!MI->isDebugValue())
1177 PrevLabel = 0;
1178
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001179 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1180 LabelsAfterInsn.find(MI);
1181
1182 // No label needed.
1183 if (I == LabelsAfterInsn.end())
1184 return;
1185
1186 // Label already assigned.
1187 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001188 return;
1189
1190 // We need a label after this instruction.
1191 if (!PrevLabel) {
1192 PrevLabel = MMI->getContext().CreateTempSymbol();
1193 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001194 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001195 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001196}
1197
Jim Grosbach1e20b962010-07-21 21:21:52 +00001198/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001199/// Each LexicalScope has first instruction and last instruction to mark
1200/// beginning and end of a scope respectively. Create an inverse map that list
1201/// scopes starts (and ends) with an instruction. One instruction may start (or
1202/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001203void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001204 SmallVector<LexicalScope *, 4> WorkList;
1205 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001206 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001207 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001208
Devang Patelbf47fdb2011-08-10 20:55:27 +00001209 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001210 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001211 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001212 SE = Children.end(); SI != SE; ++SI)
1213 WorkList.push_back(*SI);
1214
Devang Patel53bb5c92009-11-10 23:06:00 +00001215 if (S->isAbstractScope())
1216 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001217
Devang Patelbf47fdb2011-08-10 20:55:27 +00001218 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001219 if (Ranges.empty())
1220 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001221 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001222 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001223 assert(RI->first && "InsnRange does not have first instruction!");
1224 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001225 requestLabelBeforeInsn(RI->first);
1226 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001227 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001228 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001229}
1230
Devang Patela3f48672011-05-09 22:14:49 +00001231/// getScopeNode - Get MDNode for DebugLoc's scope.
1232static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1233 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1234 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1235 return DL.getScope(Ctx);
1236}
1237
Devang Patel4243e672011-05-11 19:22:19 +00001238/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1239/// line number info for the function.
1240static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1241 const MDNode *Scope = getScopeNode(DL, Ctx);
1242 DISubprogram SP = getDISubprogram(Scope);
1243 if (SP.Verify())
1244 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1245 return DebugLoc();
1246}
1247
Devang Patel2c4ceb12009-11-21 02:48:08 +00001248/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001249/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001250void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001251 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001252 LScopes.initialize(*MF);
1253 if (LScopes.empty()) return;
1254 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001255
Devang Pateleac9c072010-04-27 19:46:33 +00001256 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1257 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001258 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001259 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001260
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001261 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1262
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001263 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001264 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1265 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1266
Devang Patelb2b31a62010-05-26 19:37:24 +00001267 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001268 I != E; ++I) {
1269 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001270 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1271 II != IE; ++II) {
1272 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001273
Devang Patelb2b31a62010-05-26 19:37:24 +00001274 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00001275 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001276
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001277 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001278 const MDNode *Var =
1279 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001280
1281 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001282 if (isDbgValueInDefinedReg(MI))
1283 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1284
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001285 // Check the history of this variable.
1286 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1287 if (History.empty()) {
1288 UserVariables.push_back(Var);
1289 // The first mention of a function argument gets the FunctionBeginSym
1290 // label, so arguments are visible when breaking at function entry.
1291 DIVariable DV(Var);
1292 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1293 DISubprogram(getDISubprogram(DV.getContext()))
1294 .describes(MF->getFunction()))
1295 LabelsBeforeInsn[MI] = FunctionBeginSym;
1296 } else {
1297 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1298 const MachineInstr *Prev = History.back();
1299 if (Prev->isDebugValue()) {
1300 // Coalesce identical entries at the end of History.
1301 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001302 Prev->isIdenticalTo(History[History.size() - 2])) {
1303 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1304 << "\t" << *Prev
1305 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001306 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001307 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001308
1309 // Terminate old register assignments that don't reach MI;
1310 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1311 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1312 isDbgValueInDefinedReg(Prev)) {
1313 // Previous register assignment needs to terminate at the end of
1314 // its basic block.
1315 MachineBasicBlock::const_iterator LastMI =
1316 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001317 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001318 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001319 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1320 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001321 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001322 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001323 else {
1324 // Terminate after LastMI.
1325 History.push_back(LastMI);
1326 }
1327 }
1328 }
1329 }
1330 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001331 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001332 // Not a DBG_VALUE instruction.
1333 if (!MI->isLabel())
1334 AtBlockEntry = false;
1335
Devang Patel4243e672011-05-11 19:22:19 +00001336 // First known non DBG_VALUE location marks beginning of function
1337 // body.
1338 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1339 PrologEndLoc = MI->getDebugLoc();
1340
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001341 // Check if the instruction clobbers any registers with debug vars.
1342 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1343 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1344 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1345 continue;
1346 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1347 unsigned Reg = *AI; ++AI) {
1348 const MDNode *Var = LiveUserVar[Reg];
1349 if (!Var)
1350 continue;
1351 // Reg is now clobbered.
1352 LiveUserVar[Reg] = 0;
1353
1354 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001355 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1356 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001357 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001358 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1359 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001360 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001361 const MachineInstr *Prev = History.back();
1362 // Sanity-check: Register assignments are terminated at the end of
1363 // their block.
1364 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1365 continue;
1366 // Is the variable still in Reg?
1367 if (!isDbgValueInDefinedReg(Prev) ||
1368 Prev->getOperand(0).getReg() != Reg)
1369 continue;
1370 // Var is clobbered. Make sure the next instruction gets a label.
1371 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001372 }
1373 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001374 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001375 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001376 }
1377
1378 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1379 I != E; ++I) {
1380 SmallVectorImpl<const MachineInstr*> &History = I->second;
1381 if (History.empty())
1382 continue;
1383
1384 // Make sure the final register assignments are terminated.
1385 const MachineInstr *Prev = History.back();
1386 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1387 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001388 MachineBasicBlock::const_iterator LastMI =
1389 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001390 if (LastMI == PrevMBB->end())
1391 // Drop DBG_VALUE for empty range.
1392 History.pop_back();
1393 else {
1394 // Terminate after LastMI.
1395 History.push_back(LastMI);
1396 }
1397 }
1398 // Request labels for the full history.
1399 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1400 const MachineInstr *MI = History[i];
1401 if (MI->isDebugValue())
1402 requestLabelBeforeInsn(MI);
1403 else
1404 requestLabelAfterInsn(MI);
1405 }
1406 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001407
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001408 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001409 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001410
1411 // Record beginning of function.
1412 if (!PrologEndLoc.isUnknown()) {
1413 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1414 MF->getFunction()->getContext());
1415 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1416 FnStartDL.getScope(MF->getFunction()->getContext()),
1417 DWARF2_FLAG_IS_STMT);
1418 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001419}
1420
Devang Patelbf47fdb2011-08-10 20:55:27 +00001421void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1422// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1423 ScopeVariables[LS].push_back(Var);
1424// Vars.push_back(Var);
1425}
1426
Devang Patel2c4ceb12009-11-21 02:48:08 +00001427/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001428///
Chris Lattnereec791a2010-01-26 23:18:02 +00001429void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001430 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001431
Devang Patelbf47fdb2011-08-10 20:55:27 +00001432 // Define end label for subprogram.
1433 FunctionEndSym = Asm->GetTempSymbol("func_end",
1434 Asm->getFunctionNumber());
1435 // Assumes in correct section after the entry point.
1436 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1437
1438 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1439 collectVariableInfo(MF, ProcessedVars);
1440
1441 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001442 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1443 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1444 LexicalScope *AScope = AList[i];
1445 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001446 if (SP.Verify()) {
1447 // Collect info for variables that were optimized out.
1448 StringRef FName = SP.getLinkageName();
1449 if (FName.empty())
1450 FName = SP.getName();
1451 if (NamedMDNode *NMD =
1452 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
1453 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001454 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00001455 if (!DV || !ProcessedVars.insert(DV))
1456 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001457 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1458 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patel78e127d2010-06-25 22:07:34 +00001459 }
1460 }
1461 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001462 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
1463 constructScopeDIE(AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001464 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001465
1466 DIE *CurFnDIE = constructScopeDIE(LScopes.getCurrentFunctionScope());
1467
1468 if (!DisableFramePointerElim(*MF)) {
1469 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel5bc942c2011-08-10 23:58:09 +00001470 CompileUnit *TheCU = getCompileUnit(FnScope->getScopeNode());
1471 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1472 dwarf::DW_FORM_flag, 1);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001473 }
1474 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1475 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001476
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001477 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001478 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1479 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1480 DeleteContainerPointers(I->second);
1481 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001482 DeleteContainerPointers(CurrentFnArguments);
Devang Patel26c1e562010-05-20 16:36:41 +00001483 DbgVariableToFrameIndexMap.clear();
1484 VarToAbstractVarMap.clear();
1485 DbgVariableToDbgInstMap.clear();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001486 UserVariables.clear();
1487 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001488 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001489 LabelsBeforeInsn.clear();
1490 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001491 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001492}
1493
Devang Patel26c1e562010-05-20 16:36:41 +00001494/// recordVariableFrameIndex - Record a variable's index.
1495void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
1496 assert (V && "Invalid DbgVariable!");
1497 DbgVariableToFrameIndexMap[V] = Index;
1498}
1499
1500/// findVariableFrameIndex - Return true if frame index for the variable
1501/// is found. Update FI to hold value of the index.
1502bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
1503 assert (V && "Invalid DbgVariable!");
1504 DenseMap<const DbgVariable *, int>::iterator I =
1505 DbgVariableToFrameIndexMap.find(V);
1506 if (I == DbgVariableToFrameIndexMap.end())
1507 return false;
1508 *FI = I->second;
1509 return true;
1510}
1511
Chris Lattnerc6087842010-03-09 04:54:43 +00001512/// recordSourceLine - Register a source line with debug info. Returns the
1513/// unique label that was emitted and which provides correspondence to
1514/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001515void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1516 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001517 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001518 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001519 unsigned Src = 1;
1520 if (S) {
1521 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001522
Dan Gohman1cc0d622010-05-05 23:41:32 +00001523 if (Scope.isCompileUnit()) {
1524 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001525 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001526 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001527 } else if (Scope.isFile()) {
1528 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001529 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001530 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001531 } else if (Scope.isSubprogram()) {
1532 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001533 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001534 Dir = SP.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001535 } else if (Scope.isLexicalBlock()) {
1536 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001537 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001538 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001539 } else
1540 assert(0 && "Unexpected scope info");
1541
Devang Patel23670e52011-03-24 20:30:50 +00001542 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001543 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001544 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001545}
1546
Bill Wendling829e67b2009-05-20 23:22:40 +00001547//===----------------------------------------------------------------------===//
1548// Emit Methods
1549//===----------------------------------------------------------------------===//
1550
Devang Patel2c4ceb12009-11-21 02:48:08 +00001551/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001552///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001553unsigned
1554DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001555 // Get the children.
1556 const std::vector<DIE *> &Children = Die->getChildren();
1557
1558 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001559 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001560 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001561
1562 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001563 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001564
1565 // Get the abbreviation for this DIE.
1566 unsigned AbbrevNumber = Die->getAbbrevNumber();
1567 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1568
1569 // Set DIE offset
1570 Die->setOffset(Offset);
1571
1572 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001573 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001574
1575 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1576 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1577
1578 // Size the DIE attribute values.
1579 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1580 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001581 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001582
1583 // Size the DIE children if any.
1584 if (!Children.empty()) {
1585 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1586 "Children flag not set");
1587
1588 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001589 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001590
1591 // End of children marker.
1592 Offset += sizeof(int8_t);
1593 }
1594
1595 Die->setSize(Offset - Die->getOffset());
1596 return Offset;
1597}
1598
Devang Patel2c4ceb12009-11-21 02:48:08 +00001599/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001600///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001601void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001602 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1603 E = CUMap.end(); I != E; ++I) {
1604 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001605 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001606 sizeof(int32_t) + // Length of Compilation Unit Info
1607 sizeof(int16_t) + // DWARF version number
1608 sizeof(int32_t) + // Offset Into Abbrev. Section
1609 sizeof(int8_t); // Pointer Size (in bytes)
1610 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001611 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001612}
1613
Chris Lattner11b8f302010-04-04 23:02:02 +00001614/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1615/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00001616static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00001617 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00001618 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00001619 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001620
Chris Lattner9c69e285532010-04-04 22:59:04 +00001621 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1622 Asm->OutStreamer.EmitLabel(TmpSym);
1623 return TmpSym;
1624}
1625
1626/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1627/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001628void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001629 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001630
Bill Wendling94d04b82009-05-20 23:21:38 +00001631 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001632 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001633 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001634 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001635 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001636 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001637
Chris Lattner9c69e285532010-04-04 22:59:04 +00001638 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001639 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001640
Devang Patelaf608bd2010-08-24 00:06:12 +00001641 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001642 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1643 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1644 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001645 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001646 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001647 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1648 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001649
Devang Patelc3f5f782010-05-25 23:40:22 +00001650 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1651 "section_debug_loc");
1652
Chris Lattner9c69e285532010-04-04 22:59:04 +00001653 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001654 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001655}
1656
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001657/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001658///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001659void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001660 // Get the abbreviation for this DIE.
1661 unsigned AbbrevNumber = Die->getAbbrevNumber();
1662 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1663
Bill Wendling94d04b82009-05-20 23:21:38 +00001664 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001665 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001666 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1667 Twine::utohexstr(Die->getOffset()) + ":0x" +
1668 Twine::utohexstr(Die->getSize()) + " " +
1669 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001670 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001671
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001672 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001673 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1674
1675 // Emit the DIE attribute values.
1676 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1677 unsigned Attr = AbbrevData[i].getAttribute();
1678 unsigned Form = AbbrevData[i].getForm();
1679 assert(Form && "Too many attributes for DIE (check abbreviation)");
1680
Chris Lattner3f53c832010-04-04 18:52:31 +00001681 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001682 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001683
Bill Wendling94d04b82009-05-20 23:21:38 +00001684 switch (Attr) {
1685 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001686 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001687 break;
1688 case dwarf::DW_AT_abstract_origin: {
1689 DIEEntry *E = cast<DIEEntry>(Values[i]);
1690 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001691 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001692 Asm->EmitInt32(Addr);
1693 break;
1694 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001695 case dwarf::DW_AT_ranges: {
1696 // DW_AT_range Value encodes offset in debug_range section.
1697 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001698
1699 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1700 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1701 V->getValue(),
1702 4);
1703 } else {
1704 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1705 V->getValue(),
1706 DwarfDebugRangeSectionSym,
1707 4);
1708 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001709 break;
1710 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001711 case dwarf::DW_AT_location: {
1712 if (UseDotDebugLocEntry.count(Die) != 0) {
1713 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbar83320a02011-03-16 22:16:39 +00001714 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patelc3f5f782010-05-25 23:40:22 +00001715 } else
1716 Values[i]->EmitValue(Asm, Form);
1717 break;
1718 }
Devang Patel2a361602010-09-29 19:08:08 +00001719 case dwarf::DW_AT_accessibility: {
1720 if (Asm->isVerbose()) {
1721 DIEInteger *V = cast<DIEInteger>(Values[i]);
1722 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1723 }
1724 Values[i]->EmitValue(Asm, Form);
1725 break;
1726 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001727 default:
1728 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001729 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001730 break;
1731 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001732 }
1733
1734 // Emit the DIE children if any.
1735 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1736 const std::vector<DIE *> &Children = Die->getChildren();
1737
1738 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001739 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001740
Chris Lattner3f53c832010-04-04 18:52:31 +00001741 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001742 Asm->OutStreamer.AddComment("End Of Children Mark");
1743 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001744 }
1745}
1746
Devang Patel8a241142009-12-09 18:24:21 +00001747/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001748///
Devang Patel8a241142009-12-09 18:24:21 +00001749void DwarfDebug::emitDebugInfo() {
1750 // Start debug info section.
1751 Asm->OutStreamer.SwitchSection(
1752 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001753 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1754 E = CUMap.end(); I != E; ++I) {
1755 CompileUnit *TheCU = I->second;
1756 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001757
Devang Patel163a9f72010-05-10 22:49:55 +00001758 // Emit the compile units header.
1759 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1760 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001761
Devang Patel163a9f72010-05-10 22:49:55 +00001762 // Emit size of content not including length itself
1763 unsigned ContentSize = Die->getSize() +
1764 sizeof(int16_t) + // DWARF version number
1765 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001766 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001767
Devang Patel163a9f72010-05-10 22:49:55 +00001768 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1769 Asm->EmitInt32(ContentSize);
1770 Asm->OutStreamer.AddComment("DWARF version number");
1771 Asm->EmitInt16(dwarf::DWARF_VERSION);
1772 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1773 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1774 DwarfAbbrevSectionSym);
1775 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1776 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001777
Devang Patel163a9f72010-05-10 22:49:55 +00001778 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001779 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1780 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001781}
1782
Devang Patel2c4ceb12009-11-21 02:48:08 +00001783/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001784///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001785void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001786 // Check to see if it is worth the effort.
1787 if (!Abbreviations.empty()) {
1788 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001789 Asm->OutStreamer.SwitchSection(
1790 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001791
Chris Lattnerc0215722010-04-04 19:25:43 +00001792 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001793
1794 // For each abbrevation.
1795 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1796 // Get abbreviation data
1797 const DIEAbbrev *Abbrev = Abbreviations[i];
1798
1799 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001800 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001801
1802 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001803 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001804 }
1805
1806 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001807 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001808
Chris Lattnerc0215722010-04-04 19:25:43 +00001809 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001810 }
1811}
1812
Devang Patel2c4ceb12009-11-21 02:48:08 +00001813/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001814/// the line matrix.
1815///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001816void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001817 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001818 Asm->OutStreamer.AddComment("Extended Op");
1819 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001820
Chris Lattner233f52b2010-03-09 23:52:58 +00001821 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001822 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001823 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1824 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1825
1826 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001827
Chris Lattnerc0215722010-04-04 19:25:43 +00001828 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001829 Asm->getTargetData().getPointerSize(),
1830 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001831
1832 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001833 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1834 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001835 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001836 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001837}
1838
Devang Patel8a241142009-12-09 18:24:21 +00001839/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1840///
1841void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00001842 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1843 E = CUMap.end(); I != E; ++I) {
1844 CompileUnit *TheCU = I->second;
1845 // Start the dwarf pubnames section.
1846 Asm->OutStreamer.SwitchSection(
1847 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001848
Devang Patel163a9f72010-05-10 22:49:55 +00001849 Asm->OutStreamer.AddComment("Length of Public Names Info");
1850 Asm->EmitLabelDifference(
1851 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1852 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001853
Devang Patel163a9f72010-05-10 22:49:55 +00001854 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1855 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001856
Devang Patel163a9f72010-05-10 22:49:55 +00001857 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001858 Asm->EmitInt16(dwarf::DWARF_VERSION);
1859
Devang Patel163a9f72010-05-10 22:49:55 +00001860 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001861 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00001862 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001863
Devang Patel163a9f72010-05-10 22:49:55 +00001864 Asm->OutStreamer.AddComment("Compilation Unit Length");
1865 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1866 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1867 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001868
Devang Patel163a9f72010-05-10 22:49:55 +00001869 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1870 for (StringMap<DIE*>::const_iterator
1871 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1872 const char *Name = GI->getKeyData();
1873 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001874
Devang Patel163a9f72010-05-10 22:49:55 +00001875 Asm->OutStreamer.AddComment("DIE offset");
1876 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001877
Devang Patel163a9f72010-05-10 22:49:55 +00001878 if (Asm->isVerbose())
1879 Asm->OutStreamer.AddComment("External Name");
1880 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1881 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001882
Devang Patel163a9f72010-05-10 22:49:55 +00001883 Asm->OutStreamer.AddComment("End Mark");
1884 Asm->EmitInt32(0);
1885 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001886 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00001887 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001888}
1889
Devang Patel193f7202009-11-24 01:14:22 +00001890void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001891 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1892 E = CUMap.end(); I != E; ++I) {
1893 CompileUnit *TheCU = I->second;
1894 // Start the dwarf pubnames section.
1895 Asm->OutStreamer.SwitchSection(
1896 Asm->getObjFileLowering().getDwarfPubTypesSection());
1897 Asm->OutStreamer.AddComment("Length of Public Types Info");
1898 Asm->EmitLabelDifference(
1899 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1900 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001901
Devang Patel163a9f72010-05-10 22:49:55 +00001902 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1903 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001904
Devang Patel163a9f72010-05-10 22:49:55 +00001905 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1906 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001907
Devang Patel163a9f72010-05-10 22:49:55 +00001908 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1909 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1910 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001911
Devang Patel163a9f72010-05-10 22:49:55 +00001912 Asm->OutStreamer.AddComment("Compilation Unit Length");
1913 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1914 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1915 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001916
Devang Patel163a9f72010-05-10 22:49:55 +00001917 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1918 for (StringMap<DIE*>::const_iterator
1919 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1920 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001921 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001922
Devang Patel163a9f72010-05-10 22:49:55 +00001923 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1924 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001925
Devang Patel163a9f72010-05-10 22:49:55 +00001926 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1927 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1928 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001929
Devang Patel163a9f72010-05-10 22:49:55 +00001930 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001931 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001932 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1933 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001934 }
Devang Patel193f7202009-11-24 01:14:22 +00001935}
1936
Devang Patel2c4ceb12009-11-21 02:48:08 +00001937/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001938///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001939void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001940 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001941 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001942
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001943 // Start the dwarf str section.
1944 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001945 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001946
Chris Lattnerbc733f52010-03-13 02:17:42 +00001947 // Get all of the string pool entries and put them in an array by their ID so
1948 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001949 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001950 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001951
Chris Lattnerbc733f52010-03-13 02:17:42 +00001952 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1953 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1954 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001955
Chris Lattnerbc733f52010-03-13 02:17:42 +00001956 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001957
Chris Lattnerbc733f52010-03-13 02:17:42 +00001958 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001959 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001960 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001961
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001962 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001963 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001964 }
1965}
1966
Devang Patel2c4ceb12009-11-21 02:48:08 +00001967/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001968///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001969void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001970 if (DotDebugLocEntries.empty())
1971 return;
1972
Devang Patel6c3ea902011-02-04 22:57:18 +00001973 for (SmallVector<DotDebugLocEntry, 4>::iterator
1974 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1975 I != E; ++I) {
1976 DotDebugLocEntry &Entry = *I;
1977 if (I + 1 != DotDebugLocEntries.end())
1978 Entry.Merge(I+1);
1979 }
1980
Daniel Dunbar83320a02011-03-16 22:16:39 +00001981 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001982 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00001983 Asm->getObjFileLowering().getDwarfLocSection());
1984 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00001985 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1986 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001987 for (SmallVector<DotDebugLocEntry, 4>::iterator
1988 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00001989 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00001990 DotDebugLocEntry &Entry = *I;
1991 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00001992 if (Entry.isEmpty()) {
1993 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1994 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00001995 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00001996 } else {
1997 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1998 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00001999 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002000 Asm->OutStreamer.AddComment("Loc expr size");
2001 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2002 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2003 Asm->EmitLabelDifference(end, begin, 2);
2004 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002005 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002006 DIBasicType BTy(DV.getType());
2007 if (BTy.Verify() &&
2008 (BTy.getEncoding() == dwarf::DW_ATE_signed
2009 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2010 Asm->OutStreamer.AddComment("DW_OP_consts");
2011 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002012 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002013 } else {
2014 Asm->OutStreamer.AddComment("DW_OP_constu");
2015 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002016 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002017 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002018 } else if (Entry.isLocation()) {
2019 if (!DV.hasComplexAddress())
2020 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002021 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002022 else {
2023 // Complex address entry.
2024 unsigned N = DV.getNumAddrElements();
2025 unsigned i = 0;
2026 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2027 if (Entry.Loc.getOffset()) {
2028 i = 2;
2029 Asm->EmitDwarfRegOp(Entry.Loc);
2030 Asm->OutStreamer.AddComment("DW_OP_deref");
2031 Asm->EmitInt8(dwarf::DW_OP_deref);
2032 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2033 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2034 Asm->EmitSLEB128(DV.getAddrElement(1));
2035 } else {
2036 // If first address element is OpPlus then emit
2037 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2038 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2039 Asm->EmitDwarfRegOp(Loc);
2040 i = 2;
2041 }
2042 } else {
2043 Asm->EmitDwarfRegOp(Entry.Loc);
2044 }
2045
2046 // Emit remaining complex address elements.
2047 for (; i < N; ++i) {
2048 uint64_t Element = DV.getAddrElement(i);
2049 if (Element == DIBuilder::OpPlus) {
2050 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2051 Asm->EmitULEB128(DV.getAddrElement(++i));
2052 } else if (Element == DIBuilder::OpDeref)
2053 Asm->EmitInt8(dwarf::DW_OP_deref);
2054 else llvm_unreachable("unknown Opcode found in complex address");
2055 }
Devang Patelc26f5442011-04-28 02:22:40 +00002056 }
Devang Patelc26f5442011-04-28 02:22:40 +00002057 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002058 // else ... ignore constant fp. There is not any good way to
2059 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002060 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002061 }
2062 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002063}
2064
2065/// EmitDebugARanges - Emit visible names into a debug aranges section.
2066///
2067void DwarfDebug::EmitDebugARanges() {
2068 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002069 Asm->OutStreamer.SwitchSection(
2070 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002071}
2072
Devang Patel2c4ceb12009-11-21 02:48:08 +00002073/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002074///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002075void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002076 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002077 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002078 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002079 unsigned char Size = Asm->getTargetData().getPointerSize();
2080 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002081 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002082 I != E; ++I) {
2083 if (*I)
2084 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002085 else
Devang Pateleac9c072010-04-27 19:46:33 +00002086 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002087 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002088}
2089
Devang Patel2c4ceb12009-11-21 02:48:08 +00002090/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002091///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002092void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002093 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002094 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002095 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002096 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002097 }
2098}
2099
Devang Patel2c4ceb12009-11-21 02:48:08 +00002100/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002101/// Section Header:
2102/// 1. length of section
2103/// 2. Dwarf version number
2104/// 3. address size.
2105///
2106/// Entries (one "entry" for each function that was inlined):
2107///
2108/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2109/// otherwise offset into __debug_str for regular function name.
2110/// 2. offset into __debug_str section for regular function name.
2111/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2112/// instances for the function.
2113///
2114/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2115/// inlined instance; the die_offset points to the inlined_subroutine die in the
2116/// __debug_info section, and the low_pc is the starting address for the
2117/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002118void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002119 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002120 return;
2121
Devang Patel163a9f72010-05-10 22:49:55 +00002122 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002123 return;
2124
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002125 Asm->OutStreamer.SwitchSection(
2126 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002127
Chris Lattner233f52b2010-03-09 23:52:58 +00002128 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002129 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2130 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002131
Chris Lattnerc0215722010-04-04 19:25:43 +00002132 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002133
Chris Lattner233f52b2010-03-09 23:52:58 +00002134 Asm->OutStreamer.AddComment("Dwarf Version");
2135 Asm->EmitInt16(dwarf::DWARF_VERSION);
2136 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002137 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002138
Devang Patele9f8f5e2010-05-07 20:54:48 +00002139 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002140 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002141
Devang Patele9f8f5e2010-05-07 20:54:48 +00002142 const MDNode *Node = *I;
2143 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002144 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002145 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002146 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002147 StringRef LName = SP.getLinkageName();
2148 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002149
Chris Lattner233f52b2010-03-09 23:52:58 +00002150 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002151 if (LName.empty()) {
2152 Asm->OutStreamer.EmitBytes(Name, 0);
2153 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002154 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00002155 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2156 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002157
Chris Lattner233f52b2010-03-09 23:52:58 +00002158 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002159 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002160 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002161
Devang Patel53bb5c92009-11-10 23:06:00 +00002162 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002163 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002164 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002165 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002166
Chris Lattner3f53c832010-04-04 18:52:31 +00002167 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002168 Asm->OutStreamer.EmitSymbolValue(LI->first,
2169 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002170 }
2171 }
2172
Chris Lattnerc0215722010-04-04 19:25:43 +00002173 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002174}