blob: 403f78cdfc20b7736ca1fcb393cd2a604d907a63 [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,
56 cl::desc("Make an absense of debug location information explicit."),
57 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 Patel1eac3e72010-03-02 17:58:15 +0000325 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +0000326 if (!SP.getName().empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000327 SPCU->addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
328 SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +0000329
Devang Patel65dbc902009-11-25 17:36:49 +0000330 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +0000331 if (!LinkageName.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000332 SPCU->addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
333 getRealLinkageName(LinkageName));
Devang Patel351ca332010-01-05 01:46:14 +0000334
Devang Patel3cbee302011-04-12 22:53:02 +0000335 SPCU->addSourceLine(SPDie, SP);
Bill Wendling0310d762009-05-15 09:23:25 +0000336
Devang Patel7b172c62010-10-07 22:03:01 +0000337 if (SP.isPrototyped())
Devang Patel3cbee302011-04-12 22:53:02 +0000338 SPCU->addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
339
Bill Wendling0310d762009-05-15 09:23:25 +0000340 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000341 DICompositeType SPTy = SP.getType();
342 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +0000343 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +0000344
Devang Patel3c91b052010-03-08 20:52:55 +0000345 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel3cbee302011-04-12 22:53:02 +0000346 SPCU->addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000347 else
Devang Patel3cbee302011-04-12 22:53:02 +0000348 SPCU->addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000349
Devang Patel5d11eb02009-12-03 19:11:07 +0000350 unsigned VK = SP.getVirtuality();
351 if (VK) {
Devang Patel3cbee302011-04-12 22:53:02 +0000352 SPCU->addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
353 DIEBlock *Block = SPCU->getDIEBlock();
354 SPCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
355 SPCU->addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
356 SPCU->addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000357 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +0000358 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +0000359 }
360
Devang Patelee70fa72010-09-27 23:15:27 +0000361 if (!SP.isDefinition()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000362 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
363
Bill Wendling0310d762009-05-15 09:23:25 +0000364 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000365 // be handled while processing variables.
366 DICompositeType SPTy = SP.getType();
367 DIArray Args = SPTy.getTypeArray();
368 unsigned SPTag = SPTy.getTag();
369
Bill Wendling0310d762009-05-15 09:23:25 +0000370 if (SPTag == dwarf::DW_TAG_subroutine_type)
371 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
372 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +0000373 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patel3cbee302011-04-12 22:53:02 +0000374 SPCU->addType(Arg, ATy);
Devang Patelb4645642010-02-06 01:02:37 +0000375 if (ATy.isArtificial())
Devang Patel3cbee302011-04-12 22:53:02 +0000376 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000377 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000378 }
379 }
380
Devang Patel4e0d19d2010-02-03 19:57:19 +0000381 if (SP.isArtificial())
Devang Patel3cbee302011-04-12 22:53:02 +0000382 SPCU->addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel4e0d19d2010-02-03 19:57:19 +0000383
Devang Patelccff8122010-04-30 19:38:23 +0000384 if (!SP.isLocalToUnit())
Devang Patel3cbee302011-04-12 22:53:02 +0000385 SPCU->addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000386
Devang Patelccff8122010-04-30 19:38:23 +0000387 if (SP.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000388 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Devang Patelccff8122010-04-30 19:38:23 +0000389
Jim Grosbach91729002010-07-21 23:03:52 +0000390 if (unsigned isa = Asm->getISAEncoding()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000391 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
Jim Grosbach91729002010-07-21 23:03:52 +0000392 }
393
Devang Patelda194752011-04-05 22:52:06 +0000394 // Add function template parameters.
Devang Patel3cbee302011-04-12 22:53:02 +0000395 SPCU->addTemplateParams(*SPDie, SP.getTemplateParams());
Devang Patelda194752011-04-05 22:52:06 +0000396
Devang Patelccff8122010-04-30 19:38:23 +0000397 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel163a9f72010-05-10 22:49:55 +0000398 SPCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +0000399
Stuart Hastings215aa152010-06-11 20:08:44 +0000400 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000401 SPCU->addToContextOwner(SPDie, SP.getContext());
Stuart Hastings215aa152010-06-11 20:08:44 +0000402
Bill Wendling0310d762009-05-15 09:23:25 +0000403 return SPDie;
404}
405
Devang Patele9f8f5e2010-05-07 20:54:48 +0000406DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +0000407 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +0000408
409 DbgScope *AScope = AbstractScopes.lookup(N);
410 if (AScope)
411 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000412
Devang Patel53bb5c92009-11-10 23:06:00 +0000413 DbgScope *Parent = NULL;
414
415 DIDescriptor Scope(N);
416 if (Scope.isLexicalBlock()) {
417 DILexicalBlock DB(N);
418 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +0000419 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +0000420 }
421
422 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
423
424 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000425 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +0000426 AScope->setAbstractScope();
427 AbstractScopes[N] = AScope;
428 if (DIDescriptor(N).isSubprogram())
429 AbstractScopesList.push_back(AScope);
430 return AScope;
431}
Devang Patelaf9e8472009-10-01 20:31:14 +0000432
Devang Patel5f094002010-04-06 23:53:48 +0000433/// isSubprogramContext - Return true if Context is either a subprogram
434/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000435static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +0000436 if (!Context)
437 return false;
438 DIDescriptor D(Context);
439 if (D.isSubprogram())
440 return true;
441 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000442 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +0000443 return false;
444}
445
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000446/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000447/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
448/// If there are global variables in this scope then create and insert
449/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000450DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000451 CompileUnit *SPCU = getCompileUnit(SPNode);
452 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000453
Chris Lattnerd38fee82010-04-05 00:13:49 +0000454 assert(SPDie && "Unable to find subprogram DIE!");
455 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000456
Chris Lattnerd38fee82010-04-05 00:13:49 +0000457 // There is not any need to generate specification DIE for a function
458 // defined at compile unit level. If a function is defined inside another
459 // function then gdb prefers the definition at top level and but does not
Jim Grosbach1e20b962010-07-21 21:21:52 +0000460 // expect specification DIE in parent function. So avoid creating
Chris Lattnerd38fee82010-04-05 00:13:49 +0000461 // specification DIE for a function defined inside a function.
462 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbach1e20b962010-07-21 21:21:52 +0000463 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +0000464 !isSubprogramContext(SP.getContext())) {
Devang Patel3cbee302011-04-12 22:53:02 +0000465 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000466
467 // Add arguments.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000468 DICompositeType SPTy = SP.getType();
469 DIArray Args = SPTy.getTypeArray();
470 unsigned SPTag = SPTy.getTag();
471 if (SPTag == dwarf::DW_TAG_subroutine_type)
472 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
473 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +0000474 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patel3cbee302011-04-12 22:53:02 +0000475 SPCU->addType(Arg, ATy);
Chris Lattnerd38fee82010-04-05 00:13:49 +0000476 if (ATy.isArtificial())
Devang Patel3cbee302011-04-12 22:53:02 +0000477 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Chris Lattnerd38fee82010-04-05 00:13:49 +0000478 SPDie->addChild(Arg);
479 }
480 DIE *SPDeclDie = SPDie;
481 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000482 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
483 SPDeclDie);
Devang Patel163a9f72010-05-10 22:49:55 +0000484 SPCU->addDie(SPDie);
Chris Lattnerd38fee82010-04-05 00:13:49 +0000485 }
Jim Grosbach1e20b962010-07-21 21:21:52 +0000486
Devang Patel8aa61472010-07-07 22:20:57 +0000487 // Pick up abstract subprogram DIE.
488 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
489 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000490 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
491 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000492 SPCU->addDie(SPDie);
493 }
494
Devang Patel3cbee302011-04-12 22:53:02 +0000495 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
496 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
497 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
498 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000499 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
500 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000501 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000502
Chris Lattnerd38fee82010-04-05 00:13:49 +0000503 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000504}
505
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000506/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000507/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
508DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000509
510 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
511 if (Scope->isAbstractScope())
512 return ScopeDIE;
513
514 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
515 if (Ranges.empty())
516 return 0;
517
Devang Patel3cbee302011-04-12 22:53:02 +0000518 CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
Devang Pateleac9c072010-04-27 19:46:33 +0000519 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
520 if (Ranges.size() > 1) {
521 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000522 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000523 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000524 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
525 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
Devang Pateleac9c072010-04-27 19:46:33 +0000526 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
527 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000528 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
529 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000530 }
531 DebugRangeSymbols.push_back(NULL);
532 DebugRangeSymbols.push_back(NULL);
533 return ScopeDIE;
534 }
535
Devang Patelc3f5f782010-05-25 23:40:22 +0000536 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
537 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000538
Devang Patelc3f5f782010-05-25 23:40:22 +0000539 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000540
Chris Lattnerb7db7332010-03-09 01:58:53 +0000541 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
542 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000543
Devang Patel3cbee302011-04-12 22:53:02 +0000544 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
545 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000546
547 return ScopeDIE;
548}
549
Devang Patel2c4ceb12009-11-21 02:48:08 +0000550/// constructInlinedScopeDIE - This scope represents inlined body of
551/// a function. Construct DIE to represent this concrete inlined copy
552/// of the function.
553DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000554
555 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000556 assert (Ranges.empty() == false
Devang Pateleac9c072010-04-27 19:46:33 +0000557 && "DbgScope does not have instruction markers!");
558
559 // FIXME : .debug_inlined section specification does not clearly state how
560 // to emit inlined scope that is split into multiple instruction ranges.
561 // For now, use first instruction range and emit low_pc/high_pc pair and
562 // corresponding .debug_inlined section entry for this pair.
563 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000564 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
565 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000566
Devang Patel0afbf232010-07-08 22:39:20 +0000567 if (StartLabel == 0 || EndLabel == 0) {
Devang Pateleac9c072010-04-27 19:46:33 +0000568 assert (0 && "Unexpected Start and End labels for a inlined scope!");
569 return 0;
570 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000571 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000572 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000573 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000574 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000575
Devang Patel3c91b052010-03-08 20:52:55 +0000576 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +0000577 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000578 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +0000579 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
580
Devang Patel2db49d72010-05-07 18:11:54 +0000581 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +0000582 CompileUnit *TheCU = getCompileUnit(InlinedSP);
583 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +0000584 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel3cbee302011-04-12 22:53:02 +0000585 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
586 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000587
Devang Patel3cbee302011-04-12 22:53:02 +0000588 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
589 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000590
591 InlinedSubprogramDIEs.insert(OriginDIE);
592
593 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000594 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000595 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000596
597 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000598 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000599 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000600 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000601 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000602 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000603
Devang Patel53bb5c92009-11-10 23:06:00 +0000604 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000605 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
606 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000607
608 return ScopeDIE;
609}
610
Devang Patel2c4ceb12009-11-21 02:48:08 +0000611
612/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +0000613DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000614 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000615 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +0000616 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +0000617
618 // Translate tag to proper Dwarf tag. The result variable is dropped for
619 // now.
620 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +0000621 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +0000622 case dwarf::DW_TAG_return_variable:
623 return NULL;
624 case dwarf::DW_TAG_arg_variable:
625 Tag = dwarf::DW_TAG_formal_parameter;
626 break;
627 case dwarf::DW_TAG_auto_variable: // fall thru
628 default:
629 Tag = dwarf::DW_TAG_variable;
630 break;
631 }
632
633 // Define variable debug information entry.
634 DIE *VariableDie = new DIE(Tag);
Devang Patel3cbee302011-04-12 22:53:02 +0000635 CompileUnit *TheCU = getCompileUnit(DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000636 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +0000637 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
638 V2AVI = VarToAbstractVarMap.find(DV);
639 if (V2AVI != VarToAbstractVarMap.end())
640 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000641
Devang Patel26c1e562010-05-20 16:36:41 +0000642 if (AbsDIE)
Devang Patel3cbee302011-04-12 22:53:02 +0000643 TheCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
644 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000645 else {
Devang Patel3cbee302011-04-12 22:53:02 +0000646 TheCU->addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
647 TheCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000648
649 // Add variable type.
Devang Patel3cbee302011-04-12 22:53:02 +0000650 TheCU->addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +0000651 }
652
Devang Patel8bd11de2010-08-09 21:01:39 +0000653 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel3cbee302011-04-12 22:53:02 +0000654 TheCU->addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel3cf763d2010-09-29 23:07:21 +0000655 else if (DIVariable(DV->getVariable()).isArtificial())
Devang Patel3cbee302011-04-12 22:53:02 +0000656 TheCU->addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +0000657
658 if (Scope->isAbstractScope()) {
659 DV->setDIE(VariableDie);
660 return VariableDie;
661 }
662
663 // Add variable address.
664
665 unsigned Offset = DV->getDotDebugLocOffset();
666 if (Offset != ~0U) {
Devang Patel3cbee302011-04-12 22:53:02 +0000667 TheCU->addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
Devang Patelc3f5f782010-05-25 23:40:22 +0000668 Asm->GetTempSymbol("debug_loc", Offset));
669 DV->setDIE(VariableDie);
670 UseDotDebugLocEntry.insert(VariableDie);
671 return VariableDie;
672 }
673
674 // Check if variable is described by a DBG_VALUE instruction.
675 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
676 DbgVariableToDbgInstMap.find(DV);
677 if (DVI != DbgVariableToDbgInstMap.end()) {
678 const MachineInstr *DVInsn = DVI->second;
Devang Patelc3f5f782010-05-25 23:40:22 +0000679 bool updated = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000680 // FIXME : Handle getNumOperands != 3
Devang Patelc3f5f782010-05-25 23:40:22 +0000681 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +0000682 if (DVInsn->getOperand(0).isReg()) {
683 const MachineOperand RegOp = DVInsn->getOperand(0);
684 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
685 if (DVInsn->getOperand(1).isImm() &&
686 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000687 TheCU->addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
Devang Patel0b48ead2010-08-31 22:22:42 +0000688 updated = true;
689 } else
Devang Patel3cbee302011-04-12 22:53:02 +0000690 updated = TheCU->addRegisterAddress(VariableDie, RegOp);
Devang Patel0b48ead2010-08-31 22:22:42 +0000691 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000692 else if (DVInsn->getOperand(0).isImm())
Devang Patel3cbee302011-04-12 22:53:02 +0000693 updated = TheCU->addConstantValue(VariableDie, DVInsn->getOperand(0));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000694 else if (DVInsn->getOperand(0).isFPImm())
695 updated =
Devang Patel3cbee302011-04-12 22:53:02 +0000696 TheCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patelc3f5f782010-05-25 23:40:22 +0000697 } else {
698 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
699 if (Location.getReg()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000700 TheCU->addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patelc3f5f782010-05-25 23:40:22 +0000701 updated = true;
702 }
703 }
704 if (!updated) {
705 // If variableDie is not updated then DBG_VALUE instruction does not
706 // have valid variable info.
707 delete VariableDie;
708 return NULL;
709 }
710 DV->setDIE(VariableDie);
711 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000712 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000713
714 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +0000715 int FI = 0;
Devang Patel9e3bd2c2010-08-31 06:11:28 +0000716 if (findVariableFrameIndex(DV, &FI))
Devang Patel3cbee302011-04-12 22:53:02 +0000717 TheCU->addVariableAddress(DV, VariableDie, FI);
Devang Patel9e3bd2c2010-08-31 06:11:28 +0000718
Devang Patel53bb5c92009-11-10 23:06:00 +0000719 DV->setDIE(VariableDie);
720 return VariableDie;
721
722}
Devang Patel2c4ceb12009-11-21 02:48:08 +0000723
Devang Patel3cbee302011-04-12 22:53:02 +0000724void CompileUnit::addPubTypes(DISubprogram SP) {
Devang Patel193f7202009-11-24 01:14:22 +0000725 DICompositeType SPTy = SP.getType();
726 unsigned SPTag = SPTy.getTag();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000727 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel193f7202009-11-24 01:14:22 +0000728 return;
729
730 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +0000731 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +0000732 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +0000733 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +0000734 continue;
735 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +0000736 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +0000737 && !CATy.isForwardDecl()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000738 if (DIEEntry *Entry = getDIEEntry(CATy))
739 addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +0000740 }
741 }
742}
743
Devang Patel2c4ceb12009-11-21 02:48:08 +0000744/// constructScopeDIE - Construct a DIE for this scope.
745DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000746 if (!Scope || !Scope->getScopeNode())
747 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000748
Devang Patel5bc9fec2011-02-19 01:31:27 +0000749 SmallVector <DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000750
751 // Collect arguments for current function.
752 if (Scope == CurrentFnDbgScope)
753 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
754 if (DbgVariable *ArgDV = CurrentFnArguments[i])
755 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
756 Children.push_back(Arg);
757
Devang Patel5bc9fec2011-02-19 01:31:27 +0000758 // Collect lexical scope childrens first.
759 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
760 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
761 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
762 Children.push_back(Variable);
763 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
764 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
765 if (DIE *Nested = constructScopeDIE(Scopes[j]))
766 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000767 DIScope DS(Scope->getScopeNode());
768 DIE *ScopeDIE = NULL;
769 if (Scope->getInlinedAt())
770 ScopeDIE = constructInlinedScopeDIE(Scope);
771 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000772 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000773 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +0000774 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000775 // Note down abstract DIE.
776 if (ScopeDIE)
777 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
778 }
Devang Patel3c91b052010-03-08 20:52:55 +0000779 else
Devang Patel2db49d72010-05-07 18:11:54 +0000780 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000781 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000782 else {
783 // There is no need to emit empty lexical block DIE.
784 if (Children.empty())
785 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000786 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000787 }
788
Devang Patelaead63c2010-03-29 22:59:58 +0000789 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000790
Devang Patel5bc9fec2011-02-19 01:31:27 +0000791 // Add children
792 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
793 E = Children.end(); I != E; ++I)
794 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000795
Jim Grosbach1e20b962010-07-21 21:21:52 +0000796 if (DS.isSubprogram())
Devang Patel3cbee302011-04-12 22:53:02 +0000797 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000798
Devang Patel193f7202009-11-24 01:14:22 +0000799 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000800}
801
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000802/// GetOrCreateSourceID - Look up the source id with the given directory and
803/// source file names. If none currently exists, create a new id and insert it
804/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
805/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +0000806
Devang Patel23670e52011-03-24 20:30:50 +0000807unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
808 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000809 // If FE did not provide a file name, then assume stdin.
810 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000811 return GetOrCreateSourceID("<stdin>", StringRef());
812
813 // MCStream expects full path name as filename.
814 if (!DirName.empty() && !FileName.startswith("/")) {
815 std::string FullPathName(DirName.data());
816 if (!DirName.endswith("/"))
817 FullPathName += "/";
818 FullPathName += FileName.data();
819 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
820 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
821 }
Devang Patel1905a182010-09-16 20:57:49 +0000822
Rafael Espindola5c055632010-11-18 02:04:25 +0000823 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
824 if (Entry.getValue())
825 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000826
Rafael Espindola5c055632010-11-18 02:04:25 +0000827 unsigned SrcId = SourceIdMap.size();
828 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000829
Rafael Espindola5c055632010-11-18 02:04:25 +0000830 // Print out a .file directive to specify files for .loc directives.
Devang Patel23670e52011-03-24 20:30:50 +0000831 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000832
833 return SrcId;
834}
835
Devang Patel6404e4e2009-12-15 19:16:48 +0000836/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Devang Patel3cbee302011-04-12 22:53:02 +0000837DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
838 DIE *NDie = getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +0000839 if (NDie)
840 return NDie;
841 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel3cbee302011-04-12 22:53:02 +0000842 insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +0000843 if (!NS.getName().empty())
844 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patel81625742010-08-09 20:20:05 +0000845 addSourceLine(NDie, NS);
Devang Patel6404e4e2009-12-15 19:16:48 +0000846 addToContextOwner(NDie, NS.getContext());
847 return NDie;
848}
849
Jim Grosbach1e20b962010-07-21 21:21:52 +0000850/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000851/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000852void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000853 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000854 StringRef FN = DIUnit.getFilename();
855 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000856 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000857
858 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000859 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
860 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
861 DIUnit.getProducer());
862 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
863 DIUnit.getLanguage());
864 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000865 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
866 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000867 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000868 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000869 // compile unit in debug_line section.
Devang Patelae84d5b2010-08-31 23:50:19 +0000870 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
Devang Patel3cbee302011-04-12 22:53:02 +0000871 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
872 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000873 else
Devang Patel3cbee302011-04-12 22:53:02 +0000874 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000875
Devang Patel65dbc902009-11-25 17:36:49 +0000876 if (!Dir.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000877 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000878 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000879 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000880
Devang Patel65dbc902009-11-25 17:36:49 +0000881 StringRef Flags = DIUnit.getFlags();
882 if (!Flags.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000883 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
884
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000885 unsigned RVer = DIUnit.getRunTimeVersion();
886 if (RVer)
Devang Patel3cbee302011-04-12 22:53:02 +0000887 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000888 dwarf::DW_FORM_data1, RVer);
889
Devang Patel163a9f72010-05-10 22:49:55 +0000890 if (!FirstCU)
891 FirstCU = NewCU;
892 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000893}
894
Devang Patel163a9f72010-05-10 22:49:55 +0000895/// getCompielUnit - Get CompileUnit DIE.
896CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
897 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
898 DIDescriptor D(N);
899 const MDNode *CUNode = NULL;
900 if (D.isCompileUnit())
901 CUNode = N;
902 else if (D.isSubprogram())
903 CUNode = DISubprogram(N).getCompileUnit();
904 else if (D.isType())
905 CUNode = DIType(N).getCompileUnit();
906 else if (D.isGlobalVariable())
907 CUNode = DIGlobalVariable(N).getCompileUnit();
908 else if (D.isVariable())
909 CUNode = DIVariable(N).getCompileUnit();
910 else if (D.isNameSpace())
911 CUNode = DINameSpace(N).getCompileUnit();
912 else if (D.isFile())
913 CUNode = DIFile(N).getCompileUnit();
914 else
915 return FirstCU;
916
917 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
918 = CUMap.find(CUNode);
919 if (I == CUMap.end())
920 return FirstCU;
921 return I->second;
922}
923
Devang Patel0c4720c2010-08-23 18:25:56 +0000924/// isUnsignedDIType - Return true if type encoding is unsigned.
925static bool isUnsignedDIType(DIType Ty) {
926 DIDerivedType DTy(Ty);
927 if (DTy.Verify())
928 return isUnsignedDIType(DTy.getTypeDerivedFrom());
929
930 DIBasicType BTy(Ty);
931 if (BTy.Verify()) {
932 unsigned Encoding = BTy.getEncoding();
933 if (Encoding == dwarf::DW_ATE_unsigned ||
934 Encoding == dwarf::DW_ATE_unsigned_char)
935 return true;
936 }
937 return false;
938}
Devang Patel163a9f72010-05-10 22:49:55 +0000939
Devang Patele449d1f2011-01-20 00:02:16 +0000940// Return const exprssion if value is a GEP to access merged global
941// constant. e.g.
942// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
943static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
944 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
945 if (!CE || CE->getNumOperands() != 3 ||
946 CE->getOpcode() != Instruction::GetElementPtr)
947 return NULL;
948
949 // First operand points to a global value.
950 if (!isa<GlobalValue>(CE->getOperand(0)))
951 return NULL;
952
953 // Second operand is zero.
954 const ConstantInt *CI =
955 dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
956 if (!CI || !CI->isZero())
957 return NULL;
958
959 // Third operand is offset.
960 if (!isa<ConstantInt>(CE->getOperand(2)))
961 return NULL;
962
963 return CE;
964}
965
Devang Patel163a9f72010-05-10 22:49:55 +0000966/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000967void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +0000968 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000969
Devang Patel905cf5e2009-09-04 23:59:07 +0000970 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +0000971 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +0000972 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000973
974 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000975 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +0000976 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +0000977 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000978
Devang Patel9fa539c2010-08-10 01:37:23 +0000979 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +0000980 DIE *VariableDIE = new DIE(GV.getTag());
981
982 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000983
Devang Patel9fa539c2010-08-10 01:37:23 +0000984 // Add name.
Devang Patel3cbee302011-04-12 22:53:02 +0000985 TheCU->addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
986 GV.getDisplayName());
Devang Patel9fa539c2010-08-10 01:37:23 +0000987 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +0000988 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel3cbee302011-04-12 22:53:02 +0000989 TheCU->addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
990 dwarf::DW_FORM_string,
991 getRealLinkageName(LinkageName));
Devang Patel9fa539c2010-08-10 01:37:23 +0000992 // Add type.
Devang Patel3cbee302011-04-12 22:53:02 +0000993 TheCU->addType(VariableDIE, GTy);
Devang Patel50d80e32010-04-13 20:35:04 +0000994 if (GTy.isCompositeType() && !GTy.getName().empty()
995 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +0000996 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +0000997 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +0000998 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +0000999 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001000 // Add scoping info.
1001 if (!GV.isLocalToUnit()) {
Devang Patel3cbee302011-04-12 22:53:02 +00001002 TheCU->addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel9fa539c2010-08-10 01:37:23 +00001003 // Expose as global.
1004 TheCU->addGlobal(GV.getName(), VariableDIE);
1005 }
1006 // Add line number info.
Devang Patel3cbee302011-04-12 22:53:02 +00001007 TheCU->addSourceLine(VariableDIE, GV);
Devang Patel9fa539c2010-08-10 01:37:23 +00001008 // Add to map.
1009 TheCU->insertDIE(N, VariableDIE);
1010 // Add to context owner.
1011 DIDescriptor GVContext = GV.getContext();
Devang Patel3cbee302011-04-12 22:53:02 +00001012 TheCU->addToContextOwner(VariableDIE, GVContext);
Devang Patel9fa539c2010-08-10 01:37:23 +00001013 // Add location.
Devang Patel29368072010-08-10 07:11:13 +00001014 if (isGlobalVariable) {
1015 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel3cbee302011-04-12 22:53:02 +00001016 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1017 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patel29368072010-08-10 07:11:13 +00001018 Asm->Mang->getSymbol(GV.getGlobal()));
1019 // Do not create specification DIE if context is either compile unit
1020 // or a subprogram.
1021 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1022 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1023 // Create specification DIE.
1024 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel3cbee302011-04-12 22:53:02 +00001025 TheCU->addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
Devang Patel29368072010-08-10 07:11:13 +00001026 dwarf::DW_FORM_ref4, VariableDIE);
Devang Patel3cbee302011-04-12 22:53:02 +00001027 TheCU->addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1028 TheCU->addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel29368072010-08-10 07:11:13 +00001029 TheCU->addDie(VariableSpecDIE);
1030 } else {
Devang Patel3cbee302011-04-12 22:53:02 +00001031 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel29368072010-08-10 07:11:13 +00001032 }
Devang Patel76a788c2011-01-06 21:39:25 +00001033 } else if (ConstantInt *CI =
1034 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
Devang Patel3cbee302011-04-12 22:53:02 +00001035 TheCU->addConstantValue(VariableDIE, CI, isUnsignedDIType(GTy));
Devang Patele449d1f2011-01-20 00:02:16 +00001036 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
1037 // GV is a merged global.
1038 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel3cbee302011-04-12 22:53:02 +00001039 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1040 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
1041 Asm->Mang->getSymbol(cast<GlobalValue>(CE->getOperand(0))));
Devang Patele449d1f2011-01-20 00:02:16 +00001042 ConstantInt *CII = cast<ConstantInt>(CE->getOperand(2));
Devang Patel3cbee302011-04-12 22:53:02 +00001043 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1044 TheCU->addUInt(Block, 0, dwarf::DW_FORM_udata, CII->getZExtValue());
1045 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1046 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patele449d1f2011-01-20 00:02:16 +00001047 }
Devang Patel76a788c2011-01-06 21:39:25 +00001048
Devang Patel13e16b62009-06-26 01:49:18 +00001049 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001050}
1051
Devang Patel163a9f72010-05-10 22:49:55 +00001052/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001053void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001054 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001055
Stuart Hastings639336e2010-04-06 21:38:29 +00001056 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001057 CompileUnit *TheCU = getCompileUnit(N);
1058 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +00001059 return;
1060
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001061 if (!SP.isDefinition())
1062 // This is a method declaration which will be handled while constructing
1063 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001064 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001065
Stuart Hastings639336e2010-04-06 21:38:29 +00001066 DIE *SubprogramDie = createSubprogramDIE(SP);
1067
1068 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00001069 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001070
1071 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +00001072 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001073
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001074 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +00001075 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001076
Devang Patel13e16b62009-06-26 01:49:18 +00001077 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001078}
1079
Devang Patel2c4ceb12009-11-21 02:48:08 +00001080/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001081/// content. Create global DIEs and emit initial debug info sections.
1082/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00001083void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00001084 if (DisableDebugInfoPrinting)
1085 return;
1086
Devang Patel78ab9e22009-07-30 18:56:46 +00001087 DebugInfoFinder DbgFinder;
1088 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001089
Chris Lattnerd850ac72010-04-05 02:19:28 +00001090 bool HasDebugInfo = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001091
Chris Lattnerd850ac72010-04-05 02:19:28 +00001092 // Scan all the compile-units to see if there are any marked as the main unit.
1093 // if not, we do not generate debug info.
1094 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1095 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1096 if (DICompileUnit(*I).isMain()) {
1097 HasDebugInfo = true;
1098 break;
1099 }
1100 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001101
Chris Lattnerd850ac72010-04-05 02:19:28 +00001102 if (!HasDebugInfo) return;
1103
1104 // Tell MMI that we have debug info.
1105 MMI->setDebugInfoAvailability(true);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001106
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001107 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00001108 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001109
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001110 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001111 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1112 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001113 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001114
Devang Patel53bb5c92009-11-10 23:06:00 +00001115 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001116 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1117 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001118 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001119
Devang Patelc366f832009-12-10 19:14:49 +00001120 // Create DIEs for each global variable.
1121 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1122 E = DbgFinder.global_variable_end(); I != E; ++I)
1123 constructGlobalVariableDIE(*I);
1124
Devang Patele7e5a0f2010-08-10 20:01:20 +00001125 //getOrCreateTypeDIE
1126 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patel3cbee302011-04-12 22:53:02 +00001127 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1128 DIType Ty(NMD->getOperand(i));
1129 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1130 }
Devang Patele7e5a0f2010-08-10 20:01:20 +00001131
Devang Patel1a7ca032010-09-28 18:08:20 +00001132 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patel3cbee302011-04-12 22:53:02 +00001133 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1134 DIType Ty(NMD->getOperand(i));
1135 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1136 }
Devang Patel1a7ca032010-09-28 18:08:20 +00001137
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001138 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001139 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001140}
1141
Devang Patel2c4ceb12009-11-21 02:48:08 +00001142/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001143///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001144void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00001145 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +00001146 const Module *M = MMI->getModule();
Devang Patele9a1cca2010-08-02 17:32:15 +00001147 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +00001148 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
1149 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
1150 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
1151 DISubprogram SP(AllSPs->getOperand(SI));
1152 if (!SP.Verify()) continue;
1153
1154 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +00001155 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +00001156 StringRef FName = SP.getLinkageName();
1157 if (FName.empty())
1158 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00001159 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +00001160 if (!NMD) continue;
1161 unsigned E = NMD->getNumOperands();
1162 if (!E) continue;
1163 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Patele9a1cca2010-08-02 17:32:15 +00001164 DeadFnScopeMap[SP] = Scope;
Devang Patel4a1cad62010-06-28 18:25:03 +00001165 for (unsigned I = 0; I != E; ++I) {
1166 DIVariable DV(NMD->getOperand(I));
1167 if (!DV.Verify()) continue;
1168 Scope->addVariable(new DbgVariable(DV));
1169 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001170
Devang Patel4a1cad62010-06-28 18:25:03 +00001171 // Construct subprogram DIE and add variables DIEs.
1172 constructSubprogramDIE(SP);
1173 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patele03161c2010-08-09 18:51:29 +00001174 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel4a1cad62010-06-28 18:25:03 +00001175 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1176 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
1177 if (VariableDIE)
1178 ScopeDIE->addChild(VariableDIE);
1179 }
1180 }
1181 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001182
Devang Patel53bb5c92009-11-10 23:06:00 +00001183 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1184 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1185 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1186 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +00001187 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001188 }
1189
Devang Patele9f8f5e2010-05-07 20:54:48 +00001190 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00001191 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1192 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001193 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00001194 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00001195 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00001196 if (!NDie) continue;
Devang Patel3cbee302011-04-12 22:53:02 +00001197 getCompileUnit(N)->addDIEEntry(SPDie, dwarf::DW_AT_containing_type,
1198 dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00001199 }
1200
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001201 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001202 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001203 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001204 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001205 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001206
1207 // End text sections.
1208 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001209 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00001210 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001211 }
1212
1213 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001214 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001215
1216 // Emit function debug frame information
1217 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1218 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001219 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001220
1221 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001222 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001223
1224 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001225 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001226
1227 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001228 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001229
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001230 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001231 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001232
Devang Patel193f7202009-11-24 01:14:22 +00001233 // Emit info into a debug pubtypes section.
1234 emitDebugPubTypes();
1235
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001236 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001237 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001238
1239 // Emit info into a debug aranges section.
1240 EmitDebugARanges();
1241
1242 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001243 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001244
1245 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001246 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001247
1248 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001249 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001250
Chris Lattnerbc733f52010-03-13 02:17:42 +00001251 // Emit info into a debug str section.
1252 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001253
Devang Patele9a1cca2010-08-02 17:32:15 +00001254 // clean up.
1255 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +00001256 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1257 E = CUMap.end(); I != E; ++I)
1258 delete I->second;
1259 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001260}
1261
Devang Patel53bb5c92009-11-10 23:06:00 +00001262/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001263DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattnerde4845c2010-04-02 19:42:39 +00001264 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001265
Devang Patel2db49d72010-05-07 18:11:54 +00001266 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001267 if (AbsDbgVariable)
1268 return AbsDbgVariable;
1269
Devang Patel2db49d72010-05-07 18:11:54 +00001270 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00001271 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00001272 if (!Scope)
1273 return NULL;
1274
Devang Patel26c1e562010-05-20 16:36:41 +00001275 AbsDbgVariable = new DbgVariable(Var);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001276 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00001277 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00001278 return AbsDbgVariable;
1279}
1280
Devang Patel0478c152011-03-01 22:58:55 +00001281/// addCurrentFnArgument - If Var is an current function argument that add
1282/// it in CurrentFnArguments list.
1283bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
1284 DbgVariable *Var, DbgScope *Scope) {
1285 if (Scope != CurrentFnDbgScope)
1286 return false;
1287 DIVariable DV = Var->getVariable();
1288 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1289 return false;
1290 unsigned ArgNo = DV.getArgNumber();
1291 if (ArgNo == 0)
1292 return false;
1293
Devang Patelcb3a6572011-03-03 20:02:02 +00001294 size_t Size = CurrentFnArguments.size();
1295 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001296 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +00001297 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +00001298 // arguments does the function have at source level.
1299 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +00001300 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +00001301 CurrentFnArguments[ArgNo - 1] = Var;
1302 return true;
1303}
1304
Devang Patelee432862010-05-20 19:57:06 +00001305/// collectVariableInfoFromMMITable - Collect variable information from
1306/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001307void
Devang Patelee432862010-05-20 19:57:06 +00001308DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
1309 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001310 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patele717faa2009-10-06 01:26:37 +00001311 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1312 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1313 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00001314 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001315 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00001316 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001317 DIVariable DV(Var);
1318 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001319
Chris Lattnerde4845c2010-04-02 19:42:39 +00001320 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001321 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00001322 Scope = ConcreteScopes.lookup(IA);
1323 if (Scope == 0)
1324 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001325
Devang Patelfb0ee432009-11-10 23:20:04 +00001326 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00001327 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00001328 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001329
Devang Patel26c1e562010-05-20 16:36:41 +00001330 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
1331 DbgVariable *RegVar = new DbgVariable(DV);
1332 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel0478c152011-03-01 22:58:55 +00001333 if (!addCurrentFnArgument(MF, RegVar, Scope))
1334 Scope->addVariable(RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +00001335 if (AbsDbgVariable) {
1336 recordVariableFrameIndex(AbsDbgVariable, VP.first);
1337 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
1338 }
Devang Patele717faa2009-10-06 01:26:37 +00001339 }
Devang Patelee432862010-05-20 19:57:06 +00001340}
Devang Patel90a48ad2010-03-15 18:33:46 +00001341
Jim Grosbach1e20b962010-07-21 21:21:52 +00001342/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00001343/// DBG_VALUE instruction, is in a defined reg.
1344static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
1345 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001346 return MI->getNumOperands() == 3 &&
1347 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1348 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +00001349}
1350
Devang Patelee432862010-05-20 19:57:06 +00001351/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001352void
Devang Patel78e127d2010-06-25 22:07:34 +00001353DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1354 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001355
Devang Patelee432862010-05-20 19:57:06 +00001356 /// collection info from MMI table.
1357 collectVariableInfoFromMMITable(MF, Processed);
1358
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001359 for (SmallVectorImpl<const MDNode*>::const_iterator
1360 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1361 ++UVI) {
1362 const MDNode *Var = *UVI;
1363 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001364 continue;
1365
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001366 // History contains relevant DBG_VALUE instructions for Var and instructions
1367 // clobbering it.
1368 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1369 if (History.empty())
1370 continue;
1371 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001372
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001373 DIVariable DV(Var);
Devang Patela36478f2011-01-11 21:42:10 +00001374 DbgScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001375 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1376 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelc0c5a262010-05-21 00:10:20 +00001377 Scope = CurrentFnDbgScope;
Devang Patela36478f2011-01-11 21:42:10 +00001378 else
1379 Scope = findDbgScope(MInsn);
Devang Patelee432862010-05-20 19:57:06 +00001380 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001381 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001382 continue;
1383
1384 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001385 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patelee432862010-05-20 19:57:06 +00001386 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel0478c152011-03-01 22:58:55 +00001387 if (!addCurrentFnArgument(MF, RegVar, Scope))
1388 Scope->addVariable(RegVar);
Devang Patelc0c5a262010-05-21 00:10:20 +00001389 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
1390 DbgVariableToDbgInstMap[AbsVar] = MInsn;
1391 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00001392 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001393
1394 // Simple ranges that are fully coalesced.
1395 if (History.size() <= 1 || (History.size() == 2 &&
1396 MInsn->isIdenticalTo(History.back()))) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001397 DbgVariableToDbgInstMap[RegVar] = MInsn;
1398 continue;
1399 }
1400
1401 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001402 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001403
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001404 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1405 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1406 const MachineInstr *Begin = *HI;
1407 assert(Begin->isDebugValue() && "Invalid History entry");
Devang Patelc3f5f782010-05-25 23:40:22 +00001408 MachineLocation MLoc;
Devang Patelb2cf5812010-08-04 18:40:52 +00001409 if (Begin->getNumOperands() == 3) {
1410 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
1411 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
1412 } else
1413 MLoc = Asm->getDebugValueLocation(Begin);
1414
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001415 // FIXME: emitDebugLoc only understands registers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001416 if (!MLoc.getReg())
1417 continue;
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001418
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001419 // Compute the range for a register location.
1420 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1421 const MCSymbol *SLabel = 0;
1422
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001423 if (HI + 1 == HE)
1424 // If Begin is the last instruction in History then its value is valid
Devang Patelc3f5f782010-05-25 23:40:22 +00001425 // until the end of the funtion.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001426 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001427 else {
1428 const MachineInstr *End = HI[1];
1429 if (End->isDebugValue())
1430 SLabel = getLabelBeforeInsn(End);
1431 else {
1432 // End is a normal instruction clobbering the range.
1433 SLabel = getLabelAfterInsn(End);
1434 assert(SLabel && "Forgot label after clobber instruction");
1435 ++HI;
1436 }
1437 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001438
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001439 // The value is valid until the next DBG_VALUE or clobber.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001440 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
Devang Patelc3f5f782010-05-25 23:40:22 +00001441 }
1442 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001443 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001444
1445 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00001446 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00001447 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00001448 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001449 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00001450 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00001451 continue;
1452 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
1453 if (Scope)
Devang Patel26c1e562010-05-20 16:36:41 +00001454 Scope->addVariable(new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00001455 }
1456 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001457}
Devang Patel98e1cac2010-05-14 21:01:35 +00001458
Devang Patelc3f5f782010-05-25 23:40:22 +00001459/// getLabelBeforeInsn - Return Label preceding the instruction.
1460const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001461 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1462 assert(Label && "Didn't insert label before instruction");
1463 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001464}
1465
1466/// getLabelAfterInsn - Return Label immediately following the instruction.
1467const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001468 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001469}
1470
Devang Patelcbbe2872010-10-26 17:49:02 +00001471/// beginInstruction - Process beginning of an instruction.
1472void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001473 // Check if source location changes, but ignore DBG_VALUE locations.
1474 if (!MI->isDebugValue()) {
1475 DebugLoc DL = MI->getDebugLoc();
1476 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
1477 PrevInstLoc = DL;
1478 if (!DL.isUnknown()) {
1479 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
1480 recordSourceLine(DL.getLine(), DL.getCol(), Scope);
1481 } else
1482 recordSourceLine(0, 0, 0);
1483 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001484 }
Devang Patelaead63c2010-03-29 22:59:58 +00001485
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001486 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001487 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1488 LabelsBeforeInsn.find(MI);
1489
1490 // No label needed.
1491 if (I == LabelsBeforeInsn.end())
1492 return;
1493
1494 // Label already assigned.
1495 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001496 return;
Devang Patel553881b2010-03-29 17:20:31 +00001497
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001498 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001499 PrevLabel = MMI->getContext().CreateTempSymbol();
1500 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001501 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001502 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001503}
1504
Devang Patelcbbe2872010-10-26 17:49:02 +00001505/// endInstruction - Process end of an instruction.
1506void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001507 // Don't create a new label after DBG_VALUE instructions.
1508 // They don't generate code.
1509 if (!MI->isDebugValue())
1510 PrevLabel = 0;
1511
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001512 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1513 LabelsAfterInsn.find(MI);
1514
1515 // No label needed.
1516 if (I == LabelsAfterInsn.end())
1517 return;
1518
1519 // Label already assigned.
1520 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001521 return;
1522
1523 // We need a label after this instruction.
1524 if (!PrevLabel) {
1525 PrevLabel = MMI->getContext().CreateTempSymbol();
1526 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001527 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001528 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001529}
1530
Devang Pateleac9c072010-04-27 19:46:33 +00001531/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001532DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel61409622010-07-07 20:12:52 +00001533 const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001534 if (!InlinedAt) {
1535 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1536 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00001537 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001538 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1539 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00001540 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001541 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00001542 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00001543 WScope->setParent(Parent);
1544 Parent->addScope(WScope);
1545 }
1546
1547 if (!WScope->getParent()) {
1548 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastingscad22ad2010-06-15 23:06:30 +00001549 // We used to check only for a linkage name, but that fails
1550 // since we began omitting the linkage name for private
1551 // functions. The new way is to check for the name in metadata,
1552 // but that's not supported in old .ll test cases. Ergo, we
1553 // check both.
Stuart Hastings215aa152010-06-11 20:08:44 +00001554 if (SPName == Asm->MF->getFunction()->getName() ||
1555 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Pateleac9c072010-04-27 19:46:33 +00001556 CurrentFnDbgScope = WScope;
1557 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001558
Devang Pateleac9c072010-04-27 19:46:33 +00001559 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001560 }
1561
Devang Patel78e127d2010-06-25 22:07:34 +00001562 getOrCreateAbstractScope(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001563 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1564 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00001565 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001566
1567 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1568 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1569 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00001570 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00001571 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00001572 WScope->setParent(Parent);
1573 Parent->addScope(WScope);
1574
1575 ConcreteScopes[InlinedAt] = WScope;
Devang Pateleac9c072010-04-27 19:46:33 +00001576
1577 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00001578}
1579
Devang Pateleac9c072010-04-27 19:46:33 +00001580/// hasValidLocation - Return true if debug location entry attached with
1581/// machine instruction encodes valid location info.
1582static bool hasValidLocation(LLVMContext &Ctx,
1583 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00001584 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00001585 DebugLoc DL = MInsn->getDebugLoc();
1586 if (DL.isUnknown()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001587
Devang Patele9f8f5e2010-05-07 20:54:48 +00001588 const MDNode *S = DL.getScope(Ctx);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001589
Devang Pateleac9c072010-04-27 19:46:33 +00001590 // There is no need to create another DIE for compile unit. For all
1591 // other scopes, create one DbgScope now. This will be translated
1592 // into a scope DIE at the end.
1593 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001594
Devang Pateleac9c072010-04-27 19:46:33 +00001595 Scope = S;
1596 InlinedAt = DL.getInlinedAt(Ctx);
1597 return true;
1598}
1599
1600/// calculateDominanceGraph - Calculate dominance graph for DbgScope
1601/// hierarchy.
1602static void calculateDominanceGraph(DbgScope *Scope) {
1603 assert (Scope && "Unable to calculate scop edominance graph!");
1604 SmallVector<DbgScope *, 4> WorkStack;
1605 WorkStack.push_back(Scope);
1606 unsigned Counter = 0;
1607 while (!WorkStack.empty()) {
1608 DbgScope *WS = WorkStack.back();
1609 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
1610 bool visitedChildren = false;
1611 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
1612 SE = Children.end(); SI != SE; ++SI) {
1613 DbgScope *ChildScope = *SI;
1614 if (!ChildScope->getDFSOut()) {
1615 WorkStack.push_back(ChildScope);
1616 visitedChildren = true;
1617 ChildScope->setDFSIn(++Counter);
1618 break;
1619 }
1620 }
1621 if (!visitedChildren) {
1622 WorkStack.pop_back();
1623 WS->setDFSOut(++Counter);
1624 }
1625 }
1626}
1627
1628/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001629static
Devang Pateleac9c072010-04-27 19:46:33 +00001630void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
1631 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
1632{
1633#ifndef NDEBUG
1634 unsigned PrevDFSIn = 0;
1635 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1636 I != E; ++I) {
1637 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1638 II != IE; ++II) {
1639 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001640 const MDNode *Scope = NULL;
1641 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00001642
1643 // Check if instruction has valid location information.
1644 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
1645 dbgs() << " [ ";
Jim Grosbach1e20b962010-07-21 21:21:52 +00001646 if (InlinedAt)
Devang Pateleac9c072010-04-27 19:46:33 +00001647 dbgs() << "*";
Jim Grosbach1e20b962010-07-21 21:21:52 +00001648 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Pateleac9c072010-04-27 19:46:33 +00001649 MI2ScopeMap.find(MInsn);
1650 if (DI != MI2ScopeMap.end()) {
1651 DbgScope *S = DI->second;
1652 dbgs() << S->getDFSIn();
1653 PrevDFSIn = S->getDFSIn();
1654 } else
1655 dbgs() << PrevDFSIn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001656 } else
Devang Pateleac9c072010-04-27 19:46:33 +00001657 dbgs() << " [ x" << PrevDFSIn;
1658 dbgs() << " ]";
1659 MInsn->dump();
1660 }
1661 dbgs() << "\n";
1662 }
1663#endif
1664}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001665/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00001666/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00001667bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00001668 // If scope information was extracted using .dbg intrinsics then there is not
1669 // any need to extract these information by scanning each instruction.
1670 if (!DbgScopeMap.empty())
1671 return false;
1672
Dan Gohman314bf7c2010-04-23 01:18:53 +00001673 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00001674 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
1675 SmallVector<DbgRange, 4> MIRanges;
1676 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001677 const MDNode *PrevScope = NULL;
1678 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00001679 const MachineInstr *RangeBeginMI = NULL;
1680 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00001681 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00001682 I != E; ++I) {
1683 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1684 II != IE; ++II) {
1685 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001686 const MDNode *Scope = NULL;
1687 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00001688
1689 // Check if instruction has valid location information.
1690 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
1691 PrevMI = MInsn;
1692 continue;
1693 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001694
Devang Pateleac9c072010-04-27 19:46:33 +00001695 // If scope has not changed then skip this instruction.
1696 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
1697 PrevMI = MInsn;
1698 continue;
1699 }
1700
Devang Pateld3526ea2011-02-15 17:56:09 +00001701 // Ignore DBG_VALUE. It does not contribute any instruction in output.
1702 if (MInsn->isDebugValue())
1703 continue;
1704
Jim Grosbach1e20b962010-07-21 21:21:52 +00001705 if (RangeBeginMI) {
1706 // If we have alread seen a beginning of a instruction range and
Devang Pateleac9c072010-04-27 19:46:33 +00001707 // current instruction scope does not match scope of first instruction
1708 // in this range then create a new instruction range.
1709 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001710 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel61409622010-07-07 20:12:52 +00001711 PrevInlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00001712 MIRanges.push_back(R);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001713 }
Devang Pateleac9c072010-04-27 19:46:33 +00001714
1715 // This is a beginning of a new instruction range.
1716 RangeBeginMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001717
Devang Pateleac9c072010-04-27 19:46:33 +00001718 // Reset previous markers.
1719 PrevMI = MInsn;
1720 PrevScope = Scope;
1721 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00001722 }
1723 }
1724
Devang Pateleac9c072010-04-27 19:46:33 +00001725 // Create last instruction range.
1726 if (RangeBeginMI && PrevMI && PrevScope) {
1727 DbgRange R(RangeBeginMI, PrevMI);
1728 MIRanges.push_back(R);
1729 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00001730 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001731
Devang Patel344130e2010-01-04 20:44:00 +00001732 if (!CurrentFnDbgScope)
1733 return false;
1734
Devang Pateleac9c072010-04-27 19:46:33 +00001735 calculateDominanceGraph(CurrentFnDbgScope);
1736 if (PrintDbgScope)
1737 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
1738
1739 // Find ranges of instructions covered by each DbgScope;
1740 DbgScope *PrevDbgScope = NULL;
1741 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
1742 RE = MIRanges.end(); RI != RE; ++RI) {
1743 const DbgRange &R = *RI;
1744 DbgScope *S = MI2ScopeMap.lookup(R.first);
1745 assert (S && "Lost DbgScope for a machine instruction!");
1746 if (PrevDbgScope && !PrevDbgScope->dominates(S))
1747 PrevDbgScope->closeInsnRange(S);
1748 S->openInsnRange(R.first);
1749 S->extendInsnRange(R.second);
1750 PrevDbgScope = S;
1751 }
1752
1753 if (PrevDbgScope)
1754 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00001755
Devang Patele37b0c62010-04-08 18:43:56 +00001756 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00001757
1758 return !DbgScopeMap.empty();
1759}
1760
Jim Grosbach1e20b962010-07-21 21:21:52 +00001761/// identifyScopeMarkers() -
Devang Pateleac9c072010-04-27 19:46:33 +00001762/// Each DbgScope has first instruction and last instruction to mark beginning
1763/// and end of a scope respectively. Create an inverse map that list scopes
1764/// starts (and ends) with an instruction. One instruction may start (or end)
1765/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001766void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00001767 SmallVector<DbgScope *, 4> WorkList;
1768 WorkList.push_back(CurrentFnDbgScope);
1769 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00001770 DbgScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001771
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001772 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001773 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001774 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001775 SE = Children.end(); SI != SE; ++SI)
1776 WorkList.push_back(*SI);
1777
Devang Patel53bb5c92009-11-10 23:06:00 +00001778 if (S->isAbstractScope())
1779 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001780
Devang Pateleac9c072010-04-27 19:46:33 +00001781 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
1782 if (Ranges.empty())
1783 continue;
1784 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1785 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001786 assert(RI->first && "DbgRange does not have first instruction!");
1787 assert(RI->second && "DbgRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001788 requestLabelBeforeInsn(RI->first);
1789 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001790 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001791 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001792}
1793
Dan Gohman084751c2010-04-20 00:37:27 +00001794/// FindFirstDebugLoc - Find the first debug location in the function. This
1795/// is intended to be an approximation for the source position of the
1796/// beginning of the function.
1797static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
1798 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1799 I != E; ++I)
1800 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
1801 MBBI != MBBE; ++MBBI) {
1802 DebugLoc DL = MBBI->getDebugLoc();
1803 if (!DL.isUnknown())
1804 return DL;
1805 }
1806 return DebugLoc();
1807}
1808
Devang Patel9a31f0f2010-10-25 20:45:32 +00001809#ifndef NDEBUG
1810/// CheckLineNumbers - Count basicblocks whose instructions do not have any
1811/// line number information.
1812static void CheckLineNumbers(const MachineFunction *MF) {
1813 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1814 I != E; ++I) {
1815 bool FoundLineNo = false;
1816 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1817 II != IE; ++II) {
1818 const MachineInstr *MI = II;
1819 if (!MI->getDebugLoc().isUnknown()) {
1820 FoundLineNo = true;
1821 break;
1822 }
1823 }
Devang Patel4d7f9a02010-10-28 22:11:59 +00001824 if (!FoundLineNo && I->size())
Devang Patel9a31f0f2010-10-25 20:45:32 +00001825 ++BlocksWithoutLineNo;
1826 }
1827}
1828#endif
1829
Devang Patel2c4ceb12009-11-21 02:48:08 +00001830/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001831/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001832void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001833 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00001834 if (!extractScopeInformation()) return;
Devang Patel60b35bd2009-10-06 18:37:31 +00001835
Devang Patel9a31f0f2010-10-25 20:45:32 +00001836#ifndef NDEBUG
1837 CheckLineNumbers(MF);
1838#endif
1839
Devang Pateleac9c072010-04-27 19:46:33 +00001840 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1841 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001842 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001843 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001844
1845 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1846 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00001847 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001848 if (FDL.isUnknown()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001849
Devang Patele9f8f5e2010-05-07 20:54:48 +00001850 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings0db42712010-07-19 23:56:30 +00001851 const MDNode *TheScope = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001852
Chris Lattnerde4845c2010-04-02 19:42:39 +00001853 DISubprogram SP = getDISubprogram(Scope);
1854 unsigned Line, Col;
1855 if (SP.Verify()) {
1856 Line = SP.getLineNumber();
1857 Col = 0;
Stuart Hastings0db42712010-07-19 23:56:30 +00001858 TheScope = SP;
Chris Lattnerde4845c2010-04-02 19:42:39 +00001859 } else {
1860 Line = FDL.getLine();
1861 Col = FDL.getCol();
Stuart Hastings0db42712010-07-19 23:56:30 +00001862 TheScope = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001863 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001864
Stuart Hastings0db42712010-07-19 23:56:30 +00001865 recordSourceLine(Line, Col, TheScope);
Devang Patelb2b31a62010-05-26 19:37:24 +00001866
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001867 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1868
Devang Patelb9abe9f2010-06-02 16:42:51 +00001869 /// ProcessedArgs - Collection of arguments already processed.
1870 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
1871
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001872 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1873
1874 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1875 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1876
Devang Patelb2b31a62010-05-26 19:37:24 +00001877 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001878 I != E; ++I) {
1879 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001880 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1881 II != IE; ++II) {
1882 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001883
Devang Patelb2b31a62010-05-26 19:37:24 +00001884 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00001885 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001886
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001887 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001888 const MDNode *Var =
1889 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001890
1891 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001892 if (isDbgValueInDefinedReg(MI))
1893 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1894
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001895 // Check the history of this variable.
1896 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1897 if (History.empty()) {
1898 UserVariables.push_back(Var);
1899 // The first mention of a function argument gets the FunctionBeginSym
1900 // label, so arguments are visible when breaking at function entry.
1901 DIVariable DV(Var);
1902 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1903 DISubprogram(getDISubprogram(DV.getContext()))
1904 .describes(MF->getFunction()))
1905 LabelsBeforeInsn[MI] = FunctionBeginSym;
1906 } else {
1907 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1908 const MachineInstr *Prev = History.back();
1909 if (Prev->isDebugValue()) {
1910 // Coalesce identical entries at the end of History.
1911 if (History.size() >= 2 &&
1912 Prev->isIdenticalTo(History[History.size() - 2]))
1913 History.pop_back();
1914
1915 // Terminate old register assignments that don't reach MI;
1916 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1917 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1918 isDbgValueInDefinedReg(Prev)) {
1919 // Previous register assignment needs to terminate at the end of
1920 // its basic block.
1921 MachineBasicBlock::const_iterator LastMI =
1922 PrevMBB->getLastNonDebugInstr();
1923 if (LastMI == PrevMBB->end())
1924 // Drop DBG_VALUE for empty range.
1925 History.pop_back();
1926 else {
1927 // Terminate after LastMI.
1928 History.push_back(LastMI);
1929 }
1930 }
1931 }
1932 }
1933 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001934 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001935 // Not a DBG_VALUE instruction.
1936 if (!MI->isLabel())
1937 AtBlockEntry = false;
1938
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001939 // Check if the instruction clobbers any registers with debug vars.
1940 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1941 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1942 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1943 continue;
1944 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1945 unsigned Reg = *AI; ++AI) {
1946 const MDNode *Var = LiveUserVar[Reg];
1947 if (!Var)
1948 continue;
1949 // Reg is now clobbered.
1950 LiveUserVar[Reg] = 0;
1951
1952 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001953 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1954 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001955 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001956 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1957 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001958 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001959 const MachineInstr *Prev = History.back();
1960 // Sanity-check: Register assignments are terminated at the end of
1961 // their block.
1962 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1963 continue;
1964 // Is the variable still in Reg?
1965 if (!isDbgValueInDefinedReg(Prev) ||
1966 Prev->getOperand(0).getReg() != Reg)
1967 continue;
1968 // Var is clobbered. Make sure the next instruction gets a label.
1969 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001970 }
1971 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001972 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001973 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001974 }
1975
1976 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1977 I != E; ++I) {
1978 SmallVectorImpl<const MachineInstr*> &History = I->second;
1979 if (History.empty())
1980 continue;
1981
1982 // Make sure the final register assignments are terminated.
1983 const MachineInstr *Prev = History.back();
1984 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1985 const MachineBasicBlock *PrevMBB = Prev->getParent();
1986 MachineBasicBlock::const_iterator LastMI = PrevMBB->getLastNonDebugInstr();
1987 if (LastMI == PrevMBB->end())
1988 // Drop DBG_VALUE for empty range.
1989 History.pop_back();
1990 else {
1991 // Terminate after LastMI.
1992 History.push_back(LastMI);
1993 }
1994 }
1995 // Request labels for the full history.
1996 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1997 const MachineInstr *MI = History[i];
1998 if (MI->isDebugValue())
1999 requestLabelBeforeInsn(MI);
2000 else
2001 requestLabelAfterInsn(MI);
2002 }
2003 }
Devang Patelb2b31a62010-05-26 19:37:24 +00002004
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00002005 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00002006 PrevLabel = FunctionBeginSym;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002007}
2008
Devang Patel2c4ceb12009-11-21 02:48:08 +00002009/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002010///
Chris Lattnereec791a2010-01-26 23:18:02 +00002011void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002012 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002013
Devang Patel344130e2010-01-04 20:44:00 +00002014 if (CurrentFnDbgScope) {
Devang Patel65eb4822010-05-22 00:04:14 +00002015
Devang Patelc3f5f782010-05-25 23:40:22 +00002016 // Define end label for subprogram.
2017 FunctionEndSym = Asm->GetTempSymbol("func_end",
2018 Asm->getFunctionNumber());
2019 // Assumes in correct section after the entry point.
2020 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002021
Devang Patel78e127d2010-06-25 22:07:34 +00002022 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2023 collectVariableInfo(MF, ProcessedVars);
Devang Patel65eb4822010-05-22 00:04:14 +00002024
Devang Patel344130e2010-01-04 20:44:00 +00002025 // Construct abstract scopes.
2026 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel78e127d2010-06-25 22:07:34 +00002027 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel78e127d2010-06-25 22:07:34 +00002028 DISubprogram SP((*AI)->getScopeNode());
2029 if (SP.Verify()) {
2030 // Collect info for variables that were optimized out.
2031 StringRef FName = SP.getLinkageName();
2032 if (FName.empty())
2033 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002034 if (NamedMDNode *NMD =
2035 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
Devang Patel78e127d2010-06-25 22:07:34 +00002036 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002037 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00002038 if (!DV || !ProcessedVars.insert(DV))
2039 continue;
Devang Patel1d68d212010-06-30 00:11:08 +00002040 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel78e127d2010-06-25 22:07:34 +00002041 if (Scope)
2042 Scope->addVariable(new DbgVariable(DV));
2043 }
2044 }
2045 }
Devang Patel90e19aa2010-06-30 01:40:11 +00002046 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2047 constructScopeDIE(*AI);
Devang Patel78e127d2010-06-25 22:07:34 +00002048 }
Devang Patel61409622010-07-07 20:12:52 +00002049
Devang Patel9c488372010-05-04 06:15:30 +00002050 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002051
Devang Patel9c488372010-05-04 06:15:30 +00002052 if (!DisableFramePointerElim(*MF))
Devang Patel3cbee302011-04-12 22:53:02 +00002053 getCompileUnit(CurrentFnDbgScope->getScopeNode())->addUInt(CurFnDIE,
2054 dwarf::DW_AT_APPLE_omit_frame_ptr,
2055 dwarf::DW_FORM_flag, 1);
Devang Patel9c488372010-05-04 06:15:30 +00002056
2057
Chris Lattnerd38fee82010-04-05 00:13:49 +00002058 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002059 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002060 }
2061
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002062 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002063 CurrentFnDbgScope = NULL;
Devang Patel0478c152011-03-01 22:58:55 +00002064 CurrentFnArguments.clear();
Devang Patel26c1e562010-05-20 16:36:41 +00002065 DbgVariableToFrameIndexMap.clear();
2066 VarToAbstractVarMap.clear();
2067 DbgVariableToDbgInstMap.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002068 DeleteContainerSeconds(DbgScopeMap);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00002069 UserVariables.clear();
2070 DbgValues.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002071 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002072 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002073 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002074 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002075 LabelsBeforeInsn.clear();
2076 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002077 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002078}
2079
Devang Patel26c1e562010-05-20 16:36:41 +00002080/// recordVariableFrameIndex - Record a variable's index.
2081void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2082 assert (V && "Invalid DbgVariable!");
2083 DbgVariableToFrameIndexMap[V] = Index;
2084}
2085
2086/// findVariableFrameIndex - Return true if frame index for the variable
2087/// is found. Update FI to hold value of the index.
2088bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2089 assert (V && "Invalid DbgVariable!");
2090 DenseMap<const DbgVariable *, int>::iterator I =
2091 DbgVariableToFrameIndexMap.find(V);
2092 if (I == DbgVariableToFrameIndexMap.end())
2093 return false;
2094 *FI = I->second;
2095 return true;
2096}
2097
Jim Grosbach1e20b962010-07-21 21:21:52 +00002098/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patelee432862010-05-20 19:57:06 +00002099/// instruction.
2100DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2101 DbgScope *Scope = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002102 LLVMContext &Ctx =
Devang Patelee432862010-05-20 19:57:06 +00002103 MInsn->getParent()->getParent()->getFunction()->getContext();
2104 DebugLoc DL = MInsn->getDebugLoc();
2105
Jim Grosbach1e20b962010-07-21 21:21:52 +00002106 if (DL.isUnknown())
Devang Patelee432862010-05-20 19:57:06 +00002107 return Scope;
2108
2109 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2110 Scope = ConcreteScopes.lookup(IA);
2111 if (Scope == 0)
2112 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002113
Devang Patelee432862010-05-20 19:57:06 +00002114 return Scope;
2115}
2116
2117
Chris Lattnerc6087842010-03-09 04:54:43 +00002118/// recordSourceLine - Register a source line with debug info. Returns the
2119/// unique label that was emitted and which provides correspondence to
2120/// the source line list.
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00002121void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S){
Devang Patel65dbc902009-11-25 17:36:49 +00002122 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00002123 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00002124 unsigned Src = 1;
2125 if (S) {
2126 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002127
Dan Gohman1cc0d622010-05-05 23:41:32 +00002128 if (Scope.isCompileUnit()) {
2129 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002130 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00002131 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00002132 } else if (Scope.isFile()) {
2133 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00002134 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00002135 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002136 } else if (Scope.isSubprogram()) {
2137 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002138 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00002139 Dir = SP.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002140 } else if (Scope.isLexicalBlock()) {
2141 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002142 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00002143 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002144 } else
2145 assert(0 && "Unexpected scope info");
2146
Devang Patel23670e52011-03-24 20:30:50 +00002147 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002148 }
2149
Rafael Espindola5c055632010-11-18 02:04:25 +00002150 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, DWARF2_FLAG_IS_STMT,
2151 0, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002152}
2153
Bill Wendling829e67b2009-05-20 23:22:40 +00002154//===----------------------------------------------------------------------===//
2155// Emit Methods
2156//===----------------------------------------------------------------------===//
2157
Devang Patel2c4ceb12009-11-21 02:48:08 +00002158/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002159///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002160unsigned
2161DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002162 // Get the children.
2163 const std::vector<DIE *> &Children = Die->getChildren();
2164
2165 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002166 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00002167 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00002168
2169 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002170 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002171
2172 // Get the abbreviation for this DIE.
2173 unsigned AbbrevNumber = Die->getAbbrevNumber();
2174 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2175
2176 // Set DIE offset
2177 Die->setOffset(Offset);
2178
2179 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002180 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002181
2182 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2183 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2184
2185 // Size the DIE attribute values.
2186 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2187 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00002188 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00002189
2190 // Size the DIE children if any.
2191 if (!Children.empty()) {
2192 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2193 "Children flag not set");
2194
2195 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002196 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002197
2198 // End of children marker.
2199 Offset += sizeof(int8_t);
2200 }
2201
2202 Die->setSize(Offset - Die->getOffset());
2203 return Offset;
2204}
2205
Devang Patel2c4ceb12009-11-21 02:48:08 +00002206/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002207///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002208void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00002209 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2210 E = CUMap.end(); I != E; ++I) {
2211 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00002212 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00002213 sizeof(int32_t) + // Length of Compilation Unit Info
2214 sizeof(int16_t) + // DWARF version number
2215 sizeof(int32_t) + // Offset Into Abbrev. Section
2216 sizeof(int8_t); // Pointer Size (in bytes)
2217 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00002218 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002219}
2220
Chris Lattner11b8f302010-04-04 23:02:02 +00002221/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2222/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00002223static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00002224 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002225 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00002226 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002227
Chris Lattner9c69e285532010-04-04 22:59:04 +00002228 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2229 Asm->OutStreamer.EmitLabel(TmpSym);
2230 return TmpSym;
2231}
2232
2233/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2234/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00002235void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002236 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002237
Bill Wendling94d04b82009-05-20 23:21:38 +00002238 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002239 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002240 DwarfFrameSectionSym =
2241 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2242 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002243
Jim Grosbach1e20b962010-07-21 21:21:52 +00002244 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00002245 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002246 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00002247 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00002248 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002249
Chris Lattner9c69e285532010-04-04 22:59:04 +00002250 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00002251 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002252
Devang Patelaf608bd2010-08-24 00:06:12 +00002253 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00002254 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2255 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2256 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002257 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00002258 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00002259 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2260 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00002261
Devang Patelc3f5f782010-05-25 23:40:22 +00002262 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
2263 "section_debug_loc");
2264
Chris Lattner9c69e285532010-04-04 22:59:04 +00002265 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00002266 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002267}
2268
Devang Patel2c4ceb12009-11-21 02:48:08 +00002269/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002270///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002271void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002272 // Get the abbreviation for this DIE.
2273 unsigned AbbrevNumber = Die->getAbbrevNumber();
2274 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2275
Bill Wendling94d04b82009-05-20 23:21:38 +00002276 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00002277 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00002278 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2279 Twine::utohexstr(Die->getOffset()) + ":0x" +
2280 Twine::utohexstr(Die->getSize()) + " " +
2281 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002282 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002283
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002284 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00002285 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2286
2287 // Emit the DIE attribute values.
2288 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2289 unsigned Attr = AbbrevData[i].getAttribute();
2290 unsigned Form = AbbrevData[i].getForm();
2291 assert(Form && "Too many attributes for DIE (check abbreviation)");
2292
Chris Lattner3f53c832010-04-04 18:52:31 +00002293 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00002294 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002295
Bill Wendling94d04b82009-05-20 23:21:38 +00002296 switch (Attr) {
2297 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002298 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002299 break;
2300 case dwarf::DW_AT_abstract_origin: {
2301 DIEEntry *E = cast<DIEEntry>(Values[i]);
2302 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002303 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002304 Asm->EmitInt32(Addr);
2305 break;
2306 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002307 case dwarf::DW_AT_ranges: {
2308 // DW_AT_range Value encodes offset in debug_range section.
2309 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00002310
2311 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
2312 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
2313 V->getValue(),
2314 4);
2315 } else {
2316 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2317 V->getValue(),
2318 DwarfDebugRangeSectionSym,
2319 4);
2320 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002321 break;
2322 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002323 case dwarf::DW_AT_location: {
2324 if (UseDotDebugLocEntry.count(Die) != 0) {
2325 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbar83320a02011-03-16 22:16:39 +00002326 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patelc3f5f782010-05-25 23:40:22 +00002327 } else
2328 Values[i]->EmitValue(Asm, Form);
2329 break;
2330 }
Devang Patel2a361602010-09-29 19:08:08 +00002331 case dwarf::DW_AT_accessibility: {
2332 if (Asm->isVerbose()) {
2333 DIEInteger *V = cast<DIEInteger>(Values[i]);
2334 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2335 }
2336 Values[i]->EmitValue(Asm, Form);
2337 break;
2338 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002339 default:
2340 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002341 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00002342 break;
2343 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002344 }
2345
2346 // Emit the DIE children if any.
2347 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2348 const std::vector<DIE *> &Children = Die->getChildren();
2349
2350 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002351 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002352
Chris Lattner3f53c832010-04-04 18:52:31 +00002353 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00002354 Asm->OutStreamer.AddComment("End Of Children Mark");
2355 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002356 }
2357}
2358
Devang Patel8a241142009-12-09 18:24:21 +00002359/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002360///
Devang Patel8a241142009-12-09 18:24:21 +00002361void DwarfDebug::emitDebugInfo() {
2362 // Start debug info section.
2363 Asm->OutStreamer.SwitchSection(
2364 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00002365 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2366 E = CUMap.end(); I != E; ++I) {
2367 CompileUnit *TheCU = I->second;
2368 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002369
Devang Patel163a9f72010-05-10 22:49:55 +00002370 // Emit the compile units header.
2371 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2372 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002373
Devang Patel163a9f72010-05-10 22:49:55 +00002374 // Emit size of content not including length itself
2375 unsigned ContentSize = Die->getSize() +
2376 sizeof(int16_t) + // DWARF version number
2377 sizeof(int32_t) + // Offset Into Abbrev. Section
2378 sizeof(int8_t) + // Pointer Size (in bytes)
2379 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002380
Devang Patel163a9f72010-05-10 22:49:55 +00002381 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2382 Asm->EmitInt32(ContentSize);
2383 Asm->OutStreamer.AddComment("DWARF version number");
2384 Asm->EmitInt16(dwarf::DWARF_VERSION);
2385 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2386 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2387 DwarfAbbrevSectionSym);
2388 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2389 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002390
Devang Patel163a9f72010-05-10 22:49:55 +00002391 emitDIE(Die);
2392 // FIXME - extra padding for gdb bug.
2393 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2394 Asm->EmitInt8(0);
2395 Asm->EmitInt8(0);
2396 Asm->EmitInt8(0);
2397 Asm->EmitInt8(0);
2398 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2399 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002400}
2401
Devang Patel2c4ceb12009-11-21 02:48:08 +00002402/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002403///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002404void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002405 // Check to see if it is worth the effort.
2406 if (!Abbreviations.empty()) {
2407 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002408 Asm->OutStreamer.SwitchSection(
2409 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002410
Chris Lattnerc0215722010-04-04 19:25:43 +00002411 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002412
2413 // For each abbrevation.
2414 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2415 // Get abbreviation data
2416 const DIEAbbrev *Abbrev = Abbreviations[i];
2417
2418 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002419 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002420
2421 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002422 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00002423 }
2424
2425 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002426 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002427
Chris Lattnerc0215722010-04-04 19:25:43 +00002428 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002429 }
2430}
2431
Devang Patel2c4ceb12009-11-21 02:48:08 +00002432/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002433/// the line matrix.
2434///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002435void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002436 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00002437 Asm->OutStreamer.AddComment("Extended Op");
2438 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002439
Chris Lattner233f52b2010-03-09 23:52:58 +00002440 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002441 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002442 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2443 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2444
2445 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002446
Chris Lattnerc0215722010-04-04 19:25:43 +00002447 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002448 Asm->getTargetData().getPointerSize(),
2449 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002450
2451 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00002452 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2453 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002454 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002455 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002456}
2457
Devang Patel2c4ceb12009-11-21 02:48:08 +00002458/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002459///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002460void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002461 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002462 return;
2463
Chris Lattnerd38fee82010-04-05 00:13:49 +00002464 int stackGrowth = Asm->getTargetData().getPointerSize();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002465 if (Asm->TM.getFrameLowering()->getStackGrowthDirection() ==
2466 TargetFrameLowering::StackGrowsDown)
Chris Lattnerd38fee82010-04-05 00:13:49 +00002467 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00002468
2469 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002470 Asm->OutStreamer.SwitchSection(
2471 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002472
Chris Lattnerc0215722010-04-04 19:25:43 +00002473 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00002474 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002475 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
2476 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002477
Chris Lattnerc0215722010-04-04 19:25:43 +00002478 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00002479 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00002480 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00002481 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00002482 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00002483 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002484 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002485 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
2486 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00002487 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002488 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002489 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Bill Wendling94d04b82009-05-20 23:21:38 +00002490 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00002491
2492 std::vector<MachineMove> Moves;
Anton Korobeynikovd9e33852010-11-18 23:25:52 +00002493 TFI->getInitialFrameState(Moves);
Bill Wendling94d04b82009-05-20 23:21:38 +00002494
Chris Lattner02b86b92010-04-04 23:41:46 +00002495 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00002496
Chris Lattner059ea132010-04-28 01:05:45 +00002497 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00002498 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002499}
2500
Devang Patel2c4ceb12009-11-21 02:48:08 +00002501/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00002502/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00002503void DwarfDebug::
2504emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002505 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002506 return;
2507
2508 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002509 Asm->OutStreamer.SwitchSection(
2510 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002511
Chris Lattner233f52b2010-03-09 23:52:58 +00002512 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00002513 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00002514 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00002515 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00002516 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00002517 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002518
Chris Lattner206d61e2010-03-13 07:26:18 +00002519 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00002520
Chris Lattner233f52b2010-03-09 23:52:58 +00002521 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002522 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner6189ed12010-04-04 23:25:33 +00002523 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00002524
Chris Lattner233f52b2010-03-09 23:52:58 +00002525 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00002526 MCSymbol *FuncBeginSym =
2527 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00002528 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00002529 Asm->getTargetData().getPointerSize(),
2530 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002531
2532
Chris Lattner233f52b2010-03-09 23:52:58 +00002533 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00002534 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002535 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002536
Chris Lattner02b86b92010-04-04 23:41:46 +00002537 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00002538
Chris Lattner059ea132010-04-28 01:05:45 +00002539 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00002540 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00002541}
2542
Devang Patel8a241142009-12-09 18:24:21 +00002543/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2544///
2545void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00002546 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2547 E = CUMap.end(); I != E; ++I) {
2548 CompileUnit *TheCU = I->second;
2549 // Start the dwarf pubnames section.
2550 Asm->OutStreamer.SwitchSection(
2551 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002552
Devang Patel163a9f72010-05-10 22:49:55 +00002553 Asm->OutStreamer.AddComment("Length of Public Names Info");
2554 Asm->EmitLabelDifference(
2555 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
2556 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002557
Devang Patel163a9f72010-05-10 22:49:55 +00002558 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
2559 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002560
Devang Patel163a9f72010-05-10 22:49:55 +00002561 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002562 Asm->EmitInt16(dwarf::DWARF_VERSION);
2563
Devang Patel163a9f72010-05-10 22:49:55 +00002564 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002565 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002566 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002567
Devang Patel163a9f72010-05-10 22:49:55 +00002568 Asm->OutStreamer.AddComment("Compilation Unit Length");
2569 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2570 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2571 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002572
Devang Patel163a9f72010-05-10 22:49:55 +00002573 const StringMap<DIE*> &Globals = TheCU->getGlobals();
2574 for (StringMap<DIE*>::const_iterator
2575 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2576 const char *Name = GI->getKeyData();
2577 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002578
Devang Patel163a9f72010-05-10 22:49:55 +00002579 Asm->OutStreamer.AddComment("DIE offset");
2580 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002581
Devang Patel163a9f72010-05-10 22:49:55 +00002582 if (Asm->isVerbose())
2583 Asm->OutStreamer.AddComment("External Name");
2584 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
2585 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002586
Devang Patel163a9f72010-05-10 22:49:55 +00002587 Asm->OutStreamer.AddComment("End Mark");
2588 Asm->EmitInt32(0);
2589 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
2590 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002591 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002592}
2593
Devang Patel193f7202009-11-24 01:14:22 +00002594void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00002595 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2596 E = CUMap.end(); I != E; ++I) {
2597 CompileUnit *TheCU = I->second;
2598 // Start the dwarf pubnames section.
2599 Asm->OutStreamer.SwitchSection(
2600 Asm->getObjFileLowering().getDwarfPubTypesSection());
2601 Asm->OutStreamer.AddComment("Length of Public Types Info");
2602 Asm->EmitLabelDifference(
2603 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
2604 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002605
Devang Patel163a9f72010-05-10 22:49:55 +00002606 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
2607 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002608
Devang Patel163a9f72010-05-10 22:49:55 +00002609 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2610 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002611
Devang Patel163a9f72010-05-10 22:49:55 +00002612 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2613 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
2614 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002615
Devang Patel163a9f72010-05-10 22:49:55 +00002616 Asm->OutStreamer.AddComment("Compilation Unit Length");
2617 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2618 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2619 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002620
Devang Patel163a9f72010-05-10 22:49:55 +00002621 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2622 for (StringMap<DIE*>::const_iterator
2623 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2624 const char *Name = GI->getKeyData();
2625 DIE * Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002626
Devang Patel163a9f72010-05-10 22:49:55 +00002627 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2628 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002629
Devang Patel163a9f72010-05-10 22:49:55 +00002630 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
2631 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2632 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002633
Devang Patel163a9f72010-05-10 22:49:55 +00002634 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002635 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00002636 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
2637 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00002638 }
Devang Patel193f7202009-11-24 01:14:22 +00002639}
2640
Devang Patel2c4ceb12009-11-21 02:48:08 +00002641/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002642///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002643void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002644 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002645 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002646
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002647 // Start the dwarf str section.
2648 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002649 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002650
Chris Lattnerbc733f52010-03-13 02:17:42 +00002651 // Get all of the string pool entries and put them in an array by their ID so
2652 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002653 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002654 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002655
Chris Lattnerbc733f52010-03-13 02:17:42 +00002656 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2657 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2658 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002659
Chris Lattnerbc733f52010-03-13 02:17:42 +00002660 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002661
Chris Lattnerbc733f52010-03-13 02:17:42 +00002662 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002663 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002664 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002665
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002666 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002667 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002668 }
2669}
2670
Devang Patel2c4ceb12009-11-21 02:48:08 +00002671/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002672///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002673void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002674 if (DotDebugLocEntries.empty())
2675 return;
2676
Devang Patel6c3ea902011-02-04 22:57:18 +00002677 for (SmallVector<DotDebugLocEntry, 4>::iterator
2678 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2679 I != E; ++I) {
2680 DotDebugLocEntry &Entry = *I;
2681 if (I + 1 != DotDebugLocEntries.end())
2682 Entry.Merge(I+1);
2683 }
2684
Daniel Dunbar83320a02011-03-16 22:16:39 +00002685 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002686 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002687 Asm->getObjFileLowering().getDwarfLocSection());
2688 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002689 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2690 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002691 for (SmallVector<DotDebugLocEntry, 4>::iterator
2692 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002693 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002694 DotDebugLocEntry &Entry = *I;
2695 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002696 if (Entry.isEmpty()) {
2697 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2698 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002699 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002700 } else {
2701 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2702 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
2703 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
2704 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patelb2cf5812010-08-04 18:40:52 +00002705 if (int Offset = Entry.Loc.getOffset()) {
2706 // If the value is at a certain offset from frame register then
2707 // use DW_OP_fbreg.
2708 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patelc3f5f782010-05-25 23:40:22 +00002709 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patelb2cf5812010-08-04 18:40:52 +00002710 Asm->EmitInt16(1 + OffsetSize);
2711 Asm->OutStreamer.AddComment(
2712 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
2713 Asm->EmitInt8(dwarf::DW_OP_fbreg);
2714 Asm->OutStreamer.AddComment("Offset");
2715 Asm->EmitSLEB128(Offset);
Devang Patelc3f5f782010-05-25 23:40:22 +00002716 } else {
Devang Patelb2cf5812010-08-04 18:40:52 +00002717 if (Reg < 32) {
2718 Asm->OutStreamer.AddComment("Loc expr size");
2719 Asm->EmitInt16(1);
2720 Asm->OutStreamer.AddComment(
Devang Patela54e0cc2010-08-04 20:32:36 +00002721 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patelb2cf5812010-08-04 18:40:52 +00002722 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
2723 } else {
2724 Asm->OutStreamer.AddComment("Loc expr size");
2725 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
2726 Asm->EmitInt8(dwarf::DW_OP_regx);
2727 Asm->EmitULEB128(Reg);
2728 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002729 }
2730 }
2731 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002732}
2733
2734/// EmitDebugARanges - Emit visible names into a debug aranges section.
2735///
2736void DwarfDebug::EmitDebugARanges() {
2737 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002738 Asm->OutStreamer.SwitchSection(
2739 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002740}
2741
Devang Patel2c4ceb12009-11-21 02:48:08 +00002742/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002743///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002744void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002745 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002746 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002747 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002748 unsigned char Size = Asm->getTargetData().getPointerSize();
2749 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002750 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002751 I != E; ++I) {
2752 if (*I)
2753 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002754 else
Devang Pateleac9c072010-04-27 19:46:33 +00002755 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002756 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002757}
2758
Devang Patel2c4ceb12009-11-21 02:48:08 +00002759/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002760///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002761void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002762 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002763 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002764 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002765 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002766 }
2767}
2768
Devang Patel2c4ceb12009-11-21 02:48:08 +00002769/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002770/// Section Header:
2771/// 1. length of section
2772/// 2. Dwarf version number
2773/// 3. address size.
2774///
2775/// Entries (one "entry" for each function that was inlined):
2776///
2777/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2778/// otherwise offset into __debug_str for regular function name.
2779/// 2. offset into __debug_str section for regular function name.
2780/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2781/// instances for the function.
2782///
2783/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2784/// inlined instance; the die_offset points to the inlined_subroutine die in the
2785/// __debug_info section, and the low_pc is the starting address for the
2786/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002787void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002788 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002789 return;
2790
Devang Patel163a9f72010-05-10 22:49:55 +00002791 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002792 return;
2793
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002794 Asm->OutStreamer.SwitchSection(
2795 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002796
Chris Lattner233f52b2010-03-09 23:52:58 +00002797 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002798 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2799 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002800
Chris Lattnerc0215722010-04-04 19:25:43 +00002801 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002802
Chris Lattner233f52b2010-03-09 23:52:58 +00002803 Asm->OutStreamer.AddComment("Dwarf Version");
2804 Asm->EmitInt16(dwarf::DWARF_VERSION);
2805 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002806 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002807
Devang Patele9f8f5e2010-05-07 20:54:48 +00002808 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002809 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002810
Devang Patele9f8f5e2010-05-07 20:54:48 +00002811 const MDNode *Node = *I;
2812 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002813 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002814 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002815 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002816 StringRef LName = SP.getLinkageName();
2817 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002818
Chris Lattner233f52b2010-03-09 23:52:58 +00002819 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002820 if (LName.empty()) {
2821 Asm->OutStreamer.EmitBytes(Name, 0);
2822 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002823 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00002824 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2825 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002826
Chris Lattner233f52b2010-03-09 23:52:58 +00002827 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002828 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002829 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002830
Devang Patel53bb5c92009-11-10 23:06:00 +00002831 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002832 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002833 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002834 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002835
Chris Lattner3f53c832010-04-04 18:52:31 +00002836 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002837 Asm->OutStreamer.EmitSymbolValue(LI->first,
2838 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002839 }
2840 }
2841
Chris Lattnerc0215722010-04-04 19:25:43 +00002842 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002843}