blob: e8479c506fb23596f3620ffd150cef136ff1e705 [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"
Chris Lattner45111d12010-01-16 21:57:06 +000027#include "llvm/Target/Mangler.h"
Bill Wendling0310d762009-05-15 09:23:25 +000028#include "llvm/Target/TargetData.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000029#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000031#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000034#include "llvm/Analysis/DebugInfo.h"
Devang Patel0eea95d2011-02-24 18:49:30 +000035#include "llvm/Analysis/DIBuilder.h"
Devang Patel9a31f0f2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Devang Pateleac9c072010-04-27 19:46:33 +000039#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000042#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000043#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000044#include "llvm/Support/Timer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000045#include "llvm/Support/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000046using namespace llvm;
47
Devang Pateleac9c072010-04-27 19:46:33 +000048static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
49 cl::desc("Print DbgScope information for each machine instruction"));
50
Jim Grosbach1e20b962010-07-21 21:21:52 +000051static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000052 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000053 cl::desc("Disable debug info printing"));
54
Dan Gohman281d65d2010-05-07 01:08:53 +000055static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner7a2bdde2011-04-15 05:18:47 +000056 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman281d65d2010-05-07 01:08:53 +000057 cl::init(false));
58
Nick Lewyckya568d662010-10-26 00:51:57 +000059#ifndef NDEBUG
Devang Patel9a31f0f2010-10-25 20:45:32 +000060STATISTIC(BlocksWithoutLineNo, "Number of blocks without any line number");
Nick Lewyckya568d662010-10-26 00:51:57 +000061#endif
Devang Patel9a31f0f2010-10-25 20:45:32 +000062
Bill Wendling5f017e82010-04-07 09:28:04 +000063namespace {
64 const char *DWARFGroupName = "DWARF Emission";
65 const char *DbgTimerName = "DWARF Debug Writer";
66} // end anonymous namespace
67
Bill Wendling0310d762009-05-15 09:23:25 +000068//===----------------------------------------------------------------------===//
69
70/// Configuration values for initial hash set sizes (log2).
71///
Bill Wendling0310d762009-05-15 09:23:25 +000072static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000073
74namespace llvm {
75
Devang Patel3cbee302011-04-12 22:53:02 +000076DIType DbgVariable::getType() const {
77 DIType Ty = Var.getType();
78 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
79 // addresses instead.
80 if (Var.isBlockByrefVariable()) {
81 /* Byref variables, in Blocks, are declared by the programmer as
82 "SomeType VarName;", but the compiler creates a
83 __Block_byref_x_VarName struct, and gives the variable VarName
84 either the struct, or a pointer to the struct, as its type. This
85 is necessary for various behind-the-scenes things the compiler
86 needs to do with by-reference variables in blocks.
87
88 However, as far as the original *programmer* is concerned, the
89 variable should still have type 'SomeType', as originally declared.
90
91 The following function dives into the __Block_byref_x_VarName
92 struct to find the original type of the variable. This will be
93 passed back to the code generating the type for the Debug
94 Information Entry for the variable 'VarName'. 'VarName' will then
95 have the original type 'SomeType' in its debug information.
96
97 The original type 'SomeType' will be the type of the field named
98 'VarName' inside the __Block_byref_x_VarName struct.
99
100 NOTE: In order for this to not completely fail on the debugger
101 side, the Debug Information Entry for the variable VarName needs to
102 have a DW_AT_location that tells the debugger how to unwind through
103 the pointers and __Block_byref_x_VarName struct to find the actual
104 value of the variable. The function addBlockByrefType does this. */
105 DIType subType = Ty;
106 unsigned tag = Ty.getTag();
107
108 if (tag == dwarf::DW_TAG_pointer_type) {
109 DIDerivedType DTy = DIDerivedType(Ty);
110 subType = DTy.getTypeDerivedFrom();
111 }
112
113 DICompositeType blockStruct = DICompositeType(subType);
114 DIArray Elements = blockStruct.getTypeArray();
115
116 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
117 DIDescriptor Element = Elements.getElement(i);
118 DIDerivedType DT = DIDerivedType(Element);
119 if (getName() == DT.getName())
120 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000121 }
122 return Ty;
123 }
Devang Patel3cbee302011-04-12 22:53:02 +0000124 return Ty;
125}
Bill Wendling0310d762009-05-15 09:23:25 +0000126
127//===----------------------------------------------------------------------===//
Devang Pateleac9c072010-04-27 19:46:33 +0000128/// DbgRange - This is used to track range of instructions with identical
129/// debug info scope.
130///
131typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
132
133//===----------------------------------------------------------------------===//
Bill Wendling0310d762009-05-15 09:23:25 +0000134/// DbgScope - This class is used to track scope information.
135///
Devang Patelf76a3d62009-11-16 21:53:40 +0000136class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000137 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000138 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000139 // Location at which this scope is inlined.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000140 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000141 bool AbstractScope; // Abstract Scope
Devang Pateld38dd112009-10-01 18:25:23 +0000142 const MachineInstr *LastInsn; // Last instruction of this scope.
143 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Pateleac9c072010-04-27 19:46:33 +0000144 unsigned DFSIn, DFSOut;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000145 // Scopes defined in scope. Contents not owned.
146 SmallVector<DbgScope *, 4> Scopes;
147 // Variables declared in scope. Contents owned.
148 SmallVector<DbgVariable *, 8> Variables;
Devang Pateleac9c072010-04-27 19:46:33 +0000149 SmallVector<DbgRange, 4> Ranges;
Owen Anderson04c05f72009-06-24 22:53:20 +0000150 // Private state for dump()
151 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000152public:
Devang Patele9f8f5e2010-05-07 20:54:48 +0000153 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000154 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Pateleac9c072010-04-27 19:46:33 +0000155 LastInsn(0), FirstInsn(0),
156 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000157 virtual ~DbgScope();
158
159 // Accessors.
160 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000161 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000162 DIDescriptor getDesc() const { return Desc; }
Devang Patele9f8f5e2010-05-07 20:54:48 +0000163 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
164 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000165 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
Devang Patele03161c2010-08-09 18:51:29 +0000166 const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
Devang Pateleac9c072010-04-27 19:46:33 +0000167 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
168
169 /// openInsnRange - This scope covers instruction range starting from MI.
170 void openInsnRange(const MachineInstr *MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000171 if (!FirstInsn)
Devang Pateleac9c072010-04-27 19:46:33 +0000172 FirstInsn = MI;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000173
Devang Pateleac9c072010-04-27 19:46:33 +0000174 if (Parent)
175 Parent->openInsnRange(MI);
176 }
177
Jim Grosbach1e20b962010-07-21 21:21:52 +0000178 /// extendInsnRange - Extend the current instruction range covered by
Devang Pateleac9c072010-04-27 19:46:33 +0000179 /// this scope.
180 void extendInsnRange(const MachineInstr *MI) {
181 assert (FirstInsn && "MI Range is not open!");
182 LastInsn = MI;
183 if (Parent)
184 Parent->extendInsnRange(MI);
185 }
186
187 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
188 /// until now. This is used when a new scope is encountered while walking
189 /// machine instructions.
190 void closeInsnRange(DbgScope *NewScope = NULL) {
191 assert (LastInsn && "Last insn missing!");
192 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000193 FirstInsn = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +0000194 LastInsn = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000195 // If Parent dominates NewScope then do not close Parent's instruction
Devang Pateleac9c072010-04-27 19:46:33 +0000196 // range.
197 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
198 Parent->closeInsnRange(NewScope);
199 }
200
Devang Patel53bb5c92009-11-10 23:06:00 +0000201 void setAbstractScope() { AbstractScope = true; }
202 bool isAbstractScope() const { return AbstractScope; }
Devang Pateleac9c072010-04-27 19:46:33 +0000203
204 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
205 unsigned getDFSOut() const { return DFSOut; }
206 void setDFSOut(unsigned O) { DFSOut = O; }
207 unsigned getDFSIn() const { return DFSIn; }
208 void setDFSIn(unsigned I) { DFSIn = I; }
209 bool dominates(const DbgScope *S) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000210 if (S == this)
Devang Pateleac9c072010-04-27 19:46:33 +0000211 return true;
212 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
213 return true;
214 return false;
215 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000216
Devang Patel2c4ceb12009-11-21 02:48:08 +0000217 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000218 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000219 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000220
Devang Patel2c4ceb12009-11-21 02:48:08 +0000221 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000222 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000223 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000224
Bill Wendling0310d762009-05-15 09:23:25 +0000225#ifndef NDEBUG
226 void dump() const;
227#endif
228};
Devang Pateleac9c072010-04-27 19:46:33 +0000229
Chris Lattnerea761862010-04-05 04:09:20 +0000230} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000231
232#ifndef NDEBUG
233void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000234 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000235 err.indent(IndentLevel);
Devang Patele9f8f5e2010-05-07 20:54:48 +0000236 const MDNode *N = Desc;
Devang Patel53bb5c92009-11-10 23:06:00 +0000237 N->dump();
Devang Patel53bb5c92009-11-10 23:06:00 +0000238 if (AbstractScope)
239 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000240
241 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000242 if (!Scopes.empty())
243 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000244 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
245 if (Scopes[i] != this)
246 Scopes[i]->dump();
247
248 IndentLevel -= 2;
249}
250#endif
251
Bill Wendling0310d762009-05-15 09:23:25 +0000252DbgScope::~DbgScope() {
Bill Wendling0310d762009-05-15 09:23:25 +0000253 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
254 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000255}
256
Chris Lattner49cd6642010-04-05 05:11:15 +0000257DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000258 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000259 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelf2548ca2010-04-16 23:33:45 +0000260 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000261 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000262
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000263 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
264 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000265 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000266 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman03c3dc72010-06-18 15:56:31 +0000267 {
268 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
269 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000270 }
Bill Wendling0310d762009-05-15 09:23:25 +0000271}
272DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000273}
274
Chris Lattnerbc733f52010-03-13 02:17:42 +0000275MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
276 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
277 if (Entry.first) return Entry.first;
278
279 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000280 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000281}
282
283
Devang Patel2c4ceb12009-11-21 02:48:08 +0000284/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000285///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000286void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000287 // Profile the node so that we can make it unique.
288 FoldingSetNodeID ID;
289 Abbrev.Profile(ID);
290
291 // Check the set for priors.
292 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
293
294 // If it's newly added.
295 if (InSet == &Abbrev) {
296 // Add to abbreviation list.
297 Abbreviations.push_back(&Abbrev);
298
299 // Assign the vector position + 1 as its number.
300 Abbrev.setNumber(Abbreviations.size());
301 } else {
302 // Assign existing abbreviation number.
303 Abbrev.setNumber(InSet->getNumber());
304 }
305}
306
Jim Grosbach1e20b962010-07-21 21:21:52 +0000307/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000308/// printer to not emit usual symbol prefix before the symbol name is used then
309/// return linkage name after skipping this special LLVM prefix.
310static StringRef getRealLinkageName(StringRef LinkageName) {
311 char One = '\1';
312 if (LinkageName.startswith(StringRef(&One, 1)))
313 return LinkageName.substr(1);
314 return LinkageName;
315}
316
Devang Patelffe966c2009-12-14 16:18:45 +0000317/// createSubprogramDIE - Create new DIE using SP.
Devang Patelee70fa72010-09-27 23:15:27 +0000318DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel163a9f72010-05-10 22:49:55 +0000319 CompileUnit *SPCU = getCompileUnit(SP);
320 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +0000321 if (SPDie)
322 return SPDie;
323
324 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel5e06bb82011-04-22 23:10:17 +0000325
326 // DW_TAG_inlined_subroutine may refer to this DIE.
327 SPCU->insertDIE(SP, SPDie);
328
329 // Add to context owner.
330 SPCU->addToContextOwner(SPDie, SP.getContext());
331
332 // Add function template parameters.
333 SPCU->addTemplateParams(*SPDie, SP.getTemplateParams());
334
Devang Patelee4a3ab2011-05-03 21:50:34 +0000335 StringRef LinkageName = SP.getLinkageName();
336 if (!LinkageName.empty())
337 SPCU->addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
338 getRealLinkageName(LinkageName));
339
Devang Patel5e06bb82011-04-22 23:10:17 +0000340 // If this DIE is going to refer declaration info using AT_specification
341 // then there is no need to add other attributes.
342 if (SP.getFunctionDeclaration().isSubprogram())
343 return SPDie;
344
Devang Patel1eac3e72010-03-02 17:58:15 +0000345 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +0000346 if (!SP.getName().empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000347 SPCU->addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
348 SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +0000349
Devang Patel3cbee302011-04-12 22:53:02 +0000350 SPCU->addSourceLine(SPDie, SP);
Bill Wendling0310d762009-05-15 09:23:25 +0000351
Devang Patel7b172c62010-10-07 22:03:01 +0000352 if (SP.isPrototyped())
Devang Patel3cbee302011-04-12 22:53:02 +0000353 SPCU->addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Devang Patel5e06bb82011-04-22 23:10:17 +0000354
Bill Wendling0310d762009-05-15 09:23:25 +0000355 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000356 DICompositeType SPTy = SP.getType();
357 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +0000358 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +0000359
Devang Patel3c91b052010-03-08 20:52:55 +0000360 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel3cbee302011-04-12 22:53:02 +0000361 SPCU->addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000362 else
Devang Patel3cbee302011-04-12 22:53:02 +0000363 SPCU->addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000364
Devang Patel5d11eb02009-12-03 19:11:07 +0000365 unsigned VK = SP.getVirtuality();
366 if (VK) {
Devang Patel3cbee302011-04-12 22:53:02 +0000367 SPCU->addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
368 DIEBlock *Block = SPCU->getDIEBlock();
369 SPCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
370 SPCU->addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
371 SPCU->addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000372 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +0000373 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +0000374 }
375
Devang Patelee70fa72010-09-27 23:15:27 +0000376 if (!SP.isDefinition()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000377 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
378
Bill Wendling0310d762009-05-15 09:23:25 +0000379 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000380 // be handled while processing variables.
381 DICompositeType SPTy = SP.getType();
382 DIArray Args = SPTy.getTypeArray();
383 unsigned SPTag = SPTy.getTag();
384
Bill Wendling0310d762009-05-15 09:23:25 +0000385 if (SPTag == dwarf::DW_TAG_subroutine_type)
386 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
387 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +0000388 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patel3cbee302011-04-12 22:53:02 +0000389 SPCU->addType(Arg, ATy);
Devang Patelb4645642010-02-06 01:02:37 +0000390 if (ATy.isArtificial())
Devang Patel3cbee302011-04-12 22:53:02 +0000391 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000392 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000393 }
394 }
395
Devang Patel4e0d19d2010-02-03 19:57:19 +0000396 if (SP.isArtificial())
Devang Patel3cbee302011-04-12 22:53:02 +0000397 SPCU->addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel4e0d19d2010-02-03 19:57:19 +0000398
Devang Patelccff8122010-04-30 19:38:23 +0000399 if (!SP.isLocalToUnit())
Devang Patel3cbee302011-04-12 22:53:02 +0000400 SPCU->addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000401
Devang Patelccff8122010-04-30 19:38:23 +0000402 if (SP.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000403 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Devang Patelccff8122010-04-30 19:38:23 +0000404
Jim Grosbach91729002010-07-21 23:03:52 +0000405 if (unsigned isa = Asm->getISAEncoding()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000406 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
Jim Grosbach91729002010-07-21 23:03:52 +0000407 }
408
Bill Wendling0310d762009-05-15 09:23:25 +0000409 return SPDie;
410}
411
Devang Patele9f8f5e2010-05-07 20:54:48 +0000412DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +0000413 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +0000414
415 DbgScope *AScope = AbstractScopes.lookup(N);
416 if (AScope)
417 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000418
Devang Patel53bb5c92009-11-10 23:06:00 +0000419 DbgScope *Parent = NULL;
420
421 DIDescriptor Scope(N);
422 if (Scope.isLexicalBlock()) {
423 DILexicalBlock DB(N);
424 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +0000425 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +0000426 }
427
428 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
429
430 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000431 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +0000432 AScope->setAbstractScope();
433 AbstractScopes[N] = AScope;
434 if (DIDescriptor(N).isSubprogram())
435 AbstractScopesList.push_back(AScope);
436 return AScope;
437}
Devang Patelaf9e8472009-10-01 20:31:14 +0000438
Devang Patel5f094002010-04-06 23:53:48 +0000439/// isSubprogramContext - Return true if Context is either a subprogram
440/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000441static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +0000442 if (!Context)
443 return false;
444 DIDescriptor D(Context);
445 if (D.isSubprogram())
446 return true;
447 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000448 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +0000449 return false;
450}
451
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000452/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000453/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
454/// If there are global variables in this scope then create and insert
455/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000456DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000457 CompileUnit *SPCU = getCompileUnit(SPNode);
458 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000459
Chris Lattnerd38fee82010-04-05 00:13:49 +0000460 assert(SPDie && "Unable to find subprogram DIE!");
461 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000462
Devang Patel5e06bb82011-04-22 23:10:17 +0000463 DISubprogram SPDecl = SP.getFunctionDeclaration();
464 if (SPDecl.isSubprogram())
465 // Refer function declaration directly.
Devang Patel3cbee302011-04-12 22:53:02 +0000466 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Patel5e06bb82011-04-22 23:10:17 +0000467 createSubprogramDIE(SPDecl));
468 else {
469 // There is not any need to generate specification DIE for a function
470 // defined at compile unit level. If a function is defined inside another
471 // function then gdb prefers the definition at top level and but does not
472 // expect specification DIE in parent function. So avoid creating
473 // specification DIE for a function defined inside a function.
474 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
475 !SP.getContext().isFile() &&
476 !isSubprogramContext(SP.getContext())) {
477 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
478
479 // Add arguments.
480 DICompositeType SPTy = SP.getType();
481 DIArray Args = SPTy.getTypeArray();
482 unsigned SPTag = SPTy.getTag();
483 if (SPTag == dwarf::DW_TAG_subroutine_type)
484 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
485 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
486 DIType ATy = DIType(DIType(Args.getElement(i)));
487 SPCU->addType(Arg, ATy);
488 if (ATy.isArtificial())
489 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
490 SPDie->addChild(Arg);
491 }
492 DIE *SPDeclDie = SPDie;
493 SPDie = new DIE(dwarf::DW_TAG_subprogram);
494 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
495 SPDeclDie);
496 SPCU->addDie(SPDie);
497 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000498 }
Devang Patel8aa61472010-07-07 22:20:57 +0000499 // Pick up abstract subprogram DIE.
500 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
501 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000502 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
503 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000504 SPCU->addDie(SPDie);
505 }
506
Devang Patel3cbee302011-04-12 22:53:02 +0000507 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
508 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
509 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
510 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000511 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
512 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000513 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000514
Chris Lattnerd38fee82010-04-05 00:13:49 +0000515 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000516}
517
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000518/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000519/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
520DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000521
522 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
523 if (Scope->isAbstractScope())
524 return ScopeDIE;
525
526 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
527 if (Ranges.empty())
528 return 0;
529
Devang Patel3cbee302011-04-12 22:53:02 +0000530 CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
Devang Pateleac9c072010-04-27 19:46:33 +0000531 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
532 if (Ranges.size() > 1) {
533 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000534 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000535 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000536 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
537 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
Devang Pateleac9c072010-04-27 19:46:33 +0000538 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
539 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000540 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
541 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000542 }
543 DebugRangeSymbols.push_back(NULL);
544 DebugRangeSymbols.push_back(NULL);
545 return ScopeDIE;
546 }
547
Devang Patelc3f5f782010-05-25 23:40:22 +0000548 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
549 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000550
Devang Patelc3f5f782010-05-25 23:40:22 +0000551 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000552
Chris Lattnerb7db7332010-03-09 01:58:53 +0000553 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
554 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000555
Devang Patel3cbee302011-04-12 22:53:02 +0000556 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
557 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000558
559 return ScopeDIE;
560}
561
Devang Patel2c4ceb12009-11-21 02:48:08 +0000562/// constructInlinedScopeDIE - This scope represents inlined body of
563/// a function. Construct DIE to represent this concrete inlined copy
564/// of the function.
565DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000566
567 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000568 assert (Ranges.empty() == false
Devang Pateleac9c072010-04-27 19:46:33 +0000569 && "DbgScope does not have instruction markers!");
570
571 // FIXME : .debug_inlined section specification does not clearly state how
572 // to emit inlined scope that is split into multiple instruction ranges.
573 // For now, use first instruction range and emit low_pc/high_pc pair and
574 // corresponding .debug_inlined section entry for this pair.
575 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000576 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
577 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000578
Devang Patel0afbf232010-07-08 22:39:20 +0000579 if (StartLabel == 0 || EndLabel == 0) {
Devang Pateleac9c072010-04-27 19:46:33 +0000580 assert (0 && "Unexpected Start and End labels for a inlined scope!");
581 return 0;
582 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000583 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000584 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000585 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000586 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000587
Devang Patel3c91b052010-03-08 20:52:55 +0000588 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +0000589 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000590 DIScope DS(Scope->getScopeNode());
Devang Patel2db49d72010-05-07 18:11:54 +0000591 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +0000592 CompileUnit *TheCU = getCompileUnit(InlinedSP);
593 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Pateld96efb82011-05-05 17:54:26 +0000594 if (!OriginDIE) {
595 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
596 return NULL;
597 }
598 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000599 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
600 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000601
Devang Patel3cbee302011-04-12 22:53:02 +0000602 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
603 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000604
605 InlinedSubprogramDIEs.insert(OriginDIE);
606
607 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000608 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000609 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000610
611 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000612 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000613 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000614 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000615 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000616 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000617
Devang Patel53bb5c92009-11-10 23:06:00 +0000618 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000619 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
620 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000621
622 return ScopeDIE;
623}
624
Devang Patel2c4ceb12009-11-21 02:48:08 +0000625
626/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +0000627DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000628 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000629 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +0000630 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +0000631
632 // Translate tag to proper Dwarf tag. The result variable is dropped for
633 // now.
634 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +0000635 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +0000636 case dwarf::DW_TAG_return_variable:
637 return NULL;
638 case dwarf::DW_TAG_arg_variable:
639 Tag = dwarf::DW_TAG_formal_parameter;
640 break;
641 case dwarf::DW_TAG_auto_variable: // fall thru
642 default:
643 Tag = dwarf::DW_TAG_variable;
644 break;
645 }
646
647 // Define variable debug information entry.
648 DIE *VariableDie = new DIE(Tag);
Devang Patel9341d102011-04-25 23:05:21 +0000649 CompileUnit *VariableCU = getCompileUnit(DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000650 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +0000651 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
652 V2AVI = VarToAbstractVarMap.find(DV);
653 if (V2AVI != VarToAbstractVarMap.end())
654 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000655
Devang Patel26c1e562010-05-20 16:36:41 +0000656 if (AbsDIE)
Devang Patel9341d102011-04-25 23:05:21 +0000657 VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel3cbee302011-04-12 22:53:02 +0000658 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000659 else {
Devang Patel9341d102011-04-25 23:05:21 +0000660 VariableCU->addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
661 Name);
662 VariableCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000663
664 // Add variable type.
Devang Patel9341d102011-04-25 23:05:21 +0000665 VariableCU->addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +0000666 }
667
Devang Patel8bd11de2010-08-09 21:01:39 +0000668 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel9341d102011-04-25 23:05:21 +0000669 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
670 dwarf::DW_FORM_flag, 1);
Devang Patel3cf763d2010-09-29 23:07:21 +0000671 else if (DIVariable(DV->getVariable()).isArtificial())
Devang Patel9341d102011-04-25 23:05:21 +0000672 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
673 dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +0000674
675 if (Scope->isAbstractScope()) {
676 DV->setDIE(VariableDie);
677 return VariableDie;
678 }
679
680 // Add variable address.
681
682 unsigned Offset = DV->getDotDebugLocOffset();
683 if (Offset != ~0U) {
Devang Patel9341d102011-04-25 23:05:21 +0000684 VariableCU->addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
Devang Patelc3f5f782010-05-25 23:40:22 +0000685 Asm->GetTempSymbol("debug_loc", Offset));
686 DV->setDIE(VariableDie);
687 UseDotDebugLocEntry.insert(VariableDie);
688 return VariableDie;
689 }
690
691 // Check if variable is described by a DBG_VALUE instruction.
692 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
693 DbgVariableToDbgInstMap.find(DV);
694 if (DVI != DbgVariableToDbgInstMap.end()) {
695 const MachineInstr *DVInsn = DVI->second;
Devang Patelc3f5f782010-05-25 23:40:22 +0000696 bool updated = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000697 // FIXME : Handle getNumOperands != 3
Devang Patelc3f5f782010-05-25 23:40:22 +0000698 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +0000699 if (DVInsn->getOperand(0).isReg()) {
700 const MachineOperand RegOp = DVInsn->getOperand(0);
701 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
702 if (DVInsn->getOperand(1).isImm() &&
703 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patele1cdf842011-04-27 22:45:24 +0000704 unsigned FrameReg = 0;
705 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
706 int Offset =
707 TFI->getFrameIndexReference(*Asm->MF,
708 DVInsn->getOperand(1).getImm(),
709 FrameReg);
710 MachineLocation Location(FrameReg, Offset);
711 VariableCU->addVariableAddress(DV, VariableDie, Location);
712
713 } else if (RegOp.getReg())
714 VariableCU->addVariableAddress(DV, VariableDie,
715 MachineLocation(RegOp.getReg()));
716 updated = true;
Devang Patel0b48ead2010-08-31 22:22:42 +0000717 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000718 else if (DVInsn->getOperand(0).isImm())
Devang Patel9341d102011-04-25 23:05:21 +0000719 updated = VariableCU->addConstantValue(VariableDie,
720 DVInsn->getOperand(0));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000721 else if (DVInsn->getOperand(0).isFPImm())
722 updated =
Devang Patel9341d102011-04-25 23:05:21 +0000723 VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patelc3f5f782010-05-25 23:40:22 +0000724 } else {
Devang Patele1cdf842011-04-27 22:45:24 +0000725 VariableCU->addVariableAddress(DV, VariableDie,
726 Asm->getDebugValueLocation(DVInsn));
727 updated = true;
Devang Patelc3f5f782010-05-25 23:40:22 +0000728 }
729 if (!updated) {
730 // If variableDie is not updated then DBG_VALUE instruction does not
731 // have valid variable info.
732 delete VariableDie;
733 return NULL;
734 }
735 DV->setDIE(VariableDie);
736 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000737 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000738
739 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +0000740 int FI = 0;
Devang Patele1cdf842011-04-27 22:45:24 +0000741 if (findVariableFrameIndex(DV, &FI)) {
742 unsigned FrameReg = 0;
743 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
744 int Offset =
745 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
746 MachineLocation Location(FrameReg, Offset);
747 VariableCU->addVariableAddress(DV, VariableDie, Location);
748 }
749
Devang Patel53bb5c92009-11-10 23:06:00 +0000750 DV->setDIE(VariableDie);
751 return VariableDie;
752
753}
Devang Patel2c4ceb12009-11-21 02:48:08 +0000754
Devang Patel3cbee302011-04-12 22:53:02 +0000755void CompileUnit::addPubTypes(DISubprogram SP) {
Devang Patel193f7202009-11-24 01:14:22 +0000756 DICompositeType SPTy = SP.getType();
757 unsigned SPTag = SPTy.getTag();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000758 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel193f7202009-11-24 01:14:22 +0000759 return;
760
761 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +0000762 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +0000763 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +0000764 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +0000765 continue;
766 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +0000767 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +0000768 && !CATy.isForwardDecl()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000769 if (DIEEntry *Entry = getDIEEntry(CATy))
770 addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +0000771 }
772 }
773}
774
Devang Patel2c4ceb12009-11-21 02:48:08 +0000775/// constructScopeDIE - Construct a DIE for this scope.
776DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000777 if (!Scope || !Scope->getScopeNode())
778 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000779
Devang Patel5bc9fec2011-02-19 01:31:27 +0000780 SmallVector <DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000781
782 // Collect arguments for current function.
783 if (Scope == CurrentFnDbgScope)
784 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
785 if (DbgVariable *ArgDV = CurrentFnArguments[i])
786 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
787 Children.push_back(Arg);
788
Devang Patel5bc9fec2011-02-19 01:31:27 +0000789 // Collect lexical scope childrens first.
790 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
791 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
792 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
793 Children.push_back(Variable);
794 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
795 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
796 if (DIE *Nested = constructScopeDIE(Scopes[j]))
797 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000798 DIScope DS(Scope->getScopeNode());
799 DIE *ScopeDIE = NULL;
800 if (Scope->getInlinedAt())
801 ScopeDIE = constructInlinedScopeDIE(Scope);
802 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000803 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000804 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +0000805 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000806 // Note down abstract DIE.
807 if (ScopeDIE)
808 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
809 }
Devang Patel3c91b052010-03-08 20:52:55 +0000810 else
Devang Patel2db49d72010-05-07 18:11:54 +0000811 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000812 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000813 else {
814 // There is no need to emit empty lexical block DIE.
815 if (Children.empty())
816 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000817 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000818 }
819
Devang Patelaead63c2010-03-29 22:59:58 +0000820 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000821
Devang Patel5bc9fec2011-02-19 01:31:27 +0000822 // Add children
823 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
824 E = Children.end(); I != E; ++I)
825 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000826
Jim Grosbach1e20b962010-07-21 21:21:52 +0000827 if (DS.isSubprogram())
Devang Patel3cbee302011-04-12 22:53:02 +0000828 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000829
Devang Patel193f7202009-11-24 01:14:22 +0000830 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000831}
832
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000833/// GetOrCreateSourceID - Look up the source id with the given directory and
834/// source file names. If none currently exists, create a new id and insert it
835/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
836/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +0000837
Devang Patel23670e52011-03-24 20:30:50 +0000838unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
839 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000840 // If FE did not provide a file name, then assume stdin.
841 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000842 return GetOrCreateSourceID("<stdin>", StringRef());
843
844 // MCStream expects full path name as filename.
845 if (!DirName.empty() && !FileName.startswith("/")) {
846 std::string FullPathName(DirName.data());
847 if (!DirName.endswith("/"))
848 FullPathName += "/";
849 FullPathName += FileName.data();
850 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
851 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
852 }
Devang Patel1905a182010-09-16 20:57:49 +0000853
Rafael Espindola5c055632010-11-18 02:04:25 +0000854 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
855 if (Entry.getValue())
856 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000857
Rafael Espindola5c055632010-11-18 02:04:25 +0000858 unsigned SrcId = SourceIdMap.size();
859 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000860
Rafael Espindola5c055632010-11-18 02:04:25 +0000861 // Print out a .file directive to specify files for .loc directives.
Devang Patel23670e52011-03-24 20:30:50 +0000862 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000863
864 return SrcId;
865}
866
Devang Patel6404e4e2009-12-15 19:16:48 +0000867/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Devang Patel3cbee302011-04-12 22:53:02 +0000868DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
869 DIE *NDie = getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +0000870 if (NDie)
871 return NDie;
872 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel3cbee302011-04-12 22:53:02 +0000873 insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +0000874 if (!NS.getName().empty())
875 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patel81625742010-08-09 20:20:05 +0000876 addSourceLine(NDie, NS);
Devang Patel6404e4e2009-12-15 19:16:48 +0000877 addToContextOwner(NDie, NS.getContext());
878 return NDie;
879}
880
Jim Grosbach1e20b962010-07-21 21:21:52 +0000881/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000882/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000883void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000884 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000885 StringRef FN = DIUnit.getFilename();
886 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000887 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000888
889 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000890 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
891 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
892 DIUnit.getProducer());
893 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
894 DIUnit.getLanguage());
895 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000896 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
897 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000898 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000899 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000900 // compile unit in debug_line section.
Rafael Espindola597a7662011-05-04 17:44:06 +0000901 if(Asm->MAI->doesDwarfRequireRelocationForStmtList())
902 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000903 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000904 else
Devang Patel3cbee302011-04-12 22:53:02 +0000905 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000906
Devang Patel65dbc902009-11-25 17:36:49 +0000907 if (!Dir.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000908 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000909 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000910 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000911
Devang Patel65dbc902009-11-25 17:36:49 +0000912 StringRef Flags = DIUnit.getFlags();
913 if (!Flags.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000914 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
915
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000916 unsigned RVer = DIUnit.getRunTimeVersion();
917 if (RVer)
Devang Patel3cbee302011-04-12 22:53:02 +0000918 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000919 dwarf::DW_FORM_data1, RVer);
920
Devang Patel163a9f72010-05-10 22:49:55 +0000921 if (!FirstCU)
922 FirstCU = NewCU;
923 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000924}
925
Devang Patel163a9f72010-05-10 22:49:55 +0000926/// getCompielUnit - Get CompileUnit DIE.
927CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
928 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
929 DIDescriptor D(N);
930 const MDNode *CUNode = NULL;
931 if (D.isCompileUnit())
932 CUNode = N;
933 else if (D.isSubprogram())
934 CUNode = DISubprogram(N).getCompileUnit();
935 else if (D.isType())
936 CUNode = DIType(N).getCompileUnit();
937 else if (D.isGlobalVariable())
938 CUNode = DIGlobalVariable(N).getCompileUnit();
939 else if (D.isVariable())
940 CUNode = DIVariable(N).getCompileUnit();
941 else if (D.isNameSpace())
942 CUNode = DINameSpace(N).getCompileUnit();
943 else if (D.isFile())
944 CUNode = DIFile(N).getCompileUnit();
945 else
946 return FirstCU;
947
948 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
949 = CUMap.find(CUNode);
950 if (I == CUMap.end())
951 return FirstCU;
952 return I->second;
953}
954
Devang Patel0c4720c2010-08-23 18:25:56 +0000955/// isUnsignedDIType - Return true if type encoding is unsigned.
956static bool isUnsignedDIType(DIType Ty) {
957 DIDerivedType DTy(Ty);
958 if (DTy.Verify())
959 return isUnsignedDIType(DTy.getTypeDerivedFrom());
960
961 DIBasicType BTy(Ty);
962 if (BTy.Verify()) {
963 unsigned Encoding = BTy.getEncoding();
964 if (Encoding == dwarf::DW_ATE_unsigned ||
965 Encoding == dwarf::DW_ATE_unsigned_char)
966 return true;
967 }
968 return false;
969}
Devang Patel163a9f72010-05-10 22:49:55 +0000970
Devang Patele449d1f2011-01-20 00:02:16 +0000971// Return const exprssion if value is a GEP to access merged global
972// constant. e.g.
973// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
974static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
975 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
976 if (!CE || CE->getNumOperands() != 3 ||
977 CE->getOpcode() != Instruction::GetElementPtr)
978 return NULL;
979
980 // First operand points to a global value.
981 if (!isa<GlobalValue>(CE->getOperand(0)))
982 return NULL;
983
984 // Second operand is zero.
985 const ConstantInt *CI =
986 dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
987 if (!CI || !CI->isZero())
988 return NULL;
989
990 // Third operand is offset.
991 if (!isa<ConstantInt>(CE->getOperand(2)))
992 return NULL;
993
994 return CE;
995}
996
Devang Patel163a9f72010-05-10 22:49:55 +0000997/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000998void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +0000999 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001000
Devang Patel905cf5e2009-09-04 23:59:07 +00001001 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +00001002 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +00001003 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001004
1005 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001006 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +00001007 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001008 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001009
Devang Patel9fa539c2010-08-10 01:37:23 +00001010 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +00001011 DIE *VariableDIE = new DIE(GV.getTag());
1012
1013 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001014
Devang Patel9fa539c2010-08-10 01:37:23 +00001015 // Add name.
Devang Patel3cbee302011-04-12 22:53:02 +00001016 TheCU->addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1017 GV.getDisplayName());
Devang Patel9fa539c2010-08-10 01:37:23 +00001018 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +00001019 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel3cbee302011-04-12 22:53:02 +00001020 TheCU->addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1021 dwarf::DW_FORM_string,
1022 getRealLinkageName(LinkageName));
Devang Patel9fa539c2010-08-10 01:37:23 +00001023 // Add type.
Devang Patel3cbee302011-04-12 22:53:02 +00001024 TheCU->addType(VariableDIE, GTy);
Devang Patel50d80e32010-04-13 20:35:04 +00001025 if (GTy.isCompositeType() && !GTy.getName().empty()
1026 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001027 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001028 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +00001029 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001030 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001031 // Add scoping info.
1032 if (!GV.isLocalToUnit()) {
Devang Patel3cbee302011-04-12 22:53:02 +00001033 TheCU->addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel9fa539c2010-08-10 01:37:23 +00001034 // Expose as global.
1035 TheCU->addGlobal(GV.getName(), VariableDIE);
1036 }
1037 // Add line number info.
Devang Patel3cbee302011-04-12 22:53:02 +00001038 TheCU->addSourceLine(VariableDIE, GV);
Devang Patel9fa539c2010-08-10 01:37:23 +00001039 // Add to map.
1040 TheCU->insertDIE(N, VariableDIE);
1041 // Add to context owner.
1042 DIDescriptor GVContext = GV.getContext();
Devang Patel3cbee302011-04-12 22:53:02 +00001043 TheCU->addToContextOwner(VariableDIE, GVContext);
Devang Patel9fa539c2010-08-10 01:37:23 +00001044 // Add location.
Devang Patel29368072010-08-10 07:11:13 +00001045 if (isGlobalVariable) {
1046 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel3cbee302011-04-12 22:53:02 +00001047 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1048 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patel29368072010-08-10 07:11:13 +00001049 Asm->Mang->getSymbol(GV.getGlobal()));
1050 // Do not create specification DIE if context is either compile unit
1051 // or a subprogram.
1052 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1053 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1054 // Create specification DIE.
1055 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel3cbee302011-04-12 22:53:02 +00001056 TheCU->addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
Devang Patel29368072010-08-10 07:11:13 +00001057 dwarf::DW_FORM_ref4, VariableDIE);
Devang Patel3cbee302011-04-12 22:53:02 +00001058 TheCU->addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1059 TheCU->addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel29368072010-08-10 07:11:13 +00001060 TheCU->addDie(VariableSpecDIE);
1061 } else {
Devang Patel3cbee302011-04-12 22:53:02 +00001062 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel29368072010-08-10 07:11:13 +00001063 }
Devang Patel76a788c2011-01-06 21:39:25 +00001064 } else if (ConstantInt *CI =
1065 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
Devang Patel3cbee302011-04-12 22:53:02 +00001066 TheCU->addConstantValue(VariableDIE, CI, isUnsignedDIType(GTy));
Devang Patele449d1f2011-01-20 00:02:16 +00001067 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
1068 // GV is a merged global.
1069 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel3cbee302011-04-12 22:53:02 +00001070 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1071 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
1072 Asm->Mang->getSymbol(cast<GlobalValue>(CE->getOperand(0))));
Devang Patele449d1f2011-01-20 00:02:16 +00001073 ConstantInt *CII = cast<ConstantInt>(CE->getOperand(2));
Devang Patel3cbee302011-04-12 22:53:02 +00001074 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1075 TheCU->addUInt(Block, 0, dwarf::DW_FORM_udata, CII->getZExtValue());
1076 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1077 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patele449d1f2011-01-20 00:02:16 +00001078 }
Devang Patel76a788c2011-01-06 21:39:25 +00001079
Devang Patel13e16b62009-06-26 01:49:18 +00001080 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001081}
1082
Devang Patel163a9f72010-05-10 22:49:55 +00001083/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001084void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001085 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001086
Stuart Hastings639336e2010-04-06 21:38:29 +00001087 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001088 CompileUnit *TheCU = getCompileUnit(N);
1089 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +00001090 return;
1091
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001092 if (!SP.isDefinition())
1093 // This is a method declaration which will be handled while constructing
1094 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001095 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001096
Stuart Hastings639336e2010-04-06 21:38:29 +00001097 DIE *SubprogramDie = createSubprogramDIE(SP);
1098
1099 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00001100 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001101
1102 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +00001103 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001104
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001105 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +00001106 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001107
Devang Patel13e16b62009-06-26 01:49:18 +00001108 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001109}
1110
Devang Patel2c4ceb12009-11-21 02:48:08 +00001111/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001112/// content. Create global DIEs and emit initial debug info sections.
1113/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00001114void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00001115 if (DisableDebugInfoPrinting)
1116 return;
1117
Devang Patel30692ab2011-05-03 16:45:22 +00001118 // If module has named metadata anchors then use them, otherwise scan the module
1119 // using debug info finder to collect debug info.
1120 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
1121 if (CU_Nodes) {
Devang Patela938dd02011-05-04 16:34:02 +00001122
1123 NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
1124 NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
1125 if (!GV_Nodes && !SP_Nodes)
1126 // If there are not any global variables or any functions then
1127 // there is not any debug info in this module.
Devang Patel30692ab2011-05-03 16:45:22 +00001128 return;
1129
Devang Patela938dd02011-05-04 16:34:02 +00001130 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
1131 constructCompileUnit(CU_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +00001132
Devang Patela938dd02011-05-04 16:34:02 +00001133 if (GV_Nodes)
1134 for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i)
1135 constructGlobalVariableDIE(GV_Nodes->getOperand(i));
1136
1137 if (SP_Nodes)
1138 for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i)
1139 constructSubprogramDIE(SP_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +00001140
1141 } else {
1142
1143 DebugInfoFinder DbgFinder;
1144 DbgFinder.processModule(*M);
1145
Devang Patela938dd02011-05-04 16:34:02 +00001146 bool HasDebugInfo = false;
Devang Patel30692ab2011-05-03 16:45:22 +00001147 // Scan all the compile-units to see if there are any marked as the main unit.
1148 // if not, we do not generate debug info.
1149 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1150 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1151 if (DICompileUnit(*I).isMain()) {
1152 HasDebugInfo = true;
1153 break;
1154 }
1155 }
Devang Patela938dd02011-05-04 16:34:02 +00001156 if (!HasDebugInfo) return;
Devang Patel30692ab2011-05-03 16:45:22 +00001157
1158 // Create all the compile unit DIEs.
1159 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1160 E = DbgFinder.compile_unit_end(); I != E; ++I)
1161 constructCompileUnit(*I);
1162
1163 // Create DIEs for each global variable.
1164 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1165 E = DbgFinder.global_variable_end(); I != E; ++I)
1166 constructGlobalVariableDIE(*I);
1167
1168 // Create DIEs for each subprogram.
1169 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1170 E = DbgFinder.subprogram_end(); I != E; ++I)
1171 constructSubprogramDIE(*I);
Chris Lattnerd850ac72010-04-05 02:19:28 +00001172 }
Devang Patel30692ab2011-05-03 16:45:22 +00001173
Chris Lattnerd850ac72010-04-05 02:19:28 +00001174 // Tell MMI that we have debug info.
1175 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +00001176
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001177 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00001178 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001179
Devang Patele7e5a0f2010-08-10 20:01:20 +00001180 //getOrCreateTypeDIE
1181 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patel3cbee302011-04-12 22:53:02 +00001182 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1183 DIType Ty(NMD->getOperand(i));
1184 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1185 }
Devang Patele7e5a0f2010-08-10 20:01:20 +00001186
Devang Patel1a7ca032010-09-28 18:08:20 +00001187 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patel3cbee302011-04-12 22:53:02 +00001188 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1189 DIType Ty(NMD->getOperand(i));
1190 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1191 }
Devang Patel1a7ca032010-09-28 18:08:20 +00001192
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001193 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001194 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001195}
1196
Devang Patel2c4ceb12009-11-21 02:48:08 +00001197/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001198///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001199void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00001200 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +00001201 const Module *M = MMI->getModule();
Devang Patele9a1cca2010-08-02 17:32:15 +00001202 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +00001203 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
1204 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
1205 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
1206 DISubprogram SP(AllSPs->getOperand(SI));
1207 if (!SP.Verify()) continue;
1208
1209 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +00001210 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +00001211 StringRef FName = SP.getLinkageName();
1212 if (FName.empty())
1213 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00001214 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +00001215 if (!NMD) continue;
1216 unsigned E = NMD->getNumOperands();
1217 if (!E) continue;
1218 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Patele9a1cca2010-08-02 17:32:15 +00001219 DeadFnScopeMap[SP] = Scope;
Devang Patel4a1cad62010-06-28 18:25:03 +00001220 for (unsigned I = 0; I != E; ++I) {
1221 DIVariable DV(NMD->getOperand(I));
1222 if (!DV.Verify()) continue;
1223 Scope->addVariable(new DbgVariable(DV));
1224 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001225
Devang Patel4a1cad62010-06-28 18:25:03 +00001226 // Construct subprogram DIE and add variables DIEs.
1227 constructSubprogramDIE(SP);
1228 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patele03161c2010-08-09 18:51:29 +00001229 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel4a1cad62010-06-28 18:25:03 +00001230 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1231 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
1232 if (VariableDIE)
1233 ScopeDIE->addChild(VariableDIE);
1234 }
1235 }
1236 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001237
Devang Patel53bb5c92009-11-10 23:06:00 +00001238 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1239 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1240 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1241 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +00001242 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001243 }
1244
Devang Patele9f8f5e2010-05-07 20:54:48 +00001245 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00001246 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1247 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001248 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00001249 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00001250 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00001251 if (!NDie) continue;
Devang Patel3cbee302011-04-12 22:53:02 +00001252 getCompileUnit(N)->addDIEEntry(SPDie, dwarf::DW_AT_containing_type,
1253 dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00001254 }
1255
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001256 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001257 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001258 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001259 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001260 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001261
1262 // End text sections.
1263 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001264 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00001265 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001266 }
1267
1268 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001269 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001270
1271 // Emit function debug frame information
1272 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1273 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001274 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001275
1276 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001277 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001278
1279 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001280 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001281
1282 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001283 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001284
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001285 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001286 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001287
Devang Patel193f7202009-11-24 01:14:22 +00001288 // Emit info into a debug pubtypes section.
1289 emitDebugPubTypes();
1290
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001291 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001292 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001293
1294 // Emit info into a debug aranges section.
1295 EmitDebugARanges();
1296
1297 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001298 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001299
1300 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001301 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001302
1303 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001304 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001305
Chris Lattnerbc733f52010-03-13 02:17:42 +00001306 // Emit info into a debug str section.
1307 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001308
Devang Patele9a1cca2010-08-02 17:32:15 +00001309 // clean up.
1310 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +00001311 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1312 E = CUMap.end(); I != E; ++I)
1313 delete I->second;
1314 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001315}
1316
Devang Patel53bb5c92009-11-10 23:06:00 +00001317/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001318DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattnerde4845c2010-04-02 19:42:39 +00001319 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001320
Devang Patel2db49d72010-05-07 18:11:54 +00001321 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001322 if (AbsDbgVariable)
1323 return AbsDbgVariable;
1324
Devang Patel2db49d72010-05-07 18:11:54 +00001325 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00001326 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00001327 if (!Scope)
1328 return NULL;
1329
Devang Patel26c1e562010-05-20 16:36:41 +00001330 AbsDbgVariable = new DbgVariable(Var);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001331 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00001332 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00001333 return AbsDbgVariable;
1334}
1335
Devang Patel0478c152011-03-01 22:58:55 +00001336/// addCurrentFnArgument - If Var is an current function argument that add
1337/// it in CurrentFnArguments list.
1338bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
1339 DbgVariable *Var, DbgScope *Scope) {
1340 if (Scope != CurrentFnDbgScope)
1341 return false;
1342 DIVariable DV = Var->getVariable();
1343 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1344 return false;
1345 unsigned ArgNo = DV.getArgNumber();
1346 if (ArgNo == 0)
1347 return false;
1348
Devang Patelcb3a6572011-03-03 20:02:02 +00001349 size_t Size = CurrentFnArguments.size();
1350 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001351 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +00001352 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +00001353 // arguments does the function have at source level.
1354 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +00001355 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +00001356 CurrentFnArguments[ArgNo - 1] = Var;
1357 return true;
1358}
1359
Devang Patelee432862010-05-20 19:57:06 +00001360/// collectVariableInfoFromMMITable - Collect variable information from
1361/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001362void
Devang Patelee432862010-05-20 19:57:06 +00001363DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
1364 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001365 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patele717faa2009-10-06 01:26:37 +00001366 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1367 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1368 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00001369 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001370 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00001371 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001372 DIVariable DV(Var);
1373 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001374
Chris Lattnerde4845c2010-04-02 19:42:39 +00001375 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001376 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00001377 Scope = ConcreteScopes.lookup(IA);
1378 if (Scope == 0)
1379 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001380
Devang Patelfb0ee432009-11-10 23:20:04 +00001381 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00001382 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00001383 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001384
Devang Patel26c1e562010-05-20 16:36:41 +00001385 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
1386 DbgVariable *RegVar = new DbgVariable(DV);
1387 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel0478c152011-03-01 22:58:55 +00001388 if (!addCurrentFnArgument(MF, RegVar, Scope))
1389 Scope->addVariable(RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +00001390 if (AbsDbgVariable) {
1391 recordVariableFrameIndex(AbsDbgVariable, VP.first);
1392 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
1393 }
Devang Patele717faa2009-10-06 01:26:37 +00001394 }
Devang Patelee432862010-05-20 19:57:06 +00001395}
Devang Patel90a48ad2010-03-15 18:33:46 +00001396
Jim Grosbach1e20b962010-07-21 21:21:52 +00001397/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00001398/// DBG_VALUE instruction, is in a defined reg.
1399static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
1400 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001401 return MI->getNumOperands() == 3 &&
1402 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1403 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +00001404}
1405
Devang Patelee432862010-05-20 19:57:06 +00001406/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001407void
Devang Patel78e127d2010-06-25 22:07:34 +00001408DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1409 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001410
Devang Patelee432862010-05-20 19:57:06 +00001411 /// collection info from MMI table.
1412 collectVariableInfoFromMMITable(MF, Processed);
1413
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001414 for (SmallVectorImpl<const MDNode*>::const_iterator
1415 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1416 ++UVI) {
1417 const MDNode *Var = *UVI;
1418 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001419 continue;
1420
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001421 // History contains relevant DBG_VALUE instructions for Var and instructions
1422 // clobbering it.
1423 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1424 if (History.empty())
1425 continue;
1426 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001427
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001428 DIVariable DV(Var);
Devang Patela36478f2011-01-11 21:42:10 +00001429 DbgScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001430 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1431 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelc0c5a262010-05-21 00:10:20 +00001432 Scope = CurrentFnDbgScope;
Devang Patela36478f2011-01-11 21:42:10 +00001433 else
1434 Scope = findDbgScope(MInsn);
Devang Patelee432862010-05-20 19:57:06 +00001435 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001436 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001437 continue;
1438
1439 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001440 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patelee432862010-05-20 19:57:06 +00001441 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel0478c152011-03-01 22:58:55 +00001442 if (!addCurrentFnArgument(MF, RegVar, Scope))
1443 Scope->addVariable(RegVar);
Devang Patelc0c5a262010-05-21 00:10:20 +00001444 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
1445 DbgVariableToDbgInstMap[AbsVar] = MInsn;
1446 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00001447 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001448
1449 // Simple ranges that are fully coalesced.
1450 if (History.size() <= 1 || (History.size() == 2 &&
1451 MInsn->isIdenticalTo(History.back()))) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001452 DbgVariableToDbgInstMap[RegVar] = MInsn;
1453 continue;
1454 }
1455
1456 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001457 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001458
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001459 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1460 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1461 const MachineInstr *Begin = *HI;
1462 assert(Begin->isDebugValue() && "Invalid History entry");
Devang Patelc3f5f782010-05-25 23:40:22 +00001463 MachineLocation MLoc;
Devang Patelb2cf5812010-08-04 18:40:52 +00001464 if (Begin->getNumOperands() == 3) {
1465 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
1466 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
1467 } else
1468 MLoc = Asm->getDebugValueLocation(Begin);
1469
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001470 // FIXME: emitDebugLoc only understands registers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001471 if (!MLoc.getReg())
1472 continue;
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001473
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001474 // Compute the range for a register location.
1475 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1476 const MCSymbol *SLabel = 0;
1477
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001478 if (HI + 1 == HE)
1479 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001480 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001481 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001482 else {
1483 const MachineInstr *End = HI[1];
1484 if (End->isDebugValue())
1485 SLabel = getLabelBeforeInsn(End);
1486 else {
1487 // End is a normal instruction clobbering the range.
1488 SLabel = getLabelAfterInsn(End);
1489 assert(SLabel && "Forgot label after clobber instruction");
1490 ++HI;
1491 }
1492 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001493
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001494 // The value is valid until the next DBG_VALUE or clobber.
Devang Patelc26f5442011-04-28 02:22:40 +00001495 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc, Var));
Devang Patelc3f5f782010-05-25 23:40:22 +00001496 }
1497 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001498 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001499
1500 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00001501 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00001502 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00001503 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001504 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00001505 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00001506 continue;
1507 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
1508 if (Scope)
Devang Patel26c1e562010-05-20 16:36:41 +00001509 Scope->addVariable(new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00001510 }
1511 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001512}
Devang Patel98e1cac2010-05-14 21:01:35 +00001513
Devang Patelc3f5f782010-05-25 23:40:22 +00001514/// getLabelBeforeInsn - Return Label preceding the instruction.
1515const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001516 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1517 assert(Label && "Didn't insert label before instruction");
1518 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001519}
1520
1521/// getLabelAfterInsn - Return Label immediately following the instruction.
1522const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001523 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001524}
1525
Devang Patelcbbe2872010-10-26 17:49:02 +00001526/// beginInstruction - Process beginning of an instruction.
1527void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001528 // Check if source location changes, but ignore DBG_VALUE locations.
1529 if (!MI->isDebugValue()) {
1530 DebugLoc DL = MI->getDebugLoc();
1531 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
1532 PrevInstLoc = DL;
1533 if (!DL.isUnknown()) {
1534 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
1535 recordSourceLine(DL.getLine(), DL.getCol(), Scope);
1536 } else
1537 recordSourceLine(0, 0, 0);
1538 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001539 }
Devang Patelaead63c2010-03-29 22:59:58 +00001540
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001541 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001542 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1543 LabelsBeforeInsn.find(MI);
1544
1545 // No label needed.
1546 if (I == LabelsBeforeInsn.end())
1547 return;
1548
1549 // Label already assigned.
1550 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001551 return;
Devang Patel553881b2010-03-29 17:20:31 +00001552
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001553 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001554 PrevLabel = MMI->getContext().CreateTempSymbol();
1555 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001556 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001557 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001558}
1559
Devang Patelcbbe2872010-10-26 17:49:02 +00001560/// endInstruction - Process end of an instruction.
1561void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001562 // Don't create a new label after DBG_VALUE instructions.
1563 // They don't generate code.
1564 if (!MI->isDebugValue())
1565 PrevLabel = 0;
1566
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001567 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1568 LabelsAfterInsn.find(MI);
1569
1570 // No label needed.
1571 if (I == LabelsAfterInsn.end())
1572 return;
1573
1574 // Label already assigned.
1575 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001576 return;
1577
1578 // We need a label after this instruction.
1579 if (!PrevLabel) {
1580 PrevLabel = MMI->getContext().CreateTempSymbol();
1581 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001582 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001583 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001584}
1585
Devang Pateleac9c072010-04-27 19:46:33 +00001586/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001587DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel61409622010-07-07 20:12:52 +00001588 const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001589 if (!InlinedAt) {
1590 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1591 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00001592 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001593 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1594 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00001595 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001596 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00001597 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00001598 WScope->setParent(Parent);
1599 Parent->addScope(WScope);
1600 }
1601
1602 if (!WScope->getParent()) {
1603 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastingscad22ad2010-06-15 23:06:30 +00001604 // We used to check only for a linkage name, but that fails
1605 // since we began omitting the linkage name for private
1606 // functions. The new way is to check for the name in metadata,
1607 // but that's not supported in old .ll test cases. Ergo, we
1608 // check both.
Stuart Hastings215aa152010-06-11 20:08:44 +00001609 if (SPName == Asm->MF->getFunction()->getName() ||
1610 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Pateleac9c072010-04-27 19:46:33 +00001611 CurrentFnDbgScope = WScope;
1612 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001613
Devang Pateleac9c072010-04-27 19:46:33 +00001614 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001615 }
1616
Devang Patel78e127d2010-06-25 22:07:34 +00001617 getOrCreateAbstractScope(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001618 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1619 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00001620 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001621
1622 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1623 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1624 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00001625 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00001626 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00001627 WScope->setParent(Parent);
1628 Parent->addScope(WScope);
1629
1630 ConcreteScopes[InlinedAt] = WScope;
Devang Pateleac9c072010-04-27 19:46:33 +00001631
1632 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00001633}
1634
Devang Pateleac9c072010-04-27 19:46:33 +00001635/// hasValidLocation - Return true if debug location entry attached with
1636/// machine instruction encodes valid location info.
1637static bool hasValidLocation(LLVMContext &Ctx,
1638 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00001639 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00001640 DebugLoc DL = MInsn->getDebugLoc();
1641 if (DL.isUnknown()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001642
Devang Patele9f8f5e2010-05-07 20:54:48 +00001643 const MDNode *S = DL.getScope(Ctx);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001644
Devang Pateleac9c072010-04-27 19:46:33 +00001645 // There is no need to create another DIE for compile unit. For all
1646 // other scopes, create one DbgScope now. This will be translated
1647 // into a scope DIE at the end.
1648 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001649
Devang Pateleac9c072010-04-27 19:46:33 +00001650 Scope = S;
1651 InlinedAt = DL.getInlinedAt(Ctx);
1652 return true;
1653}
1654
1655/// calculateDominanceGraph - Calculate dominance graph for DbgScope
1656/// hierarchy.
1657static void calculateDominanceGraph(DbgScope *Scope) {
1658 assert (Scope && "Unable to calculate scop edominance graph!");
1659 SmallVector<DbgScope *, 4> WorkStack;
1660 WorkStack.push_back(Scope);
1661 unsigned Counter = 0;
1662 while (!WorkStack.empty()) {
1663 DbgScope *WS = WorkStack.back();
1664 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
1665 bool visitedChildren = false;
1666 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
1667 SE = Children.end(); SI != SE; ++SI) {
1668 DbgScope *ChildScope = *SI;
1669 if (!ChildScope->getDFSOut()) {
1670 WorkStack.push_back(ChildScope);
1671 visitedChildren = true;
1672 ChildScope->setDFSIn(++Counter);
1673 break;
1674 }
1675 }
1676 if (!visitedChildren) {
1677 WorkStack.pop_back();
1678 WS->setDFSOut(++Counter);
1679 }
1680 }
1681}
1682
1683/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001684static
Devang Pateleac9c072010-04-27 19:46:33 +00001685void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
1686 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
1687{
1688#ifndef NDEBUG
1689 unsigned PrevDFSIn = 0;
1690 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1691 I != E; ++I) {
1692 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1693 II != IE; ++II) {
1694 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001695 const MDNode *Scope = NULL;
1696 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00001697
1698 // Check if instruction has valid location information.
1699 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
1700 dbgs() << " [ ";
Jim Grosbach1e20b962010-07-21 21:21:52 +00001701 if (InlinedAt)
Devang Pateleac9c072010-04-27 19:46:33 +00001702 dbgs() << "*";
Jim Grosbach1e20b962010-07-21 21:21:52 +00001703 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Pateleac9c072010-04-27 19:46:33 +00001704 MI2ScopeMap.find(MInsn);
1705 if (DI != MI2ScopeMap.end()) {
1706 DbgScope *S = DI->second;
1707 dbgs() << S->getDFSIn();
1708 PrevDFSIn = S->getDFSIn();
1709 } else
1710 dbgs() << PrevDFSIn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001711 } else
Devang Pateleac9c072010-04-27 19:46:33 +00001712 dbgs() << " [ x" << PrevDFSIn;
1713 dbgs() << " ]";
1714 MInsn->dump();
1715 }
1716 dbgs() << "\n";
1717 }
1718#endif
1719}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001720/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00001721/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00001722bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00001723 // If scope information was extracted using .dbg intrinsics then there is not
1724 // any need to extract these information by scanning each instruction.
1725 if (!DbgScopeMap.empty())
1726 return false;
1727
Dan Gohman314bf7c2010-04-23 01:18:53 +00001728 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00001729 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
1730 SmallVector<DbgRange, 4> MIRanges;
1731 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001732 const MDNode *PrevScope = NULL;
1733 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00001734 const MachineInstr *RangeBeginMI = NULL;
1735 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00001736 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00001737 I != E; ++I) {
1738 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1739 II != IE; ++II) {
1740 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001741 const MDNode *Scope = NULL;
1742 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00001743
1744 // Check if instruction has valid location information.
1745 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
1746 PrevMI = MInsn;
1747 continue;
1748 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001749
Devang Pateleac9c072010-04-27 19:46:33 +00001750 // If scope has not changed then skip this instruction.
1751 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
1752 PrevMI = MInsn;
1753 continue;
1754 }
1755
Devang Pateld3526ea2011-02-15 17:56:09 +00001756 // Ignore DBG_VALUE. It does not contribute any instruction in output.
1757 if (MInsn->isDebugValue())
1758 continue;
1759
Jim Grosbach1e20b962010-07-21 21:21:52 +00001760 if (RangeBeginMI) {
1761 // If we have alread seen a beginning of a instruction range and
Devang Pateleac9c072010-04-27 19:46:33 +00001762 // current instruction scope does not match scope of first instruction
1763 // in this range then create a new instruction range.
1764 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001765 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel61409622010-07-07 20:12:52 +00001766 PrevInlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00001767 MIRanges.push_back(R);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001768 }
Devang Pateleac9c072010-04-27 19:46:33 +00001769
1770 // This is a beginning of a new instruction range.
1771 RangeBeginMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001772
Devang Pateleac9c072010-04-27 19:46:33 +00001773 // Reset previous markers.
1774 PrevMI = MInsn;
1775 PrevScope = Scope;
1776 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00001777 }
1778 }
1779
Devang Pateleac9c072010-04-27 19:46:33 +00001780 // Create last instruction range.
1781 if (RangeBeginMI && PrevMI && PrevScope) {
1782 DbgRange R(RangeBeginMI, PrevMI);
1783 MIRanges.push_back(R);
1784 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00001785 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001786
Devang Patel344130e2010-01-04 20:44:00 +00001787 if (!CurrentFnDbgScope)
1788 return false;
1789
Devang Pateleac9c072010-04-27 19:46:33 +00001790 calculateDominanceGraph(CurrentFnDbgScope);
1791 if (PrintDbgScope)
1792 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
1793
1794 // Find ranges of instructions covered by each DbgScope;
1795 DbgScope *PrevDbgScope = NULL;
1796 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
1797 RE = MIRanges.end(); RI != RE; ++RI) {
1798 const DbgRange &R = *RI;
1799 DbgScope *S = MI2ScopeMap.lookup(R.first);
1800 assert (S && "Lost DbgScope for a machine instruction!");
1801 if (PrevDbgScope && !PrevDbgScope->dominates(S))
1802 PrevDbgScope->closeInsnRange(S);
1803 S->openInsnRange(R.first);
1804 S->extendInsnRange(R.second);
1805 PrevDbgScope = S;
1806 }
1807
1808 if (PrevDbgScope)
1809 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00001810
Devang Patele37b0c62010-04-08 18:43:56 +00001811 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00001812
1813 return !DbgScopeMap.empty();
1814}
1815
Jim Grosbach1e20b962010-07-21 21:21:52 +00001816/// identifyScopeMarkers() -
Devang Pateleac9c072010-04-27 19:46:33 +00001817/// Each DbgScope has first instruction and last instruction to mark beginning
1818/// and end of a scope respectively. Create an inverse map that list scopes
1819/// starts (and ends) with an instruction. One instruction may start (or end)
1820/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001821void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00001822 SmallVector<DbgScope *, 4> WorkList;
1823 WorkList.push_back(CurrentFnDbgScope);
1824 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00001825 DbgScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001826
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001827 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001828 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001829 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001830 SE = Children.end(); SI != SE; ++SI)
1831 WorkList.push_back(*SI);
1832
Devang Patel53bb5c92009-11-10 23:06:00 +00001833 if (S->isAbstractScope())
1834 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001835
Devang Pateleac9c072010-04-27 19:46:33 +00001836 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
1837 if (Ranges.empty())
1838 continue;
1839 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1840 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001841 assert(RI->first && "DbgRange does not have first instruction!");
1842 assert(RI->second && "DbgRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001843 requestLabelBeforeInsn(RI->first);
1844 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001845 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001846 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001847}
1848
Dan Gohman084751c2010-04-20 00:37:27 +00001849/// FindFirstDebugLoc - Find the first debug location in the function. This
1850/// is intended to be an approximation for the source position of the
1851/// beginning of the function.
1852static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
1853 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1854 I != E; ++I)
1855 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
1856 MBBI != MBBE; ++MBBI) {
1857 DebugLoc DL = MBBI->getDebugLoc();
1858 if (!DL.isUnknown())
1859 return DL;
1860 }
1861 return DebugLoc();
1862}
1863
Devang Patel9a31f0f2010-10-25 20:45:32 +00001864#ifndef NDEBUG
1865/// CheckLineNumbers - Count basicblocks whose instructions do not have any
1866/// line number information.
1867static void CheckLineNumbers(const MachineFunction *MF) {
1868 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1869 I != E; ++I) {
1870 bool FoundLineNo = false;
1871 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1872 II != IE; ++II) {
1873 const MachineInstr *MI = II;
1874 if (!MI->getDebugLoc().isUnknown()) {
1875 FoundLineNo = true;
1876 break;
1877 }
1878 }
Devang Patel4d7f9a02010-10-28 22:11:59 +00001879 if (!FoundLineNo && I->size())
Devang Patel9a31f0f2010-10-25 20:45:32 +00001880 ++BlocksWithoutLineNo;
1881 }
1882}
1883#endif
1884
Devang Patel2c4ceb12009-11-21 02:48:08 +00001885/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001886/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001887void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001888 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00001889 if (!extractScopeInformation()) return;
Devang Patel60b35bd2009-10-06 18:37:31 +00001890
Devang Patel9a31f0f2010-10-25 20:45:32 +00001891#ifndef NDEBUG
1892 CheckLineNumbers(MF);
1893#endif
1894
Devang Pateleac9c072010-04-27 19:46:33 +00001895 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1896 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001897 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001898 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001899
1900 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1901 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00001902 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001903 if (FDL.isUnknown()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001904
Devang Patele9f8f5e2010-05-07 20:54:48 +00001905 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings0db42712010-07-19 23:56:30 +00001906 const MDNode *TheScope = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001907
Chris Lattnerde4845c2010-04-02 19:42:39 +00001908 DISubprogram SP = getDISubprogram(Scope);
1909 unsigned Line, Col;
1910 if (SP.Verify()) {
1911 Line = SP.getLineNumber();
1912 Col = 0;
Stuart Hastings0db42712010-07-19 23:56:30 +00001913 TheScope = SP;
Chris Lattnerde4845c2010-04-02 19:42:39 +00001914 } else {
1915 Line = FDL.getLine();
1916 Col = FDL.getCol();
Stuart Hastings0db42712010-07-19 23:56:30 +00001917 TheScope = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001918 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001919
Stuart Hastings0db42712010-07-19 23:56:30 +00001920 recordSourceLine(Line, Col, TheScope);
Devang Patelb2b31a62010-05-26 19:37:24 +00001921
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001922 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1923
Devang Patelb9abe9f2010-06-02 16:42:51 +00001924 /// ProcessedArgs - Collection of arguments already processed.
1925 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
1926
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001927 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1928
1929 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1930 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1931
Devang Patelb2b31a62010-05-26 19:37:24 +00001932 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001933 I != E; ++I) {
1934 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001935 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1936 II != IE; ++II) {
1937 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001938
Devang Patelb2b31a62010-05-26 19:37:24 +00001939 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00001940 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001941
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001942 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001943 const MDNode *Var =
1944 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001945
1946 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001947 if (isDbgValueInDefinedReg(MI))
1948 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1949
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001950 // Check the history of this variable.
1951 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1952 if (History.empty()) {
1953 UserVariables.push_back(Var);
1954 // The first mention of a function argument gets the FunctionBeginSym
1955 // label, so arguments are visible when breaking at function entry.
1956 DIVariable DV(Var);
1957 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1958 DISubprogram(getDISubprogram(DV.getContext()))
1959 .describes(MF->getFunction()))
1960 LabelsBeforeInsn[MI] = FunctionBeginSym;
1961 } else {
1962 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1963 const MachineInstr *Prev = History.back();
1964 if (Prev->isDebugValue()) {
1965 // Coalesce identical entries at the end of History.
1966 if (History.size() >= 2 &&
1967 Prev->isIdenticalTo(History[History.size() - 2]))
1968 History.pop_back();
1969
1970 // Terminate old register assignments that don't reach MI;
1971 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1972 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1973 isDbgValueInDefinedReg(Prev)) {
1974 // Previous register assignment needs to terminate at the end of
1975 // its basic block.
1976 MachineBasicBlock::const_iterator LastMI =
1977 PrevMBB->getLastNonDebugInstr();
1978 if (LastMI == PrevMBB->end())
1979 // Drop DBG_VALUE for empty range.
1980 History.pop_back();
1981 else {
1982 // Terminate after LastMI.
1983 History.push_back(LastMI);
1984 }
1985 }
1986 }
1987 }
1988 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001989 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001990 // Not a DBG_VALUE instruction.
1991 if (!MI->isLabel())
1992 AtBlockEntry = false;
1993
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001994 // Check if the instruction clobbers any registers with debug vars.
1995 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1996 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1997 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1998 continue;
1999 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
2000 unsigned Reg = *AI; ++AI) {
2001 const MDNode *Var = LiveUserVar[Reg];
2002 if (!Var)
2003 continue;
2004 // Reg is now clobbered.
2005 LiveUserVar[Reg] = 0;
2006
2007 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00002008 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
2009 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00002010 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00002011 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
2012 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00002013 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00002014 const MachineInstr *Prev = History.back();
2015 // Sanity-check: Register assignments are terminated at the end of
2016 // their block.
2017 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
2018 continue;
2019 // Is the variable still in Reg?
2020 if (!isDbgValueInDefinedReg(Prev) ||
2021 Prev->getOperand(0).getReg() != Reg)
2022 continue;
2023 // Var is clobbered. Make sure the next instruction gets a label.
2024 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00002025 }
2026 }
Devang Patelb2b31a62010-05-26 19:37:24 +00002027 }
Devang Patelb2b31a62010-05-26 19:37:24 +00002028 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00002029 }
2030
2031 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
2032 I != E; ++I) {
2033 SmallVectorImpl<const MachineInstr*> &History = I->second;
2034 if (History.empty())
2035 continue;
2036
2037 // Make sure the final register assignments are terminated.
2038 const MachineInstr *Prev = History.back();
2039 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
2040 const MachineBasicBlock *PrevMBB = Prev->getParent();
2041 MachineBasicBlock::const_iterator LastMI = PrevMBB->getLastNonDebugInstr();
2042 if (LastMI == PrevMBB->end())
2043 // Drop DBG_VALUE for empty range.
2044 History.pop_back();
2045 else {
2046 // Terminate after LastMI.
2047 History.push_back(LastMI);
2048 }
2049 }
2050 // Request labels for the full history.
2051 for (unsigned i = 0, e = History.size(); i != e; ++i) {
2052 const MachineInstr *MI = History[i];
2053 if (MI->isDebugValue())
2054 requestLabelBeforeInsn(MI);
2055 else
2056 requestLabelAfterInsn(MI);
2057 }
2058 }
Devang Patelb2b31a62010-05-26 19:37:24 +00002059
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00002060 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00002061 PrevLabel = FunctionBeginSym;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002062}
2063
Devang Patel2c4ceb12009-11-21 02:48:08 +00002064/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002065///
Chris Lattnereec791a2010-01-26 23:18:02 +00002066void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002067 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002068
Devang Patel344130e2010-01-04 20:44:00 +00002069 if (CurrentFnDbgScope) {
Devang Patel65eb4822010-05-22 00:04:14 +00002070
Devang Patelc3f5f782010-05-25 23:40:22 +00002071 // Define end label for subprogram.
2072 FunctionEndSym = Asm->GetTempSymbol("func_end",
2073 Asm->getFunctionNumber());
2074 // Assumes in correct section after the entry point.
2075 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002076
Devang Patel78e127d2010-06-25 22:07:34 +00002077 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2078 collectVariableInfo(MF, ProcessedVars);
Devang Patel65eb4822010-05-22 00:04:14 +00002079
Devang Patel344130e2010-01-04 20:44:00 +00002080 // Construct abstract scopes.
2081 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel78e127d2010-06-25 22:07:34 +00002082 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel78e127d2010-06-25 22:07:34 +00002083 DISubprogram SP((*AI)->getScopeNode());
2084 if (SP.Verify()) {
2085 // Collect info for variables that were optimized out.
2086 StringRef FName = SP.getLinkageName();
2087 if (FName.empty())
2088 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002089 if (NamedMDNode *NMD =
2090 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
Devang Patel78e127d2010-06-25 22:07:34 +00002091 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002092 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00002093 if (!DV || !ProcessedVars.insert(DV))
2094 continue;
Devang Patel1d68d212010-06-30 00:11:08 +00002095 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel78e127d2010-06-25 22:07:34 +00002096 if (Scope)
2097 Scope->addVariable(new DbgVariable(DV));
2098 }
2099 }
2100 }
Devang Patel90e19aa2010-06-30 01:40:11 +00002101 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2102 constructScopeDIE(*AI);
Devang Patel78e127d2010-06-25 22:07:34 +00002103 }
Devang Patel61409622010-07-07 20:12:52 +00002104
Devang Patel9c488372010-05-04 06:15:30 +00002105 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002106
Devang Patel9c488372010-05-04 06:15:30 +00002107 if (!DisableFramePointerElim(*MF))
Devang Patel3cbee302011-04-12 22:53:02 +00002108 getCompileUnit(CurrentFnDbgScope->getScopeNode())->addUInt(CurFnDIE,
2109 dwarf::DW_AT_APPLE_omit_frame_ptr,
2110 dwarf::DW_FORM_flag, 1);
Devang Patel9c488372010-05-04 06:15:30 +00002111
2112
Chris Lattnerd38fee82010-04-05 00:13:49 +00002113 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002114 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002115 }
2116
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002117 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002118 CurrentFnDbgScope = NULL;
Devang Pateleac0c9d2011-04-22 18:09:57 +00002119 DeleteContainerPointers(CurrentFnArguments);
Devang Patel26c1e562010-05-20 16:36:41 +00002120 DbgVariableToFrameIndexMap.clear();
2121 VarToAbstractVarMap.clear();
2122 DbgVariableToDbgInstMap.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002123 DeleteContainerSeconds(DbgScopeMap);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00002124 UserVariables.clear();
2125 DbgValues.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002126 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002127 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002128 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002129 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002130 LabelsBeforeInsn.clear();
2131 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002132 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002133}
2134
Devang Patel26c1e562010-05-20 16:36:41 +00002135/// recordVariableFrameIndex - Record a variable's index.
2136void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2137 assert (V && "Invalid DbgVariable!");
2138 DbgVariableToFrameIndexMap[V] = Index;
2139}
2140
2141/// findVariableFrameIndex - Return true if frame index for the variable
2142/// is found. Update FI to hold value of the index.
2143bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2144 assert (V && "Invalid DbgVariable!");
2145 DenseMap<const DbgVariable *, int>::iterator I =
2146 DbgVariableToFrameIndexMap.find(V);
2147 if (I == DbgVariableToFrameIndexMap.end())
2148 return false;
2149 *FI = I->second;
2150 return true;
2151}
2152
Jim Grosbach1e20b962010-07-21 21:21:52 +00002153/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patelee432862010-05-20 19:57:06 +00002154/// instruction.
2155DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2156 DbgScope *Scope = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002157 LLVMContext &Ctx =
Devang Patelee432862010-05-20 19:57:06 +00002158 MInsn->getParent()->getParent()->getFunction()->getContext();
2159 DebugLoc DL = MInsn->getDebugLoc();
2160
Jim Grosbach1e20b962010-07-21 21:21:52 +00002161 if (DL.isUnknown())
Devang Patelee432862010-05-20 19:57:06 +00002162 return Scope;
2163
2164 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2165 Scope = ConcreteScopes.lookup(IA);
2166 if (Scope == 0)
2167 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002168
Devang Patelee432862010-05-20 19:57:06 +00002169 return Scope;
2170}
2171
2172
Chris Lattnerc6087842010-03-09 04:54:43 +00002173/// recordSourceLine - Register a source line with debug info. Returns the
2174/// unique label that was emitted and which provides correspondence to
2175/// the source line list.
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00002176void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S){
Devang Patel65dbc902009-11-25 17:36:49 +00002177 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00002178 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00002179 unsigned Src = 1;
2180 if (S) {
2181 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002182
Dan Gohman1cc0d622010-05-05 23:41:32 +00002183 if (Scope.isCompileUnit()) {
2184 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002185 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00002186 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00002187 } else if (Scope.isFile()) {
2188 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00002189 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00002190 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002191 } else if (Scope.isSubprogram()) {
2192 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002193 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00002194 Dir = SP.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002195 } else if (Scope.isLexicalBlock()) {
2196 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002197 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00002198 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002199 } else
2200 assert(0 && "Unexpected scope info");
2201
Devang Patel23670e52011-03-24 20:30:50 +00002202 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002203 }
Rafael Espindola5c055632010-11-18 02:04:25 +00002204 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, DWARF2_FLAG_IS_STMT,
Devang Patel3f3bf932011-04-18 20:26:49 +00002205 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002206}
2207
Bill Wendling829e67b2009-05-20 23:22:40 +00002208//===----------------------------------------------------------------------===//
2209// Emit Methods
2210//===----------------------------------------------------------------------===//
2211
Devang Patel2c4ceb12009-11-21 02:48:08 +00002212/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002213///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002214unsigned
2215DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002216 // Get the children.
2217 const std::vector<DIE *> &Children = Die->getChildren();
2218
2219 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002220 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00002221 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00002222
2223 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002224 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002225
2226 // Get the abbreviation for this DIE.
2227 unsigned AbbrevNumber = Die->getAbbrevNumber();
2228 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2229
2230 // Set DIE offset
2231 Die->setOffset(Offset);
2232
2233 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002234 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002235
2236 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2237 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2238
2239 // Size the DIE attribute values.
2240 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2241 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00002242 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00002243
2244 // Size the DIE children if any.
2245 if (!Children.empty()) {
2246 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2247 "Children flag not set");
2248
2249 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002250 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002251
2252 // End of children marker.
2253 Offset += sizeof(int8_t);
2254 }
2255
2256 Die->setSize(Offset - Die->getOffset());
2257 return Offset;
2258}
2259
Devang Patel2c4ceb12009-11-21 02:48:08 +00002260/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002261///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002262void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00002263 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2264 E = CUMap.end(); I != E; ++I) {
2265 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00002266 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00002267 sizeof(int32_t) + // Length of Compilation Unit Info
2268 sizeof(int16_t) + // DWARF version number
2269 sizeof(int32_t) + // Offset Into Abbrev. Section
2270 sizeof(int8_t); // Pointer Size (in bytes)
2271 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00002272 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002273}
2274
Chris Lattner11b8f302010-04-04 23:02:02 +00002275/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2276/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00002277static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00002278 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002279 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00002280 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002281
Chris Lattner9c69e285532010-04-04 22:59:04 +00002282 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2283 Asm->OutStreamer.EmitLabel(TmpSym);
2284 return TmpSym;
2285}
2286
2287/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2288/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00002289void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002290 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002291
Bill Wendling94d04b82009-05-20 23:21:38 +00002292 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002293 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002294 DwarfFrameSectionSym =
2295 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2296 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002297
Jim Grosbach1e20b962010-07-21 21:21:52 +00002298 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00002299 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002300 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00002301 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00002302 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002303
Chris Lattner9c69e285532010-04-04 22:59:04 +00002304 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00002305 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002306
Devang Patelaf608bd2010-08-24 00:06:12 +00002307 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00002308 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2309 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2310 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002311 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00002312 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00002313 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2314 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00002315
Devang Patelc3f5f782010-05-25 23:40:22 +00002316 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
2317 "section_debug_loc");
2318
Chris Lattner9c69e285532010-04-04 22:59:04 +00002319 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00002320 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002321}
2322
Devang Patel2c4ceb12009-11-21 02:48:08 +00002323/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002324///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002325void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002326 // Get the abbreviation for this DIE.
2327 unsigned AbbrevNumber = Die->getAbbrevNumber();
2328 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2329
Bill Wendling94d04b82009-05-20 23:21:38 +00002330 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00002331 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00002332 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2333 Twine::utohexstr(Die->getOffset()) + ":0x" +
2334 Twine::utohexstr(Die->getSize()) + " " +
2335 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002336 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002337
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002338 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00002339 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2340
2341 // Emit the DIE attribute values.
2342 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2343 unsigned Attr = AbbrevData[i].getAttribute();
2344 unsigned Form = AbbrevData[i].getForm();
2345 assert(Form && "Too many attributes for DIE (check abbreviation)");
2346
Chris Lattner3f53c832010-04-04 18:52:31 +00002347 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00002348 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002349
Bill Wendling94d04b82009-05-20 23:21:38 +00002350 switch (Attr) {
2351 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002352 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002353 break;
2354 case dwarf::DW_AT_abstract_origin: {
2355 DIEEntry *E = cast<DIEEntry>(Values[i]);
2356 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002357 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002358 Asm->EmitInt32(Addr);
2359 break;
2360 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002361 case dwarf::DW_AT_ranges: {
2362 // DW_AT_range Value encodes offset in debug_range section.
2363 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00002364
2365 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
2366 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
2367 V->getValue(),
2368 4);
2369 } else {
2370 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2371 V->getValue(),
2372 DwarfDebugRangeSectionSym,
2373 4);
2374 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002375 break;
2376 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002377 case dwarf::DW_AT_location: {
2378 if (UseDotDebugLocEntry.count(Die) != 0) {
2379 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbar83320a02011-03-16 22:16:39 +00002380 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patelc3f5f782010-05-25 23:40:22 +00002381 } else
2382 Values[i]->EmitValue(Asm, Form);
2383 break;
2384 }
Devang Patel2a361602010-09-29 19:08:08 +00002385 case dwarf::DW_AT_accessibility: {
2386 if (Asm->isVerbose()) {
2387 DIEInteger *V = cast<DIEInteger>(Values[i]);
2388 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2389 }
2390 Values[i]->EmitValue(Asm, Form);
2391 break;
2392 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002393 default:
2394 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002395 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00002396 break;
2397 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002398 }
2399
2400 // Emit the DIE children if any.
2401 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2402 const std::vector<DIE *> &Children = Die->getChildren();
2403
2404 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002405 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002406
Chris Lattner3f53c832010-04-04 18:52:31 +00002407 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00002408 Asm->OutStreamer.AddComment("End Of Children Mark");
2409 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002410 }
2411}
2412
Devang Patel8a241142009-12-09 18:24:21 +00002413/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002414///
Devang Patel8a241142009-12-09 18:24:21 +00002415void DwarfDebug::emitDebugInfo() {
2416 // Start debug info section.
2417 Asm->OutStreamer.SwitchSection(
2418 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00002419 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2420 E = CUMap.end(); I != E; ++I) {
2421 CompileUnit *TheCU = I->second;
2422 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002423
Devang Patel163a9f72010-05-10 22:49:55 +00002424 // Emit the compile units header.
2425 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2426 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002427
Devang Patel163a9f72010-05-10 22:49:55 +00002428 // Emit size of content not including length itself
2429 unsigned ContentSize = Die->getSize() +
2430 sizeof(int16_t) + // DWARF version number
2431 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00002432 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00002433
Devang Patel163a9f72010-05-10 22:49:55 +00002434 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2435 Asm->EmitInt32(ContentSize);
2436 Asm->OutStreamer.AddComment("DWARF version number");
2437 Asm->EmitInt16(dwarf::DWARF_VERSION);
2438 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2439 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2440 DwarfAbbrevSectionSym);
2441 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2442 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002443
Devang Patel163a9f72010-05-10 22:49:55 +00002444 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00002445 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2446 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002447}
2448
Devang Patel2c4ceb12009-11-21 02:48:08 +00002449/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002450///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002451void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002452 // Check to see if it is worth the effort.
2453 if (!Abbreviations.empty()) {
2454 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002455 Asm->OutStreamer.SwitchSection(
2456 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002457
Chris Lattnerc0215722010-04-04 19:25:43 +00002458 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002459
2460 // For each abbrevation.
2461 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2462 // Get abbreviation data
2463 const DIEAbbrev *Abbrev = Abbreviations[i];
2464
2465 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002466 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002467
2468 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002469 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00002470 }
2471
2472 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002473 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002474
Chris Lattnerc0215722010-04-04 19:25:43 +00002475 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002476 }
2477}
2478
Devang Patel2c4ceb12009-11-21 02:48:08 +00002479/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002480/// the line matrix.
2481///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002482void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002483 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00002484 Asm->OutStreamer.AddComment("Extended Op");
2485 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002486
Chris Lattner233f52b2010-03-09 23:52:58 +00002487 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002488 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002489 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2490 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2491
2492 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002493
Chris Lattnerc0215722010-04-04 19:25:43 +00002494 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002495 Asm->getTargetData().getPointerSize(),
2496 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002497
2498 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00002499 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2500 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002501 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002502 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002503}
2504
Devang Patel2c4ceb12009-11-21 02:48:08 +00002505/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002506///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002507void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002508 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002509 return;
2510
Chris Lattnerd38fee82010-04-05 00:13:49 +00002511 int stackGrowth = Asm->getTargetData().getPointerSize();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002512 if (Asm->TM.getFrameLowering()->getStackGrowthDirection() ==
2513 TargetFrameLowering::StackGrowsDown)
Chris Lattnerd38fee82010-04-05 00:13:49 +00002514 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00002515
2516 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002517 Asm->OutStreamer.SwitchSection(
2518 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002519
Chris Lattnerc0215722010-04-04 19:25:43 +00002520 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00002521 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002522 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
2523 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002524
Chris Lattnerc0215722010-04-04 19:25:43 +00002525 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00002526 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00002527 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00002528 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00002529 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00002530 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002531 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002532 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
2533 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00002534 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002535 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002536 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Bill Wendling94d04b82009-05-20 23:21:38 +00002537 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00002538
2539 std::vector<MachineMove> Moves;
Anton Korobeynikovd9e33852010-11-18 23:25:52 +00002540 TFI->getInitialFrameState(Moves);
Bill Wendling94d04b82009-05-20 23:21:38 +00002541
Chris Lattner02b86b92010-04-04 23:41:46 +00002542 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00002543
Chris Lattner059ea132010-04-28 01:05:45 +00002544 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00002545 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002546}
2547
Devang Patel2c4ceb12009-11-21 02:48:08 +00002548/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00002549/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00002550void DwarfDebug::
2551emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002552 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002553 return;
2554
2555 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002556 Asm->OutStreamer.SwitchSection(
2557 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002558
Chris Lattner233f52b2010-03-09 23:52:58 +00002559 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00002560 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00002561 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00002562 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00002563 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00002564 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002565
Chris Lattner206d61e2010-03-13 07:26:18 +00002566 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00002567
Chris Lattner233f52b2010-03-09 23:52:58 +00002568 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002569 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner6189ed12010-04-04 23:25:33 +00002570 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00002571
Chris Lattner233f52b2010-03-09 23:52:58 +00002572 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00002573 MCSymbol *FuncBeginSym =
2574 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00002575 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00002576 Asm->getTargetData().getPointerSize(),
2577 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002578
2579
Chris Lattner233f52b2010-03-09 23:52:58 +00002580 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00002581 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002582 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002583
Chris Lattner02b86b92010-04-04 23:41:46 +00002584 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00002585
Chris Lattner059ea132010-04-28 01:05:45 +00002586 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00002587 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00002588}
2589
Devang Patel8a241142009-12-09 18:24:21 +00002590/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2591///
2592void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00002593 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2594 E = CUMap.end(); I != E; ++I) {
2595 CompileUnit *TheCU = I->second;
2596 // Start the dwarf pubnames section.
2597 Asm->OutStreamer.SwitchSection(
2598 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002599
Devang Patel163a9f72010-05-10 22:49:55 +00002600 Asm->OutStreamer.AddComment("Length of Public Names Info");
2601 Asm->EmitLabelDifference(
2602 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
2603 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002604
Devang Patel163a9f72010-05-10 22:49:55 +00002605 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
2606 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002607
Devang Patel163a9f72010-05-10 22:49:55 +00002608 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002609 Asm->EmitInt16(dwarf::DWARF_VERSION);
2610
Devang Patel163a9f72010-05-10 22:49:55 +00002611 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002612 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002613 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002614
Devang Patel163a9f72010-05-10 22:49:55 +00002615 Asm->OutStreamer.AddComment("Compilation Unit Length");
2616 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2617 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2618 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002619
Devang Patel163a9f72010-05-10 22:49:55 +00002620 const StringMap<DIE*> &Globals = TheCU->getGlobals();
2621 for (StringMap<DIE*>::const_iterator
2622 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2623 const char *Name = GI->getKeyData();
2624 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002625
Devang Patel163a9f72010-05-10 22:49:55 +00002626 Asm->OutStreamer.AddComment("DIE offset");
2627 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002628
Devang Patel163a9f72010-05-10 22:49:55 +00002629 if (Asm->isVerbose())
2630 Asm->OutStreamer.AddComment("External Name");
2631 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
2632 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002633
Devang Patel163a9f72010-05-10 22:49:55 +00002634 Asm->OutStreamer.AddComment("End Mark");
2635 Asm->EmitInt32(0);
2636 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
2637 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002638 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002639}
2640
Devang Patel193f7202009-11-24 01:14:22 +00002641void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00002642 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2643 E = CUMap.end(); I != E; ++I) {
2644 CompileUnit *TheCU = I->second;
2645 // Start the dwarf pubnames section.
2646 Asm->OutStreamer.SwitchSection(
2647 Asm->getObjFileLowering().getDwarfPubTypesSection());
2648 Asm->OutStreamer.AddComment("Length of Public Types Info");
2649 Asm->EmitLabelDifference(
2650 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
2651 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002652
Devang Patel163a9f72010-05-10 22:49:55 +00002653 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
2654 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002655
Devang Patel163a9f72010-05-10 22:49:55 +00002656 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2657 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002658
Devang Patel163a9f72010-05-10 22:49:55 +00002659 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2660 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
2661 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002662
Devang Patel163a9f72010-05-10 22:49:55 +00002663 Asm->OutStreamer.AddComment("Compilation Unit Length");
2664 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2665 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2666 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002667
Devang Patel163a9f72010-05-10 22:49:55 +00002668 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2669 for (StringMap<DIE*>::const_iterator
2670 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2671 const char *Name = GI->getKeyData();
2672 DIE * Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002673
Devang Patel163a9f72010-05-10 22:49:55 +00002674 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2675 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002676
Devang Patel163a9f72010-05-10 22:49:55 +00002677 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
2678 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2679 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002680
Devang Patel163a9f72010-05-10 22:49:55 +00002681 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002682 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00002683 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
2684 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00002685 }
Devang Patel193f7202009-11-24 01:14:22 +00002686}
2687
Devang Patel2c4ceb12009-11-21 02:48:08 +00002688/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002689///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002690void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002691 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002692 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002693
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002694 // Start the dwarf str section.
2695 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002696 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002697
Chris Lattnerbc733f52010-03-13 02:17:42 +00002698 // Get all of the string pool entries and put them in an array by their ID so
2699 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002700 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002701 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002702
Chris Lattnerbc733f52010-03-13 02:17:42 +00002703 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2704 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2705 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002706
Chris Lattnerbc733f52010-03-13 02:17:42 +00002707 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002708
Chris Lattnerbc733f52010-03-13 02:17:42 +00002709 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002710 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002711 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002712
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002713 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002714 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002715 }
2716}
2717
Devang Patel2c4ceb12009-11-21 02:48:08 +00002718/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002719///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002720void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002721 if (DotDebugLocEntries.empty())
2722 return;
2723
Devang Patel6c3ea902011-02-04 22:57:18 +00002724 for (SmallVector<DotDebugLocEntry, 4>::iterator
2725 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2726 I != E; ++I) {
2727 DotDebugLocEntry &Entry = *I;
2728 if (I + 1 != DotDebugLocEntries.end())
2729 Entry.Merge(I+1);
2730 }
2731
Daniel Dunbar83320a02011-03-16 22:16:39 +00002732 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002733 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002734 Asm->getObjFileLowering().getDwarfLocSection());
2735 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002736 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2737 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002738 for (SmallVector<DotDebugLocEntry, 4>::iterator
2739 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002740 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002741 DotDebugLocEntry &Entry = *I;
2742 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002743 if (Entry.isEmpty()) {
2744 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2745 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002746 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002747 } else {
2748 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2749 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002750 DIVariable DV(Entry.Variable);
2751 if (DV.hasComplexAddress()) {
2752 unsigned N = DV.getNumAddrElements();
2753 unsigned i = 0;
2754 Asm->OutStreamer.AddComment("Loc expr size");
2755 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2756 // If first address element is OpPlus then emit
2757 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2758 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2759 Asm->EmitInt16(Asm->getDwarfRegOpSize(Loc) + N - 2);
2760 Asm->EmitDwarfRegOp(Loc);
2761// Asm->EmitULEB128(DV.getAddrElement(1));
2762 i = 2;
2763 } else {
2764 Asm->EmitInt16(Asm->getDwarfRegOpSize(Entry.Loc) + N);
2765 Asm->EmitDwarfRegOp(Entry.Loc);
2766 }
2767
2768 // Emit remaining complex address elements.
2769 for (; i < N; ++i) {
2770 uint64_t Element = DV.getAddrElement(i);
2771 if (Element == DIBuilder::OpPlus) {
2772 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2773 Asm->EmitULEB128(DV.getAddrElement(++i));
2774 } else if (Element == DIBuilder::OpDeref)
2775 Asm->EmitInt8(dwarf::DW_OP_deref);
2776 else llvm_unreachable("unknown Opcode found in complex address");
2777 }
2778 } else {
2779 Asm->OutStreamer.AddComment("Loc expr size");
2780 Asm->EmitInt16(Asm->getDwarfRegOpSize(Entry.Loc));
2781 Asm->EmitDwarfRegOp(Entry.Loc);
2782 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002783 }
2784 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002785}
2786
2787/// EmitDebugARanges - Emit visible names into a debug aranges section.
2788///
2789void DwarfDebug::EmitDebugARanges() {
2790 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002791 Asm->OutStreamer.SwitchSection(
2792 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002793}
2794
Devang Patel2c4ceb12009-11-21 02:48:08 +00002795/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002796///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002797void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002798 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002799 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002800 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002801 unsigned char Size = Asm->getTargetData().getPointerSize();
2802 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002803 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002804 I != E; ++I) {
2805 if (*I)
2806 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002807 else
Devang Pateleac9c072010-04-27 19:46:33 +00002808 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002809 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002810}
2811
Devang Patel2c4ceb12009-11-21 02:48:08 +00002812/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002813///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002814void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002815 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002816 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002817 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002818 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002819 }
2820}
2821
Devang Patel2c4ceb12009-11-21 02:48:08 +00002822/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002823/// Section Header:
2824/// 1. length of section
2825/// 2. Dwarf version number
2826/// 3. address size.
2827///
2828/// Entries (one "entry" for each function that was inlined):
2829///
2830/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2831/// otherwise offset into __debug_str for regular function name.
2832/// 2. offset into __debug_str section for regular function name.
2833/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2834/// instances for the function.
2835///
2836/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2837/// inlined instance; the die_offset points to the inlined_subroutine die in the
2838/// __debug_info section, and the low_pc is the starting address for the
2839/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002840void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002841 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002842 return;
2843
Devang Patel163a9f72010-05-10 22:49:55 +00002844 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002845 return;
2846
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002847 Asm->OutStreamer.SwitchSection(
2848 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002849
Chris Lattner233f52b2010-03-09 23:52:58 +00002850 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002851 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2852 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002853
Chris Lattnerc0215722010-04-04 19:25:43 +00002854 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002855
Chris Lattner233f52b2010-03-09 23:52:58 +00002856 Asm->OutStreamer.AddComment("Dwarf Version");
2857 Asm->EmitInt16(dwarf::DWARF_VERSION);
2858 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002859 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002860
Devang Patele9f8f5e2010-05-07 20:54:48 +00002861 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002862 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002863
Devang Patele9f8f5e2010-05-07 20:54:48 +00002864 const MDNode *Node = *I;
2865 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002866 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002867 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002868 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002869 StringRef LName = SP.getLinkageName();
2870 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002871
Chris Lattner233f52b2010-03-09 23:52:58 +00002872 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002873 if (LName.empty()) {
2874 Asm->OutStreamer.EmitBytes(Name, 0);
2875 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002876 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00002877 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2878 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002879
Chris Lattner233f52b2010-03-09 23:52:58 +00002880 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002881 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002882 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002883
Devang Patel53bb5c92009-11-10 23:06:00 +00002884 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002885 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002886 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002887 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002888
Chris Lattner3f53c832010-04-04 18:52:31 +00002889 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002890 Asm->OutStreamer.EmitSymbolValue(LI->first,
2891 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002892 }
2893 }
2894
Chris Lattnerc0215722010-04-04 19:25:43 +00002895 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002896}