blob: d3e343c30879bbfe4a267d42c78bf209969212ad [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendling0310d762009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendling0310d762009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000032#include "llvm/Analysis/DebugInfo.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000033#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000034#include "llvm/ADT/StringExtras.h"
Devang Pateleac9c072010-04-27 19:46:33 +000035#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000038#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000040#include "llvm/Support/Timer.h"
41#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000042using namespace llvm;
43
Devang Pateleac9c072010-04-27 19:46:33 +000044static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
45 cl::desc("Print DbgScope information for each machine instruction"));
46
Jim Grosbach1e20b962010-07-21 21:21:52 +000047static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000048 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000049 cl::desc("Disable debug info printing"));
50
Dan Gohman281d65d2010-05-07 01:08:53 +000051static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
52 cl::desc("Make an absense of debug location information explicit."),
53 cl::init(false));
54
Bill Wendling5f017e82010-04-07 09:28:04 +000055namespace {
56 const char *DWARFGroupName = "DWARF Emission";
57 const char *DbgTimerName = "DWARF Debug Writer";
58} // end anonymous namespace
59
Bill Wendling0310d762009-05-15 09:23:25 +000060//===----------------------------------------------------------------------===//
61
62/// Configuration values for initial hash set sizes (log2).
63///
Bill Wendling0310d762009-05-15 09:23:25 +000064static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000065
66namespace llvm {
67
68//===----------------------------------------------------------------------===//
69/// CompileUnit - This dwarf writer support class manages information associate
70/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000071class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000072 /// ID - File identifier for source.
73 ///
74 unsigned ID;
75
76 /// Die - Compile unit debug information entry.
77 ///
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000078 const OwningPtr<DIE> CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000079
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +000080 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel6f01d9c2009-11-21 00:31:03 +000081 DIE *IndexTyDie;
82
Devang Patel869aa462010-07-07 20:49:57 +000083 /// MDNodeToDieMap - Tracks the mapping of unit level debug informaton
Bill Wendling0310d762009-05-15 09:23:25 +000084 /// variables to debug information entries.
Devang Patel869aa462010-07-07 20:49:57 +000085 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000086
Devang Patel869aa462010-07-07 20:49:57 +000087 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug informaton
Bill Wendling0310d762009-05-15 09:23:25 +000088 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel869aa462010-07-07 20:49:57 +000089 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000090
91 /// Globals - A map of globally visible named entities for this unit.
92 ///
93 StringMap<DIE*> Globals;
94
Devang Patel193f7202009-11-24 01:14:22 +000095 /// GlobalTypes - A map of globally visible types for this unit.
96 ///
97 StringMap<DIE*> GlobalTypes;
98
Bill Wendling0310d762009-05-15 09:23:25 +000099public:
100 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000101 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000102
103 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +0000104 unsigned getID() const { return ID; }
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +0000105 DIE* getCUDie() const { return CUDie.get(); }
Devang Patel193f7202009-11-24 01:14:22 +0000106 const StringMap<DIE*> &getGlobals() const { return Globals; }
107 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +0000108
109 /// hasContent - Return true if this compile unit has something to write out.
110 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000111 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000112
Devang Patel2c4ceb12009-11-21 02:48:08 +0000113 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000114 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000115 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +0000116
Devang Patel193f7202009-11-24 01:14:22 +0000117 /// addGlobalType - Add a new global type to the compile unit.
118 ///
Jim Grosbach1e20b962010-07-21 21:21:52 +0000119 void addGlobalType(StringRef Name, DIE *Die) {
120 GlobalTypes[Name] = Die;
Devang Patel193f7202009-11-24 01:14:22 +0000121 }
122
Devang Patel017d1212009-11-20 21:37:22 +0000123 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000124 /// specified debug variable.
Devang Patel869aa462010-07-07 20:49:57 +0000125 DIE *getDIE(const MDNode *N) { return MDNodeToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000126
Devang Patel017d1212009-11-20 21:37:22 +0000127 /// insertDIE - Insert DIE into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000128 void insertDIE(const MDNode *N, DIE *D) {
Devang Patel869aa462010-07-07 20:49:57 +0000129 MDNodeToDieMap.insert(std::make_pair(N, D));
Devang Patel017d1212009-11-20 21:37:22 +0000130 }
Bill Wendling0310d762009-05-15 09:23:25 +0000131
Devang Patel017d1212009-11-20 21:37:22 +0000132 /// getDIEEntry - Returns the debug information entry for the speciefied
133 /// debug variable.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000134 DIEEntry *getDIEEntry(const MDNode *N) {
135 DenseMap<const MDNode *, DIEEntry *>::iterator I =
136 MDNodeToDIEEntryMap.find(N);
Devang Patel869aa462010-07-07 20:49:57 +0000137 if (I == MDNodeToDIEEntryMap.end())
Devang Patel6404e4e2009-12-15 19:16:48 +0000138 return NULL;
139 return I->second;
140 }
Devang Patel017d1212009-11-20 21:37:22 +0000141
142 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000143 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Patel869aa462010-07-07 20:49:57 +0000144 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000145 }
146
Devang Patel2c4ceb12009-11-21 02:48:08 +0000147 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000148 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000149 void addDie(DIE *Buffer) {
150 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000151 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000152
153 // getIndexTyDie - Get an anonymous type for index type.
154 DIE *getIndexTyDie() {
155 return IndexTyDie;
156 }
157
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000158 // setIndexTyDie - Set D as anonymous type for index which can be reused
159 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000160 void setIndexTyDie(DIE *D) {
161 IndexTyDie = D;
162 }
163
Bill Wendling0310d762009-05-15 09:23:25 +0000164};
165
166//===----------------------------------------------------------------------===//
167/// DbgVariable - This class is used to track local variable information.
168///
Devang Patelf76a3d62009-11-16 21:53:40 +0000169class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000170 DIVariable Var; // Variable Descriptor.
Devang Patelc3f5f782010-05-25 23:40:22 +0000171 DIE *TheDIE; // Variable DIE.
172 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
Bill Wendling0310d762009-05-15 09:23:25 +0000173public:
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000174 // AbsVar may be NULL.
Devang Patelc3f5f782010-05-25 23:40:22 +0000175 DbgVariable(DIVariable V) : Var(V), TheDIE(0), DotDebugLocOffset(~0U) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000176
177 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000178 DIVariable getVariable() const { return Var; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000179 void setDIE(DIE *D) { TheDIE = D; }
180 DIE *getDIE() const { return TheDIE; }
Devang Patelc3f5f782010-05-25 23:40:22 +0000181 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
182 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
Devang Patel8bd11de2010-08-09 21:01:39 +0000183 StringRef getName() const { return Var.getName(); }
184 unsigned getTag() const { return Var.getTag(); }
185 bool variableHasComplexAddress() const {
186 assert(Var.Verify() && "Invalid complex DbgVariable!");
187 return Var.hasComplexAddress();
188 }
189 bool isBlockByrefVariable() const {
190 assert(Var.Verify() && "Invalid complex DbgVariable!");
191 return Var.isBlockByrefVariable();
192 }
193 unsigned getNumAddrElements() const {
194 assert(Var.Verify() && "Invalid complex DbgVariable!");
195 return Var.getNumAddrElements();
196 }
197 uint64_t getAddrElement(unsigned i) const {
198 return Var.getAddrElement(i);
199 }
200 DIType getType() const {
201 DIType Ty = Var.getType();
202 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
203 // addresses instead.
204 if (Var.isBlockByrefVariable()) {
205 /* Byref variables, in Blocks, are declared by the programmer as
206 "SomeType VarName;", but the compiler creates a
207 __Block_byref_x_VarName struct, and gives the variable VarName
208 either the struct, or a pointer to the struct, as its type. This
209 is necessary for various behind-the-scenes things the compiler
210 needs to do with by-reference variables in blocks.
211
212 However, as far as the original *programmer* is concerned, the
213 variable should still have type 'SomeType', as originally declared.
214
215 The following function dives into the __Block_byref_x_VarName
216 struct to find the original type of the variable. This will be
217 passed back to the code generating the type for the Debug
218 Information Entry for the variable 'VarName'. 'VarName' will then
219 have the original type 'SomeType' in its debug information.
220
221 The original type 'SomeType' will be the type of the field named
222 'VarName' inside the __Block_byref_x_VarName struct.
223
224 NOTE: In order for this to not completely fail on the debugger
225 side, the Debug Information Entry for the variable VarName needs to
226 have a DW_AT_location that tells the debugger how to unwind through
227 the pointers and __Block_byref_x_VarName struct to find the actual
228 value of the variable. The function addBlockByrefType does this. */
229 DIType subType = Ty;
230 unsigned tag = Ty.getTag();
231
232 if (tag == dwarf::DW_TAG_pointer_type) {
233 DIDerivedType DTy = DIDerivedType(Ty);
234 subType = DTy.getTypeDerivedFrom();
235 }
236
237 DICompositeType blockStruct = DICompositeType(subType);
238 DIArray Elements = blockStruct.getTypeArray();
239
240 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
241 DIDescriptor Element = Elements.getElement(i);
242 DIDerivedType DT = DIDerivedType(Element);
243 if (getName() == DT.getName())
244 return (DT.getTypeDerivedFrom());
245 }
246 return Ty;
247 }
248 return Ty;
249 }
Bill Wendling0310d762009-05-15 09:23:25 +0000250};
251
252//===----------------------------------------------------------------------===//
Devang Pateleac9c072010-04-27 19:46:33 +0000253/// DbgRange - This is used to track range of instructions with identical
254/// debug info scope.
255///
256typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
257
258//===----------------------------------------------------------------------===//
Bill Wendling0310d762009-05-15 09:23:25 +0000259/// DbgScope - This class is used to track scope information.
260///
Devang Patelf76a3d62009-11-16 21:53:40 +0000261class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000262 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000263 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000264 // Location at which this scope is inlined.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000265 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000266 bool AbstractScope; // Abstract Scope
Devang Pateld38dd112009-10-01 18:25:23 +0000267 const MachineInstr *LastInsn; // Last instruction of this scope.
268 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Pateleac9c072010-04-27 19:46:33 +0000269 unsigned DFSIn, DFSOut;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000270 // Scopes defined in scope. Contents not owned.
271 SmallVector<DbgScope *, 4> Scopes;
272 // Variables declared in scope. Contents owned.
273 SmallVector<DbgVariable *, 8> Variables;
Devang Pateleac9c072010-04-27 19:46:33 +0000274 SmallVector<DbgRange, 4> Ranges;
Owen Anderson04c05f72009-06-24 22:53:20 +0000275 // Private state for dump()
276 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000277public:
Devang Patele9f8f5e2010-05-07 20:54:48 +0000278 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000279 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Pateleac9c072010-04-27 19:46:33 +0000280 LastInsn(0), FirstInsn(0),
281 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000282 virtual ~DbgScope();
283
284 // Accessors.
285 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000286 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000287 DIDescriptor getDesc() const { return Desc; }
Devang Patele9f8f5e2010-05-07 20:54:48 +0000288 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
289 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000290 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
Devang Patele03161c2010-08-09 18:51:29 +0000291 const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
Devang Pateleac9c072010-04-27 19:46:33 +0000292 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
293
294 /// openInsnRange - This scope covers instruction range starting from MI.
295 void openInsnRange(const MachineInstr *MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000296 if (!FirstInsn)
Devang Pateleac9c072010-04-27 19:46:33 +0000297 FirstInsn = MI;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000298
Devang Pateleac9c072010-04-27 19:46:33 +0000299 if (Parent)
300 Parent->openInsnRange(MI);
301 }
302
Jim Grosbach1e20b962010-07-21 21:21:52 +0000303 /// extendInsnRange - Extend the current instruction range covered by
Devang Pateleac9c072010-04-27 19:46:33 +0000304 /// this scope.
305 void extendInsnRange(const MachineInstr *MI) {
306 assert (FirstInsn && "MI Range is not open!");
307 LastInsn = MI;
308 if (Parent)
309 Parent->extendInsnRange(MI);
310 }
311
312 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
313 /// until now. This is used when a new scope is encountered while walking
314 /// machine instructions.
315 void closeInsnRange(DbgScope *NewScope = NULL) {
316 assert (LastInsn && "Last insn missing!");
317 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000318 FirstInsn = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +0000319 LastInsn = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000320 // If Parent dominates NewScope then do not close Parent's instruction
Devang Pateleac9c072010-04-27 19:46:33 +0000321 // range.
322 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
323 Parent->closeInsnRange(NewScope);
324 }
325
Devang Patel53bb5c92009-11-10 23:06:00 +0000326 void setAbstractScope() { AbstractScope = true; }
327 bool isAbstractScope() const { return AbstractScope; }
Devang Pateleac9c072010-04-27 19:46:33 +0000328
329 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
330 unsigned getDFSOut() const { return DFSOut; }
331 void setDFSOut(unsigned O) { DFSOut = O; }
332 unsigned getDFSIn() const { return DFSIn; }
333 void setDFSIn(unsigned I) { DFSIn = I; }
334 bool dominates(const DbgScope *S) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000335 if (S == this)
Devang Pateleac9c072010-04-27 19:46:33 +0000336 return true;
337 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
338 return true;
339 return false;
340 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000341
Devang Patel2c4ceb12009-11-21 02:48:08 +0000342 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000343 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000344 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000345
Devang Patel2c4ceb12009-11-21 02:48:08 +0000346 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000347 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000348 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000349
Bill Wendling0310d762009-05-15 09:23:25 +0000350#ifndef NDEBUG
351 void dump() const;
352#endif
353};
Devang Pateleac9c072010-04-27 19:46:33 +0000354
Chris Lattnerea761862010-04-05 04:09:20 +0000355} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000356
357#ifndef NDEBUG
358void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000359 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000360 err.indent(IndentLevel);
Devang Patele9f8f5e2010-05-07 20:54:48 +0000361 const MDNode *N = Desc;
Devang Patel53bb5c92009-11-10 23:06:00 +0000362 N->dump();
Devang Patel53bb5c92009-11-10 23:06:00 +0000363 if (AbstractScope)
364 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000365
366 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000367 if (!Scopes.empty())
368 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000369 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
370 if (Scopes[i] != this)
371 Scopes[i]->dump();
372
373 IndentLevel -= 2;
374}
375#endif
376
Bill Wendling0310d762009-05-15 09:23:25 +0000377DbgScope::~DbgScope() {
Bill Wendling0310d762009-05-15 09:23:25 +0000378 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
379 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000380}
381
Chris Lattner49cd6642010-04-05 05:11:15 +0000382DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000383 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000384 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelf2548ca2010-04-16 23:33:45 +0000385 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000386 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000387
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000388 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
389 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000390 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000391 FunctionBeginSym = FunctionEndSym = 0;
Devang Patelca76f6f2010-07-08 20:10:35 +0000392 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Dan Gohman03c3dc72010-06-18 15:56:31 +0000393 {
394 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
395 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000396 }
Bill Wendling0310d762009-05-15 09:23:25 +0000397}
398DwarfDebug::~DwarfDebug() {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000399 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
400 DIEBlocks[j]->~DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000401}
402
Chris Lattnerbc733f52010-03-13 02:17:42 +0000403MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
404 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
405 if (Entry.first) return Entry.first;
406
407 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000408 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000409}
410
411
Devang Patel2c4ceb12009-11-21 02:48:08 +0000412/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000413///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000414void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000415 // Profile the node so that we can make it unique.
416 FoldingSetNodeID ID;
417 Abbrev.Profile(ID);
418
419 // Check the set for priors.
420 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
421
422 // If it's newly added.
423 if (InSet == &Abbrev) {
424 // Add to abbreviation list.
425 Abbreviations.push_back(&Abbrev);
426
427 // Assign the vector position + 1 as its number.
428 Abbrev.setNumber(Abbreviations.size());
429 } else {
430 // Assign existing abbreviation number.
431 Abbrev.setNumber(InSet->getNumber());
432 }
433}
434
Devang Patel2c4ceb12009-11-21 02:48:08 +0000435/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000436/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000437DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000438 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000439 return Value;
440}
441
Devang Patel2c4ceb12009-11-21 02:48:08 +0000442/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000443///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000444void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000445 unsigned Form, uint64_t Integer) {
446 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000447 DIEValue *Value = Integer == 1 ?
Devang Patelca76f6f2010-07-08 20:10:35 +0000448 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000449 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000450}
451
Devang Patel2c4ceb12009-11-21 02:48:08 +0000452/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000453///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000454void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000455 unsigned Form, int64_t Integer) {
456 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000457 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000458 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000459}
460
Devang Patel69f57b12009-12-02 15:25:16 +0000461/// addString - Add a string attribute data and value. DIEString only
Jim Grosbach1e20b962010-07-21 21:21:52 +0000462/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000463void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner4cf202b2010-01-23 03:11:46 +0000464 StringRef String) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000465 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000466 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000467}
468
Devang Patel2c4ceb12009-11-21 02:48:08 +0000469/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000470///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000471void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000472 const MCSymbol *Label) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000473 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000474 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000475}
476
Devang Patel2c4ceb12009-11-21 02:48:08 +0000477/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000478///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000479void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000480 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000481 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000482 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000483}
484
Chris Lattner74e41f92010-04-05 05:24:55 +0000485/// addDIEEntry - Add a DIE attribute data and value.
486///
487void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
488 DIE *Entry) {
489 Die->addValue(Attribute, Form, createDIEEntry(Entry));
490}
491
492
Devang Patel2c4ceb12009-11-21 02:48:08 +0000493/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000494///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000495void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000496 DIEBlock *Block) {
Chris Lattnera37d5382010-04-05 00:18:22 +0000497 Block->ComputeSize(Asm);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000498 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000499 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000500}
501
Devang Patel2c4ceb12009-11-21 02:48:08 +0000502/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000503/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000504void DwarfDebug::addSourceLine(DIE *Die, DIVariable V) {
Devang Patelc665a512010-05-07 23:33:41 +0000505 // Verify variable.
Devang Patel81625742010-08-09 20:20:05 +0000506 if (!V.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000507 return;
508
Devang Patel81625742010-08-09 20:20:05 +0000509 unsigned Line = V.getLineNumber();
510 unsigned FileID = GetOrCreateSourceID(V.getContext().getDirectory(),
511 V.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000512 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000513 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
514 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000515}
516
Devang Patel2c4ceb12009-11-21 02:48:08 +0000517/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000518/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000519void DwarfDebug::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patelc665a512010-05-07 23:33:41 +0000520 // Verify global variable.
Devang Patel81625742010-08-09 20:20:05 +0000521 if (!G.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000522 return;
523
Devang Patel81625742010-08-09 20:20:05 +0000524 unsigned Line = G.getLineNumber();
525 unsigned FileID = GetOrCreateSourceID(G.getContext().getDirectory(),
526 G.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000527 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000528 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
529 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000530}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000531
Devang Patel2c4ceb12009-11-21 02:48:08 +0000532/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000533/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000534void DwarfDebug::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patelc665a512010-05-07 23:33:41 +0000535 // Verify subprogram.
Devang Patel81625742010-08-09 20:20:05 +0000536 if (!SP.Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000537 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000538 // If the line number is 0, don't add it.
Devang Patel81625742010-08-09 20:20:05 +0000539 if (SP.getLineNumber() == 0)
Caroline Ticec6f9d622009-09-11 18:25:54 +0000540 return;
541
Devang Patel81625742010-08-09 20:20:05 +0000542 unsigned Line = SP.getLineNumber();
543 if (!SP.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000544 return;
Devang Patel81625742010-08-09 20:20:05 +0000545 unsigned FileID = GetOrCreateSourceID(SP.getDirectory(),
546 SP.getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000547 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000548 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
549 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000550}
551
Devang Patel2c4ceb12009-11-21 02:48:08 +0000552/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000553/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000554void DwarfDebug::addSourceLine(DIE *Die, DIType Ty) {
Devang Patelc665a512010-05-07 23:33:41 +0000555 // Verify type.
Devang Patel81625742010-08-09 20:20:05 +0000556 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000557 return;
558
Devang Patel81625742010-08-09 20:20:05 +0000559 unsigned Line = Ty.getLineNumber();
560 if (!Ty.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000561 return;
Devang Patel81625742010-08-09 20:20:05 +0000562 unsigned FileID = GetOrCreateSourceID(Ty.getContext().getDirectory(),
563 Ty.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000564 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000565 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
566 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000567}
568
Devang Patel6404e4e2009-12-15 19:16:48 +0000569/// addSourceLine - Add location information to specified debug information
570/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000571void DwarfDebug::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patelc665a512010-05-07 23:33:41 +0000572 // Verify namespace.
Devang Patel81625742010-08-09 20:20:05 +0000573 if (!NS.Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000574 return;
575
Devang Patel81625742010-08-09 20:20:05 +0000576 unsigned Line = NS.getLineNumber();
577 StringRef FN = NS.getFilename();
578 StringRef Dir = NS.getDirectory();
Devang Patel6404e4e2009-12-15 19:16:48 +0000579
580 unsigned FileID = GetOrCreateSourceID(Dir, FN);
581 assert(FileID && "Invalid file id");
582 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
583 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
584}
585
Devang Patel9e3bd2c2010-08-31 06:11:28 +0000586/// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
587/// on provided frame index.
588void DwarfDebug::addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI) {
589 MachineLocation Location;
590 unsigned FrameReg;
591 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
592 int Offset = RI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
593 Location.set(FrameReg, Offset);
594
Devang Patel8bd11de2010-08-09 21:01:39 +0000595 if (DV->variableHasComplexAddress())
596 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
597 else if (DV->isBlockByrefVariable())
598 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
599 else
600 addAddress(Die, dwarf::DW_AT_location, Location);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000601}
602
Devang Patel2c4ceb12009-11-21 02:48:08 +0000603/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000604/// and generate the DWARF information necessary to find the actual variable
605/// given the extra address information encoded in the DIVariable, starting from
606/// the starting location. Add the DWARF information to the die.
607///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000608void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000609 unsigned Attribute,
610 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000611 DIType Ty = DV->getType();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000612
613 // Decode the original location, and use that as the start of the byref
614 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000615 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000616 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000617 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000618
619 if (Location.isReg()) {
620 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000621 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000622 } else {
623 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000624 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
625 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000626 }
627 } else {
628 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000629 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000630 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000631 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
632 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000633 }
634
Devang Patel2c4ceb12009-11-21 02:48:08 +0000635 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000636 }
637
Devang Patel8bd11de2010-08-09 21:01:39 +0000638 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
639 uint64_t Element = DV->getAddrElement(i);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000640
641 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000642 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel8bd11de2010-08-09 21:01:39 +0000643 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000644 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000645 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000646 } else llvm_unreachable("unknown DIFactory Opcode");
647 }
648
649 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000650 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000651}
652
Caroline Ticedc8f6042009-08-31 21:19:37 +0000653/* Byref variables, in Blocks, are declared by the programmer as "SomeType
654 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
655 gives the variable VarName either the struct, or a pointer to the struct, as
656 its type. This is necessary for various behind-the-scenes things the
657 compiler needs to do with by-reference variables in Blocks.
658
659 However, as far as the original *programmer* is concerned, the variable
660 should still have type 'SomeType', as originally declared.
661
Devang Patel2c4ceb12009-11-21 02:48:08 +0000662 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000663 struct to find the original type of the variable, which is then assigned to
664 the variable's Debug Information Entry as its real type. So far, so good.
665 However now the debugger will expect the variable VarName to have the type
666 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000667 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000668 pointers and struct to find the actual variable of type SomeType.
669
670 The following function does just that. We start by getting
671 the "normal" location for the variable. This will be the location
672 of either the struct __Block_byref_x_VarName or the pointer to the
673 struct __Block_byref_x_VarName.
674
675 The struct will look something like:
676
677 struct __Block_byref_x_VarName {
678 ... <various fields>
679 struct __Block_byref_x_VarName *forwarding;
680 ... <various other fields>
681 SomeType VarName;
682 ... <maybe more fields>
683 };
684
685 If we are given the struct directly (as our starting point) we
686 need to tell the debugger to:
687
688 1). Add the offset of the forwarding field.
689
Dan Gohmanf451cb82010-02-10 16:03:48 +0000690 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000691 struct to use (the real one may have been copied onto the heap).
692
693 3). Add the offset for the field VarName, to find the actual variable.
694
695 If we started with a pointer to the struct, then we need to
696 dereference that pointer first, before the other steps.
697 Translating this into DWARF ops, we will need to append the following
698 to the current location description for the variable:
699
700 DW_OP_deref -- optional, if we start with a pointer
701 DW_OP_plus_uconst <forward_fld_offset>
702 DW_OP_deref
703 DW_OP_plus_uconst <varName_fld_offset>
704
705 That is what this function does. */
706
Devang Patel2c4ceb12009-11-21 02:48:08 +0000707/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000708/// provided, and generate the DWARF information necessary to find the
709/// actual Block variable (navigating the Block struct) based on the
710/// starting location. Add the DWARF information to the die. For
711/// more information, read large comment just above here.
712///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000713void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000714 unsigned Attribute,
715 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000716 DIType Ty = DV->getType();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000717 DIType TmpTy = Ty;
718 unsigned Tag = Ty.getTag();
719 bool isPointer = false;
720
Devang Patel8bd11de2010-08-09 21:01:39 +0000721 StringRef varName = DV->getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000722
723 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000724 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000725 TmpTy = DTy.getTypeDerivedFrom();
726 isPointer = true;
727 }
728
Devang Patel2db49d72010-05-07 18:11:54 +0000729 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000730
Daniel Dunbar00564992009-09-19 20:40:14 +0000731 // Find the __forwarding field and the variable field in the __Block_byref
732 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000733 DIArray Fields = blockStruct.getTypeArray();
734 DIDescriptor varField = DIDescriptor();
735 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000736
Daniel Dunbar00564992009-09-19 20:40:14 +0000737 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
738 DIDescriptor Element = Fields.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000739 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000740 StringRef fieldName = DT.getName();
741 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000742 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000743 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000744 varField = Element;
745 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000746
Daniel Dunbar00564992009-09-19 20:40:14 +0000747 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000748 unsigned forwardingFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000749 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000750 unsigned varFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000751 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000752
Mike Stump7e3720d2009-09-24 23:21:26 +0000753 // Decode the original location, and use that as the start of the byref
754 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000755 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000756 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000757 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000758
Daniel Dunbar00564992009-09-19 20:40:14 +0000759 if (Location.isReg()) {
760 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000761 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000762 else {
763 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000764 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
765 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000766 }
767 } else {
768 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000769 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000770 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000771 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
772 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000773 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000774
Devang Patel2c4ceb12009-11-21 02:48:08 +0000775 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000776 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000777
Mike Stump7e3720d2009-09-24 23:21:26 +0000778 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000779 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000780 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000781 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000782
Daniel Dunbar00564992009-09-19 20:40:14 +0000783 // Next add the offset for the '__forwarding' field:
784 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
785 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000786 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000787 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
788 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000789 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000790
Daniel Dunbar00564992009-09-19 20:40:14 +0000791 // Now dereference the __forwarding field to get to the real __Block_byref
792 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000793 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000794
Daniel Dunbar00564992009-09-19 20:40:14 +0000795 // Now that we've got the real __Block_byref... struct, add the offset
796 // for the variable's field to get to the location of the actual variable:
797 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000798 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000799 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
800 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000801 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000802
Daniel Dunbar00564992009-09-19 20:40:14 +0000803 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000804 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000805}
806
Devang Patel2c4ceb12009-11-21 02:48:08 +0000807/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000808/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000809void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000810 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000811 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000812 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000813 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelbe90c3a2010-09-22 21:10:38 +0000814 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
815
816 if (TRI->getFrameRegister(*Asm->MF) == Location.getReg()
817 && Location.getOffset()) {
818 // If variable offset is based in frame register then use fbreg.
819 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
820 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
821 addBlock(Die, Attribute, 0, Block);
822 return;
823 }
Bill Wendling0310d762009-05-15 09:23:25 +0000824
825 if (Location.isReg()) {
826 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000827 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000828 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000829 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
830 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000831 }
832 } else {
833 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000834 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000835 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000836 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
837 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000838 }
839
Devang Patel2c4ceb12009-11-21 02:48:08 +0000840 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000841 }
842
Devang Patel2c4ceb12009-11-21 02:48:08 +0000843 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000844}
845
Devang Patela43098d2010-04-28 01:03:09 +0000846/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patel26c1e562010-05-20 16:36:41 +0000847bool DwarfDebug::addRegisterAddress(DIE *Die, const MCSymbol *VS,
Devang Patela43098d2010-04-28 01:03:09 +0000848 const MachineOperand &MO) {
849 assert (MO.isReg() && "Invalid machine operand!");
850 if (!MO.getReg())
851 return false;
852 MachineLocation Location;
853 Location.set(MO.getReg());
854 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patel26c1e562010-05-20 16:36:41 +0000855 if (VS)
Devang Patela43098d2010-04-28 01:03:09 +0000856 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
857 return true;
858}
859
860/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel26c1e562010-05-20 16:36:41 +0000861bool DwarfDebug::addConstantValue(DIE *Die, const MCSymbol *VS,
Devang Patela43098d2010-04-28 01:03:09 +0000862 const MachineOperand &MO) {
863 assert (MO.isImm() && "Invalid machine operand!");
864 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
865 unsigned Imm = MO.getImm();
866 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
867 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel26c1e562010-05-20 16:36:41 +0000868 if (VS)
Devang Patela43098d2010-04-28 01:03:09 +0000869 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
870 return true;
871}
872
873/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patel26c1e562010-05-20 16:36:41 +0000874bool DwarfDebug::addConstantFPValue(DIE *Die, const MCSymbol *VS,
Devang Patela43098d2010-04-28 01:03:09 +0000875 const MachineOperand &MO) {
876 assert (MO.isFPImm() && "Invalid machine operand!");
877 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
878 APFloat FPImm = MO.getFPImm()->getValueAPF();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000879
Devang Patela43098d2010-04-28 01:03:09 +0000880 // Get the raw data form of the floating point.
881 const APInt FltVal = FPImm.bitcastToAPInt();
882 const char *FltPtr = (const char*)FltVal.getRawData();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000883
Devang Patela43098d2010-04-28 01:03:09 +0000884 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
885 bool LittleEndian = Asm->getTargetData().isLittleEndian();
886 int Incr = (LittleEndian ? 1 : -1);
887 int Start = (LittleEndian ? 0 : NumBytes - 1);
888 int Stop = (LittleEndian ? NumBytes : -1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000889
Devang Patela43098d2010-04-28 01:03:09 +0000890 // Output the constant to DWARF one byte at a time.
891 for (; Start != Stop; Start += Incr)
892 addUInt(Block, 0, dwarf::DW_FORM_data1,
893 (unsigned char)0xFF & FltPtr[Start]);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000894
Devang Patela43098d2010-04-28 01:03:09 +0000895 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel26c1e562010-05-20 16:36:41 +0000896 if (VS)
897 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000898 return true;
Devang Patela43098d2010-04-28 01:03:09 +0000899}
900
901
Devang Patelc366f832009-12-10 19:14:49 +0000902/// addToContextOwner - Add Die into the list of its context owner's children.
903void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000904 if (Context.isType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000905 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patelc366f832009-12-10 19:14:49 +0000906 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000907 } else if (Context.isNameSpace()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000908 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel6404e4e2009-12-15 19:16:48 +0000909 ContextDIE->addChild(Die);
Stuart Hastings215aa152010-06-11 20:08:44 +0000910 } else if (Context.isSubprogram()) {
Devang Patelee70fa72010-09-27 23:15:27 +0000911 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context));
Stuart Hastings215aa152010-06-11 20:08:44 +0000912 ContextDIE->addChild(Die);
Devang Patel163a9f72010-05-10 22:49:55 +0000913 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patelc366f832009-12-10 19:14:49 +0000914 ContextDIE->addChild(Die);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000915 else
Devang Patel163a9f72010-05-10 22:49:55 +0000916 getCompileUnit(Context)->addDie(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000917}
918
Devang Patel16ced732009-12-10 18:05:33 +0000919/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
920/// given DIType.
921DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel163a9f72010-05-10 22:49:55 +0000922 CompileUnit *TypeCU = getCompileUnit(Ty);
923 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel16ced732009-12-10 18:05:33 +0000924 if (TyDIE)
925 return TyDIE;
926
927 // Create new type.
928 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel163a9f72010-05-10 22:49:55 +0000929 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel16ced732009-12-10 18:05:33 +0000930 if (Ty.isBasicType())
Devang Patel2db49d72010-05-07 18:11:54 +0000931 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000932 else if (Ty.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000933 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000934 else {
935 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2db49d72010-05-07 18:11:54 +0000936 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000937 }
938
Devang Patelc366f832009-12-10 19:14:49 +0000939 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000940 return TyDIE;
941}
942
Devang Patel2c4ceb12009-11-21 02:48:08 +0000943/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000944void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9c004872010-05-07 21:45:47 +0000945 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000946 return;
947
948 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000949 CompileUnit *TypeCU = getCompileUnit(Ty);
950 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000951 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000952 if (Entry) {
953 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000954 return;
955 }
956
Bill Wendling0310d762009-05-15 09:23:25 +0000957 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000958 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000959
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000960 // Set up proxy.
961 Entry = createDIEEntry(Buffer);
Devang Patel163a9f72010-05-10 22:49:55 +0000962 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000963
Devang Patel2c4ceb12009-11-21 02:48:08 +0000964 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000965}
966
Devang Patel2c4ceb12009-11-21 02:48:08 +0000967/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000968void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000969 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000970 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000971 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000972 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000973 BTy.getEncoding());
974
975 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000976 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000977 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000978 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000979 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000980}
981
Devang Patel2c4ceb12009-11-21 02:48:08 +0000982/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000983void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000984 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000985 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000986 uint64_t Size = DTy.getSizeInBits() >> 3;
987 unsigned Tag = DTy.getTag();
988
989 // FIXME - Workaround for templates.
990 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
991
992 Buffer.setTag(Tag);
993
994 // Map to main type, void will not have a type.
995 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000996 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000997
998 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000999 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001000 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001001
1002 // Add size if non-zero (derived types might be zero-sized.)
1003 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001004 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001005
1006 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +00001007 if (!DTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001008 addSourceLine(&Buffer, DTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001009}
1010
Devang Patel2c4ceb12009-11-21 02:48:08 +00001011/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001012void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001013 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +00001014 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001015
1016 uint64_t Size = CTy.getSizeInBits() >> 3;
1017 unsigned Tag = CTy.getTag();
1018 Buffer.setTag(Tag);
1019
1020 switch (Tag) {
1021 case dwarf::DW_TAG_vector_type:
1022 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +00001023 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001024 break;
1025 case dwarf::DW_TAG_enumeration_type: {
1026 DIArray Elements = CTy.getTypeArray();
1027
1028 // Add enumerators to enumeration type.
1029 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1030 DIE *ElemDie = NULL;
Devang Patel2db49d72010-05-07 18:11:54 +00001031 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3c91b052010-03-08 20:52:55 +00001032 if (Enum.isEnumerator()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001033 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001034 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +00001035 }
Bill Wendling0310d762009-05-15 09:23:25 +00001036 }
1037 }
1038 break;
1039 case dwarf::DW_TAG_subroutine_type: {
1040 // Add return type.
1041 DIArray Elements = CTy.getTypeArray();
1042 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2db49d72010-05-07 18:11:54 +00001043 addType(&Buffer, DIType(RTy));
Bill Wendling0310d762009-05-15 09:23:25 +00001044
1045 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001046 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001047
1048 // Add arguments.
1049 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1050 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1051 DIDescriptor Ty = Elements.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +00001052 addType(Arg, DIType(Ty));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001053 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001054 }
1055 }
1056 break;
1057 case dwarf::DW_TAG_structure_type:
1058 case dwarf::DW_TAG_union_type:
1059 case dwarf::DW_TAG_class_type: {
1060 // Add elements to structure type.
1061 DIArray Elements = CTy.getTypeArray();
1062
1063 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +00001064 unsigned N = Elements.getNumElements();
1065 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +00001066 break;
1067
1068 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +00001069 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001070 DIDescriptor Element = Elements.getElement(i);
1071 DIE *ElemDie = NULL;
Devang Patel1a301232010-09-29 21:44:16 +00001072 if (Element.isSubprogram()) {
1073 DISubprogram SP(Element);
Devang Patel2db49d72010-05-07 18:11:54 +00001074 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel1a301232010-09-29 21:44:16 +00001075 if (SP.isProtected())
1076 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1077 dwarf::DW_ACCESS_protected);
1078 else if (SP.isPrivate())
1079 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1080 dwarf::DW_ACCESS_private);
1081 else
1082 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1083 dwarf::DW_ACCESS_public);
1084 }
Devang Patel3c91b052010-03-08 20:52:55 +00001085 else if (Element.isVariable()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001086 DIVariable DV(Element);
Devang Patel1ee0cb92010-01-30 01:08:30 +00001087 ElemDie = new DIE(dwarf::DW_TAG_variable);
1088 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1089 DV.getName());
1090 addType(ElemDie, DV.getType());
1091 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1092 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel81625742010-08-09 20:20:05 +00001093 addSourceLine(ElemDie, DV);
Devang Patel3c91b052010-03-08 20:52:55 +00001094 } else if (Element.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001095 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001096 else
1097 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001098 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001099 }
1100
Devang Patela1ba2692009-08-27 23:51:51 +00001101 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001102 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001103
1104 unsigned RLang = CTy.getRunTimeLang();
1105 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001106 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001107 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001108
1109 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel2db49d72010-05-07 18:11:54 +00001110 if (DIDescriptor(ContainingType).isCompositeType())
Jim Grosbach1e20b962010-07-21 21:21:52 +00001111 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel2db49d72010-05-07 18:11:54 +00001112 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastings215aa152010-06-11 20:08:44 +00001113 else {
1114 DIDescriptor Context = CTy.getContext();
1115 addToContextOwner(&Buffer, Context);
1116 }
Bill Wendling0310d762009-05-15 09:23:25 +00001117 break;
1118 }
1119 default:
1120 break;
1121 }
1122
1123 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001124 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001125 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001126
Jim Grosbach1e20b962010-07-21 21:21:52 +00001127 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
Devang Patel61409622010-07-07 20:12:52 +00001128 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1129 {
Bill Wendling0310d762009-05-15 09:23:25 +00001130 // Add size if non-zero (derived types might be zero-sized.)
1131 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001132 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001133 else {
1134 // Add zero size if it is not a forward declaration.
1135 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001136 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001137 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001138 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001139 }
1140
1141 // Add source line info if available.
1142 if (!CTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001143 addSourceLine(&Buffer, CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001144 }
1145}
1146
Devang Patel2c4ceb12009-11-21 02:48:08 +00001147/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1148void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001149 int64_t L = SR.getLo();
1150 int64_t H = SR.getHi();
1151 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1152
Devang Patel2c4ceb12009-11-21 02:48:08 +00001153 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001154 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001155 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001156 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001157
Devang Patel2c4ceb12009-11-21 02:48:08 +00001158 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001159}
1160
Devang Patel2c4ceb12009-11-21 02:48:08 +00001161/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001162void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001163 DICompositeType *CTy) {
1164 Buffer.setTag(dwarf::DW_TAG_array_type);
1165 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001166 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001167
1168 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001169 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001170 DIArray Elements = CTy->getTypeArray();
1171
Devang Patel6f01d9c2009-11-21 00:31:03 +00001172 // Get an anonymous type for index type.
Devang Patel163a9f72010-05-10 22:49:55 +00001173 CompileUnit *TheCU = getCompileUnit(*CTy);
1174 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001175 if (!IdxTy) {
1176 // Construct an anonymous type for index type.
1177 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001178 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1179 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001180 dwarf::DW_ATE_signed);
Devang Patel163a9f72010-05-10 22:49:55 +00001181 TheCU->addDie(IdxTy);
1182 TheCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001183 }
Bill Wendling0310d762009-05-15 09:23:25 +00001184
1185 // Add subranges to array type.
1186 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1187 DIDescriptor Element = Elements.getElement(i);
1188 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2db49d72010-05-07 18:11:54 +00001189 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001190 }
1191}
1192
Devang Patel2c4ceb12009-11-21 02:48:08 +00001193/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001194DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001195 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001196 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001197 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001198 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001199 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001200 return Enumerator;
1201}
1202
Jim Grosbach1e20b962010-07-21 21:21:52 +00001203/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +00001204/// printer to not emit usual symbol prefix before the symbol name is used then
1205/// return linkage name after skipping this special LLVM prefix.
1206static StringRef getRealLinkageName(StringRef LinkageName) {
1207 char One = '\1';
1208 if (LinkageName.startswith(StringRef(&One, 1)))
1209 return LinkageName.substr(1);
1210 return LinkageName;
1211}
1212
Devang Patel2c4ceb12009-11-21 02:48:08 +00001213/// createMemberDIE - Create new member DIE.
Devang Patelecbd8e82010-08-10 04:12:17 +00001214DIE *DwarfDebug::createMemberDIE(DIDerivedType DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001215 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001216 StringRef Name = DT.getName();
1217 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001218 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001219
Devang Patel8a241142009-12-09 18:24:21 +00001220 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001221
Devang Patel81625742010-08-09 20:20:05 +00001222 addSourceLine(MemberDie, DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001223
Benjamin Kramer345ef342010-03-31 19:34:01 +00001224 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001225 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001226
Bill Wendling0310d762009-05-15 09:23:25 +00001227 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001228 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001229
1230 if (Size != FieldSize) {
1231 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001232 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1233 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001234
1235 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001236 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1237 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001238 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001239 Offset -= FieldOffset;
1240
1241 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001242 if (Asm->getTargetData().isLittleEndian())
1243 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001244 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001245
Devang Patel33db5082009-11-04 22:06:12 +00001246 // Here WD_AT_data_member_location points to the anonymous
1247 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001248 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001249
1250 } else
1251 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001252 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001253
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001254 if (DT.getTag() == dwarf::DW_TAG_inheritance
1255 && DT.isVirtual()) {
1256
1257 // For C++, virtual base classes are not at fixed offset. Use following
1258 // expression to extract appropriate offset from vtable.
1259 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1260
Benjamin Kramer345ef342010-03-31 19:34:01 +00001261 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001262 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1263 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1264 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1265 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1266 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1267 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1268 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1269
Jim Grosbach1e20b962010-07-21 21:21:52 +00001270 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001271 VBaseLocationDie);
1272 } else
1273 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001274
1275 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001276 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001277 dwarf::DW_ACCESS_protected);
1278 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001279 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001280 dwarf::DW_ACCESS_private);
Devang Patel2a361602010-09-29 19:08:08 +00001281 // Otherwise C++ member and base classes are considered public.
1282 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
Devang Patel5d11eb02009-12-03 19:11:07 +00001283 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1284 dwarf::DW_ACCESS_public);
1285 if (DT.isVirtual())
1286 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1287 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001288 return MemberDie;
1289}
1290
Devang Patelffe966c2009-12-14 16:18:45 +00001291/// createSubprogramDIE - Create new DIE using SP.
Devang Patelee70fa72010-09-27 23:15:27 +00001292DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel163a9f72010-05-10 22:49:55 +00001293 CompileUnit *SPCU = getCompileUnit(SP);
1294 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +00001295 if (SPDie)
1296 return SPDie;
1297
1298 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001299 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001300 if (!SP.getName().empty())
1301 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001302
Devang Patel65dbc902009-11-25 17:36:49 +00001303 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001304 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001305 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001306 getRealLinkageName(LinkageName));
1307
Devang Patel81625742010-08-09 20:20:05 +00001308 addSourceLine(SPDie, SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001309
Bill Wendling0310d762009-05-15 09:23:25 +00001310 // Add prototyped tag, if C or ObjC.
1311 unsigned Lang = SP.getCompileUnit().getLanguage();
1312 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1313 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001314 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001315
1316 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001317 DICompositeType SPTy = SP.getType();
1318 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001319 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001320
Devang Patel3c91b052010-03-08 20:52:55 +00001321 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001322 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001323 else
Devang Patel2db49d72010-05-07 18:11:54 +00001324 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001325
Devang Patel5d11eb02009-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 Kramer345ef342010-03-31 19:34:01 +00001329 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001330 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1331 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1332 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001333 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +00001334 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001335 }
1336
Devang Patelee70fa72010-09-27 23:15:27 +00001337 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001338 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001339
1340 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-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 Wendling0310d762009-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 Patel2db49d72010-05-07 18:11:54 +00001349 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelb4645642010-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 Patel2c4ceb12009-11-21 02:48:08 +00001353 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001354 }
1355 }
1356
Devang Patel4e0d19d2010-02-03 19:57:19 +00001357 if (SP.isArtificial())
1358 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1359
Devang Patelccff8122010-04-30 19:38:23 +00001360 if (!SP.isLocalToUnit())
1361 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001362
Devang Patelccff8122010-04-30 19:38:23 +00001363 if (SP.isOptimized())
1364 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1365
Jim Grosbach91729002010-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 Patelccff8122010-04-30 19:38:23 +00001370 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel163a9f72010-05-10 22:49:55 +00001371 SPCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +00001372
Stuart Hastings215aa152010-06-11 20:08:44 +00001373 // Add to context owner.
1374 addToContextOwner(SPDie, SP.getContext());
1375
Bill Wendling0310d762009-05-15 09:23:25 +00001376 return SPDie;
1377}
1378
Devang Patele9f8f5e2010-05-07 20:54:48 +00001379DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001380 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001381
1382 DbgScope *AScope = AbstractScopes.lookup(N);
1383 if (AScope)
1384 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001385
Devang Patel53bb5c92009-11-10 23:06:00 +00001386 DbgScope *Parent = NULL;
1387
1388 DIDescriptor Scope(N);
1389 if (Scope.isLexicalBlock()) {
1390 DILexicalBlock DB(N);
1391 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +00001392 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +00001393 }
1394
1395 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1396
1397 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001398 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001399 AScope->setAbstractScope();
1400 AbstractScopes[N] = AScope;
1401 if (DIDescriptor(N).isSubprogram())
1402 AbstractScopesList.push_back(AScope);
1403 return AScope;
1404}
Devang Patelaf9e8472009-10-01 20:31:14 +00001405
Devang Patel5f094002010-04-06 23:53:48 +00001406/// isSubprogramContext - Return true if Context is either a subprogram
1407/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001408static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001409 if (!Context)
1410 return false;
1411 DIDescriptor D(Context);
1412 if (D.isSubprogram())
1413 return true;
1414 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001415 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +00001416 return false;
1417}
1418
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001419/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001420/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1421/// If there are global variables in this scope then create and insert
1422/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001423DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +00001424 CompileUnit *SPCU = getCompileUnit(SPNode);
1425 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +00001426
Chris Lattnerd38fee82010-04-05 00:13:49 +00001427 assert(SPDie && "Unable to find subprogram DIE!");
1428 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001429
Chris Lattnerd38fee82010-04-05 00:13:49 +00001430 // There is not any need to generate specification DIE for a function
1431 // defined at compile unit level. If a function is defined inside another
1432 // function then gdb prefers the definition at top level and but does not
Jim Grosbach1e20b962010-07-21 21:21:52 +00001433 // expect specification DIE in parent function. So avoid creating
Chris Lattnerd38fee82010-04-05 00:13:49 +00001434 // specification DIE for a function defined inside a function.
1435 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbach1e20b962010-07-21 21:21:52 +00001436 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001437 !isSubprogramContext(SP.getContext())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001438 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001439
1440 // Add arguments.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001441 DICompositeType SPTy = SP.getType();
1442 DIArray Args = SPTy.getTypeArray();
1443 unsigned SPTag = SPTy.getTag();
1444 if (SPTag == dwarf::DW_TAG_subroutine_type)
1445 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1446 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001447 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerd38fee82010-04-05 00:13:49 +00001448 addType(Arg, ATy);
1449 if (ATy.isArtificial())
1450 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1451 SPDie->addChild(Arg);
1452 }
1453 DIE *SPDeclDie = SPDie;
1454 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001455 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Chris Lattnerd38fee82010-04-05 00:13:49 +00001456 SPDeclDie);
Devang Patel163a9f72010-05-10 22:49:55 +00001457 SPCU->addDie(SPDie);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001458 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001459
Devang Patel8aa61472010-07-07 22:20:57 +00001460 // Pick up abstract subprogram DIE.
1461 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1462 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1463 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1464 dwarf::DW_FORM_ref4, AbsSPDIE);
1465 SPCU->addDie(SPDie);
1466 }
1467
Chris Lattnerd38fee82010-04-05 00:13:49 +00001468 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1469 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1470 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1471 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1472 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1473 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1474 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001475
Chris Lattnerd38fee82010-04-05 00:13:49 +00001476 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001477}
1478
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001479/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001480/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1481DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001482
1483 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1484 if (Scope->isAbstractScope())
1485 return ScopeDIE;
1486
1487 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1488 if (Ranges.empty())
1489 return 0;
1490
1491 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1492 if (Ranges.size() > 1) {
1493 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +00001494 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +00001495 // DW_AT_ranges appropriately.
1496 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1497 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1498 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1499 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001500 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1501 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +00001502 }
1503 DebugRangeSymbols.push_back(NULL);
1504 DebugRangeSymbols.push_back(NULL);
1505 return ScopeDIE;
1506 }
1507
Devang Patelc3f5f782010-05-25 23:40:22 +00001508 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1509 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001510
Devang Patelc3f5f782010-05-25 23:40:22 +00001511 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001512
Chris Lattnerb7db7332010-03-09 01:58:53 +00001513 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1514 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001515
Devang Pateleac9c072010-04-27 19:46:33 +00001516 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1517 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001518
1519 return ScopeDIE;
1520}
1521
Devang Patel2c4ceb12009-11-21 02:48:08 +00001522/// constructInlinedScopeDIE - This scope represents inlined body of
1523/// a function. Construct DIE to represent this concrete inlined copy
1524/// of the function.
1525DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001526
1527 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001528 assert (Ranges.empty() == false
Devang Pateleac9c072010-04-27 19:46:33 +00001529 && "DbgScope does not have instruction markers!");
1530
1531 // FIXME : .debug_inlined section specification does not clearly state how
1532 // to emit inlined scope that is split into multiple instruction ranges.
1533 // For now, use first instruction range and emit low_pc/high_pc pair and
1534 // corresponding .debug_inlined section entry for this pair.
1535 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +00001536 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1537 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001538
Devang Patel0afbf232010-07-08 22:39:20 +00001539 if (StartLabel == 0 || EndLabel == 0) {
Devang Pateleac9c072010-04-27 19:46:33 +00001540 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1541 return 0;
1542 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001543 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001544 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001545 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001546 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +00001547
Devang Patel3c91b052010-03-08 20:52:55 +00001548 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001549 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001550 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001551 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1552
Devang Patel2db49d72010-05-07 18:11:54 +00001553 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001554 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1555 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001556 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001557 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001558 dwarf::DW_FORM_ref4, OriginDIE);
1559
Chris Lattner6ed0f902010-03-09 00:31:02 +00001560 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001561 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001562
1563 InlinedSubprogramDIEs.insert(OriginDIE);
1564
1565 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001566 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +00001567 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001568
1569 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001570 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001571 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +00001572 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001573 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001574 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001575
Devang Patel53bb5c92009-11-10 23:06:00 +00001576 DILocation DL(Scope->getInlinedAt());
Devang Patel163a9f72010-05-10 22:49:55 +00001577 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001578 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001579
1580 return ScopeDIE;
1581}
1582
Devang Patel2c4ceb12009-11-21 02:48:08 +00001583
1584/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001585DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +00001586 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +00001587 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001588 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001589
1590 // Translate tag to proper Dwarf tag. The result variable is dropped for
1591 // now.
1592 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +00001593 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001594 case dwarf::DW_TAG_return_variable:
1595 return NULL;
1596 case dwarf::DW_TAG_arg_variable:
1597 Tag = dwarf::DW_TAG_formal_parameter;
1598 break;
1599 case dwarf::DW_TAG_auto_variable: // fall thru
1600 default:
1601 Tag = dwarf::DW_TAG_variable;
1602 break;
1603 }
1604
1605 // Define variable debug information entry.
1606 DIE *VariableDie = new DIE(Tag);
1607
Devang Patel53bb5c92009-11-10 23:06:00 +00001608 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +00001609 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1610 V2AVI = VarToAbstractVarMap.find(DV);
1611 if (V2AVI != VarToAbstractVarMap.end())
1612 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001613
Devang Patel26c1e562010-05-20 16:36:41 +00001614 if (AbsDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001615 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001616 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001617 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001618 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel8bd11de2010-08-09 21:01:39 +00001619 addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +00001620
1621 // Add variable type.
Devang Patel8bd11de2010-08-09 21:01:39 +00001622 addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001623 }
1624
Devang Patel8bd11de2010-08-09 21:01:39 +00001625 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patelb4645642010-02-06 01:02:37 +00001626 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +00001627
1628 if (Scope->isAbstractScope()) {
1629 DV->setDIE(VariableDie);
1630 return VariableDie;
1631 }
1632
1633 // Add variable address.
1634
1635 unsigned Offset = DV->getDotDebugLocOffset();
1636 if (Offset != ~0U) {
1637 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1638 Asm->GetTempSymbol("debug_loc", Offset));
1639 DV->setDIE(VariableDie);
1640 UseDotDebugLocEntry.insert(VariableDie);
1641 return VariableDie;
1642 }
1643
1644 // Check if variable is described by a DBG_VALUE instruction.
1645 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1646 DbgVariableToDbgInstMap.find(DV);
1647 if (DVI != DbgVariableToDbgInstMap.end()) {
1648 const MachineInstr *DVInsn = DVI->second;
1649 const MCSymbol *DVLabel = findVariableLabel(DV);
1650 bool updated = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001651 // FIXME : Handle getNumOperands != 3
Devang Patelc3f5f782010-05-25 23:40:22 +00001652 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +00001653 if (DVInsn->getOperand(0).isReg()) {
1654 const MachineOperand RegOp = DVInsn->getOperand(0);
1655 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1656 if (DVInsn->getOperand(1).isImm() &&
1657 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1658 addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
1659 updated = true;
1660 } else
1661 updated = addRegisterAddress(VariableDie, DVLabel, RegOp);
1662 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001663 else if (DVInsn->getOperand(0).isImm())
1664 updated = addConstantValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001665 else if (DVInsn->getOperand(0).isFPImm())
1666 updated =
Devang Patel61409622010-07-07 20:12:52 +00001667 addConstantFPValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patelc3f5f782010-05-25 23:40:22 +00001668 } else {
1669 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1670 if (Location.getReg()) {
1671 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1672 if (DVLabel)
1673 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1674 DVLabel);
1675 updated = true;
1676 }
1677 }
1678 if (!updated) {
1679 // If variableDie is not updated then DBG_VALUE instruction does not
1680 // have valid variable info.
1681 delete VariableDie;
1682 return NULL;
1683 }
1684 DV->setDIE(VariableDie);
1685 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001686 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001687
1688 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +00001689 int FI = 0;
Devang Patel9e3bd2c2010-08-31 06:11:28 +00001690 if (findVariableFrameIndex(DV, &FI))
1691 addVariableAddress(DV, VariableDie, FI);
1692
Devang Patel53bb5c92009-11-10 23:06:00 +00001693 DV->setDIE(VariableDie);
1694 return VariableDie;
1695
1696}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001697
Devang Patel193f7202009-11-24 01:14:22 +00001698void DwarfDebug::addPubTypes(DISubprogram SP) {
1699 DICompositeType SPTy = SP.getType();
1700 unsigned SPTag = SPTy.getTag();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001701 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel193f7202009-11-24 01:14:22 +00001702 return;
1703
1704 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001705 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +00001706 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +00001707 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +00001708 continue;
1709 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +00001710 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +00001711 && !CATy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001712 CompileUnit *TheCU = getCompileUnit(CATy);
1713 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1714 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001715 }
1716 }
1717}
1718
Devang Patel2c4ceb12009-11-21 02:48:08 +00001719/// constructScopeDIE - Construct a DIE for this scope.
1720DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001721 if (!Scope || !Scope->getScopeNode())
1722 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001723
Devang Patel3c91b052010-03-08 20:52:55 +00001724 DIScope DS(Scope->getScopeNode());
1725 DIE *ScopeDIE = NULL;
1726 if (Scope->getInlinedAt())
1727 ScopeDIE = constructInlinedScopeDIE(Scope);
1728 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +00001729 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001730 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001731 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001732 // Note down abstract DIE.
1733 if (ScopeDIE)
1734 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1735 }
Devang Patel3c91b052010-03-08 20:52:55 +00001736 else
Devang Patel2db49d72010-05-07 18:11:54 +00001737 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001738 }
Devang Patelaead63c2010-03-29 22:59:58 +00001739 else
Devang Patel3c91b052010-03-08 20:52:55 +00001740 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001741 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001742
Devang Patel53bb5c92009-11-10 23:06:00 +00001743 // Add variables to scope.
Devang Patele03161c2010-08-09 18:51:29 +00001744 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001745 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001746 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001747 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001748 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001749 }
1750
1751 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001752 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001753 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1754 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001755 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001756 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001757 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001758 }
Devang Patel193f7202009-11-24 01:14:22 +00001759
Jim Grosbach1e20b962010-07-21 21:21:52 +00001760 if (DS.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001761 addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001762
Devang Patel193f7202009-11-24 01:14:22 +00001763 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001764}
1765
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001766/// GetOrCreateSourceID - Look up the source id with the given directory and
1767/// source file names. If none currently exists, create a new id and insert it
1768/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1769/// maps as well.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001770unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001771 unsigned DId;
Devang Patel37032352010-07-27 20:51:15 +00001772 assert (DirName.empty() == false && "Invalid directory name!");
Devang Patel2f584852010-07-24 00:53:22 +00001773
Devang Patel1905a182010-09-16 20:57:49 +00001774 // If FE did not provide a file name, then assume stdin.
1775 if (FileName.empty())
1776 return GetOrCreateSourceID(DirName, "<stdin>");
1777
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001778 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1779 if (DI != DirectoryIdMap.end()) {
1780 DId = DI->getValue();
1781 } else {
1782 DId = DirectoryNames.size() + 1;
1783 DirectoryIdMap[DirName] = DId;
1784 DirectoryNames.push_back(DirName);
1785 }
1786
1787 unsigned FId;
1788 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1789 if (FI != SourceFileIdMap.end()) {
1790 FId = FI->getValue();
1791 } else {
1792 FId = SourceFileNames.size() + 1;
1793 SourceFileIdMap[FileName] = FId;
1794 SourceFileNames.push_back(FileName);
1795 }
1796
1797 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1798 SourceIdMap.find(std::make_pair(DId, FId));
1799 if (SI != SourceIdMap.end())
1800 return SI->second;
1801
1802 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1803 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1804 SourceIds.push_back(std::make_pair(DId, FId));
1805
1806 return SrcId;
1807}
1808
Devang Patel6404e4e2009-12-15 19:16:48 +00001809/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1810DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel163a9f72010-05-10 22:49:55 +00001811 CompileUnit *TheCU = getCompileUnit(NS);
1812 DIE *NDie = TheCU->getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001813 if (NDie)
1814 return NDie;
1815 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel163a9f72010-05-10 22:49:55 +00001816 TheCU->insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +00001817 if (!NS.getName().empty())
1818 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patel81625742010-08-09 20:20:05 +00001819 addSourceLine(NDie, NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001820 addToContextOwner(NDie, NS.getContext());
1821 return NDie;
1822}
1823
Jim Grosbach1e20b962010-07-21 21:21:52 +00001824/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +00001825/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001826void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001827 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001828 StringRef FN = DIUnit.getFilename();
1829 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001830 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001831
1832 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001833 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001834 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001835 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001836 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001837 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +00001838 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1839 // simplifies debug range entries.
Devang Patel9b93b6b2010-06-28 22:22:47 +00001840 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +00001841 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +00001842 // compile unit in debug_line section.
Devang Patelae84d5b2010-08-31 23:50:19 +00001843 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
1844 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
1845 Asm->GetTempSymbol("section_line"));
1846 else
1847 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001848
Devang Patel65dbc902009-11-25 17:36:49 +00001849 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001850 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001851 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001852 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001853
Devang Patel65dbc902009-11-25 17:36:49 +00001854 StringRef Flags = DIUnit.getFlags();
1855 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001856 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001857
1858 unsigned RVer = DIUnit.getRunTimeVersion();
1859 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001860 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001861 dwarf::DW_FORM_data1, RVer);
1862
Devang Patel163a9f72010-05-10 22:49:55 +00001863 CompileUnit *NewCU = new CompileUnit(ID, Die);
1864 if (!FirstCU)
1865 FirstCU = NewCU;
1866 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001867}
1868
Devang Patel163a9f72010-05-10 22:49:55 +00001869/// getCompielUnit - Get CompileUnit DIE.
1870CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1871 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1872 DIDescriptor D(N);
1873 const MDNode *CUNode = NULL;
1874 if (D.isCompileUnit())
1875 CUNode = N;
1876 else if (D.isSubprogram())
1877 CUNode = DISubprogram(N).getCompileUnit();
1878 else if (D.isType())
1879 CUNode = DIType(N).getCompileUnit();
1880 else if (D.isGlobalVariable())
1881 CUNode = DIGlobalVariable(N).getCompileUnit();
1882 else if (D.isVariable())
1883 CUNode = DIVariable(N).getCompileUnit();
1884 else if (D.isNameSpace())
1885 CUNode = DINameSpace(N).getCompileUnit();
1886 else if (D.isFile())
1887 CUNode = DIFile(N).getCompileUnit();
1888 else
1889 return FirstCU;
1890
1891 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1892 = CUMap.find(CUNode);
1893 if (I == CUMap.end())
1894 return FirstCU;
1895 return I->second;
1896}
1897
Devang Patel0c4720c2010-08-23 18:25:56 +00001898/// isUnsignedDIType - Return true if type encoding is unsigned.
1899static bool isUnsignedDIType(DIType Ty) {
1900 DIDerivedType DTy(Ty);
1901 if (DTy.Verify())
1902 return isUnsignedDIType(DTy.getTypeDerivedFrom());
1903
1904 DIBasicType BTy(Ty);
1905 if (BTy.Verify()) {
1906 unsigned Encoding = BTy.getEncoding();
1907 if (Encoding == dwarf::DW_ATE_unsigned ||
1908 Encoding == dwarf::DW_ATE_unsigned_char)
1909 return true;
1910 }
1911 return false;
1912}
Devang Patel163a9f72010-05-10 22:49:55 +00001913
1914/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001915void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +00001916 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001917
Devang Patel905cf5e2009-09-04 23:59:07 +00001918 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +00001919 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +00001920 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001921
1922 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001923 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +00001924 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001925 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001926
Devang Patel9fa539c2010-08-10 01:37:23 +00001927 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +00001928 DIE *VariableDIE = new DIE(GV.getTag());
1929
1930 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001931
Devang Patel9fa539c2010-08-10 01:37:23 +00001932 // Add name.
1933 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1934 GV.getDisplayName());
1935 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +00001936 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel9fa539c2010-08-10 01:37:23 +00001937 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1938 getRealLinkageName(LinkageName));
1939 // Add type.
1940 addType(VariableDIE, GTy);
Devang Patel50d80e32010-04-13 20:35:04 +00001941 if (GTy.isCompositeType() && !GTy.getName().empty()
1942 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001943 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001944 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +00001945 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001946 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001947 // Add scoping info.
1948 if (!GV.isLocalToUnit()) {
1949 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1950 // Expose as global.
1951 TheCU->addGlobal(GV.getName(), VariableDIE);
1952 }
1953 // Add line number info.
1954 addSourceLine(VariableDIE, GV);
1955 // Add to map.
1956 TheCU->insertDIE(N, VariableDIE);
1957 // Add to context owner.
1958 DIDescriptor GVContext = GV.getContext();
1959 addToContextOwner(VariableDIE, GVContext);
1960 // Add location.
Devang Patel29368072010-08-10 07:11:13 +00001961 if (isGlobalVariable) {
1962 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1963 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1964 addLabel(Block, 0, dwarf::DW_FORM_udata,
1965 Asm->Mang->getSymbol(GV.getGlobal()));
1966 // Do not create specification DIE if context is either compile unit
1967 // or a subprogram.
1968 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1969 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1970 // Create specification DIE.
1971 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1972 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1973 dwarf::DW_FORM_ref4, VariableDIE);
1974 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1975 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1976 TheCU->addDie(VariableSpecDIE);
1977 } else {
1978 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1979 }
1980 } else if (Constant *C = GV.getConstant()) {
1981 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
Devang Patel0c4720c2010-08-23 18:25:56 +00001982 if (isUnsignedDIType(GTy))
Devang Patel29368072010-08-10 07:11:13 +00001983 addUInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
1984 CI->getZExtValue());
1985 else
1986 addSInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1987 CI->getSExtValue());
Devang Patel29368072010-08-10 07:11:13 +00001988 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001989 }
Devang Patel13e16b62009-06-26 01:49:18 +00001990 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001991}
1992
Devang Patel163a9f72010-05-10 22:49:55 +00001993/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001994void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001995 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001996
Stuart Hastings639336e2010-04-06 21:38:29 +00001997 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001998 CompileUnit *TheCU = getCompileUnit(N);
1999 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +00002000 return;
2001
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002002 if (!SP.isDefinition())
2003 // This is a method declaration which will be handled while constructing
2004 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00002005 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002006
Stuart Hastings639336e2010-04-06 21:38:29 +00002007 DIE *SubprogramDie = createSubprogramDIE(SP);
2008
2009 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00002010 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002011
2012 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00002013 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00002014
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002015 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +00002016 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00002017
Devang Patel13e16b62009-06-26 01:49:18 +00002018 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002019}
2020
Devang Patel2c4ceb12009-11-21 02:48:08 +00002021/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00002022/// content. Create global DIEs and emit initial debug info sections.
2023/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00002024void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00002025 if (DisableDebugInfoPrinting)
2026 return;
2027
Devang Patel78ab9e22009-07-30 18:56:46 +00002028 DebugInfoFinder DbgFinder;
2029 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00002030
Chris Lattnerd850ac72010-04-05 02:19:28 +00002031 bool HasDebugInfo = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002032
Chris Lattnerd850ac72010-04-05 02:19:28 +00002033 // Scan all the compile-units to see if there are any marked as the main unit.
2034 // if not, we do not generate debug info.
2035 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2036 E = DbgFinder.compile_unit_end(); I != E; ++I) {
2037 if (DICompileUnit(*I).isMain()) {
2038 HasDebugInfo = true;
2039 break;
2040 }
2041 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002042
Chris Lattnerd850ac72010-04-05 02:19:28 +00002043 if (!HasDebugInfo) return;
2044
2045 // Tell MMI that we have debug info.
2046 MMI->setDebugInfoAvailability(true);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002047
Chris Lattnerbe15beb2010-04-04 23:17:54 +00002048 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00002049 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002050
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002051 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00002052 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2053 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002054 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002055
Devang Patel53bb5c92009-11-10 23:06:00 +00002056 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00002057 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
2058 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002059 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00002060
Devang Patelc366f832009-12-10 19:14:49 +00002061 // Create DIEs for each global variable.
2062 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
2063 E = DbgFinder.global_variable_end(); I != E; ++I)
2064 constructGlobalVariableDIE(*I);
2065
Devang Patele7e5a0f2010-08-10 20:01:20 +00002066 //getOrCreateTypeDIE
2067 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
2068 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2069 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2070
Devang Patel1a7ca032010-09-28 18:08:20 +00002071 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
2072 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2073 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2074
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002075 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00002076 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002077
2078 // Print out .file directives to specify files for .loc directives. These are
2079 // printed out early so that they precede any .loc directives.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002080 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002081 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2082 // Remember source id starts at 1.
2083 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002084 // FIXME: don't use sys::path for this! This should not depend on the
2085 // host.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002086 sys::Path FullPath(getSourceDirectoryName(Id.first));
2087 bool AppendOk =
2088 FullPath.appendComponent(getSourceFileName(Id.second));
2089 assert(AppendOk && "Could not append filename to directory!");
2090 AppendOk = false;
Chris Lattnera6594fc2010-01-25 18:58:59 +00002091 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002092 }
2093 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002094}
2095
Devang Patel2c4ceb12009-11-21 02:48:08 +00002096/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002097///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002098void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00002099 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +00002100 const Module *M = MMI->getModule();
Devang Patele9a1cca2010-08-02 17:32:15 +00002101 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +00002102 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2103 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2104 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2105 DISubprogram SP(AllSPs->getOperand(SI));
2106 if (!SP.Verify()) continue;
2107
2108 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +00002109 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +00002110 StringRef FName = SP.getLinkageName();
2111 if (FName.empty())
2112 FName = SP.getName();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002113 NamedMDNode *NMD =
Devang Patel4a1cad62010-06-28 18:25:03 +00002114 M->getNamedMetadata(Twine("llvm.dbg.lv.", getRealLinkageName(FName)));
2115 if (!NMD) continue;
2116 unsigned E = NMD->getNumOperands();
2117 if (!E) continue;
2118 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Patele9a1cca2010-08-02 17:32:15 +00002119 DeadFnScopeMap[SP] = Scope;
Devang Patel4a1cad62010-06-28 18:25:03 +00002120 for (unsigned I = 0; I != E; ++I) {
2121 DIVariable DV(NMD->getOperand(I));
2122 if (!DV.Verify()) continue;
2123 Scope->addVariable(new DbgVariable(DV));
2124 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002125
Devang Patel4a1cad62010-06-28 18:25:03 +00002126 // Construct subprogram DIE and add variables DIEs.
2127 constructSubprogramDIE(SP);
2128 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patele03161c2010-08-09 18:51:29 +00002129 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel4a1cad62010-06-28 18:25:03 +00002130 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2131 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2132 if (VariableDIE)
2133 ScopeDIE->addChild(VariableDIE);
2134 }
2135 }
2136 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002137
Devang Patel53bb5c92009-11-10 23:06:00 +00002138 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2139 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2140 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2141 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002142 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00002143 }
2144
Devang Patele9f8f5e2010-05-07 20:54:48 +00002145 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00002146 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2147 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002148 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00002149 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00002150 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00002151 if (!NDie) continue;
2152 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00002153 }
2154
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002155 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002156 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002157 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002158 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002159 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002160
2161 // End text sections.
2162 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002163 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00002164 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002165 }
2166
2167 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002168 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002169
2170 // Emit function debug frame information
2171 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2172 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002173 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002174
2175 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002176 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002177
2178 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00002179 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002180
2181 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002182 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002183
Devang Patelaf608bd2010-08-24 00:06:12 +00002184 // Emit source line correspondence into a debug line section.
2185 emitDebugLines();
2186
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002187 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002188 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002189
Devang Patel193f7202009-11-24 01:14:22 +00002190 // Emit info into a debug pubtypes section.
2191 emitDebugPubTypes();
2192
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002193 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002194 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002195
2196 // Emit info into a debug aranges section.
2197 EmitDebugARanges();
2198
2199 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002200 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002201
2202 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002203 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002204
2205 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002206 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002207
Chris Lattnerbc733f52010-03-13 02:17:42 +00002208 // Emit info into a debug str section.
2209 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002210
Devang Patele9a1cca2010-08-02 17:32:15 +00002211 // clean up.
2212 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +00002213 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2214 E = CUMap.end(); I != E; ++I)
2215 delete I->second;
2216 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002217}
2218
Devang Patel53bb5c92009-11-10 23:06:00 +00002219/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002220DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002221 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002222
Devang Patel2db49d72010-05-07 18:11:54 +00002223 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00002224 if (AbsDbgVariable)
2225 return AbsDbgVariable;
2226
Devang Patel2db49d72010-05-07 18:11:54 +00002227 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002228 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00002229 if (!Scope)
2230 return NULL;
2231
Devang Patel26c1e562010-05-20 16:36:41 +00002232 AbsDbgVariable = new DbgVariable(Var);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002233 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002234 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00002235 return AbsDbgVariable;
2236}
2237
Devang Patelee432862010-05-20 19:57:06 +00002238/// collectVariableInfoFromMMITable - Collect variable information from
2239/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002240void
Devang Patelee432862010-05-20 19:57:06 +00002241DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2242 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002243 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patele717faa2009-10-06 01:26:37 +00002244 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2245 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2246 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00002247 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00002248 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00002249 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002250 DIVariable DV(Var);
2251 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002252
Chris Lattnerde4845c2010-04-02 19:42:39 +00002253 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002254 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002255 Scope = ConcreteScopes.lookup(IA);
2256 if (Scope == 0)
2257 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002258
Devang Patelfb0ee432009-11-10 23:20:04 +00002259 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002260 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00002261 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00002262
Devang Patel26c1e562010-05-20 16:36:41 +00002263 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2264 DbgVariable *RegVar = new DbgVariable(DV);
2265 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002266 Scope->addVariable(RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +00002267 if (AbsDbgVariable) {
2268 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2269 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2270 }
Devang Patele717faa2009-10-06 01:26:37 +00002271 }
Devang Patelee432862010-05-20 19:57:06 +00002272}
Devang Patel90a48ad2010-03-15 18:33:46 +00002273
Jim Grosbach1e20b962010-07-21 21:21:52 +00002274/// isDbgValueInUndefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00002275/// DBG_VALUE instruction, is in undefined reg.
2276static bool isDbgValueInUndefinedReg(const MachineInstr *MI) {
2277 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2278 if (MI->getOperand(0).isReg() && !MI->getOperand(0).getReg())
2279 return true;
2280 return false;
2281}
2282
Jim Grosbach1e20b962010-07-21 21:21:52 +00002283/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00002284/// DBG_VALUE instruction, is in a defined reg.
2285static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2286 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2287 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
2288 return true;
2289 return false;
2290}
2291
Devang Patelee432862010-05-20 19:57:06 +00002292/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002293void
Devang Patel78e127d2010-06-25 22:07:34 +00002294DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2295 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002296
Devang Patelee432862010-05-20 19:57:06 +00002297 /// collection info from MMI table.
2298 collectVariableInfoFromMMITable(MF, Processed);
2299
2300 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patel90a48ad2010-03-15 18:33:46 +00002301 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002302 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelee432862010-05-20 19:57:06 +00002303 I != E; ++I)
Devang Patel90a48ad2010-03-15 18:33:46 +00002304 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2305 II != IE; ++II) {
2306 const MachineInstr *MInsn = II;
Devang Patelc3f5f782010-05-25 23:40:22 +00002307 if (!MInsn->isDebugValue() || isDbgValueInUndefinedReg(MInsn))
Devang Patel90a48ad2010-03-15 18:33:46 +00002308 continue;
Devang Patelee432862010-05-20 19:57:06 +00002309 DbgValues.push_back(MInsn);
2310 }
Devang Patel90a48ad2010-03-15 18:33:46 +00002311
Devang Patelc3f5f782010-05-25 23:40:22 +00002312 // This is a collection of DBV_VALUE instructions describing same variable.
2313 SmallVector<const MachineInstr *, 4> MultipleValues;
Devang Patelee432862010-05-20 19:57:06 +00002314 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2315 E = DbgValues.end(); I != E; ++I) {
2316 const MachineInstr *MInsn = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00002317 MultipleValues.clear();
2318 if (isDbgValueInDefinedReg(MInsn))
2319 MultipleValues.push_back(MInsn);
Devang Patelee432862010-05-20 19:57:06 +00002320 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2321 if (Processed.count(DV) != 0)
2322 continue;
2323
Devang Patel354eb7e2010-06-02 19:05:13 +00002324 const MachineInstr *PrevMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002325 for (SmallVector<const MachineInstr *, 8>::iterator MI = I+1,
Devang Patelc3f5f782010-05-25 23:40:22 +00002326 ME = DbgValues.end(); MI != ME; ++MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002327 const MDNode *Var =
Devang Patelc3f5f782010-05-25 23:40:22 +00002328 (*MI)->getOperand((*MI)->getNumOperands()-1).getMetadata();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002329 if (Var == DV && isDbgValueInDefinedReg(*MI) &&
Devang Patel354eb7e2010-06-02 19:05:13 +00002330 !PrevMI->isIdenticalTo(*MI))
Devang Patelc3f5f782010-05-25 23:40:22 +00002331 MultipleValues.push_back(*MI);
Devang Patel354eb7e2010-06-02 19:05:13 +00002332 PrevMI = *MI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002333 }
2334
Devang Patelee432862010-05-20 19:57:06 +00002335 DbgScope *Scope = findDbgScope(MInsn);
Devang Pateld8720f42010-05-27 20:25:04 +00002336 bool CurFnArg = false;
2337 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2338 DISubprogram(DV.getContext()).describes(MF->getFunction()))
2339 CurFnArg = true;
2340 if (!Scope && CurFnArg)
Devang Patelc0c5a262010-05-21 00:10:20 +00002341 Scope = CurrentFnDbgScope;
Devang Patelee432862010-05-20 19:57:06 +00002342 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00002343 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00002344 continue;
2345
2346 Processed.insert(DV);
Devang Patelee432862010-05-20 19:57:06 +00002347 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patelee432862010-05-20 19:57:06 +00002348 Scope->addVariable(RegVar);
Devang Pateld8720f42010-05-27 20:25:04 +00002349 if (!CurFnArg)
Jim Grosbach1e20b962010-07-21 21:21:52 +00002350 DbgVariableLabelsMap[RegVar] = getLabelBeforeInsn(MInsn);
Devang Patelc0c5a262010-05-21 00:10:20 +00002351 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2352 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2353 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002354 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002355 if (MultipleValues.size() <= 1) {
2356 DbgVariableToDbgInstMap[RegVar] = MInsn;
2357 continue;
2358 }
2359
2360 // handle multiple DBG_VALUE instructions describing one variable.
Devang Patelc3f5f782010-05-25 23:40:22 +00002361 if (DotDebugLocEntries.empty())
Devang Patel80250682010-05-26 23:55:23 +00002362 RegVar->setDotDebugLocOffset(0);
2363 else
2364 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Devang Patele2df8422010-05-26 17:29:32 +00002365 const MachineInstr *Begin = NULL;
2366 const MachineInstr *End = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002367 for (SmallVector<const MachineInstr *, 4>::iterator
2368 MVI = MultipleValues.begin(), MVE = MultipleValues.end();
Devang Patel61409622010-07-07 20:12:52 +00002369 MVI != MVE; ++MVI) {
Devang Patele2df8422010-05-26 17:29:32 +00002370 if (!Begin) {
2371 Begin = *MVI;
2372 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002373 }
Devang Patele2df8422010-05-26 17:29:32 +00002374 End = *MVI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002375 MachineLocation MLoc;
Devang Patelb2cf5812010-08-04 18:40:52 +00002376 if (Begin->getNumOperands() == 3) {
2377 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2378 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2379 } else
2380 MLoc = Asm->getDebugValueLocation(Begin);
2381
Devang Patele2df8422010-05-26 17:29:32 +00002382 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2383 const MCSymbol *SLabel = getLabelBeforeInsn(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002384 if (MLoc.getReg())
2385 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
2386
Devang Patele2df8422010-05-26 17:29:32 +00002387 Begin = End;
2388 if (MVI + 1 == MVE) {
2389 // If End is the last instruction then its value is valid
Devang Patelc3f5f782010-05-25 23:40:22 +00002390 // until the end of the funtion.
Devang Patelb2cf5812010-08-04 18:40:52 +00002391 MachineLocation EMLoc;
2392 if (End->getNumOperands() == 3) {
2393 if (End->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2394 EMLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2395 } else
2396 EMLoc = Asm->getDebugValueLocation(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002397 if (EMLoc.getReg())
2398 DotDebugLocEntries.
2399 push_back(DotDebugLocEntry(SLabel, FunctionEndSym, EMLoc));
Devang Patelc3f5f782010-05-25 23:40:22 +00002400 }
2401 }
2402 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00002403 }
Devang Patel98e1cac2010-05-14 21:01:35 +00002404
2405 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00002406 const Function *F = MF->getFunction();
2407 const Module *M = F->getParent();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002408 if (NamedMDNode *NMD =
2409 M->getNamedMetadata(Twine("llvm.dbg.lv.",
Devang Patela762b092010-06-22 01:19:38 +00002410 getRealLinkageName(F->getName())))) {
Devang Patel98e1cac2010-05-14 21:01:35 +00002411 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002412 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00002413 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00002414 continue;
2415 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2416 if (Scope)
Devang Patel26c1e562010-05-20 16:36:41 +00002417 Scope->addVariable(new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00002418 }
2419 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002420}
Devang Patel98e1cac2010-05-14 21:01:35 +00002421
Devang Patelc3f5f782010-05-25 23:40:22 +00002422/// getLabelBeforeInsn - Return Label preceding the instruction.
2423const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2424 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2425 LabelsBeforeInsn.find(MI);
2426 if (I == LabelsBeforeInsn.end())
2427 // FunctionBeginSym always preceeds all the instruction in current function.
2428 return FunctionBeginSym;
2429 return I->second;
2430}
2431
2432/// getLabelAfterInsn - Return Label immediately following the instruction.
2433const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2434 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2435 LabelsAfterInsn.find(MI);
2436 if (I == LabelsAfterInsn.end())
2437 return NULL;
2438 return I->second;
Devang Patele717faa2009-10-06 01:26:37 +00002439}
2440
Devang Patel553881b2010-03-29 17:20:31 +00002441/// beginScope - Process beginning of a scope.
2442void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002443 if (InsnNeedsLabel.count(MI) == 0) {
2444 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patel553881b2010-03-29 17:20:31 +00002445 return;
Devang Patelc3f5f782010-05-25 23:40:22 +00002446 }
Devang Patelaead63c2010-03-29 22:59:58 +00002447
Devang Patelb2b31a62010-05-26 19:37:24 +00002448 // Check location.
2449 DebugLoc DL = MI->getDebugLoc();
2450 if (!DL.isUnknown()) {
2451 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2452 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Pateleac9c072010-04-27 19:46:33 +00002453 PrevInstLoc = DL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002454 LabelsBeforeInsn[MI] = PrevLabel;
2455 return;
Devang Pateleac9c072010-04-27 19:46:33 +00002456 }
Devang Patel553881b2010-03-29 17:20:31 +00002457
Jim Grosbach1e20b962010-07-21 21:21:52 +00002458 // If location is unknown then use temp label for this DBG_VALUE
Devang Patelb2b31a62010-05-26 19:37:24 +00002459 // instruction.
2460 if (MI->isDebugValue()) {
Devang Patel77051f52010-05-26 21:23:46 +00002461 PrevLabel = MMI->getContext().CreateTempSymbol();
2462 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00002463 LabelsBeforeInsn[MI] = PrevLabel;
2464 return;
2465 }
2466
2467 if (UnknownLocations) {
2468 PrevLabel = recordSourceLine(0, 0, 0);
2469 LabelsBeforeInsn[MI] = PrevLabel;
2470 return;
2471 }
2472
2473 assert (0 && "Instruction is not processed!");
Devang Patel0d20ac82009-10-06 01:50:42 +00002474}
2475
Devang Patel2c4ceb12009-11-21 02:48:08 +00002476/// endScope - Process end of a scope.
2477void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002478 if (InsnsEndScopeSet.count(MI) != 0) {
2479 // Emit a label if this instruction ends a scope.
2480 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2481 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002482 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002483 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002484}
2485
Devang Pateleac9c072010-04-27 19:46:33 +00002486/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002487DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel61409622010-07-07 20:12:52 +00002488 const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002489 if (!InlinedAt) {
2490 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2491 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002492 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002493 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2494 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002495 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002496 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002497 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002498 WScope->setParent(Parent);
2499 Parent->addScope(WScope);
2500 }
2501
2502 if (!WScope->getParent()) {
2503 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastingscad22ad2010-06-15 23:06:30 +00002504 // We used to check only for a linkage name, but that fails
2505 // since we began omitting the linkage name for private
2506 // functions. The new way is to check for the name in metadata,
2507 // but that's not supported in old .ll test cases. Ergo, we
2508 // check both.
Stuart Hastings215aa152010-06-11 20:08:44 +00002509 if (SPName == Asm->MF->getFunction()->getName() ||
2510 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Pateleac9c072010-04-27 19:46:33 +00002511 CurrentFnDbgScope = WScope;
2512 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002513
Devang Pateleac9c072010-04-27 19:46:33 +00002514 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002515 }
2516
Devang Patel78e127d2010-06-25 22:07:34 +00002517 getOrCreateAbstractScope(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00002518 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2519 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002520 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002521
2522 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2523 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2524 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002525 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002526 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002527 WScope->setParent(Parent);
2528 Parent->addScope(WScope);
2529
2530 ConcreteScopes[InlinedAt] = WScope;
Devang Pateleac9c072010-04-27 19:46:33 +00002531
2532 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002533}
2534
Devang Pateleac9c072010-04-27 19:46:33 +00002535/// hasValidLocation - Return true if debug location entry attached with
2536/// machine instruction encodes valid location info.
2537static bool hasValidLocation(LLVMContext &Ctx,
2538 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002539 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002540 DebugLoc DL = MInsn->getDebugLoc();
2541 if (DL.isUnknown()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002542
Devang Patele9f8f5e2010-05-07 20:54:48 +00002543 const MDNode *S = DL.getScope(Ctx);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002544
Devang Pateleac9c072010-04-27 19:46:33 +00002545 // There is no need to create another DIE for compile unit. For all
2546 // other scopes, create one DbgScope now. This will be translated
2547 // into a scope DIE at the end.
2548 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002549
Devang Pateleac9c072010-04-27 19:46:33 +00002550 Scope = S;
2551 InlinedAt = DL.getInlinedAt(Ctx);
2552 return true;
2553}
2554
2555/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2556/// hierarchy.
2557static void calculateDominanceGraph(DbgScope *Scope) {
2558 assert (Scope && "Unable to calculate scop edominance graph!");
2559 SmallVector<DbgScope *, 4> WorkStack;
2560 WorkStack.push_back(Scope);
2561 unsigned Counter = 0;
2562 while (!WorkStack.empty()) {
2563 DbgScope *WS = WorkStack.back();
2564 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2565 bool visitedChildren = false;
2566 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2567 SE = Children.end(); SI != SE; ++SI) {
2568 DbgScope *ChildScope = *SI;
2569 if (!ChildScope->getDFSOut()) {
2570 WorkStack.push_back(ChildScope);
2571 visitedChildren = true;
2572 ChildScope->setDFSIn(++Counter);
2573 break;
2574 }
2575 }
2576 if (!visitedChildren) {
2577 WorkStack.pop_back();
2578 WS->setDFSOut(++Counter);
2579 }
2580 }
2581}
2582
2583/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002584static
Devang Pateleac9c072010-04-27 19:46:33 +00002585void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2586 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2587{
2588#ifndef NDEBUG
2589 unsigned PrevDFSIn = 0;
2590 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2591 I != E; ++I) {
2592 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2593 II != IE; ++II) {
2594 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002595 const MDNode *Scope = NULL;
2596 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002597
2598 // Check if instruction has valid location information.
2599 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2600 dbgs() << " [ ";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002601 if (InlinedAt)
Devang Pateleac9c072010-04-27 19:46:33 +00002602 dbgs() << "*";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002603 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Pateleac9c072010-04-27 19:46:33 +00002604 MI2ScopeMap.find(MInsn);
2605 if (DI != MI2ScopeMap.end()) {
2606 DbgScope *S = DI->second;
2607 dbgs() << S->getDFSIn();
2608 PrevDFSIn = S->getDFSIn();
2609 } else
2610 dbgs() << PrevDFSIn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002611 } else
Devang Pateleac9c072010-04-27 19:46:33 +00002612 dbgs() << " [ x" << PrevDFSIn;
2613 dbgs() << " ]";
2614 MInsn->dump();
2615 }
2616 dbgs() << "\n";
2617 }
2618#endif
2619}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002620/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002621/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002622bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002623 // If scope information was extracted using .dbg intrinsics then there is not
2624 // any need to extract these information by scanning each instruction.
2625 if (!DbgScopeMap.empty())
2626 return false;
2627
Dan Gohman314bf7c2010-04-23 01:18:53 +00002628 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002629 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2630 SmallVector<DbgRange, 4> MIRanges;
2631 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002632 const MDNode *PrevScope = NULL;
2633 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002634 const MachineInstr *RangeBeginMI = NULL;
2635 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002636 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002637 I != E; ++I) {
2638 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2639 II != IE; ++II) {
2640 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002641 const MDNode *Scope = NULL;
2642 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002643
2644 // Check if instruction has valid location information.
2645 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2646 PrevMI = MInsn;
2647 continue;
2648 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002649
Devang Pateleac9c072010-04-27 19:46:33 +00002650 // If scope has not changed then skip this instruction.
2651 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2652 PrevMI = MInsn;
2653 continue;
2654 }
2655
Jim Grosbach1e20b962010-07-21 21:21:52 +00002656 if (RangeBeginMI) {
2657 // If we have alread seen a beginning of a instruction range and
Devang Pateleac9c072010-04-27 19:46:33 +00002658 // current instruction scope does not match scope of first instruction
2659 // in this range then create a new instruction range.
2660 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002661 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel61409622010-07-07 20:12:52 +00002662 PrevInlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002663 MIRanges.push_back(R);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002664 }
Devang Pateleac9c072010-04-27 19:46:33 +00002665
2666 // This is a beginning of a new instruction range.
2667 RangeBeginMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002668
Devang Pateleac9c072010-04-27 19:46:33 +00002669 // Reset previous markers.
2670 PrevMI = MInsn;
2671 PrevScope = Scope;
2672 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002673 }
2674 }
2675
Devang Pateleac9c072010-04-27 19:46:33 +00002676 // Create last instruction range.
2677 if (RangeBeginMI && PrevMI && PrevScope) {
2678 DbgRange R(RangeBeginMI, PrevMI);
2679 MIRanges.push_back(R);
2680 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002681 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002682
Devang Patel344130e2010-01-04 20:44:00 +00002683 if (!CurrentFnDbgScope)
2684 return false;
2685
Devang Pateleac9c072010-04-27 19:46:33 +00002686 calculateDominanceGraph(CurrentFnDbgScope);
2687 if (PrintDbgScope)
2688 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2689
2690 // Find ranges of instructions covered by each DbgScope;
2691 DbgScope *PrevDbgScope = NULL;
2692 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2693 RE = MIRanges.end(); RI != RE; ++RI) {
2694 const DbgRange &R = *RI;
2695 DbgScope *S = MI2ScopeMap.lookup(R.first);
2696 assert (S && "Lost DbgScope for a machine instruction!");
2697 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2698 PrevDbgScope->closeInsnRange(S);
2699 S->openInsnRange(R.first);
2700 S->extendInsnRange(R.second);
2701 PrevDbgScope = S;
2702 }
2703
2704 if (PrevDbgScope)
2705 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002706
Devang Patele37b0c62010-04-08 18:43:56 +00002707 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002708
2709 return !DbgScopeMap.empty();
2710}
2711
Jim Grosbach1e20b962010-07-21 21:21:52 +00002712/// identifyScopeMarkers() -
Devang Pateleac9c072010-04-27 19:46:33 +00002713/// Each DbgScope has first instruction and last instruction to mark beginning
2714/// and end of a scope respectively. Create an inverse map that list scopes
2715/// starts (and ends) with an instruction. One instruction may start (or end)
2716/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002717void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002718 SmallVector<DbgScope *, 4> WorkList;
2719 WorkList.push_back(CurrentFnDbgScope);
2720 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002721 DbgScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002722
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002723 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002724 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002725 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002726 SE = Children.end(); SI != SE; ++SI)
2727 WorkList.push_back(*SI);
2728
Devang Patel53bb5c92009-11-10 23:06:00 +00002729 if (S->isAbstractScope())
2730 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002731
Devang Pateleac9c072010-04-27 19:46:33 +00002732 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2733 if (Ranges.empty())
2734 continue;
2735 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2736 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002737 assert(RI->first && "DbgRange does not have first instruction!");
2738 assert(RI->second && "DbgRange does not have second instruction!");
Devang Pateleac9c072010-04-27 19:46:33 +00002739 InsnsEndScopeSet.insert(RI->second);
2740 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002741 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002742}
2743
Dan Gohman084751c2010-04-20 00:37:27 +00002744/// FindFirstDebugLoc - Find the first debug location in the function. This
2745/// is intended to be an approximation for the source position of the
2746/// beginning of the function.
2747static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2748 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2749 I != E; ++I)
2750 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2751 MBBI != MBBE; ++MBBI) {
2752 DebugLoc DL = MBBI->getDebugLoc();
2753 if (!DL.isUnknown())
2754 return DL;
2755 }
2756 return DebugLoc();
2757}
2758
Devang Patel2c4ceb12009-11-21 02:48:08 +00002759/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002760/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002761void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002762 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002763 if (!extractScopeInformation()) return;
Devang Patel60b35bd2009-10-06 18:37:31 +00002764
Devang Pateleac9c072010-04-27 19:46:33 +00002765 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2766 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002767 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002768 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002769
2770 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2771 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002772 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002773 if (FDL.isUnknown()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002774
Devang Patele9f8f5e2010-05-07 20:54:48 +00002775 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings0db42712010-07-19 23:56:30 +00002776 const MDNode *TheScope = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002777
Chris Lattnerde4845c2010-04-02 19:42:39 +00002778 DISubprogram SP = getDISubprogram(Scope);
2779 unsigned Line, Col;
2780 if (SP.Verify()) {
2781 Line = SP.getLineNumber();
2782 Col = 0;
Stuart Hastings0db42712010-07-19 23:56:30 +00002783 TheScope = SP;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002784 } else {
2785 Line = FDL.getLine();
2786 Col = FDL.getCol();
Stuart Hastings0db42712010-07-19 23:56:30 +00002787 TheScope = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002788 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002789
Stuart Hastings0db42712010-07-19 23:56:30 +00002790 recordSourceLine(Line, Col, TheScope);
Devang Patelb2b31a62010-05-26 19:37:24 +00002791
Devang Patelb9abe9f2010-06-02 16:42:51 +00002792 /// ProcessedArgs - Collection of arguments already processed.
2793 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2794
Devang Patelb2b31a62010-05-26 19:37:24 +00002795 DebugLoc PrevLoc;
2796 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2797 I != E; ++I)
2798 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2799 II != IE; ++II) {
2800 const MachineInstr *MI = II;
2801 DebugLoc DL = MI->getDebugLoc();
2802 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002803 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2804 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2805 if (!DV.Verify()) continue;
Devang Patel55e97172010-05-27 16:47:30 +00002806 // If DBG_VALUE is for a local variable then it needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002807 if (DV.getTag() != dwarf::DW_TAG_arg_variable
Devang Patel7fb231c2010-07-01 21:38:08 +00002808 && isDbgValueInUndefinedReg(MI) == false)
Devang Patelb2b31a62010-05-26 19:37:24 +00002809 InsnNeedsLabel.insert(MI);
Devang Patel55e97172010-05-27 16:47:30 +00002810 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel7fb231c2010-07-01 21:38:08 +00002811 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2812 describes(MF->getFunction()))
Devang Patel55e97172010-05-27 16:47:30 +00002813 InsnNeedsLabel.insert(MI);
2814 // DBG_VALUE indicating argument location change needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002815 else if (isDbgValueInUndefinedReg(MI) == false
2816 && !ProcessedArgs.insert(DV))
Devang Patelb2b31a62010-05-26 19:37:24 +00002817 InsnNeedsLabel.insert(MI);
2818 } else {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002819 // If location is unknown then instruction needs a location only if
Devang Patelb2b31a62010-05-26 19:37:24 +00002820 // UnknownLocations flag is set.
2821 if (DL.isUnknown()) {
2822 if (UnknownLocations && !PrevLoc.isUnknown())
2823 InsnNeedsLabel.insert(MI);
2824 } else if (DL != PrevLoc)
2825 // Otherwise, instruction needs a location only if it is new location.
2826 InsnNeedsLabel.insert(MI);
2827 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002828
Devang Patelb2b31a62010-05-26 19:37:24 +00002829 if (!DL.isUnknown() || UnknownLocations)
2830 PrevLoc = DL;
2831 }
2832
2833 PrevLabel = FunctionBeginSym;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002834}
2835
Devang Patel2c4ceb12009-11-21 02:48:08 +00002836/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002837///
Chris Lattnereec791a2010-01-26 23:18:02 +00002838void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002839 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002840
Devang Patel344130e2010-01-04 20:44:00 +00002841 if (CurrentFnDbgScope) {
Devang Patel65eb4822010-05-22 00:04:14 +00002842
Devang Patelc3f5f782010-05-25 23:40:22 +00002843 // Define end label for subprogram.
2844 FunctionEndSym = Asm->GetTempSymbol("func_end",
2845 Asm->getFunctionNumber());
2846 // Assumes in correct section after the entry point.
2847 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002848
Devang Patel78e127d2010-06-25 22:07:34 +00002849 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2850 collectVariableInfo(MF, ProcessedVars);
Devang Patel65eb4822010-05-22 00:04:14 +00002851
Devang Patel344130e2010-01-04 20:44:00 +00002852 // Get function line info.
2853 if (!Lines.empty()) {
2854 // Get section line info.
2855 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2856 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2857 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2858 // Append the function info to section info.
2859 SectionLineInfos.insert(SectionLineInfos.end(),
2860 Lines.begin(), Lines.end());
2861 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002862
Devang Patel344130e2010-01-04 20:44:00 +00002863 // Construct abstract scopes.
2864 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel78e127d2010-06-25 22:07:34 +00002865 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel78e127d2010-06-25 22:07:34 +00002866 DISubprogram SP((*AI)->getScopeNode());
2867 if (SP.Verify()) {
2868 // Collect info for variables that were optimized out.
2869 StringRef FName = SP.getLinkageName();
2870 if (FName.empty())
2871 FName = SP.getName();
2872 const Module *M = MF->getFunction()->getParent();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002873 if (NamedMDNode *NMD =
2874 M->getNamedMetadata(Twine("llvm.dbg.lv.",
Devang Patel78e127d2010-06-25 22:07:34 +00002875 getRealLinkageName(FName)))) {
2876 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002877 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00002878 if (!DV || !ProcessedVars.insert(DV))
2879 continue;
Devang Patel1d68d212010-06-30 00:11:08 +00002880 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel78e127d2010-06-25 22:07:34 +00002881 if (Scope)
2882 Scope->addVariable(new DbgVariable(DV));
2883 }
2884 }
2885 }
Devang Patel90e19aa2010-06-30 01:40:11 +00002886 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2887 constructScopeDIE(*AI);
Devang Patel78e127d2010-06-25 22:07:34 +00002888 }
Devang Patel61409622010-07-07 20:12:52 +00002889
Devang Patel9c488372010-05-04 06:15:30 +00002890 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002891
Devang Patel9c488372010-05-04 06:15:30 +00002892 if (!DisableFramePointerElim(*MF))
Jim Grosbach1e20b962010-07-21 21:21:52 +00002893 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
Devang Patel9c488372010-05-04 06:15:30 +00002894 dwarf::DW_FORM_flag, 1);
2895
2896
Chris Lattnerd38fee82010-04-05 00:13:49 +00002897 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002898 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002899 }
2900
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002901 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002902 CurrentFnDbgScope = NULL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002903 InsnNeedsLabel.clear();
Devang Patel26c1e562010-05-20 16:36:41 +00002904 DbgVariableToFrameIndexMap.clear();
2905 VarToAbstractVarMap.clear();
2906 DbgVariableToDbgInstMap.clear();
2907 DbgVariableLabelsMap.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002908 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002909 InsnsEndScopeSet.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002910 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002911 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002912 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002913 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002914 LabelsBeforeInsn.clear();
2915 LabelsAfterInsn.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002916 Lines.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002917 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002918}
2919
Devang Patel26c1e562010-05-20 16:36:41 +00002920/// recordVariableFrameIndex - Record a variable's index.
2921void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2922 assert (V && "Invalid DbgVariable!");
2923 DbgVariableToFrameIndexMap[V] = Index;
2924}
2925
2926/// findVariableFrameIndex - Return true if frame index for the variable
2927/// is found. Update FI to hold value of the index.
2928bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2929 assert (V && "Invalid DbgVariable!");
2930 DenseMap<const DbgVariable *, int>::iterator I =
2931 DbgVariableToFrameIndexMap.find(V);
2932 if (I == DbgVariableToFrameIndexMap.end())
2933 return false;
2934 *FI = I->second;
2935 return true;
2936}
2937
2938/// findVariableLabel - Find MCSymbol for the variable.
2939const MCSymbol *DwarfDebug::findVariableLabel(const DbgVariable *V) {
2940 DenseMap<const DbgVariable *, const MCSymbol *>::iterator I
2941 = DbgVariableLabelsMap.find(V);
2942 if (I == DbgVariableLabelsMap.end())
2943 return NULL;
2944 else return I->second;
2945}
2946
Jim Grosbach1e20b962010-07-21 21:21:52 +00002947/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patelee432862010-05-20 19:57:06 +00002948/// instruction.
2949DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2950 DbgScope *Scope = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002951 LLVMContext &Ctx =
Devang Patelee432862010-05-20 19:57:06 +00002952 MInsn->getParent()->getParent()->getFunction()->getContext();
2953 DebugLoc DL = MInsn->getDebugLoc();
2954
Jim Grosbach1e20b962010-07-21 21:21:52 +00002955 if (DL.isUnknown())
Devang Patelee432862010-05-20 19:57:06 +00002956 return Scope;
2957
2958 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2959 Scope = ConcreteScopes.lookup(IA);
2960 if (Scope == 0)
2961 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002962
Devang Patelee432862010-05-20 19:57:06 +00002963 return Scope;
2964}
2965
2966
Chris Lattnerc6087842010-03-09 04:54:43 +00002967/// recordSourceLine - Register a source line with debug info. Returns the
2968/// unique label that was emitted and which provides correspondence to
2969/// the source line list.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002970MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel61409622010-07-07 20:12:52 +00002971 const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00002972 StringRef Dir;
2973 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002974
Dan Gohman1cc0d622010-05-05 23:41:32 +00002975 unsigned Src = 1;
2976 if (S) {
2977 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002978
Dan Gohman1cc0d622010-05-05 23:41:32 +00002979 if (Scope.isCompileUnit()) {
2980 DICompileUnit CU(S);
2981 Dir = CU.getDirectory();
2982 Fn = CU.getFilename();
2983 } else if (Scope.isSubprogram()) {
2984 DISubprogram SP(S);
2985 Dir = SP.getDirectory();
2986 Fn = SP.getFilename();
2987 } else if (Scope.isLexicalBlock()) {
2988 DILexicalBlock DB(S);
2989 Dir = DB.getDirectory();
2990 Fn = DB.getFilename();
2991 } else
2992 assert(0 && "Unexpected scope info");
2993
2994 Src = GetOrCreateSourceID(Dir, Fn);
2995 }
2996
Chris Lattner63d78362010-03-14 08:36:50 +00002997 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner25b68c62010-03-14 08:15:55 +00002998 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002999
Chris Lattnerc6087842010-03-09 04:54:43 +00003000 Asm->OutStreamer.EmitLabel(Label);
3001 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00003002}
3003
Bill Wendling829e67b2009-05-20 23:22:40 +00003004//===----------------------------------------------------------------------===//
3005// Emit Methods
3006//===----------------------------------------------------------------------===//
3007
Devang Patel2c4ceb12009-11-21 02:48:08 +00003008/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00003009///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003010unsigned
3011DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003012 // Get the children.
3013 const std::vector<DIE *> &Children = Die->getChildren();
3014
3015 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003016 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00003017 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00003018
3019 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003020 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00003021
3022 // Get the abbreviation for this DIE.
3023 unsigned AbbrevNumber = Die->getAbbrevNumber();
3024 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3025
3026 // Set DIE offset
3027 Die->setOffset(Offset);
3028
3029 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00003030 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003031
3032 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
3033 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3034
3035 // Size the DIE attribute values.
3036 for (unsigned i = 0, N = Values.size(); i < N; ++i)
3037 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00003038 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00003039
3040 // Size the DIE children if any.
3041 if (!Children.empty()) {
3042 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
3043 "Children flag not set");
3044
3045 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003046 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00003047
3048 // End of children marker.
3049 Offset += sizeof(int8_t);
3050 }
3051
3052 Die->setSize(Offset - Die->getOffset());
3053 return Offset;
3054}
3055
Devang Patel2c4ceb12009-11-21 02:48:08 +00003056/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00003057///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003058void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00003059 unsigned PrevOffset = 0;
3060 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3061 E = CUMap.end(); I != E; ++I) {
3062 // Compute size of compile unit header.
3063 static unsigned Offset = PrevOffset +
3064 sizeof(int32_t) + // Length of Compilation Unit Info
3065 sizeof(int16_t) + // DWARF version number
3066 sizeof(int32_t) + // Offset Into Abbrev. Section
3067 sizeof(int8_t); // Pointer Size (in bytes)
3068 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
3069 PrevOffset = Offset;
3070 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003071}
3072
Chris Lattner11b8f302010-04-04 23:02:02 +00003073/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
3074/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00003075static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00003076 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003077 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00003078 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003079
Chris Lattner9c69e285532010-04-04 22:59:04 +00003080 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
3081 Asm->OutStreamer.EmitLabel(TmpSym);
3082 return TmpSym;
3083}
3084
3085/// EmitSectionLabels - Emit initial Dwarf sections with a label at
3086/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00003087void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003088 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003089
Bill Wendling94d04b82009-05-20 23:21:38 +00003090 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003091 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003092 DwarfFrameSectionSym =
3093 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3094 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003095
Jim Grosbach1e20b962010-07-21 21:21:52 +00003096 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003097 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003098 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003099 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00003100 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003101
Chris Lattner9c69e285532010-04-04 22:59:04 +00003102 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00003103 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003104
Devang Patelaf608bd2010-08-24 00:06:12 +00003105 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00003106 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3107 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3108 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003109 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003110 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00003111 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3112 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00003113
Devang Patelc3f5f782010-05-25 23:40:22 +00003114 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3115 "section_debug_loc");
3116
Chris Lattner9c69e285532010-04-04 22:59:04 +00003117 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00003118 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003119}
3120
Devang Patel2c4ceb12009-11-21 02:48:08 +00003121/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00003122///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003123void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003124 // Get the abbreviation for this DIE.
3125 unsigned AbbrevNumber = Die->getAbbrevNumber();
3126 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3127
Bill Wendling94d04b82009-05-20 23:21:38 +00003128 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00003129 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00003130 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3131 Twine::utohexstr(Die->getOffset()) + ":0x" +
3132 Twine::utohexstr(Die->getSize()) + " " +
3133 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003134 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003135
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003136 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00003137 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3138
3139 // Emit the DIE attribute values.
3140 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3141 unsigned Attr = AbbrevData[i].getAttribute();
3142 unsigned Form = AbbrevData[i].getForm();
3143 assert(Form && "Too many attributes for DIE (check abbreviation)");
3144
Chris Lattner3f53c832010-04-04 18:52:31 +00003145 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00003146 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003147
Bill Wendling94d04b82009-05-20 23:21:38 +00003148 switch (Attr) {
3149 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00003150 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003151 break;
3152 case dwarf::DW_AT_abstract_origin: {
3153 DIEEntry *E = cast<DIEEntry>(Values[i]);
3154 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00003155 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00003156 Asm->EmitInt32(Addr);
3157 break;
3158 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003159 case dwarf::DW_AT_ranges: {
3160 // DW_AT_range Value encodes offset in debug_range section.
3161 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00003162
3163 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
3164 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
3165 V->getValue(),
3166 4);
3167 } else {
3168 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3169 V->getValue(),
3170 DwarfDebugRangeSectionSym,
3171 4);
3172 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003173 break;
3174 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003175 case dwarf::DW_AT_location: {
3176 if (UseDotDebugLocEntry.count(Die) != 0) {
3177 DIELabel *L = cast<DIELabel>(Values[i]);
3178 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3179 } else
3180 Values[i]->EmitValue(Asm, Form);
3181 break;
3182 }
Devang Patel2a361602010-09-29 19:08:08 +00003183 case dwarf::DW_AT_accessibility: {
3184 if (Asm->isVerbose()) {
3185 DIEInteger *V = cast<DIEInteger>(Values[i]);
3186 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
3187 }
3188 Values[i]->EmitValue(Asm, Form);
3189 break;
3190 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003191 default:
3192 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003193 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00003194 break;
3195 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003196 }
3197
3198 // Emit the DIE children if any.
3199 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3200 const std::vector<DIE *> &Children = Die->getChildren();
3201
3202 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003203 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00003204
Chris Lattner3f53c832010-04-04 18:52:31 +00003205 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00003206 Asm->OutStreamer.AddComment("End Of Children Mark");
3207 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003208 }
3209}
3210
Devang Patel8a241142009-12-09 18:24:21 +00003211/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003212///
Devang Patel8a241142009-12-09 18:24:21 +00003213void DwarfDebug::emitDebugInfo() {
3214 // Start debug info section.
3215 Asm->OutStreamer.SwitchSection(
3216 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00003217 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3218 E = CUMap.end(); I != E; ++I) {
3219 CompileUnit *TheCU = I->second;
3220 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00003221
Devang Patel163a9f72010-05-10 22:49:55 +00003222 // Emit the compile units header.
3223 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3224 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003225
Devang Patel163a9f72010-05-10 22:49:55 +00003226 // Emit size of content not including length itself
3227 unsigned ContentSize = Die->getSize() +
3228 sizeof(int16_t) + // DWARF version number
3229 sizeof(int32_t) + // Offset Into Abbrev. Section
3230 sizeof(int8_t) + // Pointer Size (in bytes)
3231 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003232
Devang Patel163a9f72010-05-10 22:49:55 +00003233 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3234 Asm->EmitInt32(ContentSize);
3235 Asm->OutStreamer.AddComment("DWARF version number");
3236 Asm->EmitInt16(dwarf::DWARF_VERSION);
3237 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3238 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3239 DwarfAbbrevSectionSym);
3240 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3241 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003242
Devang Patel163a9f72010-05-10 22:49:55 +00003243 emitDIE(Die);
3244 // FIXME - extra padding for gdb bug.
3245 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3246 Asm->EmitInt8(0);
3247 Asm->EmitInt8(0);
3248 Asm->EmitInt8(0);
3249 Asm->EmitInt8(0);
3250 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3251 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003252}
3253
Devang Patel2c4ceb12009-11-21 02:48:08 +00003254/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003255///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003256void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00003257 // Check to see if it is worth the effort.
3258 if (!Abbreviations.empty()) {
3259 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003260 Asm->OutStreamer.SwitchSection(
3261 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003262
Chris Lattnerc0215722010-04-04 19:25:43 +00003263 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003264
3265 // For each abbrevation.
3266 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3267 // Get abbreviation data
3268 const DIEAbbrev *Abbrev = Abbreviations[i];
3269
3270 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003271 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00003272
3273 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003274 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00003275 }
3276
3277 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003278 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00003279
Chris Lattnerc0215722010-04-04 19:25:43 +00003280 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003281 }
3282}
3283
Devang Patel2c4ceb12009-11-21 02:48:08 +00003284/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00003285/// the line matrix.
3286///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003287void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003288 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00003289 Asm->OutStreamer.AddComment("Extended Op");
3290 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003291
Chris Lattner233f52b2010-03-09 23:52:58 +00003292 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003293 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003294 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3295 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3296
3297 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003298
Chris Lattnerc0215722010-04-04 19:25:43 +00003299 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003300 Asm->getTargetData().getPointerSize(),
3301 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003302
3303 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00003304 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3305 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00003306 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00003307 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003308}
3309
Devang Patel2c4ceb12009-11-21 02:48:08 +00003310/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00003311///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003312void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003313 // If the target is using .loc/.file, the assembler will be emitting the
3314 // .debug_line table automatically.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003315 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00003316 return;
3317
3318 // Minimum line delta, thus ranging from -10..(255-10).
3319 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
3320 // Maximum line delta, thus ranging from -10..(255-10).
3321 const int MaxLineDelta = 255 + MinLineDelta;
3322
3323 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003324 Asm->OutStreamer.SwitchSection(
3325 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003326
3327 // Construct the section header.
Chris Lattner233f52b2010-03-09 23:52:58 +00003328 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnera6437182010-04-04 19:58:12 +00003329 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
3330 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00003331 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003332
Chris Lattner233f52b2010-03-09 23:52:58 +00003333 Asm->OutStreamer.AddComment("DWARF version number");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003334 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00003335
Chris Lattner233f52b2010-03-09 23:52:58 +00003336 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnera6437182010-04-04 19:58:12 +00003337 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
3338 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00003339 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003340
Chris Lattner233f52b2010-03-09 23:52:58 +00003341 Asm->OutStreamer.AddComment("Minimum Instruction Length");
3342 Asm->EmitInt8(1);
3343 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
3344 Asm->EmitInt8(1);
3345 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
3346 Asm->EmitInt8(MinLineDelta);
3347 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3348 Asm->EmitInt8(MaxLineDelta);
3349 Asm->OutStreamer.AddComment("Special Opcode Base");
3350 Asm->EmitInt8(-MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003351
3352 // Line number standard opcode encodings argument count
Chris Lattner233f52b2010-03-09 23:52:58 +00003353 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3354 Asm->EmitInt8(0);
3355 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3356 Asm->EmitInt8(1);
3357 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3358 Asm->EmitInt8(1);
3359 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3360 Asm->EmitInt8(1);
3361 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3362 Asm->EmitInt8(1);
3363 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3364 Asm->EmitInt8(0);
3365 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3366 Asm->EmitInt8(0);
3367 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3368 Asm->EmitInt8(0);
3369 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3370 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003371
3372 // Emit directories.
3373 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00003374 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner3f53c832010-04-04 18:52:31 +00003375 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003376 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003377 }
3378
Chris Lattner233f52b2010-03-09 23:52:58 +00003379 Asm->OutStreamer.AddComment("End of directories");
3380 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003381
3382 // Emit files.
3383 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3384 // Remember source id starts at 1.
3385 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003386 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner3f53c832010-04-04 18:52:31 +00003387 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003388 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003389
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003390 Asm->EmitULEB128(Id.first, "Directory #");
3391 Asm->EmitULEB128(0, "Mod date");
3392 Asm->EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00003393 }
3394
Chris Lattner233f52b2010-03-09 23:52:58 +00003395 Asm->OutStreamer.AddComment("End of files");
3396 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003397
Chris Lattnerc0215722010-04-04 19:25:43 +00003398 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003399
3400 // A sequence for each text section.
3401 unsigned SecSrcLinesSize = SectionSourceLines.size();
3402
3403 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3404 // Isolate current sections line info.
3405 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3406
Bill Wendling94d04b82009-05-20 23:21:38 +00003407 // Dwarf assumes we start with first line of first source file.
3408 unsigned Source = 1;
3409 unsigned Line = 1;
3410
3411 // Construct rows of the address, source, line, column matrix.
3412 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3413 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner25b68c62010-03-14 08:15:55 +00003414 MCSymbol *Label = LineInfo.getLabel();
Chris Lattnerb9130602010-03-14 02:20:58 +00003415 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling94d04b82009-05-20 23:21:38 +00003416
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003417 if (Asm->isVerbose()) {
Chris Lattner188a87d2010-03-10 01:04:13 +00003418 std::pair<unsigned, unsigned> SrcID =
Bill Wendling94d04b82009-05-20 23:21:38 +00003419 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner188a87d2010-03-10 01:04:13 +00003420 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner49f618a2010-03-10 02:29:31 +00003421 "/" +
3422 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner188a87d2010-03-10 01:04:13 +00003423 ":" + Twine(LineInfo.getLine()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003424 }
3425
3426 // Define the line address.
Chris Lattner233f52b2010-03-09 23:52:58 +00003427 Asm->OutStreamer.AddComment("Extended Op");
3428 Asm->EmitInt8(0);
3429 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003430 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003431
3432 Asm->OutStreamer.AddComment("DW_LNE_set_address");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003433 Asm->EmitInt8(dwarf::DW_LNE_set_address);
Chris Lattner233f52b2010-03-09 23:52:58 +00003434
3435 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003436 Asm->OutStreamer.EmitSymbolValue(Label,
3437 Asm->getTargetData().getPointerSize(),
Chris Lattnerb9130602010-03-14 02:20:58 +00003438 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003439
Bill Wendling94d04b82009-05-20 23:21:38 +00003440 // If change of source, then switch to the new source.
3441 if (Source != LineInfo.getSourceID()) {
3442 Source = LineInfo.getSourceID();
Chris Lattner233f52b2010-03-09 23:52:58 +00003443 Asm->OutStreamer.AddComment("DW_LNS_set_file");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003444 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003445 Asm->EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00003446 }
3447
3448 // If change of line.
3449 if (Line != LineInfo.getLine()) {
3450 // Determine offset.
3451 int Offset = LineInfo.getLine() - Line;
3452 int Delta = Offset - MinLineDelta;
3453
3454 // Update line.
3455 Line = LineInfo.getLine();
3456
3457 // If delta is small enough and in range...
3458 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3459 // ... then use fast opcode.
Chris Lattner233f52b2010-03-09 23:52:58 +00003460 Asm->OutStreamer.AddComment("Line Delta");
3461 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003462 } else {
3463 // ... otherwise use long hand.
Chris Lattner233f52b2010-03-09 23:52:58 +00003464 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling94d04b82009-05-20 23:21:38 +00003465 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003466 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner233f52b2010-03-09 23:52:58 +00003467 Asm->OutStreamer.AddComment("DW_LNS_copy");
3468 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003469 }
3470 } else {
3471 // Copy the previous row (different address or source)
Chris Lattner233f52b2010-03-09 23:52:58 +00003472 Asm->OutStreamer.AddComment("DW_LNS_copy");
3473 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003474 }
3475 }
3476
Devang Patel2c4ceb12009-11-21 02:48:08 +00003477 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003478 }
3479
3480 if (SecSrcLinesSize == 0)
3481 // Because we're emitting a debug_line section, we still need a line
3482 // table. The linker and friends expect it to exist. If there's nothing to
3483 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003484 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003485
Chris Lattnerc0215722010-04-04 19:25:43 +00003486 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003487}
3488
Devang Patel2c4ceb12009-11-21 02:48:08 +00003489/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003490///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003491void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003492 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003493 return;
3494
Chris Lattnerd38fee82010-04-05 00:13:49 +00003495 int stackGrowth = Asm->getTargetData().getPointerSize();
3496 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3497 TargetFrameInfo::StackGrowsDown)
3498 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003499
3500 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003501 Asm->OutStreamer.SwitchSection(
3502 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003503
Chris Lattnerc0215722010-04-04 19:25:43 +00003504 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003505 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003506 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3507 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003508
Chris Lattnerc0215722010-04-04 19:25:43 +00003509 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003510 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003511 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003512 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003513 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003514 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003515 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003516 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3517 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003518 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003519 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003520 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003521
3522 std::vector<MachineMove> Moves;
3523 RI->getInitialFrameState(Moves);
3524
Chris Lattner02b86b92010-04-04 23:41:46 +00003525 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003526
Chris Lattner059ea132010-04-28 01:05:45 +00003527 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003528 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003529}
3530
Devang Patel2c4ceb12009-11-21 02:48:08 +00003531/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003532/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003533void DwarfDebug::
3534emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003535 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003536 return;
3537
3538 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003539 Asm->OutStreamer.SwitchSection(
3540 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003541
Chris Lattner233f52b2010-03-09 23:52:58 +00003542 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003543 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003544 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003545 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003546 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003547 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003548
Chris Lattner206d61e2010-03-13 07:26:18 +00003549 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003550
Chris Lattner233f52b2010-03-09 23:52:58 +00003551 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003552 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner6189ed12010-04-04 23:25:33 +00003553 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003554
Chris Lattner233f52b2010-03-09 23:52:58 +00003555 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003556 MCSymbol *FuncBeginSym =
3557 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003558 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003559 Asm->getTargetData().getPointerSize(),
3560 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003561
3562
Chris Lattner233f52b2010-03-09 23:52:58 +00003563 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003564 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003565 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003566
Chris Lattner02b86b92010-04-04 23:41:46 +00003567 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003568
Chris Lattner059ea132010-04-28 01:05:45 +00003569 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003570 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003571}
3572
Devang Patel8a241142009-12-09 18:24:21 +00003573/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3574///
3575void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00003576 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3577 E = CUMap.end(); I != E; ++I) {
3578 CompileUnit *TheCU = I->second;
3579 // Start the dwarf pubnames section.
3580 Asm->OutStreamer.SwitchSection(
3581 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003582
Devang Patel163a9f72010-05-10 22:49:55 +00003583 Asm->OutStreamer.AddComment("Length of Public Names Info");
3584 Asm->EmitLabelDifference(
3585 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3586 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003587
Devang Patel163a9f72010-05-10 22:49:55 +00003588 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3589 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003590
Devang Patel163a9f72010-05-10 22:49:55 +00003591 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003592 Asm->EmitInt16(dwarf::DWARF_VERSION);
3593
Devang Patel163a9f72010-05-10 22:49:55 +00003594 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003595 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00003596 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003597
Devang Patel163a9f72010-05-10 22:49:55 +00003598 Asm->OutStreamer.AddComment("Compilation Unit Length");
3599 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3600 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3601 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003602
Devang Patel163a9f72010-05-10 22:49:55 +00003603 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3604 for (StringMap<DIE*>::const_iterator
3605 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3606 const char *Name = GI->getKeyData();
3607 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003608
Devang Patel163a9f72010-05-10 22:49:55 +00003609 Asm->OutStreamer.AddComment("DIE offset");
3610 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003611
Devang Patel163a9f72010-05-10 22:49:55 +00003612 if (Asm->isVerbose())
3613 Asm->OutStreamer.AddComment("External Name");
3614 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3615 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003616
Devang Patel163a9f72010-05-10 22:49:55 +00003617 Asm->OutStreamer.AddComment("End Mark");
3618 Asm->EmitInt32(0);
3619 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3620 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003621 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003622}
3623
Devang Patel193f7202009-11-24 01:14:22 +00003624void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00003625 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3626 E = CUMap.end(); I != E; ++I) {
3627 CompileUnit *TheCU = I->second;
3628 // Start the dwarf pubnames section.
3629 Asm->OutStreamer.SwitchSection(
3630 Asm->getObjFileLowering().getDwarfPubTypesSection());
3631 Asm->OutStreamer.AddComment("Length of Public Types Info");
3632 Asm->EmitLabelDifference(
3633 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3634 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003635
Devang Patel163a9f72010-05-10 22:49:55 +00003636 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3637 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003638
Devang Patel163a9f72010-05-10 22:49:55 +00003639 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3640 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003641
Devang Patel163a9f72010-05-10 22:49:55 +00003642 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3643 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3644 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003645
Devang Patel163a9f72010-05-10 22:49:55 +00003646 Asm->OutStreamer.AddComment("Compilation Unit Length");
3647 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3648 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3649 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003650
Devang Patel163a9f72010-05-10 22:49:55 +00003651 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3652 for (StringMap<DIE*>::const_iterator
3653 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3654 const char *Name = GI->getKeyData();
3655 DIE * Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003656
Devang Patel163a9f72010-05-10 22:49:55 +00003657 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3658 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003659
Devang Patel163a9f72010-05-10 22:49:55 +00003660 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3661 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3662 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003663
Devang Patel163a9f72010-05-10 22:49:55 +00003664 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003665 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00003666 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3667 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003668 }
Devang Patel193f7202009-11-24 01:14:22 +00003669}
3670
Devang Patel2c4ceb12009-11-21 02:48:08 +00003671/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003672///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003673void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003674 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003675 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003676
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003677 // Start the dwarf str section.
3678 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003679 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003680
Chris Lattnerbc733f52010-03-13 02:17:42 +00003681 // Get all of the string pool entries and put them in an array by their ID so
3682 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003683 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00003684 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003685
Chris Lattnerbc733f52010-03-13 02:17:42 +00003686 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3687 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3688 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003689
Chris Lattnerbc733f52010-03-13 02:17:42 +00003690 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003691
Chris Lattnerbc733f52010-03-13 02:17:42 +00003692 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003693 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003694 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003695
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003696 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003697 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003698 }
3699}
3700
Devang Patel2c4ceb12009-11-21 02:48:08 +00003701/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003702///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003703void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00003704 if (DotDebugLocEntries.empty())
3705 return;
3706
Bill Wendling94d04b82009-05-20 23:21:38 +00003707 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003708 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00003709 Asm->getObjFileLowering().getDwarfLocSection());
3710 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00003711 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3712 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003713 for (SmallVector<DotDebugLocEntry, 4>::iterator
3714 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00003715 I != E; ++I, ++index) {
Devang Patelc3f5f782010-05-25 23:40:22 +00003716 DotDebugLocEntry Entry = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00003717 if (Entry.isEmpty()) {
3718 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3719 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00003720 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00003721 } else {
3722 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3723 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3724 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3725 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patelb2cf5812010-08-04 18:40:52 +00003726 if (int Offset = Entry.Loc.getOffset()) {
3727 // If the value is at a certain offset from frame register then
3728 // use DW_OP_fbreg.
3729 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patelc3f5f782010-05-25 23:40:22 +00003730 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patelb2cf5812010-08-04 18:40:52 +00003731 Asm->EmitInt16(1 + OffsetSize);
3732 Asm->OutStreamer.AddComment(
3733 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
3734 Asm->EmitInt8(dwarf::DW_OP_fbreg);
3735 Asm->OutStreamer.AddComment("Offset");
3736 Asm->EmitSLEB128(Offset);
Devang Patelc3f5f782010-05-25 23:40:22 +00003737 } else {
Devang Patelb2cf5812010-08-04 18:40:52 +00003738 if (Reg < 32) {
3739 Asm->OutStreamer.AddComment("Loc expr size");
3740 Asm->EmitInt16(1);
3741 Asm->OutStreamer.AddComment(
Devang Patela54e0cc2010-08-04 20:32:36 +00003742 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patelb2cf5812010-08-04 18:40:52 +00003743 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3744 } else {
3745 Asm->OutStreamer.AddComment("Loc expr size");
3746 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
3747 Asm->EmitInt8(dwarf::DW_OP_regx);
3748 Asm->EmitULEB128(Reg);
3749 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003750 }
3751 }
3752 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003753}
3754
3755/// EmitDebugARanges - Emit visible names into a debug aranges section.
3756///
3757void DwarfDebug::EmitDebugARanges() {
3758 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003759 Asm->OutStreamer.SwitchSection(
3760 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003761}
3762
Devang Patel2c4ceb12009-11-21 02:48:08 +00003763/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003764///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003765void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003766 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003767 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003768 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003769 unsigned char Size = Asm->getTargetData().getPointerSize();
3770 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00003771 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00003772 I != E; ++I) {
3773 if (*I)
3774 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003775 else
Devang Pateleac9c072010-04-27 19:46:33 +00003776 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003777 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003778}
3779
Devang Patel2c4ceb12009-11-21 02:48:08 +00003780/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003781///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003782void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003783 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003784 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003785 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003786 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003787 }
3788}
3789
Devang Patel2c4ceb12009-11-21 02:48:08 +00003790/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003791/// Section Header:
3792/// 1. length of section
3793/// 2. Dwarf version number
3794/// 3. address size.
3795///
3796/// Entries (one "entry" for each function that was inlined):
3797///
3798/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3799/// otherwise offset into __debug_str for regular function name.
3800/// 2. offset into __debug_str section for regular function name.
3801/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3802/// instances for the function.
3803///
3804/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3805/// inlined instance; the die_offset points to the inlined_subroutine die in the
3806/// __debug_info section, and the low_pc is the starting address for the
3807/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003808void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003809 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003810 return;
3811
Devang Patel163a9f72010-05-10 22:49:55 +00003812 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003813 return;
3814
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003815 Asm->OutStreamer.SwitchSection(
3816 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003817
Chris Lattner233f52b2010-03-09 23:52:58 +00003818 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003819 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3820 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003821
Chris Lattnerc0215722010-04-04 19:25:43 +00003822 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003823
Chris Lattner233f52b2010-03-09 23:52:58 +00003824 Asm->OutStreamer.AddComment("Dwarf Version");
3825 Asm->EmitInt16(dwarf::DWARF_VERSION);
3826 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003827 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003828
Devang Patele9f8f5e2010-05-07 20:54:48 +00003829 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003830 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003831
Devang Patele9f8f5e2010-05-07 20:54:48 +00003832 const MDNode *Node = *I;
3833 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003834 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003835 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003836 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003837 StringRef LName = SP.getLinkageName();
3838 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003839
Chris Lattner233f52b2010-03-09 23:52:58 +00003840 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003841 if (LName.empty()) {
3842 Asm->OutStreamer.EmitBytes(Name, 0);
3843 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003844 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003845 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3846 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003847
Chris Lattner233f52b2010-03-09 23:52:58 +00003848 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003849 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003850 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003851
Devang Patel53bb5c92009-11-10 23:06:00 +00003852 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003853 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003854 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003855 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003856
Chris Lattner3f53c832010-04-04 18:52:31 +00003857 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003858 Asm->OutStreamer.EmitSymbolValue(LI->first,
3859 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003860 }
3861 }
3862
Chris Lattnerc0215722010-04-04 19:25:43 +00003863 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003864}