blob: 69675ee93f1ef5813ebea57d4db46216a670c522 [file] [log] [blame]
Bill Wendling2f921f82009-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 Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Devang Patel5eb43192011-04-12 17:40:32 +000017#include "DwarfCompileUnit.h"
Bill Wendling30346342010-04-05 22:59:21 +000018#include "llvm/Constants.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000019#include "llvm/Module.h"
Devang Patel2d9e5322011-01-20 00:02:16 +000020#include "llvm/Instructions.h"
David Greene829b3e82009-08-19 21:52:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000023#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000024#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000025#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000026#include "llvm/MC/MCSymbol.h"
Chris Lattnerf62e3ee2010-01-16 21:57:06 +000027#include "llvm/Target/Mangler.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000028#include "llvm/Target/TargetData.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000029#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000031#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000034#include "llvm/Analysis/DebugInfo.h"
Devang Patela5d93242011-02-24 18:49:30 +000035#include "llvm/Analysis/DIBuilder.h"
Devang Patela86114b2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Devang Patel6c74a872010-04-27 19:46:33 +000039#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000042#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000043#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000044#include "llvm/Support/Timer.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000045#include "llvm/Support/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000046using namespace llvm;
47
Devang Patel6c74a872010-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 Grosbacha8683bb2010-07-21 21:21:52 +000051static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000052 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000053 cl::desc("Disable debug info printing"));
54
Dan Gohman7421ae42010-05-07 01:08:53 +000055static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000056 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman7421ae42010-05-07 01:08:53 +000057 cl::init(false));
58
Nick Lewycky90b2ac22010-10-26 00:51:57 +000059#ifndef NDEBUG
Devang Patela86114b2010-10-25 20:45:32 +000060STATISTIC(BlocksWithoutLineNo, "Number of blocks without any line number");
Nick Lewycky90b2ac22010-10-26 00:51:57 +000061#endif
Devang Patela86114b2010-10-25 20:45:32 +000062
Bill Wendlingfcc14142010-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 Wendling2f921f82009-05-15 09:23:25 +000068//===----------------------------------------------------------------------===//
69
70/// Configuration values for initial hash set sizes (log2).
71///
Bill Wendling2f921f82009-05-15 09:23:25 +000072static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000073
74namespace llvm {
75
Devang Patelf20c4f72011-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 Patel6d9f9fe2010-08-09 21:01:39 +0000121 }
122 return Ty;
123 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000124 return Ty;
125}
Bill Wendling2f921f82009-05-15 09:23:25 +0000126
127//===----------------------------------------------------------------------===//
Devang Patel6c74a872010-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 Wendling2f921f82009-05-15 09:23:25 +0000134/// DbgScope - This class is used to track scope information.
135///
Devang Patelf3d7c082009-11-16 21:53:40 +0000136class DbgScope {
Bill Wendling2f921f82009-05-15 09:23:25 +0000137 DbgScope *Parent; // Parent to this scope.
Jim Grosbach042483e2009-11-21 23:12:12 +0000138 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel1973df22010-01-26 21:39:14 +0000139 // Location at which this scope is inlined.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000140 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000141 bool AbstractScope; // Abstract Scope
Devang Patel787f94c2009-10-01 18:25:23 +0000142 const MachineInstr *LastInsn; // Last instruction of this scope.
143 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patel6c74a872010-04-27 19:46:33 +0000144 unsigned DFSIn, DFSOut;
Jeffrey Yasskin35b4e4f2010-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 Patel6c74a872010-04-27 19:46:33 +0000149 SmallVector<DbgRange, 4> Ranges;
Owen Anderson9becc182009-06-24 22:53:20 +0000150 // Private state for dump()
151 mutable unsigned IndentLevel;
Bill Wendling2f921f82009-05-15 09:23:25 +0000152public:
Devang Patel32cc43c2010-05-07 20:54:48 +0000153 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000154 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patel6c74a872010-04-27 19:46:33 +0000155 LastInsn(0), FirstInsn(0),
156 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000157 virtual ~DbgScope();
158
159 // Accessors.
160 DbgScope *getParent() const { return Parent; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000161 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000162 DIDescriptor getDesc() const { return Desc; }
Devang Patel32cc43c2010-05-07 20:54:48 +0000163 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
164 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000165 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
Devang Patel406798a2010-08-09 18:51:29 +0000166 const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
Devang Patel6c74a872010-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 Grosbacha8683bb2010-07-21 21:21:52 +0000171 if (!FirstInsn)
Devang Patel6c74a872010-04-27 19:46:33 +0000172 FirstInsn = MI;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000173
Devang Patel6c74a872010-04-27 19:46:33 +0000174 if (Parent)
175 Parent->openInsnRange(MI);
176 }
177
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000178 /// extendInsnRange - Extend the current instruction range covered by
Devang Patel6c74a872010-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 Grosbacha8683bb2010-07-21 21:21:52 +0000193 FirstInsn = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +0000194 LastInsn = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000195 // If Parent dominates NewScope then do not close Parent's instruction
Devang Patel6c74a872010-04-27 19:46:33 +0000196 // range.
197 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
198 Parent->closeInsnRange(NewScope);
199 }
200
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000201 void setAbstractScope() { AbstractScope = true; }
202 bool isAbstractScope() const { return AbstractScope; }
Devang Patel6c74a872010-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 Grosbacha8683bb2010-07-21 21:21:52 +0000210 if (S == this)
Devang Patel6c74a872010-04-27 19:46:33 +0000211 return true;
212 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
213 return true;
214 return false;
215 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000216
Devang Patel930143b2009-11-21 02:48:08 +0000217 /// addScope - Add a scope to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000218 ///
Devang Patel930143b2009-11-21 02:48:08 +0000219 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000220
Devang Patel930143b2009-11-21 02:48:08 +0000221 /// addVariable - Add a variable to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000222 ///
Devang Patel930143b2009-11-21 02:48:08 +0000223 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000224
Bill Wendling2f921f82009-05-15 09:23:25 +0000225#ifndef NDEBUG
226 void dump() const;
227#endif
228};
Devang Patel6c74a872010-04-27 19:46:33 +0000229
Chris Lattnerf5d06362010-04-05 04:09:20 +0000230} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000231
232#ifndef NDEBUG
233void DbgScope::dump() const {
David Greenec230cb92009-12-24 00:31:35 +0000234 raw_ostream &err = dbgs();
Chris Lattner81e8e022009-08-23 00:51:00 +0000235 err.indent(IndentLevel);
Devang Patel32cc43c2010-05-07 20:54:48 +0000236 const MDNode *N = Desc;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000237 N->dump();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000238 if (AbstractScope)
239 err << "Abstract Scope\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000240
241 IndentLevel += 2;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000242 if (!Scopes.empty())
243 err << "Children ...\n";
Bill Wendling2f921f82009-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 Wendling2f921f82009-05-15 09:23:25 +0000252DbgScope::~DbgScope() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000253 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
254 delete Variables[j];
Bill Wendling2f921f82009-05-15 09:23:25 +0000255}
256
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000257DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000258 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000259 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel12563b32010-04-16 23:33:45 +0000260 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000261 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000262
Chris Lattnere58b5472010-04-04 23:10:38 +0000263 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
264 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000265 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000266 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman6e681a52010-06-18 15:56:31 +0000267 {
268 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
269 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000270 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000271}
272DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000273}
274
Chris Lattnerb7aa9522010-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 Lattnera179b522010-04-04 19:25:43 +0000280 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000281}
282
283
Devang Patel930143b2009-11-21 02:48:08 +0000284/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000285///
Devang Patel930143b2009-11-21 02:48:08 +0000286void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-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 Grosbacha8683bb2010-07-21 21:21:52 +0000307/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-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 Patel525dda02009-12-14 16:18:45 +0000317/// createSubprogramDIE - Create new DIE using SP.
Devang Patel185051c2010-09-27 23:15:27 +0000318DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000319 CompileUnit *SPCU = getCompileUnit(SP);
320 DIE *SPDie = SPCU->getDIE(SP);
Devang Patel525dda02009-12-14 16:18:45 +0000321 if (SPDie)
322 return SPDie;
323
324 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1d6bbd42011-04-22 23:10:17 +0000325
326 // DW_TAG_inlined_subroutine may refer to this DIE.
327 SPCU->insertDIE(SP, SPDie);
328
329 // Add to context owner.
330 SPCU->addToContextOwner(SPDie, SP.getContext());
331
332 // Add function template parameters.
333 SPCU->addTemplateParams(*SPDie, SP.getTemplateParams());
334
335 // If this DIE is going to refer declaration info using AT_specification
336 // then there is no need to add other attributes.
337 if (SP.getFunctionDeclaration().isSubprogram())
338 return SPDie;
339
Devang Patelf200b392010-03-02 17:58:15 +0000340 // Constructors and operators for anonymous aggregates do not have names.
Devang Pateld0fa3042010-03-02 01:26:20 +0000341 if (!SP.getName().empty())
Devang Patelf20c4f72011-04-12 22:53:02 +0000342 SPCU->addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
343 SP.getName());
Bill Wendling2f921f82009-05-15 09:23:25 +0000344
Devang Patel2d9caf92009-11-25 17:36:49 +0000345 StringRef LinkageName = SP.getLinkageName();
Devang Patel43ef34d2010-01-05 01:46:14 +0000346 if (!LinkageName.empty())
Devang Patelf20c4f72011-04-12 22:53:02 +0000347 SPCU->addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
348 getRealLinkageName(LinkageName));
Devang Patel43ef34d2010-01-05 01:46:14 +0000349
Devang Patelf20c4f72011-04-12 22:53:02 +0000350 SPCU->addSourceLine(SPDie, SP);
Bill Wendling2f921f82009-05-15 09:23:25 +0000351
Devang Patel3a24f922010-10-07 22:03:01 +0000352 if (SP.isPrototyped())
Devang Patelf20c4f72011-04-12 22:53:02 +0000353 SPCU->addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Devang Patel1d6bbd42011-04-22 23:10:17 +0000354
Bill Wendling2f921f82009-05-15 09:23:25 +0000355 // Add Return Type.
Devang Patel236526d2009-12-03 01:25:38 +0000356 DICompositeType SPTy = SP.getType();
357 DIArray Args = SPTy.getTypeArray();
Bill Wendling2f921f82009-05-15 09:23:25 +0000358 unsigned SPTag = SPTy.getTag();
Devang Pateleb57c592009-12-03 19:11:07 +0000359
Devang Patel3b548aa2010-03-08 20:52:55 +0000360 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelf20c4f72011-04-12 22:53:02 +0000361 SPCU->addType(SPDie, SPTy);
Devang Patel236526d2009-12-03 01:25:38 +0000362 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000363 SPCU->addType(SPDie, DIType(Args.getElement(0)));
Devang Patel236526d2009-12-03 01:25:38 +0000364
Devang Pateleb57c592009-12-03 19:11:07 +0000365 unsigned VK = SP.getVirtuality();
366 if (VK) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000367 SPCU->addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
368 DIEBlock *Block = SPCU->getDIEBlock();
369 SPCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
370 SPCU->addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
371 SPCU->addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000372 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000373 SP.getContainingType()));
Devang Pateleb57c592009-12-03 19:11:07 +0000374 }
375
Devang Patel185051c2010-09-27 23:15:27 +0000376 if (!SP.isDefinition()) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000377 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
378
Bill Wendling2f921f82009-05-15 09:23:25 +0000379 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel236526d2009-12-03 01:25:38 +0000380 // be handled while processing variables.
381 DICompositeType SPTy = SP.getType();
382 DIArray Args = SPTy.getTypeArray();
383 unsigned SPTag = SPTy.getTag();
384
Bill Wendling2f921f82009-05-15 09:23:25 +0000385 if (SPTag == dwarf::DW_TAG_subroutine_type)
386 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
387 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000388 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelf20c4f72011-04-12 22:53:02 +0000389 SPCU->addType(Arg, ATy);
Devang Patel6efc8e52010-02-06 01:02:37 +0000390 if (ATy.isArtificial())
Devang Patelf20c4f72011-04-12 22:53:02 +0000391 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel930143b2009-11-21 02:48:08 +0000392 SPDie->addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000393 }
394 }
395
Devang Patel999b4992010-02-03 19:57:19 +0000396 if (SP.isArtificial())
Devang Patelf20c4f72011-04-12 22:53:02 +0000397 SPCU->addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel999b4992010-02-03 19:57:19 +0000398
Devang Patelb4e3b902010-04-30 19:38:23 +0000399 if (!SP.isLocalToUnit())
Devang Patelf20c4f72011-04-12 22:53:02 +0000400 SPCU->addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000401
Devang Patelb4e3b902010-04-30 19:38:23 +0000402 if (SP.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000403 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Devang Patelb4e3b902010-04-30 19:38:23 +0000404
Jim Grosbach965a73a2010-07-21 23:03:52 +0000405 if (unsigned isa = Asm->getISAEncoding()) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000406 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
Jim Grosbach965a73a2010-07-21 23:03:52 +0000407 }
408
Bill Wendling2f921f82009-05-15 09:23:25 +0000409 return SPDie;
410}
411
Devang Patel32cc43c2010-05-07 20:54:48 +0000412DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattner8d2fe282010-03-31 05:36:29 +0000413 assert(N && "Invalid Scope encoding!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000414
415 DbgScope *AScope = AbstractScopes.lookup(N);
416 if (AScope)
417 return AScope;
Jim Grosbach042483e2009-11-21 23:12:12 +0000418
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000419 DbgScope *Parent = NULL;
420
421 DIDescriptor Scope(N);
422 if (Scope.isLexicalBlock()) {
423 DILexicalBlock DB(N);
424 DIDescriptor ParentDesc = DB.getContext();
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000425 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000426 }
427
428 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
429
430 if (Parent)
Devang Patel930143b2009-11-21 02:48:08 +0000431 Parent->addScope(AScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000432 AScope->setAbstractScope();
433 AbstractScopes[N] = AScope;
434 if (DIDescriptor(N).isSubprogram())
435 AbstractScopesList.push_back(AScope);
436 return AScope;
437}
Devang Patel75cc16c2009-10-01 20:31:14 +0000438
Devang Patel019922d2010-04-06 23:53:48 +0000439/// isSubprogramContext - Return true if Context is either a subprogram
440/// or another context nested inside a subprogram.
Devang Patel32cc43c2010-05-07 20:54:48 +0000441static bool isSubprogramContext(const MDNode *Context) {
Devang Patel019922d2010-04-06 23:53:48 +0000442 if (!Context)
443 return false;
444 DIDescriptor D(Context);
445 if (D.isSubprogram())
446 return true;
447 if (D.isType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000448 return isSubprogramContext(DIType(Context).getContext());
Devang Patel019922d2010-04-06 23:53:48 +0000449 return false;
450}
451
Jim Grosbach042483e2009-11-21 23:12:12 +0000452/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +0000453/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
454/// If there are global variables in this scope then create and insert
455/// DIEs for these variables.
Devang Patel32cc43c2010-05-07 20:54:48 +0000456DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000457 CompileUnit *SPCU = getCompileUnit(SPNode);
458 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000459
Chris Lattner3a383cb2010-04-05 00:13:49 +0000460 assert(SPDie && "Unable to find subprogram DIE!");
461 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000462
Devang Patel1d6bbd42011-04-22 23:10:17 +0000463 DISubprogram SPDecl = SP.getFunctionDeclaration();
464 if (SPDecl.isSubprogram())
465 // Refer function declaration directly.
Devang Patelf20c4f72011-04-12 22:53:02 +0000466 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Patel1d6bbd42011-04-22 23:10:17 +0000467 createSubprogramDIE(SPDecl));
468 else {
469 // There is not any need to generate specification DIE for a function
470 // defined at compile unit level. If a function is defined inside another
471 // function then gdb prefers the definition at top level and but does not
472 // expect specification DIE in parent function. So avoid creating
473 // specification DIE for a function defined inside a function.
474 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
475 !SP.getContext().isFile() &&
476 !isSubprogramContext(SP.getContext())) {
477 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
478
479 // Add arguments.
480 DICompositeType SPTy = SP.getType();
481 DIArray Args = SPTy.getTypeArray();
482 unsigned SPTag = SPTy.getTag();
483 if (SPTag == dwarf::DW_TAG_subroutine_type)
484 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
485 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
486 DIType ATy = DIType(DIType(Args.getElement(i)));
487 SPCU->addType(Arg, ATy);
488 if (ATy.isArtificial())
489 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
490 SPDie->addChild(Arg);
491 }
492 DIE *SPDeclDie = SPDie;
493 SPDie = new DIE(dwarf::DW_TAG_subprogram);
494 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
495 SPDeclDie);
496 SPCU->addDie(SPDie);
497 }
Chris Lattner3a383cb2010-04-05 00:13:49 +0000498 }
Devang Patela37a95e2010-07-07 22:20:57 +0000499 // Pick up abstract subprogram DIE.
500 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
501 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000502 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
503 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000504 SPCU->addDie(SPDie);
505 }
506
Devang Patelf20c4f72011-04-12 22:53:02 +0000507 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
508 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
509 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
510 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000511 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
512 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000513 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000514
Chris Lattner3a383cb2010-04-05 00:13:49 +0000515 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000516}
517
Jim Grosbach042483e2009-11-21 23:12:12 +0000518/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +0000519/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
520DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000521
522 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
523 if (Scope->isAbstractScope())
524 return ScopeDIE;
525
526 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
527 if (Ranges.empty())
528 return 0;
529
Devang Patelf20c4f72011-04-12 22:53:02 +0000530 CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
Devang Patel6c74a872010-04-27 19:46:33 +0000531 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
532 if (Ranges.size() > 1) {
533 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000534 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000535 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000536 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
537 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
Devang Patel6c74a872010-04-27 19:46:33 +0000538 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
539 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000540 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
541 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000542 }
543 DebugRangeSymbols.push_back(NULL);
544 DebugRangeSymbols.push_back(NULL);
545 return ScopeDIE;
546 }
547
Devang Patel9fc11702010-05-25 23:40:22 +0000548 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
549 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000550
Devang Patel9fc11702010-05-25 23:40:22 +0000551 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000552
Chris Lattnere13c3722010-03-09 01:58:53 +0000553 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
554 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000555
Devang Patelf20c4f72011-04-12 22:53:02 +0000556 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
557 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000558
559 return ScopeDIE;
560}
561
Devang Patel930143b2009-11-21 02:48:08 +0000562/// constructInlinedScopeDIE - This scope represents inlined body of
563/// a function. Construct DIE to represent this concrete inlined copy
564/// of the function.
565DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000566
567 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000568 assert (Ranges.empty() == false
Devang Patel6c74a872010-04-27 19:46:33 +0000569 && "DbgScope does not have instruction markers!");
570
571 // FIXME : .debug_inlined section specification does not clearly state how
572 // to emit inlined scope that is split into multiple instruction ranges.
573 // For now, use first instruction range and emit low_pc/high_pc pair and
574 // corresponding .debug_inlined section entry for this pair.
575 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000576 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
577 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000578
Devang Patel4c6bd662010-07-08 22:39:20 +0000579 if (StartLabel == 0 || EndLabel == 0) {
Devang Patel6c74a872010-04-27 19:46:33 +0000580 assert (0 && "Unexpected Start and End labels for a inlined scope!");
581 return 0;
582 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000583 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000584 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000585 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000586 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000587
Devang Patel3b548aa2010-03-08 20:52:55 +0000588 if (!Scope->getScopeNode())
Devang Patelbc97f6b2010-03-08 19:20:38 +0000589 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +0000590 DIScope DS(Scope->getScopeNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000591 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
592
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000593 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000594 CompileUnit *TheCU = getCompileUnit(InlinedSP);
595 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattner8d2fe282010-03-31 05:36:29 +0000596 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patelf20c4f72011-04-12 22:53:02 +0000597 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
598 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000599
Devang Patelf20c4f72011-04-12 22:53:02 +0000600 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
601 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000602
603 InlinedSubprogramDIEs.insert(OriginDIE);
604
605 // Track the start label for this inlined function.
Devang Patel32cc43c2010-05-07 20:54:48 +0000606 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000607 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000608
609 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000610 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +0000611 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000612 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000613 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000614 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000615
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000616 DILocation DL(Scope->getInlinedAt());
Devang Patelf20c4f72011-04-12 22:53:02 +0000617 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
618 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000619
620 return ScopeDIE;
621}
622
Devang Patel930143b2009-11-21 02:48:08 +0000623
624/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel9ccfb642009-12-09 18:24:21 +0000625DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000626 StringRef Name = DV->getName();
Devang Patel2d9caf92009-11-25 17:36:49 +0000627 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +0000628 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000629
630 // Translate tag to proper Dwarf tag. The result variable is dropped for
631 // now.
632 unsigned Tag;
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000633 switch (DV->getTag()) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000634 case dwarf::DW_TAG_return_variable:
635 return NULL;
636 case dwarf::DW_TAG_arg_variable:
637 Tag = dwarf::DW_TAG_formal_parameter;
638 break;
639 case dwarf::DW_TAG_auto_variable: // fall thru
640 default:
641 Tag = dwarf::DW_TAG_variable;
642 break;
643 }
644
645 // Define variable debug information entry.
646 DIE *VariableDie = new DIE(Tag);
Devang Patel2606f4d2011-04-25 23:05:21 +0000647 CompileUnit *VariableCU = getCompileUnit(DV->getVariable());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000648 DIE *AbsDIE = NULL;
Devang Patele1c53f22010-05-20 16:36:41 +0000649 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
650 V2AVI = VarToAbstractVarMap.find(DV);
651 if (V2AVI != VarToAbstractVarMap.end())
652 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +0000653
Devang Patele1c53f22010-05-20 16:36:41 +0000654 if (AbsDIE)
Devang Patel2606f4d2011-04-25 23:05:21 +0000655 VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf20c4f72011-04-12 22:53:02 +0000656 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000657 else {
Devang Patel2606f4d2011-04-25 23:05:21 +0000658 VariableCU->addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
659 Name);
660 VariableCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000661
662 // Add variable type.
Devang Patel2606f4d2011-04-25 23:05:21 +0000663 VariableCU->addType(VariableDie, DV->getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000664 }
665
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000666 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel2606f4d2011-04-25 23:05:21 +0000667 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
668 dwarf::DW_FORM_flag, 1);
Devang Patelbea08d12010-09-29 23:07:21 +0000669 else if (DIVariable(DV->getVariable()).isArtificial())
Devang Patel2606f4d2011-04-25 23:05:21 +0000670 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
671 dwarf::DW_FORM_flag, 1);
Devang Patel9fc11702010-05-25 23:40:22 +0000672
673 if (Scope->isAbstractScope()) {
674 DV->setDIE(VariableDie);
675 return VariableDie;
676 }
677
678 // Add variable address.
679
680 unsigned Offset = DV->getDotDebugLocOffset();
681 if (Offset != ~0U) {
Devang Patel2606f4d2011-04-25 23:05:21 +0000682 VariableCU->addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
Devang Patel9fc11702010-05-25 23:40:22 +0000683 Asm->GetTempSymbol("debug_loc", Offset));
684 DV->setDIE(VariableDie);
685 UseDotDebugLocEntry.insert(VariableDie);
686 return VariableDie;
687 }
688
689 // Check if variable is described by a DBG_VALUE instruction.
690 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
691 DbgVariableToDbgInstMap.find(DV);
692 if (DVI != DbgVariableToDbgInstMap.end()) {
693 const MachineInstr *DVInsn = DVI->second;
Devang Patel9fc11702010-05-25 23:40:22 +0000694 bool updated = false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000695 // FIXME : Handle getNumOperands != 3
Devang Patel9fc11702010-05-25 23:40:22 +0000696 if (DVInsn->getNumOperands() == 3) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000697 if (DVInsn->getOperand(0).isReg()) {
698 const MachineOperand RegOp = DVInsn->getOperand(0);
699 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
700 if (DVInsn->getOperand(1).isImm() &&
701 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patel2606f4d2011-04-25 23:05:21 +0000702 VariableCU->addFrameVariableAddress(DV, VariableDie,
703 DVInsn->getOperand(1).getImm());
Devang Patel86ec8b32010-08-31 22:22:42 +0000704 updated = true;
705 } else
Devang Patel2606f4d2011-04-25 23:05:21 +0000706 updated = VariableCU->addRegisterAddress(VariableDie, RegOp);
Devang Patel86ec8b32010-08-31 22:22:42 +0000707 }
Devang Patel9fc11702010-05-25 23:40:22 +0000708 else if (DVInsn->getOperand(0).isImm())
Devang Patel2606f4d2011-04-25 23:05:21 +0000709 updated = VariableCU->addConstantValue(VariableDie,
710 DVInsn->getOperand(0));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000711 else if (DVInsn->getOperand(0).isFPImm())
712 updated =
Devang Patel2606f4d2011-04-25 23:05:21 +0000713 VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patel9fc11702010-05-25 23:40:22 +0000714 } else {
715 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
716 if (Location.getReg()) {
Devang Patel2688e4a2011-04-25 22:52:55 +0000717 if (DV->getVariable().hasComplexAddress())
Devang Patel2606f4d2011-04-25 23:05:21 +0000718 VariableCU->addComplexAddress(DV, VariableDie, dwarf::DW_AT_location,
719 Location);
Devang Patel2688e4a2011-04-25 22:52:55 +0000720 else
Devang Patel2606f4d2011-04-25 23:05:21 +0000721 VariableCU->addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel9fc11702010-05-25 23:40:22 +0000722 updated = true;
723 }
724 }
725 if (!updated) {
726 // If variableDie is not updated then DBG_VALUE instruction does not
727 // have valid variable info.
728 delete VariableDie;
729 return NULL;
730 }
731 DV->setDIE(VariableDie);
732 return VariableDie;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000733 }
Devang Patel9fc11702010-05-25 23:40:22 +0000734
735 // .. else use frame index, if available.
Devang Patel9fc11702010-05-25 23:40:22 +0000736 int FI = 0;
Devang Patel2cfc3af2010-08-31 06:11:28 +0000737 if (findVariableFrameIndex(DV, &FI))
Devang Patel2606f4d2011-04-25 23:05:21 +0000738 VariableCU->addFrameVariableAddress(DV, VariableDie, FI);
Devang Patel2cfc3af2010-08-31 06:11:28 +0000739
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000740 DV->setDIE(VariableDie);
741 return VariableDie;
742
743}
Devang Patel930143b2009-11-21 02:48:08 +0000744
Devang Patelf20c4f72011-04-12 22:53:02 +0000745void CompileUnit::addPubTypes(DISubprogram SP) {
Devang Patel04d2f2d2009-11-24 01:14:22 +0000746 DICompositeType SPTy = SP.getType();
747 unsigned SPTag = SPTy.getTag();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000748 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel04d2f2d2009-11-24 01:14:22 +0000749 return;
750
751 DIArray Args = SPTy.getTypeArray();
Devang Patel04d2f2d2009-11-24 01:14:22 +0000752 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000753 DIType ATy(Args.getElement(i));
Devang Patel8d6a2b72010-05-07 21:45:47 +0000754 if (!ATy.Verify())
Devang Patel04d2f2d2009-11-24 01:14:22 +0000755 continue;
756 DICompositeType CATy = getDICompositeType(ATy);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000757 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel12d150e2010-04-13 20:35:04 +0000758 && !CATy.isForwardDecl()) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000759 if (DIEEntry *Entry = getDIEEntry(CATy))
760 addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +0000761 }
762 }
763}
764
Devang Patel930143b2009-11-21 02:48:08 +0000765/// constructScopeDIE - Construct a DIE for this scope.
766DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000767 if (!Scope || !Scope->getScopeNode())
768 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000769
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000770 SmallVector <DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +0000771
772 // Collect arguments for current function.
773 if (Scope == CurrentFnDbgScope)
774 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
775 if (DbgVariable *ArgDV = CurrentFnArguments[i])
776 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
777 Children.push_back(Arg);
778
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000779 // Collect lexical scope childrens first.
780 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
781 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
782 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
783 Children.push_back(Variable);
784 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
785 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
786 if (DIE *Nested = constructScopeDIE(Scopes[j]))
787 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000788 DIScope DS(Scope->getScopeNode());
789 DIE *ScopeDIE = NULL;
790 if (Scope->getInlinedAt())
791 ScopeDIE = constructInlinedScopeDIE(Scope);
792 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000793 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000794 if (Scope->isAbstractScope()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000795 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000796 // Note down abstract DIE.
797 if (ScopeDIE)
798 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
799 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000800 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000801 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000802 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000803 else {
804 // There is no need to emit empty lexical block DIE.
805 if (Children.empty())
806 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +0000807 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000808 }
809
Devang Patel23b2ae62010-03-29 22:59:58 +0000810 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000811
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000812 // Add children
813 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
814 E = Children.end(); I != E; ++I)
815 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000816
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000817 if (DS.isSubprogram())
Devang Patelf20c4f72011-04-12 22:53:02 +0000818 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000819
Devang Patel04d2f2d2009-11-24 01:14:22 +0000820 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000821}
822
Bill Wendling2b128d72009-05-20 23:19:06 +0000823/// GetOrCreateSourceID - Look up the source id with the given directory and
824/// source file names. If none currently exists, create a new id and insert it
825/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
826/// maps as well.
Devang Patel498877d2010-07-24 00:53:22 +0000827
Devang Patele01b75c2011-03-24 20:30:50 +0000828unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
829 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000830 // If FE did not provide a file name, then assume stdin.
831 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000832 return GetOrCreateSourceID("<stdin>", StringRef());
833
834 // MCStream expects full path name as filename.
835 if (!DirName.empty() && !FileName.startswith("/")) {
836 std::string FullPathName(DirName.data());
837 if (!DirName.endswith("/"))
838 FullPathName += "/";
839 FullPathName += FileName.data();
840 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
841 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
842 }
Devang Patel871d0b12010-09-16 20:57:49 +0000843
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000844 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
845 if (Entry.getValue())
846 return Entry.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000847
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000848 unsigned SrcId = SourceIdMap.size();
849 Entry.setValue(SrcId);
Bill Wendling2b128d72009-05-20 23:19:06 +0000850
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000851 // Print out a .file directive to specify files for .loc directives.
Devang Patele01b75c2011-03-24 20:30:50 +0000852 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendling2b128d72009-05-20 23:19:06 +0000853
854 return SrcId;
855}
856
Devang Patel1f4690c2009-12-15 19:16:48 +0000857/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Devang Patelf20c4f72011-04-12 22:53:02 +0000858DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
859 DIE *NDie = getDIE(NS);
Devang Patel1f4690c2009-12-15 19:16:48 +0000860 if (NDie)
861 return NDie;
862 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patelf20c4f72011-04-12 22:53:02 +0000863 insertDIE(NS, NDie);
Devang Patel1f4690c2009-12-15 19:16:48 +0000864 if (!NS.getName().empty())
865 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patelb6511a32010-08-09 20:20:05 +0000866 addSourceLine(NDie, NS);
Devang Patel1f4690c2009-12-15 19:16:48 +0000867 addToContextOwner(NDie, NS.getContext());
868 return NDie;
869}
870
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000871/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +0000872/// metadata node with tag DW_TAG_compile_unit.
Devang Patel32cc43c2010-05-07 20:54:48 +0000873void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000874 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000875 StringRef FN = DIUnit.getFilename();
876 StringRef Dir = DIUnit.getDirectory();
Devang Patele01b75c2011-03-24 20:30:50 +0000877 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000878
879 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelf20c4f72011-04-12 22:53:02 +0000880 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
881 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
882 DIUnit.getProducer());
883 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
884 DIUnit.getLanguage());
885 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +0000886 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
887 // simplifies debug range entries.
Devang Patelf20c4f72011-04-12 22:53:02 +0000888 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000889 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000890 // compile unit in debug_line section.
Devang Patelea636392010-08-31 23:50:19 +0000891 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
Devang Patelf20c4f72011-04-12 22:53:02 +0000892 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
893 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000894 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000895 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000896
Devang Patel2d9caf92009-11-25 17:36:49 +0000897 if (!Dir.empty())
Devang Patelf20c4f72011-04-12 22:53:02 +0000898 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000899 if (DIUnit.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000900 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +0000901
Devang Patel2d9caf92009-11-25 17:36:49 +0000902 StringRef Flags = DIUnit.getFlags();
903 if (!Flags.empty())
Devang Patelf20c4f72011-04-12 22:53:02 +0000904 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
905
Bill Wendling2b128d72009-05-20 23:19:06 +0000906 unsigned RVer = DIUnit.getRunTimeVersion();
907 if (RVer)
Devang Patelf20c4f72011-04-12 22:53:02 +0000908 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000909 dwarf::DW_FORM_data1, RVer);
910
Devang Patel1a0df9a2010-05-10 22:49:55 +0000911 if (!FirstCU)
912 FirstCU = NewCU;
913 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendling2b128d72009-05-20 23:19:06 +0000914}
915
Devang Patel1a0df9a2010-05-10 22:49:55 +0000916/// getCompielUnit - Get CompileUnit DIE.
917CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
918 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
919 DIDescriptor D(N);
920 const MDNode *CUNode = NULL;
921 if (D.isCompileUnit())
922 CUNode = N;
923 else if (D.isSubprogram())
924 CUNode = DISubprogram(N).getCompileUnit();
925 else if (D.isType())
926 CUNode = DIType(N).getCompileUnit();
927 else if (D.isGlobalVariable())
928 CUNode = DIGlobalVariable(N).getCompileUnit();
929 else if (D.isVariable())
930 CUNode = DIVariable(N).getCompileUnit();
931 else if (D.isNameSpace())
932 CUNode = DINameSpace(N).getCompileUnit();
933 else if (D.isFile())
934 CUNode = DIFile(N).getCompileUnit();
935 else
936 return FirstCU;
937
938 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
939 = CUMap.find(CUNode);
940 if (I == CUMap.end())
941 return FirstCU;
942 return I->second;
943}
944
Devang Patela8652672010-08-23 18:25:56 +0000945/// isUnsignedDIType - Return true if type encoding is unsigned.
946static bool isUnsignedDIType(DIType Ty) {
947 DIDerivedType DTy(Ty);
948 if (DTy.Verify())
949 return isUnsignedDIType(DTy.getTypeDerivedFrom());
950
951 DIBasicType BTy(Ty);
952 if (BTy.Verify()) {
953 unsigned Encoding = BTy.getEncoding();
954 if (Encoding == dwarf::DW_ATE_unsigned ||
955 Encoding == dwarf::DW_ATE_unsigned_char)
956 return true;
957 }
958 return false;
959}
Devang Patel1a0df9a2010-05-10 22:49:55 +0000960
Devang Patel2d9e5322011-01-20 00:02:16 +0000961// Return const exprssion if value is a GEP to access merged global
962// constant. e.g.
963// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
964static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
965 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
966 if (!CE || CE->getNumOperands() != 3 ||
967 CE->getOpcode() != Instruction::GetElementPtr)
968 return NULL;
969
970 // First operand points to a global value.
971 if (!isa<GlobalValue>(CE->getOperand(0)))
972 return NULL;
973
974 // Second operand is zero.
975 const ConstantInt *CI =
976 dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
977 if (!CI || !CI->isZero())
978 return NULL;
979
980 // Third operand is offset.
981 if (!isa<ConstantInt>(CE->getOperand(2)))
982 return NULL;
983
984 return CE;
985}
986
Devang Patel1a0df9a2010-05-10 22:49:55 +0000987/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +0000988void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel469c12d22010-08-10 01:37:23 +0000989 DIGlobalVariable GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000990
Devang Patelf5d53602009-09-04 23:59:07 +0000991 // If debug information is malformed then ignore it.
Devang Patel469c12d22010-08-10 01:37:23 +0000992 if (GV.Verify() == false)
Devang Patelf5d53602009-09-04 23:59:07 +0000993 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000994
995 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000996 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel469c12d22010-08-10 01:37:23 +0000997 if (TheCU->getDIE(GV))
Devang Patel0751a282009-06-26 01:49:18 +0000998 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000999
Devang Patel469c12d22010-08-10 01:37:23 +00001000 DIType GTy = GV.getType();
Devang Patelb2197462010-08-10 07:11:13 +00001001 DIE *VariableDIE = new DIE(GV.getTag());
1002
1003 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001004
Devang Patel469c12d22010-08-10 01:37:23 +00001005 // Add name.
Devang Patelf20c4f72011-04-12 22:53:02 +00001006 TheCU->addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1007 GV.getDisplayName());
Devang Patel469c12d22010-08-10 01:37:23 +00001008 StringRef LinkageName = GV.getLinkageName();
Devang Patelb2197462010-08-10 07:11:13 +00001009 if (!LinkageName.empty() && isGlobalVariable)
Devang Patelf20c4f72011-04-12 22:53:02 +00001010 TheCU->addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1011 dwarf::DW_FORM_string,
1012 getRealLinkageName(LinkageName));
Devang Patel469c12d22010-08-10 01:37:23 +00001013 // Add type.
Devang Patelf20c4f72011-04-12 22:53:02 +00001014 TheCU->addType(VariableDIE, GTy);
Devang Patel12d150e2010-04-13 20:35:04 +00001015 if (GTy.isCompositeType() && !GTy.getName().empty()
1016 && !GTy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001017 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001018 assert(Entry && "Missing global type!");
Devang Patel1a0df9a2010-05-10 22:49:55 +00001019 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001020 }
Devang Patel469c12d22010-08-10 01:37:23 +00001021 // Add scoping info.
1022 if (!GV.isLocalToUnit()) {
Devang Patelf20c4f72011-04-12 22:53:02 +00001023 TheCU->addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel469c12d22010-08-10 01:37:23 +00001024 // Expose as global.
1025 TheCU->addGlobal(GV.getName(), VariableDIE);
1026 }
1027 // Add line number info.
Devang Patelf20c4f72011-04-12 22:53:02 +00001028 TheCU->addSourceLine(VariableDIE, GV);
Devang Patel469c12d22010-08-10 01:37:23 +00001029 // Add to map.
1030 TheCU->insertDIE(N, VariableDIE);
1031 // Add to context owner.
1032 DIDescriptor GVContext = GV.getContext();
Devang Patelf20c4f72011-04-12 22:53:02 +00001033 TheCU->addToContextOwner(VariableDIE, GVContext);
Devang Patel469c12d22010-08-10 01:37:23 +00001034 // Add location.
Devang Patelb2197462010-08-10 07:11:13 +00001035 if (isGlobalVariable) {
1036 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelf20c4f72011-04-12 22:53:02 +00001037 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1038 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patelb2197462010-08-10 07:11:13 +00001039 Asm->Mang->getSymbol(GV.getGlobal()));
1040 // Do not create specification DIE if context is either compile unit
1041 // or a subprogram.
1042 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1043 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1044 // Create specification DIE.
1045 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patelf20c4f72011-04-12 22:53:02 +00001046 TheCU->addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
Devang Patelb2197462010-08-10 07:11:13 +00001047 dwarf::DW_FORM_ref4, VariableDIE);
Devang Patelf20c4f72011-04-12 22:53:02 +00001048 TheCU->addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1049 TheCU->addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patelb2197462010-08-10 07:11:13 +00001050 TheCU->addDie(VariableSpecDIE);
1051 } else {
Devang Patelf20c4f72011-04-12 22:53:02 +00001052 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patelb2197462010-08-10 07:11:13 +00001053 }
Devang Patel70eb9822011-01-06 21:39:25 +00001054 } else if (ConstantInt *CI =
1055 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
Devang Patelf20c4f72011-04-12 22:53:02 +00001056 TheCU->addConstantValue(VariableDIE, CI, isUnsignedDIType(GTy));
Devang Patel2d9e5322011-01-20 00:02:16 +00001057 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
1058 // GV is a merged global.
1059 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelf20c4f72011-04-12 22:53:02 +00001060 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1061 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
1062 Asm->Mang->getSymbol(cast<GlobalValue>(CE->getOperand(0))));
Devang Patel2d9e5322011-01-20 00:02:16 +00001063 ConstantInt *CII = cast<ConstantInt>(CE->getOperand(2));
Devang Patelf20c4f72011-04-12 22:53:02 +00001064 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1065 TheCU->addUInt(Block, 0, dwarf::DW_FORM_udata, CII->getZExtValue());
1066 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1067 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel2d9e5322011-01-20 00:02:16 +00001068 }
Devang Patel70eb9822011-01-06 21:39:25 +00001069
Devang Patel0751a282009-06-26 01:49:18 +00001070 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001071}
1072
Devang Patel1a0df9a2010-05-10 22:49:55 +00001073/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00001074void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001075 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +00001076
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001077 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001078 CompileUnit *TheCU = getCompileUnit(N);
1079 if (TheCU->getDIE(N))
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001080 return;
1081
Bill Wendling2b128d72009-05-20 23:19:06 +00001082 if (!SP.isDefinition())
1083 // This is a method declaration which will be handled while constructing
1084 // class type.
Devang Patel0751a282009-06-26 01:49:18 +00001085 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001086
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001087 DIE *SubprogramDie = createSubprogramDIE(SP);
1088
1089 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001090 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001091
1092 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +00001093 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +00001094
Bill Wendling2b128d72009-05-20 23:19:06 +00001095 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001096 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001097
Devang Patel0751a282009-06-26 01:49:18 +00001098 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001099}
1100
Devang Patel930143b2009-11-21 02:48:08 +00001101/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +00001102/// content. Create global DIEs and emit initial debug info sections.
1103/// This is inovked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +00001104void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +00001105 if (DisableDebugInfoPrinting)
1106 return;
1107
Devang Patel63524442009-07-30 18:56:46 +00001108 DebugInfoFinder DbgFinder;
1109 DbgFinder.processModule(*M);
Devang Patel0751a282009-06-26 01:49:18 +00001110
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001111 bool HasDebugInfo = false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001112
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001113 // Scan all the compile-units to see if there are any marked as the main unit.
1114 // if not, we do not generate debug info.
1115 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1116 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1117 if (DICompileUnit(*I).isMain()) {
1118 HasDebugInfo = true;
1119 break;
1120 }
1121 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001122
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001123 if (!HasDebugInfo) return;
1124
1125 // Tell MMI that we have debug info.
1126 MMI->setDebugInfoAvailability(true);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001127
Chris Lattnerd442aa32010-04-04 23:17:54 +00001128 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001129 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001130
Bill Wendling2b128d72009-05-20 23:19:06 +00001131 // Create all the compile unit DIEs.
Devang Patel63524442009-07-30 18:56:46 +00001132 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1133 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001134 constructCompileUnit(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00001135
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001136 // Create DIEs for each subprogram.
Devang Patel63524442009-07-30 18:56:46 +00001137 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1138 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001139 constructSubprogramDIE(*I);
Devang Patel0751a282009-06-26 01:49:18 +00001140
Devang Patel2b75ed22009-12-10 19:14:49 +00001141 // Create DIEs for each global variable.
1142 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1143 E = DbgFinder.global_variable_end(); I != E; ++I)
1144 constructGlobalVariableDIE(*I);
1145
Devang Patel8e06a5e2010-08-10 20:01:20 +00001146 //getOrCreateTypeDIE
1147 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patelf20c4f72011-04-12 22:53:02 +00001148 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1149 DIType Ty(NMD->getOperand(i));
1150 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1151 }
Devang Patel8e06a5e2010-08-10 20:01:20 +00001152
Devang Patel7a554812010-09-28 18:08:20 +00001153 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patelf20c4f72011-04-12 22:53:02 +00001154 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1155 DIType Ty(NMD->getOperand(i));
1156 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1157 }
Devang Patel7a554812010-09-28 18:08:20 +00001158
Bill Wendling2b128d72009-05-20 23:19:06 +00001159 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +00001160 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00001161}
1162
Devang Patel930143b2009-11-21 02:48:08 +00001163/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +00001164///
Devang Patel930143b2009-11-21 02:48:08 +00001165void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001166 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +00001167 const Module *M = MMI->getModule();
Devang Pateld0701282010-08-02 17:32:15 +00001168 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +00001169 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
1170 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
1171 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
1172 DISubprogram SP(AllSPs->getOperand(SI));
1173 if (!SP.Verify()) continue;
1174
1175 // Collect info for variables that were optimized out.
Devang Patel18efced2010-07-19 17:53:55 +00001176 if (!SP.isDefinition()) continue;
Devang Patelf3b2db62010-06-28 18:25:03 +00001177 StringRef FName = SP.getLinkageName();
1178 if (FName.empty())
1179 FName = SP.getName();
Devang Patel364bf042010-11-10 22:19:21 +00001180 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patelf3b2db62010-06-28 18:25:03 +00001181 if (!NMD) continue;
1182 unsigned E = NMD->getNumOperands();
1183 if (!E) continue;
1184 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Pateld0701282010-08-02 17:32:15 +00001185 DeadFnScopeMap[SP] = Scope;
Devang Patelf3b2db62010-06-28 18:25:03 +00001186 for (unsigned I = 0; I != E; ++I) {
1187 DIVariable DV(NMD->getOperand(I));
1188 if (!DV.Verify()) continue;
1189 Scope->addVariable(new DbgVariable(DV));
1190 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001191
Devang Patelf3b2db62010-06-28 18:25:03 +00001192 // Construct subprogram DIE and add variables DIEs.
1193 constructSubprogramDIE(SP);
1194 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patel406798a2010-08-09 18:51:29 +00001195 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patelf3b2db62010-06-28 18:25:03 +00001196 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1197 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
1198 if (VariableDIE)
1199 ScopeDIE->addChild(VariableDIE);
1200 }
1201 }
1202 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001203
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001204 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1205 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1206 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1207 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +00001208 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001209 }
1210
Devang Patel32cc43c2010-05-07 20:54:48 +00001211 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Pateleb57c592009-12-03 19:11:07 +00001212 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1213 DIE *SPDie = CI->first;
Devang Patel32cc43c2010-05-07 20:54:48 +00001214 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Pateleb57c592009-12-03 19:11:07 +00001215 if (!N) continue;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001216 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Pateleb57c592009-12-03 19:11:07 +00001217 if (!NDie) continue;
Devang Patelf20c4f72011-04-12 22:53:02 +00001218 getCompileUnit(N)->addDIEEntry(SPDie, dwarf::DW_AT_containing_type,
1219 dwarf::DW_FORM_ref4, NDie);
Devang Pateleb57c592009-12-03 19:11:07 +00001220 }
1221
Bill Wendling2b128d72009-05-20 23:19:06 +00001222 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001223 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +00001224 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001225 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +00001226 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +00001227
1228 // End text sections.
1229 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001230 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +00001231 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +00001232 }
1233
1234 // Emit common frame information.
Devang Patel930143b2009-11-21 02:48:08 +00001235 emitCommonDebugFrame();
Bill Wendling2b128d72009-05-20 23:19:06 +00001236
1237 // Emit function debug frame information
1238 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1239 E = DebugFrames.end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001240 emitFunctionDebugFrame(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00001241
1242 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +00001243 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +00001244
1245 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +00001246 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001247
1248 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +00001249 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +00001250
Bill Wendling2b128d72009-05-20 23:19:06 +00001251 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +00001252 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +00001253
Devang Patel04d2f2d2009-11-24 01:14:22 +00001254 // Emit info into a debug pubtypes section.
1255 emitDebugPubTypes();
1256
Bill Wendling2b128d72009-05-20 23:19:06 +00001257 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00001258 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +00001259
1260 // Emit info into a debug aranges section.
1261 EmitDebugARanges();
1262
1263 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00001264 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +00001265
1266 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00001267 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001268
1269 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +00001270 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001271
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001272 // Emit info into a debug str section.
1273 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001274
Devang Pateld0701282010-08-02 17:32:15 +00001275 // clean up.
1276 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001277 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1278 E = CUMap.end(); I != E; ++I)
1279 delete I->second;
1280 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +00001281}
1282
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001283/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001284DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattner915c5f92010-04-02 19:42:39 +00001285 DebugLoc ScopeLoc) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001286
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001287 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001288 if (AbsDbgVariable)
1289 return AbsDbgVariable;
1290
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001291 LLVMContext &Ctx = Var->getContext();
Chris Lattner915c5f92010-04-02 19:42:39 +00001292 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001293 if (!Scope)
1294 return NULL;
1295
Devang Patele1c53f22010-05-20 16:36:41 +00001296 AbsDbgVariable = new DbgVariable(Var);
Devang Patel930143b2009-11-21 02:48:08 +00001297 Scope->addVariable(AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001298 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001299 return AbsDbgVariable;
1300}
1301
Devang Patel6c622ef2011-03-01 22:58:55 +00001302/// addCurrentFnArgument - If Var is an current function argument that add
1303/// it in CurrentFnArguments list.
1304bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
1305 DbgVariable *Var, DbgScope *Scope) {
1306 if (Scope != CurrentFnDbgScope)
1307 return false;
1308 DIVariable DV = Var->getVariable();
1309 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1310 return false;
1311 unsigned ArgNo = DV.getArgNumber();
1312 if (ArgNo == 0)
1313 return false;
1314
Devang Patel4ab660b2011-03-03 20:02:02 +00001315 size_t Size = CurrentFnArguments.size();
1316 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001317 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001318 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001319 // arguments does the function have at source level.
1320 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001321 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001322 CurrentFnArguments[ArgNo - 1] = Var;
1323 return true;
1324}
1325
Devang Patel490c8ab2010-05-20 19:57:06 +00001326/// collectVariableInfoFromMMITable - Collect variable information from
1327/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001328void
Devang Patel490c8ab2010-05-20 19:57:06 +00001329DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
1330 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00001331 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patel475d32a2009-10-06 01:26:37 +00001332 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1333 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1334 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001335 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001336 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001337 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001338 DIVariable DV(Var);
1339 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001340
Chris Lattner915c5f92010-04-02 19:42:39 +00001341 DbgScope *Scope = 0;
Devang Patel32cc43c2010-05-07 20:54:48 +00001342 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattner915c5f92010-04-02 19:42:39 +00001343 Scope = ConcreteScopes.lookup(IA);
1344 if (Scope == 0)
1345 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001346
Devang Patelcdb7d442009-11-10 23:20:04 +00001347 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001348 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001349 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001350
Devang Patele1c53f22010-05-20 16:36:41 +00001351 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
1352 DbgVariable *RegVar = new DbgVariable(DV);
1353 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +00001354 if (!addCurrentFnArgument(MF, RegVar, Scope))
1355 Scope->addVariable(RegVar);
Devang Patele1c53f22010-05-20 16:36:41 +00001356 if (AbsDbgVariable) {
1357 recordVariableFrameIndex(AbsDbgVariable, VP.first);
1358 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
1359 }
Devang Patel475d32a2009-10-06 01:26:37 +00001360 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001361}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001362
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001363/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +00001364/// DBG_VALUE instruction, is in a defined reg.
1365static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
1366 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001367 return MI->getNumOperands() == 3 &&
1368 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1369 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +00001370}
1371
Devang Patel490c8ab2010-05-20 19:57:06 +00001372/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001373void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001374DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1375 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001376
Devang Patel490c8ab2010-05-20 19:57:06 +00001377 /// collection info from MMI table.
1378 collectVariableInfoFromMMITable(MF, Processed);
1379
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001380 for (SmallVectorImpl<const MDNode*>::const_iterator
1381 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1382 ++UVI) {
1383 const MDNode *Var = *UVI;
1384 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001385 continue;
1386
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001387 // History contains relevant DBG_VALUE instructions for Var and instructions
1388 // clobbering it.
1389 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1390 if (History.empty())
1391 continue;
1392 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001393
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001394 DIVariable DV(Var);
Devang Patel447cb382011-01-11 21:42:10 +00001395 DbgScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001396 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1397 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelfbd6c452010-05-21 00:10:20 +00001398 Scope = CurrentFnDbgScope;
Devang Patel447cb382011-01-11 21:42:10 +00001399 else
1400 Scope = findDbgScope(MInsn);
Devang Patel490c8ab2010-05-20 19:57:06 +00001401 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001402 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001403 continue;
1404
1405 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001406 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel490c8ab2010-05-20 19:57:06 +00001407 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel6c622ef2011-03-01 22:58:55 +00001408 if (!addCurrentFnArgument(MF, RegVar, Scope))
1409 Scope->addVariable(RegVar);
Devang Patelfbd6c452010-05-21 00:10:20 +00001410 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
1411 DbgVariableToDbgInstMap[AbsVar] = MInsn;
1412 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patela3e9c9c2010-03-15 18:33:46 +00001413 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001414
1415 // Simple ranges that are fully coalesced.
1416 if (History.size() <= 1 || (History.size() == 2 &&
1417 MInsn->isIdenticalTo(History.back()))) {
Devang Patel9fc11702010-05-25 23:40:22 +00001418 DbgVariableToDbgInstMap[RegVar] = MInsn;
1419 continue;
1420 }
1421
1422 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001423 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001424
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001425 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1426 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1427 const MachineInstr *Begin = *HI;
1428 assert(Begin->isDebugValue() && "Invalid History entry");
Devang Patel9fc11702010-05-25 23:40:22 +00001429 MachineLocation MLoc;
Devang Patel0e60e672010-08-04 18:40:52 +00001430 if (Begin->getNumOperands() == 3) {
1431 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
1432 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
1433 } else
1434 MLoc = Asm->getDebugValueLocation(Begin);
1435
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001436 // FIXME: emitDebugLoc only understands registers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001437 if (!MLoc.getReg())
1438 continue;
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001439
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001440 // Compute the range for a register location.
1441 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1442 const MCSymbol *SLabel = 0;
1443
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001444 if (HI + 1 == HE)
1445 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001446 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001447 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001448 else {
1449 const MachineInstr *End = HI[1];
1450 if (End->isDebugValue())
1451 SLabel = getLabelBeforeInsn(End);
1452 else {
1453 // End is a normal instruction clobbering the range.
1454 SLabel = getLabelAfterInsn(End);
1455 assert(SLabel && "Forgot label after clobber instruction");
1456 ++HI;
1457 }
1458 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001459
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001460 // The value is valid until the next DBG_VALUE or clobber.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001461 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
Devang Patel9fc11702010-05-25 23:40:22 +00001462 }
1463 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001464 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001465
1466 // Collect info for variables that were optimized out.
Devang Patelad517352010-06-22 01:01:58 +00001467 const Function *F = MF->getFunction();
Devang Patel364bf042010-11-10 22:19:21 +00001468 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patele0a94bf2010-05-14 21:01:35 +00001469 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00001470 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001471 if (!DV || !Processed.insert(DV))
Devang Patele0a94bf2010-05-14 21:01:35 +00001472 continue;
1473 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
1474 if (Scope)
Devang Patele1c53f22010-05-20 16:36:41 +00001475 Scope->addVariable(new DbgVariable(DV));
Devang Patele0a94bf2010-05-14 21:01:35 +00001476 }
1477 }
Devang Patel9fc11702010-05-25 23:40:22 +00001478}
Devang Patele0a94bf2010-05-14 21:01:35 +00001479
Devang Patel9fc11702010-05-25 23:40:22 +00001480/// getLabelBeforeInsn - Return Label preceding the instruction.
1481const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001482 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1483 assert(Label && "Didn't insert label before instruction");
1484 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001485}
1486
1487/// getLabelAfterInsn - Return Label immediately following the instruction.
1488const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001489 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001490}
1491
Devang Patelb5694e72010-10-26 17:49:02 +00001492/// beginInstruction - Process beginning of an instruction.
1493void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001494 // Check if source location changes, but ignore DBG_VALUE locations.
1495 if (!MI->isDebugValue()) {
1496 DebugLoc DL = MI->getDebugLoc();
1497 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
1498 PrevInstLoc = DL;
1499 if (!DL.isUnknown()) {
1500 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
1501 recordSourceLine(DL.getLine(), DL.getCol(), Scope);
1502 } else
1503 recordSourceLine(0, 0, 0);
1504 }
Devang Patel9fc11702010-05-25 23:40:22 +00001505 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001506
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001507 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001508 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1509 LabelsBeforeInsn.find(MI);
1510
1511 // No label needed.
1512 if (I == LabelsBeforeInsn.end())
1513 return;
1514
1515 // Label already assigned.
1516 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001517 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001518
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001519 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001520 PrevLabel = MMI->getContext().CreateTempSymbol();
1521 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001522 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001523 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001524}
1525
Devang Patelb5694e72010-10-26 17:49:02 +00001526/// endInstruction - Process end of an instruction.
1527void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001528 // Don't create a new label after DBG_VALUE instructions.
1529 // They don't generate code.
1530 if (!MI->isDebugValue())
1531 PrevLabel = 0;
1532
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001533 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1534 LabelsAfterInsn.find(MI);
1535
1536 // No label needed.
1537 if (I == LabelsAfterInsn.end())
1538 return;
1539
1540 // Label already assigned.
1541 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001542 return;
1543
1544 // We need a label after this instruction.
1545 if (!PrevLabel) {
1546 PrevLabel = MMI->getContext().CreateTempSymbol();
1547 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001548 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001549 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001550}
1551
Devang Patel6c74a872010-04-27 19:46:33 +00001552/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001553DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel30265c42010-07-07 20:12:52 +00001554 const MDNode *InlinedAt) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001555 if (!InlinedAt) {
1556 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1557 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00001558 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001559 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1560 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patel6c74a872010-04-27 19:46:33 +00001561 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001562 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001563 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Patel6c74a872010-04-27 19:46:33 +00001564 WScope->setParent(Parent);
1565 Parent->addScope(WScope);
1566 }
1567
1568 if (!WScope->getParent()) {
1569 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastings9b5005c2010-06-15 23:06:30 +00001570 // We used to check only for a linkage name, but that fails
1571 // since we began omitting the linkage name for private
1572 // functions. The new way is to check for the name in metadata,
1573 // but that's not supported in old .ll test cases. Ergo, we
1574 // check both.
Stuart Hastingsafe54f12010-06-11 20:08:44 +00001575 if (SPName == Asm->MF->getFunction()->getName() ||
1576 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Patel6c74a872010-04-27 19:46:33 +00001577 CurrentFnDbgScope = WScope;
1578 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001579
Devang Patel6c74a872010-04-27 19:46:33 +00001580 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001581 }
1582
Devang Patel5c0f85c2010-06-25 22:07:34 +00001583 getOrCreateAbstractScope(Scope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001584 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1585 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00001586 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001587
1588 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1589 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1590 DILocation DL(InlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00001591 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001592 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Patel6c74a872010-04-27 19:46:33 +00001593 WScope->setParent(Parent);
1594 Parent->addScope(WScope);
1595
1596 ConcreteScopes[InlinedAt] = WScope;
Devang Patel6c74a872010-04-27 19:46:33 +00001597
1598 return WScope;
Devang Patel8db360d2009-10-06 01:50:42 +00001599}
1600
Devang Patel6c74a872010-04-27 19:46:33 +00001601/// hasValidLocation - Return true if debug location entry attached with
1602/// machine instruction encodes valid location info.
1603static bool hasValidLocation(LLVMContext &Ctx,
1604 const MachineInstr *MInsn,
Devang Patel32cc43c2010-05-07 20:54:48 +00001605 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Patel6c74a872010-04-27 19:46:33 +00001606 DebugLoc DL = MInsn->getDebugLoc();
1607 if (DL.isUnknown()) return false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001608
Devang Patel32cc43c2010-05-07 20:54:48 +00001609 const MDNode *S = DL.getScope(Ctx);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001610
Devang Patel6c74a872010-04-27 19:46:33 +00001611 // There is no need to create another DIE for compile unit. For all
1612 // other scopes, create one DbgScope now. This will be translated
1613 // into a scope DIE at the end.
1614 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001615
Devang Patel6c74a872010-04-27 19:46:33 +00001616 Scope = S;
1617 InlinedAt = DL.getInlinedAt(Ctx);
1618 return true;
1619}
1620
1621/// calculateDominanceGraph - Calculate dominance graph for DbgScope
1622/// hierarchy.
1623static void calculateDominanceGraph(DbgScope *Scope) {
1624 assert (Scope && "Unable to calculate scop edominance graph!");
1625 SmallVector<DbgScope *, 4> WorkStack;
1626 WorkStack.push_back(Scope);
1627 unsigned Counter = 0;
1628 while (!WorkStack.empty()) {
1629 DbgScope *WS = WorkStack.back();
1630 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
1631 bool visitedChildren = false;
1632 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
1633 SE = Children.end(); SI != SE; ++SI) {
1634 DbgScope *ChildScope = *SI;
1635 if (!ChildScope->getDFSOut()) {
1636 WorkStack.push_back(ChildScope);
1637 visitedChildren = true;
1638 ChildScope->setDFSIn(++Counter);
1639 break;
1640 }
1641 }
1642 if (!visitedChildren) {
1643 WorkStack.pop_back();
1644 WS->setDFSOut(++Counter);
1645 }
1646 }
1647}
1648
1649/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001650static
Devang Patel6c74a872010-04-27 19:46:33 +00001651void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
1652 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
1653{
1654#ifndef NDEBUG
1655 unsigned PrevDFSIn = 0;
1656 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1657 I != E; ++I) {
1658 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1659 II != IE; ++II) {
1660 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00001661 const MDNode *Scope = NULL;
1662 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00001663
1664 // Check if instruction has valid location information.
1665 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
1666 dbgs() << " [ ";
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001667 if (InlinedAt)
Devang Patel6c74a872010-04-27 19:46:33 +00001668 dbgs() << "*";
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001669 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Patel6c74a872010-04-27 19:46:33 +00001670 MI2ScopeMap.find(MInsn);
1671 if (DI != MI2ScopeMap.end()) {
1672 DbgScope *S = DI->second;
1673 dbgs() << S->getDFSIn();
1674 PrevDFSIn = S->getDFSIn();
1675 } else
1676 dbgs() << PrevDFSIn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001677 } else
Devang Patel6c74a872010-04-27 19:46:33 +00001678 dbgs() << " [ x" << PrevDFSIn;
1679 dbgs() << " ]";
1680 MInsn->dump();
1681 }
1682 dbgs() << "\n";
1683 }
1684#endif
1685}
Devang Patel930143b2009-11-21 02:48:08 +00001686/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner848c7d22010-03-31 05:39:57 +00001687/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner76555b52010-01-26 23:18:02 +00001688bool DwarfDebug::extractScopeInformation() {
Devang Patel75cc16c2009-10-01 20:31:14 +00001689 // If scope information was extracted using .dbg intrinsics then there is not
1690 // any need to extract these information by scanning each instruction.
1691 if (!DbgScopeMap.empty())
1692 return false;
1693
Dan Gohmane9135cb2010-04-23 01:18:53 +00001694 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6c74a872010-04-27 19:46:33 +00001695 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
1696 SmallVector<DbgRange, 4> MIRanges;
1697 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patel32cc43c2010-05-07 20:54:48 +00001698 const MDNode *PrevScope = NULL;
1699 const MDNode *PrevInlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00001700 const MachineInstr *RangeBeginMI = NULL;
1701 const MachineInstr *PrevMI = NULL;
Chris Lattner3a383cb2010-04-05 00:13:49 +00001702 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel75cc16c2009-10-01 20:31:14 +00001703 I != E; ++I) {
1704 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1705 II != IE; ++II) {
1706 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00001707 const MDNode *Scope = NULL;
1708 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00001709
1710 // Check if instruction has valid location information.
1711 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
1712 PrevMI = MInsn;
1713 continue;
1714 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001715
Devang Patel6c74a872010-04-27 19:46:33 +00001716 // If scope has not changed then skip this instruction.
1717 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
1718 PrevMI = MInsn;
1719 continue;
1720 }
1721
Devang Pateld12c0a22011-02-15 17:56:09 +00001722 // Ignore DBG_VALUE. It does not contribute any instruction in output.
1723 if (MInsn->isDebugValue())
1724 continue;
1725
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001726 if (RangeBeginMI) {
1727 // If we have alread seen a beginning of a instruction range and
Devang Patel6c74a872010-04-27 19:46:33 +00001728 // current instruction scope does not match scope of first instruction
1729 // in this range then create a new instruction range.
1730 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001731 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel30265c42010-07-07 20:12:52 +00001732 PrevInlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00001733 MIRanges.push_back(R);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001734 }
Devang Patel6c74a872010-04-27 19:46:33 +00001735
1736 // This is a beginning of a new instruction range.
1737 RangeBeginMI = MInsn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001738
Devang Patel6c74a872010-04-27 19:46:33 +00001739 // Reset previous markers.
1740 PrevMI = MInsn;
1741 PrevScope = Scope;
1742 PrevInlinedAt = InlinedAt;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001743 }
1744 }
1745
Devang Patel6c74a872010-04-27 19:46:33 +00001746 // Create last instruction range.
1747 if (RangeBeginMI && PrevMI && PrevScope) {
1748 DbgRange R(RangeBeginMI, PrevMI);
1749 MIRanges.push_back(R);
1750 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel75cc16c2009-10-01 20:31:14 +00001751 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001752
Devang Patel530a0752010-01-04 20:44:00 +00001753 if (!CurrentFnDbgScope)
1754 return false;
1755
Devang Patel6c74a872010-04-27 19:46:33 +00001756 calculateDominanceGraph(CurrentFnDbgScope);
1757 if (PrintDbgScope)
1758 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
1759
1760 // Find ranges of instructions covered by each DbgScope;
1761 DbgScope *PrevDbgScope = NULL;
1762 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
1763 RE = MIRanges.end(); RI != RE; ++RI) {
1764 const DbgRange &R = *RI;
1765 DbgScope *S = MI2ScopeMap.lookup(R.first);
1766 assert (S && "Lost DbgScope for a machine instruction!");
1767 if (PrevDbgScope && !PrevDbgScope->dominates(S))
1768 PrevDbgScope->closeInsnRange(S);
1769 S->openInsnRange(R.first);
1770 S->extendInsnRange(R.second);
1771 PrevDbgScope = S;
1772 }
1773
1774 if (PrevDbgScope)
1775 PrevDbgScope->closeInsnRange();
Devang Patel75cc16c2009-10-01 20:31:14 +00001776
Devang Patel359b0132010-04-08 18:43:56 +00001777 identifyScopeMarkers();
Devang Patelf1d5a1e2010-04-08 15:37:09 +00001778
1779 return !DbgScopeMap.empty();
1780}
1781
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001782/// identifyScopeMarkers() -
Devang Patel6c74a872010-04-27 19:46:33 +00001783/// Each DbgScope has first instruction and last instruction to mark beginning
1784/// and end of a scope respectively. Create an inverse map that list scopes
1785/// starts (and ends) with an instruction. One instruction may start (or end)
1786/// multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001787void DwarfDebug::identifyScopeMarkers() {
Devang Patel7771b7c2010-01-20 02:05:23 +00001788 SmallVector<DbgScope *, 4> WorkList;
1789 WorkList.push_back(CurrentFnDbgScope);
1790 while (!WorkList.empty()) {
Chris Lattner848c7d22010-03-31 05:39:57 +00001791 DbgScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001792
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001793 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001794 if (!Children.empty())
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001795 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001796 SE = Children.end(); SI != SE; ++SI)
1797 WorkList.push_back(*SI);
1798
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001799 if (S->isAbstractScope())
1800 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001801
Devang Patel6c74a872010-04-27 19:46:33 +00001802 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
1803 if (Ranges.empty())
1804 continue;
1805 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1806 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001807 assert(RI->first && "DbgRange does not have first instruction!");
1808 assert(RI->second && "DbgRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001809 requestLabelBeforeInsn(RI->first);
1810 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001811 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001812 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001813}
1814
Dan Gohman3df671a2010-04-20 00:37:27 +00001815/// FindFirstDebugLoc - Find the first debug location in the function. This
1816/// is intended to be an approximation for the source position of the
1817/// beginning of the function.
1818static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
1819 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1820 I != E; ++I)
1821 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
1822 MBBI != MBBE; ++MBBI) {
1823 DebugLoc DL = MBBI->getDebugLoc();
1824 if (!DL.isUnknown())
1825 return DL;
1826 }
1827 return DebugLoc();
1828}
1829
Devang Patela86114b2010-10-25 20:45:32 +00001830#ifndef NDEBUG
1831/// CheckLineNumbers - Count basicblocks whose instructions do not have any
1832/// line number information.
1833static void CheckLineNumbers(const MachineFunction *MF) {
1834 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1835 I != E; ++I) {
1836 bool FoundLineNo = false;
1837 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1838 II != IE; ++II) {
1839 const MachineInstr *MI = II;
1840 if (!MI->getDebugLoc().isUnknown()) {
1841 FoundLineNo = true;
1842 break;
1843 }
1844 }
Devang Patel6e0d5892010-10-28 22:11:59 +00001845 if (!FoundLineNo && I->size())
Devang Patela86114b2010-10-25 20:45:32 +00001846 ++BlocksWithoutLineNo;
1847 }
1848}
1849#endif
1850
Devang Patel930143b2009-11-21 02:48:08 +00001851/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001852/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001853void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001854 if (!MMI->hasDebugInfo()) return;
Bill Wendlingfcc14142010-04-07 09:28:04 +00001855 if (!extractScopeInformation()) return;
Devang Patel4598eb62009-10-06 18:37:31 +00001856
Devang Patela86114b2010-10-25 20:45:32 +00001857#ifndef NDEBUG
1858 CheckLineNumbers(MF);
1859#endif
1860
Devang Patel6c74a872010-04-27 19:46:33 +00001861 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1862 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001863 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001864 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001865
1866 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1867 // function.
Dan Gohman3df671a2010-04-20 00:37:27 +00001868 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattner915c5f92010-04-02 19:42:39 +00001869 if (FDL.isUnknown()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001870
Devang Patel32cc43c2010-05-07 20:54:48 +00001871 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings61475c52010-07-19 23:56:30 +00001872 const MDNode *TheScope = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001873
Chris Lattner915c5f92010-04-02 19:42:39 +00001874 DISubprogram SP = getDISubprogram(Scope);
1875 unsigned Line, Col;
1876 if (SP.Verify()) {
1877 Line = SP.getLineNumber();
1878 Col = 0;
Stuart Hastings61475c52010-07-19 23:56:30 +00001879 TheScope = SP;
Chris Lattner915c5f92010-04-02 19:42:39 +00001880 } else {
1881 Line = FDL.getLine();
1882 Col = FDL.getCol();
Stuart Hastings61475c52010-07-19 23:56:30 +00001883 TheScope = Scope;
Bill Wendling2b128d72009-05-20 23:19:06 +00001884 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001885
Stuart Hastings61475c52010-07-19 23:56:30 +00001886 recordSourceLine(Line, Col, TheScope);
Devang Patel002d54d2010-05-26 19:37:24 +00001887
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001888 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1889
Devang Patel21ccf052010-06-02 16:42:51 +00001890 /// ProcessedArgs - Collection of arguments already processed.
1891 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
1892
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001893 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1894
1895 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1896 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1897
Devang Patel002d54d2010-05-26 19:37:24 +00001898 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001899 I != E; ++I) {
1900 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001901 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1902 II != IE; ++II) {
1903 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001904
Devang Patel002d54d2010-05-26 19:37:24 +00001905 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00001906 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001907
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001908 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001909 const MDNode *Var =
1910 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001911
1912 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001913 if (isDbgValueInDefinedReg(MI))
1914 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1915
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001916 // Check the history of this variable.
1917 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1918 if (History.empty()) {
1919 UserVariables.push_back(Var);
1920 // The first mention of a function argument gets the FunctionBeginSym
1921 // label, so arguments are visible when breaking at function entry.
1922 DIVariable DV(Var);
1923 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1924 DISubprogram(getDISubprogram(DV.getContext()))
1925 .describes(MF->getFunction()))
1926 LabelsBeforeInsn[MI] = FunctionBeginSym;
1927 } else {
1928 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1929 const MachineInstr *Prev = History.back();
1930 if (Prev->isDebugValue()) {
1931 // Coalesce identical entries at the end of History.
1932 if (History.size() >= 2 &&
1933 Prev->isIdenticalTo(History[History.size() - 2]))
1934 History.pop_back();
1935
1936 // Terminate old register assignments that don't reach MI;
1937 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1938 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1939 isDbgValueInDefinedReg(Prev)) {
1940 // Previous register assignment needs to terminate at the end of
1941 // its basic block.
1942 MachineBasicBlock::const_iterator LastMI =
1943 PrevMBB->getLastNonDebugInstr();
1944 if (LastMI == PrevMBB->end())
1945 // Drop DBG_VALUE for empty range.
1946 History.pop_back();
1947 else {
1948 // Terminate after LastMI.
1949 History.push_back(LastMI);
1950 }
1951 }
1952 }
1953 }
1954 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001955 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001956 // Not a DBG_VALUE instruction.
1957 if (!MI->isLabel())
1958 AtBlockEntry = false;
1959
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001960 // Check if the instruction clobbers any registers with debug vars.
1961 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1962 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1963 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1964 continue;
1965 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1966 unsigned Reg = *AI; ++AI) {
1967 const MDNode *Var = LiveUserVar[Reg];
1968 if (!Var)
1969 continue;
1970 // Reg is now clobbered.
1971 LiveUserVar[Reg] = 0;
1972
1973 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001974 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1975 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001976 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001977 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1978 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001979 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001980 const MachineInstr *Prev = History.back();
1981 // Sanity-check: Register assignments are terminated at the end of
1982 // their block.
1983 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1984 continue;
1985 // Is the variable still in Reg?
1986 if (!isDbgValueInDefinedReg(Prev) ||
1987 Prev->getOperand(0).getReg() != Reg)
1988 continue;
1989 // Var is clobbered. Make sure the next instruction gets a label.
1990 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001991 }
1992 }
Devang Patel002d54d2010-05-26 19:37:24 +00001993 }
Devang Patel002d54d2010-05-26 19:37:24 +00001994 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001995 }
1996
1997 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1998 I != E; ++I) {
1999 SmallVectorImpl<const MachineInstr*> &History = I->second;
2000 if (History.empty())
2001 continue;
2002
2003 // Make sure the final register assignments are terminated.
2004 const MachineInstr *Prev = History.back();
2005 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
2006 const MachineBasicBlock *PrevMBB = Prev->getParent();
2007 MachineBasicBlock::const_iterator LastMI = PrevMBB->getLastNonDebugInstr();
2008 if (LastMI == PrevMBB->end())
2009 // Drop DBG_VALUE for empty range.
2010 History.pop_back();
2011 else {
2012 // Terminate after LastMI.
2013 History.push_back(LastMI);
2014 }
2015 }
2016 // Request labels for the full history.
2017 for (unsigned i = 0, e = History.size(); i != e; ++i) {
2018 const MachineInstr *MI = History[i];
2019 if (MI->isDebugValue())
2020 requestLabelBeforeInsn(MI);
2021 else
2022 requestLabelAfterInsn(MI);
2023 }
2024 }
Devang Patel002d54d2010-05-26 19:37:24 +00002025
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00002026 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00002027 PrevLabel = FunctionBeginSym;
Bill Wendling2b128d72009-05-20 23:19:06 +00002028}
2029
Devang Patel930143b2009-11-21 02:48:08 +00002030/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00002031///
Chris Lattner76555b52010-01-26 23:18:02 +00002032void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingfcc14142010-04-07 09:28:04 +00002033 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00002034
Devang Patel530a0752010-01-04 20:44:00 +00002035 if (CurrentFnDbgScope) {
Devang Patel4a8e6e82010-05-22 00:04:14 +00002036
Devang Patel9fc11702010-05-25 23:40:22 +00002037 // Define end label for subprogram.
2038 FunctionEndSym = Asm->GetTempSymbol("func_end",
2039 Asm->getFunctionNumber());
2040 // Assumes in correct section after the entry point.
2041 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002042
Devang Patel5c0f85c2010-06-25 22:07:34 +00002043 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2044 collectVariableInfo(MF, ProcessedVars);
Devang Patel4a8e6e82010-05-22 00:04:14 +00002045
Devang Patel530a0752010-01-04 20:44:00 +00002046 // Construct abstract scopes.
2047 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel5c0f85c2010-06-25 22:07:34 +00002048 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel5c0f85c2010-06-25 22:07:34 +00002049 DISubprogram SP((*AI)->getScopeNode());
2050 if (SP.Verify()) {
2051 // Collect info for variables that were optimized out.
2052 StringRef FName = SP.getLinkageName();
2053 if (FName.empty())
2054 FName = SP.getName();
Devang Patel364bf042010-11-10 22:19:21 +00002055 if (NamedMDNode *NMD =
2056 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
Devang Patel5c0f85c2010-06-25 22:07:34 +00002057 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00002058 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel5c0f85c2010-06-25 22:07:34 +00002059 if (!DV || !ProcessedVars.insert(DV))
2060 continue;
Devang Patel648df7b2010-06-30 00:11:08 +00002061 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel5c0f85c2010-06-25 22:07:34 +00002062 if (Scope)
2063 Scope->addVariable(new DbgVariable(DV));
2064 }
2065 }
2066 }
Devang Patelc5b31092010-06-30 01:40:11 +00002067 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2068 constructScopeDIE(*AI);
Devang Patel5c0f85c2010-06-25 22:07:34 +00002069 }
Devang Patel30265c42010-07-07 20:12:52 +00002070
Devang Patel075e9b52010-05-04 06:15:30 +00002071 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002072
Devang Patel075e9b52010-05-04 06:15:30 +00002073 if (!DisableFramePointerElim(*MF))
Devang Patelf20c4f72011-04-12 22:53:02 +00002074 getCompileUnit(CurrentFnDbgScope->getScopeNode())->addUInt(CurFnDIE,
2075 dwarf::DW_AT_APPLE_omit_frame_ptr,
2076 dwarf::DW_FORM_flag, 1);
Devang Patel075e9b52010-05-04 06:15:30 +00002077
2078
Chris Lattner3a383cb2010-04-05 00:13:49 +00002079 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel530a0752010-01-04 20:44:00 +00002080 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00002081 }
2082
Bill Wendling2b128d72009-05-20 23:19:06 +00002083 // Clear debug info
Devang Patelfe189e62010-01-19 01:26:02 +00002084 CurrentFnDbgScope = NULL;
Devang Patelad45d912011-04-22 18:09:57 +00002085 DeleteContainerPointers(CurrentFnArguments);
Devang Patele1c53f22010-05-20 16:36:41 +00002086 DbgVariableToFrameIndexMap.clear();
2087 VarToAbstractVarMap.clear();
2088 DbgVariableToDbgInstMap.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002089 DeleteContainerSeconds(DbgScopeMap);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002090 UserVariables.clear();
2091 DbgValues.clear();
Devang Patelfe189e62010-01-19 01:26:02 +00002092 ConcreteScopes.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002093 DeleteContainerSeconds(AbstractScopes);
Devang Patelfe189e62010-01-19 01:26:02 +00002094 AbstractScopesList.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002095 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00002096 LabelsBeforeInsn.clear();
2097 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00002098 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00002099}
2100
Devang Patele1c53f22010-05-20 16:36:41 +00002101/// recordVariableFrameIndex - Record a variable's index.
2102void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2103 assert (V && "Invalid DbgVariable!");
2104 DbgVariableToFrameIndexMap[V] = Index;
2105}
2106
2107/// findVariableFrameIndex - Return true if frame index for the variable
2108/// is found. Update FI to hold value of the index.
2109bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2110 assert (V && "Invalid DbgVariable!");
2111 DenseMap<const DbgVariable *, int>::iterator I =
2112 DbgVariableToFrameIndexMap.find(V);
2113 if (I == DbgVariableToFrameIndexMap.end())
2114 return false;
2115 *FI = I->second;
2116 return true;
2117}
2118
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002119/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patel490c8ab2010-05-20 19:57:06 +00002120/// instruction.
2121DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2122 DbgScope *Scope = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002123 LLVMContext &Ctx =
Devang Patel490c8ab2010-05-20 19:57:06 +00002124 MInsn->getParent()->getParent()->getFunction()->getContext();
2125 DebugLoc DL = MInsn->getDebugLoc();
2126
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002127 if (DL.isUnknown())
Devang Patel490c8ab2010-05-20 19:57:06 +00002128 return Scope;
2129
2130 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2131 Scope = ConcreteScopes.lookup(IA);
2132 if (Scope == 0)
2133 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002134
Devang Patel490c8ab2010-05-20 19:57:06 +00002135 return Scope;
2136}
2137
2138
Chris Lattnerba35a672010-03-09 04:54:43 +00002139/// recordSourceLine - Register a source line with debug info. Returns the
2140/// unique label that was emitted and which provides correspondence to
2141/// the source line list.
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00002142void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S){
Devang Patel2d9caf92009-11-25 17:36:49 +00002143 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00002144 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00002145 unsigned Src = 1;
2146 if (S) {
2147 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00002148
Dan Gohman50849c62010-05-05 23:41:32 +00002149 if (Scope.isCompileUnit()) {
2150 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00002151 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00002152 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00002153 } else if (Scope.isFile()) {
2154 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00002155 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00002156 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00002157 } else if (Scope.isSubprogram()) {
2158 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00002159 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00002160 Dir = SP.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00002161 } else if (Scope.isLexicalBlock()) {
2162 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00002163 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00002164 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00002165 } else
2166 assert(0 && "Unexpected scope info");
2167
Devang Patele01b75c2011-03-24 20:30:50 +00002168 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00002169 }
Rafael Espindola67c6ab82010-11-18 02:04:25 +00002170 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, DWARF2_FLAG_IS_STMT,
Devang Patel17740e72011-04-18 20:26:49 +00002171 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00002172}
2173
Bill Wendling806535f2009-05-20 23:22:40 +00002174//===----------------------------------------------------------------------===//
2175// Emit Methods
2176//===----------------------------------------------------------------------===//
2177
Devang Patel930143b2009-11-21 02:48:08 +00002178/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00002179///
Jim Grosbach00e9c612009-11-22 19:20:36 +00002180unsigned
2181DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00002182 // Get the children.
2183 const std::vector<DIE *> &Children = Die->getChildren();
2184
2185 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00002186 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00002187 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00002188
2189 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00002190 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00002191
2192 // Get the abbreviation for this DIE.
2193 unsigned AbbrevNumber = Die->getAbbrevNumber();
2194 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2195
2196 // Set DIE offset
2197 Die->setOffset(Offset);
2198
2199 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00002200 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002201
2202 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2203 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2204
2205 // Size the DIE attribute values.
2206 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2207 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00002208 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00002209
2210 // Size the DIE children if any.
2211 if (!Children.empty()) {
2212 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2213 "Children flag not set");
2214
2215 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00002216 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00002217
2218 // End of children marker.
2219 Offset += sizeof(int8_t);
2220 }
2221
2222 Die->setSize(Offset - Die->getOffset());
2223 return Offset;
2224}
2225
Devang Patel930143b2009-11-21 02:48:08 +00002226/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00002227///
Devang Patel930143b2009-11-21 02:48:08 +00002228void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002229 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2230 E = CUMap.end(); I != E; ++I) {
2231 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00002232 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00002233 sizeof(int32_t) + // Length of Compilation Unit Info
2234 sizeof(int16_t) + // DWARF version number
2235 sizeof(int32_t) + // Offset Into Abbrev. Section
2236 sizeof(int8_t); // Pointer Size (in bytes)
2237 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002238 }
Bill Wendling480ff322009-05-20 23:21:38 +00002239}
2240
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002241/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2242/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00002243static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002244 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00002245 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002246 if (!SymbolStem) return 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002247
Chris Lattner6629ca92010-04-04 22:59:04 +00002248 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2249 Asm->OutStreamer.EmitLabel(TmpSym);
2250 return TmpSym;
2251}
2252
2253/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2254/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00002255void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002256 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002257
Bill Wendling480ff322009-05-20 23:21:38 +00002258 // Dwarf sections base addresses.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002259 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6629ca92010-04-04 22:59:04 +00002260 DwarfFrameSectionSym =
2261 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2262 }
Bill Wendling480ff322009-05-20 23:21:38 +00002263
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002264 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00002265 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002266 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00002267 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002268 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002269
Chris Lattner6629ca92010-04-04 22:59:04 +00002270 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002271 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002272
Devang Patel4a213872010-08-24 00:06:12 +00002273 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002274 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2275 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2276 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002277 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00002278 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00002279 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2280 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00002281
Devang Patel9fc11702010-05-25 23:40:22 +00002282 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
2283 "section_debug_loc");
2284
Chris Lattner6629ca92010-04-04 22:59:04 +00002285 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00002286 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002287}
2288
Devang Patel930143b2009-11-21 02:48:08 +00002289/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00002290///
Devang Patel930143b2009-11-21 02:48:08 +00002291void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00002292 // Get the abbreviation for this DIE.
2293 unsigned AbbrevNumber = Die->getAbbrevNumber();
2294 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2295
Bill Wendling480ff322009-05-20 23:21:38 +00002296 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00002297 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00002298 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2299 Twine::utohexstr(Die->getOffset()) + ":0x" +
2300 Twine::utohexstr(Die->getSize()) + " " +
2301 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00002302 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002303
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00002304 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00002305 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2306
2307 // Emit the DIE attribute values.
2308 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2309 unsigned Attr = AbbrevData[i].getAttribute();
2310 unsigned Form = AbbrevData[i].getForm();
2311 assert(Form && "Too many attributes for DIE (check abbreviation)");
2312
Chris Lattner7bde8c02010-04-04 18:52:31 +00002313 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00002314 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002315
Bill Wendling480ff322009-05-20 23:21:38 +00002316 switch (Attr) {
2317 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00002318 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002319 break;
2320 case dwarf::DW_AT_abstract_origin: {
2321 DIEEntry *E = cast<DIEEntry>(Values[i]);
2322 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002323 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00002324 Asm->EmitInt32(Addr);
2325 break;
2326 }
Devang Patel12563b32010-04-16 23:33:45 +00002327 case dwarf::DW_AT_ranges: {
2328 // DW_AT_range Value encodes offset in debug_range section.
2329 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00002330
2331 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
2332 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
2333 V->getValue(),
2334 4);
2335 } else {
2336 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2337 V->getValue(),
2338 DwarfDebugRangeSectionSym,
2339 4);
2340 }
Devang Patel12563b32010-04-16 23:33:45 +00002341 break;
2342 }
Devang Patel9fc11702010-05-25 23:40:22 +00002343 case dwarf::DW_AT_location: {
2344 if (UseDotDebugLocEntry.count(Die) != 0) {
2345 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002346 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel9fc11702010-05-25 23:40:22 +00002347 } else
2348 Values[i]->EmitValue(Asm, Form);
2349 break;
2350 }
Devang Patela1bd5a12010-09-29 19:08:08 +00002351 case dwarf::DW_AT_accessibility: {
2352 if (Asm->isVerbose()) {
2353 DIEInteger *V = cast<DIEInteger>(Values[i]);
2354 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2355 }
2356 Values[i]->EmitValue(Asm, Form);
2357 break;
2358 }
Bill Wendling480ff322009-05-20 23:21:38 +00002359 default:
2360 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002361 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00002362 break;
2363 }
Bill Wendling480ff322009-05-20 23:21:38 +00002364 }
2365
2366 // Emit the DIE children if any.
2367 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2368 const std::vector<DIE *> &Children = Die->getChildren();
2369
2370 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00002371 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00002372
Chris Lattner7bde8c02010-04-04 18:52:31 +00002373 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00002374 Asm->OutStreamer.AddComment("End Of Children Mark");
2375 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00002376 }
2377}
2378
Devang Patel9ccfb642009-12-09 18:24:21 +00002379/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00002380///
Devang Patel9ccfb642009-12-09 18:24:21 +00002381void DwarfDebug::emitDebugInfo() {
2382 // Start debug info section.
2383 Asm->OutStreamer.SwitchSection(
2384 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00002385 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2386 E = CUMap.end(); I != E; ++I) {
2387 CompileUnit *TheCU = I->second;
2388 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002389
Devang Patel1a0df9a2010-05-10 22:49:55 +00002390 // Emit the compile units header.
2391 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2392 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002393
Devang Patel1a0df9a2010-05-10 22:49:55 +00002394 // Emit size of content not including length itself
2395 unsigned ContentSize = Die->getSize() +
2396 sizeof(int16_t) + // DWARF version number
2397 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00002398 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002399
Devang Patel1a0df9a2010-05-10 22:49:55 +00002400 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2401 Asm->EmitInt32(ContentSize);
2402 Asm->OutStreamer.AddComment("DWARF version number");
2403 Asm->EmitInt16(dwarf::DWARF_VERSION);
2404 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2405 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2406 DwarfAbbrevSectionSym);
2407 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2408 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002409
Devang Patel1a0df9a2010-05-10 22:49:55 +00002410 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002411 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2412 }
Bill Wendling480ff322009-05-20 23:21:38 +00002413}
2414
Devang Patel930143b2009-11-21 02:48:08 +00002415/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00002416///
Devang Patel930143b2009-11-21 02:48:08 +00002417void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00002418 // Check to see if it is worth the effort.
2419 if (!Abbreviations.empty()) {
2420 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002421 Asm->OutStreamer.SwitchSection(
2422 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002423
Chris Lattnera179b522010-04-04 19:25:43 +00002424 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00002425
2426 // For each abbrevation.
2427 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2428 // Get abbreviation data
2429 const DIEAbbrev *Abbrev = Abbreviations[i];
2430
2431 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002432 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002433
2434 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002435 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002436 }
2437
2438 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002439 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002440
Chris Lattnera179b522010-04-04 19:25:43 +00002441 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00002442 }
2443}
2444
Devang Patel930143b2009-11-21 02:48:08 +00002445/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00002446/// the line matrix.
2447///
Devang Patel930143b2009-11-21 02:48:08 +00002448void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002449 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002450 Asm->OutStreamer.AddComment("Extended Op");
2451 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002452
Chris Lattner566cae92010-03-09 23:52:58 +00002453 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002454 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002455 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2456 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2457
2458 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002459
Chris Lattnera179b522010-04-04 19:25:43 +00002460 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00002461 Asm->getTargetData().getPointerSize(),
2462 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00002463
2464 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002465 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2466 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002467 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002468 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002469}
2470
Devang Patel930143b2009-11-21 02:48:08 +00002471/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling480ff322009-05-20 23:21:38 +00002472///
Devang Patel930143b2009-11-21 02:48:08 +00002473void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002474 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002475 return;
2476
Chris Lattner3a383cb2010-04-05 00:13:49 +00002477 int stackGrowth = Asm->getTargetData().getPointerSize();
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002478 if (Asm->TM.getFrameLowering()->getStackGrowthDirection() ==
2479 TargetFrameLowering::StackGrowsDown)
Chris Lattner3a383cb2010-04-05 00:13:49 +00002480 stackGrowth *= -1;
Bill Wendling480ff322009-05-20 23:21:38 +00002481
2482 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002483 Asm->OutStreamer.SwitchSection(
2484 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002485
Chris Lattnera179b522010-04-04 19:25:43 +00002486 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner566cae92010-03-09 23:52:58 +00002487 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002488 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
2489 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002490
Chris Lattnera179b522010-04-04 19:25:43 +00002491 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner566cae92010-03-09 23:52:58 +00002492 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling480ff322009-05-20 23:21:38 +00002493 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner566cae92010-03-09 23:52:58 +00002494 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling480ff322009-05-20 23:21:38 +00002495 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner566cae92010-03-09 23:52:58 +00002496 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002497 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner9efd1182010-04-04 19:09:29 +00002498 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
2499 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner566cae92010-03-09 23:52:58 +00002500 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002501 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002502 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Bill Wendling480ff322009-05-20 23:21:38 +00002503 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling480ff322009-05-20 23:21:38 +00002504
2505 std::vector<MachineMove> Moves;
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00002506 TFI->getInitialFrameState(Moves);
Bill Wendling480ff322009-05-20 23:21:38 +00002507
Chris Lattneraabc6042010-04-04 23:41:46 +00002508 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling480ff322009-05-20 23:21:38 +00002509
Chris Lattner9e06e532010-04-28 01:05:45 +00002510 Asm->EmitAlignment(2);
Chris Lattnera179b522010-04-04 19:25:43 +00002511 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00002512}
2513
Devang Patel930143b2009-11-21 02:48:08 +00002514/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling480ff322009-05-20 23:21:38 +00002515/// section.
Chris Lattner2c3f4782010-03-13 07:26:18 +00002516void DwarfDebug::
2517emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002518 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002519 return;
2520
2521 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002522 Asm->OutStreamer.SwitchSection(
2523 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002524
Chris Lattner566cae92010-03-09 23:52:58 +00002525 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner2c3f4782010-03-13 07:26:18 +00002526 MCSymbol *DebugFrameBegin =
Chris Lattnera179b522010-04-04 19:25:43 +00002527 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner2c3f4782010-03-13 07:26:18 +00002528 MCSymbol *DebugFrameEnd =
Chris Lattnera179b522010-04-04 19:25:43 +00002529 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerf1429f12010-04-04 19:58:12 +00002530 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002531
Chris Lattner2c3f4782010-03-13 07:26:18 +00002532 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling480ff322009-05-20 23:21:38 +00002533
Chris Lattner566cae92010-03-09 23:52:58 +00002534 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002535 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner70a4fce2010-04-04 23:25:33 +00002536 DwarfFrameSectionSym);
Bill Wendling480ff322009-05-20 23:21:38 +00002537
Chris Lattner566cae92010-03-09 23:52:58 +00002538 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnera179b522010-04-04 19:25:43 +00002539 MCSymbol *FuncBeginSym =
2540 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner8811e122010-03-13 07:40:56 +00002541 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattner3a383cb2010-04-05 00:13:49 +00002542 Asm->getTargetData().getPointerSize(),
2543 0/*AddrSpace*/);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002544
2545
Chris Lattner566cae92010-03-09 23:52:58 +00002546 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002547 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattner3a383cb2010-04-05 00:13:49 +00002548 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002549
Chris Lattneraabc6042010-04-04 23:41:46 +00002550 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling480ff322009-05-20 23:21:38 +00002551
Chris Lattner9e06e532010-04-28 01:05:45 +00002552 Asm->EmitAlignment(2);
Chris Lattner2c3f4782010-03-13 07:26:18 +00002553 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling480ff322009-05-20 23:21:38 +00002554}
2555
Devang Patel9ccfb642009-12-09 18:24:21 +00002556/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2557///
2558void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002559 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2560 E = CUMap.end(); I != E; ++I) {
2561 CompileUnit *TheCU = I->second;
2562 // Start the dwarf pubnames section.
2563 Asm->OutStreamer.SwitchSection(
2564 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002565
Devang Patel1a0df9a2010-05-10 22:49:55 +00002566 Asm->OutStreamer.AddComment("Length of Public Names Info");
2567 Asm->EmitLabelDifference(
2568 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
2569 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002570
Devang Patel1a0df9a2010-05-10 22:49:55 +00002571 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
2572 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002573
Devang Patel1a0df9a2010-05-10 22:49:55 +00002574 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002575 Asm->EmitInt16(dwarf::DWARF_VERSION);
2576
Devang Patel1a0df9a2010-05-10 22:49:55 +00002577 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002578 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00002579 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002580
Devang Patel1a0df9a2010-05-10 22:49:55 +00002581 Asm->OutStreamer.AddComment("Compilation Unit Length");
2582 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2583 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2584 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002585
Devang Patel1a0df9a2010-05-10 22:49:55 +00002586 const StringMap<DIE*> &Globals = TheCU->getGlobals();
2587 for (StringMap<DIE*>::const_iterator
2588 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2589 const char *Name = GI->getKeyData();
2590 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002591
Devang Patel1a0df9a2010-05-10 22:49:55 +00002592 Asm->OutStreamer.AddComment("DIE offset");
2593 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002594
Devang Patel1a0df9a2010-05-10 22:49:55 +00002595 if (Asm->isVerbose())
2596 Asm->OutStreamer.AddComment("External Name");
2597 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
2598 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002599
Devang Patel1a0df9a2010-05-10 22:49:55 +00002600 Asm->OutStreamer.AddComment("End Mark");
2601 Asm->EmitInt32(0);
2602 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
2603 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00002604 }
Bill Wendling480ff322009-05-20 23:21:38 +00002605}
2606
Devang Patel04d2f2d2009-11-24 01:14:22 +00002607void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002608 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2609 E = CUMap.end(); I != E; ++I) {
2610 CompileUnit *TheCU = I->second;
2611 // Start the dwarf pubnames section.
2612 Asm->OutStreamer.SwitchSection(
2613 Asm->getObjFileLowering().getDwarfPubTypesSection());
2614 Asm->OutStreamer.AddComment("Length of Public Types Info");
2615 Asm->EmitLabelDifference(
2616 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
2617 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002618
Devang Patel1a0df9a2010-05-10 22:49:55 +00002619 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
2620 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002621
Devang Patel1a0df9a2010-05-10 22:49:55 +00002622 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2623 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002624
Devang Patel1a0df9a2010-05-10 22:49:55 +00002625 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2626 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
2627 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002628
Devang Patel1a0df9a2010-05-10 22:49:55 +00002629 Asm->OutStreamer.AddComment("Compilation Unit Length");
2630 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2631 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2632 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002633
Devang Patel1a0df9a2010-05-10 22:49:55 +00002634 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2635 for (StringMap<DIE*>::const_iterator
2636 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2637 const char *Name = GI->getKeyData();
2638 DIE * Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002639
Devang Patel1a0df9a2010-05-10 22:49:55 +00002640 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2641 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002642
Devang Patel1a0df9a2010-05-10 22:49:55 +00002643 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
2644 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2645 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002646
Devang Patel1a0df9a2010-05-10 22:49:55 +00002647 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002648 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002649 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
2650 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002651 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002652}
2653
Devang Patel930143b2009-11-21 02:48:08 +00002654/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00002655///
Devang Patel930143b2009-11-21 02:48:08 +00002656void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00002657 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00002658 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002659
Chris Lattner3d72a672010-03-09 23:38:23 +00002660 // Start the dwarf str section.
2661 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002662 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002663
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002664 // Get all of the string pool entries and put them in an array by their ID so
2665 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002666 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002667 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002668
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002669 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2670 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2671 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002672
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002673 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002674
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002675 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002676 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002677 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002678
Chris Lattner3d72a672010-03-09 23:38:23 +00002679 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002680 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00002681 }
2682}
2683
Devang Patel930143b2009-11-21 02:48:08 +00002684/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00002685///
Devang Patel930143b2009-11-21 02:48:08 +00002686void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002687 if (DotDebugLocEntries.empty())
2688 return;
2689
Devang Patel116a9d72011-02-04 22:57:18 +00002690 for (SmallVector<DotDebugLocEntry, 4>::iterator
2691 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2692 I != E; ++I) {
2693 DotDebugLocEntry &Entry = *I;
2694 if (I + 1 != DotDebugLocEntries.end())
2695 Entry.Merge(I+1);
2696 }
2697
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002698 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002699 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00002700 Asm->getObjFileLowering().getDwarfLocSection());
2701 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002702 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2703 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002704 for (SmallVector<DotDebugLocEntry, 4>::iterator
2705 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002706 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002707 DotDebugLocEntry &Entry = *I;
2708 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002709 if (Entry.isEmpty()) {
2710 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2711 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002712 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002713 } else {
2714 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2715 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel2266aa82011-04-21 21:07:35 +00002716 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel9fc11702010-05-25 23:40:22 +00002717 }
2718 }
Bill Wendling480ff322009-05-20 23:21:38 +00002719}
2720
2721/// EmitDebugARanges - Emit visible names into a debug aranges section.
2722///
2723void DwarfDebug::EmitDebugARanges() {
2724 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002725 Asm->OutStreamer.SwitchSection(
2726 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002727}
2728
Devang Patel930143b2009-11-21 02:48:08 +00002729/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002730///
Devang Patel930143b2009-11-21 02:48:08 +00002731void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002732 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002733 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002734 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00002735 unsigned char Size = Asm->getTargetData().getPointerSize();
2736 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002737 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002738 I != E; ++I) {
2739 if (*I)
2740 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00002741 else
Devang Patel6c74a872010-04-27 19:46:33 +00002742 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00002743 }
Bill Wendling480ff322009-05-20 23:21:38 +00002744}
2745
Devang Patel930143b2009-11-21 02:48:08 +00002746/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002747///
Devang Patel930143b2009-11-21 02:48:08 +00002748void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002749 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002750 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002751 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002752 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002753 }
2754}
2755
Devang Patel930143b2009-11-21 02:48:08 +00002756/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002757/// Section Header:
2758/// 1. length of section
2759/// 2. Dwarf version number
2760/// 3. address size.
2761///
2762/// Entries (one "entry" for each function that was inlined):
2763///
2764/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2765/// otherwise offset into __debug_str for regular function name.
2766/// 2. offset into __debug_str section for regular function name.
2767/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2768/// instances for the function.
2769///
2770/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2771/// inlined instance; the die_offset points to the inlined_subroutine die in the
2772/// __debug_info section, and the low_pc is the starting address for the
2773/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002774void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002775 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002776 return;
2777
Devang Patel1a0df9a2010-05-10 22:49:55 +00002778 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002779 return;
2780
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002781 Asm->OutStreamer.SwitchSection(
2782 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002783
Chris Lattner566cae92010-03-09 23:52:58 +00002784 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002785 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2786 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002787
Chris Lattnera179b522010-04-04 19:25:43 +00002788 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002789
Chris Lattner566cae92010-03-09 23:52:58 +00002790 Asm->OutStreamer.AddComment("Dwarf Version");
2791 Asm->EmitInt16(dwarf::DWARF_VERSION);
2792 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002793 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002794
Devang Patel32cc43c2010-05-07 20:54:48 +00002795 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002796 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002797
Devang Patel32cc43c2010-05-07 20:54:48 +00002798 const MDNode *Node = *I;
2799 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002800 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002801 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002802 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002803 StringRef LName = SP.getLinkageName();
2804 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002805
Chris Lattner566cae92010-03-09 23:52:58 +00002806 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002807 if (LName.empty()) {
2808 Asm->OutStreamer.EmitBytes(Name, 0);
2809 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002810 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00002811 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2812 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002813
Chris Lattner566cae92010-03-09 23:52:58 +00002814 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00002815 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002816 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002817
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002818 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002819 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002820 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002821 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002822
Chris Lattner7bde8c02010-04-04 18:52:31 +00002823 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002824 Asm->OutStreamer.EmitSymbolValue(LI->first,
2825 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002826 }
2827 }
2828
Chris Lattnera179b522010-04-04 19:25:43 +00002829 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002830}