blob: fbf6526396ace4c0658f9116033f22fe2026d7bd [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"
Devang Patel9a31f0f2010-10-25 20:45:32 +000033#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000034#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000035#include "llvm/ADT/StringExtras.h"
Devang Pateleac9c072010-04-27 19:46:33 +000036#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000039#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000040#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000041#include "llvm/Support/Timer.h"
42#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000043using namespace llvm;
44
Devang Pateleac9c072010-04-27 19:46:33 +000045static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
46 cl::desc("Print DbgScope information for each machine instruction"));
47
Jim Grosbach1e20b962010-07-21 21:21:52 +000048static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000049 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000050 cl::desc("Disable debug info printing"));
51
Dan Gohman281d65d2010-05-07 01:08:53 +000052static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
53 cl::desc("Make an absense of debug location information explicit."),
54 cl::init(false));
55
Nick Lewyckya568d662010-10-26 00:51:57 +000056#ifndef NDEBUG
Devang Patel9a31f0f2010-10-25 20:45:32 +000057STATISTIC(BlocksWithoutLineNo, "Number of blocks without any line number");
Nick Lewyckya568d662010-10-26 00:51:57 +000058#endif
Devang Patel9a31f0f2010-10-25 20:45:32 +000059
Bill Wendling5f017e82010-04-07 09:28:04 +000060namespace {
61 const char *DWARFGroupName = "DWARF Emission";
62 const char *DbgTimerName = "DWARF Debug Writer";
63} // end anonymous namespace
64
Bill Wendling0310d762009-05-15 09:23:25 +000065//===----------------------------------------------------------------------===//
66
67/// Configuration values for initial hash set sizes (log2).
68///
Bill Wendling0310d762009-05-15 09:23:25 +000069static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000070
71namespace llvm {
72
73//===----------------------------------------------------------------------===//
74/// CompileUnit - This dwarf writer support class manages information associate
75/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000076class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000077 /// ID - File identifier for source.
78 ///
79 unsigned ID;
80
81 /// Die - Compile unit debug information entry.
82 ///
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000083 const OwningPtr<DIE> CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000084
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +000085 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel6f01d9c2009-11-21 00:31:03 +000086 DIE *IndexTyDie;
87
Devang Patel869aa462010-07-07 20:49:57 +000088 /// MDNodeToDieMap - Tracks the mapping of unit level debug informaton
Bill Wendling0310d762009-05-15 09:23:25 +000089 /// variables to debug information entries.
Devang Patel869aa462010-07-07 20:49:57 +000090 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000091
Devang Patel869aa462010-07-07 20:49:57 +000092 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug informaton
Bill Wendling0310d762009-05-15 09:23:25 +000093 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel869aa462010-07-07 20:49:57 +000094 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000095
96 /// Globals - A map of globally visible named entities for this unit.
97 ///
98 StringMap<DIE*> Globals;
99
Devang Patel193f7202009-11-24 01:14:22 +0000100 /// GlobalTypes - A map of globally visible types for this unit.
101 ///
102 StringMap<DIE*> GlobalTypes;
103
Bill Wendling0310d762009-05-15 09:23:25 +0000104public:
105 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000106 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000107
108 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +0000109 unsigned getID() const { return ID; }
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +0000110 DIE* getCUDie() const { return CUDie.get(); }
Devang Patel193f7202009-11-24 01:14:22 +0000111 const StringMap<DIE*> &getGlobals() const { return Globals; }
112 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +0000113
114 /// hasContent - Return true if this compile unit has something to write out.
115 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000116 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000117
Devang Patel2c4ceb12009-11-21 02:48:08 +0000118 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000119 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000120 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +0000121
Devang Patel193f7202009-11-24 01:14:22 +0000122 /// addGlobalType - Add a new global type to the compile unit.
123 ///
Jim Grosbach1e20b962010-07-21 21:21:52 +0000124 void addGlobalType(StringRef Name, DIE *Die) {
125 GlobalTypes[Name] = Die;
Devang Patel193f7202009-11-24 01:14:22 +0000126 }
127
Devang Patel017d1212009-11-20 21:37:22 +0000128 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000129 /// specified debug variable.
Devang Patel869aa462010-07-07 20:49:57 +0000130 DIE *getDIE(const MDNode *N) { return MDNodeToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000131
Devang Patel017d1212009-11-20 21:37:22 +0000132 /// insertDIE - Insert DIE into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000133 void insertDIE(const MDNode *N, DIE *D) {
Devang Patel869aa462010-07-07 20:49:57 +0000134 MDNodeToDieMap.insert(std::make_pair(N, D));
Devang Patel017d1212009-11-20 21:37:22 +0000135 }
Bill Wendling0310d762009-05-15 09:23:25 +0000136
Devang Patel017d1212009-11-20 21:37:22 +0000137 /// getDIEEntry - Returns the debug information entry for the speciefied
138 /// debug variable.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000139 DIEEntry *getDIEEntry(const MDNode *N) {
140 DenseMap<const MDNode *, DIEEntry *>::iterator I =
141 MDNodeToDIEEntryMap.find(N);
Devang Patel869aa462010-07-07 20:49:57 +0000142 if (I == MDNodeToDIEEntryMap.end())
Devang Patel6404e4e2009-12-15 19:16:48 +0000143 return NULL;
144 return I->second;
145 }
Devang Patel017d1212009-11-20 21:37:22 +0000146
147 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000148 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Patel869aa462010-07-07 20:49:57 +0000149 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000150 }
151
Devang Patel2c4ceb12009-11-21 02:48:08 +0000152 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000153 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000154 void addDie(DIE *Buffer) {
155 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000156 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000157
158 // getIndexTyDie - Get an anonymous type for index type.
159 DIE *getIndexTyDie() {
160 return IndexTyDie;
161 }
162
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000163 // setIndexTyDie - Set D as anonymous type for index which can be reused
164 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000165 void setIndexTyDie(DIE *D) {
166 IndexTyDie = D;
167 }
168
Bill Wendling0310d762009-05-15 09:23:25 +0000169};
170
171//===----------------------------------------------------------------------===//
172/// DbgVariable - This class is used to track local variable information.
173///
Devang Patelf76a3d62009-11-16 21:53:40 +0000174class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000175 DIVariable Var; // Variable Descriptor.
Devang Patelc3f5f782010-05-25 23:40:22 +0000176 DIE *TheDIE; // Variable DIE.
177 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
Bill Wendling0310d762009-05-15 09:23:25 +0000178public:
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000179 // AbsVar may be NULL.
Devang Patelc3f5f782010-05-25 23:40:22 +0000180 DbgVariable(DIVariable V) : Var(V), TheDIE(0), DotDebugLocOffset(~0U) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000181
182 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000183 DIVariable getVariable() const { return Var; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000184 void setDIE(DIE *D) { TheDIE = D; }
185 DIE *getDIE() const { return TheDIE; }
Devang Patelc3f5f782010-05-25 23:40:22 +0000186 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
187 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
Devang Patel8bd11de2010-08-09 21:01:39 +0000188 StringRef getName() const { return Var.getName(); }
189 unsigned getTag() const { return Var.getTag(); }
190 bool variableHasComplexAddress() const {
191 assert(Var.Verify() && "Invalid complex DbgVariable!");
192 return Var.hasComplexAddress();
193 }
194 bool isBlockByrefVariable() const {
195 assert(Var.Verify() && "Invalid complex DbgVariable!");
196 return Var.isBlockByrefVariable();
197 }
198 unsigned getNumAddrElements() const {
199 assert(Var.Verify() && "Invalid complex DbgVariable!");
200 return Var.getNumAddrElements();
201 }
202 uint64_t getAddrElement(unsigned i) const {
203 return Var.getAddrElement(i);
204 }
205 DIType getType() const {
206 DIType Ty = Var.getType();
207 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
208 // addresses instead.
209 if (Var.isBlockByrefVariable()) {
210 /* Byref variables, in Blocks, are declared by the programmer as
211 "SomeType VarName;", but the compiler creates a
212 __Block_byref_x_VarName struct, and gives the variable VarName
213 either the struct, or a pointer to the struct, as its type. This
214 is necessary for various behind-the-scenes things the compiler
215 needs to do with by-reference variables in blocks.
216
217 However, as far as the original *programmer* is concerned, the
218 variable should still have type 'SomeType', as originally declared.
219
220 The following function dives into the __Block_byref_x_VarName
221 struct to find the original type of the variable. This will be
222 passed back to the code generating the type for the Debug
223 Information Entry for the variable 'VarName'. 'VarName' will then
224 have the original type 'SomeType' in its debug information.
225
226 The original type 'SomeType' will be the type of the field named
227 'VarName' inside the __Block_byref_x_VarName struct.
228
229 NOTE: In order for this to not completely fail on the debugger
230 side, the Debug Information Entry for the variable VarName needs to
231 have a DW_AT_location that tells the debugger how to unwind through
232 the pointers and __Block_byref_x_VarName struct to find the actual
233 value of the variable. The function addBlockByrefType does this. */
234 DIType subType = Ty;
235 unsigned tag = Ty.getTag();
236
237 if (tag == dwarf::DW_TAG_pointer_type) {
238 DIDerivedType DTy = DIDerivedType(Ty);
239 subType = DTy.getTypeDerivedFrom();
240 }
241
242 DICompositeType blockStruct = DICompositeType(subType);
243 DIArray Elements = blockStruct.getTypeArray();
244
245 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
246 DIDescriptor Element = Elements.getElement(i);
247 DIDerivedType DT = DIDerivedType(Element);
248 if (getName() == DT.getName())
249 return (DT.getTypeDerivedFrom());
250 }
251 return Ty;
252 }
253 return Ty;
254 }
Bill Wendling0310d762009-05-15 09:23:25 +0000255};
256
257//===----------------------------------------------------------------------===//
Devang Pateleac9c072010-04-27 19:46:33 +0000258/// DbgRange - This is used to track range of instructions with identical
259/// debug info scope.
260///
261typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
262
263//===----------------------------------------------------------------------===//
Bill Wendling0310d762009-05-15 09:23:25 +0000264/// DbgScope - This class is used to track scope information.
265///
Devang Patelf76a3d62009-11-16 21:53:40 +0000266class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000267 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000268 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000269 // Location at which this scope is inlined.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000270 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000271 bool AbstractScope; // Abstract Scope
Devang Pateld38dd112009-10-01 18:25:23 +0000272 const MachineInstr *LastInsn; // Last instruction of this scope.
273 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Pateleac9c072010-04-27 19:46:33 +0000274 unsigned DFSIn, DFSOut;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000275 // Scopes defined in scope. Contents not owned.
276 SmallVector<DbgScope *, 4> Scopes;
277 // Variables declared in scope. Contents owned.
278 SmallVector<DbgVariable *, 8> Variables;
Devang Pateleac9c072010-04-27 19:46:33 +0000279 SmallVector<DbgRange, 4> Ranges;
Owen Anderson04c05f72009-06-24 22:53:20 +0000280 // Private state for dump()
281 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000282public:
Devang Patele9f8f5e2010-05-07 20:54:48 +0000283 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000284 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Pateleac9c072010-04-27 19:46:33 +0000285 LastInsn(0), FirstInsn(0),
286 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000287 virtual ~DbgScope();
288
289 // Accessors.
290 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000291 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000292 DIDescriptor getDesc() const { return Desc; }
Devang Patele9f8f5e2010-05-07 20:54:48 +0000293 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
294 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000295 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
Devang Patele03161c2010-08-09 18:51:29 +0000296 const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
Devang Pateleac9c072010-04-27 19:46:33 +0000297 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
298
299 /// openInsnRange - This scope covers instruction range starting from MI.
300 void openInsnRange(const MachineInstr *MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000301 if (!FirstInsn)
Devang Pateleac9c072010-04-27 19:46:33 +0000302 FirstInsn = MI;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000303
Devang Pateleac9c072010-04-27 19:46:33 +0000304 if (Parent)
305 Parent->openInsnRange(MI);
306 }
307
Jim Grosbach1e20b962010-07-21 21:21:52 +0000308 /// extendInsnRange - Extend the current instruction range covered by
Devang Pateleac9c072010-04-27 19:46:33 +0000309 /// this scope.
310 void extendInsnRange(const MachineInstr *MI) {
311 assert (FirstInsn && "MI Range is not open!");
312 LastInsn = MI;
313 if (Parent)
314 Parent->extendInsnRange(MI);
315 }
316
317 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
318 /// until now. This is used when a new scope is encountered while walking
319 /// machine instructions.
320 void closeInsnRange(DbgScope *NewScope = NULL) {
321 assert (LastInsn && "Last insn missing!");
322 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000323 FirstInsn = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +0000324 LastInsn = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000325 // If Parent dominates NewScope then do not close Parent's instruction
Devang Pateleac9c072010-04-27 19:46:33 +0000326 // range.
327 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
328 Parent->closeInsnRange(NewScope);
329 }
330
Devang Patel53bb5c92009-11-10 23:06:00 +0000331 void setAbstractScope() { AbstractScope = true; }
332 bool isAbstractScope() const { return AbstractScope; }
Devang Pateleac9c072010-04-27 19:46:33 +0000333
334 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
335 unsigned getDFSOut() const { return DFSOut; }
336 void setDFSOut(unsigned O) { DFSOut = O; }
337 unsigned getDFSIn() const { return DFSIn; }
338 void setDFSIn(unsigned I) { DFSIn = I; }
339 bool dominates(const DbgScope *S) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000340 if (S == this)
Devang Pateleac9c072010-04-27 19:46:33 +0000341 return true;
342 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
343 return true;
344 return false;
345 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000346
Devang Patel2c4ceb12009-11-21 02:48:08 +0000347 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000348 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000349 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000350
Devang Patel2c4ceb12009-11-21 02:48:08 +0000351 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000352 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000353 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000354
Bill Wendling0310d762009-05-15 09:23:25 +0000355#ifndef NDEBUG
356 void dump() const;
357#endif
358};
Devang Pateleac9c072010-04-27 19:46:33 +0000359
Chris Lattnerea761862010-04-05 04:09:20 +0000360} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000361
362#ifndef NDEBUG
363void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000364 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000365 err.indent(IndentLevel);
Devang Patele9f8f5e2010-05-07 20:54:48 +0000366 const MDNode *N = Desc;
Devang Patel53bb5c92009-11-10 23:06:00 +0000367 N->dump();
Devang Patel53bb5c92009-11-10 23:06:00 +0000368 if (AbstractScope)
369 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000370
371 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000372 if (!Scopes.empty())
373 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000374 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
375 if (Scopes[i] != this)
376 Scopes[i]->dump();
377
378 IndentLevel -= 2;
379}
380#endif
381
Bill Wendling0310d762009-05-15 09:23:25 +0000382DbgScope::~DbgScope() {
Bill Wendling0310d762009-05-15 09:23:25 +0000383 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
384 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000385}
386
Chris Lattner49cd6642010-04-05 05:11:15 +0000387DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000388 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000389 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelf2548ca2010-04-16 23:33:45 +0000390 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000391 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000392
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000393 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
394 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000395 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000396 FunctionBeginSym = FunctionEndSym = 0;
Devang Patelca76f6f2010-07-08 20:10:35 +0000397 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Dan Gohman03c3dc72010-06-18 15:56:31 +0000398 {
399 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
400 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000401 }
Bill Wendling0310d762009-05-15 09:23:25 +0000402}
403DwarfDebug::~DwarfDebug() {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000404 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
405 DIEBlocks[j]->~DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000406}
407
Chris Lattnerbc733f52010-03-13 02:17:42 +0000408MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
409 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
410 if (Entry.first) return Entry.first;
411
412 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000413 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000414}
415
416
Devang Patel2c4ceb12009-11-21 02:48:08 +0000417/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000418///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000419void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000420 // Profile the node so that we can make it unique.
421 FoldingSetNodeID ID;
422 Abbrev.Profile(ID);
423
424 // Check the set for priors.
425 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
426
427 // If it's newly added.
428 if (InSet == &Abbrev) {
429 // Add to abbreviation list.
430 Abbreviations.push_back(&Abbrev);
431
432 // Assign the vector position + 1 as its number.
433 Abbrev.setNumber(Abbreviations.size());
434 } else {
435 // Assign existing abbreviation number.
436 Abbrev.setNumber(InSet->getNumber());
437 }
438}
439
Devang Patel2c4ceb12009-11-21 02:48:08 +0000440/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000441/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000442DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000443 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000444 return Value;
445}
446
Devang Patel2c4ceb12009-11-21 02:48:08 +0000447/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000448///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000449void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000450 unsigned Form, uint64_t Integer) {
451 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000452 DIEValue *Value = Integer == 1 ?
Devang Patelca76f6f2010-07-08 20:10:35 +0000453 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000454 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000455}
456
Devang Patel2c4ceb12009-11-21 02:48:08 +0000457/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000458///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000459void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000460 unsigned Form, int64_t Integer) {
461 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000462 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000463 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000464}
465
Devang Patel69f57b12009-12-02 15:25:16 +0000466/// addString - Add a string attribute data and value. DIEString only
Jim Grosbach1e20b962010-07-21 21:21:52 +0000467/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000468void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner4cf202b2010-01-23 03:11:46 +0000469 StringRef String) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000470 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000471 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000472}
473
Devang Patel2c4ceb12009-11-21 02:48:08 +0000474/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000475///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000476void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000477 const MCSymbol *Label) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000478 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000479 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000480}
481
Devang Patel2c4ceb12009-11-21 02:48:08 +0000482/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000483///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000484void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000485 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000486 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000487 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000488}
489
Chris Lattner74e41f92010-04-05 05:24:55 +0000490/// addDIEEntry - Add a DIE attribute data and value.
491///
492void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
493 DIE *Entry) {
494 Die->addValue(Attribute, Form, createDIEEntry(Entry));
495}
496
497
Devang Patel2c4ceb12009-11-21 02:48:08 +0000498/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000499///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000500void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000501 DIEBlock *Block) {
Chris Lattnera37d5382010-04-05 00:18:22 +0000502 Block->ComputeSize(Asm);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000503 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000504 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000505}
506
Devang Patel2c4ceb12009-11-21 02:48:08 +0000507/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000508/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000509void DwarfDebug::addSourceLine(DIE *Die, DIVariable V) {
Devang Patelc665a512010-05-07 23:33:41 +0000510 // Verify variable.
Devang Patel81625742010-08-09 20:20:05 +0000511 if (!V.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000512 return;
513
Devang Patel81625742010-08-09 20:20:05 +0000514 unsigned Line = V.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000515 if (Line == 0)
516 return;
Devang Patel81625742010-08-09 20:20:05 +0000517 unsigned FileID = GetOrCreateSourceID(V.getContext().getDirectory(),
518 V.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000519 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000520 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
521 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000522}
523
Devang Patel2c4ceb12009-11-21 02:48:08 +0000524/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000525/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000526void DwarfDebug::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patelc665a512010-05-07 23:33:41 +0000527 // Verify global variable.
Devang Patel81625742010-08-09 20:20:05 +0000528 if (!G.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000529 return;
530
Devang Patel81625742010-08-09 20:20:05 +0000531 unsigned Line = G.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000532 if (Line == 0)
533 return;
Devang Patel81625742010-08-09 20:20:05 +0000534 unsigned FileID = GetOrCreateSourceID(G.getContext().getDirectory(),
535 G.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000536 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000537 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
538 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000539}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000540
Devang Patel2c4ceb12009-11-21 02:48:08 +0000541/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000542/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000543void DwarfDebug::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patelc665a512010-05-07 23:33:41 +0000544 // Verify subprogram.
Devang Patel81625742010-08-09 20:20:05 +0000545 if (!SP.Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000546 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000547 // If the line number is 0, don't add it.
Devang Patel81625742010-08-09 20:20:05 +0000548 if (SP.getLineNumber() == 0)
Caroline Ticec6f9d622009-09-11 18:25:54 +0000549 return;
550
Devang Patel81625742010-08-09 20:20:05 +0000551 unsigned Line = SP.getLineNumber();
552 if (!SP.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000553 return;
Devang Patel81625742010-08-09 20:20:05 +0000554 unsigned FileID = GetOrCreateSourceID(SP.getDirectory(),
555 SP.getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000556 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000557 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
558 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000559}
560
Devang Patel2c4ceb12009-11-21 02:48:08 +0000561/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000562/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000563void DwarfDebug::addSourceLine(DIE *Die, DIType Ty) {
Devang Patelc665a512010-05-07 23:33:41 +0000564 // Verify type.
Devang Patel81625742010-08-09 20:20:05 +0000565 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000566 return;
567
Devang Patel81625742010-08-09 20:20:05 +0000568 unsigned Line = Ty.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000569 if (Line == 0 || !Ty.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000570 return;
Devang Patelbc2bb9b2010-10-28 19:50:08 +0000571 unsigned FileID = GetOrCreateSourceID(Ty.getDirectory(),
572 Ty.getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000573 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000574 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
575 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000576}
577
Devang Patel6404e4e2009-12-15 19:16:48 +0000578/// addSourceLine - Add location information to specified debug information
579/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000580void DwarfDebug::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patelc665a512010-05-07 23:33:41 +0000581 // Verify namespace.
Devang Patel81625742010-08-09 20:20:05 +0000582 if (!NS.Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000583 return;
584
Devang Patel81625742010-08-09 20:20:05 +0000585 unsigned Line = NS.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000586 if (Line == 0)
587 return;
Devang Patel81625742010-08-09 20:20:05 +0000588 StringRef FN = NS.getFilename();
589 StringRef Dir = NS.getDirectory();
Devang Patel6404e4e2009-12-15 19:16:48 +0000590
591 unsigned FileID = GetOrCreateSourceID(Dir, FN);
592 assert(FileID && "Invalid file id");
593 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
594 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
595}
596
Devang Patel9e3bd2c2010-08-31 06:11:28 +0000597/// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
598/// on provided frame index.
599void DwarfDebug::addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI) {
600 MachineLocation Location;
601 unsigned FrameReg;
602 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
603 int Offset = RI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
604 Location.set(FrameReg, Offset);
605
Devang Patel8bd11de2010-08-09 21:01:39 +0000606 if (DV->variableHasComplexAddress())
607 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
608 else if (DV->isBlockByrefVariable())
609 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
610 else
611 addAddress(Die, dwarf::DW_AT_location, Location);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000612}
613
Devang Patel2c4ceb12009-11-21 02:48:08 +0000614/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000615/// and generate the DWARF information necessary to find the actual variable
616/// given the extra address information encoded in the DIVariable, starting from
617/// the starting location. Add the DWARF information to the die.
618///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000619void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000620 unsigned Attribute,
621 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000622 DIType Ty = DV->getType();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000623
624 // Decode the original location, and use that as the start of the byref
625 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000626 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000627 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000628 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000629
630 if (Location.isReg()) {
631 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000632 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000633 } else {
634 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000635 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
636 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000637 }
638 } else {
639 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000640 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000641 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000642 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
643 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000644 }
645
Devang Patel2c4ceb12009-11-21 02:48:08 +0000646 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000647 }
648
Devang Patel8bd11de2010-08-09 21:01:39 +0000649 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
650 uint64_t Element = DV->getAddrElement(i);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000651
652 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000653 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel8bd11de2010-08-09 21:01:39 +0000654 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000655 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000656 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000657 } else llvm_unreachable("unknown DIFactory Opcode");
658 }
659
660 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000661 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000662}
663
Caroline Ticedc8f6042009-08-31 21:19:37 +0000664/* Byref variables, in Blocks, are declared by the programmer as "SomeType
665 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
666 gives the variable VarName either the struct, or a pointer to the struct, as
667 its type. This is necessary for various behind-the-scenes things the
668 compiler needs to do with by-reference variables in Blocks.
669
670 However, as far as the original *programmer* is concerned, the variable
671 should still have type 'SomeType', as originally declared.
672
Devang Patel2c4ceb12009-11-21 02:48:08 +0000673 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000674 struct to find the original type of the variable, which is then assigned to
675 the variable's Debug Information Entry as its real type. So far, so good.
676 However now the debugger will expect the variable VarName to have the type
677 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000678 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000679 pointers and struct to find the actual variable of type SomeType.
680
681 The following function does just that. We start by getting
682 the "normal" location for the variable. This will be the location
683 of either the struct __Block_byref_x_VarName or the pointer to the
684 struct __Block_byref_x_VarName.
685
686 The struct will look something like:
687
688 struct __Block_byref_x_VarName {
689 ... <various fields>
690 struct __Block_byref_x_VarName *forwarding;
691 ... <various other fields>
692 SomeType VarName;
693 ... <maybe more fields>
694 };
695
696 If we are given the struct directly (as our starting point) we
697 need to tell the debugger to:
698
699 1). Add the offset of the forwarding field.
700
Dan Gohmanf451cb82010-02-10 16:03:48 +0000701 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000702 struct to use (the real one may have been copied onto the heap).
703
704 3). Add the offset for the field VarName, to find the actual variable.
705
706 If we started with a pointer to the struct, then we need to
707 dereference that pointer first, before the other steps.
708 Translating this into DWARF ops, we will need to append the following
709 to the current location description for the variable:
710
711 DW_OP_deref -- optional, if we start with a pointer
712 DW_OP_plus_uconst <forward_fld_offset>
713 DW_OP_deref
714 DW_OP_plus_uconst <varName_fld_offset>
715
716 That is what this function does. */
717
Devang Patel2c4ceb12009-11-21 02:48:08 +0000718/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000719/// provided, and generate the DWARF information necessary to find the
720/// actual Block variable (navigating the Block struct) based on the
721/// starting location. Add the DWARF information to the die. For
722/// more information, read large comment just above here.
723///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000724void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000725 unsigned Attribute,
726 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000727 DIType Ty = DV->getType();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000728 DIType TmpTy = Ty;
729 unsigned Tag = Ty.getTag();
730 bool isPointer = false;
731
Devang Patel8bd11de2010-08-09 21:01:39 +0000732 StringRef varName = DV->getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000733
734 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000735 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000736 TmpTy = DTy.getTypeDerivedFrom();
737 isPointer = true;
738 }
739
Devang Patel2db49d72010-05-07 18:11:54 +0000740 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000741
Daniel Dunbar00564992009-09-19 20:40:14 +0000742 // Find the __forwarding field and the variable field in the __Block_byref
743 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000744 DIArray Fields = blockStruct.getTypeArray();
745 DIDescriptor varField = DIDescriptor();
746 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000747
Daniel Dunbar00564992009-09-19 20:40:14 +0000748 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
749 DIDescriptor Element = Fields.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000750 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000751 StringRef fieldName = DT.getName();
752 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000753 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000754 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000755 varField = Element;
756 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000757
Daniel Dunbar00564992009-09-19 20:40:14 +0000758 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000759 unsigned forwardingFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000760 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000761 unsigned varFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000762 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000763
Mike Stump7e3720d2009-09-24 23:21:26 +0000764 // Decode the original location, and use that as the start of the byref
765 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000766 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000767 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000768 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000769
Daniel Dunbar00564992009-09-19 20:40:14 +0000770 if (Location.isReg()) {
771 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000772 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000773 else {
774 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000775 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
776 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000777 }
778 } else {
779 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000780 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000781 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000782 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
783 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000784 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000785
Devang Patel2c4ceb12009-11-21 02:48:08 +0000786 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000787 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000788
Mike Stump7e3720d2009-09-24 23:21:26 +0000789 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000790 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000791 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000792 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000793
Daniel Dunbar00564992009-09-19 20:40:14 +0000794 // Next add the offset for the '__forwarding' field:
795 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
796 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000797 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000798 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
799 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000800 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000801
Daniel Dunbar00564992009-09-19 20:40:14 +0000802 // Now dereference the __forwarding field to get to the real __Block_byref
803 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000804 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000805
Daniel Dunbar00564992009-09-19 20:40:14 +0000806 // Now that we've got the real __Block_byref... struct, add the offset
807 // for the variable's field to get to the location of the actual variable:
808 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000809 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000810 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
811 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000812 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000813
Daniel Dunbar00564992009-09-19 20:40:14 +0000814 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000815 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000816}
817
Devang Patel2c4ceb12009-11-21 02:48:08 +0000818/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000819/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000820void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000821 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000822 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000823 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000824 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelbe90c3a2010-09-22 21:10:38 +0000825
Devang Patelc8821042010-11-02 17:37:00 +0000826 if (RI->getFrameRegister(*Asm->MF) == Location.getReg()
Devang Patelbe90c3a2010-09-22 21:10:38 +0000827 && Location.getOffset()) {
828 // If variable offset is based in frame register then use fbreg.
829 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
830 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
831 addBlock(Die, Attribute, 0, Block);
832 return;
833 }
Bill Wendling0310d762009-05-15 09:23:25 +0000834
835 if (Location.isReg()) {
836 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000837 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000838 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000839 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
840 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000841 }
842 } else {
843 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000844 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000845 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000846 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
847 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000848 }
849
Devang Patel2c4ceb12009-11-21 02:48:08 +0000850 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000851 }
852
Devang Patel2c4ceb12009-11-21 02:48:08 +0000853 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000854}
855
Devang Patela43098d2010-04-28 01:03:09 +0000856/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patel522ad742010-11-12 23:20:42 +0000857bool DwarfDebug::addRegisterAddress(DIE *Die, const MachineOperand &MO) {
Devang Patela43098d2010-04-28 01:03:09 +0000858 assert (MO.isReg() && "Invalid machine operand!");
859 if (!MO.getReg())
860 return false;
861 MachineLocation Location;
862 Location.set(MO.getReg());
863 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patela43098d2010-04-28 01:03:09 +0000864 return true;
865}
866
867/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel522ad742010-11-12 23:20:42 +0000868bool DwarfDebug::addConstantValue(DIE *Die, const MachineOperand &MO) {
Devang Patela43098d2010-04-28 01:03:09 +0000869 assert (MO.isImm() && "Invalid machine operand!");
870 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
871 unsigned Imm = MO.getImm();
872 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
873 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patela43098d2010-04-28 01:03:09 +0000874 return true;
875}
876
877/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patel522ad742010-11-12 23:20:42 +0000878bool DwarfDebug::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Devang Patela43098d2010-04-28 01:03:09 +0000879 assert (MO.isFPImm() && "Invalid machine operand!");
880 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
881 APFloat FPImm = MO.getFPImm()->getValueAPF();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000882
Devang Patela43098d2010-04-28 01:03:09 +0000883 // Get the raw data form of the floating point.
884 const APInt FltVal = FPImm.bitcastToAPInt();
885 const char *FltPtr = (const char*)FltVal.getRawData();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000886
Devang Patela43098d2010-04-28 01:03:09 +0000887 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
888 bool LittleEndian = Asm->getTargetData().isLittleEndian();
889 int Incr = (LittleEndian ? 1 : -1);
890 int Start = (LittleEndian ? 0 : NumBytes - 1);
891 int Stop = (LittleEndian ? NumBytes : -1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000892
Devang Patela43098d2010-04-28 01:03:09 +0000893 // Output the constant to DWARF one byte at a time.
894 for (; Start != Stop; Start += Incr)
895 addUInt(Block, 0, dwarf::DW_FORM_data1,
896 (unsigned char)0xFF & FltPtr[Start]);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000897
Devang Patela43098d2010-04-28 01:03:09 +0000898 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000899 return true;
Devang Patela43098d2010-04-28 01:03:09 +0000900}
901
902
Devang Patelc366f832009-12-10 19:14:49 +0000903/// addToContextOwner - Add Die into the list of its context owner's children.
904void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000905 if (Context.isType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000906 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patelc366f832009-12-10 19:14:49 +0000907 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000908 } else if (Context.isNameSpace()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000909 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel6404e4e2009-12-15 19:16:48 +0000910 ContextDIE->addChild(Die);
Stuart Hastings215aa152010-06-11 20:08:44 +0000911 } else if (Context.isSubprogram()) {
Devang Patelee70fa72010-09-27 23:15:27 +0000912 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context));
Stuart Hastings215aa152010-06-11 20:08:44 +0000913 ContextDIE->addChild(Die);
Devang Patel163a9f72010-05-10 22:49:55 +0000914 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patelc366f832009-12-10 19:14:49 +0000915 ContextDIE->addChild(Die);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000916 else
Devang Patel163a9f72010-05-10 22:49:55 +0000917 getCompileUnit(Context)->addDie(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000918}
919
Devang Patel16ced732009-12-10 18:05:33 +0000920/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
921/// given DIType.
922DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel163a9f72010-05-10 22:49:55 +0000923 CompileUnit *TypeCU = getCompileUnit(Ty);
924 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel16ced732009-12-10 18:05:33 +0000925 if (TyDIE)
926 return TyDIE;
927
928 // Create new type.
929 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel163a9f72010-05-10 22:49:55 +0000930 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel16ced732009-12-10 18:05:33 +0000931 if (Ty.isBasicType())
Devang Patel2db49d72010-05-07 18:11:54 +0000932 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000933 else if (Ty.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000934 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000935 else {
936 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2db49d72010-05-07 18:11:54 +0000937 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000938 }
939
Devang Patelc366f832009-12-10 19:14:49 +0000940 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000941 return TyDIE;
942}
943
Devang Patel2c4ceb12009-11-21 02:48:08 +0000944/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000945void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9c004872010-05-07 21:45:47 +0000946 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000947 return;
948
949 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000950 CompileUnit *TypeCU = getCompileUnit(Ty);
951 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000952 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000953 if (Entry) {
954 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000955 return;
956 }
957
Bill Wendling0310d762009-05-15 09:23:25 +0000958 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000959 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000960
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000961 // Set up proxy.
962 Entry = createDIEEntry(Buffer);
Devang Patel163a9f72010-05-10 22:49:55 +0000963 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000964
Devang Patel2c4ceb12009-11-21 02:48:08 +0000965 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000966}
967
Devang Patel2c4ceb12009-11-21 02:48:08 +0000968/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000969void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000970 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000971 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000972 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000973 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000974 BTy.getEncoding());
975
976 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000977 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000978 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000979 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000980 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000981}
982
Devang Patel2c4ceb12009-11-21 02:48:08 +0000983/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000984void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000985 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000986 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000987 uint64_t Size = DTy.getSizeInBits() >> 3;
988 unsigned Tag = DTy.getTag();
989
990 // FIXME - Workaround for templates.
991 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
992
993 Buffer.setTag(Tag);
994
995 // Map to main type, void will not have a type.
996 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000997 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000998
999 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +00001000 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001001 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001002
1003 // Add size if non-zero (derived types might be zero-sized.)
1004 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001005 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001006
1007 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +00001008 if (!DTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001009 addSourceLine(&Buffer, DTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001010}
1011
Devang Patel2c4ceb12009-11-21 02:48:08 +00001012/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001013void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001014 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +00001015 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001016
1017 uint64_t Size = CTy.getSizeInBits() >> 3;
1018 unsigned Tag = CTy.getTag();
1019 Buffer.setTag(Tag);
1020
1021 switch (Tag) {
1022 case dwarf::DW_TAG_vector_type:
1023 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +00001024 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001025 break;
1026 case dwarf::DW_TAG_enumeration_type: {
1027 DIArray Elements = CTy.getTypeArray();
1028
1029 // Add enumerators to enumeration type.
1030 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1031 DIE *ElemDie = NULL;
Devang Patel2db49d72010-05-07 18:11:54 +00001032 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3c91b052010-03-08 20:52:55 +00001033 if (Enum.isEnumerator()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001034 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001035 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +00001036 }
Bill Wendling0310d762009-05-15 09:23:25 +00001037 }
1038 }
1039 break;
1040 case dwarf::DW_TAG_subroutine_type: {
1041 // Add return type.
1042 DIArray Elements = CTy.getTypeArray();
1043 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2db49d72010-05-07 18:11:54 +00001044 addType(&Buffer, DIType(RTy));
Bill Wendling0310d762009-05-15 09:23:25 +00001045
Devang Pateld6747df2010-10-06 20:50:40 +00001046 bool isPrototyped = true;
Bill Wendling0310d762009-05-15 09:23:25 +00001047 // Add arguments.
1048 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001049 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld6747df2010-10-06 20:50:40 +00001050 if (Ty.isUnspecifiedParameter()) {
1051 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
1052 Buffer.addChild(Arg);
1053 isPrototyped = false;
1054 } else {
1055 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1056 addType(Arg, DIType(Ty));
1057 Buffer.addChild(Arg);
1058 }
Bill Wendling0310d762009-05-15 09:23:25 +00001059 }
Devang Pateld6747df2010-10-06 20:50:40 +00001060 // Add prototype flag.
1061 if (isPrototyped)
1062 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001063 }
1064 break;
1065 case dwarf::DW_TAG_structure_type:
1066 case dwarf::DW_TAG_union_type:
1067 case dwarf::DW_TAG_class_type: {
1068 // Add elements to structure type.
1069 DIArray Elements = CTy.getTypeArray();
1070
1071 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +00001072 unsigned N = Elements.getNumElements();
1073 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +00001074 break;
1075
1076 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +00001077 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001078 DIDescriptor Element = Elements.getElement(i);
1079 DIE *ElemDie = NULL;
Devang Patel1a301232010-09-29 21:44:16 +00001080 if (Element.isSubprogram()) {
1081 DISubprogram SP(Element);
Devang Patel2db49d72010-05-07 18:11:54 +00001082 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel1a301232010-09-29 21:44:16 +00001083 if (SP.isProtected())
1084 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1085 dwarf::DW_ACCESS_protected);
1086 else if (SP.isPrivate())
1087 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1088 dwarf::DW_ACCESS_private);
1089 else
1090 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1091 dwarf::DW_ACCESS_public);
Devang Patel21ea1d52010-10-01 23:31:40 +00001092 if (SP.isExplicit())
1093 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
Devang Patel1a301232010-09-29 21:44:16 +00001094 }
Devang Patel3c91b052010-03-08 20:52:55 +00001095 else if (Element.isVariable()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001096 DIVariable DV(Element);
Devang Patel1ee0cb92010-01-30 01:08:30 +00001097 ElemDie = new DIE(dwarf::DW_TAG_variable);
1098 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1099 DV.getName());
1100 addType(ElemDie, DV.getType());
1101 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1102 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel81625742010-08-09 20:20:05 +00001103 addSourceLine(ElemDie, DV);
Devang Patel3c91b052010-03-08 20:52:55 +00001104 } else if (Element.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001105 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001106 else
1107 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001108 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001109 }
1110
Devang Patela1ba2692009-08-27 23:51:51 +00001111 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001112 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001113
1114 unsigned RLang = CTy.getRunTimeLang();
1115 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001116 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001117 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001118
1119 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel2db49d72010-05-07 18:11:54 +00001120 if (DIDescriptor(ContainingType).isCompositeType())
Jim Grosbach1e20b962010-07-21 21:21:52 +00001121 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel2db49d72010-05-07 18:11:54 +00001122 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastings215aa152010-06-11 20:08:44 +00001123 else {
1124 DIDescriptor Context = CTy.getContext();
1125 addToContextOwner(&Buffer, Context);
1126 }
Bill Wendling0310d762009-05-15 09:23:25 +00001127 break;
1128 }
1129 default:
1130 break;
1131 }
1132
1133 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001134 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001135 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001136
Jim Grosbach1e20b962010-07-21 21:21:52 +00001137 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
Devang Patel61409622010-07-07 20:12:52 +00001138 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1139 {
Bill Wendling0310d762009-05-15 09:23:25 +00001140 // Add size if non-zero (derived types might be zero-sized.)
1141 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001142 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001143 else {
1144 // Add zero size if it is not a forward declaration.
1145 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001146 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001147 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001148 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001149 }
1150
1151 // Add source line info if available.
1152 if (!CTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001153 addSourceLine(&Buffer, CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001154 }
1155}
1156
Devang Patel2c4ceb12009-11-21 02:48:08 +00001157/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1158void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001159 int64_t L = SR.getLo();
1160 int64_t H = SR.getHi();
1161 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1162
Devang Patel2c4ceb12009-11-21 02:48:08 +00001163 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001164 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001165 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001166 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001167
Devang Patel2c4ceb12009-11-21 02:48:08 +00001168 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001169}
1170
Devang Patel2c4ceb12009-11-21 02:48:08 +00001171/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001172void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001173 DICompositeType *CTy) {
1174 Buffer.setTag(dwarf::DW_TAG_array_type);
1175 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001176 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001177
1178 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001179 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001180 DIArray Elements = CTy->getTypeArray();
1181
Devang Patel6f01d9c2009-11-21 00:31:03 +00001182 // Get an anonymous type for index type.
Devang Patel163a9f72010-05-10 22:49:55 +00001183 CompileUnit *TheCU = getCompileUnit(*CTy);
1184 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001185 if (!IdxTy) {
1186 // Construct an anonymous type for index type.
1187 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001188 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1189 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001190 dwarf::DW_ATE_signed);
Devang Patel163a9f72010-05-10 22:49:55 +00001191 TheCU->addDie(IdxTy);
1192 TheCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001193 }
Bill Wendling0310d762009-05-15 09:23:25 +00001194
1195 // Add subranges to array type.
1196 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1197 DIDescriptor Element = Elements.getElement(i);
1198 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2db49d72010-05-07 18:11:54 +00001199 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001200 }
1201}
1202
Devang Patel2c4ceb12009-11-21 02:48:08 +00001203/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001204DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001205 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001206 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001207 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001208 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001209 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001210 return Enumerator;
1211}
1212
Jim Grosbach1e20b962010-07-21 21:21:52 +00001213/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +00001214/// printer to not emit usual symbol prefix before the symbol name is used then
1215/// return linkage name after skipping this special LLVM prefix.
1216static StringRef getRealLinkageName(StringRef LinkageName) {
1217 char One = '\1';
1218 if (LinkageName.startswith(StringRef(&One, 1)))
1219 return LinkageName.substr(1);
1220 return LinkageName;
1221}
1222
Devang Patel2c4ceb12009-11-21 02:48:08 +00001223/// createMemberDIE - Create new member DIE.
Devang Patelecbd8e82010-08-10 04:12:17 +00001224DIE *DwarfDebug::createMemberDIE(DIDerivedType DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001225 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001226 StringRef Name = DT.getName();
1227 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001228 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001229
Devang Patel8a241142009-12-09 18:24:21 +00001230 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001231
Devang Patel81625742010-08-09 20:20:05 +00001232 addSourceLine(MemberDie, DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001233
Benjamin Kramer345ef342010-03-31 19:34:01 +00001234 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001235 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001236
Bill Wendling0310d762009-05-15 09:23:25 +00001237 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001238 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001239
1240 if (Size != FieldSize) {
1241 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001242 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1243 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001244
1245 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001246 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1247 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001248 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001249 Offset -= FieldOffset;
1250
1251 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001252 if (Asm->getTargetData().isLittleEndian())
1253 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001254 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001255
Devang Patel33db5082009-11-04 22:06:12 +00001256 // Here WD_AT_data_member_location points to the anonymous
1257 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001258 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001259
1260 } else
1261 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001262 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001263
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001264 if (DT.getTag() == dwarf::DW_TAG_inheritance
1265 && DT.isVirtual()) {
1266
1267 // For C++, virtual base classes are not at fixed offset. Use following
1268 // expression to extract appropriate offset from vtable.
1269 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1270
Benjamin Kramer345ef342010-03-31 19:34:01 +00001271 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001272 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1273 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1274 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1275 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1276 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1277 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1278 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1279
Jim Grosbach1e20b962010-07-21 21:21:52 +00001280 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001281 VBaseLocationDie);
1282 } else
1283 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001284
1285 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001286 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001287 dwarf::DW_ACCESS_protected);
1288 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001289 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001290 dwarf::DW_ACCESS_private);
Devang Patel2a361602010-09-29 19:08:08 +00001291 // Otherwise C++ member and base classes are considered public.
1292 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
Devang Patel5d11eb02009-12-03 19:11:07 +00001293 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1294 dwarf::DW_ACCESS_public);
1295 if (DT.isVirtual())
1296 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1297 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001298 return MemberDie;
1299}
1300
Devang Patelffe966c2009-12-14 16:18:45 +00001301/// createSubprogramDIE - Create new DIE using SP.
Devang Patelee70fa72010-09-27 23:15:27 +00001302DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel163a9f72010-05-10 22:49:55 +00001303 CompileUnit *SPCU = getCompileUnit(SP);
1304 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +00001305 if (SPDie)
1306 return SPDie;
1307
1308 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001309 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001310 if (!SP.getName().empty())
1311 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001312
Devang Patel65dbc902009-11-25 17:36:49 +00001313 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001314 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001315 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001316 getRealLinkageName(LinkageName));
1317
Devang Patel81625742010-08-09 20:20:05 +00001318 addSourceLine(SPDie, SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001319
Devang Patel7b172c62010-10-07 22:03:01 +00001320 if (SP.isPrototyped())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001321 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001322
1323 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001324 DICompositeType SPTy = SP.getType();
1325 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001326 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001327
Devang Patel3c91b052010-03-08 20:52:55 +00001328 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001329 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001330 else
Devang Patel2db49d72010-05-07 18:11:54 +00001331 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001332
Devang Patel5d11eb02009-12-03 19:11:07 +00001333 unsigned VK = SP.getVirtuality();
1334 if (VK) {
1335 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001336 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001337 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1338 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1339 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001340 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +00001341 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001342 }
1343
Devang Patelee70fa72010-09-27 23:15:27 +00001344 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001345 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001346
1347 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001348 // be handled while processing variables.
1349 DICompositeType SPTy = SP.getType();
1350 DIArray Args = SPTy.getTypeArray();
1351 unsigned SPTag = SPTy.getTag();
1352
Bill Wendling0310d762009-05-15 09:23:25 +00001353 if (SPTag == dwarf::DW_TAG_subroutine_type)
1354 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1355 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001356 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelb4645642010-02-06 01:02:37 +00001357 addType(Arg, ATy);
1358 if (ATy.isArtificial())
1359 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001360 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001361 }
1362 }
1363
Devang Patel4e0d19d2010-02-03 19:57:19 +00001364 if (SP.isArtificial())
1365 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1366
Devang Patelccff8122010-04-30 19:38:23 +00001367 if (!SP.isLocalToUnit())
1368 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001369
Devang Patelccff8122010-04-30 19:38:23 +00001370 if (SP.isOptimized())
1371 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1372
Jim Grosbach91729002010-07-21 23:03:52 +00001373 if (unsigned isa = Asm->getISAEncoding()) {
1374 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1375 }
1376
Devang Patelccff8122010-04-30 19:38:23 +00001377 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel163a9f72010-05-10 22:49:55 +00001378 SPCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +00001379
Stuart Hastings215aa152010-06-11 20:08:44 +00001380 // Add to context owner.
1381 addToContextOwner(SPDie, SP.getContext());
1382
Bill Wendling0310d762009-05-15 09:23:25 +00001383 return SPDie;
1384}
1385
Devang Patele9f8f5e2010-05-07 20:54:48 +00001386DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001387 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001388
1389 DbgScope *AScope = AbstractScopes.lookup(N);
1390 if (AScope)
1391 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001392
Devang Patel53bb5c92009-11-10 23:06:00 +00001393 DbgScope *Parent = NULL;
1394
1395 DIDescriptor Scope(N);
1396 if (Scope.isLexicalBlock()) {
1397 DILexicalBlock DB(N);
1398 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +00001399 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +00001400 }
1401
1402 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1403
1404 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001405 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001406 AScope->setAbstractScope();
1407 AbstractScopes[N] = AScope;
1408 if (DIDescriptor(N).isSubprogram())
1409 AbstractScopesList.push_back(AScope);
1410 return AScope;
1411}
Devang Patelaf9e8472009-10-01 20:31:14 +00001412
Devang Patel5f094002010-04-06 23:53:48 +00001413/// isSubprogramContext - Return true if Context is either a subprogram
1414/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001415static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001416 if (!Context)
1417 return false;
1418 DIDescriptor D(Context);
1419 if (D.isSubprogram())
1420 return true;
1421 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001422 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +00001423 return false;
1424}
1425
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001426/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001427/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1428/// If there are global variables in this scope then create and insert
1429/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001430DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +00001431 CompileUnit *SPCU = getCompileUnit(SPNode);
1432 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +00001433
Chris Lattnerd38fee82010-04-05 00:13:49 +00001434 assert(SPDie && "Unable to find subprogram DIE!");
1435 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001436
Chris Lattnerd38fee82010-04-05 00:13:49 +00001437 // There is not any need to generate specification DIE for a function
1438 // defined at compile unit level. If a function is defined inside another
1439 // function then gdb prefers the definition at top level and but does not
Jim Grosbach1e20b962010-07-21 21:21:52 +00001440 // expect specification DIE in parent function. So avoid creating
Chris Lattnerd38fee82010-04-05 00:13:49 +00001441 // specification DIE for a function defined inside a function.
1442 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbach1e20b962010-07-21 21:21:52 +00001443 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001444 !isSubprogramContext(SP.getContext())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001445 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001446
1447 // Add arguments.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001448 DICompositeType SPTy = SP.getType();
1449 DIArray Args = SPTy.getTypeArray();
1450 unsigned SPTag = SPTy.getTag();
1451 if (SPTag == dwarf::DW_TAG_subroutine_type)
1452 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1453 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001454 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerd38fee82010-04-05 00:13:49 +00001455 addType(Arg, ATy);
1456 if (ATy.isArtificial())
1457 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1458 SPDie->addChild(Arg);
1459 }
1460 DIE *SPDeclDie = SPDie;
1461 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001462 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Chris Lattnerd38fee82010-04-05 00:13:49 +00001463 SPDeclDie);
Devang Patel163a9f72010-05-10 22:49:55 +00001464 SPCU->addDie(SPDie);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001465 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001466
Devang Patel8aa61472010-07-07 22:20:57 +00001467 // Pick up abstract subprogram DIE.
1468 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1469 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1470 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1471 dwarf::DW_FORM_ref4, AbsSPDIE);
1472 SPCU->addDie(SPDie);
1473 }
1474
Chris Lattnerd38fee82010-04-05 00:13:49 +00001475 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1476 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1477 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1478 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1479 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1480 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1481 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001482
Chris Lattnerd38fee82010-04-05 00:13:49 +00001483 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001484}
1485
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001486/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001487/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1488DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001489
1490 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1491 if (Scope->isAbstractScope())
1492 return ScopeDIE;
1493
1494 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1495 if (Ranges.empty())
1496 return 0;
1497
1498 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1499 if (Ranges.size() > 1) {
1500 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +00001501 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +00001502 // DW_AT_ranges appropriately.
1503 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1504 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1505 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1506 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001507 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1508 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +00001509 }
1510 DebugRangeSymbols.push_back(NULL);
1511 DebugRangeSymbols.push_back(NULL);
1512 return ScopeDIE;
1513 }
1514
Devang Patelc3f5f782010-05-25 23:40:22 +00001515 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1516 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001517
Devang Patelc3f5f782010-05-25 23:40:22 +00001518 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001519
Chris Lattnerb7db7332010-03-09 01:58:53 +00001520 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1521 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001522
Devang Pateleac9c072010-04-27 19:46:33 +00001523 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1524 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001525
1526 return ScopeDIE;
1527}
1528
Devang Patel2c4ceb12009-11-21 02:48:08 +00001529/// constructInlinedScopeDIE - This scope represents inlined body of
1530/// a function. Construct DIE to represent this concrete inlined copy
1531/// of the function.
1532DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001533
1534 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001535 assert (Ranges.empty() == false
Devang Pateleac9c072010-04-27 19:46:33 +00001536 && "DbgScope does not have instruction markers!");
1537
1538 // FIXME : .debug_inlined section specification does not clearly state how
1539 // to emit inlined scope that is split into multiple instruction ranges.
1540 // For now, use first instruction range and emit low_pc/high_pc pair and
1541 // corresponding .debug_inlined section entry for this pair.
1542 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +00001543 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1544 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001545
Devang Patel0afbf232010-07-08 22:39:20 +00001546 if (StartLabel == 0 || EndLabel == 0) {
Devang Pateleac9c072010-04-27 19:46:33 +00001547 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1548 return 0;
1549 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001550 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001551 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001552 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001553 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +00001554
Devang Patel3c91b052010-03-08 20:52:55 +00001555 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001556 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001557 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001558 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1559
Devang Patel2db49d72010-05-07 18:11:54 +00001560 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001561 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1562 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001563 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001564 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001565 dwarf::DW_FORM_ref4, OriginDIE);
1566
Chris Lattner6ed0f902010-03-09 00:31:02 +00001567 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001568 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001569
1570 InlinedSubprogramDIEs.insert(OriginDIE);
1571
1572 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001573 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +00001574 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001575
1576 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001577 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001578 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +00001579 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001580 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001581 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001582
Devang Patel53bb5c92009-11-10 23:06:00 +00001583 DILocation DL(Scope->getInlinedAt());
Devang Patel163a9f72010-05-10 22:49:55 +00001584 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001585 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001586
1587 return ScopeDIE;
1588}
1589
Devang Patel2c4ceb12009-11-21 02:48:08 +00001590
1591/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001592DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +00001593 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +00001594 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001595 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001596
1597 // Translate tag to proper Dwarf tag. The result variable is dropped for
1598 // now.
1599 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +00001600 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001601 case dwarf::DW_TAG_return_variable:
1602 return NULL;
1603 case dwarf::DW_TAG_arg_variable:
1604 Tag = dwarf::DW_TAG_formal_parameter;
1605 break;
1606 case dwarf::DW_TAG_auto_variable: // fall thru
1607 default:
1608 Tag = dwarf::DW_TAG_variable;
1609 break;
1610 }
1611
1612 // Define variable debug information entry.
1613 DIE *VariableDie = new DIE(Tag);
1614
Devang Patel53bb5c92009-11-10 23:06:00 +00001615 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +00001616 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1617 V2AVI = VarToAbstractVarMap.find(DV);
1618 if (V2AVI != VarToAbstractVarMap.end())
1619 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001620
Devang Patel26c1e562010-05-20 16:36:41 +00001621 if (AbsDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001622 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001623 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001624 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001625 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel8bd11de2010-08-09 21:01:39 +00001626 addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +00001627
1628 // Add variable type.
Devang Patel8bd11de2010-08-09 21:01:39 +00001629 addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001630 }
1631
Devang Patel8bd11de2010-08-09 21:01:39 +00001632 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patelb4645642010-02-06 01:02:37 +00001633 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel3cf763d2010-09-29 23:07:21 +00001634 else if (DIVariable(DV->getVariable()).isArtificial())
1635 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +00001636
1637 if (Scope->isAbstractScope()) {
1638 DV->setDIE(VariableDie);
1639 return VariableDie;
1640 }
1641
1642 // Add variable address.
1643
1644 unsigned Offset = DV->getDotDebugLocOffset();
1645 if (Offset != ~0U) {
1646 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1647 Asm->GetTempSymbol("debug_loc", Offset));
1648 DV->setDIE(VariableDie);
1649 UseDotDebugLocEntry.insert(VariableDie);
1650 return VariableDie;
1651 }
1652
1653 // Check if variable is described by a DBG_VALUE instruction.
1654 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1655 DbgVariableToDbgInstMap.find(DV);
1656 if (DVI != DbgVariableToDbgInstMap.end()) {
1657 const MachineInstr *DVInsn = DVI->second;
Devang Patelc3f5f782010-05-25 23:40:22 +00001658 bool updated = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001659 // FIXME : Handle getNumOperands != 3
Devang Patelc3f5f782010-05-25 23:40:22 +00001660 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +00001661 if (DVInsn->getOperand(0).isReg()) {
1662 const MachineOperand RegOp = DVInsn->getOperand(0);
1663 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1664 if (DVInsn->getOperand(1).isImm() &&
1665 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1666 addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
1667 updated = true;
1668 } else
Devang Patel522ad742010-11-12 23:20:42 +00001669 updated = addRegisterAddress(VariableDie, RegOp);
Devang Patel0b48ead2010-08-31 22:22:42 +00001670 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001671 else if (DVInsn->getOperand(0).isImm())
Devang Patel522ad742010-11-12 23:20:42 +00001672 updated = addConstantValue(VariableDie, DVInsn->getOperand(0));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001673 else if (DVInsn->getOperand(0).isFPImm())
1674 updated =
Devang Patel522ad742010-11-12 23:20:42 +00001675 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patelc3f5f782010-05-25 23:40:22 +00001676 } else {
1677 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1678 if (Location.getReg()) {
1679 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patelc3f5f782010-05-25 23:40:22 +00001680 updated = true;
1681 }
1682 }
1683 if (!updated) {
1684 // If variableDie is not updated then DBG_VALUE instruction does not
1685 // have valid variable info.
1686 delete VariableDie;
1687 return NULL;
1688 }
1689 DV->setDIE(VariableDie);
1690 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001691 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001692
1693 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +00001694 int FI = 0;
Devang Patel9e3bd2c2010-08-31 06:11:28 +00001695 if (findVariableFrameIndex(DV, &FI))
1696 addVariableAddress(DV, VariableDie, FI);
1697
Devang Patel53bb5c92009-11-10 23:06:00 +00001698 DV->setDIE(VariableDie);
1699 return VariableDie;
1700
1701}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001702
Devang Patel193f7202009-11-24 01:14:22 +00001703void DwarfDebug::addPubTypes(DISubprogram SP) {
1704 DICompositeType SPTy = SP.getType();
1705 unsigned SPTag = SPTy.getTag();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001706 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel193f7202009-11-24 01:14:22 +00001707 return;
1708
1709 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001710 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +00001711 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +00001712 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +00001713 continue;
1714 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +00001715 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +00001716 && !CATy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001717 CompileUnit *TheCU = getCompileUnit(CATy);
1718 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1719 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001720 }
1721 }
1722}
1723
Devang Patel2c4ceb12009-11-21 02:48:08 +00001724/// constructScopeDIE - Construct a DIE for this scope.
1725DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001726 if (!Scope || !Scope->getScopeNode())
1727 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001728
Devang Patel3c91b052010-03-08 20:52:55 +00001729 DIScope DS(Scope->getScopeNode());
1730 DIE *ScopeDIE = NULL;
1731 if (Scope->getInlinedAt())
1732 ScopeDIE = constructInlinedScopeDIE(Scope);
1733 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +00001734 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001735 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001736 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001737 // Note down abstract DIE.
1738 if (ScopeDIE)
1739 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1740 }
Devang Patel3c91b052010-03-08 20:52:55 +00001741 else
Devang Patel2db49d72010-05-07 18:11:54 +00001742 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001743 }
Devang Patelaead63c2010-03-29 22:59:58 +00001744 else
Devang Patel3c91b052010-03-08 20:52:55 +00001745 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001746 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001747
Devang Patel53bb5c92009-11-10 23:06:00 +00001748 // Add variables to scope.
Devang Patele03161c2010-08-09 18:51:29 +00001749 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001750 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001751 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001752 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001753 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001754 }
1755
1756 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001757 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001758 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1759 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001760 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001761 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001762 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001763 }
Devang Patel193f7202009-11-24 01:14:22 +00001764
Jim Grosbach1e20b962010-07-21 21:21:52 +00001765 if (DS.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001766 addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001767
Devang Patel193f7202009-11-24 01:14:22 +00001768 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001769}
1770
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001771/// GetOrCreateSourceID - Look up the source id with the given directory and
1772/// source file names. If none currently exists, create a new id and insert it
1773/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1774/// maps as well.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001775unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001776 unsigned DId;
Devang Patel37032352010-07-27 20:51:15 +00001777 assert (DirName.empty() == false && "Invalid directory name!");
Devang Patel2f584852010-07-24 00:53:22 +00001778
Devang Patel1905a182010-09-16 20:57:49 +00001779 // If FE did not provide a file name, then assume stdin.
1780 if (FileName.empty())
1781 return GetOrCreateSourceID(DirName, "<stdin>");
1782
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001783 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1784 if (DI != DirectoryIdMap.end()) {
1785 DId = DI->getValue();
1786 } else {
1787 DId = DirectoryNames.size() + 1;
1788 DirectoryIdMap[DirName] = DId;
1789 DirectoryNames.push_back(DirName);
1790 }
1791
1792 unsigned FId;
1793 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1794 if (FI != SourceFileIdMap.end()) {
1795 FId = FI->getValue();
1796 } else {
1797 FId = SourceFileNames.size() + 1;
1798 SourceFileIdMap[FileName] = FId;
1799 SourceFileNames.push_back(FileName);
1800 }
1801
1802 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1803 SourceIdMap.find(std::make_pair(DId, FId));
1804 if (SI != SourceIdMap.end())
1805 return SI->second;
1806
1807 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1808 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1809 SourceIds.push_back(std::make_pair(DId, FId));
1810
1811 return SrcId;
1812}
1813
Devang Patel6404e4e2009-12-15 19:16:48 +00001814/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1815DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel163a9f72010-05-10 22:49:55 +00001816 CompileUnit *TheCU = getCompileUnit(NS);
1817 DIE *NDie = TheCU->getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001818 if (NDie)
1819 return NDie;
1820 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel163a9f72010-05-10 22:49:55 +00001821 TheCU->insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +00001822 if (!NS.getName().empty())
1823 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patel81625742010-08-09 20:20:05 +00001824 addSourceLine(NDie, NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001825 addToContextOwner(NDie, NS.getContext());
1826 return NDie;
1827}
1828
Jim Grosbach1e20b962010-07-21 21:21:52 +00001829/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +00001830/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001831void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001832 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001833 StringRef FN = DIUnit.getFilename();
1834 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001835 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001836
1837 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001838 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001839 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001840 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001841 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001842 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +00001843 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1844 // simplifies debug range entries.
Devang Patel9b93b6b2010-06-28 22:22:47 +00001845 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +00001846 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +00001847 // compile unit in debug_line section.
Devang Patelae84d5b2010-08-31 23:50:19 +00001848 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
1849 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
1850 Asm->GetTempSymbol("section_line"));
1851 else
1852 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001853
Devang Patel65dbc902009-11-25 17:36:49 +00001854 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001855 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001856 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001857 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001858
Devang Patel65dbc902009-11-25 17:36:49 +00001859 StringRef Flags = DIUnit.getFlags();
1860 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001861 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001862
1863 unsigned RVer = DIUnit.getRunTimeVersion();
1864 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001865 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001866 dwarf::DW_FORM_data1, RVer);
1867
Devang Patel163a9f72010-05-10 22:49:55 +00001868 CompileUnit *NewCU = new CompileUnit(ID, Die);
1869 if (!FirstCU)
1870 FirstCU = NewCU;
1871 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001872}
1873
Devang Patel163a9f72010-05-10 22:49:55 +00001874/// getCompielUnit - Get CompileUnit DIE.
1875CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1876 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1877 DIDescriptor D(N);
1878 const MDNode *CUNode = NULL;
1879 if (D.isCompileUnit())
1880 CUNode = N;
1881 else if (D.isSubprogram())
1882 CUNode = DISubprogram(N).getCompileUnit();
1883 else if (D.isType())
1884 CUNode = DIType(N).getCompileUnit();
1885 else if (D.isGlobalVariable())
1886 CUNode = DIGlobalVariable(N).getCompileUnit();
1887 else if (D.isVariable())
1888 CUNode = DIVariable(N).getCompileUnit();
1889 else if (D.isNameSpace())
1890 CUNode = DINameSpace(N).getCompileUnit();
1891 else if (D.isFile())
1892 CUNode = DIFile(N).getCompileUnit();
1893 else
1894 return FirstCU;
1895
1896 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1897 = CUMap.find(CUNode);
1898 if (I == CUMap.end())
1899 return FirstCU;
1900 return I->second;
1901}
1902
Devang Patel0c4720c2010-08-23 18:25:56 +00001903/// isUnsignedDIType - Return true if type encoding is unsigned.
1904static bool isUnsignedDIType(DIType Ty) {
1905 DIDerivedType DTy(Ty);
1906 if (DTy.Verify())
1907 return isUnsignedDIType(DTy.getTypeDerivedFrom());
1908
1909 DIBasicType BTy(Ty);
1910 if (BTy.Verify()) {
1911 unsigned Encoding = BTy.getEncoding();
1912 if (Encoding == dwarf::DW_ATE_unsigned ||
1913 Encoding == dwarf::DW_ATE_unsigned_char)
1914 return true;
1915 }
1916 return false;
1917}
Devang Patel163a9f72010-05-10 22:49:55 +00001918
1919/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001920void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +00001921 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001922
Devang Patel905cf5e2009-09-04 23:59:07 +00001923 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +00001924 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +00001925 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001926
1927 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001928 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +00001929 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001930 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001931
Devang Patel9fa539c2010-08-10 01:37:23 +00001932 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +00001933 DIE *VariableDIE = new DIE(GV.getTag());
1934
1935 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001936
Devang Patel9fa539c2010-08-10 01:37:23 +00001937 // Add name.
1938 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1939 GV.getDisplayName());
1940 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +00001941 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel9fa539c2010-08-10 01:37:23 +00001942 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1943 getRealLinkageName(LinkageName));
1944 // Add type.
1945 addType(VariableDIE, GTy);
Devang Patel50d80e32010-04-13 20:35:04 +00001946 if (GTy.isCompositeType() && !GTy.getName().empty()
1947 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001948 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001949 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +00001950 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001951 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001952 // Add scoping info.
1953 if (!GV.isLocalToUnit()) {
1954 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1955 // Expose as global.
1956 TheCU->addGlobal(GV.getName(), VariableDIE);
1957 }
1958 // Add line number info.
1959 addSourceLine(VariableDIE, GV);
1960 // Add to map.
1961 TheCU->insertDIE(N, VariableDIE);
1962 // Add to context owner.
1963 DIDescriptor GVContext = GV.getContext();
1964 addToContextOwner(VariableDIE, GVContext);
1965 // Add location.
Devang Patel29368072010-08-10 07:11:13 +00001966 if (isGlobalVariable) {
1967 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1968 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1969 addLabel(Block, 0, dwarf::DW_FORM_udata,
1970 Asm->Mang->getSymbol(GV.getGlobal()));
1971 // Do not create specification DIE if context is either compile unit
1972 // or a subprogram.
1973 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1974 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1975 // Create specification DIE.
1976 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1977 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1978 dwarf::DW_FORM_ref4, VariableDIE);
1979 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1980 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1981 TheCU->addDie(VariableSpecDIE);
1982 } else {
1983 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1984 }
1985 } else if (Constant *C = GV.getConstant()) {
1986 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
Devang Patel0c4720c2010-08-23 18:25:56 +00001987 if (isUnsignedDIType(GTy))
Devang Patel29368072010-08-10 07:11:13 +00001988 addUInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
1989 CI->getZExtValue());
1990 else
1991 addSInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1992 CI->getSExtValue());
Devang Patel29368072010-08-10 07:11:13 +00001993 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001994 }
Devang Patel13e16b62009-06-26 01:49:18 +00001995 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001996}
1997
Devang Patel163a9f72010-05-10 22:49:55 +00001998/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001999void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00002000 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002001
Stuart Hastings639336e2010-04-06 21:38:29 +00002002 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00002003 CompileUnit *TheCU = getCompileUnit(N);
2004 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +00002005 return;
2006
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002007 if (!SP.isDefinition())
2008 // This is a method declaration which will be handled while constructing
2009 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00002010 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002011
Stuart Hastings639336e2010-04-06 21:38:29 +00002012 DIE *SubprogramDie = createSubprogramDIE(SP);
2013
2014 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00002015 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002016
2017 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00002018 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00002019
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002020 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +00002021 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00002022
Devang Patel13e16b62009-06-26 01:49:18 +00002023 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002024}
2025
Devang Patel2c4ceb12009-11-21 02:48:08 +00002026/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00002027/// content. Create global DIEs and emit initial debug info sections.
2028/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00002029void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00002030 if (DisableDebugInfoPrinting)
2031 return;
2032
Devang Patel78ab9e22009-07-30 18:56:46 +00002033 DebugInfoFinder DbgFinder;
2034 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00002035
Chris Lattnerd850ac72010-04-05 02:19:28 +00002036 bool HasDebugInfo = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002037
Chris Lattnerd850ac72010-04-05 02:19:28 +00002038 // Scan all the compile-units to see if there are any marked as the main unit.
2039 // if not, we do not generate debug info.
2040 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2041 E = DbgFinder.compile_unit_end(); I != E; ++I) {
2042 if (DICompileUnit(*I).isMain()) {
2043 HasDebugInfo = true;
2044 break;
2045 }
2046 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002047
Chris Lattnerd850ac72010-04-05 02:19:28 +00002048 if (!HasDebugInfo) return;
2049
2050 // Tell MMI that we have debug info.
2051 MMI->setDebugInfoAvailability(true);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002052
Chris Lattnerbe15beb2010-04-04 23:17:54 +00002053 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00002054 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002055
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002056 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00002057 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2058 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002059 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002060
Devang Patel53bb5c92009-11-10 23:06:00 +00002061 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00002062 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
2063 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002064 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00002065
Devang Patelc366f832009-12-10 19:14:49 +00002066 // Create DIEs for each global variable.
2067 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
2068 E = DbgFinder.global_variable_end(); I != E; ++I)
2069 constructGlobalVariableDIE(*I);
2070
Devang Patele7e5a0f2010-08-10 20:01:20 +00002071 //getOrCreateTypeDIE
2072 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
2073 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2074 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2075
Devang Patel1a7ca032010-09-28 18:08:20 +00002076 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
2077 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2078 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2079
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002080 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00002081 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002082
2083 // Print out .file directives to specify files for .loc directives. These are
2084 // printed out early so that they precede any .loc directives.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002085 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002086 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2087 // Remember source id starts at 1.
2088 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002089 // FIXME: don't use sys::path for this! This should not depend on the
2090 // host.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002091 sys::Path FullPath(getSourceDirectoryName(Id.first));
2092 bool AppendOk =
2093 FullPath.appendComponent(getSourceFileName(Id.second));
2094 assert(AppendOk && "Could not append filename to directory!");
2095 AppendOk = false;
Chris Lattnera6594fc2010-01-25 18:58:59 +00002096 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002097 }
2098 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002099}
2100
Devang Patel2c4ceb12009-11-21 02:48:08 +00002101/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002102///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002103void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00002104 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +00002105 const Module *M = MMI->getModule();
Devang Patele9a1cca2010-08-02 17:32:15 +00002106 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +00002107 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2108 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2109 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2110 DISubprogram SP(AllSPs->getOperand(SI));
2111 if (!SP.Verify()) continue;
2112
2113 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +00002114 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +00002115 StringRef FName = SP.getLinkageName();
2116 if (FName.empty())
2117 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002118 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +00002119 if (!NMD) continue;
2120 unsigned E = NMD->getNumOperands();
2121 if (!E) continue;
2122 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Patele9a1cca2010-08-02 17:32:15 +00002123 DeadFnScopeMap[SP] = Scope;
Devang Patel4a1cad62010-06-28 18:25:03 +00002124 for (unsigned I = 0; I != E; ++I) {
2125 DIVariable DV(NMD->getOperand(I));
2126 if (!DV.Verify()) continue;
2127 Scope->addVariable(new DbgVariable(DV));
2128 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002129
Devang Patel4a1cad62010-06-28 18:25:03 +00002130 // Construct subprogram DIE and add variables DIEs.
2131 constructSubprogramDIE(SP);
2132 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patele03161c2010-08-09 18:51:29 +00002133 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel4a1cad62010-06-28 18:25:03 +00002134 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2135 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2136 if (VariableDIE)
2137 ScopeDIE->addChild(VariableDIE);
2138 }
2139 }
2140 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002141
Devang Patel53bb5c92009-11-10 23:06:00 +00002142 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2143 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2144 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2145 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002146 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00002147 }
2148
Devang Patele9f8f5e2010-05-07 20:54:48 +00002149 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00002150 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2151 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002152 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00002153 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00002154 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00002155 if (!NDie) continue;
2156 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00002157 }
2158
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002159 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002160 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002161 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002162 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002163 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002164
2165 // End text sections.
2166 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002167 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00002168 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002169 }
2170
2171 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002172 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002173
2174 // Emit function debug frame information
2175 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2176 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002177 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002178
2179 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002180 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002181
2182 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00002183 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002184
2185 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002186 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002187
Devang Patelaf608bd2010-08-24 00:06:12 +00002188 // Emit source line correspondence into a debug line section.
2189 emitDebugLines();
2190
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002191 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002192 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002193
Devang Patel193f7202009-11-24 01:14:22 +00002194 // Emit info into a debug pubtypes section.
2195 emitDebugPubTypes();
2196
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002197 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002198 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002199
2200 // Emit info into a debug aranges section.
2201 EmitDebugARanges();
2202
2203 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002204 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002205
2206 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002207 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002208
2209 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002210 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002211
Chris Lattnerbc733f52010-03-13 02:17:42 +00002212 // Emit info into a debug str section.
2213 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002214
Devang Patele9a1cca2010-08-02 17:32:15 +00002215 // clean up.
2216 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +00002217 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2218 E = CUMap.end(); I != E; ++I)
2219 delete I->second;
2220 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002221}
2222
Devang Patel53bb5c92009-11-10 23:06:00 +00002223/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002224DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002225 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002226
Devang Patel2db49d72010-05-07 18:11:54 +00002227 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00002228 if (AbsDbgVariable)
2229 return AbsDbgVariable;
2230
Devang Patel2db49d72010-05-07 18:11:54 +00002231 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002232 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00002233 if (!Scope)
2234 return NULL;
2235
Devang Patel26c1e562010-05-20 16:36:41 +00002236 AbsDbgVariable = new DbgVariable(Var);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002237 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002238 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00002239 return AbsDbgVariable;
2240}
2241
Devang Patelee432862010-05-20 19:57:06 +00002242/// collectVariableInfoFromMMITable - Collect variable information from
2243/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002244void
Devang Patelee432862010-05-20 19:57:06 +00002245DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2246 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002247 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patele717faa2009-10-06 01:26:37 +00002248 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2249 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2250 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00002251 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00002252 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00002253 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002254 DIVariable DV(Var);
2255 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002256
Chris Lattnerde4845c2010-04-02 19:42:39 +00002257 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002258 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002259 Scope = ConcreteScopes.lookup(IA);
2260 if (Scope == 0)
2261 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002262
Devang Patelfb0ee432009-11-10 23:20:04 +00002263 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002264 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00002265 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00002266
Devang Patel26c1e562010-05-20 16:36:41 +00002267 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2268 DbgVariable *RegVar = new DbgVariable(DV);
2269 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002270 Scope->addVariable(RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +00002271 if (AbsDbgVariable) {
2272 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2273 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2274 }
Devang Patele717faa2009-10-06 01:26:37 +00002275 }
Devang Patelee432862010-05-20 19:57:06 +00002276}
Devang Patel90a48ad2010-03-15 18:33:46 +00002277
Jim Grosbach1e20b962010-07-21 21:21:52 +00002278/// isDbgValueInUndefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00002279/// DBG_VALUE instruction, is in undefined reg.
2280static bool isDbgValueInUndefinedReg(const MachineInstr *MI) {
2281 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2282 if (MI->getOperand(0).isReg() && !MI->getOperand(0).getReg())
2283 return true;
2284 return false;
2285}
2286
Jim Grosbach1e20b962010-07-21 21:21:52 +00002287/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00002288/// DBG_VALUE instruction, is in a defined reg.
2289static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2290 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2291 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
2292 return true;
2293 return false;
2294}
2295
Devang Patelee432862010-05-20 19:57:06 +00002296/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002297void
Devang Patel78e127d2010-06-25 22:07:34 +00002298DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2299 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002300
Devang Patelee432862010-05-20 19:57:06 +00002301 /// collection info from MMI table.
2302 collectVariableInfoFromMMITable(MF, Processed);
2303
2304 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patel90a48ad2010-03-15 18:33:46 +00002305 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002306 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelee432862010-05-20 19:57:06 +00002307 I != E; ++I)
Devang Patel90a48ad2010-03-15 18:33:46 +00002308 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2309 II != IE; ++II) {
2310 const MachineInstr *MInsn = II;
Devang Patelc3f5f782010-05-25 23:40:22 +00002311 if (!MInsn->isDebugValue() || isDbgValueInUndefinedReg(MInsn))
Devang Patel90a48ad2010-03-15 18:33:46 +00002312 continue;
Devang Patelee432862010-05-20 19:57:06 +00002313 DbgValues.push_back(MInsn);
2314 }
Devang Patel90a48ad2010-03-15 18:33:46 +00002315
Devang Patelc3f5f782010-05-25 23:40:22 +00002316 // This is a collection of DBV_VALUE instructions describing same variable.
2317 SmallVector<const MachineInstr *, 4> MultipleValues;
Devang Patelee432862010-05-20 19:57:06 +00002318 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2319 E = DbgValues.end(); I != E; ++I) {
2320 const MachineInstr *MInsn = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00002321 MultipleValues.clear();
2322 if (isDbgValueInDefinedReg(MInsn))
2323 MultipleValues.push_back(MInsn);
Devang Patelee432862010-05-20 19:57:06 +00002324 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2325 if (Processed.count(DV) != 0)
2326 continue;
2327
Devang Patel354eb7e2010-06-02 19:05:13 +00002328 const MachineInstr *PrevMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002329 for (SmallVector<const MachineInstr *, 8>::iterator MI = I+1,
Devang Patelc3f5f782010-05-25 23:40:22 +00002330 ME = DbgValues.end(); MI != ME; ++MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002331 const MDNode *Var =
Devang Patelc3f5f782010-05-25 23:40:22 +00002332 (*MI)->getOperand((*MI)->getNumOperands()-1).getMetadata();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002333 if (Var == DV && isDbgValueInDefinedReg(*MI) &&
Devang Patel354eb7e2010-06-02 19:05:13 +00002334 !PrevMI->isIdenticalTo(*MI))
Devang Patelc3f5f782010-05-25 23:40:22 +00002335 MultipleValues.push_back(*MI);
Devang Patel354eb7e2010-06-02 19:05:13 +00002336 PrevMI = *MI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002337 }
2338
Devang Patelee432862010-05-20 19:57:06 +00002339 DbgScope *Scope = findDbgScope(MInsn);
Devang Pateld8720f42010-05-27 20:25:04 +00002340 bool CurFnArg = false;
2341 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2342 DISubprogram(DV.getContext()).describes(MF->getFunction()))
2343 CurFnArg = true;
2344 if (!Scope && CurFnArg)
Devang Patelc0c5a262010-05-21 00:10:20 +00002345 Scope = CurrentFnDbgScope;
Devang Patelee432862010-05-20 19:57:06 +00002346 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00002347 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00002348 continue;
2349
2350 Processed.insert(DV);
Devang Patelee432862010-05-20 19:57:06 +00002351 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patelee432862010-05-20 19:57:06 +00002352 Scope->addVariable(RegVar);
Devang Patelc0c5a262010-05-21 00:10:20 +00002353 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2354 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2355 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002356 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002357 if (MultipleValues.size() <= 1) {
2358 DbgVariableToDbgInstMap[RegVar] = MInsn;
2359 continue;
2360 }
2361
2362 // handle multiple DBG_VALUE instructions describing one variable.
Devang Patelc3f5f782010-05-25 23:40:22 +00002363 if (DotDebugLocEntries.empty())
Devang Patel80250682010-05-26 23:55:23 +00002364 RegVar->setDotDebugLocOffset(0);
2365 else
2366 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Devang Patele2df8422010-05-26 17:29:32 +00002367 const MachineInstr *Begin = NULL;
2368 const MachineInstr *End = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002369 for (SmallVector<const MachineInstr *, 4>::iterator
2370 MVI = MultipleValues.begin(), MVE = MultipleValues.end();
Devang Patel61409622010-07-07 20:12:52 +00002371 MVI != MVE; ++MVI) {
Devang Patele2df8422010-05-26 17:29:32 +00002372 if (!Begin) {
2373 Begin = *MVI;
2374 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002375 }
Devang Patele2df8422010-05-26 17:29:32 +00002376 End = *MVI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002377 MachineLocation MLoc;
Devang Patelb2cf5812010-08-04 18:40:52 +00002378 if (Begin->getNumOperands() == 3) {
2379 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2380 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2381 } else
2382 MLoc = Asm->getDebugValueLocation(Begin);
2383
Devang Patele2df8422010-05-26 17:29:32 +00002384 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2385 const MCSymbol *SLabel = getLabelBeforeInsn(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002386 if (MLoc.getReg())
2387 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
2388
Devang Patele2df8422010-05-26 17:29:32 +00002389 Begin = End;
2390 if (MVI + 1 == MVE) {
2391 // If End is the last instruction then its value is valid
Devang Patelc3f5f782010-05-25 23:40:22 +00002392 // until the end of the funtion.
Devang Patelb2cf5812010-08-04 18:40:52 +00002393 MachineLocation EMLoc;
2394 if (End->getNumOperands() == 3) {
2395 if (End->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2396 EMLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2397 } else
2398 EMLoc = Asm->getDebugValueLocation(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002399 if (EMLoc.getReg())
2400 DotDebugLocEntries.
2401 push_back(DotDebugLocEntry(SLabel, FunctionEndSym, EMLoc));
Devang Patelc3f5f782010-05-25 23:40:22 +00002402 }
2403 }
2404 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00002405 }
Devang Patel98e1cac2010-05-14 21:01:35 +00002406
2407 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00002408 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00002409 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00002410 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002411 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00002412 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00002413 continue;
2414 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2415 if (Scope)
Devang Patel26c1e562010-05-20 16:36:41 +00002416 Scope->addVariable(new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00002417 }
2418 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002419}
Devang Patel98e1cac2010-05-14 21:01:35 +00002420
Devang Patelc3f5f782010-05-25 23:40:22 +00002421/// getLabelBeforeInsn - Return Label preceding the instruction.
2422const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2423 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2424 LabelsBeforeInsn.find(MI);
2425 if (I == LabelsBeforeInsn.end())
2426 // FunctionBeginSym always preceeds all the instruction in current function.
2427 return FunctionBeginSym;
2428 return I->second;
2429}
2430
2431/// getLabelAfterInsn - Return Label immediately following the instruction.
2432const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2433 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2434 LabelsAfterInsn.find(MI);
2435 if (I == LabelsAfterInsn.end())
2436 return NULL;
2437 return I->second;
Devang Patele717faa2009-10-06 01:26:37 +00002438}
2439
Devang Patelcbbe2872010-10-26 17:49:02 +00002440/// beginInstruction - Process beginning of an instruction.
2441void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002442 if (InsnNeedsLabel.count(MI) == 0) {
2443 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patel553881b2010-03-29 17:20:31 +00002444 return;
Devang Patelc3f5f782010-05-25 23:40:22 +00002445 }
Devang Patelaead63c2010-03-29 22:59:58 +00002446
Devang Patelb2b31a62010-05-26 19:37:24 +00002447 // Check location.
2448 DebugLoc DL = MI->getDebugLoc();
2449 if (!DL.isUnknown()) {
2450 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2451 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Pateleac9c072010-04-27 19:46:33 +00002452 PrevInstLoc = DL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002453 LabelsBeforeInsn[MI] = PrevLabel;
2454 return;
Devang Pateleac9c072010-04-27 19:46:33 +00002455 }
Devang Patel553881b2010-03-29 17:20:31 +00002456
Jim Grosbach1e20b962010-07-21 21:21:52 +00002457 // If location is unknown then use temp label for this DBG_VALUE
Devang Patelb2b31a62010-05-26 19:37:24 +00002458 // instruction.
2459 if (MI->isDebugValue()) {
Devang Patel77051f52010-05-26 21:23:46 +00002460 PrevLabel = MMI->getContext().CreateTempSymbol();
2461 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00002462 LabelsBeforeInsn[MI] = PrevLabel;
2463 return;
2464 }
2465
2466 if (UnknownLocations) {
2467 PrevLabel = recordSourceLine(0, 0, 0);
2468 LabelsBeforeInsn[MI] = PrevLabel;
2469 return;
2470 }
2471
2472 assert (0 && "Instruction is not processed!");
Devang Patel0d20ac82009-10-06 01:50:42 +00002473}
2474
Devang Patelcbbe2872010-10-26 17:49:02 +00002475/// endInstruction - Process end of an instruction.
2476void DwarfDebug::endInstruction(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002477 if (InsnsEndScopeSet.count(MI) != 0) {
2478 // Emit a label if this instruction ends a scope.
2479 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2480 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002481 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002482 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002483}
2484
Devang Pateleac9c072010-04-27 19:46:33 +00002485/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002486DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel61409622010-07-07 20:12:52 +00002487 const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002488 if (!InlinedAt) {
2489 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2490 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002491 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002492 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2493 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002494 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002495 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002496 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002497 WScope->setParent(Parent);
2498 Parent->addScope(WScope);
2499 }
2500
2501 if (!WScope->getParent()) {
2502 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastingscad22ad2010-06-15 23:06:30 +00002503 // We used to check only for a linkage name, but that fails
2504 // since we began omitting the linkage name for private
2505 // functions. The new way is to check for the name in metadata,
2506 // but that's not supported in old .ll test cases. Ergo, we
2507 // check both.
Stuart Hastings215aa152010-06-11 20:08:44 +00002508 if (SPName == Asm->MF->getFunction()->getName() ||
2509 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Pateleac9c072010-04-27 19:46:33 +00002510 CurrentFnDbgScope = WScope;
2511 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002512
Devang Pateleac9c072010-04-27 19:46:33 +00002513 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002514 }
2515
Devang Patel78e127d2010-06-25 22:07:34 +00002516 getOrCreateAbstractScope(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00002517 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2518 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002519 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002520
2521 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2522 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2523 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002524 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002525 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002526 WScope->setParent(Parent);
2527 Parent->addScope(WScope);
2528
2529 ConcreteScopes[InlinedAt] = WScope;
Devang Pateleac9c072010-04-27 19:46:33 +00002530
2531 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002532}
2533
Devang Pateleac9c072010-04-27 19:46:33 +00002534/// hasValidLocation - Return true if debug location entry attached with
2535/// machine instruction encodes valid location info.
2536static bool hasValidLocation(LLVMContext &Ctx,
2537 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002538 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002539 DebugLoc DL = MInsn->getDebugLoc();
2540 if (DL.isUnknown()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002541
Devang Patele9f8f5e2010-05-07 20:54:48 +00002542 const MDNode *S = DL.getScope(Ctx);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002543
Devang Pateleac9c072010-04-27 19:46:33 +00002544 // There is no need to create another DIE for compile unit. For all
2545 // other scopes, create one DbgScope now. This will be translated
2546 // into a scope DIE at the end.
2547 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002548
Devang Pateleac9c072010-04-27 19:46:33 +00002549 Scope = S;
2550 InlinedAt = DL.getInlinedAt(Ctx);
2551 return true;
2552}
2553
2554/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2555/// hierarchy.
2556static void calculateDominanceGraph(DbgScope *Scope) {
2557 assert (Scope && "Unable to calculate scop edominance graph!");
2558 SmallVector<DbgScope *, 4> WorkStack;
2559 WorkStack.push_back(Scope);
2560 unsigned Counter = 0;
2561 while (!WorkStack.empty()) {
2562 DbgScope *WS = WorkStack.back();
2563 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2564 bool visitedChildren = false;
2565 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2566 SE = Children.end(); SI != SE; ++SI) {
2567 DbgScope *ChildScope = *SI;
2568 if (!ChildScope->getDFSOut()) {
2569 WorkStack.push_back(ChildScope);
2570 visitedChildren = true;
2571 ChildScope->setDFSIn(++Counter);
2572 break;
2573 }
2574 }
2575 if (!visitedChildren) {
2576 WorkStack.pop_back();
2577 WS->setDFSOut(++Counter);
2578 }
2579 }
2580}
2581
2582/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002583static
Devang Pateleac9c072010-04-27 19:46:33 +00002584void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2585 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2586{
2587#ifndef NDEBUG
2588 unsigned PrevDFSIn = 0;
2589 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2590 I != E; ++I) {
2591 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2592 II != IE; ++II) {
2593 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002594 const MDNode *Scope = NULL;
2595 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002596
2597 // Check if instruction has valid location information.
2598 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2599 dbgs() << " [ ";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002600 if (InlinedAt)
Devang Pateleac9c072010-04-27 19:46:33 +00002601 dbgs() << "*";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002602 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Pateleac9c072010-04-27 19:46:33 +00002603 MI2ScopeMap.find(MInsn);
2604 if (DI != MI2ScopeMap.end()) {
2605 DbgScope *S = DI->second;
2606 dbgs() << S->getDFSIn();
2607 PrevDFSIn = S->getDFSIn();
2608 } else
2609 dbgs() << PrevDFSIn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002610 } else
Devang Pateleac9c072010-04-27 19:46:33 +00002611 dbgs() << " [ x" << PrevDFSIn;
2612 dbgs() << " ]";
2613 MInsn->dump();
2614 }
2615 dbgs() << "\n";
2616 }
2617#endif
2618}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002619/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002620/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002621bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002622 // If scope information was extracted using .dbg intrinsics then there is not
2623 // any need to extract these information by scanning each instruction.
2624 if (!DbgScopeMap.empty())
2625 return false;
2626
Dan Gohman314bf7c2010-04-23 01:18:53 +00002627 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002628 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2629 SmallVector<DbgRange, 4> MIRanges;
2630 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002631 const MDNode *PrevScope = NULL;
2632 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002633 const MachineInstr *RangeBeginMI = NULL;
2634 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002635 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002636 I != E; ++I) {
2637 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2638 II != IE; ++II) {
2639 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002640 const MDNode *Scope = NULL;
2641 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002642
2643 // Check if instruction has valid location information.
2644 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2645 PrevMI = MInsn;
2646 continue;
2647 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002648
Devang Pateleac9c072010-04-27 19:46:33 +00002649 // If scope has not changed then skip this instruction.
2650 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2651 PrevMI = MInsn;
2652 continue;
2653 }
2654
Jim Grosbach1e20b962010-07-21 21:21:52 +00002655 if (RangeBeginMI) {
2656 // If we have alread seen a beginning of a instruction range and
Devang Pateleac9c072010-04-27 19:46:33 +00002657 // current instruction scope does not match scope of first instruction
2658 // in this range then create a new instruction range.
2659 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002660 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel61409622010-07-07 20:12:52 +00002661 PrevInlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002662 MIRanges.push_back(R);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002663 }
Devang Pateleac9c072010-04-27 19:46:33 +00002664
2665 // This is a beginning of a new instruction range.
2666 RangeBeginMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002667
Devang Pateleac9c072010-04-27 19:46:33 +00002668 // Reset previous markers.
2669 PrevMI = MInsn;
2670 PrevScope = Scope;
2671 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002672 }
2673 }
2674
Devang Pateleac9c072010-04-27 19:46:33 +00002675 // Create last instruction range.
2676 if (RangeBeginMI && PrevMI && PrevScope) {
2677 DbgRange R(RangeBeginMI, PrevMI);
2678 MIRanges.push_back(R);
2679 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002680 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002681
Devang Patel344130e2010-01-04 20:44:00 +00002682 if (!CurrentFnDbgScope)
2683 return false;
2684
Devang Pateleac9c072010-04-27 19:46:33 +00002685 calculateDominanceGraph(CurrentFnDbgScope);
2686 if (PrintDbgScope)
2687 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2688
2689 // Find ranges of instructions covered by each DbgScope;
2690 DbgScope *PrevDbgScope = NULL;
2691 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2692 RE = MIRanges.end(); RI != RE; ++RI) {
2693 const DbgRange &R = *RI;
2694 DbgScope *S = MI2ScopeMap.lookup(R.first);
2695 assert (S && "Lost DbgScope for a machine instruction!");
2696 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2697 PrevDbgScope->closeInsnRange(S);
2698 S->openInsnRange(R.first);
2699 S->extendInsnRange(R.second);
2700 PrevDbgScope = S;
2701 }
2702
2703 if (PrevDbgScope)
2704 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002705
Devang Patele37b0c62010-04-08 18:43:56 +00002706 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002707
2708 return !DbgScopeMap.empty();
2709}
2710
Jim Grosbach1e20b962010-07-21 21:21:52 +00002711/// identifyScopeMarkers() -
Devang Pateleac9c072010-04-27 19:46:33 +00002712/// Each DbgScope has first instruction and last instruction to mark beginning
2713/// and end of a scope respectively. Create an inverse map that list scopes
2714/// starts (and ends) with an instruction. One instruction may start (or end)
2715/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002716void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002717 SmallVector<DbgScope *, 4> WorkList;
2718 WorkList.push_back(CurrentFnDbgScope);
2719 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002720 DbgScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002721
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002722 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002723 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002724 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002725 SE = Children.end(); SI != SE; ++SI)
2726 WorkList.push_back(*SI);
2727
Devang Patel53bb5c92009-11-10 23:06:00 +00002728 if (S->isAbstractScope())
2729 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002730
Devang Pateleac9c072010-04-27 19:46:33 +00002731 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2732 if (Ranges.empty())
2733 continue;
2734 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2735 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002736 assert(RI->first && "DbgRange does not have first instruction!");
2737 assert(RI->second && "DbgRange does not have second instruction!");
Devang Pateleac9c072010-04-27 19:46:33 +00002738 InsnsEndScopeSet.insert(RI->second);
2739 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002740 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002741}
2742
Dan Gohman084751c2010-04-20 00:37:27 +00002743/// FindFirstDebugLoc - Find the first debug location in the function. This
2744/// is intended to be an approximation for the source position of the
2745/// beginning of the function.
2746static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2747 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2748 I != E; ++I)
2749 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2750 MBBI != MBBE; ++MBBI) {
2751 DebugLoc DL = MBBI->getDebugLoc();
2752 if (!DL.isUnknown())
2753 return DL;
2754 }
2755 return DebugLoc();
2756}
2757
Devang Patel9a31f0f2010-10-25 20:45:32 +00002758#ifndef NDEBUG
2759/// CheckLineNumbers - Count basicblocks whose instructions do not have any
2760/// line number information.
2761static void CheckLineNumbers(const MachineFunction *MF) {
2762 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2763 I != E; ++I) {
2764 bool FoundLineNo = false;
2765 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2766 II != IE; ++II) {
2767 const MachineInstr *MI = II;
2768 if (!MI->getDebugLoc().isUnknown()) {
2769 FoundLineNo = true;
2770 break;
2771 }
2772 }
Devang Patel4d7f9a02010-10-28 22:11:59 +00002773 if (!FoundLineNo && I->size())
Devang Patel9a31f0f2010-10-25 20:45:32 +00002774 ++BlocksWithoutLineNo;
2775 }
2776}
2777#endif
2778
Devang Patel2c4ceb12009-11-21 02:48:08 +00002779/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002780/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002781void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002782 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002783 if (!extractScopeInformation()) return;
Devang Patel60b35bd2009-10-06 18:37:31 +00002784
Devang Patel9a31f0f2010-10-25 20:45:32 +00002785#ifndef NDEBUG
2786 CheckLineNumbers(MF);
2787#endif
2788
Devang Pateleac9c072010-04-27 19:46:33 +00002789 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2790 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002791 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002792 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002793
2794 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2795 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002796 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002797 if (FDL.isUnknown()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002798
Devang Patele9f8f5e2010-05-07 20:54:48 +00002799 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings0db42712010-07-19 23:56:30 +00002800 const MDNode *TheScope = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002801
Chris Lattnerde4845c2010-04-02 19:42:39 +00002802 DISubprogram SP = getDISubprogram(Scope);
2803 unsigned Line, Col;
2804 if (SP.Verify()) {
2805 Line = SP.getLineNumber();
2806 Col = 0;
Stuart Hastings0db42712010-07-19 23:56:30 +00002807 TheScope = SP;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002808 } else {
2809 Line = FDL.getLine();
2810 Col = FDL.getCol();
Stuart Hastings0db42712010-07-19 23:56:30 +00002811 TheScope = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002812 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002813
Stuart Hastings0db42712010-07-19 23:56:30 +00002814 recordSourceLine(Line, Col, TheScope);
Devang Patelb2b31a62010-05-26 19:37:24 +00002815
Devang Patelb9abe9f2010-06-02 16:42:51 +00002816 /// ProcessedArgs - Collection of arguments already processed.
2817 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2818
Devang Patelb2b31a62010-05-26 19:37:24 +00002819 DebugLoc PrevLoc;
2820 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2821 I != E; ++I)
2822 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2823 II != IE; ++II) {
2824 const MachineInstr *MI = II;
2825 DebugLoc DL = MI->getDebugLoc();
2826 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002827 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2828 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2829 if (!DV.Verify()) continue;
Devang Patel55e97172010-05-27 16:47:30 +00002830 // If DBG_VALUE is for a local variable then it needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002831 if (DV.getTag() != dwarf::DW_TAG_arg_variable
Devang Patel7fb231c2010-07-01 21:38:08 +00002832 && isDbgValueInUndefinedReg(MI) == false)
Devang Patelb2b31a62010-05-26 19:37:24 +00002833 InsnNeedsLabel.insert(MI);
Devang Patel55e97172010-05-27 16:47:30 +00002834 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel7fb231c2010-07-01 21:38:08 +00002835 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2836 describes(MF->getFunction()))
Devang Patel55e97172010-05-27 16:47:30 +00002837 InsnNeedsLabel.insert(MI);
2838 // DBG_VALUE indicating argument location change needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002839 else if (isDbgValueInUndefinedReg(MI) == false
2840 && !ProcessedArgs.insert(DV))
Devang Patelb2b31a62010-05-26 19:37:24 +00002841 InsnNeedsLabel.insert(MI);
2842 } else {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002843 // If location is unknown then instruction needs a location only if
Devang Patelb2b31a62010-05-26 19:37:24 +00002844 // UnknownLocations flag is set.
2845 if (DL.isUnknown()) {
2846 if (UnknownLocations && !PrevLoc.isUnknown())
2847 InsnNeedsLabel.insert(MI);
2848 } else if (DL != PrevLoc)
2849 // Otherwise, instruction needs a location only if it is new location.
2850 InsnNeedsLabel.insert(MI);
2851 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002852
Devang Patelb2b31a62010-05-26 19:37:24 +00002853 if (!DL.isUnknown() || UnknownLocations)
2854 PrevLoc = DL;
2855 }
2856
2857 PrevLabel = FunctionBeginSym;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002858}
2859
Devang Patel2c4ceb12009-11-21 02:48:08 +00002860/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002861///
Chris Lattnereec791a2010-01-26 23:18:02 +00002862void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002863 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002864
Devang Patel344130e2010-01-04 20:44:00 +00002865 if (CurrentFnDbgScope) {
Devang Patel65eb4822010-05-22 00:04:14 +00002866
Devang Patelc3f5f782010-05-25 23:40:22 +00002867 // Define end label for subprogram.
2868 FunctionEndSym = Asm->GetTempSymbol("func_end",
2869 Asm->getFunctionNumber());
2870 // Assumes in correct section after the entry point.
2871 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002872
Devang Patel78e127d2010-06-25 22:07:34 +00002873 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2874 collectVariableInfo(MF, ProcessedVars);
Devang Patel65eb4822010-05-22 00:04:14 +00002875
Devang Patel344130e2010-01-04 20:44:00 +00002876 // Get function line info.
2877 if (!Lines.empty()) {
2878 // Get section line info.
2879 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2880 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2881 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2882 // Append the function info to section info.
2883 SectionLineInfos.insert(SectionLineInfos.end(),
2884 Lines.begin(), Lines.end());
2885 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002886
Devang Patel344130e2010-01-04 20:44:00 +00002887 // Construct abstract scopes.
2888 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel78e127d2010-06-25 22:07:34 +00002889 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel78e127d2010-06-25 22:07:34 +00002890 DISubprogram SP((*AI)->getScopeNode());
2891 if (SP.Verify()) {
2892 // Collect info for variables that were optimized out.
2893 StringRef FName = SP.getLinkageName();
2894 if (FName.empty())
2895 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002896 if (NamedMDNode *NMD =
2897 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
Devang Patel78e127d2010-06-25 22:07:34 +00002898 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002899 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00002900 if (!DV || !ProcessedVars.insert(DV))
2901 continue;
Devang Patel1d68d212010-06-30 00:11:08 +00002902 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel78e127d2010-06-25 22:07:34 +00002903 if (Scope)
2904 Scope->addVariable(new DbgVariable(DV));
2905 }
2906 }
2907 }
Devang Patel90e19aa2010-06-30 01:40:11 +00002908 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2909 constructScopeDIE(*AI);
Devang Patel78e127d2010-06-25 22:07:34 +00002910 }
Devang Patel61409622010-07-07 20:12:52 +00002911
Devang Patel9c488372010-05-04 06:15:30 +00002912 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002913
Devang Patel9c488372010-05-04 06:15:30 +00002914 if (!DisableFramePointerElim(*MF))
Jim Grosbach1e20b962010-07-21 21:21:52 +00002915 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
Devang Patel9c488372010-05-04 06:15:30 +00002916 dwarf::DW_FORM_flag, 1);
2917
2918
Chris Lattnerd38fee82010-04-05 00:13:49 +00002919 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002920 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002921 }
2922
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002923 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002924 CurrentFnDbgScope = NULL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002925 InsnNeedsLabel.clear();
Devang Patel26c1e562010-05-20 16:36:41 +00002926 DbgVariableToFrameIndexMap.clear();
2927 VarToAbstractVarMap.clear();
2928 DbgVariableToDbgInstMap.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002929 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002930 InsnsEndScopeSet.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002931 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002932 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002933 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002934 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002935 LabelsBeforeInsn.clear();
2936 LabelsAfterInsn.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002937 Lines.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002938 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002939}
2940
Devang Patel26c1e562010-05-20 16:36:41 +00002941/// recordVariableFrameIndex - Record a variable's index.
2942void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2943 assert (V && "Invalid DbgVariable!");
2944 DbgVariableToFrameIndexMap[V] = Index;
2945}
2946
2947/// findVariableFrameIndex - Return true if frame index for the variable
2948/// is found. Update FI to hold value of the index.
2949bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2950 assert (V && "Invalid DbgVariable!");
2951 DenseMap<const DbgVariable *, int>::iterator I =
2952 DbgVariableToFrameIndexMap.find(V);
2953 if (I == DbgVariableToFrameIndexMap.end())
2954 return false;
2955 *FI = I->second;
2956 return true;
2957}
2958
Jim Grosbach1e20b962010-07-21 21:21:52 +00002959/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patelee432862010-05-20 19:57:06 +00002960/// instruction.
2961DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2962 DbgScope *Scope = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002963 LLVMContext &Ctx =
Devang Patelee432862010-05-20 19:57:06 +00002964 MInsn->getParent()->getParent()->getFunction()->getContext();
2965 DebugLoc DL = MInsn->getDebugLoc();
2966
Jim Grosbach1e20b962010-07-21 21:21:52 +00002967 if (DL.isUnknown())
Devang Patelee432862010-05-20 19:57:06 +00002968 return Scope;
2969
2970 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2971 Scope = ConcreteScopes.lookup(IA);
2972 if (Scope == 0)
2973 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002974
Devang Patelee432862010-05-20 19:57:06 +00002975 return Scope;
2976}
2977
2978
Chris Lattnerc6087842010-03-09 04:54:43 +00002979/// recordSourceLine - Register a source line with debug info. Returns the
2980/// unique label that was emitted and which provides correspondence to
2981/// the source line list.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002982MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel61409622010-07-07 20:12:52 +00002983 const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00002984 StringRef Dir;
2985 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002986
Dan Gohman1cc0d622010-05-05 23:41:32 +00002987 unsigned Src = 1;
2988 if (S) {
2989 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002990
Dan Gohman1cc0d622010-05-05 23:41:32 +00002991 if (Scope.isCompileUnit()) {
2992 DICompileUnit CU(S);
2993 Dir = CU.getDirectory();
2994 Fn = CU.getFilename();
Devang Patel3cabc9d2010-10-28 17:30:52 +00002995 } else if (Scope.isFile()) {
2996 DIFile F(S);
2997 Dir = F.getDirectory();
2998 Fn = F.getFilename();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002999 } else if (Scope.isSubprogram()) {
3000 DISubprogram SP(S);
3001 Dir = SP.getDirectory();
3002 Fn = SP.getFilename();
3003 } else if (Scope.isLexicalBlock()) {
3004 DILexicalBlock DB(S);
3005 Dir = DB.getDirectory();
3006 Fn = DB.getFilename();
3007 } else
3008 assert(0 && "Unexpected scope info");
3009
3010 Src = GetOrCreateSourceID(Dir, Fn);
3011 }
3012
Chris Lattner63d78362010-03-14 08:36:50 +00003013 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner25b68c62010-03-14 08:15:55 +00003014 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00003015
Chris Lattnerc6087842010-03-09 04:54:43 +00003016 Asm->OutStreamer.EmitLabel(Label);
3017 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00003018}
3019
Bill Wendling829e67b2009-05-20 23:22:40 +00003020//===----------------------------------------------------------------------===//
3021// Emit Methods
3022//===----------------------------------------------------------------------===//
3023
Devang Patel2c4ceb12009-11-21 02:48:08 +00003024/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00003025///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003026unsigned
3027DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003028 // Get the children.
3029 const std::vector<DIE *> &Children = Die->getChildren();
3030
3031 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003032 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00003033 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00003034
3035 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003036 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00003037
3038 // Get the abbreviation for this DIE.
3039 unsigned AbbrevNumber = Die->getAbbrevNumber();
3040 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3041
3042 // Set DIE offset
3043 Die->setOffset(Offset);
3044
3045 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00003046 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003047
3048 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
3049 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3050
3051 // Size the DIE attribute values.
3052 for (unsigned i = 0, N = Values.size(); i < N; ++i)
3053 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00003054 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00003055
3056 // Size the DIE children if any.
3057 if (!Children.empty()) {
3058 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
3059 "Children flag not set");
3060
3061 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003062 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00003063
3064 // End of children marker.
3065 Offset += sizeof(int8_t);
3066 }
3067
3068 Die->setSize(Offset - Die->getOffset());
3069 return Offset;
3070}
3071
Devang Patel2c4ceb12009-11-21 02:48:08 +00003072/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00003073///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003074void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00003075 unsigned PrevOffset = 0;
3076 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3077 E = CUMap.end(); I != E; ++I) {
3078 // Compute size of compile unit header.
3079 static unsigned Offset = PrevOffset +
3080 sizeof(int32_t) + // Length of Compilation Unit Info
3081 sizeof(int16_t) + // DWARF version number
3082 sizeof(int32_t) + // Offset Into Abbrev. Section
3083 sizeof(int8_t); // Pointer Size (in bytes)
3084 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
3085 PrevOffset = Offset;
3086 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003087}
3088
Chris Lattner11b8f302010-04-04 23:02:02 +00003089/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
3090/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00003091static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00003092 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003093 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00003094 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003095
Chris Lattner9c69e285532010-04-04 22:59:04 +00003096 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
3097 Asm->OutStreamer.EmitLabel(TmpSym);
3098 return TmpSym;
3099}
3100
3101/// EmitSectionLabels - Emit initial Dwarf sections with a label at
3102/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00003103void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003104 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003105
Bill Wendling94d04b82009-05-20 23:21:38 +00003106 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003107 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003108 DwarfFrameSectionSym =
3109 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3110 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003111
Jim Grosbach1e20b962010-07-21 21:21:52 +00003112 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003113 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003114 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003115 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00003116 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003117
Chris Lattner9c69e285532010-04-04 22:59:04 +00003118 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00003119 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003120
Devang Patelaf608bd2010-08-24 00:06:12 +00003121 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00003122 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3123 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3124 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003125 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003126 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00003127 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3128 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00003129
Devang Patelc3f5f782010-05-25 23:40:22 +00003130 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3131 "section_debug_loc");
3132
Chris Lattner9c69e285532010-04-04 22:59:04 +00003133 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00003134 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003135}
3136
Devang Patel2c4ceb12009-11-21 02:48:08 +00003137/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00003138///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003139void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003140 // Get the abbreviation for this DIE.
3141 unsigned AbbrevNumber = Die->getAbbrevNumber();
3142 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3143
Bill Wendling94d04b82009-05-20 23:21:38 +00003144 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00003145 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00003146 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3147 Twine::utohexstr(Die->getOffset()) + ":0x" +
3148 Twine::utohexstr(Die->getSize()) + " " +
3149 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003150 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003151
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003152 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00003153 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3154
3155 // Emit the DIE attribute values.
3156 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3157 unsigned Attr = AbbrevData[i].getAttribute();
3158 unsigned Form = AbbrevData[i].getForm();
3159 assert(Form && "Too many attributes for DIE (check abbreviation)");
3160
Chris Lattner3f53c832010-04-04 18:52:31 +00003161 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00003162 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003163
Bill Wendling94d04b82009-05-20 23:21:38 +00003164 switch (Attr) {
3165 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00003166 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003167 break;
3168 case dwarf::DW_AT_abstract_origin: {
3169 DIEEntry *E = cast<DIEEntry>(Values[i]);
3170 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00003171 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00003172 Asm->EmitInt32(Addr);
3173 break;
3174 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003175 case dwarf::DW_AT_ranges: {
3176 // DW_AT_range Value encodes offset in debug_range section.
3177 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00003178
3179 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
3180 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
3181 V->getValue(),
3182 4);
3183 } else {
3184 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3185 V->getValue(),
3186 DwarfDebugRangeSectionSym,
3187 4);
3188 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003189 break;
3190 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003191 case dwarf::DW_AT_location: {
3192 if (UseDotDebugLocEntry.count(Die) != 0) {
3193 DIELabel *L = cast<DIELabel>(Values[i]);
3194 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3195 } else
3196 Values[i]->EmitValue(Asm, Form);
3197 break;
3198 }
Devang Patel2a361602010-09-29 19:08:08 +00003199 case dwarf::DW_AT_accessibility: {
3200 if (Asm->isVerbose()) {
3201 DIEInteger *V = cast<DIEInteger>(Values[i]);
3202 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
3203 }
3204 Values[i]->EmitValue(Asm, Form);
3205 break;
3206 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003207 default:
3208 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003209 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00003210 break;
3211 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003212 }
3213
3214 // Emit the DIE children if any.
3215 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3216 const std::vector<DIE *> &Children = Die->getChildren();
3217
3218 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003219 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00003220
Chris Lattner3f53c832010-04-04 18:52:31 +00003221 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00003222 Asm->OutStreamer.AddComment("End Of Children Mark");
3223 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003224 }
3225}
3226
Devang Patel8a241142009-12-09 18:24:21 +00003227/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003228///
Devang Patel8a241142009-12-09 18:24:21 +00003229void DwarfDebug::emitDebugInfo() {
3230 // Start debug info section.
3231 Asm->OutStreamer.SwitchSection(
3232 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00003233 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3234 E = CUMap.end(); I != E; ++I) {
3235 CompileUnit *TheCU = I->second;
3236 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00003237
Devang Patel163a9f72010-05-10 22:49:55 +00003238 // Emit the compile units header.
3239 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3240 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003241
Devang Patel163a9f72010-05-10 22:49:55 +00003242 // Emit size of content not including length itself
3243 unsigned ContentSize = Die->getSize() +
3244 sizeof(int16_t) + // DWARF version number
3245 sizeof(int32_t) + // Offset Into Abbrev. Section
3246 sizeof(int8_t) + // Pointer Size (in bytes)
3247 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003248
Devang Patel163a9f72010-05-10 22:49:55 +00003249 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3250 Asm->EmitInt32(ContentSize);
3251 Asm->OutStreamer.AddComment("DWARF version number");
3252 Asm->EmitInt16(dwarf::DWARF_VERSION);
3253 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3254 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3255 DwarfAbbrevSectionSym);
3256 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3257 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003258
Devang Patel163a9f72010-05-10 22:49:55 +00003259 emitDIE(Die);
3260 // FIXME - extra padding for gdb bug.
3261 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3262 Asm->EmitInt8(0);
3263 Asm->EmitInt8(0);
3264 Asm->EmitInt8(0);
3265 Asm->EmitInt8(0);
3266 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3267 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003268}
3269
Devang Patel2c4ceb12009-11-21 02:48:08 +00003270/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003271///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003272void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00003273 // Check to see if it is worth the effort.
3274 if (!Abbreviations.empty()) {
3275 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003276 Asm->OutStreamer.SwitchSection(
3277 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003278
Chris Lattnerc0215722010-04-04 19:25:43 +00003279 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003280
3281 // For each abbrevation.
3282 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3283 // Get abbreviation data
3284 const DIEAbbrev *Abbrev = Abbreviations[i];
3285
3286 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003287 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00003288
3289 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003290 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00003291 }
3292
3293 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003294 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00003295
Chris Lattnerc0215722010-04-04 19:25:43 +00003296 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003297 }
3298}
3299
Devang Patel2c4ceb12009-11-21 02:48:08 +00003300/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00003301/// the line matrix.
3302///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003303void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003304 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00003305 Asm->OutStreamer.AddComment("Extended Op");
3306 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003307
Chris Lattner233f52b2010-03-09 23:52:58 +00003308 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003309 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003310 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3311 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3312
3313 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003314
Chris Lattnerc0215722010-04-04 19:25:43 +00003315 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003316 Asm->getTargetData().getPointerSize(),
3317 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003318
3319 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00003320 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3321 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00003322 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00003323 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003324}
3325
Devang Patel2c4ceb12009-11-21 02:48:08 +00003326/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00003327///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003328void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003329 // If the target is using .loc/.file, the assembler will be emitting the
3330 // .debug_line table automatically.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003331 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00003332 return;
3333
3334 // Minimum line delta, thus ranging from -10..(255-10).
3335 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
3336 // Maximum line delta, thus ranging from -10..(255-10).
3337 const int MaxLineDelta = 255 + MinLineDelta;
3338
3339 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003340 Asm->OutStreamer.SwitchSection(
3341 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003342
3343 // Construct the section header.
Chris Lattner233f52b2010-03-09 23:52:58 +00003344 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnera6437182010-04-04 19:58:12 +00003345 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
3346 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00003347 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003348
Chris Lattner233f52b2010-03-09 23:52:58 +00003349 Asm->OutStreamer.AddComment("DWARF version number");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003350 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00003351
Chris Lattner233f52b2010-03-09 23:52:58 +00003352 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnera6437182010-04-04 19:58:12 +00003353 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
3354 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00003355 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003356
Chris Lattner233f52b2010-03-09 23:52:58 +00003357 Asm->OutStreamer.AddComment("Minimum Instruction Length");
3358 Asm->EmitInt8(1);
3359 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
3360 Asm->EmitInt8(1);
3361 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
3362 Asm->EmitInt8(MinLineDelta);
3363 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3364 Asm->EmitInt8(MaxLineDelta);
3365 Asm->OutStreamer.AddComment("Special Opcode Base");
3366 Asm->EmitInt8(-MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003367
3368 // Line number standard opcode encodings argument count
Chris Lattner233f52b2010-03-09 23:52:58 +00003369 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3370 Asm->EmitInt8(0);
3371 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3372 Asm->EmitInt8(1);
3373 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3374 Asm->EmitInt8(1);
3375 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3376 Asm->EmitInt8(1);
3377 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3378 Asm->EmitInt8(1);
3379 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3380 Asm->EmitInt8(0);
3381 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3382 Asm->EmitInt8(0);
3383 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3384 Asm->EmitInt8(0);
3385 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3386 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003387
3388 // Emit directories.
3389 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00003390 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner3f53c832010-04-04 18:52:31 +00003391 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003392 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003393 }
3394
Chris Lattner233f52b2010-03-09 23:52:58 +00003395 Asm->OutStreamer.AddComment("End of directories");
3396 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003397
3398 // Emit files.
3399 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3400 // Remember source id starts at 1.
3401 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003402 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner3f53c832010-04-04 18:52:31 +00003403 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003404 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003405
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003406 Asm->EmitULEB128(Id.first, "Directory #");
3407 Asm->EmitULEB128(0, "Mod date");
3408 Asm->EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00003409 }
3410
Chris Lattner233f52b2010-03-09 23:52:58 +00003411 Asm->OutStreamer.AddComment("End of files");
3412 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003413
Chris Lattnerc0215722010-04-04 19:25:43 +00003414 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003415
3416 // A sequence for each text section.
3417 unsigned SecSrcLinesSize = SectionSourceLines.size();
3418
3419 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3420 // Isolate current sections line info.
3421 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3422
Bill Wendling94d04b82009-05-20 23:21:38 +00003423 // Dwarf assumes we start with first line of first source file.
3424 unsigned Source = 1;
3425 unsigned Line = 1;
3426
3427 // Construct rows of the address, source, line, column matrix.
3428 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3429 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner25b68c62010-03-14 08:15:55 +00003430 MCSymbol *Label = LineInfo.getLabel();
Chris Lattnerb9130602010-03-14 02:20:58 +00003431 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling94d04b82009-05-20 23:21:38 +00003432
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003433 if (Asm->isVerbose()) {
Chris Lattner188a87d2010-03-10 01:04:13 +00003434 std::pair<unsigned, unsigned> SrcID =
Bill Wendling94d04b82009-05-20 23:21:38 +00003435 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner188a87d2010-03-10 01:04:13 +00003436 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner49f618a2010-03-10 02:29:31 +00003437 "/" +
3438 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner188a87d2010-03-10 01:04:13 +00003439 ":" + Twine(LineInfo.getLine()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003440 }
3441
3442 // Define the line address.
Chris Lattner233f52b2010-03-09 23:52:58 +00003443 Asm->OutStreamer.AddComment("Extended Op");
3444 Asm->EmitInt8(0);
3445 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003446 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003447
3448 Asm->OutStreamer.AddComment("DW_LNE_set_address");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003449 Asm->EmitInt8(dwarf::DW_LNE_set_address);
Chris Lattner233f52b2010-03-09 23:52:58 +00003450
3451 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003452 Asm->OutStreamer.EmitSymbolValue(Label,
3453 Asm->getTargetData().getPointerSize(),
Chris Lattnerb9130602010-03-14 02:20:58 +00003454 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003455
Bill Wendling94d04b82009-05-20 23:21:38 +00003456 // If change of source, then switch to the new source.
3457 if (Source != LineInfo.getSourceID()) {
3458 Source = LineInfo.getSourceID();
Chris Lattner233f52b2010-03-09 23:52:58 +00003459 Asm->OutStreamer.AddComment("DW_LNS_set_file");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003460 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003461 Asm->EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00003462 }
3463
3464 // If change of line.
3465 if (Line != LineInfo.getLine()) {
3466 // Determine offset.
3467 int Offset = LineInfo.getLine() - Line;
3468 int Delta = Offset - MinLineDelta;
3469
3470 // Update line.
3471 Line = LineInfo.getLine();
3472
3473 // If delta is small enough and in range...
3474 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3475 // ... then use fast opcode.
Chris Lattner233f52b2010-03-09 23:52:58 +00003476 Asm->OutStreamer.AddComment("Line Delta");
3477 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003478 } else {
3479 // ... otherwise use long hand.
Chris Lattner233f52b2010-03-09 23:52:58 +00003480 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling94d04b82009-05-20 23:21:38 +00003481 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003482 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner233f52b2010-03-09 23:52:58 +00003483 Asm->OutStreamer.AddComment("DW_LNS_copy");
3484 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003485 }
3486 } else {
3487 // Copy the previous row (different address or source)
Chris Lattner233f52b2010-03-09 23:52:58 +00003488 Asm->OutStreamer.AddComment("DW_LNS_copy");
3489 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003490 }
3491 }
3492
Devang Patel2c4ceb12009-11-21 02:48:08 +00003493 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003494 }
3495
3496 if (SecSrcLinesSize == 0)
3497 // Because we're emitting a debug_line section, we still need a line
3498 // table. The linker and friends expect it to exist. If there's nothing to
3499 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003500 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003501
Chris Lattnerc0215722010-04-04 19:25:43 +00003502 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003503}
3504
Devang Patel2c4ceb12009-11-21 02:48:08 +00003505/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003506///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003507void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003508 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003509 return;
3510
Chris Lattnerd38fee82010-04-05 00:13:49 +00003511 int stackGrowth = Asm->getTargetData().getPointerSize();
3512 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3513 TargetFrameInfo::StackGrowsDown)
3514 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003515
3516 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003517 Asm->OutStreamer.SwitchSection(
3518 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003519
Chris Lattnerc0215722010-04-04 19:25:43 +00003520 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003521 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003522 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3523 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003524
Chris Lattnerc0215722010-04-04 19:25:43 +00003525 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003526 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003527 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003528 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003529 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003530 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003531 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003532 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3533 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003534 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003535 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003536 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003537
3538 std::vector<MachineMove> Moves;
3539 RI->getInitialFrameState(Moves);
3540
Chris Lattner02b86b92010-04-04 23:41:46 +00003541 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003542
Chris Lattner059ea132010-04-28 01:05:45 +00003543 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003544 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003545}
3546
Devang Patel2c4ceb12009-11-21 02:48:08 +00003547/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003548/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003549void DwarfDebug::
3550emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003551 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003552 return;
3553
3554 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003555 Asm->OutStreamer.SwitchSection(
3556 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003557
Chris Lattner233f52b2010-03-09 23:52:58 +00003558 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003559 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003560 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003561 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003562 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003563 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003564
Chris Lattner206d61e2010-03-13 07:26:18 +00003565 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003566
Chris Lattner233f52b2010-03-09 23:52:58 +00003567 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003568 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner6189ed12010-04-04 23:25:33 +00003569 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003570
Chris Lattner233f52b2010-03-09 23:52:58 +00003571 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003572 MCSymbol *FuncBeginSym =
3573 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003574 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003575 Asm->getTargetData().getPointerSize(),
3576 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003577
3578
Chris Lattner233f52b2010-03-09 23:52:58 +00003579 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003580 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003581 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003582
Chris Lattner02b86b92010-04-04 23:41:46 +00003583 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003584
Chris Lattner059ea132010-04-28 01:05:45 +00003585 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003586 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003587}
3588
Devang Patel8a241142009-12-09 18:24:21 +00003589/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3590///
3591void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00003592 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3593 E = CUMap.end(); I != E; ++I) {
3594 CompileUnit *TheCU = I->second;
3595 // Start the dwarf pubnames section.
3596 Asm->OutStreamer.SwitchSection(
3597 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003598
Devang Patel163a9f72010-05-10 22:49:55 +00003599 Asm->OutStreamer.AddComment("Length of Public Names Info");
3600 Asm->EmitLabelDifference(
3601 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3602 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003603
Devang Patel163a9f72010-05-10 22:49:55 +00003604 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3605 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003606
Devang Patel163a9f72010-05-10 22:49:55 +00003607 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003608 Asm->EmitInt16(dwarf::DWARF_VERSION);
3609
Devang Patel163a9f72010-05-10 22:49:55 +00003610 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003611 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00003612 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003613
Devang Patel163a9f72010-05-10 22:49:55 +00003614 Asm->OutStreamer.AddComment("Compilation Unit Length");
3615 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3616 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3617 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003618
Devang Patel163a9f72010-05-10 22:49:55 +00003619 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3620 for (StringMap<DIE*>::const_iterator
3621 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3622 const char *Name = GI->getKeyData();
3623 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003624
Devang Patel163a9f72010-05-10 22:49:55 +00003625 Asm->OutStreamer.AddComment("DIE offset");
3626 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003627
Devang Patel163a9f72010-05-10 22:49:55 +00003628 if (Asm->isVerbose())
3629 Asm->OutStreamer.AddComment("External Name");
3630 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3631 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003632
Devang Patel163a9f72010-05-10 22:49:55 +00003633 Asm->OutStreamer.AddComment("End Mark");
3634 Asm->EmitInt32(0);
3635 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3636 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003637 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003638}
3639
Devang Patel193f7202009-11-24 01:14:22 +00003640void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00003641 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3642 E = CUMap.end(); I != E; ++I) {
3643 CompileUnit *TheCU = I->second;
3644 // Start the dwarf pubnames section.
3645 Asm->OutStreamer.SwitchSection(
3646 Asm->getObjFileLowering().getDwarfPubTypesSection());
3647 Asm->OutStreamer.AddComment("Length of Public Types Info");
3648 Asm->EmitLabelDifference(
3649 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3650 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003651
Devang Patel163a9f72010-05-10 22:49:55 +00003652 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3653 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003654
Devang Patel163a9f72010-05-10 22:49:55 +00003655 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3656 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003657
Devang Patel163a9f72010-05-10 22:49:55 +00003658 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3659 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3660 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003661
Devang Patel163a9f72010-05-10 22:49:55 +00003662 Asm->OutStreamer.AddComment("Compilation Unit Length");
3663 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3664 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3665 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003666
Devang Patel163a9f72010-05-10 22:49:55 +00003667 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3668 for (StringMap<DIE*>::const_iterator
3669 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3670 const char *Name = GI->getKeyData();
3671 DIE * Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003672
Devang Patel163a9f72010-05-10 22:49:55 +00003673 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3674 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003675
Devang Patel163a9f72010-05-10 22:49:55 +00003676 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3677 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3678 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003679
Devang Patel163a9f72010-05-10 22:49:55 +00003680 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003681 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00003682 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3683 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003684 }
Devang Patel193f7202009-11-24 01:14:22 +00003685}
3686
Devang Patel2c4ceb12009-11-21 02:48:08 +00003687/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003688///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003689void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003690 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003691 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003692
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003693 // Start the dwarf str section.
3694 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003695 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003696
Chris Lattnerbc733f52010-03-13 02:17:42 +00003697 // Get all of the string pool entries and put them in an array by their ID so
3698 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003699 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00003700 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003701
Chris Lattnerbc733f52010-03-13 02:17:42 +00003702 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3703 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3704 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003705
Chris Lattnerbc733f52010-03-13 02:17:42 +00003706 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003707
Chris Lattnerbc733f52010-03-13 02:17:42 +00003708 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003709 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003710 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003711
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003712 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003713 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003714 }
3715}
3716
Devang Patel2c4ceb12009-11-21 02:48:08 +00003717/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003718///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003719void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00003720 if (DotDebugLocEntries.empty())
3721 return;
3722
Bill Wendling94d04b82009-05-20 23:21:38 +00003723 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003724 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00003725 Asm->getObjFileLowering().getDwarfLocSection());
3726 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00003727 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3728 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003729 for (SmallVector<DotDebugLocEntry, 4>::iterator
3730 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00003731 I != E; ++I, ++index) {
Devang Patelc3f5f782010-05-25 23:40:22 +00003732 DotDebugLocEntry Entry = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00003733 if (Entry.isEmpty()) {
3734 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3735 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00003736 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00003737 } else {
3738 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3739 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3740 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3741 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patelb2cf5812010-08-04 18:40:52 +00003742 if (int Offset = Entry.Loc.getOffset()) {
3743 // If the value is at a certain offset from frame register then
3744 // use DW_OP_fbreg.
3745 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patelc3f5f782010-05-25 23:40:22 +00003746 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patelb2cf5812010-08-04 18:40:52 +00003747 Asm->EmitInt16(1 + OffsetSize);
3748 Asm->OutStreamer.AddComment(
3749 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
3750 Asm->EmitInt8(dwarf::DW_OP_fbreg);
3751 Asm->OutStreamer.AddComment("Offset");
3752 Asm->EmitSLEB128(Offset);
Devang Patelc3f5f782010-05-25 23:40:22 +00003753 } else {
Devang Patelb2cf5812010-08-04 18:40:52 +00003754 if (Reg < 32) {
3755 Asm->OutStreamer.AddComment("Loc expr size");
3756 Asm->EmitInt16(1);
3757 Asm->OutStreamer.AddComment(
Devang Patela54e0cc2010-08-04 20:32:36 +00003758 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patelb2cf5812010-08-04 18:40:52 +00003759 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3760 } else {
3761 Asm->OutStreamer.AddComment("Loc expr size");
3762 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
3763 Asm->EmitInt8(dwarf::DW_OP_regx);
3764 Asm->EmitULEB128(Reg);
3765 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003766 }
3767 }
3768 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003769}
3770
3771/// EmitDebugARanges - Emit visible names into a debug aranges section.
3772///
3773void DwarfDebug::EmitDebugARanges() {
3774 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003775 Asm->OutStreamer.SwitchSection(
3776 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003777}
3778
Devang Patel2c4ceb12009-11-21 02:48:08 +00003779/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003780///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003781void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003782 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003783 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003784 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003785 unsigned char Size = Asm->getTargetData().getPointerSize();
3786 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00003787 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00003788 I != E; ++I) {
3789 if (*I)
3790 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003791 else
Devang Pateleac9c072010-04-27 19:46:33 +00003792 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003793 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003794}
3795
Devang Patel2c4ceb12009-11-21 02:48:08 +00003796/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003797///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003798void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003799 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003800 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003801 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003802 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003803 }
3804}
3805
Devang Patel2c4ceb12009-11-21 02:48:08 +00003806/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003807/// Section Header:
3808/// 1. length of section
3809/// 2. Dwarf version number
3810/// 3. address size.
3811///
3812/// Entries (one "entry" for each function that was inlined):
3813///
3814/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3815/// otherwise offset into __debug_str for regular function name.
3816/// 2. offset into __debug_str section for regular function name.
3817/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3818/// instances for the function.
3819///
3820/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3821/// inlined instance; the die_offset points to the inlined_subroutine die in the
3822/// __debug_info section, and the low_pc is the starting address for the
3823/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003824void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003825 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003826 return;
3827
Devang Patel163a9f72010-05-10 22:49:55 +00003828 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003829 return;
3830
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003831 Asm->OutStreamer.SwitchSection(
3832 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003833
Chris Lattner233f52b2010-03-09 23:52:58 +00003834 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003835 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3836 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003837
Chris Lattnerc0215722010-04-04 19:25:43 +00003838 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003839
Chris Lattner233f52b2010-03-09 23:52:58 +00003840 Asm->OutStreamer.AddComment("Dwarf Version");
3841 Asm->EmitInt16(dwarf::DWARF_VERSION);
3842 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003843 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003844
Devang Patele9f8f5e2010-05-07 20:54:48 +00003845 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003846 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003847
Devang Patele9f8f5e2010-05-07 20:54:48 +00003848 const MDNode *Node = *I;
3849 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003850 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003851 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003852 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003853 StringRef LName = SP.getLinkageName();
3854 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003855
Chris Lattner233f52b2010-03-09 23:52:58 +00003856 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003857 if (LName.empty()) {
3858 Asm->OutStreamer.EmitBytes(Name, 0);
3859 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003860 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003861 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3862 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003863
Chris Lattner233f52b2010-03-09 23:52:58 +00003864 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003865 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003866 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003867
Devang Patel53bb5c92009-11-10 23:06:00 +00003868 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003869 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003870 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003871 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003872
Chris Lattner3f53c832010-04-04 18:52:31 +00003873 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003874 Asm->OutStreamer.EmitSymbolValue(LI->first,
3875 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003876 }
3877 }
3878
Chris Lattnerc0215722010-04-04 19:25:43 +00003879 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003880}