blob: 69791a9684b40b8819791a83d264662ed75f245e [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 Patel5a1a67c2011-08-15 19:01:20 +0000386 DbgVariable *AbsVar = DV->getAbstractVariable();
387 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Devang Patel26c1e562010-05-20 16:36:41 +0000388 if (AbsDIE)
Devang Patel9341d102011-04-25 23:05:21 +0000389 VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel5a1a67c2011-08-15 19:01:20 +0000390 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000391 else {
Devang Patel5a1a67c2011-08-15 19:01:20 +0000392 VariableCU->addString(VariableDie, dwarf::DW_AT_name,
393 dwarf::DW_FORM_string, Name);
Devang Patel9341d102011-04-25 23:05:21 +0000394 VariableCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patel9341d102011-04-25 23:05:21 +0000395 VariableCU->addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +0000396 }
397
Devang Patela098c502011-08-15 18:40:16 +0000398 if (DV->isArtificial())
399 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
Devang Patel9341d102011-04-25 23:05:21 +0000400 dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +0000401
402 if (Scope->isAbstractScope()) {
403 DV->setDIE(VariableDie);
404 return VariableDie;
405 }
406
407 // Add variable address.
408
409 unsigned Offset = DV->getDotDebugLocOffset();
410 if (Offset != ~0U) {
Devang Patel5bc942c2011-08-10 23:58:09 +0000411 VariableCU->addLabel(VariableDie, dwarf::DW_AT_location,
412 dwarf::DW_FORM_data4,
413 Asm->GetTempSymbol("debug_loc", Offset));
Devang Patelc3f5f782010-05-25 23:40:22 +0000414 DV->setDIE(VariableDie);
415 UseDotDebugLocEntry.insert(VariableDie);
416 return VariableDie;
417 }
418
419 // Check if variable is described by a DBG_VALUE instruction.
Devang Patelff9dd0a2011-08-15 21:24:36 +0000420 if (const MachineInstr *DVInsn = DV->getMInsn()) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000421 bool updated = false;
Devang Patelc3f5f782010-05-25 23:40:22 +0000422 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +0000423 if (DVInsn->getOperand(0).isReg()) {
424 const MachineOperand RegOp = DVInsn->getOperand(0);
425 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
426 if (DVInsn->getOperand(1).isImm() &&
427 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patele1cdf842011-04-27 22:45:24 +0000428 unsigned FrameReg = 0;
429 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
430 int Offset =
431 TFI->getFrameIndexReference(*Asm->MF,
432 DVInsn->getOperand(1).getImm(),
433 FrameReg);
434 MachineLocation Location(FrameReg, Offset);
435 VariableCU->addVariableAddress(DV, VariableDie, Location);
436
437 } else if (RegOp.getReg())
438 VariableCU->addVariableAddress(DV, VariableDie,
439 MachineLocation(RegOp.getReg()));
440 updated = true;
Devang Patel0b48ead2010-08-31 22:22:42 +0000441 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000442 else if (DVInsn->getOperand(0).isImm())
Devang Patelb58128e2011-05-27 16:45:18 +0000443 updated =
444 VariableCU->addConstantValue(VariableDie, DVInsn->getOperand(0),
445 DV->getType());
Jim Grosbach1e20b962010-07-21 21:21:52 +0000446 else if (DVInsn->getOperand(0).isFPImm())
447 updated =
Devang Patel9341d102011-04-25 23:05:21 +0000448 VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patel8594d422011-06-24 20:46:11 +0000449 else if (DVInsn->getOperand(0).isCImm())
450 updated =
451 VariableCU->addConstantValue(VariableDie,
452 DVInsn->getOperand(0).getCImm(),
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000453 DV->getType().isUnsignedDIType());
Devang Patelc3f5f782010-05-25 23:40:22 +0000454 } else {
Devang Patele1cdf842011-04-27 22:45:24 +0000455 VariableCU->addVariableAddress(DV, VariableDie,
456 Asm->getDebugValueLocation(DVInsn));
457 updated = true;
Devang Patelc3f5f782010-05-25 23:40:22 +0000458 }
459 if (!updated) {
460 // If variableDie is not updated then DBG_VALUE instruction does not
461 // have valid variable info.
462 delete VariableDie;
463 return NULL;
464 }
465 DV->setDIE(VariableDie);
466 return VariableDie;
Devang Patelff9dd0a2011-08-15 21:24:36 +0000467 } else {
468 // .. else use frame index.
469 int FI = DV->getFrameIndex();
470 if (FI != ~0U) {
471 unsigned FrameReg = 0;
472 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
473 int Offset =
474 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
475 MachineLocation Location(FrameReg, Offset);
476 VariableCU->addVariableAddress(DV, VariableDie, Location);
477 }
Devang Patele1cdf842011-04-27 22:45:24 +0000478 }
479
Devang Patel53bb5c92009-11-10 23:06:00 +0000480 DV->setDIE(VariableDie);
481 return VariableDie;
482
483}
Devang Patel2c4ceb12009-11-21 02:48:08 +0000484
485/// constructScopeDIE - Construct a DIE for this scope.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000486DIE *DwarfDebug::constructScopeDIE(LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000487 if (!Scope || !Scope->getScopeNode())
488 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000489
Devang Patel5bc9fec2011-02-19 01:31:27 +0000490 SmallVector <DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000491
492 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000493 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000494 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
495 if (DbgVariable *ArgDV = CurrentFnArguments[i])
496 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
497 Children.push_back(Arg);
498
Devang Patel5bc9fec2011-02-19 01:31:27 +0000499 // Collect lexical scope childrens first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000500 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000501 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
502 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
503 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000504 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000505 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
506 if (DIE *Nested = constructScopeDIE(Scopes[j]))
507 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000508 DIScope DS(Scope->getScopeNode());
509 DIE *ScopeDIE = NULL;
510 if (Scope->getInlinedAt())
511 ScopeDIE = constructInlinedScopeDIE(Scope);
512 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000513 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000514 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +0000515 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000516 // Note down abstract DIE.
517 if (ScopeDIE)
518 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
519 }
Devang Patel3c91b052010-03-08 20:52:55 +0000520 else
Devang Patel2db49d72010-05-07 18:11:54 +0000521 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000522 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000523 else {
524 // There is no need to emit empty lexical block DIE.
525 if (Children.empty())
526 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000527 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000528 }
529
Devang Patelaead63c2010-03-29 22:59:58 +0000530 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000531
Devang Patel5bc9fec2011-02-19 01:31:27 +0000532 // Add children
533 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
534 E = Children.end(); I != E; ++I)
535 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000536
Jim Grosbach1e20b962010-07-21 21:21:52 +0000537 if (DS.isSubprogram())
Devang Patel3cbee302011-04-12 22:53:02 +0000538 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000539
Devang Patel193f7202009-11-24 01:14:22 +0000540 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000541}
542
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000543/// GetOrCreateSourceID - Look up the source id with the given directory and
544/// source file names. If none currently exists, create a new id and insert it
545/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
546/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +0000547
Devang Patel23670e52011-03-24 20:30:50 +0000548unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
549 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000550 // If FE did not provide a file name, then assume stdin.
551 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000552 return GetOrCreateSourceID("<stdin>", StringRef());
553
554 // MCStream expects full path name as filename.
Benjamin Kramerab5f7882011-06-05 14:36:47 +0000555 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
556 SmallString<128> FullPathName = DirName;
557 sys::path::append(FullPathName, FileName);
Devang Patel23670e52011-03-24 20:30:50 +0000558 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
559 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
560 }
Devang Patel1905a182010-09-16 20:57:49 +0000561
Rafael Espindola5c055632010-11-18 02:04:25 +0000562 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
563 if (Entry.getValue())
564 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000565
Rafael Espindola5c055632010-11-18 02:04:25 +0000566 unsigned SrcId = SourceIdMap.size();
567 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000568
Rafael Espindola5c055632010-11-18 02:04:25 +0000569 // Print out a .file directive to specify files for .loc directives.
Devang Patel23670e52011-03-24 20:30:50 +0000570 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000571
572 return SrcId;
573}
574
Jim Grosbach1e20b962010-07-21 21:21:52 +0000575/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000576/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000577void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000578 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000579 StringRef FN = DIUnit.getFilename();
580 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000581 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000582
583 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000584 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
585 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
586 DIUnit.getProducer());
587 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
588 DIUnit.getLanguage());
589 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000590 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
591 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000592 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000593 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000594 // compile unit in debug_line section.
Rafael Espindoladc52ecf2011-05-10 20:35:05 +0000595 if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000596 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000597 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000598 else
Devang Patel3cbee302011-04-12 22:53:02 +0000599 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000600
Devang Patel65dbc902009-11-25 17:36:49 +0000601 if (!Dir.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000602 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000603 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000604 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000605
Devang Patel65dbc902009-11-25 17:36:49 +0000606 StringRef Flags = DIUnit.getFlags();
607 if (!Flags.empty())
Devang Patel5bc942c2011-08-10 23:58:09 +0000608 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
609 Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000610
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000611 unsigned RVer = DIUnit.getRunTimeVersion();
612 if (RVer)
Devang Patel3cbee302011-04-12 22:53:02 +0000613 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000614 dwarf::DW_FORM_data1, RVer);
615
Devang Patel163a9f72010-05-10 22:49:55 +0000616 if (!FirstCU)
617 FirstCU = NewCU;
618 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000619}
620
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000621/// getCompileUnit - Get CompileUnit DIE.
Devang Patel163a9f72010-05-10 22:49:55 +0000622CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
623 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
624 DIDescriptor D(N);
625 const MDNode *CUNode = NULL;
626 if (D.isCompileUnit())
627 CUNode = N;
628 else if (D.isSubprogram())
629 CUNode = DISubprogram(N).getCompileUnit();
630 else if (D.isType())
631 CUNode = DIType(N).getCompileUnit();
632 else if (D.isGlobalVariable())
633 CUNode = DIGlobalVariable(N).getCompileUnit();
634 else if (D.isVariable())
635 CUNode = DIVariable(N).getCompileUnit();
636 else if (D.isNameSpace())
637 CUNode = DINameSpace(N).getCompileUnit();
638 else if (D.isFile())
639 CUNode = DIFile(N).getCompileUnit();
640 else
641 return FirstCU;
642
643 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
644 = CUMap.find(CUNode);
645 if (I == CUMap.end())
646 return FirstCU;
647 return I->second;
648}
649
Devang Patel163a9f72010-05-10 22:49:55 +0000650/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000651void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +0000652 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000653
Devang Patel905cf5e2009-09-04 23:59:07 +0000654 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +0000655 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +0000656 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000657
658 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000659 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000660 TheCU->createGlobalVariableDIE(N);
Devang Patel13e16b62009-06-26 01:49:18 +0000661 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000662}
663
Devang Patel163a9f72010-05-10 22:49:55 +0000664/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000665void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000666 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000667
Stuart Hastings639336e2010-04-06 21:38:29 +0000668 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000669 CompileUnit *TheCU = getCompileUnit(N);
670 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +0000671 return;
672
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000673 if (!SP.isDefinition())
674 // This is a method declaration which will be handled while constructing
675 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000676 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000677
Devang Pateldbc64af2011-08-15 17:24:54 +0000678 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000679
680 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000681 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000682
683 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000684 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000685
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000686 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +0000687 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +0000688
Devang Patel13e16b62009-06-26 01:49:18 +0000689 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000690}
691
Devang Patel2c4ceb12009-11-21 02:48:08 +0000692/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000693/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000694/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000695void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000696 if (DisableDebugInfoPrinting)
697 return;
698
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000699 // If module has named metadata anchors then use them, otherwise scan the
700 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000701 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
702 if (CU_Nodes) {
Devang Patela938dd02011-05-04 16:34:02 +0000703
704 NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
705 NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
706 if (!GV_Nodes && !SP_Nodes)
707 // If there are not any global variables or any functions then
708 // there is not any debug info in this module.
Devang Patel30692ab2011-05-03 16:45:22 +0000709 return;
710
Devang Patela938dd02011-05-04 16:34:02 +0000711 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
712 constructCompileUnit(CU_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +0000713
Devang Patela938dd02011-05-04 16:34:02 +0000714 if (GV_Nodes)
715 for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i)
716 constructGlobalVariableDIE(GV_Nodes->getOperand(i));
717
718 if (SP_Nodes)
719 for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i)
720 constructSubprogramDIE(SP_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +0000721
722 } else {
723
724 DebugInfoFinder DbgFinder;
725 DbgFinder.processModule(*M);
726
Devang Patela938dd02011-05-04 16:34:02 +0000727 bool HasDebugInfo = false;
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000728 // Scan all the compile-units to see if there are any marked as the main
729 // unit. If not, we do not generate debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000730 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
731 E = DbgFinder.compile_unit_end(); I != E; ++I) {
732 if (DICompileUnit(*I).isMain()) {
733 HasDebugInfo = true;
734 break;
735 }
736 }
Devang Patela938dd02011-05-04 16:34:02 +0000737 if (!HasDebugInfo) return;
Devang Patel30692ab2011-05-03 16:45:22 +0000738
739 // Create all the compile unit DIEs.
740 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
741 E = DbgFinder.compile_unit_end(); I != E; ++I)
742 constructCompileUnit(*I);
743
744 // Create DIEs for each global variable.
745 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
746 E = DbgFinder.global_variable_end(); I != E; ++I)
747 constructGlobalVariableDIE(*I);
748
749 // Create DIEs for each subprogram.
750 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
751 E = DbgFinder.subprogram_end(); I != E; ++I)
752 constructSubprogramDIE(*I);
Chris Lattnerd850ac72010-04-05 02:19:28 +0000753 }
Devang Patel30692ab2011-05-03 16:45:22 +0000754
Chris Lattnerd850ac72010-04-05 02:19:28 +0000755 // Tell MMI that we have debug info.
756 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000757
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000758 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000759 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000760
Devang Patele7e5a0f2010-08-10 20:01:20 +0000761 //getOrCreateTypeDIE
762 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patel3cbee302011-04-12 22:53:02 +0000763 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
764 DIType Ty(NMD->getOperand(i));
765 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
766 }
Devang Patele7e5a0f2010-08-10 20:01:20 +0000767
Devang Patel1a7ca032010-09-28 18:08:20 +0000768 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patel3cbee302011-04-12 22:53:02 +0000769 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
770 DIType Ty(NMD->getOperand(i));
771 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
772 }
Devang Patel1a7ca032010-09-28 18:08:20 +0000773
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000774 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000775 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000776}
777
Devang Patel2c4ceb12009-11-21 02:48:08 +0000778/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000779///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000780void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000781 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000782 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000783 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000784 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
785 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
786 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
787 DISubprogram SP(AllSPs->getOperand(SI));
788 if (!SP.Verify()) continue;
789
790 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +0000791 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +0000792 StringRef FName = SP.getLinkageName();
793 if (FName.empty())
794 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +0000795 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +0000796 if (!NMD) continue;
797 unsigned E = NMD->getNumOperands();
798 if (!E) continue;
Devang Patel5bc942c2011-08-10 23:58:09 +0000799 LexicalScope *Scope = new LexicalScope(NULL, DIDescriptor(SP), NULL,
800 false);
Devang Patele9a1cca2010-08-02 17:32:15 +0000801 DeadFnScopeMap[SP] = Scope;
Benjamin Kramer0fa5e052011-08-11 18:39:28 +0000802 SmallVector<DbgVariable, 8> Variables;
Devang Patel4a1cad62010-06-28 18:25:03 +0000803 for (unsigned I = 0; I != E; ++I) {
804 DIVariable DV(NMD->getOperand(I));
805 if (!DV.Verify()) continue;
Devang Patel5a1a67c2011-08-15 19:01:20 +0000806 Variables.push_back(DbgVariable(DV, NULL));
Devang Patel4a1cad62010-06-28 18:25:03 +0000807 }
Jim Grosbach1e20b962010-07-21 21:21:52 +0000808
Devang Patel4a1cad62010-06-28 18:25:03 +0000809 // Construct subprogram DIE and add variables DIEs.
810 constructSubprogramDIE(SP);
811 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patel4a1cad62010-06-28 18:25:03 +0000812 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Benjamin Kramer0fa5e052011-08-11 18:39:28 +0000813 if (DIE *VariableDIE = constructVariableDIE(&Variables[i], Scope))
Devang Patel4a1cad62010-06-28 18:25:03 +0000814 ScopeDIE->addChild(VariableDIE);
815 }
816 }
817 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000818
Devang Patel53bb5c92009-11-10 23:06:00 +0000819 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
820 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
821 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
822 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000823 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000824 }
825
Devang Pateldbc64af2011-08-15 17:24:54 +0000826 // Emit DW_AT_containing_type attribute to connect types with their
827 // vtable holding type.
828 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
829 CUE = CUMap.end(); CUI != CUE; ++CUI) {
830 CompileUnit *TheCU = CUI->second;
831 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000832 }
833
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000834 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000835 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000836 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000837 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000838 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000839
840 // End text sections.
841 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000842 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000843 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000844 }
845
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000846 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000847 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000848
849 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000850 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000851
852 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000853 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000854
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000855 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000856 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000857
Devang Patel193f7202009-11-24 01:14:22 +0000858 // Emit info into a debug pubtypes section.
859 emitDebugPubTypes();
860
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000861 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000862 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000863
864 // Emit info into a debug aranges section.
865 EmitDebugARanges();
866
867 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000868 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000869
870 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000871 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000872
873 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000874 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000875
Chris Lattnerbc733f52010-03-13 02:17:42 +0000876 // Emit info into a debug str section.
877 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000878
Devang Patele9a1cca2010-08-02 17:32:15 +0000879 // clean up.
880 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +0000881 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
882 E = CUMap.end(); I != E; ++I)
883 delete I->second;
884 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000885}
886
Devang Patel53bb5c92009-11-10 23:06:00 +0000887/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000888DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000889 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000890 LLVMContext &Ctx = DV->getContext();
891 // More then one inlined variable corresponds to one abstract variable.
892 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000893 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000894 if (AbsDbgVariable)
895 return AbsDbgVariable;
896
Devang Patelbf47fdb2011-08-10 20:55:27 +0000897 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000898 if (!Scope)
899 return NULL;
900
Devang Patel5a1a67c2011-08-15 19:01:20 +0000901 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000902 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000903 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000904 return AbsDbgVariable;
905}
906
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000907/// addCurrentFnArgument - If Var is a current function argument then add
908/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000909bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000910 DbgVariable *Var, LexicalScope *Scope) {
911 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000912 return false;
913 DIVariable DV = Var->getVariable();
914 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
915 return false;
916 unsigned ArgNo = DV.getArgNumber();
917 if (ArgNo == 0)
918 return false;
919
Devang Patelcb3a6572011-03-03 20:02:02 +0000920 size_t Size = CurrentFnArguments.size();
921 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000922 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000923 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000924 // arguments does the function have at source level.
925 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000926 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000927 CurrentFnArguments[ArgNo - 1] = Var;
928 return true;
929}
930
Devang Patelee432862010-05-20 19:57:06 +0000931/// collectVariableInfoFromMMITable - Collect variable information from
932/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000933void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000934DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000935 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000936 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
937 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
938 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000939 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000940 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000941 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000942 DIVariable DV(Var);
943 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000944
Devang Patelbf47fdb2011-08-10 20:55:27 +0000945 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000946
Devang Patelfb0ee432009-11-10 23:20:04 +0000947 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000948 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000949 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000950
Devang Patel26c1e562010-05-20 16:36:41 +0000951 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000952 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000953 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000954 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000955 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000956 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000957 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000958 }
Devang Patelee432862010-05-20 19:57:06 +0000959}
Devang Patel90a48ad2010-03-15 18:33:46 +0000960
Jim Grosbach1e20b962010-07-21 21:21:52 +0000961/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000962/// DBG_VALUE instruction, is in a defined reg.
963static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
964 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000965 return MI->getNumOperands() == 3 &&
966 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
967 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000968}
969
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000970/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000971/// at MI.
972static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
973 const MCSymbol *FLabel,
974 const MCSymbol *SLabel,
975 const MachineInstr *MI) {
976 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
977
978 if (MI->getNumOperands() != 3) {
979 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
980 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
981 }
982 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
983 MachineLocation MLoc;
984 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
985 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
986 }
987 if (MI->getOperand(0).isImm())
988 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
989 if (MI->getOperand(0).isFPImm())
990 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
991 if (MI->getOperand(0).isCImm())
992 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
993
994 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
995 return DotDebugLocEntry();
996}
997
Devang Patelbf47fdb2011-08-10 20:55:27 +0000998/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000999void
Devang Patel78e127d2010-06-25 22:07:34 +00001000DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1001 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001002
Devang Patelee432862010-05-20 19:57:06 +00001003 /// collection info from MMI table.
1004 collectVariableInfoFromMMITable(MF, Processed);
1005
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001006 for (SmallVectorImpl<const MDNode*>::const_iterator
1007 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1008 ++UVI) {
1009 const MDNode *Var = *UVI;
1010 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001011 continue;
1012
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001013 // History contains relevant DBG_VALUE instructions for Var and instructions
1014 // clobbering it.
1015 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1016 if (History.empty())
1017 continue;
1018 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001019
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001020 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001021 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001022 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1023 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001024 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001025 else {
1026 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001027 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001028 else {
1029 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001030 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001031 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001032 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001033 }
1034 }
Devang Patelee432862010-05-20 19:57:06 +00001035 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001036 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001037 continue;
1038
1039 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001040 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001041 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1042 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001043 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001044 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001045 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001046 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001047
1048 // Simple ranges that are fully coalesced.
1049 if (History.size() <= 1 || (History.size() == 2 &&
1050 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001051 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001052 continue;
1053 }
1054
1055 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001056 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001057
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001058 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1059 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1060 const MachineInstr *Begin = *HI;
1061 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001062
Devang Patel4ada1d72011-06-01 23:00:17 +00001063 // Check if DBG_VALUE is truncating a range.
1064 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1065 && !Begin->getOperand(0).getReg())
1066 continue;
1067
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001068 // Compute the range for a register location.
1069 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1070 const MCSymbol *SLabel = 0;
1071
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001072 if (HI + 1 == HE)
1073 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001074 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001075 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001076 else {
1077 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001078 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1079 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001080 if (End->isDebugValue())
1081 SLabel = getLabelBeforeInsn(End);
1082 else {
1083 // End is a normal instruction clobbering the range.
1084 SLabel = getLabelAfterInsn(End);
1085 assert(SLabel && "Forgot label after clobber instruction");
1086 ++HI;
1087 }
1088 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001089
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001090 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +00001091 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001092 }
1093 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001094 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001095
1096 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00001097 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00001098 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00001099 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001100 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00001101 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00001102 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001103 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Devang Patel5a1a67c2011-08-15 19:01:20 +00001104 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001105 }
1106 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001107}
Devang Patel98e1cac2010-05-14 21:01:35 +00001108
Devang Patelc3f5f782010-05-25 23:40:22 +00001109/// getLabelBeforeInsn - Return Label preceding the instruction.
1110const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001111 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1112 assert(Label && "Didn't insert label before instruction");
1113 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001114}
1115
1116/// getLabelAfterInsn - Return Label immediately following the instruction.
1117const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001118 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001119}
1120
Devang Patelcbbe2872010-10-26 17:49:02 +00001121/// beginInstruction - Process beginning of an instruction.
1122void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001123 // Check if source location changes, but ignore DBG_VALUE locations.
1124 if (!MI->isDebugValue()) {
1125 DebugLoc DL = MI->getDebugLoc();
1126 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +00001127 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001128 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001129 if (DL == PrologEndLoc) {
1130 Flags |= DWARF2_FLAG_PROLOGUE_END;
1131 PrologEndLoc = DebugLoc();
1132 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001133 if (!DL.isUnknown()) {
1134 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001135 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001136 } else
Devang Patel4243e672011-05-11 19:22:19 +00001137 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001138 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001139 }
Devang Patelaead63c2010-03-29 22:59:58 +00001140
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001141 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001142 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1143 LabelsBeforeInsn.find(MI);
1144
1145 // No label needed.
1146 if (I == LabelsBeforeInsn.end())
1147 return;
1148
1149 // Label already assigned.
1150 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001151 return;
Devang Patel553881b2010-03-29 17:20:31 +00001152
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001153 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001154 PrevLabel = MMI->getContext().CreateTempSymbol();
1155 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001156 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001157 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001158}
1159
Devang Patelcbbe2872010-10-26 17:49:02 +00001160/// endInstruction - Process end of an instruction.
1161void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001162 // Don't create a new label after DBG_VALUE instructions.
1163 // They don't generate code.
1164 if (!MI->isDebugValue())
1165 PrevLabel = 0;
1166
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001167 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1168 LabelsAfterInsn.find(MI);
1169
1170 // No label needed.
1171 if (I == LabelsAfterInsn.end())
1172 return;
1173
1174 // Label already assigned.
1175 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001176 return;
1177
1178 // We need a label after this instruction.
1179 if (!PrevLabel) {
1180 PrevLabel = MMI->getContext().CreateTempSymbol();
1181 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001182 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001183 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001184}
1185
Jim Grosbach1e20b962010-07-21 21:21:52 +00001186/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001187/// Each LexicalScope has first instruction and last instruction to mark
1188/// beginning and end of a scope respectively. Create an inverse map that list
1189/// scopes starts (and ends) with an instruction. One instruction may start (or
1190/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001191void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001192 SmallVector<LexicalScope *, 4> WorkList;
1193 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001194 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001195 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001196
Devang Patelbf47fdb2011-08-10 20:55:27 +00001197 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001198 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001199 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001200 SE = Children.end(); SI != SE; ++SI)
1201 WorkList.push_back(*SI);
1202
Devang Patel53bb5c92009-11-10 23:06:00 +00001203 if (S->isAbstractScope())
1204 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001205
Devang Patelbf47fdb2011-08-10 20:55:27 +00001206 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001207 if (Ranges.empty())
1208 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001209 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001210 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001211 assert(RI->first && "InsnRange does not have first instruction!");
1212 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001213 requestLabelBeforeInsn(RI->first);
1214 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001215 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001216 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001217}
1218
Devang Patela3f48672011-05-09 22:14:49 +00001219/// getScopeNode - Get MDNode for DebugLoc's scope.
1220static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1221 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1222 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1223 return DL.getScope(Ctx);
1224}
1225
Devang Patel4243e672011-05-11 19:22:19 +00001226/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1227/// line number info for the function.
1228static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1229 const MDNode *Scope = getScopeNode(DL, Ctx);
1230 DISubprogram SP = getDISubprogram(Scope);
1231 if (SP.Verify())
1232 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1233 return DebugLoc();
1234}
1235
Devang Patel2c4ceb12009-11-21 02:48:08 +00001236/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001237/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001238void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001239 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001240 LScopes.initialize(*MF);
1241 if (LScopes.empty()) return;
1242 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001243
Devang Pateleac9c072010-04-27 19:46:33 +00001244 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1245 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001246 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001247 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001248
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001249 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1250
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001251 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001252 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1253 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1254
Devang Patelb2b31a62010-05-26 19:37:24 +00001255 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001256 I != E; ++I) {
1257 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001258 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1259 II != IE; ++II) {
1260 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001261
Devang Patelb2b31a62010-05-26 19:37:24 +00001262 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00001263 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001264
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001265 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001266 const MDNode *Var =
1267 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001268
1269 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001270 if (isDbgValueInDefinedReg(MI))
1271 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1272
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001273 // Check the history of this variable.
1274 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1275 if (History.empty()) {
1276 UserVariables.push_back(Var);
1277 // The first mention of a function argument gets the FunctionBeginSym
1278 // label, so arguments are visible when breaking at function entry.
1279 DIVariable DV(Var);
1280 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1281 DISubprogram(getDISubprogram(DV.getContext()))
1282 .describes(MF->getFunction()))
1283 LabelsBeforeInsn[MI] = FunctionBeginSym;
1284 } else {
1285 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1286 const MachineInstr *Prev = History.back();
1287 if (Prev->isDebugValue()) {
1288 // Coalesce identical entries at the end of History.
1289 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001290 Prev->isIdenticalTo(History[History.size() - 2])) {
1291 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1292 << "\t" << *Prev
1293 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001294 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001295 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001296
1297 // Terminate old register assignments that don't reach MI;
1298 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1299 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1300 isDbgValueInDefinedReg(Prev)) {
1301 // Previous register assignment needs to terminate at the end of
1302 // its basic block.
1303 MachineBasicBlock::const_iterator LastMI =
1304 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001305 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001306 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001307 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1308 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001309 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001310 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001311 else {
1312 // Terminate after LastMI.
1313 History.push_back(LastMI);
1314 }
1315 }
1316 }
1317 }
1318 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001319 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001320 // Not a DBG_VALUE instruction.
1321 if (!MI->isLabel())
1322 AtBlockEntry = false;
1323
Devang Patel4243e672011-05-11 19:22:19 +00001324 // First known non DBG_VALUE location marks beginning of function
1325 // body.
1326 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1327 PrologEndLoc = MI->getDebugLoc();
1328
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001329 // Check if the instruction clobbers any registers with debug vars.
1330 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1331 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1332 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1333 continue;
1334 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1335 unsigned Reg = *AI; ++AI) {
1336 const MDNode *Var = LiveUserVar[Reg];
1337 if (!Var)
1338 continue;
1339 // Reg is now clobbered.
1340 LiveUserVar[Reg] = 0;
1341
1342 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001343 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1344 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001345 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001346 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1347 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001348 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001349 const MachineInstr *Prev = History.back();
1350 // Sanity-check: Register assignments are terminated at the end of
1351 // their block.
1352 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1353 continue;
1354 // Is the variable still in Reg?
1355 if (!isDbgValueInDefinedReg(Prev) ||
1356 Prev->getOperand(0).getReg() != Reg)
1357 continue;
1358 // Var is clobbered. Make sure the next instruction gets a label.
1359 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001360 }
1361 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001362 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001363 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001364 }
1365
1366 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1367 I != E; ++I) {
1368 SmallVectorImpl<const MachineInstr*> &History = I->second;
1369 if (History.empty())
1370 continue;
1371
1372 // Make sure the final register assignments are terminated.
1373 const MachineInstr *Prev = History.back();
1374 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1375 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001376 MachineBasicBlock::const_iterator LastMI =
1377 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001378 if (LastMI == PrevMBB->end())
1379 // Drop DBG_VALUE for empty range.
1380 History.pop_back();
1381 else {
1382 // Terminate after LastMI.
1383 History.push_back(LastMI);
1384 }
1385 }
1386 // Request labels for the full history.
1387 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1388 const MachineInstr *MI = History[i];
1389 if (MI->isDebugValue())
1390 requestLabelBeforeInsn(MI);
1391 else
1392 requestLabelAfterInsn(MI);
1393 }
1394 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001395
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001396 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001397 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001398
1399 // Record beginning of function.
1400 if (!PrologEndLoc.isUnknown()) {
1401 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1402 MF->getFunction()->getContext());
1403 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1404 FnStartDL.getScope(MF->getFunction()->getContext()),
1405 DWARF2_FLAG_IS_STMT);
1406 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001407}
1408
Devang Patelbf47fdb2011-08-10 20:55:27 +00001409void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1410// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1411 ScopeVariables[LS].push_back(Var);
1412// Vars.push_back(Var);
1413}
1414
Devang Patel2c4ceb12009-11-21 02:48:08 +00001415/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001416///
Chris Lattnereec791a2010-01-26 23:18:02 +00001417void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001418 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001419
Devang Patelbf47fdb2011-08-10 20:55:27 +00001420 // Define end label for subprogram.
1421 FunctionEndSym = Asm->GetTempSymbol("func_end",
1422 Asm->getFunctionNumber());
1423 // Assumes in correct section after the entry point.
1424 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1425
1426 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1427 collectVariableInfo(MF, ProcessedVars);
1428
1429 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001430 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1431 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1432 LexicalScope *AScope = AList[i];
1433 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001434 if (SP.Verify()) {
1435 // Collect info for variables that were optimized out.
1436 StringRef FName = SP.getLinkageName();
1437 if (FName.empty())
1438 FName = SP.getName();
1439 if (NamedMDNode *NMD =
1440 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
1441 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001442 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00001443 if (!DV || !ProcessedVars.insert(DV))
1444 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001445 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Devang Patel5a1a67c2011-08-15 19:01:20 +00001446 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001447 }
1448 }
1449 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001450 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
1451 constructScopeDIE(AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001452 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001453
1454 DIE *CurFnDIE = constructScopeDIE(LScopes.getCurrentFunctionScope());
1455
1456 if (!DisableFramePointerElim(*MF)) {
1457 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel5bc942c2011-08-10 23:58:09 +00001458 CompileUnit *TheCU = getCompileUnit(FnScope->getScopeNode());
1459 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1460 dwarf::DW_FORM_flag, 1);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001461 }
1462 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1463 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001464
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001465 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001466 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1467 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1468 DeleteContainerPointers(I->second);
1469 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001470 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001471 UserVariables.clear();
1472 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001473 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001474 LabelsBeforeInsn.clear();
1475 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001476 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001477}
1478
Chris Lattnerc6087842010-03-09 04:54:43 +00001479/// recordSourceLine - Register a source line with debug info. Returns the
1480/// unique label that was emitted and which provides correspondence to
1481/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001482void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1483 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001484 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001485 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001486 unsigned Src = 1;
1487 if (S) {
1488 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001489
Dan Gohman1cc0d622010-05-05 23:41:32 +00001490 if (Scope.isCompileUnit()) {
1491 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001492 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001493 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001494 } else if (Scope.isFile()) {
1495 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001496 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001497 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001498 } else if (Scope.isSubprogram()) {
1499 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001500 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001501 Dir = SP.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001502 } else if (Scope.isLexicalBlock()) {
1503 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001504 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001505 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001506 } else
1507 assert(0 && "Unexpected scope info");
1508
Devang Patel23670e52011-03-24 20:30:50 +00001509 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001510 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001511 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001512}
1513
Bill Wendling829e67b2009-05-20 23:22:40 +00001514//===----------------------------------------------------------------------===//
1515// Emit Methods
1516//===----------------------------------------------------------------------===//
1517
Devang Patel2c4ceb12009-11-21 02:48:08 +00001518/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001519///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001520unsigned
1521DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001522 // Get the children.
1523 const std::vector<DIE *> &Children = Die->getChildren();
1524
1525 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001526 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001527 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001528
1529 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001530 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001531
1532 // Get the abbreviation for this DIE.
1533 unsigned AbbrevNumber = Die->getAbbrevNumber();
1534 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1535
1536 // Set DIE offset
1537 Die->setOffset(Offset);
1538
1539 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001540 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001541
1542 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1543 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1544
1545 // Size the DIE attribute values.
1546 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1547 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001548 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001549
1550 // Size the DIE children if any.
1551 if (!Children.empty()) {
1552 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1553 "Children flag not set");
1554
1555 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001556 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001557
1558 // End of children marker.
1559 Offset += sizeof(int8_t);
1560 }
1561
1562 Die->setSize(Offset - Die->getOffset());
1563 return Offset;
1564}
1565
Devang Patel2c4ceb12009-11-21 02:48:08 +00001566/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001567///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001568void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001569 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1570 E = CUMap.end(); I != E; ++I) {
1571 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001572 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001573 sizeof(int32_t) + // Length of Compilation Unit Info
1574 sizeof(int16_t) + // DWARF version number
1575 sizeof(int32_t) + // Offset Into Abbrev. Section
1576 sizeof(int8_t); // Pointer Size (in bytes)
1577 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001578 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001579}
1580
Chris Lattner11b8f302010-04-04 23:02:02 +00001581/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1582/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00001583static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00001584 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00001585 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00001586 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001587
Chris Lattner9c69e285532010-04-04 22:59:04 +00001588 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1589 Asm->OutStreamer.EmitLabel(TmpSym);
1590 return TmpSym;
1591}
1592
1593/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1594/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001595void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001596 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001597
Bill Wendling94d04b82009-05-20 23:21:38 +00001598 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001599 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001600 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001601 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001602 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001603 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001604
Chris Lattner9c69e285532010-04-04 22:59:04 +00001605 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001606 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001607
Devang Patelaf608bd2010-08-24 00:06:12 +00001608 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001609 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1610 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1611 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001612 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001613 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001614 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1615 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001616
Devang Patelc3f5f782010-05-25 23:40:22 +00001617 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1618 "section_debug_loc");
1619
Chris Lattner9c69e285532010-04-04 22:59:04 +00001620 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001621 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001622}
1623
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001624/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001625///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001626void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001627 // Get the abbreviation for this DIE.
1628 unsigned AbbrevNumber = Die->getAbbrevNumber();
1629 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1630
Bill Wendling94d04b82009-05-20 23:21:38 +00001631 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001632 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001633 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1634 Twine::utohexstr(Die->getOffset()) + ":0x" +
1635 Twine::utohexstr(Die->getSize()) + " " +
1636 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001637 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001638
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001639 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001640 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1641
1642 // Emit the DIE attribute values.
1643 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1644 unsigned Attr = AbbrevData[i].getAttribute();
1645 unsigned Form = AbbrevData[i].getForm();
1646 assert(Form && "Too many attributes for DIE (check abbreviation)");
1647
Chris Lattner3f53c832010-04-04 18:52:31 +00001648 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001649 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001650
Bill Wendling94d04b82009-05-20 23:21:38 +00001651 switch (Attr) {
1652 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001653 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001654 break;
1655 case dwarf::DW_AT_abstract_origin: {
1656 DIEEntry *E = cast<DIEEntry>(Values[i]);
1657 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001658 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001659 Asm->EmitInt32(Addr);
1660 break;
1661 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001662 case dwarf::DW_AT_ranges: {
1663 // DW_AT_range Value encodes offset in debug_range section.
1664 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001665
1666 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1667 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1668 V->getValue(),
1669 4);
1670 } else {
1671 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1672 V->getValue(),
1673 DwarfDebugRangeSectionSym,
1674 4);
1675 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001676 break;
1677 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001678 case dwarf::DW_AT_location: {
1679 if (UseDotDebugLocEntry.count(Die) != 0) {
1680 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbar83320a02011-03-16 22:16:39 +00001681 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patelc3f5f782010-05-25 23:40:22 +00001682 } else
1683 Values[i]->EmitValue(Asm, Form);
1684 break;
1685 }
Devang Patel2a361602010-09-29 19:08:08 +00001686 case dwarf::DW_AT_accessibility: {
1687 if (Asm->isVerbose()) {
1688 DIEInteger *V = cast<DIEInteger>(Values[i]);
1689 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1690 }
1691 Values[i]->EmitValue(Asm, Form);
1692 break;
1693 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001694 default:
1695 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001696 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001697 break;
1698 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001699 }
1700
1701 // Emit the DIE children if any.
1702 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1703 const std::vector<DIE *> &Children = Die->getChildren();
1704
1705 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001706 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001707
Chris Lattner3f53c832010-04-04 18:52:31 +00001708 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001709 Asm->OutStreamer.AddComment("End Of Children Mark");
1710 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001711 }
1712}
1713
Devang Patel8a241142009-12-09 18:24:21 +00001714/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001715///
Devang Patel8a241142009-12-09 18:24:21 +00001716void DwarfDebug::emitDebugInfo() {
1717 // Start debug info section.
1718 Asm->OutStreamer.SwitchSection(
1719 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001720 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1721 E = CUMap.end(); I != E; ++I) {
1722 CompileUnit *TheCU = I->second;
1723 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001724
Devang Patel163a9f72010-05-10 22:49:55 +00001725 // Emit the compile units header.
1726 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1727 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001728
Devang Patel163a9f72010-05-10 22:49:55 +00001729 // Emit size of content not including length itself
1730 unsigned ContentSize = Die->getSize() +
1731 sizeof(int16_t) + // DWARF version number
1732 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001733 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001734
Devang Patel163a9f72010-05-10 22:49:55 +00001735 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1736 Asm->EmitInt32(ContentSize);
1737 Asm->OutStreamer.AddComment("DWARF version number");
1738 Asm->EmitInt16(dwarf::DWARF_VERSION);
1739 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1740 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1741 DwarfAbbrevSectionSym);
1742 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1743 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001744
Devang Patel163a9f72010-05-10 22:49:55 +00001745 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001746 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1747 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001748}
1749
Devang Patel2c4ceb12009-11-21 02:48:08 +00001750/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001751///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001752void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001753 // Check to see if it is worth the effort.
1754 if (!Abbreviations.empty()) {
1755 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001756 Asm->OutStreamer.SwitchSection(
1757 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001758
Chris Lattnerc0215722010-04-04 19:25:43 +00001759 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001760
1761 // For each abbrevation.
1762 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1763 // Get abbreviation data
1764 const DIEAbbrev *Abbrev = Abbreviations[i];
1765
1766 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001767 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001768
1769 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001770 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001771 }
1772
1773 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001774 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001775
Chris Lattnerc0215722010-04-04 19:25:43 +00001776 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001777 }
1778}
1779
Devang Patel2c4ceb12009-11-21 02:48:08 +00001780/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001781/// the line matrix.
1782///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001783void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001784 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001785 Asm->OutStreamer.AddComment("Extended Op");
1786 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001787
Chris Lattner233f52b2010-03-09 23:52:58 +00001788 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001789 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001790 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1791 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1792
1793 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001794
Chris Lattnerc0215722010-04-04 19:25:43 +00001795 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001796 Asm->getTargetData().getPointerSize(),
1797 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001798
1799 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001800 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1801 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001802 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001803 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001804}
1805
Devang Patel8a241142009-12-09 18:24:21 +00001806/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1807///
1808void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00001809 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1810 E = CUMap.end(); I != E; ++I) {
1811 CompileUnit *TheCU = I->second;
1812 // Start the dwarf pubnames section.
1813 Asm->OutStreamer.SwitchSection(
1814 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001815
Devang Patel163a9f72010-05-10 22:49:55 +00001816 Asm->OutStreamer.AddComment("Length of Public Names Info");
1817 Asm->EmitLabelDifference(
1818 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1819 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001820
Devang Patel163a9f72010-05-10 22:49:55 +00001821 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1822 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001823
Devang Patel163a9f72010-05-10 22:49:55 +00001824 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001825 Asm->EmitInt16(dwarf::DWARF_VERSION);
1826
Devang Patel163a9f72010-05-10 22:49:55 +00001827 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001828 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00001829 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001830
Devang Patel163a9f72010-05-10 22:49:55 +00001831 Asm->OutStreamer.AddComment("Compilation Unit Length");
1832 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1833 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1834 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001835
Devang Patel163a9f72010-05-10 22:49:55 +00001836 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1837 for (StringMap<DIE*>::const_iterator
1838 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1839 const char *Name = GI->getKeyData();
1840 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001841
Devang Patel163a9f72010-05-10 22:49:55 +00001842 Asm->OutStreamer.AddComment("DIE offset");
1843 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001844
Devang Patel163a9f72010-05-10 22:49:55 +00001845 if (Asm->isVerbose())
1846 Asm->OutStreamer.AddComment("External Name");
1847 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1848 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001849
Devang Patel163a9f72010-05-10 22:49:55 +00001850 Asm->OutStreamer.AddComment("End Mark");
1851 Asm->EmitInt32(0);
1852 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001853 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00001854 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001855}
1856
Devang Patel193f7202009-11-24 01:14:22 +00001857void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001858 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1859 E = CUMap.end(); I != E; ++I) {
1860 CompileUnit *TheCU = I->second;
1861 // Start the dwarf pubnames section.
1862 Asm->OutStreamer.SwitchSection(
1863 Asm->getObjFileLowering().getDwarfPubTypesSection());
1864 Asm->OutStreamer.AddComment("Length of Public Types Info");
1865 Asm->EmitLabelDifference(
1866 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1867 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001868
Devang Patel163a9f72010-05-10 22:49:55 +00001869 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1870 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001871
Devang Patel163a9f72010-05-10 22:49:55 +00001872 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1873 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001874
Devang Patel163a9f72010-05-10 22:49:55 +00001875 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1876 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1877 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001878
Devang Patel163a9f72010-05-10 22:49:55 +00001879 Asm->OutStreamer.AddComment("Compilation Unit Length");
1880 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1881 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1882 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001883
Devang Patel163a9f72010-05-10 22:49:55 +00001884 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1885 for (StringMap<DIE*>::const_iterator
1886 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1887 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001888 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001889
Devang Patel163a9f72010-05-10 22:49:55 +00001890 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1891 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001892
Devang Patel163a9f72010-05-10 22:49:55 +00001893 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1894 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1895 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001896
Devang Patel163a9f72010-05-10 22:49:55 +00001897 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001898 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001899 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1900 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001901 }
Devang Patel193f7202009-11-24 01:14:22 +00001902}
1903
Devang Patel2c4ceb12009-11-21 02:48:08 +00001904/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001905///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001906void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001907 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001908 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001909
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001910 // Start the dwarf str section.
1911 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001912 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001913
Chris Lattnerbc733f52010-03-13 02:17:42 +00001914 // Get all of the string pool entries and put them in an array by their ID so
1915 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001916 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001917 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001918
Chris Lattnerbc733f52010-03-13 02:17:42 +00001919 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1920 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1921 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001922
Chris Lattnerbc733f52010-03-13 02:17:42 +00001923 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001924
Chris Lattnerbc733f52010-03-13 02:17:42 +00001925 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001926 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001927 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001928
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001929 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001930 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001931 }
1932}
1933
Devang Patel2c4ceb12009-11-21 02:48:08 +00001934/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001935///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001936void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001937 if (DotDebugLocEntries.empty())
1938 return;
1939
Devang Patel6c3ea902011-02-04 22:57:18 +00001940 for (SmallVector<DotDebugLocEntry, 4>::iterator
1941 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1942 I != E; ++I) {
1943 DotDebugLocEntry &Entry = *I;
1944 if (I + 1 != DotDebugLocEntries.end())
1945 Entry.Merge(I+1);
1946 }
1947
Daniel Dunbar83320a02011-03-16 22:16:39 +00001948 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001949 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00001950 Asm->getObjFileLowering().getDwarfLocSection());
1951 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00001952 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1953 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001954 for (SmallVector<DotDebugLocEntry, 4>::iterator
1955 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00001956 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00001957 DotDebugLocEntry &Entry = *I;
1958 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00001959 if (Entry.isEmpty()) {
1960 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1961 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00001962 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00001963 } else {
1964 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1965 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00001966 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001967 Asm->OutStreamer.AddComment("Loc expr size");
1968 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1969 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1970 Asm->EmitLabelDifference(end, begin, 2);
1971 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00001972 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00001973 DIBasicType BTy(DV.getType());
1974 if (BTy.Verify() &&
1975 (BTy.getEncoding() == dwarf::DW_ATE_signed
1976 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1977 Asm->OutStreamer.AddComment("DW_OP_consts");
1978 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00001979 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001980 } else {
1981 Asm->OutStreamer.AddComment("DW_OP_constu");
1982 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00001983 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001984 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001985 } else if (Entry.isLocation()) {
1986 if (!DV.hasComplexAddress())
1987 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00001988 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00001989 else {
1990 // Complex address entry.
1991 unsigned N = DV.getNumAddrElements();
1992 unsigned i = 0;
1993 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
1994 if (Entry.Loc.getOffset()) {
1995 i = 2;
1996 Asm->EmitDwarfRegOp(Entry.Loc);
1997 Asm->OutStreamer.AddComment("DW_OP_deref");
1998 Asm->EmitInt8(dwarf::DW_OP_deref);
1999 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2000 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2001 Asm->EmitSLEB128(DV.getAddrElement(1));
2002 } else {
2003 // If first address element is OpPlus then emit
2004 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2005 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2006 Asm->EmitDwarfRegOp(Loc);
2007 i = 2;
2008 }
2009 } else {
2010 Asm->EmitDwarfRegOp(Entry.Loc);
2011 }
2012
2013 // Emit remaining complex address elements.
2014 for (; i < N; ++i) {
2015 uint64_t Element = DV.getAddrElement(i);
2016 if (Element == DIBuilder::OpPlus) {
2017 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2018 Asm->EmitULEB128(DV.getAddrElement(++i));
2019 } else if (Element == DIBuilder::OpDeref)
2020 Asm->EmitInt8(dwarf::DW_OP_deref);
2021 else llvm_unreachable("unknown Opcode found in complex address");
2022 }
Devang Patelc26f5442011-04-28 02:22:40 +00002023 }
Devang Patelc26f5442011-04-28 02:22:40 +00002024 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002025 // else ... ignore constant fp. There is not any good way to
2026 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002027 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002028 }
2029 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002030}
2031
2032/// EmitDebugARanges - Emit visible names into a debug aranges section.
2033///
2034void DwarfDebug::EmitDebugARanges() {
2035 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002036 Asm->OutStreamer.SwitchSection(
2037 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002038}
2039
Devang Patel2c4ceb12009-11-21 02:48:08 +00002040/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002041///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002042void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002043 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002044 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002045 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002046 unsigned char Size = Asm->getTargetData().getPointerSize();
2047 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002048 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002049 I != E; ++I) {
2050 if (*I)
2051 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002052 else
Devang Pateleac9c072010-04-27 19:46:33 +00002053 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002054 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002055}
2056
Devang Patel2c4ceb12009-11-21 02:48:08 +00002057/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002058///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002059void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002060 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002061 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002062 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002063 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002064 }
2065}
2066
Devang Patel2c4ceb12009-11-21 02:48:08 +00002067/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002068/// Section Header:
2069/// 1. length of section
2070/// 2. Dwarf version number
2071/// 3. address size.
2072///
2073/// Entries (one "entry" for each function that was inlined):
2074///
2075/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2076/// otherwise offset into __debug_str for regular function name.
2077/// 2. offset into __debug_str section for regular function name.
2078/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2079/// instances for the function.
2080///
2081/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2082/// inlined instance; the die_offset points to the inlined_subroutine die in the
2083/// __debug_info section, and the low_pc is the starting address for the
2084/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002085void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002086 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002087 return;
2088
Devang Patel163a9f72010-05-10 22:49:55 +00002089 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002090 return;
2091
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002092 Asm->OutStreamer.SwitchSection(
2093 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002094
Chris Lattner233f52b2010-03-09 23:52:58 +00002095 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002096 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2097 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002098
Chris Lattnerc0215722010-04-04 19:25:43 +00002099 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002100
Chris Lattner233f52b2010-03-09 23:52:58 +00002101 Asm->OutStreamer.AddComment("Dwarf Version");
2102 Asm->EmitInt16(dwarf::DWARF_VERSION);
2103 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002104 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002105
Devang Patele9f8f5e2010-05-07 20:54:48 +00002106 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002107 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002108
Devang Patele9f8f5e2010-05-07 20:54:48 +00002109 const MDNode *Node = *I;
2110 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002111 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002112 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002113 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002114 StringRef LName = SP.getLinkageName();
2115 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002116
Chris Lattner233f52b2010-03-09 23:52:58 +00002117 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002118 if (LName.empty()) {
2119 Asm->OutStreamer.EmitBytes(Name, 0);
2120 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002121 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00002122 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2123 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002124
Chris Lattner233f52b2010-03-09 23:52:58 +00002125 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002126 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002127 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002128
Devang Patel53bb5c92009-11-10 23:06:00 +00002129 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002130 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002131 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002132 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002133
Chris Lattner3f53c832010-04-04 18:52:31 +00002134 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002135 Asm->OutStreamer.EmitSymbolValue(LI->first,
2136 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002137 }
2138 }
2139
Chris Lattnerc0215722010-04-04 19:25:43 +00002140 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002141}