blob: e9bfff30792a744d2b632b430e801e7716ea46f3 [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,
56 cl::desc("Make an absense of debug location information explicit."),
57 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
76//===----------------------------------------------------------------------===//
Bill Wendling2f921f82009-05-15 09:23:25 +000077/// DbgVariable - This class is used to track local variable information.
78///
Devang Patelf3d7c082009-11-16 21:53:40 +000079class DbgVariable {
Bill Wendling2f921f82009-05-15 09:23:25 +000080 DIVariable Var; // Variable Descriptor.
Devang Patel9fc11702010-05-25 23:40:22 +000081 DIE *TheDIE; // Variable DIE.
82 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
Bill Wendling2f921f82009-05-15 09:23:25 +000083public:
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000084 // AbsVar may be NULL.
Devang Patel9fc11702010-05-25 23:40:22 +000085 DbgVariable(DIVariable V) : Var(V), TheDIE(0), DotDebugLocOffset(~0U) {}
Bill Wendling2f921f82009-05-15 09:23:25 +000086
87 // Accessors.
Devang Patelf6eeaeb2009-11-10 23:06:00 +000088 DIVariable getVariable() const { return Var; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +000089 void setDIE(DIE *D) { TheDIE = D; }
90 DIE *getDIE() const { return TheDIE; }
Devang Patel9fc11702010-05-25 23:40:22 +000091 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
92 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
Devang Patel6d9f9fe2010-08-09 21:01:39 +000093 StringRef getName() const { return Var.getName(); }
94 unsigned getTag() const { return Var.getTag(); }
95 bool variableHasComplexAddress() const {
96 assert(Var.Verify() && "Invalid complex DbgVariable!");
97 return Var.hasComplexAddress();
98 }
99 bool isBlockByrefVariable() const {
100 assert(Var.Verify() && "Invalid complex DbgVariable!");
101 return Var.isBlockByrefVariable();
102 }
103 unsigned getNumAddrElements() const {
104 assert(Var.Verify() && "Invalid complex DbgVariable!");
105 return Var.getNumAddrElements();
106 }
107 uint64_t getAddrElement(unsigned i) const {
108 return Var.getAddrElement(i);
109 }
110 DIType getType() const {
111 DIType Ty = Var.getType();
112 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
113 // addresses instead.
114 if (Var.isBlockByrefVariable()) {
115 /* Byref variables, in Blocks, are declared by the programmer as
116 "SomeType VarName;", but the compiler creates a
117 __Block_byref_x_VarName struct, and gives the variable VarName
118 either the struct, or a pointer to the struct, as its type. This
119 is necessary for various behind-the-scenes things the compiler
120 needs to do with by-reference variables in blocks.
121
122 However, as far as the original *programmer* is concerned, the
123 variable should still have type 'SomeType', as originally declared.
124
125 The following function dives into the __Block_byref_x_VarName
126 struct to find the original type of the variable. This will be
127 passed back to the code generating the type for the Debug
128 Information Entry for the variable 'VarName'. 'VarName' will then
129 have the original type 'SomeType' in its debug information.
130
131 The original type 'SomeType' will be the type of the field named
132 'VarName' inside the __Block_byref_x_VarName struct.
133
134 NOTE: In order for this to not completely fail on the debugger
135 side, the Debug Information Entry for the variable VarName needs to
136 have a DW_AT_location that tells the debugger how to unwind through
137 the pointers and __Block_byref_x_VarName struct to find the actual
138 value of the variable. The function addBlockByrefType does this. */
139 DIType subType = Ty;
140 unsigned tag = Ty.getTag();
141
142 if (tag == dwarf::DW_TAG_pointer_type) {
143 DIDerivedType DTy = DIDerivedType(Ty);
144 subType = DTy.getTypeDerivedFrom();
145 }
146
147 DICompositeType blockStruct = DICompositeType(subType);
148 DIArray Elements = blockStruct.getTypeArray();
149
150 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
151 DIDescriptor Element = Elements.getElement(i);
152 DIDerivedType DT = DIDerivedType(Element);
153 if (getName() == DT.getName())
154 return (DT.getTypeDerivedFrom());
155 }
156 return Ty;
157 }
158 return Ty;
159 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000160};
161
162//===----------------------------------------------------------------------===//
Devang Patel6c74a872010-04-27 19:46:33 +0000163/// DbgRange - This is used to track range of instructions with identical
164/// debug info scope.
165///
166typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
167
168//===----------------------------------------------------------------------===//
Bill Wendling2f921f82009-05-15 09:23:25 +0000169/// DbgScope - This class is used to track scope information.
170///
Devang Patelf3d7c082009-11-16 21:53:40 +0000171class DbgScope {
Bill Wendling2f921f82009-05-15 09:23:25 +0000172 DbgScope *Parent; // Parent to this scope.
Jim Grosbach042483e2009-11-21 23:12:12 +0000173 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel1973df22010-01-26 21:39:14 +0000174 // Location at which this scope is inlined.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000175 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000176 bool AbstractScope; // Abstract Scope
Devang Patel787f94c2009-10-01 18:25:23 +0000177 const MachineInstr *LastInsn; // Last instruction of this scope.
178 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patel6c74a872010-04-27 19:46:33 +0000179 unsigned DFSIn, DFSOut;
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000180 // Scopes defined in scope. Contents not owned.
181 SmallVector<DbgScope *, 4> Scopes;
182 // Variables declared in scope. Contents owned.
183 SmallVector<DbgVariable *, 8> Variables;
Devang Patel6c74a872010-04-27 19:46:33 +0000184 SmallVector<DbgRange, 4> Ranges;
Owen Anderson9becc182009-06-24 22:53:20 +0000185 // Private state for dump()
186 mutable unsigned IndentLevel;
Bill Wendling2f921f82009-05-15 09:23:25 +0000187public:
Devang Patel32cc43c2010-05-07 20:54:48 +0000188 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000189 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patel6c74a872010-04-27 19:46:33 +0000190 LastInsn(0), FirstInsn(0),
191 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000192 virtual ~DbgScope();
193
194 // Accessors.
195 DbgScope *getParent() const { return Parent; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000196 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000197 DIDescriptor getDesc() const { return Desc; }
Devang Patel32cc43c2010-05-07 20:54:48 +0000198 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
199 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000200 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
Devang Patel406798a2010-08-09 18:51:29 +0000201 const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
Devang Patel6c74a872010-04-27 19:46:33 +0000202 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
203
204 /// openInsnRange - This scope covers instruction range starting from MI.
205 void openInsnRange(const MachineInstr *MI) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000206 if (!FirstInsn)
Devang Patel6c74a872010-04-27 19:46:33 +0000207 FirstInsn = MI;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000208
Devang Patel6c74a872010-04-27 19:46:33 +0000209 if (Parent)
210 Parent->openInsnRange(MI);
211 }
212
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000213 /// extendInsnRange - Extend the current instruction range covered by
Devang Patel6c74a872010-04-27 19:46:33 +0000214 /// this scope.
215 void extendInsnRange(const MachineInstr *MI) {
216 assert (FirstInsn && "MI Range is not open!");
217 LastInsn = MI;
218 if (Parent)
219 Parent->extendInsnRange(MI);
220 }
221
222 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
223 /// until now. This is used when a new scope is encountered while walking
224 /// machine instructions.
225 void closeInsnRange(DbgScope *NewScope = NULL) {
226 assert (LastInsn && "Last insn missing!");
227 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000228 FirstInsn = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +0000229 LastInsn = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000230 // If Parent dominates NewScope then do not close Parent's instruction
Devang Patel6c74a872010-04-27 19:46:33 +0000231 // range.
232 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
233 Parent->closeInsnRange(NewScope);
234 }
235
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000236 void setAbstractScope() { AbstractScope = true; }
237 bool isAbstractScope() const { return AbstractScope; }
Devang Patel6c74a872010-04-27 19:46:33 +0000238
239 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
240 unsigned getDFSOut() const { return DFSOut; }
241 void setDFSOut(unsigned O) { DFSOut = O; }
242 unsigned getDFSIn() const { return DFSIn; }
243 void setDFSIn(unsigned I) { DFSIn = I; }
244 bool dominates(const DbgScope *S) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000245 if (S == this)
Devang Patel6c74a872010-04-27 19:46:33 +0000246 return true;
247 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
248 return true;
249 return false;
250 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000251
Devang Patel930143b2009-11-21 02:48:08 +0000252 /// addScope - Add a scope to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000253 ///
Devang Patel930143b2009-11-21 02:48:08 +0000254 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000255
Devang Patel930143b2009-11-21 02:48:08 +0000256 /// addVariable - Add a variable to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000257 ///
Devang Patel930143b2009-11-21 02:48:08 +0000258 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000259
Bill Wendling2f921f82009-05-15 09:23:25 +0000260#ifndef NDEBUG
261 void dump() const;
262#endif
263};
Devang Patel6c74a872010-04-27 19:46:33 +0000264
Chris Lattnerf5d06362010-04-05 04:09:20 +0000265} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000266
267#ifndef NDEBUG
268void DbgScope::dump() const {
David Greenec230cb92009-12-24 00:31:35 +0000269 raw_ostream &err = dbgs();
Chris Lattner81e8e022009-08-23 00:51:00 +0000270 err.indent(IndentLevel);
Devang Patel32cc43c2010-05-07 20:54:48 +0000271 const MDNode *N = Desc;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000272 N->dump();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000273 if (AbstractScope)
274 err << "Abstract Scope\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000275
276 IndentLevel += 2;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000277 if (!Scopes.empty())
278 err << "Children ...\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000279 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
280 if (Scopes[i] != this)
281 Scopes[i]->dump();
282
283 IndentLevel -= 2;
284}
285#endif
286
Bill Wendling2f921f82009-05-15 09:23:25 +0000287DbgScope::~DbgScope() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000288 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
289 delete Variables[j];
Bill Wendling2f921f82009-05-15 09:23:25 +0000290}
291
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000292DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000293 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000294 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel12563b32010-04-16 23:33:45 +0000295 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000296 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000297
Chris Lattnere58b5472010-04-04 23:10:38 +0000298 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
299 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000300 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000301 FunctionBeginSym = FunctionEndSym = 0;
Devang Patel9c160e12010-07-08 20:10:35 +0000302 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Dan Gohman6e681a52010-06-18 15:56:31 +0000303 {
304 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
305 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000306 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000307}
308DwarfDebug::~DwarfDebug() {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000309 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
310 DIEBlocks[j]->~DIEBlock();
Bill Wendling2f921f82009-05-15 09:23:25 +0000311}
312
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000313MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
314 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
315 if (Entry.first) return Entry.first;
316
317 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000318 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000319}
320
321
Devang Patel930143b2009-11-21 02:48:08 +0000322/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000323///
Devang Patel930143b2009-11-21 02:48:08 +0000324void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000325 // Profile the node so that we can make it unique.
326 FoldingSetNodeID ID;
327 Abbrev.Profile(ID);
328
329 // Check the set for priors.
330 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
331
332 // If it's newly added.
333 if (InSet == &Abbrev) {
334 // Add to abbreviation list.
335 Abbreviations.push_back(&Abbrev);
336
337 // Assign the vector position + 1 as its number.
338 Abbrev.setNumber(Abbreviations.size());
339 } else {
340 // Assign existing abbreviation number.
341 Abbrev.setNumber(InSet->getNumber());
342 }
343}
344
Devang Patel930143b2009-11-21 02:48:08 +0000345/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendlingbcad77a2009-05-20 23:24:48 +0000346/// information entry.
Devang Patel930143b2009-11-21 02:48:08 +0000347DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000348 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000349 return Value;
350}
351
Devang Patel930143b2009-11-21 02:48:08 +0000352/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000353///
Devang Patel930143b2009-11-21 02:48:08 +0000354void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000355 unsigned Form, uint64_t Integer) {
356 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000357 DIEValue *Value = Integer == 1 ?
Devang Patel9c160e12010-07-08 20:10:35 +0000358 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000359 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000360}
361
Devang Patel930143b2009-11-21 02:48:08 +0000362/// addSInt - Add an signed integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000363///
Devang Patel930143b2009-11-21 02:48:08 +0000364void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000365 unsigned Form, int64_t Integer) {
366 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000367 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000368 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000369}
370
Devang Patel8c339592009-12-02 15:25:16 +0000371/// addString - Add a string attribute data and value. DIEString only
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000372/// keeps string reference.
Devang Patel930143b2009-11-21 02:48:08 +0000373void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerc3f23b82010-01-23 03:11:46 +0000374 StringRef String) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000375 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel930143b2009-11-21 02:48:08 +0000376 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000377}
378
Devang Patel930143b2009-11-21 02:48:08 +0000379/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000380///
Devang Patel930143b2009-11-21 02:48:08 +0000381void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000382 const MCSymbol *Label) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000383 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel930143b2009-11-21 02:48:08 +0000384 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000385}
386
Devang Patel930143b2009-11-21 02:48:08 +0000387/// addDelta - Add a label delta attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000388///
Devang Patel930143b2009-11-21 02:48:08 +0000389void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000390 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000391 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel930143b2009-11-21 02:48:08 +0000392 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000393}
394
Chris Lattner3f3fb972010-04-05 05:24:55 +0000395/// addDIEEntry - Add a DIE attribute data and value.
396///
397void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
398 DIE *Entry) {
399 Die->addValue(Attribute, Form, createDIEEntry(Entry));
400}
401
402
Devang Patel930143b2009-11-21 02:48:08 +0000403/// addBlock - Add block data.
Bill Wendling2f921f82009-05-15 09:23:25 +0000404///
Devang Patel930143b2009-11-21 02:48:08 +0000405void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000406 DIEBlock *Block) {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000407 Block->ComputeSize(Asm);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000408 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel930143b2009-11-21 02:48:08 +0000409 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000410}
411
Devang Patel930143b2009-11-21 02:48:08 +0000412/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000413/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000414void DwarfDebug::addSourceLine(DIE *Die, DIVariable V) {
Devang Patel0625af22010-05-07 23:33:41 +0000415 // Verify variable.
Devang Patelb6511a32010-08-09 20:20:05 +0000416 if (!V.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000417 return;
418
Devang Patelb6511a32010-08-09 20:20:05 +0000419 unsigned Line = V.getLineNumber();
Devang Pateldd1c2892010-10-08 17:18:54 +0000420 if (Line == 0)
421 return;
Devang Patele01b75c2011-03-24 20:30:50 +0000422 unsigned FileID = GetOrCreateSourceID(V.getContext().getFilename(),
423 V.getContext().getDirectory());
Bill Wendling2f921f82009-05-15 09:23:25 +0000424 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000425 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
426 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000427}
428
Devang Patel930143b2009-11-21 02:48:08 +0000429/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000430/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000431void DwarfDebug::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patel0625af22010-05-07 23:33:41 +0000432 // Verify global variable.
Devang Patelb6511a32010-08-09 20:20:05 +0000433 if (!G.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000434 return;
435
Devang Patelb6511a32010-08-09 20:20:05 +0000436 unsigned Line = G.getLineNumber();
Devang Pateldd1c2892010-10-08 17:18:54 +0000437 if (Line == 0)
438 return;
Devang Patele01b75c2011-03-24 20:30:50 +0000439 unsigned FileID = GetOrCreateSourceID(G.getContext().getFilename(),
440 G.getContext().getDirectory());
Bill Wendling2f921f82009-05-15 09:23:25 +0000441 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000442 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
443 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000444}
Devang Patelb2de5fa2009-08-31 22:47:13 +0000445
Devang Patel930143b2009-11-21 02:48:08 +0000446/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000447/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000448void DwarfDebug::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patel0625af22010-05-07 23:33:41 +0000449 // Verify subprogram.
Devang Patelb6511a32010-08-09 20:20:05 +0000450 if (!SP.Verify())
Devang Patelb2de5fa2009-08-31 22:47:13 +0000451 return;
Caroline Tice183a5192009-09-11 18:25:54 +0000452 // If the line number is 0, don't add it.
Devang Patelb6511a32010-08-09 20:20:05 +0000453 if (SP.getLineNumber() == 0)
Caroline Tice183a5192009-09-11 18:25:54 +0000454 return;
455
Devang Patelb6511a32010-08-09 20:20:05 +0000456 unsigned Line = SP.getLineNumber();
457 if (!SP.getContext().Verify())
Devang Patel8119fe872010-03-08 22:02:50 +0000458 return;
Devang Patele01b75c2011-03-24 20:30:50 +0000459 unsigned FileID = GetOrCreateSourceID(SP.getFilename(), SP.getDirectory());
Devang Patelb2de5fa2009-08-31 22:47:13 +0000460 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000461 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
462 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patelb2de5fa2009-08-31 22:47:13 +0000463}
464
Devang Patel930143b2009-11-21 02:48:08 +0000465/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000466/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000467void DwarfDebug::addSourceLine(DIE *Die, DIType Ty) {
Devang Patel0625af22010-05-07 23:33:41 +0000468 // Verify type.
Devang Patelb6511a32010-08-09 20:20:05 +0000469 if (!Ty.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000470 return;
471
Devang Patelb6511a32010-08-09 20:20:05 +0000472 unsigned Line = Ty.getLineNumber();
Devang Pateldd1c2892010-10-08 17:18:54 +0000473 if (Line == 0 || !Ty.getContext().Verify())
Devang Patel8119fe872010-03-08 22:02:50 +0000474 return;
Devang Patele01b75c2011-03-24 20:30:50 +0000475 unsigned FileID = GetOrCreateSourceID(Ty.getFilename(), Ty.getDirectory());
Bill Wendling2f921f82009-05-15 09:23:25 +0000476 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000477 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
478 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000479}
480
Devang Patel1f4690c2009-12-15 19:16:48 +0000481/// addSourceLine - Add location information to specified debug information
482/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000483void DwarfDebug::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patel0625af22010-05-07 23:33:41 +0000484 // Verify namespace.
Devang Patelb6511a32010-08-09 20:20:05 +0000485 if (!NS.Verify())
Devang Patel1f4690c2009-12-15 19:16:48 +0000486 return;
487
Devang Patelb6511a32010-08-09 20:20:05 +0000488 unsigned Line = NS.getLineNumber();
Devang Pateldd1c2892010-10-08 17:18:54 +0000489 if (Line == 0)
490 return;
Devang Patelb6511a32010-08-09 20:20:05 +0000491 StringRef FN = NS.getFilename();
Devang Patel1f4690c2009-12-15 19:16:48 +0000492
Devang Patele01b75c2011-03-24 20:30:50 +0000493 unsigned FileID = GetOrCreateSourceID(FN, NS.getDirectory());
Devang Patel1f4690c2009-12-15 19:16:48 +0000494 assert(FileID && "Invalid file id");
495 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
496 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
497}
498
Devang Patel2cfc3af2010-08-31 06:11:28 +0000499/// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
500/// on provided frame index.
501void DwarfDebug::addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI) {
502 MachineLocation Location;
503 unsigned FrameReg;
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000504 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Anton Korobeynikov46877782010-11-20 15:59:32 +0000505 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel2cfc3af2010-08-31 06:11:28 +0000506 Location.set(FrameReg, Offset);
507
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000508 if (DV->variableHasComplexAddress())
509 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
510 else if (DV->isBlockByrefVariable())
511 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
512 else
513 addAddress(Die, dwarf::DW_AT_location, Location);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000514}
515
Devang Patel930143b2009-11-21 02:48:08 +0000516/// addComplexAddress - Start with the address based on the location provided,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000517/// and generate the DWARF information necessary to find the actual variable
518/// given the extra address information encoded in the DIVariable, starting from
519/// the starting location. Add the DWARF information to the die.
520///
Devang Patel930143b2009-11-21 02:48:08 +0000521void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000522 unsigned Attribute,
523 const MachineLocation &Location) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000524 DIType Ty = DV->getType();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000525
526 // Decode the original location, and use that as the start of the byref
527 // variable's location.
Chris Lattner3a383cb2010-04-05 00:13:49 +0000528 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000529 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000530 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000531
532 if (Location.isReg()) {
533 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000534 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000535 } else {
Devang Patel8698f092011-01-19 23:04:47 +0000536 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
Devang Patel930143b2009-11-21 02:48:08 +0000537 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000538 }
539 } else {
540 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000541 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000542 else {
Devang Patel930143b2009-11-21 02:48:08 +0000543 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
544 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000545 }
546
Devang Patel930143b2009-11-21 02:48:08 +0000547 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump14cf8ec2009-09-30 00:08:22 +0000548 }
549
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000550 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
551 uint64_t Element = DV->getAddrElement(i);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000552
Devang Patela5d93242011-02-24 18:49:30 +0000553 if (Element == DIBuilder::OpPlus) {
Devang Patel930143b2009-11-21 02:48:08 +0000554 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000555 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
Devang Patela5d93242011-02-24 18:49:30 +0000556 } else if (Element == DIBuilder::OpDeref) {
Devang Patel930143b2009-11-21 02:48:08 +0000557 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patela5d93242011-02-24 18:49:30 +0000558 } else llvm_unreachable("unknown DIBuilder Opcode");
Mike Stump14cf8ec2009-09-30 00:08:22 +0000559 }
560
561 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000562 addBlock(Die, Attribute, 0, Block);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000563}
564
Caroline Ticec87c1e22009-08-31 21:19:37 +0000565/* Byref variables, in Blocks, are declared by the programmer as "SomeType
566 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
567 gives the variable VarName either the struct, or a pointer to the struct, as
568 its type. This is necessary for various behind-the-scenes things the
569 compiler needs to do with by-reference variables in Blocks.
570
571 However, as far as the original *programmer* is concerned, the variable
572 should still have type 'SomeType', as originally declared.
573
Devang Patel930143b2009-11-21 02:48:08 +0000574 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000575 struct to find the original type of the variable, which is then assigned to
576 the variable's Debug Information Entry as its real type. So far, so good.
577 However now the debugger will expect the variable VarName to have the type
578 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000579 expression that explains to the debugger how to navigate through the
Caroline Ticec87c1e22009-08-31 21:19:37 +0000580 pointers and struct to find the actual variable of type SomeType.
581
582 The following function does just that. We start by getting
583 the "normal" location for the variable. This will be the location
584 of either the struct __Block_byref_x_VarName or the pointer to the
585 struct __Block_byref_x_VarName.
586
587 The struct will look something like:
588
589 struct __Block_byref_x_VarName {
590 ... <various fields>
591 struct __Block_byref_x_VarName *forwarding;
592 ... <various other fields>
593 SomeType VarName;
594 ... <maybe more fields>
595 };
596
597 If we are given the struct directly (as our starting point) we
598 need to tell the debugger to:
599
600 1). Add the offset of the forwarding field.
601
Dan Gohman4a618822010-02-10 16:03:48 +0000602 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000603 struct to use (the real one may have been copied onto the heap).
604
605 3). Add the offset for the field VarName, to find the actual variable.
606
607 If we started with a pointer to the struct, then we need to
608 dereference that pointer first, before the other steps.
609 Translating this into DWARF ops, we will need to append the following
610 to the current location description for the variable:
611
612 DW_OP_deref -- optional, if we start with a pointer
613 DW_OP_plus_uconst <forward_fld_offset>
614 DW_OP_deref
615 DW_OP_plus_uconst <varName_fld_offset>
616
617 That is what this function does. */
618
Devang Patel930143b2009-11-21 02:48:08 +0000619/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticec87c1e22009-08-31 21:19:37 +0000620/// provided, and generate the DWARF information necessary to find the
621/// actual Block variable (navigating the Block struct) based on the
622/// starting location. Add the DWARF information to the die. For
623/// more information, read large comment just above here.
624///
Devang Patel930143b2009-11-21 02:48:08 +0000625void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000626 unsigned Attribute,
627 const MachineLocation &Location) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000628 DIType Ty = DV->getType();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000629 DIType TmpTy = Ty;
630 unsigned Tag = Ty.getTag();
631 bool isPointer = false;
632
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000633 StringRef varName = DV->getName();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000634
635 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000636 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000637 TmpTy = DTy.getTypeDerivedFrom();
638 isPointer = true;
639 }
640
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000641 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000642
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000643 // Find the __forwarding field and the variable field in the __Block_byref
644 // struct.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000645 DIArray Fields = blockStruct.getTypeArray();
646 DIDescriptor varField = DIDescriptor();
647 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000648
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000649 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
650 DIDescriptor Element = Fields.getElement(i);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000651 DIDerivedType DT = DIDerivedType(Element);
Devang Patel2d9caf92009-11-25 17:36:49 +0000652 StringRef fieldName = DT.getName();
653 if (fieldName == "__forwarding")
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000654 forwardingField = Element;
Devang Patel2d9caf92009-11-25 17:36:49 +0000655 else if (fieldName == varName)
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000656 varField = Element;
657 }
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000658
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000659 // Get the offsets for the forwarding field and the variable field.
Chris Lattner71696ef2010-03-31 06:06:37 +0000660 unsigned forwardingFieldOffset =
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000661 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner71696ef2010-03-31 06:06:37 +0000662 unsigned varFieldOffset =
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000663 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticec87c1e22009-08-31 21:19:37 +0000664
Mike Stump944fa252009-09-24 23:21:26 +0000665 // Decode the original location, and use that as the start of the byref
666 // variable's location.
Chris Lattner3a383cb2010-04-05 00:13:49 +0000667 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000668 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000669 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000670
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000671 if (Location.isReg()) {
672 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000673 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000674 else {
Devang Patel8698f092011-01-19 23:04:47 +0000675 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
Devang Patel930143b2009-11-21 02:48:08 +0000676 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000677 }
678 } else {
679 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000680 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000681 else {
Devang Patel930143b2009-11-21 02:48:08 +0000682 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
683 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000684 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000685
Devang Patel930143b2009-11-21 02:48:08 +0000686 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000687 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000688
Mike Stump944fa252009-09-24 23:21:26 +0000689 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000690 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000691 if (isPointer)
Devang Patel930143b2009-11-21 02:48:08 +0000692 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000693
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000694 // Next add the offset for the '__forwarding' field:
695 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
696 // adding the offset if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000697 if (forwardingFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000698 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
699 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000700 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000701
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000702 // Now dereference the __forwarding field to get to the real __Block_byref
703 // struct: DW_OP_deref.
Devang Patel930143b2009-11-21 02:48:08 +0000704 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000705
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000706 // Now that we've got the real __Block_byref... struct, add the offset
707 // for the variable's field to get to the location of the actual variable:
708 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000709 if (varFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000710 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
711 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000712 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000713
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000714 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000715 addBlock(Die, Attribute, 0, Block);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000716}
717
Devang Patel930143b2009-11-21 02:48:08 +0000718/// addAddress - Add an address attribute to a die based on the location
Bill Wendling2f921f82009-05-15 09:23:25 +0000719/// provided.
Devang Patel930143b2009-11-21 02:48:08 +0000720void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000721 const MachineLocation &Location) {
Chris Lattner3a383cb2010-04-05 00:13:49 +0000722 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling2f921f82009-05-15 09:23:25 +0000723 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000724 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel804fcd42010-09-22 21:10:38 +0000725
Devang Patele7559662010-11-02 17:37:00 +0000726 if (RI->getFrameRegister(*Asm->MF) == Location.getReg()
Devang Patel804fcd42010-09-22 21:10:38 +0000727 && Location.getOffset()) {
728 // If variable offset is based in frame register then use fbreg.
729 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
730 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
731 addBlock(Die, Attribute, 0, Block);
732 return;
733 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000734
735 if (Location.isReg()) {
736 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000737 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000738 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000739 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
740 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000741 }
742 } else {
743 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000744 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000745 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000746 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
747 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000748 }
749
Devang Patel930143b2009-11-21 02:48:08 +0000750 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling2f921f82009-05-15 09:23:25 +0000751 }
752
Devang Patel930143b2009-11-21 02:48:08 +0000753 addBlock(Die, Attribute, 0, Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000754}
755
Devang Patel173b2b92010-04-28 01:03:09 +0000756/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patel53a40df62010-11-12 23:20:42 +0000757bool DwarfDebug::addRegisterAddress(DIE *Die, const MachineOperand &MO) {
Devang Patel173b2b92010-04-28 01:03:09 +0000758 assert (MO.isReg() && "Invalid machine operand!");
759 if (!MO.getReg())
760 return false;
761 MachineLocation Location;
762 Location.set(MO.getReg());
763 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patel173b2b92010-04-28 01:03:09 +0000764 return true;
765}
766
767/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel53a40df62010-11-12 23:20:42 +0000768bool DwarfDebug::addConstantValue(DIE *Die, const MachineOperand &MO) {
Devang Patel173b2b92010-04-28 01:03:09 +0000769 assert (MO.isImm() && "Invalid machine operand!");
770 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
771 unsigned Imm = MO.getImm();
772 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
773 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel173b2b92010-04-28 01:03:09 +0000774 return true;
775}
776
777/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patel53a40df62010-11-12 23:20:42 +0000778bool DwarfDebug::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Devang Patel173b2b92010-04-28 01:03:09 +0000779 assert (MO.isFPImm() && "Invalid machine operand!");
780 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
781 APFloat FPImm = MO.getFPImm()->getValueAPF();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000782
Devang Patel173b2b92010-04-28 01:03:09 +0000783 // Get the raw data form of the floating point.
784 const APInt FltVal = FPImm.bitcastToAPInt();
785 const char *FltPtr = (const char*)FltVal.getRawData();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000786
Devang Patel173b2b92010-04-28 01:03:09 +0000787 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
788 bool LittleEndian = Asm->getTargetData().isLittleEndian();
789 int Incr = (LittleEndian ? 1 : -1);
790 int Start = (LittleEndian ? 0 : NumBytes - 1);
791 int Stop = (LittleEndian ? NumBytes : -1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000792
Devang Patel173b2b92010-04-28 01:03:09 +0000793 // Output the constant to DWARF one byte at a time.
794 for (; Start != Stop; Start += Incr)
795 addUInt(Block, 0, dwarf::DW_FORM_data1,
796 (unsigned char)0xFF & FltPtr[Start]);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000797
Devang Patel173b2b92010-04-28 01:03:09 +0000798 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000799 return true;
Devang Patel173b2b92010-04-28 01:03:09 +0000800}
801
Devang Patel70eb9822011-01-06 21:39:25 +0000802/// addConstantValue - Add constant value entry in variable DIE.
803bool DwarfDebug::addConstantValue(DIE *Die, ConstantInt *CI,
804 bool Unsigned) {
805 if (CI->getBitWidth() <= 64) {
806 if (Unsigned)
807 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
808 CI->getZExtValue());
809 else
810 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
811 CI->getSExtValue());
812 return true;
813 }
814
815 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
816
817 // Get the raw data form of the large APInt.
818 const APInt Val = CI->getValue();
819 const char *Ptr = (const char*)Val.getRawData();
820
821 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
822 bool LittleEndian = Asm->getTargetData().isLittleEndian();
823 int Incr = (LittleEndian ? 1 : -1);
824 int Start = (LittleEndian ? 0 : NumBytes - 1);
825 int Stop = (LittleEndian ? NumBytes : -1);
826
827 // Output the constant to DWARF one byte at a time.
828 for (; Start != Stop; Start += Incr)
829 addUInt(Block, 0, dwarf::DW_FORM_data1,
830 (unsigned char)0xFF & Ptr[Start]);
831
832 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
833 return true;
834}
Devang Patel173b2b92010-04-28 01:03:09 +0000835
Devang Pateld4e20ea2011-04-05 21:08:24 +0000836/// addTemplateParams - Add template parameters in buffer.
837void DwarfDebug::addTemplateParams(DIE &Buffer, DIArray TParams) {
838 // Add template parameters.
839 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
840 DIDescriptor Element = TParams.getElement(i);
841 if (Element.isTemplateTypeParameter())
842 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
843 DITemplateTypeParameter(Element)));
844 else if (Element.isTemplateValueParameter())
845 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
846 DITemplateValueParameter(Element)));
847 }
848
849}
Devang Patel2b75ed22009-12-10 19:14:49 +0000850/// addToContextOwner - Add Die into the list of its context owner's children.
851void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000852 if (Context.isType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000853 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patel2b75ed22009-12-10 19:14:49 +0000854 ContextDIE->addChild(Die);
Devang Patel1f4690c2009-12-15 19:16:48 +0000855 } else if (Context.isNameSpace()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000856 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel1f4690c2009-12-15 19:16:48 +0000857 ContextDIE->addChild(Die);
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000858 } else if (Context.isSubprogram()) {
Devang Patel185051c2010-09-27 23:15:27 +0000859 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context));
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000860 ContextDIE->addChild(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000861 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patel2b75ed22009-12-10 19:14:49 +0000862 ContextDIE->addChild(Die);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000863 else
Devang Patel1a0df9a2010-05-10 22:49:55 +0000864 getCompileUnit(Context)->addDie(Die);
Devang Patel2b75ed22009-12-10 19:14:49 +0000865}
866
Devang Patelb5b60ea2009-12-10 18:05:33 +0000867/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
868/// given DIType.
869DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000870 CompileUnit *TypeCU = getCompileUnit(Ty);
871 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patelb5b60ea2009-12-10 18:05:33 +0000872 if (TyDIE)
873 return TyDIE;
874
875 // Create new type.
876 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000877 TypeCU->insertDIE(Ty, TyDIE);
Devang Patelb5b60ea2009-12-10 18:05:33 +0000878 if (Ty.isBasicType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000879 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000880 else if (Ty.isCompositeType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000881 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000882 else {
883 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000884 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000885 }
886
Devang Patel2b75ed22009-12-10 19:14:49 +0000887 addToContextOwner(TyDIE, Ty.getContext());
Devang Patelb5b60ea2009-12-10 18:05:33 +0000888 return TyDIE;
889}
890
Devang Patel930143b2009-11-21 02:48:08 +0000891/// addType - Add a new type attribute to the specified entity.
Devang Patel9ccfb642009-12-09 18:24:21 +0000892void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel8d6a2b72010-05-07 21:45:47 +0000893 if (!Ty.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000894 return;
895
896 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000897 CompileUnit *TypeCU = getCompileUnit(Ty);
898 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling2f921f82009-05-15 09:23:25 +0000899 // If it exists then use the existing value.
Devang Patel930143b2009-11-21 02:48:08 +0000900 if (Entry) {
901 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000902 return;
903 }
904
Bill Wendling2f921f82009-05-15 09:23:25 +0000905 // Construct type.
Devang Patelb5b60ea2009-12-10 18:05:33 +0000906 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling2f921f82009-05-15 09:23:25 +0000907
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000908 // Set up proxy.
909 Entry = createDIEEntry(Buffer);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000910 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000911
Devang Patel930143b2009-11-21 02:48:08 +0000912 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000913}
914
Devang Patel930143b2009-11-21 02:48:08 +0000915/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000916void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000917 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000918 StringRef Name = BTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000919 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +0000920 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling2f921f82009-05-15 09:23:25 +0000921 BTy.getEncoding());
922
923 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +0000924 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000925 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000926 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel930143b2009-11-21 02:48:08 +0000927 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000928}
929
Devang Patel930143b2009-11-21 02:48:08 +0000930/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000931void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000932 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000933 StringRef Name = DTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000934 uint64_t Size = DTy.getSizeInBits() >> 3;
935 unsigned Tag = DTy.getTag();
936
937 // FIXME - Workaround for templates.
938 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
939
940 Buffer.setTag(Tag);
941
942 // Map to main type, void will not have a type.
943 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel9ccfb642009-12-09 18:24:21 +0000944 addType(&Buffer, FromTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000945
946 // Add name if not anonymous or intermediate type.
Devang Patelae466ef2009-11-30 23:56:56 +0000947 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000948 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000949
950 // Add size if non-zero (derived types might be zero-sized.)
951 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +0000952 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000953
954 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb5b51592009-11-23 18:43:37 +0000955 if (!DTy.isForwardDecl())
Devang Patelb6511a32010-08-09 20:20:05 +0000956 addSourceLine(&Buffer, DTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000957}
958
Devang Patel930143b2009-11-21 02:48:08 +0000959/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000960void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000961 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000962 StringRef Name = CTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000963
964 uint64_t Size = CTy.getSizeInBits() >> 3;
965 unsigned Tag = CTy.getTag();
966 Buffer.setTag(Tag);
967
968 switch (Tag) {
969 case dwarf::DW_TAG_vector_type:
970 case dwarf::DW_TAG_array_type:
Devang Patel9ccfb642009-12-09 18:24:21 +0000971 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000972 break;
973 case dwarf::DW_TAG_enumeration_type: {
974 DIArray Elements = CTy.getTypeArray();
975
976 // Add enumerators to enumeration type.
977 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
978 DIE *ElemDie = NULL;
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000979 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3b548aa2010-03-08 20:52:55 +0000980 if (Enum.isEnumerator()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000981 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel930143b2009-11-21 02:48:08 +0000982 Buffer.addChild(ElemDie);
Devang Patelfafa1fe2009-10-09 17:51:49 +0000983 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000984 }
985 }
986 break;
987 case dwarf::DW_TAG_subroutine_type: {
988 // Add return type.
989 DIArray Elements = CTy.getTypeArray();
990 DIDescriptor RTy = Elements.getElement(0);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000991 addType(&Buffer, DIType(RTy));
Bill Wendling2f921f82009-05-15 09:23:25 +0000992
Devang Patel9a33ec22010-10-06 20:50:40 +0000993 bool isPrototyped = true;
Bill Wendling2f921f82009-05-15 09:23:25 +0000994 // Add arguments.
995 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000996 DIDescriptor Ty = Elements.getElement(i);
Devang Patel9a33ec22010-10-06 20:50:40 +0000997 if (Ty.isUnspecifiedParameter()) {
998 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
999 Buffer.addChild(Arg);
1000 isPrototyped = false;
1001 } else {
1002 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1003 addType(Arg, DIType(Ty));
1004 Buffer.addChild(Arg);
1005 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001006 }
Devang Patel9a33ec22010-10-06 20:50:40 +00001007 // Add prototype flag.
1008 if (isPrototyped)
1009 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001010 }
1011 break;
1012 case dwarf::DW_TAG_structure_type:
1013 case dwarf::DW_TAG_union_type:
1014 case dwarf::DW_TAG_class_type: {
1015 // Add elements to structure type.
1016 DIArray Elements = CTy.getTypeArray();
1017
1018 // A forward struct declared type may not have elements available.
Devang Patel3b548aa2010-03-08 20:52:55 +00001019 unsigned N = Elements.getNumElements();
1020 if (N == 0)
Bill Wendling2f921f82009-05-15 09:23:25 +00001021 break;
1022
1023 // Add elements to structure type.
Devang Patel3b548aa2010-03-08 20:52:55 +00001024 for (unsigned i = 0; i < N; ++i) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001025 DIDescriptor Element = Elements.getElement(i);
1026 DIE *ElemDie = NULL;
Devang Patelcb03b142010-09-29 21:44:16 +00001027 if (Element.isSubprogram()) {
1028 DISubprogram SP(Element);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001029 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patelcb03b142010-09-29 21:44:16 +00001030 if (SP.isProtected())
1031 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1032 dwarf::DW_ACCESS_protected);
1033 else if (SP.isPrivate())
1034 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1035 dwarf::DW_ACCESS_private);
1036 else
1037 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1038 dwarf::DW_ACCESS_public);
Devang Patele1c71462010-10-01 23:31:40 +00001039 if (SP.isExplicit())
1040 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
Devang Patelcb03b142010-09-29 21:44:16 +00001041 }
Devang Patel3b548aa2010-03-08 20:52:55 +00001042 else if (Element.isVariable()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001043 DIVariable DV(Element);
Devang Patel160c92d2010-01-30 01:08:30 +00001044 ElemDie = new DIE(dwarf::DW_TAG_variable);
1045 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1046 DV.getName());
1047 addType(ElemDie, DV.getType());
1048 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1049 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patelb6511a32010-08-09 20:20:05 +00001050 addSourceLine(ElemDie, DV);
Devang Patel3b548aa2010-03-08 20:52:55 +00001051 } else if (Element.isDerivedType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001052 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3b548aa2010-03-08 20:52:55 +00001053 else
1054 continue;
Devang Patel930143b2009-11-21 02:48:08 +00001055 Buffer.addChild(ElemDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001056 }
1057
Devang Patel3082c012009-08-27 23:51:51 +00001058 if (CTy.isAppleBlockExtension())
Devang Patel930143b2009-11-21 02:48:08 +00001059 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001060
1061 unsigned RLang = CTy.getRunTimeLang();
1062 if (RLang)
Devang Patel930143b2009-11-21 02:48:08 +00001063 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling2f921f82009-05-15 09:23:25 +00001064 dwarf::DW_FORM_data1, RLang);
Devang Patel303a1be2010-01-26 21:16:06 +00001065
1066 DICompositeType ContainingType = CTy.getContainingType();
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001067 if (DIDescriptor(ContainingType).isCompositeType())
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001068 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001069 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastingsafe54f12010-06-11 20:08:44 +00001070 else {
1071 DIDescriptor Context = CTy.getContext();
1072 addToContextOwner(&Buffer, Context);
1073 }
Devang Patel3a9e65e2011-02-02 21:38:25 +00001074
Devang Pateld4e20ea2011-04-05 21:08:24 +00001075 if (Tag == dwarf::DW_TAG_class_type)
1076 addTemplateParams(Buffer, CTy.getTemplateParams());
1077
Bill Wendling2f921f82009-05-15 09:23:25 +00001078 break;
1079 }
1080 default:
1081 break;
1082 }
1083
1084 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +00001085 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001086 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +00001087
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001088 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
Devang Patel30265c42010-07-07 20:12:52 +00001089 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1090 {
Bill Wendling2f921f82009-05-15 09:23:25 +00001091 // Add size if non-zero (derived types might be zero-sized.)
1092 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +00001093 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +00001094 else {
1095 // Add zero size if it is not a forward declaration.
1096 if (CTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +00001097 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001098 else
Devang Patel930143b2009-11-21 02:48:08 +00001099 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling2f921f82009-05-15 09:23:25 +00001100 }
1101
1102 // Add source line info if available.
1103 if (!CTy.isForwardDecl())
Devang Patelb6511a32010-08-09 20:20:05 +00001104 addSourceLine(&Buffer, CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001105 }
1106}
1107
Devang Patel3a9e65e2011-02-02 21:38:25 +00001108/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
1109/// for the given DITemplateTypeParameter.
1110DIE *
1111DwarfDebug::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1112 CompileUnit *TypeCU = getCompileUnit(TP);
1113 DIE *ParamDIE = TypeCU->getDIE(TP);
1114 if (ParamDIE)
1115 return ParamDIE;
1116
1117 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1118 addType(ParamDIE, TP.getType());
1119 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TP.getName());
1120 return ParamDIE;
1121}
1122
Devang Patelbe933b42011-02-02 22:35:53 +00001123/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
1124/// for the given DITemplateValueParameter.
1125DIE *
1126DwarfDebug::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV) {
1127 CompileUnit *TVCU = getCompileUnit(TPV);
1128 DIE *ParamDIE = TVCU->getDIE(TPV);
1129 if (ParamDIE)
1130 return ParamDIE;
1131
1132 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1133 addType(ParamDIE, TPV.getType());
Devang Patel651d06e2011-04-05 20:14:13 +00001134 if (!TPV.getName().empty())
1135 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TPV.getName());
Devang Patelbe933b42011-02-02 22:35:53 +00001136 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
1137 TPV.getValue());
1138 return ParamDIE;
1139}
1140
Devang Patel930143b2009-11-21 02:48:08 +00001141/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1142void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Devang Patele3964792011-04-08 21:55:10 +00001143 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1144 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001145 int64_t L = SR.getLo();
1146 int64_t H = SR.getHi();
Bill Wendling2f921f82009-05-15 09:23:25 +00001147
Devang Patel778947c2011-04-08 23:39:38 +00001148 // The L value defines the lower bounds which is typically zero for C/C++. The
1149 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size
1150 // of the array. If L > H then do not emit DW_AT_lower_bound and
1151 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the
1152 // array has one element and in such case do not emit lower bound.
Devang Patele3964792011-04-08 21:55:10 +00001153
Devang Patel778947c2011-04-08 23:39:38 +00001154 if (L > H) {
Devang Patele3964792011-04-08 21:55:10 +00001155 Buffer.addChild(DW_Subrange);
1156 return;
1157 }
Devang Patelf691df32009-08-14 20:59:16 +00001158 if (L)
Devang Patel930143b2009-11-21 02:48:08 +00001159 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patel8f046022009-12-04 23:10:24 +00001160 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Devang Patel930143b2009-11-21 02:48:08 +00001161 Buffer.addChild(DW_Subrange);
Bill Wendling2f921f82009-05-15 09:23:25 +00001162}
1163
Devang Patel930143b2009-11-21 02:48:08 +00001164/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +00001165void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling2f921f82009-05-15 09:23:25 +00001166 DICompositeType *CTy) {
1167 Buffer.setTag(dwarf::DW_TAG_array_type);
1168 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel930143b2009-11-21 02:48:08 +00001169 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001170
1171 // Emit derived type.
Devang Patel9ccfb642009-12-09 18:24:21 +00001172 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001173 DIArray Elements = CTy->getTypeArray();
1174
Devang Patel92e8c652009-11-21 00:31:03 +00001175 // Get an anonymous type for index type.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001176 CompileUnit *TheCU = getCompileUnit(*CTy);
1177 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel92e8c652009-11-21 00:31:03 +00001178 if (!IdxTy) {
1179 // Construct an anonymous type for index type.
1180 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +00001181 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1182 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel92e8c652009-11-21 00:31:03 +00001183 dwarf::DW_ATE_signed);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001184 TheCU->addDie(IdxTy);
1185 TheCU->setIndexTyDie(IdxTy);
Devang Patel92e8c652009-11-21 00:31:03 +00001186 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001187
1188 // Add subranges to array type.
1189 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1190 DIDescriptor Element = Elements.getElement(i);
1191 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001192 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001193 }
1194}
1195
Devang Patel930143b2009-11-21 02:48:08 +00001196/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3b548aa2010-03-08 20:52:55 +00001197DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001198 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3b548aa2010-03-08 20:52:55 +00001199 StringRef Name = ETy.getName();
Devang Patel930143b2009-11-21 02:48:08 +00001200 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3b548aa2010-03-08 20:52:55 +00001201 int64_t Value = ETy.getEnumValue();
Devang Patel930143b2009-11-21 02:48:08 +00001202 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +00001203 return Enumerator;
1204}
1205
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001206/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-01-05 01:46:14 +00001207/// printer to not emit usual symbol prefix before the symbol name is used then
1208/// return linkage name after skipping this special LLVM prefix.
1209static StringRef getRealLinkageName(StringRef LinkageName) {
1210 char One = '\1';
1211 if (LinkageName.startswith(StringRef(&One, 1)))
1212 return LinkageName.substr(1);
1213 return LinkageName;
1214}
1215
Devang Patel930143b2009-11-21 02:48:08 +00001216/// createMemberDIE - Create new member DIE.
Devang Patel18ba0b42010-08-10 04:12:17 +00001217DIE *DwarfDebug::createMemberDIE(DIDerivedType DT) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001218 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel2d9caf92009-11-25 17:36:49 +00001219 StringRef Name = DT.getName();
1220 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001221 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001222
Devang Patel9ccfb642009-12-09 18:24:21 +00001223 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001224
Devang Patelb6511a32010-08-09 20:20:05 +00001225 addSourceLine(MemberDie, DT);
Bill Wendling2f921f82009-05-15 09:23:25 +00001226
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001227 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel930143b2009-11-21 02:48:08 +00001228 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel67f56f02009-11-04 22:06:12 +00001229
Bill Wendling2f921f82009-05-15 09:23:25 +00001230 uint64_t Size = DT.getSizeInBits();
Devang Patelf05d5722009-11-04 23:48:00 +00001231 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling2f921f82009-05-15 09:23:25 +00001232
1233 if (Size != FieldSize) {
1234 // Handle bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001235 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1236 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling2f921f82009-05-15 09:23:25 +00001237
1238 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling2f921f82009-05-15 09:23:25 +00001239 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1240 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer2b459982010-01-07 17:50:57 +00001241 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling2f921f82009-05-15 09:23:25 +00001242 Offset -= FieldOffset;
1243
1244 // Maybe we need to work from the other end.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001245 if (Asm->getTargetData().isLittleEndian())
1246 Offset = FieldSize - (Offset + Size);
Devang Patel930143b2009-11-21 02:48:08 +00001247 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling2f921f82009-05-15 09:23:25 +00001248
Devang Patel67f56f02009-11-04 22:06:12 +00001249 // Here WD_AT_data_member_location points to the anonymous
1250 // field that includes this bit field.
Devang Patel930143b2009-11-21 02:48:08 +00001251 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001252
1253 } else
1254 // This is not a bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001255 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001256
Devang Pateld2316892010-02-03 20:08:48 +00001257 if (DT.getTag() == dwarf::DW_TAG_inheritance
1258 && DT.isVirtual()) {
1259
1260 // For C++, virtual base classes are not at fixed offset. Use following
1261 // expression to extract appropriate offset from vtable.
1262 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1263
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001264 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Pateld2316892010-02-03 20:08:48 +00001265 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1266 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1267 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1268 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1269 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1270 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1271 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1272
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001273 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
Devang Pateld2316892010-02-03 20:08:48 +00001274 VBaseLocationDie);
1275 } else
1276 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001277
1278 if (DT.isProtected())
Devang Pateleb57c592009-12-03 19:11:07 +00001279 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001280 dwarf::DW_ACCESS_protected);
1281 else if (DT.isPrivate())
Devang Pateleb57c592009-12-03 19:11:07 +00001282 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001283 dwarf::DW_ACCESS_private);
Devang Patela1bd5a12010-09-29 19:08:08 +00001284 // Otherwise C++ member and base classes are considered public.
1285 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
Devang Pateleb57c592009-12-03 19:11:07 +00001286 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1287 dwarf::DW_ACCESS_public);
1288 if (DT.isVirtual())
1289 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1290 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling2f921f82009-05-15 09:23:25 +00001291 return MemberDie;
1292}
1293
Devang Patel525dda02009-12-14 16:18:45 +00001294/// createSubprogramDIE - Create new DIE using SP.
Devang Patel185051c2010-09-27 23:15:27 +00001295DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001296 CompileUnit *SPCU = getCompileUnit(SP);
1297 DIE *SPDie = SPCU->getDIE(SP);
Devang Patel525dda02009-12-14 16:18:45 +00001298 if (SPDie)
1299 return SPDie;
1300
1301 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf200b392010-03-02 17:58:15 +00001302 // Constructors and operators for anonymous aggregates do not have names.
Devang Pateld0fa3042010-03-02 01:26:20 +00001303 if (!SP.getName().empty())
1304 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling2f921f82009-05-15 09:23:25 +00001305
Devang Patel2d9caf92009-11-25 17:36:49 +00001306 StringRef LinkageName = SP.getLinkageName();
Devang Patel43ef34d2010-01-05 01:46:14 +00001307 if (!LinkageName.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001308 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel43ef34d2010-01-05 01:46:14 +00001309 getRealLinkageName(LinkageName));
1310
Devang Patelb6511a32010-08-09 20:20:05 +00001311 addSourceLine(SPDie, SP);
Bill Wendling2f921f82009-05-15 09:23:25 +00001312
Devang Patel3a24f922010-10-07 22:03:01 +00001313 if (SP.isPrototyped())
Devang Patel930143b2009-11-21 02:48:08 +00001314 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001315
1316 // Add Return Type.
Devang Patel236526d2009-12-03 01:25:38 +00001317 DICompositeType SPTy = SP.getType();
1318 DIArray Args = SPTy.getTypeArray();
Bill Wendling2f921f82009-05-15 09:23:25 +00001319 unsigned SPTag = SPTy.getTag();
Devang Pateleb57c592009-12-03 19:11:07 +00001320
Devang Patel3b548aa2010-03-08 20:52:55 +00001321 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel9ccfb642009-12-09 18:24:21 +00001322 addType(SPDie, SPTy);
Devang Patel236526d2009-12-03 01:25:38 +00001323 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001324 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel236526d2009-12-03 01:25:38 +00001325
Devang Pateleb57c592009-12-03 19:11:07 +00001326 unsigned VK = SP.getVirtuality();
1327 if (VK) {
1328 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001329 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Pateleb57c592009-12-03 19:11:07 +00001330 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Devang Patelc26da902010-12-09 00:10:40 +00001331 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
Devang Pateleb57c592009-12-03 19:11:07 +00001332 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001333 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001334 SP.getContainingType()));
Devang Pateleb57c592009-12-03 19:11:07 +00001335 }
1336
Devang Patel185051c2010-09-27 23:15:27 +00001337 if (!SP.isDefinition()) {
Devang Patel930143b2009-11-21 02:48:08 +00001338 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001339
1340 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel236526d2009-12-03 01:25:38 +00001341 // be handled while processing variables.
1342 DICompositeType SPTy = SP.getType();
1343 DIArray Args = SPTy.getTypeArray();
1344 unsigned SPTag = SPTy.getTag();
1345
Bill Wendling2f921f82009-05-15 09:23:25 +00001346 if (SPTag == dwarf::DW_TAG_subroutine_type)
1347 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1348 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001349 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patel6efc8e52010-02-06 01:02:37 +00001350 addType(Arg, ATy);
1351 if (ATy.isArtificial())
1352 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel930143b2009-11-21 02:48:08 +00001353 SPDie->addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +00001354 }
1355 }
1356
Devang Patel999b4992010-02-03 19:57:19 +00001357 if (SP.isArtificial())
1358 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1359
Devang Patelb4e3b902010-04-30 19:38:23 +00001360 if (!SP.isLocalToUnit())
1361 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001362
Devang Patelb4e3b902010-04-30 19:38:23 +00001363 if (SP.isOptimized())
1364 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1365
Jim Grosbach965a73a2010-07-21 23:03:52 +00001366 if (unsigned isa = Asm->getISAEncoding()) {
1367 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1368 }
1369
Devang Patel9f738842011-04-05 22:52:06 +00001370 // Add function template parameters.
1371 addTemplateParams(*SPDie, SP.getTemplateParams());
1372
Devang Patelb4e3b902010-04-30 19:38:23 +00001373 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001374 SPCU->insertDIE(SP, SPDie);
Devang Patelb4e3b902010-04-30 19:38:23 +00001375
Stuart Hastingsafe54f12010-06-11 20:08:44 +00001376 // Add to context owner.
1377 addToContextOwner(SPDie, SP.getContext());
1378
Bill Wendling2f921f82009-05-15 09:23:25 +00001379 return SPDie;
1380}
1381
Devang Patel32cc43c2010-05-07 20:54:48 +00001382DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattner8d2fe282010-03-31 05:36:29 +00001383 assert(N && "Invalid Scope encoding!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001384
1385 DbgScope *AScope = AbstractScopes.lookup(N);
1386 if (AScope)
1387 return AScope;
Jim Grosbach042483e2009-11-21 23:12:12 +00001388
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001389 DbgScope *Parent = NULL;
1390
1391 DIDescriptor Scope(N);
1392 if (Scope.isLexicalBlock()) {
1393 DILexicalBlock DB(N);
1394 DIDescriptor ParentDesc = DB.getContext();
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001395 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001396 }
1397
1398 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1399
1400 if (Parent)
Devang Patel930143b2009-11-21 02:48:08 +00001401 Parent->addScope(AScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001402 AScope->setAbstractScope();
1403 AbstractScopes[N] = AScope;
1404 if (DIDescriptor(N).isSubprogram())
1405 AbstractScopesList.push_back(AScope);
1406 return AScope;
1407}
Devang Patel75cc16c2009-10-01 20:31:14 +00001408
Devang Patel019922d2010-04-06 23:53:48 +00001409/// isSubprogramContext - Return true if Context is either a subprogram
1410/// or another context nested inside a subprogram.
Devang Patel32cc43c2010-05-07 20:54:48 +00001411static bool isSubprogramContext(const MDNode *Context) {
Devang Patel019922d2010-04-06 23:53:48 +00001412 if (!Context)
1413 return false;
1414 DIDescriptor D(Context);
1415 if (D.isSubprogram())
1416 return true;
1417 if (D.isType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001418 return isSubprogramContext(DIType(Context).getContext());
Devang Patel019922d2010-04-06 23:53:48 +00001419 return false;
1420}
1421
Jim Grosbach042483e2009-11-21 23:12:12 +00001422/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +00001423/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1424/// If there are global variables in this scope then create and insert
1425/// DIEs for these variables.
Devang Patel32cc43c2010-05-07 20:54:48 +00001426DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001427 CompileUnit *SPCU = getCompileUnit(SPNode);
1428 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +00001429
Chris Lattner3a383cb2010-04-05 00:13:49 +00001430 assert(SPDie && "Unable to find subprogram DIE!");
1431 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001432
Chris Lattner3a383cb2010-04-05 00:13:49 +00001433 // There is not any need to generate specification DIE for a function
1434 // defined at compile unit level. If a function is defined inside another
1435 // function then gdb prefers the definition at top level and but does not
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001436 // expect specification DIE in parent function. So avoid creating
Chris Lattner3a383cb2010-04-05 00:13:49 +00001437 // specification DIE for a function defined inside a function.
1438 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001439 !SP.getContext().isFile() &&
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001440 !isSubprogramContext(SP.getContext())) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00001441 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001442
1443 // Add arguments.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001444 DICompositeType SPTy = SP.getType();
1445 DIArray Args = SPTy.getTypeArray();
1446 unsigned SPTag = SPTy.getTag();
1447 if (SPTag == dwarf::DW_TAG_subroutine_type)
1448 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1449 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001450 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattner3a383cb2010-04-05 00:13:49 +00001451 addType(Arg, ATy);
1452 if (ATy.isArtificial())
1453 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1454 SPDie->addChild(Arg);
1455 }
1456 DIE *SPDeclDie = SPDie;
1457 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001458 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Chris Lattner3a383cb2010-04-05 00:13:49 +00001459 SPDeclDie);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001460 SPCU->addDie(SPDie);
Chris Lattner3a383cb2010-04-05 00:13:49 +00001461 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001462
Devang Patela37a95e2010-07-07 22:20:57 +00001463 // Pick up abstract subprogram DIE.
1464 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1465 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1466 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1467 dwarf::DW_FORM_ref4, AbsSPDIE);
1468 SPCU->addDie(SPDie);
1469 }
1470
Chris Lattner3a383cb2010-04-05 00:13:49 +00001471 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1472 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1473 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1474 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1475 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1476 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1477 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +00001478
Chris Lattner3a383cb2010-04-05 00:13:49 +00001479 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001480}
1481
Jim Grosbach042483e2009-11-21 23:12:12 +00001482/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +00001483/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1484DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001485
1486 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1487 if (Scope->isAbstractScope())
1488 return ScopeDIE;
1489
1490 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1491 if (Ranges.empty())
1492 return 0;
1493
1494 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1495 if (Ranges.size() > 1) {
1496 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001497 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +00001498 // DW_AT_ranges appropriately.
1499 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1500 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1501 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1502 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +00001503 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1504 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +00001505 }
1506 DebugRangeSymbols.push_back(NULL);
1507 DebugRangeSymbols.push_back(NULL);
1508 return ScopeDIE;
1509 }
1510
Devang Patel9fc11702010-05-25 23:40:22 +00001511 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1512 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001513
Devang Patel9fc11702010-05-25 23:40:22 +00001514 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001515
Chris Lattnere13c3722010-03-09 01:58:53 +00001516 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1517 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001518
Devang Patel6c74a872010-04-27 19:46:33 +00001519 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1520 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001521
1522 return ScopeDIE;
1523}
1524
Devang Patel930143b2009-11-21 02:48:08 +00001525/// constructInlinedScopeDIE - This scope represents inlined body of
1526/// a function. Construct DIE to represent this concrete inlined copy
1527/// of the function.
1528DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001529
1530 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001531 assert (Ranges.empty() == false
Devang Patel6c74a872010-04-27 19:46:33 +00001532 && "DbgScope does not have instruction markers!");
1533
1534 // FIXME : .debug_inlined section specification does not clearly state how
1535 // to emit inlined scope that is split into multiple instruction ranges.
1536 // For now, use first instruction range and emit low_pc/high_pc pair and
1537 // corresponding .debug_inlined section entry for this pair.
1538 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +00001539 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1540 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001541
Devang Patel4c6bd662010-07-08 22:39:20 +00001542 if (StartLabel == 0 || EndLabel == 0) {
Devang Patel6c74a872010-04-27 19:46:33 +00001543 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1544 return 0;
1545 }
Chris Lattnere13c3722010-03-09 01:58:53 +00001546 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001547 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +00001548 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001549 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +00001550
Devang Patel3b548aa2010-03-08 20:52:55 +00001551 if (!Scope->getScopeNode())
Devang Patelbc97f6b2010-03-08 19:20:38 +00001552 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001553 DIScope DS(Scope->getScopeNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001554 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1555
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001556 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001557 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1558 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001559 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel930143b2009-11-21 02:48:08 +00001560 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001561 dwarf::DW_FORM_ref4, OriginDIE);
1562
Chris Lattner085b6522010-03-09 00:31:02 +00001563 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnere13c3722010-03-09 01:58:53 +00001564 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001565
1566 InlinedSubprogramDIEs.insert(OriginDIE);
1567
1568 // Track the start label for this inlined function.
Devang Patel32cc43c2010-05-07 20:54:48 +00001569 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001570 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001571
1572 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001573 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +00001574 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001575 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001576 } else
Chris Lattner085b6522010-03-09 00:31:02 +00001577 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001578
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001579 DILocation DL(Scope->getInlinedAt());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001580 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel930143b2009-11-21 02:48:08 +00001581 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001582
1583 return ScopeDIE;
1584}
1585
Devang Patel930143b2009-11-21 02:48:08 +00001586
1587/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel9ccfb642009-12-09 18:24:21 +00001588DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001589 StringRef Name = DV->getName();
Devang Patel2d9caf92009-11-25 17:36:49 +00001590 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +00001591 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001592
1593 // Translate tag to proper Dwarf tag. The result variable is dropped for
1594 // now.
1595 unsigned Tag;
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001596 switch (DV->getTag()) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001597 case dwarf::DW_TAG_return_variable:
1598 return NULL;
1599 case dwarf::DW_TAG_arg_variable:
1600 Tag = dwarf::DW_TAG_formal_parameter;
1601 break;
1602 case dwarf::DW_TAG_auto_variable: // fall thru
1603 default:
1604 Tag = dwarf::DW_TAG_variable;
1605 break;
1606 }
1607
1608 // Define variable debug information entry.
1609 DIE *VariableDie = new DIE(Tag);
1610
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001611 DIE *AbsDIE = NULL;
Devang Patele1c53f22010-05-20 16:36:41 +00001612 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1613 V2AVI = VarToAbstractVarMap.find(DV);
1614 if (V2AVI != VarToAbstractVarMap.end())
1615 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +00001616
Devang Patele1c53f22010-05-20 16:36:41 +00001617 if (AbsDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001618 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001619 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001620 else {
Devang Patel930143b2009-11-21 02:48:08 +00001621 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001622 addSourceLine(VariableDie, DV->getVariable());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001623
1624 // Add variable type.
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001625 addType(VariableDie, DV->getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001626 }
1627
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001628 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel6efc8e52010-02-06 01:02:37 +00001629 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelbea08d12010-09-29 23:07:21 +00001630 else if (DIVariable(DV->getVariable()).isArtificial())
1631 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel9fc11702010-05-25 23:40:22 +00001632
1633 if (Scope->isAbstractScope()) {
1634 DV->setDIE(VariableDie);
1635 return VariableDie;
1636 }
1637
1638 // Add variable address.
1639
1640 unsigned Offset = DV->getDotDebugLocOffset();
1641 if (Offset != ~0U) {
1642 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1643 Asm->GetTempSymbol("debug_loc", Offset));
1644 DV->setDIE(VariableDie);
1645 UseDotDebugLocEntry.insert(VariableDie);
1646 return VariableDie;
1647 }
1648
1649 // Check if variable is described by a DBG_VALUE instruction.
1650 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1651 DbgVariableToDbgInstMap.find(DV);
1652 if (DVI != DbgVariableToDbgInstMap.end()) {
1653 const MachineInstr *DVInsn = DVI->second;
Devang Patel9fc11702010-05-25 23:40:22 +00001654 bool updated = false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001655 // FIXME : Handle getNumOperands != 3
Devang Patel9fc11702010-05-25 23:40:22 +00001656 if (DVInsn->getNumOperands() == 3) {
Devang Patel86ec8b32010-08-31 22:22:42 +00001657 if (DVInsn->getOperand(0).isReg()) {
1658 const MachineOperand RegOp = DVInsn->getOperand(0);
1659 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1660 if (DVInsn->getOperand(1).isImm() &&
1661 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1662 addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
1663 updated = true;
1664 } else
Devang Patel53a40df62010-11-12 23:20:42 +00001665 updated = addRegisterAddress(VariableDie, RegOp);
Devang Patel86ec8b32010-08-31 22:22:42 +00001666 }
Devang Patel9fc11702010-05-25 23:40:22 +00001667 else if (DVInsn->getOperand(0).isImm())
Devang Patel53a40df62010-11-12 23:20:42 +00001668 updated = addConstantValue(VariableDie, DVInsn->getOperand(0));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001669 else if (DVInsn->getOperand(0).isFPImm())
1670 updated =
Devang Patel53a40df62010-11-12 23:20:42 +00001671 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patel9fc11702010-05-25 23:40:22 +00001672 } else {
1673 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1674 if (Location.getReg()) {
1675 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel9fc11702010-05-25 23:40:22 +00001676 updated = true;
1677 }
1678 }
1679 if (!updated) {
1680 // If variableDie is not updated then DBG_VALUE instruction does not
1681 // have valid variable info.
1682 delete VariableDie;
1683 return NULL;
1684 }
1685 DV->setDIE(VariableDie);
1686 return VariableDie;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001687 }
Devang Patel9fc11702010-05-25 23:40:22 +00001688
1689 // .. else use frame index, if available.
Devang Patel9fc11702010-05-25 23:40:22 +00001690 int FI = 0;
Devang Patel2cfc3af2010-08-31 06:11:28 +00001691 if (findVariableFrameIndex(DV, &FI))
1692 addVariableAddress(DV, VariableDie, FI);
1693
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001694 DV->setDIE(VariableDie);
1695 return VariableDie;
1696
1697}
Devang Patel930143b2009-11-21 02:48:08 +00001698
Devang Patel04d2f2d2009-11-24 01:14:22 +00001699void DwarfDebug::addPubTypes(DISubprogram SP) {
1700 DICompositeType SPTy = SP.getType();
1701 unsigned SPTag = SPTy.getTag();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001702 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel04d2f2d2009-11-24 01:14:22 +00001703 return;
1704
1705 DIArray Args = SPTy.getTypeArray();
Devang Patel04d2f2d2009-11-24 01:14:22 +00001706 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001707 DIType ATy(Args.getElement(i));
Devang Patel8d6a2b72010-05-07 21:45:47 +00001708 if (!ATy.Verify())
Devang Patel04d2f2d2009-11-24 01:14:22 +00001709 continue;
1710 DICompositeType CATy = getDICompositeType(ATy);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001711 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel12d150e2010-04-13 20:35:04 +00001712 && !CATy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001713 CompileUnit *TheCU = getCompileUnit(CATy);
1714 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1715 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001716 }
1717 }
1718}
1719
Devang Patel930143b2009-11-21 02:48:08 +00001720/// constructScopeDIE - Construct a DIE for this scope.
1721DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +00001722 if (!Scope || !Scope->getScopeNode())
1723 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001724
Devang Patel5f1b4cd2011-02-19 01:31:27 +00001725 SmallVector <DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +00001726
1727 // Collect arguments for current function.
1728 if (Scope == CurrentFnDbgScope)
1729 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
1730 if (DbgVariable *ArgDV = CurrentFnArguments[i])
1731 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
1732 Children.push_back(Arg);
1733
Devang Patel5f1b4cd2011-02-19 01:31:27 +00001734 // Collect lexical scope childrens first.
1735 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
1736 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
1737 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
1738 Children.push_back(Variable);
1739 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1740 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
1741 if (DIE *Nested = constructScopeDIE(Scopes[j]))
1742 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +00001743 DIScope DS(Scope->getScopeNode());
1744 DIE *ScopeDIE = NULL;
1745 if (Scope->getInlinedAt())
1746 ScopeDIE = constructInlinedScopeDIE(Scope);
1747 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +00001748 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +00001749 if (Scope->isAbstractScope()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001750 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +00001751 // Note down abstract DIE.
1752 if (ScopeDIE)
1753 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1754 }
Devang Patel3b548aa2010-03-08 20:52:55 +00001755 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001756 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +00001757 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +00001758 else {
1759 // There is no need to emit empty lexical block DIE.
1760 if (Children.empty())
1761 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001762 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +00001763 }
1764
Devang Patel23b2ae62010-03-29 22:59:58 +00001765 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001766
Devang Patel5f1b4cd2011-02-19 01:31:27 +00001767 // Add children
1768 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
1769 E = Children.end(); I != E; ++I)
1770 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001771
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001772 if (DS.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001773 addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001774
Devang Patel04d2f2d2009-11-24 01:14:22 +00001775 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001776}
1777
Bill Wendling2b128d72009-05-20 23:19:06 +00001778/// GetOrCreateSourceID - Look up the source id with the given directory and
1779/// source file names. If none currently exists, create a new id and insert it
1780/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1781/// maps as well.
Devang Patel498877d2010-07-24 00:53:22 +00001782
Devang Patele01b75c2011-03-24 20:30:50 +00001783unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
1784 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +00001785 // If FE did not provide a file name, then assume stdin.
1786 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +00001787 return GetOrCreateSourceID("<stdin>", StringRef());
1788
1789 // MCStream expects full path name as filename.
1790 if (!DirName.empty() && !FileName.startswith("/")) {
1791 std::string FullPathName(DirName.data());
1792 if (!DirName.endswith("/"))
1793 FullPathName += "/";
1794 FullPathName += FileName.data();
1795 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
1796 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
1797 }
Devang Patel871d0b12010-09-16 20:57:49 +00001798
Rafael Espindola67c6ab82010-11-18 02:04:25 +00001799 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
1800 if (Entry.getValue())
1801 return Entry.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +00001802
Rafael Espindola67c6ab82010-11-18 02:04:25 +00001803 unsigned SrcId = SourceIdMap.size();
1804 Entry.setValue(SrcId);
Bill Wendling2b128d72009-05-20 23:19:06 +00001805
Rafael Espindola67c6ab82010-11-18 02:04:25 +00001806 // Print out a .file directive to specify files for .loc directives.
Devang Patele01b75c2011-03-24 20:30:50 +00001807 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendling2b128d72009-05-20 23:19:06 +00001808
1809 return SrcId;
1810}
1811
Devang Patel1f4690c2009-12-15 19:16:48 +00001812/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1813DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001814 CompileUnit *TheCU = getCompileUnit(NS);
1815 DIE *NDie = TheCU->getDIE(NS);
Devang Patel1f4690c2009-12-15 19:16:48 +00001816 if (NDie)
1817 return NDie;
1818 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001819 TheCU->insertDIE(NS, NDie);
Devang Patel1f4690c2009-12-15 19:16:48 +00001820 if (!NS.getName().empty())
1821 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patelb6511a32010-08-09 20:20:05 +00001822 addSourceLine(NDie, NS);
Devang Patel1f4690c2009-12-15 19:16:48 +00001823 addToContextOwner(NDie, NS.getContext());
1824 return NDie;
1825}
1826
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001827/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +00001828/// metadata node with tag DW_TAG_compile_unit.
Devang Patel32cc43c2010-05-07 20:54:48 +00001829void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001830 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +00001831 StringRef FN = DIUnit.getFilename();
1832 StringRef Dir = DIUnit.getDirectory();
Devang Patele01b75c2011-03-24 20:30:50 +00001833 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +00001834
1835 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel930143b2009-11-21 02:48:08 +00001836 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001837 DIUnit.getProducer());
Devang Patel7b0f7962011-02-23 22:37:04 +00001838 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
Bill Wendling2b128d72009-05-20 23:19:06 +00001839 DIUnit.getLanguage());
Devang Patel930143b2009-11-21 02:48:08 +00001840 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +00001841 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1842 // simplifies debug range entries.
Devang Patel1de21ec2010-06-28 22:22:47 +00001843 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +00001844 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +00001845 // compile unit in debug_line section.
Devang Patelea636392010-08-31 23:50:19 +00001846 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
Cameron Zwarichfcf51fd2011-02-25 16:30:32 +00001847 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
1848 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +00001849 else
1850 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +00001851
Devang Patel2d9caf92009-11-25 17:36:49 +00001852 if (!Dir.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001853 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +00001854 if (DIUnit.isOptimized())
Devang Patel930143b2009-11-21 02:48:08 +00001855 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +00001856
Devang Patel2d9caf92009-11-25 17:36:49 +00001857 StringRef Flags = DIUnit.getFlags();
1858 if (!Flags.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001859 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendling2b128d72009-05-20 23:19:06 +00001860
1861 unsigned RVer = DIUnit.getRunTimeVersion();
1862 if (RVer)
Devang Patel930143b2009-11-21 02:48:08 +00001863 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +00001864 dwarf::DW_FORM_data1, RVer);
1865
Devang Patel1a0df9a2010-05-10 22:49:55 +00001866 CompileUnit *NewCU = new CompileUnit(ID, Die);
1867 if (!FirstCU)
1868 FirstCU = NewCU;
1869 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendling2b128d72009-05-20 23:19:06 +00001870}
1871
Devang Patel1a0df9a2010-05-10 22:49:55 +00001872/// getCompielUnit - Get CompileUnit DIE.
1873CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1874 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1875 DIDescriptor D(N);
1876 const MDNode *CUNode = NULL;
1877 if (D.isCompileUnit())
1878 CUNode = N;
1879 else if (D.isSubprogram())
1880 CUNode = DISubprogram(N).getCompileUnit();
1881 else if (D.isType())
1882 CUNode = DIType(N).getCompileUnit();
1883 else if (D.isGlobalVariable())
1884 CUNode = DIGlobalVariable(N).getCompileUnit();
1885 else if (D.isVariable())
1886 CUNode = DIVariable(N).getCompileUnit();
1887 else if (D.isNameSpace())
1888 CUNode = DINameSpace(N).getCompileUnit();
1889 else if (D.isFile())
1890 CUNode = DIFile(N).getCompileUnit();
1891 else
1892 return FirstCU;
1893
1894 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1895 = CUMap.find(CUNode);
1896 if (I == CUMap.end())
1897 return FirstCU;
1898 return I->second;
1899}
1900
Devang Patela8652672010-08-23 18:25:56 +00001901/// isUnsignedDIType - Return true if type encoding is unsigned.
1902static bool isUnsignedDIType(DIType Ty) {
1903 DIDerivedType DTy(Ty);
1904 if (DTy.Verify())
1905 return isUnsignedDIType(DTy.getTypeDerivedFrom());
1906
1907 DIBasicType BTy(Ty);
1908 if (BTy.Verify()) {
1909 unsigned Encoding = BTy.getEncoding();
1910 if (Encoding == dwarf::DW_ATE_unsigned ||
1911 Encoding == dwarf::DW_ATE_unsigned_char)
1912 return true;
1913 }
1914 return false;
1915}
Devang Patel1a0df9a2010-05-10 22:49:55 +00001916
Devang Patel2d9e5322011-01-20 00:02:16 +00001917// Return const exprssion if value is a GEP to access merged global
1918// constant. e.g.
1919// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1920static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1921 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1922 if (!CE || CE->getNumOperands() != 3 ||
1923 CE->getOpcode() != Instruction::GetElementPtr)
1924 return NULL;
1925
1926 // First operand points to a global value.
1927 if (!isa<GlobalValue>(CE->getOperand(0)))
1928 return NULL;
1929
1930 // Second operand is zero.
1931 const ConstantInt *CI =
1932 dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1933 if (!CI || !CI->isZero())
1934 return NULL;
1935
1936 // Third operand is offset.
1937 if (!isa<ConstantInt>(CE->getOperand(2)))
1938 return NULL;
1939
1940 return CE;
1941}
1942
Devang Patel1a0df9a2010-05-10 22:49:55 +00001943/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00001944void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel469c12d22010-08-10 01:37:23 +00001945 DIGlobalVariable GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001946
Devang Patelf5d53602009-09-04 23:59:07 +00001947 // If debug information is malformed then ignore it.
Devang Patel469c12d22010-08-10 01:37:23 +00001948 if (GV.Verify() == false)
Devang Patelf5d53602009-09-04 23:59:07 +00001949 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001950
1951 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001952 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel469c12d22010-08-10 01:37:23 +00001953 if (TheCU->getDIE(GV))
Devang Patel0751a282009-06-26 01:49:18 +00001954 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001955
Devang Patel469c12d22010-08-10 01:37:23 +00001956 DIType GTy = GV.getType();
Devang Patelb2197462010-08-10 07:11:13 +00001957 DIE *VariableDIE = new DIE(GV.getTag());
1958
1959 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001960
Devang Patel469c12d22010-08-10 01:37:23 +00001961 // Add name.
1962 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1963 GV.getDisplayName());
1964 StringRef LinkageName = GV.getLinkageName();
Devang Patelb2197462010-08-10 07:11:13 +00001965 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel469c12d22010-08-10 01:37:23 +00001966 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1967 getRealLinkageName(LinkageName));
1968 // Add type.
1969 addType(VariableDIE, GTy);
Devang Patel12d150e2010-04-13 20:35:04 +00001970 if (GTy.isCompositeType() && !GTy.getName().empty()
1971 && !GTy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001972 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001973 assert(Entry && "Missing global type!");
Devang Patel1a0df9a2010-05-10 22:49:55 +00001974 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001975 }
Devang Patel469c12d22010-08-10 01:37:23 +00001976 // Add scoping info.
1977 if (!GV.isLocalToUnit()) {
1978 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1979 // Expose as global.
1980 TheCU->addGlobal(GV.getName(), VariableDIE);
1981 }
1982 // Add line number info.
1983 addSourceLine(VariableDIE, GV);
1984 // Add to map.
1985 TheCU->insertDIE(N, VariableDIE);
1986 // Add to context owner.
1987 DIDescriptor GVContext = GV.getContext();
1988 addToContextOwner(VariableDIE, GVContext);
1989 // Add location.
Devang Patelb2197462010-08-10 07:11:13 +00001990 if (isGlobalVariable) {
1991 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1992 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1993 addLabel(Block, 0, dwarf::DW_FORM_udata,
1994 Asm->Mang->getSymbol(GV.getGlobal()));
1995 // Do not create specification DIE if context is either compile unit
1996 // or a subprogram.
1997 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1998 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1999 // Create specification DIE.
2000 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
2001 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
2002 dwarf::DW_FORM_ref4, VariableDIE);
2003 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
2004 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
2005 TheCU->addDie(VariableSpecDIE);
2006 } else {
2007 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
2008 }
Devang Patel70eb9822011-01-06 21:39:25 +00002009 } else if (ConstantInt *CI =
2010 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
2011 addConstantValue(VariableDIE, CI, isUnsignedDIType(GTy));
Devang Patel2d9e5322011-01-20 00:02:16 +00002012 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
2013 // GV is a merged global.
2014 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
2015 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
2016 addLabel(Block, 0, dwarf::DW_FORM_udata,
2017 Asm->Mang->getSymbol(cast<GlobalValue>(CE->getOperand(0))));
2018 ConstantInt *CII = cast<ConstantInt>(CE->getOperand(2));
2019 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
2020 addUInt(Block, 0, dwarf::DW_FORM_udata, CII->getZExtValue());
2021 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
2022 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
2023 }
Devang Patel70eb9822011-01-06 21:39:25 +00002024
Devang Patel0751a282009-06-26 01:49:18 +00002025 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00002026}
2027
Devang Patel1a0df9a2010-05-10 22:49:55 +00002028/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00002029void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00002030 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +00002031
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00002032 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002033 CompileUnit *TheCU = getCompileUnit(N);
2034 if (TheCU->getDIE(N))
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00002035 return;
2036
Bill Wendling2b128d72009-05-20 23:19:06 +00002037 if (!SP.isDefinition())
2038 // This is a method declaration which will be handled while constructing
2039 // class type.
Devang Patel0751a282009-06-26 01:49:18 +00002040 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00002041
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00002042 DIE *SubprogramDie = createSubprogramDIE(SP);
2043
2044 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002045 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00002046
2047 // Add to context owner.
Devang Patel1f4690c2009-12-15 19:16:48 +00002048 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +00002049
Bill Wendling2b128d72009-05-20 23:19:06 +00002050 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002051 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002052
Devang Patel0751a282009-06-26 01:49:18 +00002053 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00002054}
2055
Devang Patel930143b2009-11-21 02:48:08 +00002056/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +00002057/// content. Create global DIEs and emit initial debug info sections.
2058/// This is inovked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +00002059void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +00002060 if (DisableDebugInfoPrinting)
2061 return;
2062
Devang Patel63524442009-07-30 18:56:46 +00002063 DebugInfoFinder DbgFinder;
2064 DbgFinder.processModule(*M);
Devang Patel0751a282009-06-26 01:49:18 +00002065
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002066 bool HasDebugInfo = false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002067
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002068 // Scan all the compile-units to see if there are any marked as the main unit.
2069 // if not, we do not generate debug info.
2070 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2071 E = DbgFinder.compile_unit_end(); I != E; ++I) {
2072 if (DICompileUnit(*I).isMain()) {
2073 HasDebugInfo = true;
2074 break;
2075 }
2076 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002077
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002078 if (!HasDebugInfo) return;
2079
2080 // Tell MMI that we have debug info.
2081 MMI->setDebugInfoAvailability(true);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002082
Chris Lattnerd442aa32010-04-04 23:17:54 +00002083 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002084 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002085
Bill Wendling2b128d72009-05-20 23:19:06 +00002086 // Create all the compile unit DIEs.
Devang Patel63524442009-07-30 18:56:46 +00002087 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2088 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002089 constructCompileUnit(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00002090
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002091 // Create DIEs for each subprogram.
Devang Patel63524442009-07-30 18:56:46 +00002092 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
2093 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002094 constructSubprogramDIE(*I);
Devang Patel0751a282009-06-26 01:49:18 +00002095
Devang Patel2b75ed22009-12-10 19:14:49 +00002096 // Create DIEs for each global variable.
2097 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
2098 E = DbgFinder.global_variable_end(); I != E; ++I)
2099 constructGlobalVariableDIE(*I);
2100
Devang Patel8e06a5e2010-08-10 20:01:20 +00002101 //getOrCreateTypeDIE
2102 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
2103 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2104 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2105
Devang Patel7a554812010-09-28 18:08:20 +00002106 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
2107 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2108 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2109
Bill Wendling2b128d72009-05-20 23:19:06 +00002110 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +00002111 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00002112}
2113
Devang Patel930143b2009-11-21 02:48:08 +00002114/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +00002115///
Devang Patel930143b2009-11-21 02:48:08 +00002116void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002117 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +00002118 const Module *M = MMI->getModule();
Devang Pateld0701282010-08-02 17:32:15 +00002119 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +00002120 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2121 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2122 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2123 DISubprogram SP(AllSPs->getOperand(SI));
2124 if (!SP.Verify()) continue;
2125
2126 // Collect info for variables that were optimized out.
Devang Patel18efced2010-07-19 17:53:55 +00002127 if (!SP.isDefinition()) continue;
Devang Patelf3b2db62010-06-28 18:25:03 +00002128 StringRef FName = SP.getLinkageName();
2129 if (FName.empty())
2130 FName = SP.getName();
Devang Patel364bf042010-11-10 22:19:21 +00002131 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patelf3b2db62010-06-28 18:25:03 +00002132 if (!NMD) continue;
2133 unsigned E = NMD->getNumOperands();
2134 if (!E) continue;
2135 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Pateld0701282010-08-02 17:32:15 +00002136 DeadFnScopeMap[SP] = Scope;
Devang Patelf3b2db62010-06-28 18:25:03 +00002137 for (unsigned I = 0; I != E; ++I) {
2138 DIVariable DV(NMD->getOperand(I));
2139 if (!DV.Verify()) continue;
2140 Scope->addVariable(new DbgVariable(DV));
2141 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002142
Devang Patelf3b2db62010-06-28 18:25:03 +00002143 // Construct subprogram DIE and add variables DIEs.
2144 constructSubprogramDIE(SP);
2145 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patel406798a2010-08-09 18:51:29 +00002146 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patelf3b2db62010-06-28 18:25:03 +00002147 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2148 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2149 if (VariableDIE)
2150 ScopeDIE->addChild(VariableDIE);
2151 }
2152 }
2153 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002154
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002155 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2156 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2157 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2158 DIE *ISP = *AI;
Devang Patel930143b2009-11-21 02:48:08 +00002159 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002160 }
2161
Devang Patel32cc43c2010-05-07 20:54:48 +00002162 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Pateleb57c592009-12-03 19:11:07 +00002163 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2164 DIE *SPDie = CI->first;
Devang Patel32cc43c2010-05-07 20:54:48 +00002165 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Pateleb57c592009-12-03 19:11:07 +00002166 if (!N) continue;
Devang Patel1a0df9a2010-05-10 22:49:55 +00002167 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Pateleb57c592009-12-03 19:11:07 +00002168 if (!NDie) continue;
2169 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Pateleb57c592009-12-03 19:11:07 +00002170 }
2171
Bill Wendling2b128d72009-05-20 23:19:06 +00002172 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002173 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002174 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002175 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002176 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +00002177
2178 // End text sections.
2179 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002180 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +00002181 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +00002182 }
2183
2184 // Emit common frame information.
Devang Patel930143b2009-11-21 02:48:08 +00002185 emitCommonDebugFrame();
Bill Wendling2b128d72009-05-20 23:19:06 +00002186
2187 // Emit function debug frame information
2188 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2189 E = DebugFrames.end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002190 emitFunctionDebugFrame(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00002191
2192 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +00002193 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +00002194
2195 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +00002196 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002197
2198 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +00002199 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +00002200
Bill Wendling2b128d72009-05-20 23:19:06 +00002201 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +00002202 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +00002203
Devang Patel04d2f2d2009-11-24 01:14:22 +00002204 // Emit info into a debug pubtypes section.
2205 emitDebugPubTypes();
2206
Bill Wendling2b128d72009-05-20 23:19:06 +00002207 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002208 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +00002209
2210 // Emit info into a debug aranges section.
2211 EmitDebugARanges();
2212
2213 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002214 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +00002215
2216 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002217 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002218
2219 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +00002220 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002221
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002222 // Emit info into a debug str section.
2223 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002224
Devang Pateld0701282010-08-02 17:32:15 +00002225 // clean up.
2226 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002227 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2228 E = CUMap.end(); I != E; ++I)
2229 delete I->second;
2230 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +00002231}
2232
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002233/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002234DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattner915c5f92010-04-02 19:42:39 +00002235 DebugLoc ScopeLoc) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002236
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002237 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002238 if (AbsDbgVariable)
2239 return AbsDbgVariable;
2240
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002241 LLVMContext &Ctx = Var->getContext();
Chris Lattner915c5f92010-04-02 19:42:39 +00002242 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002243 if (!Scope)
2244 return NULL;
2245
Devang Patele1c53f22010-05-20 16:36:41 +00002246 AbsDbgVariable = new DbgVariable(Var);
Devang Patel930143b2009-11-21 02:48:08 +00002247 Scope->addVariable(AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002248 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002249 return AbsDbgVariable;
2250}
2251
Devang Patel6c622ef2011-03-01 22:58:55 +00002252/// addCurrentFnArgument - If Var is an current function argument that add
2253/// it in CurrentFnArguments list.
2254bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
2255 DbgVariable *Var, DbgScope *Scope) {
2256 if (Scope != CurrentFnDbgScope)
2257 return false;
2258 DIVariable DV = Var->getVariable();
2259 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
2260 return false;
2261 unsigned ArgNo = DV.getArgNumber();
2262 if (ArgNo == 0)
2263 return false;
2264
Devang Patel4ab660b2011-03-03 20:02:02 +00002265 size_t Size = CurrentFnArguments.size();
2266 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00002267 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00002268 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00002269 // arguments does the function have at source level.
2270 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00002271 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00002272 CurrentFnArguments[ArgNo - 1] = Var;
2273 return true;
2274}
2275
Devang Patel490c8ab2010-05-20 19:57:06 +00002276/// collectVariableInfoFromMMITable - Collect variable information from
2277/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002278void
Devang Patel490c8ab2010-05-20 19:57:06 +00002279DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2280 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002281 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patel475d32a2009-10-06 01:26:37 +00002282 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2283 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2284 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00002285 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002286 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00002287 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00002288 DIVariable DV(Var);
2289 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002290
Chris Lattner915c5f92010-04-02 19:42:39 +00002291 DbgScope *Scope = 0;
Devang Patel32cc43c2010-05-07 20:54:48 +00002292 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattner915c5f92010-04-02 19:42:39 +00002293 Scope = ConcreteScopes.lookup(IA);
2294 if (Scope == 0)
2295 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002296
Devang Patelcdb7d442009-11-10 23:20:04 +00002297 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00002298 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00002299 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002300
Devang Patele1c53f22010-05-20 16:36:41 +00002301 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2302 DbgVariable *RegVar = new DbgVariable(DV);
2303 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +00002304 if (!addCurrentFnArgument(MF, RegVar, Scope))
2305 Scope->addVariable(RegVar);
Devang Patele1c53f22010-05-20 16:36:41 +00002306 if (AbsDbgVariable) {
2307 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2308 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2309 }
Devang Patel475d32a2009-10-06 01:26:37 +00002310 }
Devang Patel490c8ab2010-05-20 19:57:06 +00002311}
Devang Patela3e9c9c2010-03-15 18:33:46 +00002312
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002313/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +00002314/// DBG_VALUE instruction, is in a defined reg.
2315static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2316 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002317 return MI->getNumOperands() == 3 &&
2318 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
2319 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +00002320}
2321
Devang Patel490c8ab2010-05-20 19:57:06 +00002322/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002323void
Devang Patel5c0f85c2010-06-25 22:07:34 +00002324DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2325 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002326
Devang Patel490c8ab2010-05-20 19:57:06 +00002327 /// collection info from MMI table.
2328 collectVariableInfoFromMMITable(MF, Processed);
2329
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002330 for (SmallVectorImpl<const MDNode*>::const_iterator
2331 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
2332 ++UVI) {
2333 const MDNode *Var = *UVI;
2334 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00002335 continue;
2336
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002337 // History contains relevant DBG_VALUE instructions for Var and instructions
2338 // clobbering it.
2339 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
2340 if (History.empty())
2341 continue;
2342 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00002343
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002344 DIVariable DV(Var);
Devang Patel447cb382011-01-11 21:42:10 +00002345 DbgScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00002346 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2347 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelfbd6c452010-05-21 00:10:20 +00002348 Scope = CurrentFnDbgScope;
Devang Patel447cb382011-01-11 21:42:10 +00002349 else
2350 Scope = findDbgScope(MInsn);
Devang Patel490c8ab2010-05-20 19:57:06 +00002351 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00002352 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00002353 continue;
2354
2355 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002356 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel490c8ab2010-05-20 19:57:06 +00002357 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel6c622ef2011-03-01 22:58:55 +00002358 if (!addCurrentFnArgument(MF, RegVar, Scope))
2359 Scope->addVariable(RegVar);
Devang Patelfbd6c452010-05-21 00:10:20 +00002360 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2361 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2362 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patela3e9c9c2010-03-15 18:33:46 +00002363 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002364
2365 // Simple ranges that are fully coalesced.
2366 if (History.size() <= 1 || (History.size() == 2 &&
2367 MInsn->isIdenticalTo(History.back()))) {
Devang Patel9fc11702010-05-25 23:40:22 +00002368 DbgVariableToDbgInstMap[RegVar] = MInsn;
2369 continue;
2370 }
2371
2372 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002373 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002374
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002375 for (SmallVectorImpl<const MachineInstr*>::const_iterator
2376 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
2377 const MachineInstr *Begin = *HI;
2378 assert(Begin->isDebugValue() && "Invalid History entry");
Devang Patel9fc11702010-05-25 23:40:22 +00002379 MachineLocation MLoc;
Devang Patel0e60e672010-08-04 18:40:52 +00002380 if (Begin->getNumOperands() == 3) {
2381 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2382 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2383 } else
2384 MLoc = Asm->getDebugValueLocation(Begin);
2385
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002386 // FIXME: emitDebugLoc only understands registers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002387 if (!MLoc.getReg())
2388 continue;
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00002389
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002390 // Compute the range for a register location.
2391 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2392 const MCSymbol *SLabel = 0;
2393
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002394 if (HI + 1 == HE)
2395 // If Begin is the last instruction in History then its value is valid
Devang Patel9fc11702010-05-25 23:40:22 +00002396 // until the end of the funtion.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002397 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002398 else {
2399 const MachineInstr *End = HI[1];
2400 if (End->isDebugValue())
2401 SLabel = getLabelBeforeInsn(End);
2402 else {
2403 // End is a normal instruction clobbering the range.
2404 SLabel = getLabelAfterInsn(End);
2405 assert(SLabel && "Forgot label after clobber instruction");
2406 ++HI;
2407 }
2408 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002409
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002410 // The value is valid until the next DBG_VALUE or clobber.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002411 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
Devang Patel9fc11702010-05-25 23:40:22 +00002412 }
2413 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00002414 }
Devang Patele0a94bf2010-05-14 21:01:35 +00002415
2416 // Collect info for variables that were optimized out.
Devang Patelad517352010-06-22 01:01:58 +00002417 const Function *F = MF->getFunction();
Devang Patel364bf042010-11-10 22:19:21 +00002418 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patele0a94bf2010-05-14 21:01:35 +00002419 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00002420 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel490c8ab2010-05-20 19:57:06 +00002421 if (!DV || !Processed.insert(DV))
Devang Patele0a94bf2010-05-14 21:01:35 +00002422 continue;
2423 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2424 if (Scope)
Devang Patele1c53f22010-05-20 16:36:41 +00002425 Scope->addVariable(new DbgVariable(DV));
Devang Patele0a94bf2010-05-14 21:01:35 +00002426 }
2427 }
Devang Patel9fc11702010-05-25 23:40:22 +00002428}
Devang Patele0a94bf2010-05-14 21:01:35 +00002429
Devang Patel9fc11702010-05-25 23:40:22 +00002430/// getLabelBeforeInsn - Return Label preceding the instruction.
2431const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002432 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
2433 assert(Label && "Didn't insert label before instruction");
2434 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00002435}
2436
2437/// getLabelAfterInsn - Return Label immediately following the instruction.
2438const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002439 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00002440}
2441
Devang Patelb5694e72010-10-26 17:49:02 +00002442/// beginInstruction - Process beginning of an instruction.
2443void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00002444 // Check if source location changes, but ignore DBG_VALUE locations.
2445 if (!MI->isDebugValue()) {
2446 DebugLoc DL = MI->getDebugLoc();
2447 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
2448 PrevInstLoc = DL;
2449 if (!DL.isUnknown()) {
2450 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2451 recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2452 } else
2453 recordSourceLine(0, 0, 0);
2454 }
Devang Patel9fc11702010-05-25 23:40:22 +00002455 }
Devang Patel23b2ae62010-03-29 22:59:58 +00002456
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00002457 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002458 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
2459 LabelsBeforeInsn.find(MI);
2460
2461 // No label needed.
2462 if (I == LabelsBeforeInsn.end())
2463 return;
2464
2465 // Label already assigned.
2466 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00002467 return;
Devang Patelbd477be2010-03-29 17:20:31 +00002468
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00002469 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00002470 PrevLabel = MMI->getContext().CreateTempSymbol();
2471 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00002472 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002473 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00002474}
2475
Devang Patelb5694e72010-10-26 17:49:02 +00002476/// endInstruction - Process end of an instruction.
2477void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00002478 // Don't create a new label after DBG_VALUE instructions.
2479 // They don't generate code.
2480 if (!MI->isDebugValue())
2481 PrevLabel = 0;
2482
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002483 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
2484 LabelsAfterInsn.find(MI);
2485
2486 // No label needed.
2487 if (I == LabelsAfterInsn.end())
2488 return;
2489
2490 // Label already assigned.
2491 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00002492 return;
2493
2494 // We need a label after this instruction.
2495 if (!PrevLabel) {
2496 PrevLabel = MMI->getContext().CreateTempSymbol();
2497 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00002498 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002499 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002500}
2501
Devang Patel6c74a872010-04-27 19:46:33 +00002502/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002503DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel30265c42010-07-07 20:12:52 +00002504 const MDNode *InlinedAt) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002505 if (!InlinedAt) {
2506 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2507 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002508 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002509 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2510 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patel6c74a872010-04-27 19:46:33 +00002511 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002512 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002513 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Patel6c74a872010-04-27 19:46:33 +00002514 WScope->setParent(Parent);
2515 Parent->addScope(WScope);
2516 }
2517
2518 if (!WScope->getParent()) {
2519 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastings9b5005c2010-06-15 23:06:30 +00002520 // We used to check only for a linkage name, but that fails
2521 // since we began omitting the linkage name for private
2522 // functions. The new way is to check for the name in metadata,
2523 // but that's not supported in old .ll test cases. Ergo, we
2524 // check both.
Stuart Hastingsafe54f12010-06-11 20:08:44 +00002525 if (SPName == Asm->MF->getFunction()->getName() ||
2526 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Patel6c74a872010-04-27 19:46:33 +00002527 CurrentFnDbgScope = WScope;
2528 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002529
Devang Patel6c74a872010-04-27 19:46:33 +00002530 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002531 }
2532
Devang Patel5c0f85c2010-06-25 22:07:34 +00002533 getOrCreateAbstractScope(Scope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002534 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2535 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002536 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002537
2538 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2539 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2540 DILocation DL(InlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002541 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002542 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Patel6c74a872010-04-27 19:46:33 +00002543 WScope->setParent(Parent);
2544 Parent->addScope(WScope);
2545
2546 ConcreteScopes[InlinedAt] = WScope;
Devang Patel6c74a872010-04-27 19:46:33 +00002547
2548 return WScope;
Devang Patel8db360d2009-10-06 01:50:42 +00002549}
2550
Devang Patel6c74a872010-04-27 19:46:33 +00002551/// hasValidLocation - Return true if debug location entry attached with
2552/// machine instruction encodes valid location info.
2553static bool hasValidLocation(LLVMContext &Ctx,
2554 const MachineInstr *MInsn,
Devang Patel32cc43c2010-05-07 20:54:48 +00002555 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Patel6c74a872010-04-27 19:46:33 +00002556 DebugLoc DL = MInsn->getDebugLoc();
2557 if (DL.isUnknown()) return false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002558
Devang Patel32cc43c2010-05-07 20:54:48 +00002559 const MDNode *S = DL.getScope(Ctx);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002560
Devang Patel6c74a872010-04-27 19:46:33 +00002561 // There is no need to create another DIE for compile unit. For all
2562 // other scopes, create one DbgScope now. This will be translated
2563 // into a scope DIE at the end.
2564 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002565
Devang Patel6c74a872010-04-27 19:46:33 +00002566 Scope = S;
2567 InlinedAt = DL.getInlinedAt(Ctx);
2568 return true;
2569}
2570
2571/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2572/// hierarchy.
2573static void calculateDominanceGraph(DbgScope *Scope) {
2574 assert (Scope && "Unable to calculate scop edominance graph!");
2575 SmallVector<DbgScope *, 4> WorkStack;
2576 WorkStack.push_back(Scope);
2577 unsigned Counter = 0;
2578 while (!WorkStack.empty()) {
2579 DbgScope *WS = WorkStack.back();
2580 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2581 bool visitedChildren = false;
2582 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2583 SE = Children.end(); SI != SE; ++SI) {
2584 DbgScope *ChildScope = *SI;
2585 if (!ChildScope->getDFSOut()) {
2586 WorkStack.push_back(ChildScope);
2587 visitedChildren = true;
2588 ChildScope->setDFSIn(++Counter);
2589 break;
2590 }
2591 }
2592 if (!visitedChildren) {
2593 WorkStack.pop_back();
2594 WS->setDFSOut(++Counter);
2595 }
2596 }
2597}
2598
2599/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002600static
Devang Patel6c74a872010-04-27 19:46:33 +00002601void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2602 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2603{
2604#ifndef NDEBUG
2605 unsigned PrevDFSIn = 0;
2606 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2607 I != E; ++I) {
2608 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2609 II != IE; ++II) {
2610 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002611 const MDNode *Scope = NULL;
2612 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002613
2614 // Check if instruction has valid location information.
2615 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2616 dbgs() << " [ ";
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002617 if (InlinedAt)
Devang Patel6c74a872010-04-27 19:46:33 +00002618 dbgs() << "*";
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002619 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Patel6c74a872010-04-27 19:46:33 +00002620 MI2ScopeMap.find(MInsn);
2621 if (DI != MI2ScopeMap.end()) {
2622 DbgScope *S = DI->second;
2623 dbgs() << S->getDFSIn();
2624 PrevDFSIn = S->getDFSIn();
2625 } else
2626 dbgs() << PrevDFSIn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002627 } else
Devang Patel6c74a872010-04-27 19:46:33 +00002628 dbgs() << " [ x" << PrevDFSIn;
2629 dbgs() << " ]";
2630 MInsn->dump();
2631 }
2632 dbgs() << "\n";
2633 }
2634#endif
2635}
Devang Patel930143b2009-11-21 02:48:08 +00002636/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner848c7d22010-03-31 05:39:57 +00002637/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner76555b52010-01-26 23:18:02 +00002638bool DwarfDebug::extractScopeInformation() {
Devang Patel75cc16c2009-10-01 20:31:14 +00002639 // If scope information was extracted using .dbg intrinsics then there is not
2640 // any need to extract these information by scanning each instruction.
2641 if (!DbgScopeMap.empty())
2642 return false;
2643
Dan Gohmane9135cb2010-04-23 01:18:53 +00002644 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6c74a872010-04-27 19:46:33 +00002645 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2646 SmallVector<DbgRange, 4> MIRanges;
2647 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patel32cc43c2010-05-07 20:54:48 +00002648 const MDNode *PrevScope = NULL;
2649 const MDNode *PrevInlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002650 const MachineInstr *RangeBeginMI = NULL;
2651 const MachineInstr *PrevMI = NULL;
Chris Lattner3a383cb2010-04-05 00:13:49 +00002652 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel75cc16c2009-10-01 20:31:14 +00002653 I != E; ++I) {
2654 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2655 II != IE; ++II) {
2656 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002657 const MDNode *Scope = NULL;
2658 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002659
2660 // Check if instruction has valid location information.
2661 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2662 PrevMI = MInsn;
2663 continue;
2664 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002665
Devang Patel6c74a872010-04-27 19:46:33 +00002666 // If scope has not changed then skip this instruction.
2667 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2668 PrevMI = MInsn;
2669 continue;
2670 }
2671
Devang Pateld12c0a22011-02-15 17:56:09 +00002672 // Ignore DBG_VALUE. It does not contribute any instruction in output.
2673 if (MInsn->isDebugValue())
2674 continue;
2675
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002676 if (RangeBeginMI) {
2677 // If we have alread seen a beginning of a instruction range and
Devang Patel6c74a872010-04-27 19:46:33 +00002678 // current instruction scope does not match scope of first instruction
2679 // in this range then create a new instruction range.
2680 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002681 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel30265c42010-07-07 20:12:52 +00002682 PrevInlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002683 MIRanges.push_back(R);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002684 }
Devang Patel6c74a872010-04-27 19:46:33 +00002685
2686 // This is a beginning of a new instruction range.
2687 RangeBeginMI = MInsn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002688
Devang Patel6c74a872010-04-27 19:46:33 +00002689 // Reset previous markers.
2690 PrevMI = MInsn;
2691 PrevScope = Scope;
2692 PrevInlinedAt = InlinedAt;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002693 }
2694 }
2695
Devang Patel6c74a872010-04-27 19:46:33 +00002696 // Create last instruction range.
2697 if (RangeBeginMI && PrevMI && PrevScope) {
2698 DbgRange R(RangeBeginMI, PrevMI);
2699 MIRanges.push_back(R);
2700 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel75cc16c2009-10-01 20:31:14 +00002701 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002702
Devang Patel530a0752010-01-04 20:44:00 +00002703 if (!CurrentFnDbgScope)
2704 return false;
2705
Devang Patel6c74a872010-04-27 19:46:33 +00002706 calculateDominanceGraph(CurrentFnDbgScope);
2707 if (PrintDbgScope)
2708 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2709
2710 // Find ranges of instructions covered by each DbgScope;
2711 DbgScope *PrevDbgScope = NULL;
2712 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2713 RE = MIRanges.end(); RI != RE; ++RI) {
2714 const DbgRange &R = *RI;
2715 DbgScope *S = MI2ScopeMap.lookup(R.first);
2716 assert (S && "Lost DbgScope for a machine instruction!");
2717 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2718 PrevDbgScope->closeInsnRange(S);
2719 S->openInsnRange(R.first);
2720 S->extendInsnRange(R.second);
2721 PrevDbgScope = S;
2722 }
2723
2724 if (PrevDbgScope)
2725 PrevDbgScope->closeInsnRange();
Devang Patel75cc16c2009-10-01 20:31:14 +00002726
Devang Patel359b0132010-04-08 18:43:56 +00002727 identifyScopeMarkers();
Devang Patelf1d5a1e2010-04-08 15:37:09 +00002728
2729 return !DbgScopeMap.empty();
2730}
2731
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002732/// identifyScopeMarkers() -
Devang Patel6c74a872010-04-27 19:46:33 +00002733/// Each DbgScope has first instruction and last instruction to mark beginning
2734/// and end of a scope respectively. Create an inverse map that list scopes
2735/// starts (and ends) with an instruction. One instruction may start (or end)
2736/// multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00002737void DwarfDebug::identifyScopeMarkers() {
Devang Patel7771b7c2010-01-20 02:05:23 +00002738 SmallVector<DbgScope *, 4> WorkList;
2739 WorkList.push_back(CurrentFnDbgScope);
2740 while (!WorkList.empty()) {
Chris Lattner848c7d22010-03-31 05:39:57 +00002741 DbgScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002742
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002743 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002744 if (!Children.empty())
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002745 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00002746 SE = Children.end(); SI != SE; ++SI)
2747 WorkList.push_back(*SI);
2748
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002749 if (S->isAbstractScope())
2750 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002751
Devang Patel6c74a872010-04-27 19:46:33 +00002752 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2753 if (Ranges.empty())
2754 continue;
2755 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2756 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002757 assert(RI->first && "DbgRange does not have first instruction!");
2758 assert(RI->second && "DbgRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002759 requestLabelBeforeInsn(RI->first);
2760 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00002761 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002762 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002763}
2764
Dan Gohman3df671a2010-04-20 00:37:27 +00002765/// FindFirstDebugLoc - Find the first debug location in the function. This
2766/// is intended to be an approximation for the source position of the
2767/// beginning of the function.
2768static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2769 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2770 I != E; ++I)
2771 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2772 MBBI != MBBE; ++MBBI) {
2773 DebugLoc DL = MBBI->getDebugLoc();
2774 if (!DL.isUnknown())
2775 return DL;
2776 }
2777 return DebugLoc();
2778}
2779
Devang Patela86114b2010-10-25 20:45:32 +00002780#ifndef NDEBUG
2781/// CheckLineNumbers - Count basicblocks whose instructions do not have any
2782/// line number information.
2783static void CheckLineNumbers(const MachineFunction *MF) {
2784 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2785 I != E; ++I) {
2786 bool FoundLineNo = false;
2787 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2788 II != IE; ++II) {
2789 const MachineInstr *MI = II;
2790 if (!MI->getDebugLoc().isUnknown()) {
2791 FoundLineNo = true;
2792 break;
2793 }
2794 }
Devang Patel6e0d5892010-10-28 22:11:59 +00002795 if (!FoundLineNo && I->size())
Devang Patela86114b2010-10-25 20:45:32 +00002796 ++BlocksWithoutLineNo;
2797 }
2798}
2799#endif
2800
Devang Patel930143b2009-11-21 02:48:08 +00002801/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00002802/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00002803void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00002804 if (!MMI->hasDebugInfo()) return;
Bill Wendlingfcc14142010-04-07 09:28:04 +00002805 if (!extractScopeInformation()) return;
Devang Patel4598eb62009-10-06 18:37:31 +00002806
Devang Patela86114b2010-10-25 20:45:32 +00002807#ifndef NDEBUG
2808 CheckLineNumbers(MF);
2809#endif
2810
Devang Patel6c74a872010-04-27 19:46:33 +00002811 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2812 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00002813 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00002814 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00002815
2816 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2817 // function.
Dan Gohman3df671a2010-04-20 00:37:27 +00002818 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattner915c5f92010-04-02 19:42:39 +00002819 if (FDL.isUnknown()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002820
Devang Patel32cc43c2010-05-07 20:54:48 +00002821 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings61475c52010-07-19 23:56:30 +00002822 const MDNode *TheScope = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002823
Chris Lattner915c5f92010-04-02 19:42:39 +00002824 DISubprogram SP = getDISubprogram(Scope);
2825 unsigned Line, Col;
2826 if (SP.Verify()) {
2827 Line = SP.getLineNumber();
2828 Col = 0;
Stuart Hastings61475c52010-07-19 23:56:30 +00002829 TheScope = SP;
Chris Lattner915c5f92010-04-02 19:42:39 +00002830 } else {
2831 Line = FDL.getLine();
2832 Col = FDL.getCol();
Stuart Hastings61475c52010-07-19 23:56:30 +00002833 TheScope = Scope;
Bill Wendling2b128d72009-05-20 23:19:06 +00002834 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002835
Stuart Hastings61475c52010-07-19 23:56:30 +00002836 recordSourceLine(Line, Col, TheScope);
Devang Patel002d54d2010-05-26 19:37:24 +00002837
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002838 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
2839
Devang Patel21ccf052010-06-02 16:42:51 +00002840 /// ProcessedArgs - Collection of arguments already processed.
2841 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2842
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002843 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
2844
2845 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
2846 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
2847
Devang Patel002d54d2010-05-26 19:37:24 +00002848 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002849 I != E; ++I) {
2850 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00002851 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2852 II != IE; ++II) {
2853 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002854
Devang Patel002d54d2010-05-26 19:37:24 +00002855 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00002856 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002857
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002858 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002859 const MDNode *Var =
2860 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002861
2862 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002863 if (isDbgValueInDefinedReg(MI))
2864 LiveUserVar[MI->getOperand(0).getReg()] = Var;
2865
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002866 // Check the history of this variable.
2867 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
2868 if (History.empty()) {
2869 UserVariables.push_back(Var);
2870 // The first mention of a function argument gets the FunctionBeginSym
2871 // label, so arguments are visible when breaking at function entry.
2872 DIVariable DV(Var);
2873 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
2874 DISubprogram(getDISubprogram(DV.getContext()))
2875 .describes(MF->getFunction()))
2876 LabelsBeforeInsn[MI] = FunctionBeginSym;
2877 } else {
2878 // We have seen this variable before. Try to coalesce DBG_VALUEs.
2879 const MachineInstr *Prev = History.back();
2880 if (Prev->isDebugValue()) {
2881 // Coalesce identical entries at the end of History.
2882 if (History.size() >= 2 &&
2883 Prev->isIdenticalTo(History[History.size() - 2]))
2884 History.pop_back();
2885
2886 // Terminate old register assignments that don't reach MI;
2887 MachineFunction::const_iterator PrevMBB = Prev->getParent();
2888 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
2889 isDbgValueInDefinedReg(Prev)) {
2890 // Previous register assignment needs to terminate at the end of
2891 // its basic block.
2892 MachineBasicBlock::const_iterator LastMI =
2893 PrevMBB->getLastNonDebugInstr();
2894 if (LastMI == PrevMBB->end())
2895 // Drop DBG_VALUE for empty range.
2896 History.pop_back();
2897 else {
2898 // Terminate after LastMI.
2899 History.push_back(LastMI);
2900 }
2901 }
2902 }
2903 }
2904 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00002905 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002906 // Not a DBG_VALUE instruction.
2907 if (!MI->isLabel())
2908 AtBlockEntry = false;
2909
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002910 // Check if the instruction clobbers any registers with debug vars.
2911 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
2912 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
2913 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
2914 continue;
2915 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
2916 unsigned Reg = *AI; ++AI) {
2917 const MDNode *Var = LiveUserVar[Reg];
2918 if (!Var)
2919 continue;
2920 // Reg is now clobbered.
2921 LiveUserVar[Reg] = 0;
2922
2923 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002924 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
2925 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002926 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002927 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
2928 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002929 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002930 const MachineInstr *Prev = History.back();
2931 // Sanity-check: Register assignments are terminated at the end of
2932 // their block.
2933 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
2934 continue;
2935 // Is the variable still in Reg?
2936 if (!isDbgValueInDefinedReg(Prev) ||
2937 Prev->getOperand(0).getReg() != Reg)
2938 continue;
2939 // Var is clobbered. Make sure the next instruction gets a label.
2940 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00002941 }
2942 }
Devang Patel002d54d2010-05-26 19:37:24 +00002943 }
Devang Patel002d54d2010-05-26 19:37:24 +00002944 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00002945 }
2946
2947 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
2948 I != E; ++I) {
2949 SmallVectorImpl<const MachineInstr*> &History = I->second;
2950 if (History.empty())
2951 continue;
2952
2953 // Make sure the final register assignments are terminated.
2954 const MachineInstr *Prev = History.back();
2955 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
2956 const MachineBasicBlock *PrevMBB = Prev->getParent();
2957 MachineBasicBlock::const_iterator LastMI = PrevMBB->getLastNonDebugInstr();
2958 if (LastMI == PrevMBB->end())
2959 // Drop DBG_VALUE for empty range.
2960 History.pop_back();
2961 else {
2962 // Terminate after LastMI.
2963 History.push_back(LastMI);
2964 }
2965 }
2966 // Request labels for the full history.
2967 for (unsigned i = 0, e = History.size(); i != e; ++i) {
2968 const MachineInstr *MI = History[i];
2969 if (MI->isDebugValue())
2970 requestLabelBeforeInsn(MI);
2971 else
2972 requestLabelAfterInsn(MI);
2973 }
2974 }
Devang Patel002d54d2010-05-26 19:37:24 +00002975
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00002976 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00002977 PrevLabel = FunctionBeginSym;
Bill Wendling2b128d72009-05-20 23:19:06 +00002978}
2979
Devang Patel930143b2009-11-21 02:48:08 +00002980/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00002981///
Chris Lattner76555b52010-01-26 23:18:02 +00002982void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingfcc14142010-04-07 09:28:04 +00002983 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00002984
Devang Patel530a0752010-01-04 20:44:00 +00002985 if (CurrentFnDbgScope) {
Devang Patel4a8e6e82010-05-22 00:04:14 +00002986
Devang Patel9fc11702010-05-25 23:40:22 +00002987 // Define end label for subprogram.
2988 FunctionEndSym = Asm->GetTempSymbol("func_end",
2989 Asm->getFunctionNumber());
2990 // Assumes in correct section after the entry point.
2991 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002992
Devang Patel5c0f85c2010-06-25 22:07:34 +00002993 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2994 collectVariableInfo(MF, ProcessedVars);
Devang Patel4a8e6e82010-05-22 00:04:14 +00002995
Devang Patel530a0752010-01-04 20:44:00 +00002996 // Construct abstract scopes.
2997 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel5c0f85c2010-06-25 22:07:34 +00002998 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel5c0f85c2010-06-25 22:07:34 +00002999 DISubprogram SP((*AI)->getScopeNode());
3000 if (SP.Verify()) {
3001 // Collect info for variables that were optimized out.
3002 StringRef FName = SP.getLinkageName();
3003 if (FName.empty())
3004 FName = SP.getName();
Devang Patel364bf042010-11-10 22:19:21 +00003005 if (NamedMDNode *NMD =
3006 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
Devang Patel5c0f85c2010-06-25 22:07:34 +00003007 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00003008 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel5c0f85c2010-06-25 22:07:34 +00003009 if (!DV || !ProcessedVars.insert(DV))
3010 continue;
Devang Patel648df7b2010-06-30 00:11:08 +00003011 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel5c0f85c2010-06-25 22:07:34 +00003012 if (Scope)
3013 Scope->addVariable(new DbgVariable(DV));
3014 }
3015 }
3016 }
Devang Patelc5b31092010-06-30 01:40:11 +00003017 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
3018 constructScopeDIE(*AI);
Devang Patel5c0f85c2010-06-25 22:07:34 +00003019 }
Devang Patel30265c42010-07-07 20:12:52 +00003020
Devang Patel075e9b52010-05-04 06:15:30 +00003021 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003022
Devang Patel075e9b52010-05-04 06:15:30 +00003023 if (!DisableFramePointerElim(*MF))
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003024 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
Devang Patel075e9b52010-05-04 06:15:30 +00003025 dwarf::DW_FORM_flag, 1);
3026
3027
Chris Lattner3a383cb2010-04-05 00:13:49 +00003028 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel530a0752010-01-04 20:44:00 +00003029 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00003030 }
3031
Bill Wendling2b128d72009-05-20 23:19:06 +00003032 // Clear debug info
Devang Patelfe189e62010-01-19 01:26:02 +00003033 CurrentFnDbgScope = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +00003034 CurrentFnArguments.clear();
Devang Patele1c53f22010-05-20 16:36:41 +00003035 DbgVariableToFrameIndexMap.clear();
3036 VarToAbstractVarMap.clear();
3037 DbgVariableToDbgInstMap.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00003038 DeleteContainerSeconds(DbgScopeMap);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00003039 UserVariables.clear();
3040 DbgValues.clear();
Devang Patelfe189e62010-01-19 01:26:02 +00003041 ConcreteScopes.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00003042 DeleteContainerSeconds(AbstractScopes);
Devang Patelfe189e62010-01-19 01:26:02 +00003043 AbstractScopesList.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00003044 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00003045 LabelsBeforeInsn.clear();
3046 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00003047 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00003048}
3049
Devang Patele1c53f22010-05-20 16:36:41 +00003050/// recordVariableFrameIndex - Record a variable's index.
3051void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
3052 assert (V && "Invalid DbgVariable!");
3053 DbgVariableToFrameIndexMap[V] = Index;
3054}
3055
3056/// findVariableFrameIndex - Return true if frame index for the variable
3057/// is found. Update FI to hold value of the index.
3058bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
3059 assert (V && "Invalid DbgVariable!");
3060 DenseMap<const DbgVariable *, int>::iterator I =
3061 DbgVariableToFrameIndexMap.find(V);
3062 if (I == DbgVariableToFrameIndexMap.end())
3063 return false;
3064 *FI = I->second;
3065 return true;
3066}
3067
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003068/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patel490c8ab2010-05-20 19:57:06 +00003069/// instruction.
3070DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
3071 DbgScope *Scope = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003072 LLVMContext &Ctx =
Devang Patel490c8ab2010-05-20 19:57:06 +00003073 MInsn->getParent()->getParent()->getFunction()->getContext();
3074 DebugLoc DL = MInsn->getDebugLoc();
3075
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003076 if (DL.isUnknown())
Devang Patel490c8ab2010-05-20 19:57:06 +00003077 return Scope;
3078
3079 if (const MDNode *IA = DL.getInlinedAt(Ctx))
3080 Scope = ConcreteScopes.lookup(IA);
3081 if (Scope == 0)
3082 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003083
Devang Patel490c8ab2010-05-20 19:57:06 +00003084 return Scope;
3085}
3086
3087
Chris Lattnerba35a672010-03-09 04:54:43 +00003088/// recordSourceLine - Register a source line with debug info. Returns the
3089/// unique label that was emitted and which provides correspondence to
3090/// the source line list.
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00003091void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S){
Devang Patel2d9caf92009-11-25 17:36:49 +00003092 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00003093 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00003094 unsigned Src = 1;
3095 if (S) {
3096 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00003097
Dan Gohman50849c62010-05-05 23:41:32 +00003098 if (Scope.isCompileUnit()) {
3099 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00003100 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00003101 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00003102 } else if (Scope.isFile()) {
3103 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00003104 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00003105 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00003106 } else if (Scope.isSubprogram()) {
3107 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00003108 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00003109 Dir = SP.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00003110 } else if (Scope.isLexicalBlock()) {
3111 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00003112 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00003113 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00003114 } else
3115 assert(0 && "Unexpected scope info");
3116
Devang Patele01b75c2011-03-24 20:30:50 +00003117 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00003118 }
3119
Rafael Espindola67c6ab82010-11-18 02:04:25 +00003120 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, DWARF2_FLAG_IS_STMT,
3121 0, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +00003122}
3123
Bill Wendling806535f2009-05-20 23:22:40 +00003124//===----------------------------------------------------------------------===//
3125// Emit Methods
3126//===----------------------------------------------------------------------===//
3127
Devang Patel930143b2009-11-21 02:48:08 +00003128/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00003129///
Jim Grosbach00e9c612009-11-22 19:20:36 +00003130unsigned
3131DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00003132 // Get the children.
3133 const std::vector<DIE *> &Children = Die->getChildren();
3134
3135 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00003136 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00003137 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00003138
3139 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00003140 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00003141
3142 // Get the abbreviation for this DIE.
3143 unsigned AbbrevNumber = Die->getAbbrevNumber();
3144 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3145
3146 // Set DIE offset
3147 Die->setOffset(Offset);
3148
3149 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00003150 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00003151
3152 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
3153 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3154
3155 // Size the DIE attribute values.
3156 for (unsigned i = 0, N = Values.size(); i < N; ++i)
3157 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00003158 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00003159
3160 // Size the DIE children if any.
3161 if (!Children.empty()) {
3162 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
3163 "Children flag not set");
3164
3165 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00003166 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00003167
3168 // End of children marker.
3169 Offset += sizeof(int8_t);
3170 }
3171
3172 Die->setSize(Offset - Die->getOffset());
3173 return Offset;
3174}
3175
Devang Patel930143b2009-11-21 02:48:08 +00003176/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00003177///
Devang Patel930143b2009-11-21 02:48:08 +00003178void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003179 unsigned PrevOffset = 0;
3180 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3181 E = CUMap.end(); I != E; ++I) {
3182 // Compute size of compile unit header.
3183 static unsigned Offset = PrevOffset +
3184 sizeof(int32_t) + // Length of Compilation Unit Info
3185 sizeof(int16_t) + // DWARF version number
3186 sizeof(int32_t) + // Offset Into Abbrev. Section
3187 sizeof(int8_t); // Pointer Size (in bytes)
3188 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
3189 PrevOffset = Offset;
3190 }
Bill Wendling480ff322009-05-20 23:21:38 +00003191}
3192
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003193/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
3194/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00003195static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003196 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00003197 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003198 if (!SymbolStem) return 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003199
Chris Lattner6629ca92010-04-04 22:59:04 +00003200 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
3201 Asm->OutStreamer.EmitLabel(TmpSym);
3202 return TmpSym;
3203}
3204
3205/// EmitSectionLabels - Emit initial Dwarf sections with a label at
3206/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00003207void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003208 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00003209
Bill Wendling480ff322009-05-20 23:21:38 +00003210 // Dwarf sections base addresses.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003211 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6629ca92010-04-04 22:59:04 +00003212 DwarfFrameSectionSym =
3213 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3214 }
Bill Wendling480ff322009-05-20 23:21:38 +00003215
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003216 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00003217 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003218 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00003219 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003220 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003221
Chris Lattner6629ca92010-04-04 22:59:04 +00003222 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003223 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003224
Devang Patel4a213872010-08-24 00:06:12 +00003225 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003226 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3227 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3228 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003229 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00003230 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00003231 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3232 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00003233
Devang Patel9fc11702010-05-25 23:40:22 +00003234 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3235 "section_debug_loc");
3236
Chris Lattner6629ca92010-04-04 22:59:04 +00003237 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00003238 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003239}
3240
Devang Patel930143b2009-11-21 02:48:08 +00003241/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00003242///
Devang Patel930143b2009-11-21 02:48:08 +00003243void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00003244 // Get the abbreviation for this DIE.
3245 unsigned AbbrevNumber = Die->getAbbrevNumber();
3246 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3247
Bill Wendling480ff322009-05-20 23:21:38 +00003248 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00003249 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00003250 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3251 Twine::utohexstr(Die->getOffset()) + ":0x" +
3252 Twine::utohexstr(Die->getSize()) + " " +
3253 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00003254 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00003255
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00003256 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00003257 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3258
3259 // Emit the DIE attribute values.
3260 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3261 unsigned Attr = AbbrevData[i].getAttribute();
3262 unsigned Form = AbbrevData[i].getForm();
3263 assert(Form && "Too many attributes for DIE (check abbreviation)");
3264
Chris Lattner7bde8c02010-04-04 18:52:31 +00003265 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00003266 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003267
Bill Wendling480ff322009-05-20 23:21:38 +00003268 switch (Attr) {
3269 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00003270 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003271 break;
3272 case dwarf::DW_AT_abstract_origin: {
3273 DIEEntry *E = cast<DIEEntry>(Values[i]);
3274 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003275 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00003276 Asm->EmitInt32(Addr);
3277 break;
3278 }
Devang Patel12563b32010-04-16 23:33:45 +00003279 case dwarf::DW_AT_ranges: {
3280 // DW_AT_range Value encodes offset in debug_range section.
3281 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00003282
3283 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
3284 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
3285 V->getValue(),
3286 4);
3287 } else {
3288 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3289 V->getValue(),
3290 DwarfDebugRangeSectionSym,
3291 4);
3292 }
Devang Patel12563b32010-04-16 23:33:45 +00003293 break;
3294 }
Devang Patel9fc11702010-05-25 23:40:22 +00003295 case dwarf::DW_AT_location: {
3296 if (UseDotDebugLocEntry.count(Die) != 0) {
3297 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbarfd95b012011-03-16 22:16:39 +00003298 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel9fc11702010-05-25 23:40:22 +00003299 } else
3300 Values[i]->EmitValue(Asm, Form);
3301 break;
3302 }
Devang Patela1bd5a12010-09-29 19:08:08 +00003303 case dwarf::DW_AT_accessibility: {
3304 if (Asm->isVerbose()) {
3305 DIEInteger *V = cast<DIEInteger>(Values[i]);
3306 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
3307 }
3308 Values[i]->EmitValue(Asm, Form);
3309 break;
3310 }
Bill Wendling480ff322009-05-20 23:21:38 +00003311 default:
3312 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003313 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00003314 break;
3315 }
Bill Wendling480ff322009-05-20 23:21:38 +00003316 }
3317
3318 // Emit the DIE children if any.
3319 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3320 const std::vector<DIE *> &Children = Die->getChildren();
3321
3322 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00003323 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00003324
Chris Lattner7bde8c02010-04-04 18:52:31 +00003325 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00003326 Asm->OutStreamer.AddComment("End Of Children Mark");
3327 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003328 }
3329}
3330
Devang Patel9ccfb642009-12-09 18:24:21 +00003331/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00003332///
Devang Patel9ccfb642009-12-09 18:24:21 +00003333void DwarfDebug::emitDebugInfo() {
3334 // Start debug info section.
3335 Asm->OutStreamer.SwitchSection(
3336 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00003337 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3338 E = CUMap.end(); I != E; ++I) {
3339 CompileUnit *TheCU = I->second;
3340 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003341
Devang Patel1a0df9a2010-05-10 22:49:55 +00003342 // Emit the compile units header.
3343 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3344 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003345
Devang Patel1a0df9a2010-05-10 22:49:55 +00003346 // Emit size of content not including length itself
3347 unsigned ContentSize = Die->getSize() +
3348 sizeof(int16_t) + // DWARF version number
3349 sizeof(int32_t) + // Offset Into Abbrev. Section
3350 sizeof(int8_t) + // Pointer Size (in bytes)
3351 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003352
Devang Patel1a0df9a2010-05-10 22:49:55 +00003353 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3354 Asm->EmitInt32(ContentSize);
3355 Asm->OutStreamer.AddComment("DWARF version number");
3356 Asm->EmitInt16(dwarf::DWARF_VERSION);
3357 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3358 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3359 DwarfAbbrevSectionSym);
3360 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3361 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003362
Devang Patel1a0df9a2010-05-10 22:49:55 +00003363 emitDIE(Die);
3364 // FIXME - extra padding for gdb bug.
3365 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3366 Asm->EmitInt8(0);
3367 Asm->EmitInt8(0);
3368 Asm->EmitInt8(0);
3369 Asm->EmitInt8(0);
3370 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3371 }
Bill Wendling480ff322009-05-20 23:21:38 +00003372}
3373
Devang Patel930143b2009-11-21 02:48:08 +00003374/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00003375///
Devang Patel930143b2009-11-21 02:48:08 +00003376void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00003377 // Check to see if it is worth the effort.
3378 if (!Abbreviations.empty()) {
3379 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003380 Asm->OutStreamer.SwitchSection(
3381 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003382
Chris Lattnera179b522010-04-04 19:25:43 +00003383 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003384
3385 // For each abbrevation.
3386 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3387 // Get abbreviation data
3388 const DIEAbbrev *Abbrev = Abbreviations[i];
3389
3390 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00003391 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00003392
3393 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003394 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00003395 }
3396
3397 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00003398 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00003399
Chris Lattnera179b522010-04-04 19:25:43 +00003400 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003401 }
3402}
3403
Devang Patel930143b2009-11-21 02:48:08 +00003404/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00003405/// the line matrix.
3406///
Devang Patel930143b2009-11-21 02:48:08 +00003407void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00003408 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00003409 Asm->OutStreamer.AddComment("Extended Op");
3410 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003411
Chris Lattner566cae92010-03-09 23:52:58 +00003412 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003413 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00003414 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3415 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3416
3417 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00003418
Chris Lattnera179b522010-04-04 19:25:43 +00003419 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003420 Asm->getTargetData().getPointerSize(),
3421 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003422
3423 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00003424 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3425 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003426 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00003427 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003428}
3429
Devang Patel930143b2009-11-21 02:48:08 +00003430/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling480ff322009-05-20 23:21:38 +00003431///
Devang Patel930143b2009-11-21 02:48:08 +00003432void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003433 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003434 return;
3435
Chris Lattner3a383cb2010-04-05 00:13:49 +00003436 int stackGrowth = Asm->getTargetData().getPointerSize();
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003437 if (Asm->TM.getFrameLowering()->getStackGrowthDirection() ==
3438 TargetFrameLowering::StackGrowsDown)
Chris Lattner3a383cb2010-04-05 00:13:49 +00003439 stackGrowth *= -1;
Bill Wendling480ff322009-05-20 23:21:38 +00003440
3441 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003442 Asm->OutStreamer.SwitchSection(
3443 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003444
Chris Lattnera179b522010-04-04 19:25:43 +00003445 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner566cae92010-03-09 23:52:58 +00003446 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003447 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3448 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003449
Chris Lattnera179b522010-04-04 19:25:43 +00003450 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner566cae92010-03-09 23:52:58 +00003451 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling480ff322009-05-20 23:21:38 +00003452 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner566cae92010-03-09 23:52:58 +00003453 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling480ff322009-05-20 23:21:38 +00003454 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner566cae92010-03-09 23:52:58 +00003455 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003456 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner9efd1182010-04-04 19:09:29 +00003457 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3458 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner566cae92010-03-09 23:52:58 +00003459 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003460 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003461 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Bill Wendling480ff322009-05-20 23:21:38 +00003462 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling480ff322009-05-20 23:21:38 +00003463
3464 std::vector<MachineMove> Moves;
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00003465 TFI->getInitialFrameState(Moves);
Bill Wendling480ff322009-05-20 23:21:38 +00003466
Chris Lattneraabc6042010-04-04 23:41:46 +00003467 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003468
Chris Lattner9e06e532010-04-28 01:05:45 +00003469 Asm->EmitAlignment(2);
Chris Lattnera179b522010-04-04 19:25:43 +00003470 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003471}
3472
Devang Patel930143b2009-11-21 02:48:08 +00003473/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling480ff322009-05-20 23:21:38 +00003474/// section.
Chris Lattner2c3f4782010-03-13 07:26:18 +00003475void DwarfDebug::
3476emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003477 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003478 return;
3479
3480 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003481 Asm->OutStreamer.SwitchSection(
3482 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003483
Chris Lattner566cae92010-03-09 23:52:58 +00003484 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner2c3f4782010-03-13 07:26:18 +00003485 MCSymbol *DebugFrameBegin =
Chris Lattnera179b522010-04-04 19:25:43 +00003486 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003487 MCSymbol *DebugFrameEnd =
Chris Lattnera179b522010-04-04 19:25:43 +00003488 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerf1429f12010-04-04 19:58:12 +00003489 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003490
Chris Lattner2c3f4782010-03-13 07:26:18 +00003491 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling480ff322009-05-20 23:21:38 +00003492
Chris Lattner566cae92010-03-09 23:52:58 +00003493 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003494 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner70a4fce2010-04-04 23:25:33 +00003495 DwarfFrameSectionSym);
Bill Wendling480ff322009-05-20 23:21:38 +00003496
Chris Lattner566cae92010-03-09 23:52:58 +00003497 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnera179b522010-04-04 19:25:43 +00003498 MCSymbol *FuncBeginSym =
3499 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner8811e122010-03-13 07:40:56 +00003500 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattner3a383cb2010-04-05 00:13:49 +00003501 Asm->getTargetData().getPointerSize(),
3502 0/*AddrSpace*/);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003503
3504
Chris Lattner566cae92010-03-09 23:52:58 +00003505 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003506 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003507 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003508
Chris Lattneraabc6042010-04-04 23:41:46 +00003509 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003510
Chris Lattner9e06e532010-04-28 01:05:45 +00003511 Asm->EmitAlignment(2);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003512 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling480ff322009-05-20 23:21:38 +00003513}
3514
Devang Patel9ccfb642009-12-09 18:24:21 +00003515/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3516///
3517void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003518 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3519 E = CUMap.end(); I != E; ++I) {
3520 CompileUnit *TheCU = I->second;
3521 // Start the dwarf pubnames section.
3522 Asm->OutStreamer.SwitchSection(
3523 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003524
Devang Patel1a0df9a2010-05-10 22:49:55 +00003525 Asm->OutStreamer.AddComment("Length of Public Names Info");
3526 Asm->EmitLabelDifference(
3527 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3528 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003529
Devang Patel1a0df9a2010-05-10 22:49:55 +00003530 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3531 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003532
Devang Patel1a0df9a2010-05-10 22:49:55 +00003533 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003534 Asm->EmitInt16(dwarf::DWARF_VERSION);
3535
Devang Patel1a0df9a2010-05-10 22:49:55 +00003536 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003537 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00003538 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003539
Devang Patel1a0df9a2010-05-10 22:49:55 +00003540 Asm->OutStreamer.AddComment("Compilation Unit Length");
3541 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3542 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3543 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003544
Devang Patel1a0df9a2010-05-10 22:49:55 +00003545 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3546 for (StringMap<DIE*>::const_iterator
3547 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3548 const char *Name = GI->getKeyData();
3549 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003550
Devang Patel1a0df9a2010-05-10 22:49:55 +00003551 Asm->OutStreamer.AddComment("DIE offset");
3552 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003553
Devang Patel1a0df9a2010-05-10 22:49:55 +00003554 if (Asm->isVerbose())
3555 Asm->OutStreamer.AddComment("External Name");
3556 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3557 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003558
Devang Patel1a0df9a2010-05-10 22:49:55 +00003559 Asm->OutStreamer.AddComment("End Mark");
3560 Asm->EmitInt32(0);
3561 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3562 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00003563 }
Bill Wendling480ff322009-05-20 23:21:38 +00003564}
3565
Devang Patel04d2f2d2009-11-24 01:14:22 +00003566void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003567 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3568 E = CUMap.end(); I != E; ++I) {
3569 CompileUnit *TheCU = I->second;
3570 // Start the dwarf pubnames section.
3571 Asm->OutStreamer.SwitchSection(
3572 Asm->getObjFileLowering().getDwarfPubTypesSection());
3573 Asm->OutStreamer.AddComment("Length of Public Types Info");
3574 Asm->EmitLabelDifference(
3575 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3576 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003577
Devang Patel1a0df9a2010-05-10 22:49:55 +00003578 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3579 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003580
Devang Patel1a0df9a2010-05-10 22:49:55 +00003581 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3582 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003583
Devang Patel1a0df9a2010-05-10 22:49:55 +00003584 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3585 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3586 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003587
Devang Patel1a0df9a2010-05-10 22:49:55 +00003588 Asm->OutStreamer.AddComment("Compilation Unit Length");
3589 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3590 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3591 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003592
Devang Patel1a0df9a2010-05-10 22:49:55 +00003593 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3594 for (StringMap<DIE*>::const_iterator
3595 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3596 const char *Name = GI->getKeyData();
3597 DIE * Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003598
Devang Patel1a0df9a2010-05-10 22:49:55 +00003599 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3600 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003601
Devang Patel1a0df9a2010-05-10 22:49:55 +00003602 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3603 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3604 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003605
Devang Patel1a0df9a2010-05-10 22:49:55 +00003606 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003607 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00003608 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3609 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00003610 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00003611}
3612
Devang Patel930143b2009-11-21 02:48:08 +00003613/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00003614///
Devang Patel930143b2009-11-21 02:48:08 +00003615void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00003616 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00003617 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003618
Chris Lattner3d72a672010-03-09 23:38:23 +00003619 // Start the dwarf str section.
3620 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003621 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003622
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003623 // Get all of the string pool entries and put them in an array by their ID so
3624 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003625 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003626 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003627
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003628 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3629 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3630 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003631
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003632 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003633
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003634 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00003635 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003636 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003637
Chris Lattner3d72a672010-03-09 23:38:23 +00003638 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003639 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003640 }
3641}
3642
Devang Patel930143b2009-11-21 02:48:08 +00003643/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00003644///
Devang Patel930143b2009-11-21 02:48:08 +00003645void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003646 if (DotDebugLocEntries.empty())
3647 return;
3648
Devang Patel116a9d72011-02-04 22:57:18 +00003649 for (SmallVector<DotDebugLocEntry, 4>::iterator
3650 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
3651 I != E; ++I) {
3652 DotDebugLocEntry &Entry = *I;
3653 if (I + 1 != DotDebugLocEntries.end())
3654 Entry.Merge(I+1);
3655 }
3656
Daniel Dunbarfd95b012011-03-16 22:16:39 +00003657 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003658 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00003659 Asm->getObjFileLowering().getDwarfLocSection());
3660 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003661 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3662 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003663 for (SmallVector<DotDebugLocEntry, 4>::iterator
3664 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00003665 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00003666 DotDebugLocEntry &Entry = *I;
3667 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00003668 if (Entry.isEmpty()) {
3669 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3670 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003671 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00003672 } else {
3673 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3674 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3675 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3676 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patel0e60e672010-08-04 18:40:52 +00003677 if (int Offset = Entry.Loc.getOffset()) {
3678 // If the value is at a certain offset from frame register then
3679 // use DW_OP_fbreg.
3680 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patel9fc11702010-05-25 23:40:22 +00003681 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patel0e60e672010-08-04 18:40:52 +00003682 Asm->EmitInt16(1 + OffsetSize);
3683 Asm->OutStreamer.AddComment(
3684 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
3685 Asm->EmitInt8(dwarf::DW_OP_fbreg);
3686 Asm->OutStreamer.AddComment("Offset");
3687 Asm->EmitSLEB128(Offset);
Devang Patel9fc11702010-05-25 23:40:22 +00003688 } else {
Devang Patel0e60e672010-08-04 18:40:52 +00003689 if (Reg < 32) {
3690 Asm->OutStreamer.AddComment("Loc expr size");
3691 Asm->EmitInt16(1);
3692 Asm->OutStreamer.AddComment(
Devang Patel6d21f612010-08-04 20:32:36 +00003693 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patel0e60e672010-08-04 18:40:52 +00003694 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3695 } else {
3696 Asm->OutStreamer.AddComment("Loc expr size");
3697 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
3698 Asm->EmitInt8(dwarf::DW_OP_regx);
3699 Asm->EmitULEB128(Reg);
3700 }
Devang Patel9fc11702010-05-25 23:40:22 +00003701 }
3702 }
3703 }
Bill Wendling480ff322009-05-20 23:21:38 +00003704}
3705
3706/// EmitDebugARanges - Emit visible names into a debug aranges section.
3707///
3708void DwarfDebug::EmitDebugARanges() {
3709 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003710 Asm->OutStreamer.SwitchSection(
3711 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003712}
3713
Devang Patel930143b2009-11-21 02:48:08 +00003714/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00003715///
Devang Patel930143b2009-11-21 02:48:08 +00003716void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00003717 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003718 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00003719 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00003720 unsigned char Size = Asm->getTargetData().getPointerSize();
3721 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003722 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00003723 I != E; ++I) {
3724 if (*I)
3725 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00003726 else
Devang Patel6c74a872010-04-27 19:46:33 +00003727 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00003728 }
Bill Wendling480ff322009-05-20 23:21:38 +00003729}
3730
Devang Patel930143b2009-11-21 02:48:08 +00003731/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00003732///
Devang Patel930143b2009-11-21 02:48:08 +00003733void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00003734 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00003735 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00003736 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003737 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003738 }
3739}
3740
Devang Patel930143b2009-11-21 02:48:08 +00003741/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00003742/// Section Header:
3743/// 1. length of section
3744/// 2. Dwarf version number
3745/// 3. address size.
3746///
3747/// Entries (one "entry" for each function that was inlined):
3748///
3749/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3750/// otherwise offset into __debug_str for regular function name.
3751/// 2. offset into __debug_str section for regular function name.
3752/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3753/// instances for the function.
3754///
3755/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3756/// inlined instance; the die_offset points to the inlined_subroutine die in the
3757/// __debug_info section, and the low_pc is the starting address for the
3758/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00003759void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003760 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003761 return;
3762
Devang Patel1a0df9a2010-05-10 22:49:55 +00003763 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00003764 return;
3765
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003766 Asm->OutStreamer.SwitchSection(
3767 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003768
Chris Lattner566cae92010-03-09 23:52:58 +00003769 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003770 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3771 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003772
Chris Lattnera179b522010-04-04 19:25:43 +00003773 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003774
Chris Lattner566cae92010-03-09 23:52:58 +00003775 Asm->OutStreamer.AddComment("Dwarf Version");
3776 Asm->EmitInt16(dwarf::DWARF_VERSION);
3777 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003778 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003779
Devang Patel32cc43c2010-05-07 20:54:48 +00003780 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003781 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00003782
Devang Patel32cc43c2010-05-07 20:54:48 +00003783 const MDNode *Node = *I;
3784 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00003785 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003786 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00003787 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00003788 StringRef LName = SP.getLinkageName();
3789 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00003790
Chris Lattner566cae92010-03-09 23:52:58 +00003791 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003792 if (LName.empty()) {
3793 Asm->OutStreamer.EmitBytes(Name, 0);
3794 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003795 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00003796 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3797 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003798
Chris Lattner566cae92010-03-09 23:52:58 +00003799 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00003800 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00003801 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00003802
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003803 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00003804 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00003805 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00003806 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003807
Chris Lattner7bde8c02010-04-04 18:52:31 +00003808 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003809 Asm->OutStreamer.EmitSymbolValue(LI->first,
3810 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00003811 }
3812 }
3813
Chris Lattnera179b522010-04-04 19:25:43 +00003814 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003815}