blob: fdd47f2c348fa90e0148e1dcb1ec0918fdd46063 [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"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000027#include "llvm/Target/TargetFrameLowering.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"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000042#include "llvm/Support/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;
Rafael Espindola5c055632010-11-18 02:04:25 +0000517 unsigned FileID = GetOrCreateSourceID(V.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000518 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000519 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
520 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000521}
522
Devang Patel2c4ceb12009-11-21 02:48:08 +0000523/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000524/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000525void DwarfDebug::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patelc665a512010-05-07 23:33:41 +0000526 // Verify global variable.
Devang Patel81625742010-08-09 20:20:05 +0000527 if (!G.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000528 return;
529
Devang Patel81625742010-08-09 20:20:05 +0000530 unsigned Line = G.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000531 if (Line == 0)
532 return;
Rafael Espindola5c055632010-11-18 02:04:25 +0000533 unsigned FileID = GetOrCreateSourceID(G.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000534 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000535 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
536 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000537}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000538
Devang Patel2c4ceb12009-11-21 02:48:08 +0000539/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000540/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000541void DwarfDebug::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patelc665a512010-05-07 23:33:41 +0000542 // Verify subprogram.
Devang Patel81625742010-08-09 20:20:05 +0000543 if (!SP.Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000544 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000545 // If the line number is 0, don't add it.
Devang Patel81625742010-08-09 20:20:05 +0000546 if (SP.getLineNumber() == 0)
Caroline Ticec6f9d622009-09-11 18:25:54 +0000547 return;
548
Devang Patel81625742010-08-09 20:20:05 +0000549 unsigned Line = SP.getLineNumber();
550 if (!SP.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000551 return;
Rafael Espindola5c055632010-11-18 02:04:25 +0000552 unsigned FileID = GetOrCreateSourceID(SP.getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000553 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000554 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
555 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000556}
557
Devang Patel2c4ceb12009-11-21 02:48:08 +0000558/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000559/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000560void DwarfDebug::addSourceLine(DIE *Die, DIType Ty) {
Devang Patelc665a512010-05-07 23:33:41 +0000561 // Verify type.
Devang Patel81625742010-08-09 20:20:05 +0000562 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000563 return;
564
Devang Patel81625742010-08-09 20:20:05 +0000565 unsigned Line = Ty.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000566 if (Line == 0 || !Ty.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000567 return;
Rafael Espindola5c055632010-11-18 02:04:25 +0000568 unsigned FileID = GetOrCreateSourceID(Ty.getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000569 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000570 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
571 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000572}
573
Devang Patel6404e4e2009-12-15 19:16:48 +0000574/// addSourceLine - Add location information to specified debug information
575/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000576void DwarfDebug::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patelc665a512010-05-07 23:33:41 +0000577 // Verify namespace.
Devang Patel81625742010-08-09 20:20:05 +0000578 if (!NS.Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000579 return;
580
Devang Patel81625742010-08-09 20:20:05 +0000581 unsigned Line = NS.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000582 if (Line == 0)
583 return;
Devang Patel81625742010-08-09 20:20:05 +0000584 StringRef FN = NS.getFilename();
Devang Patel6404e4e2009-12-15 19:16:48 +0000585
Rafael Espindola5c055632010-11-18 02:04:25 +0000586 unsigned FileID = GetOrCreateSourceID(FN);
Devang Patel6404e4e2009-12-15 19:16:48 +0000587 assert(FileID && "Invalid file id");
588 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
589 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
590}
591
Devang Patel9e3bd2c2010-08-31 06:11:28 +0000592/// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
593/// on provided frame index.
594void DwarfDebug::addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI) {
595 MachineLocation Location;
596 unsigned FrameReg;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000597 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000598 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel9e3bd2c2010-08-31 06:11:28 +0000599 Location.set(FrameReg, Offset);
600
Devang Patel8bd11de2010-08-09 21:01:39 +0000601 if (DV->variableHasComplexAddress())
602 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
603 else if (DV->isBlockByrefVariable())
604 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
605 else
606 addAddress(Die, dwarf::DW_AT_location, Location);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000607}
608
Devang Patel2c4ceb12009-11-21 02:48:08 +0000609/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000610/// and generate the DWARF information necessary to find the actual variable
611/// given the extra address information encoded in the DIVariable, starting from
612/// the starting location. Add the DWARF information to the die.
613///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000614void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000615 unsigned Attribute,
616 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000617 DIType Ty = DV->getType();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000618
619 // Decode the original location, and use that as the start of the byref
620 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000621 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000622 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000623 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000624
625 if (Location.isReg()) {
626 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000627 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000628 } else {
Devang Pateldacde942011-01-19 23:04:47 +0000629 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000630 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000631 }
632 } else {
633 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000634 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000635 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000636 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
637 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000638 }
639
Devang Patel2c4ceb12009-11-21 02:48:08 +0000640 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000641 }
642
Devang Patel8bd11de2010-08-09 21:01:39 +0000643 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
644 uint64_t Element = DV->getAddrElement(i);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000645
646 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000647 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel8bd11de2010-08-09 21:01:39 +0000648 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000649 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000650 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000651 } else llvm_unreachable("unknown DIFactory Opcode");
652 }
653
654 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000655 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000656}
657
Caroline Ticedc8f6042009-08-31 21:19:37 +0000658/* Byref variables, in Blocks, are declared by the programmer as "SomeType
659 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
660 gives the variable VarName either the struct, or a pointer to the struct, as
661 its type. This is necessary for various behind-the-scenes things the
662 compiler needs to do with by-reference variables in Blocks.
663
664 However, as far as the original *programmer* is concerned, the variable
665 should still have type 'SomeType', as originally declared.
666
Devang Patel2c4ceb12009-11-21 02:48:08 +0000667 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000668 struct to find the original type of the variable, which is then assigned to
669 the variable's Debug Information Entry as its real type. So far, so good.
670 However now the debugger will expect the variable VarName to have the type
671 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000672 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000673 pointers and struct to find the actual variable of type SomeType.
674
675 The following function does just that. We start by getting
676 the "normal" location for the variable. This will be the location
677 of either the struct __Block_byref_x_VarName or the pointer to the
678 struct __Block_byref_x_VarName.
679
680 The struct will look something like:
681
682 struct __Block_byref_x_VarName {
683 ... <various fields>
684 struct __Block_byref_x_VarName *forwarding;
685 ... <various other fields>
686 SomeType VarName;
687 ... <maybe more fields>
688 };
689
690 If we are given the struct directly (as our starting point) we
691 need to tell the debugger to:
692
693 1). Add the offset of the forwarding field.
694
Dan Gohmanf451cb82010-02-10 16:03:48 +0000695 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000696 struct to use (the real one may have been copied onto the heap).
697
698 3). Add the offset for the field VarName, to find the actual variable.
699
700 If we started with a pointer to the struct, then we need to
701 dereference that pointer first, before the other steps.
702 Translating this into DWARF ops, we will need to append the following
703 to the current location description for the variable:
704
705 DW_OP_deref -- optional, if we start with a pointer
706 DW_OP_plus_uconst <forward_fld_offset>
707 DW_OP_deref
708 DW_OP_plus_uconst <varName_fld_offset>
709
710 That is what this function does. */
711
Devang Patel2c4ceb12009-11-21 02:48:08 +0000712/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000713/// provided, and generate the DWARF information necessary to find the
714/// actual Block variable (navigating the Block struct) based on the
715/// starting location. Add the DWARF information to the die. For
716/// more information, read large comment just above here.
717///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000718void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000719 unsigned Attribute,
720 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000721 DIType Ty = DV->getType();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000722 DIType TmpTy = Ty;
723 unsigned Tag = Ty.getTag();
724 bool isPointer = false;
725
Devang Patel8bd11de2010-08-09 21:01:39 +0000726 StringRef varName = DV->getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000727
728 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000729 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000730 TmpTy = DTy.getTypeDerivedFrom();
731 isPointer = true;
732 }
733
Devang Patel2db49d72010-05-07 18:11:54 +0000734 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000735
Daniel Dunbar00564992009-09-19 20:40:14 +0000736 // Find the __forwarding field and the variable field in the __Block_byref
737 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000738 DIArray Fields = blockStruct.getTypeArray();
739 DIDescriptor varField = DIDescriptor();
740 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000741
Daniel Dunbar00564992009-09-19 20:40:14 +0000742 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
743 DIDescriptor Element = Fields.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000744 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000745 StringRef fieldName = DT.getName();
746 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000747 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000748 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000749 varField = Element;
750 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000751
Daniel Dunbar00564992009-09-19 20:40:14 +0000752 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000753 unsigned forwardingFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000754 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000755 unsigned varFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000756 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000757
Mike Stump7e3720d2009-09-24 23:21:26 +0000758 // Decode the original location, and use that as the start of the byref
759 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000760 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000761 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000762 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000763
Daniel Dunbar00564992009-09-19 20:40:14 +0000764 if (Location.isReg()) {
765 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000766 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000767 else {
Devang Pateldacde942011-01-19 23:04:47 +0000768 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000769 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000770 }
771 } else {
772 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000773 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000774 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000775 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
776 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000777 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000778
Devang Patel2c4ceb12009-11-21 02:48:08 +0000779 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000780 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000781
Mike Stump7e3720d2009-09-24 23:21:26 +0000782 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000783 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000784 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000785 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000786
Daniel Dunbar00564992009-09-19 20:40:14 +0000787 // Next add the offset for the '__forwarding' field:
788 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
789 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000790 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000791 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
792 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000793 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000794
Daniel Dunbar00564992009-09-19 20:40:14 +0000795 // Now dereference the __forwarding field to get to the real __Block_byref
796 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000797 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000798
Daniel Dunbar00564992009-09-19 20:40:14 +0000799 // Now that we've got the real __Block_byref... struct, add the offset
800 // for the variable's field to get to the location of the actual variable:
801 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000802 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000803 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
804 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000805 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000806
Daniel Dunbar00564992009-09-19 20:40:14 +0000807 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000808 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000809}
810
Devang Patel2c4ceb12009-11-21 02:48:08 +0000811/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000812/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000813void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000814 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000815 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000816 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000817 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelbe90c3a2010-09-22 21:10:38 +0000818
Devang Patelc8821042010-11-02 17:37:00 +0000819 if (RI->getFrameRegister(*Asm->MF) == Location.getReg()
Devang Patelbe90c3a2010-09-22 21:10:38 +0000820 && Location.getOffset()) {
821 // If variable offset is based in frame register then use fbreg.
822 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
823 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
824 addBlock(Die, Attribute, 0, Block);
825 return;
826 }
Bill Wendling0310d762009-05-15 09:23:25 +0000827
828 if (Location.isReg()) {
829 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000830 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000831 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000832 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
833 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000834 }
835 } else {
836 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000837 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + 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_bregx);
840 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000841 }
842
Devang Patel2c4ceb12009-11-21 02:48:08 +0000843 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000844 }
845
Devang Patel2c4ceb12009-11-21 02:48:08 +0000846 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000847}
848
Devang Patela43098d2010-04-28 01:03:09 +0000849/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patel522ad742010-11-12 23:20:42 +0000850bool DwarfDebug::addRegisterAddress(DIE *Die, const MachineOperand &MO) {
Devang Patela43098d2010-04-28 01:03:09 +0000851 assert (MO.isReg() && "Invalid machine operand!");
852 if (!MO.getReg())
853 return false;
854 MachineLocation Location;
855 Location.set(MO.getReg());
856 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patela43098d2010-04-28 01:03:09 +0000857 return true;
858}
859
860/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel522ad742010-11-12 23:20:42 +0000861bool DwarfDebug::addConstantValue(DIE *Die, const MachineOperand &MO) {
Devang Patela43098d2010-04-28 01:03:09 +0000862 assert (MO.isImm() && "Invalid machine operand!");
863 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
864 unsigned Imm = MO.getImm();
865 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
866 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patela43098d2010-04-28 01:03:09 +0000867 return true;
868}
869
870/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patel522ad742010-11-12 23:20:42 +0000871bool DwarfDebug::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Devang Patela43098d2010-04-28 01:03:09 +0000872 assert (MO.isFPImm() && "Invalid machine operand!");
873 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
874 APFloat FPImm = MO.getFPImm()->getValueAPF();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000875
Devang Patela43098d2010-04-28 01:03:09 +0000876 // Get the raw data form of the floating point.
877 const APInt FltVal = FPImm.bitcastToAPInt();
878 const char *FltPtr = (const char*)FltVal.getRawData();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000879
Devang Patela43098d2010-04-28 01:03:09 +0000880 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
881 bool LittleEndian = Asm->getTargetData().isLittleEndian();
882 int Incr = (LittleEndian ? 1 : -1);
883 int Start = (LittleEndian ? 0 : NumBytes - 1);
884 int Stop = (LittleEndian ? NumBytes : -1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000885
Devang Patela43098d2010-04-28 01:03:09 +0000886 // Output the constant to DWARF one byte at a time.
887 for (; Start != Stop; Start += Incr)
888 addUInt(Block, 0, dwarf::DW_FORM_data1,
889 (unsigned char)0xFF & FltPtr[Start]);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000890
Devang Patela43098d2010-04-28 01:03:09 +0000891 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000892 return true;
Devang Patela43098d2010-04-28 01:03:09 +0000893}
894
Devang Patel76a788c2011-01-06 21:39:25 +0000895/// addConstantValue - Add constant value entry in variable DIE.
896bool DwarfDebug::addConstantValue(DIE *Die, ConstantInt *CI,
897 bool Unsigned) {
898 if (CI->getBitWidth() <= 64) {
899 if (Unsigned)
900 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
901 CI->getZExtValue());
902 else
903 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
904 CI->getSExtValue());
905 return true;
906 }
907
908 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
909
910 // Get the raw data form of the large APInt.
911 const APInt Val = CI->getValue();
912 const char *Ptr = (const char*)Val.getRawData();
913
914 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
915 bool LittleEndian = Asm->getTargetData().isLittleEndian();
916 int Incr = (LittleEndian ? 1 : -1);
917 int Start = (LittleEndian ? 0 : NumBytes - 1);
918 int Stop = (LittleEndian ? NumBytes : -1);
919
920 // Output the constant to DWARF one byte at a time.
921 for (; Start != Stop; Start += Incr)
922 addUInt(Block, 0, dwarf::DW_FORM_data1,
923 (unsigned char)0xFF & Ptr[Start]);
924
925 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
926 return true;
927}
Devang Patela43098d2010-04-28 01:03:09 +0000928
Devang Patelc366f832009-12-10 19:14:49 +0000929/// addToContextOwner - Add Die into the list of its context owner's children.
930void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000931 if (Context.isType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000932 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patelc366f832009-12-10 19:14:49 +0000933 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000934 } else if (Context.isNameSpace()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000935 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel6404e4e2009-12-15 19:16:48 +0000936 ContextDIE->addChild(Die);
Stuart Hastings215aa152010-06-11 20:08:44 +0000937 } else if (Context.isSubprogram()) {
Devang Patelee70fa72010-09-27 23:15:27 +0000938 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context));
Stuart Hastings215aa152010-06-11 20:08:44 +0000939 ContextDIE->addChild(Die);
Devang Patel163a9f72010-05-10 22:49:55 +0000940 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patelc366f832009-12-10 19:14:49 +0000941 ContextDIE->addChild(Die);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000942 else
Devang Patel163a9f72010-05-10 22:49:55 +0000943 getCompileUnit(Context)->addDie(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000944}
945
Devang Patel16ced732009-12-10 18:05:33 +0000946/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
947/// given DIType.
948DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel163a9f72010-05-10 22:49:55 +0000949 CompileUnit *TypeCU = getCompileUnit(Ty);
950 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel16ced732009-12-10 18:05:33 +0000951 if (TyDIE)
952 return TyDIE;
953
954 // Create new type.
955 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel163a9f72010-05-10 22:49:55 +0000956 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel16ced732009-12-10 18:05:33 +0000957 if (Ty.isBasicType())
Devang Patel2db49d72010-05-07 18:11:54 +0000958 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000959 else if (Ty.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000960 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000961 else {
962 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2db49d72010-05-07 18:11:54 +0000963 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000964 }
965
Devang Patelc366f832009-12-10 19:14:49 +0000966 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000967 return TyDIE;
968}
969
Devang Patel2c4ceb12009-11-21 02:48:08 +0000970/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000971void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9c004872010-05-07 21:45:47 +0000972 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000973 return;
974
975 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000976 CompileUnit *TypeCU = getCompileUnit(Ty);
977 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000978 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000979 if (Entry) {
980 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000981 return;
982 }
983
Bill Wendling0310d762009-05-15 09:23:25 +0000984 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000985 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000986
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000987 // Set up proxy.
988 Entry = createDIEEntry(Buffer);
Devang Patel163a9f72010-05-10 22:49:55 +0000989 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000990
Devang Patel2c4ceb12009-11-21 02:48:08 +0000991 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000992}
993
Devang Patel2c4ceb12009-11-21 02:48:08 +0000994/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000995void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000996 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000997 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000998 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000999 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +00001000 BTy.getEncoding());
1001
1002 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001003 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001004 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001005 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001006 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001007}
1008
Devang Patel2c4ceb12009-11-21 02:48:08 +00001009/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +00001010void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001011 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +00001012 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001013 uint64_t Size = DTy.getSizeInBits() >> 3;
1014 unsigned Tag = DTy.getTag();
1015
1016 // FIXME - Workaround for templates.
1017 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
1018
1019 Buffer.setTag(Tag);
1020
1021 // Map to main type, void will not have a type.
1022 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +00001023 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001024
1025 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +00001026 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001027 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001028
1029 // Add size if non-zero (derived types might be zero-sized.)
1030 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001031 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001032
1033 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +00001034 if (!DTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001035 addSourceLine(&Buffer, DTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001036}
1037
Devang Patel2c4ceb12009-11-21 02:48:08 +00001038/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001039void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001040 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +00001041 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001042
1043 uint64_t Size = CTy.getSizeInBits() >> 3;
1044 unsigned Tag = CTy.getTag();
1045 Buffer.setTag(Tag);
1046
1047 switch (Tag) {
1048 case dwarf::DW_TAG_vector_type:
1049 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +00001050 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001051 break;
1052 case dwarf::DW_TAG_enumeration_type: {
1053 DIArray Elements = CTy.getTypeArray();
1054
1055 // Add enumerators to enumeration type.
1056 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1057 DIE *ElemDie = NULL;
Devang Patel2db49d72010-05-07 18:11:54 +00001058 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3c91b052010-03-08 20:52:55 +00001059 if (Enum.isEnumerator()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001060 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001061 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +00001062 }
Bill Wendling0310d762009-05-15 09:23:25 +00001063 }
1064 }
1065 break;
1066 case dwarf::DW_TAG_subroutine_type: {
1067 // Add return type.
1068 DIArray Elements = CTy.getTypeArray();
1069 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2db49d72010-05-07 18:11:54 +00001070 addType(&Buffer, DIType(RTy));
Bill Wendling0310d762009-05-15 09:23:25 +00001071
Devang Pateld6747df2010-10-06 20:50:40 +00001072 bool isPrototyped = true;
Bill Wendling0310d762009-05-15 09:23:25 +00001073 // Add arguments.
1074 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001075 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld6747df2010-10-06 20:50:40 +00001076 if (Ty.isUnspecifiedParameter()) {
1077 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
1078 Buffer.addChild(Arg);
1079 isPrototyped = false;
1080 } else {
1081 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1082 addType(Arg, DIType(Ty));
1083 Buffer.addChild(Arg);
1084 }
Bill Wendling0310d762009-05-15 09:23:25 +00001085 }
Devang Pateld6747df2010-10-06 20:50:40 +00001086 // Add prototype flag.
1087 if (isPrototyped)
1088 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001089 }
1090 break;
1091 case dwarf::DW_TAG_structure_type:
1092 case dwarf::DW_TAG_union_type:
1093 case dwarf::DW_TAG_class_type: {
1094 // Add elements to structure type.
1095 DIArray Elements = CTy.getTypeArray();
1096
1097 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +00001098 unsigned N = Elements.getNumElements();
1099 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +00001100 break;
1101
1102 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +00001103 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001104 DIDescriptor Element = Elements.getElement(i);
1105 DIE *ElemDie = NULL;
Devang Patel1a301232010-09-29 21:44:16 +00001106 if (Element.isSubprogram()) {
1107 DISubprogram SP(Element);
Devang Patel2db49d72010-05-07 18:11:54 +00001108 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel1a301232010-09-29 21:44:16 +00001109 if (SP.isProtected())
1110 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1111 dwarf::DW_ACCESS_protected);
1112 else if (SP.isPrivate())
1113 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1114 dwarf::DW_ACCESS_private);
1115 else
1116 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1117 dwarf::DW_ACCESS_public);
Devang Patel21ea1d52010-10-01 23:31:40 +00001118 if (SP.isExplicit())
1119 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
Devang Patel1a301232010-09-29 21:44:16 +00001120 }
Devang Patel3c91b052010-03-08 20:52:55 +00001121 else if (Element.isVariable()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001122 DIVariable DV(Element);
Devang Patel1ee0cb92010-01-30 01:08:30 +00001123 ElemDie = new DIE(dwarf::DW_TAG_variable);
1124 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1125 DV.getName());
1126 addType(ElemDie, DV.getType());
1127 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1128 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel81625742010-08-09 20:20:05 +00001129 addSourceLine(ElemDie, DV);
Devang Patel3c91b052010-03-08 20:52:55 +00001130 } else if (Element.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001131 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001132 else
1133 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001134 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001135 }
1136
Devang Patela1ba2692009-08-27 23:51:51 +00001137 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001138 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001139
1140 unsigned RLang = CTy.getRunTimeLang();
1141 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001142 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001143 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001144
1145 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel2db49d72010-05-07 18:11:54 +00001146 if (DIDescriptor(ContainingType).isCompositeType())
Jim Grosbach1e20b962010-07-21 21:21:52 +00001147 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel2db49d72010-05-07 18:11:54 +00001148 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastings215aa152010-06-11 20:08:44 +00001149 else {
1150 DIDescriptor Context = CTy.getContext();
1151 addToContextOwner(&Buffer, Context);
1152 }
Bill Wendling0310d762009-05-15 09:23:25 +00001153 break;
1154 }
1155 default:
1156 break;
1157 }
1158
1159 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001160 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001161 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001162
Jim Grosbach1e20b962010-07-21 21:21:52 +00001163 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
Devang Patel61409622010-07-07 20:12:52 +00001164 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1165 {
Bill Wendling0310d762009-05-15 09:23:25 +00001166 // Add size if non-zero (derived types might be zero-sized.)
1167 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001168 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001169 else {
1170 // Add zero size if it is not a forward declaration.
1171 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001172 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001173 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001174 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001175 }
1176
1177 // Add source line info if available.
1178 if (!CTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001179 addSourceLine(&Buffer, CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001180 }
1181}
1182
Devang Patel2c4ceb12009-11-21 02:48:08 +00001183/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1184void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001185 int64_t L = SR.getLo();
1186 int64_t H = SR.getHi();
1187 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1188
Devang Patel2c4ceb12009-11-21 02:48:08 +00001189 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001190 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001191 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001192 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001193
Devang Patel2c4ceb12009-11-21 02:48:08 +00001194 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001195}
1196
Devang Patel2c4ceb12009-11-21 02:48:08 +00001197/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001198void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001199 DICompositeType *CTy) {
1200 Buffer.setTag(dwarf::DW_TAG_array_type);
1201 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001202 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001203
1204 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001205 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001206 DIArray Elements = CTy->getTypeArray();
1207
Devang Patel6f01d9c2009-11-21 00:31:03 +00001208 // Get an anonymous type for index type.
Devang Patel163a9f72010-05-10 22:49:55 +00001209 CompileUnit *TheCU = getCompileUnit(*CTy);
1210 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001211 if (!IdxTy) {
1212 // Construct an anonymous type for index type.
1213 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001214 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1215 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001216 dwarf::DW_ATE_signed);
Devang Patel163a9f72010-05-10 22:49:55 +00001217 TheCU->addDie(IdxTy);
1218 TheCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001219 }
Bill Wendling0310d762009-05-15 09:23:25 +00001220
1221 // Add subranges to array type.
1222 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1223 DIDescriptor Element = Elements.getElement(i);
1224 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2db49d72010-05-07 18:11:54 +00001225 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001226 }
1227}
1228
Devang Patel2c4ceb12009-11-21 02:48:08 +00001229/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001230DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001231 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001232 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001233 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001234 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001235 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001236 return Enumerator;
1237}
1238
Jim Grosbach1e20b962010-07-21 21:21:52 +00001239/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +00001240/// printer to not emit usual symbol prefix before the symbol name is used then
1241/// return linkage name after skipping this special LLVM prefix.
1242static StringRef getRealLinkageName(StringRef LinkageName) {
1243 char One = '\1';
1244 if (LinkageName.startswith(StringRef(&One, 1)))
1245 return LinkageName.substr(1);
1246 return LinkageName;
1247}
1248
Devang Patel2c4ceb12009-11-21 02:48:08 +00001249/// createMemberDIE - Create new member DIE.
Devang Patelecbd8e82010-08-10 04:12:17 +00001250DIE *DwarfDebug::createMemberDIE(DIDerivedType DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001251 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001252 StringRef Name = DT.getName();
1253 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001254 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001255
Devang Patel8a241142009-12-09 18:24:21 +00001256 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001257
Devang Patel81625742010-08-09 20:20:05 +00001258 addSourceLine(MemberDie, DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001259
Benjamin Kramer345ef342010-03-31 19:34:01 +00001260 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001261 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001262
Bill Wendling0310d762009-05-15 09:23:25 +00001263 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001264 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001265
1266 if (Size != FieldSize) {
1267 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001268 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1269 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001270
1271 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001272 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1273 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001274 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001275 Offset -= FieldOffset;
1276
1277 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001278 if (Asm->getTargetData().isLittleEndian())
1279 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001280 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001281
Devang Patel33db5082009-11-04 22:06:12 +00001282 // Here WD_AT_data_member_location points to the anonymous
1283 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001284 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001285
1286 } else
1287 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001288 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001289
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001290 if (DT.getTag() == dwarf::DW_TAG_inheritance
1291 && DT.isVirtual()) {
1292
1293 // For C++, virtual base classes are not at fixed offset. Use following
1294 // expression to extract appropriate offset from vtable.
1295 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1296
Benjamin Kramer345ef342010-03-31 19:34:01 +00001297 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001298 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1299 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1300 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1301 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1302 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1303 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1304 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1305
Jim Grosbach1e20b962010-07-21 21:21:52 +00001306 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001307 VBaseLocationDie);
1308 } else
1309 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001310
1311 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001312 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001313 dwarf::DW_ACCESS_protected);
1314 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001315 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001316 dwarf::DW_ACCESS_private);
Devang Patel2a361602010-09-29 19:08:08 +00001317 // Otherwise C++ member and base classes are considered public.
1318 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
Devang Patel5d11eb02009-12-03 19:11:07 +00001319 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1320 dwarf::DW_ACCESS_public);
1321 if (DT.isVirtual())
1322 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1323 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001324 return MemberDie;
1325}
1326
Devang Patelffe966c2009-12-14 16:18:45 +00001327/// createSubprogramDIE - Create new DIE using SP.
Devang Patelee70fa72010-09-27 23:15:27 +00001328DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel163a9f72010-05-10 22:49:55 +00001329 CompileUnit *SPCU = getCompileUnit(SP);
1330 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +00001331 if (SPDie)
1332 return SPDie;
1333
1334 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001335 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001336 if (!SP.getName().empty())
1337 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001338
Devang Patel65dbc902009-11-25 17:36:49 +00001339 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001340 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001341 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001342 getRealLinkageName(LinkageName));
1343
Devang Patel81625742010-08-09 20:20:05 +00001344 addSourceLine(SPDie, SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001345
Devang Patel7b172c62010-10-07 22:03:01 +00001346 if (SP.isPrototyped())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001347 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001348
1349 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001350 DICompositeType SPTy = SP.getType();
1351 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001352 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001353
Devang Patel3c91b052010-03-08 20:52:55 +00001354 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001355 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001356 else
Devang Patel2db49d72010-05-07 18:11:54 +00001357 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001358
Devang Patel5d11eb02009-12-03 19:11:07 +00001359 unsigned VK = SP.getVirtuality();
1360 if (VK) {
1361 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001362 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001363 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Devang Pateld639c7c2010-12-09 00:10:40 +00001364 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
Devang Patel5d11eb02009-12-03 19:11:07 +00001365 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001366 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +00001367 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001368 }
1369
Devang Patelee70fa72010-09-27 23:15:27 +00001370 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001371 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001372
1373 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001374 // be handled while processing variables.
1375 DICompositeType SPTy = SP.getType();
1376 DIArray Args = SPTy.getTypeArray();
1377 unsigned SPTag = SPTy.getTag();
1378
Bill Wendling0310d762009-05-15 09:23:25 +00001379 if (SPTag == dwarf::DW_TAG_subroutine_type)
1380 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1381 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001382 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelb4645642010-02-06 01:02:37 +00001383 addType(Arg, ATy);
1384 if (ATy.isArtificial())
1385 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001386 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001387 }
1388 }
1389
Devang Patel4e0d19d2010-02-03 19:57:19 +00001390 if (SP.isArtificial())
1391 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1392
Devang Patelccff8122010-04-30 19:38:23 +00001393 if (!SP.isLocalToUnit())
1394 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001395
Devang Patelccff8122010-04-30 19:38:23 +00001396 if (SP.isOptimized())
1397 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1398
Jim Grosbach91729002010-07-21 23:03:52 +00001399 if (unsigned isa = Asm->getISAEncoding()) {
1400 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1401 }
1402
Devang Patelccff8122010-04-30 19:38:23 +00001403 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel163a9f72010-05-10 22:49:55 +00001404 SPCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +00001405
Stuart Hastings215aa152010-06-11 20:08:44 +00001406 // Add to context owner.
1407 addToContextOwner(SPDie, SP.getContext());
1408
Bill Wendling0310d762009-05-15 09:23:25 +00001409 return SPDie;
1410}
1411
Devang Patele9f8f5e2010-05-07 20:54:48 +00001412DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001413 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001414
1415 DbgScope *AScope = AbstractScopes.lookup(N);
1416 if (AScope)
1417 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001418
Devang Patel53bb5c92009-11-10 23:06:00 +00001419 DbgScope *Parent = NULL;
1420
1421 DIDescriptor Scope(N);
1422 if (Scope.isLexicalBlock()) {
1423 DILexicalBlock DB(N);
1424 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +00001425 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +00001426 }
1427
1428 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1429
1430 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001431 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001432 AScope->setAbstractScope();
1433 AbstractScopes[N] = AScope;
1434 if (DIDescriptor(N).isSubprogram())
1435 AbstractScopesList.push_back(AScope);
1436 return AScope;
1437}
Devang Patelaf9e8472009-10-01 20:31:14 +00001438
Devang Patel5f094002010-04-06 23:53:48 +00001439/// isSubprogramContext - Return true if Context is either a subprogram
1440/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001441static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001442 if (!Context)
1443 return false;
1444 DIDescriptor D(Context);
1445 if (D.isSubprogram())
1446 return true;
1447 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001448 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +00001449 return false;
1450}
1451
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001452/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001453/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1454/// If there are global variables in this scope then create and insert
1455/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001456DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +00001457 CompileUnit *SPCU = getCompileUnit(SPNode);
1458 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +00001459
Chris Lattnerd38fee82010-04-05 00:13:49 +00001460 assert(SPDie && "Unable to find subprogram DIE!");
1461 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001462
Chris Lattnerd38fee82010-04-05 00:13:49 +00001463 // There is not any need to generate specification DIE for a function
1464 // defined at compile unit level. If a function is defined inside another
1465 // function then gdb prefers the definition at top level and but does not
Jim Grosbach1e20b962010-07-21 21:21:52 +00001466 // expect specification DIE in parent function. So avoid creating
Chris Lattnerd38fee82010-04-05 00:13:49 +00001467 // specification DIE for a function defined inside a function.
1468 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbach1e20b962010-07-21 21:21:52 +00001469 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001470 !isSubprogramContext(SP.getContext())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001471 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001472
1473 // Add arguments.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001474 DICompositeType SPTy = SP.getType();
1475 DIArray Args = SPTy.getTypeArray();
1476 unsigned SPTag = SPTy.getTag();
1477 if (SPTag == dwarf::DW_TAG_subroutine_type)
1478 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1479 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001480 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerd38fee82010-04-05 00:13:49 +00001481 addType(Arg, ATy);
1482 if (ATy.isArtificial())
1483 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1484 SPDie->addChild(Arg);
1485 }
1486 DIE *SPDeclDie = SPDie;
1487 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001488 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Chris Lattnerd38fee82010-04-05 00:13:49 +00001489 SPDeclDie);
Devang Patel163a9f72010-05-10 22:49:55 +00001490 SPCU->addDie(SPDie);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001491 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001492
Devang Patel8aa61472010-07-07 22:20:57 +00001493 // Pick up abstract subprogram DIE.
1494 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1495 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1496 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1497 dwarf::DW_FORM_ref4, AbsSPDIE);
1498 SPCU->addDie(SPDie);
1499 }
1500
Chris Lattnerd38fee82010-04-05 00:13:49 +00001501 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1502 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1503 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1504 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1505 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1506 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1507 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001508
Chris Lattnerd38fee82010-04-05 00:13:49 +00001509 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001510}
1511
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001512/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001513/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1514DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001515
1516 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1517 if (Scope->isAbstractScope())
1518 return ScopeDIE;
1519
1520 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1521 if (Ranges.empty())
1522 return 0;
1523
1524 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1525 if (Ranges.size() > 1) {
1526 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +00001527 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +00001528 // DW_AT_ranges appropriately.
1529 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1530 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1531 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1532 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001533 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1534 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +00001535 }
1536 DebugRangeSymbols.push_back(NULL);
1537 DebugRangeSymbols.push_back(NULL);
1538 return ScopeDIE;
1539 }
1540
Devang Patelc3f5f782010-05-25 23:40:22 +00001541 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1542 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001543
Devang Patelc3f5f782010-05-25 23:40:22 +00001544 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001545
Chris Lattnerb7db7332010-03-09 01:58:53 +00001546 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1547 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001548
Devang Pateleac9c072010-04-27 19:46:33 +00001549 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1550 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001551
1552 return ScopeDIE;
1553}
1554
Devang Patel2c4ceb12009-11-21 02:48:08 +00001555/// constructInlinedScopeDIE - This scope represents inlined body of
1556/// a function. Construct DIE to represent this concrete inlined copy
1557/// of the function.
1558DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001559
1560 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001561 assert (Ranges.empty() == false
Devang Pateleac9c072010-04-27 19:46:33 +00001562 && "DbgScope does not have instruction markers!");
1563
1564 // FIXME : .debug_inlined section specification does not clearly state how
1565 // to emit inlined scope that is split into multiple instruction ranges.
1566 // For now, use first instruction range and emit low_pc/high_pc pair and
1567 // corresponding .debug_inlined section entry for this pair.
1568 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +00001569 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1570 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001571
Devang Patel0afbf232010-07-08 22:39:20 +00001572 if (StartLabel == 0 || EndLabel == 0) {
Devang Pateleac9c072010-04-27 19:46:33 +00001573 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1574 return 0;
1575 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001576 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001577 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001578 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001579 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +00001580
Devang Patel3c91b052010-03-08 20:52:55 +00001581 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001582 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001583 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001584 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1585
Devang Patel2db49d72010-05-07 18:11:54 +00001586 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001587 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1588 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001589 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001590 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001591 dwarf::DW_FORM_ref4, OriginDIE);
1592
Chris Lattner6ed0f902010-03-09 00:31:02 +00001593 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001594 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001595
1596 InlinedSubprogramDIEs.insert(OriginDIE);
1597
1598 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001599 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +00001600 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001601
1602 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001603 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001604 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +00001605 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001606 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001607 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001608
Devang Patel53bb5c92009-11-10 23:06:00 +00001609 DILocation DL(Scope->getInlinedAt());
Devang Patel163a9f72010-05-10 22:49:55 +00001610 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001611 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001612
1613 return ScopeDIE;
1614}
1615
Devang Patel2c4ceb12009-11-21 02:48:08 +00001616
1617/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001618DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +00001619 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +00001620 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001621 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001622
1623 // Translate tag to proper Dwarf tag. The result variable is dropped for
1624 // now.
1625 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +00001626 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001627 case dwarf::DW_TAG_return_variable:
1628 return NULL;
1629 case dwarf::DW_TAG_arg_variable:
1630 Tag = dwarf::DW_TAG_formal_parameter;
1631 break;
1632 case dwarf::DW_TAG_auto_variable: // fall thru
1633 default:
1634 Tag = dwarf::DW_TAG_variable;
1635 break;
1636 }
1637
1638 // Define variable debug information entry.
1639 DIE *VariableDie = new DIE(Tag);
1640
Devang Patel53bb5c92009-11-10 23:06:00 +00001641 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +00001642 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1643 V2AVI = VarToAbstractVarMap.find(DV);
1644 if (V2AVI != VarToAbstractVarMap.end())
1645 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001646
Devang Patel26c1e562010-05-20 16:36:41 +00001647 if (AbsDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001648 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001649 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001650 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001651 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel8bd11de2010-08-09 21:01:39 +00001652 addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +00001653
1654 // Add variable type.
Devang Patel8bd11de2010-08-09 21:01:39 +00001655 addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001656 }
1657
Devang Patel8bd11de2010-08-09 21:01:39 +00001658 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patelb4645642010-02-06 01:02:37 +00001659 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel3cf763d2010-09-29 23:07:21 +00001660 else if (DIVariable(DV->getVariable()).isArtificial())
1661 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +00001662
1663 if (Scope->isAbstractScope()) {
1664 DV->setDIE(VariableDie);
1665 return VariableDie;
1666 }
1667
1668 // Add variable address.
1669
1670 unsigned Offset = DV->getDotDebugLocOffset();
1671 if (Offset != ~0U) {
1672 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1673 Asm->GetTempSymbol("debug_loc", Offset));
1674 DV->setDIE(VariableDie);
1675 UseDotDebugLocEntry.insert(VariableDie);
1676 return VariableDie;
1677 }
1678
1679 // Check if variable is described by a DBG_VALUE instruction.
1680 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1681 DbgVariableToDbgInstMap.find(DV);
1682 if (DVI != DbgVariableToDbgInstMap.end()) {
1683 const MachineInstr *DVInsn = DVI->second;
Devang Patelc3f5f782010-05-25 23:40:22 +00001684 bool updated = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001685 // FIXME : Handle getNumOperands != 3
Devang Patelc3f5f782010-05-25 23:40:22 +00001686 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +00001687 if (DVInsn->getOperand(0).isReg()) {
1688 const MachineOperand RegOp = DVInsn->getOperand(0);
1689 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1690 if (DVInsn->getOperand(1).isImm() &&
1691 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1692 addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
1693 updated = true;
1694 } else
Devang Patel522ad742010-11-12 23:20:42 +00001695 updated = addRegisterAddress(VariableDie, RegOp);
Devang Patel0b48ead2010-08-31 22:22:42 +00001696 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001697 else if (DVInsn->getOperand(0).isImm())
Devang Patel522ad742010-11-12 23:20:42 +00001698 updated = addConstantValue(VariableDie, DVInsn->getOperand(0));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001699 else if (DVInsn->getOperand(0).isFPImm())
1700 updated =
Devang Patel522ad742010-11-12 23:20:42 +00001701 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patelc3f5f782010-05-25 23:40:22 +00001702 } else {
1703 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1704 if (Location.getReg()) {
1705 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patelc3f5f782010-05-25 23:40:22 +00001706 updated = true;
1707 }
1708 }
1709 if (!updated) {
1710 // If variableDie is not updated then DBG_VALUE instruction does not
1711 // have valid variable info.
1712 delete VariableDie;
1713 return NULL;
1714 }
1715 DV->setDIE(VariableDie);
1716 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001717 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001718
1719 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +00001720 int FI = 0;
Devang Patel9e3bd2c2010-08-31 06:11:28 +00001721 if (findVariableFrameIndex(DV, &FI))
1722 addVariableAddress(DV, VariableDie, FI);
1723
Devang Patel53bb5c92009-11-10 23:06:00 +00001724 DV->setDIE(VariableDie);
1725 return VariableDie;
1726
1727}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001728
Devang Patel193f7202009-11-24 01:14:22 +00001729void DwarfDebug::addPubTypes(DISubprogram SP) {
1730 DICompositeType SPTy = SP.getType();
1731 unsigned SPTag = SPTy.getTag();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001732 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel193f7202009-11-24 01:14:22 +00001733 return;
1734
1735 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001736 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +00001737 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +00001738 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +00001739 continue;
1740 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +00001741 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +00001742 && !CATy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001743 CompileUnit *TheCU = getCompileUnit(CATy);
1744 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1745 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001746 }
1747 }
1748}
1749
Devang Patel2c4ceb12009-11-21 02:48:08 +00001750/// constructScopeDIE - Construct a DIE for this scope.
1751DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001752 if (!Scope || !Scope->getScopeNode())
1753 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001754
Devang Patel3c91b052010-03-08 20:52:55 +00001755 DIScope DS(Scope->getScopeNode());
1756 DIE *ScopeDIE = NULL;
1757 if (Scope->getInlinedAt())
1758 ScopeDIE = constructInlinedScopeDIE(Scope);
1759 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +00001760 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001761 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001762 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001763 // Note down abstract DIE.
1764 if (ScopeDIE)
1765 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1766 }
Devang Patel3c91b052010-03-08 20:52:55 +00001767 else
Devang Patel2db49d72010-05-07 18:11:54 +00001768 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001769 }
Devang Patelaead63c2010-03-29 22:59:58 +00001770 else
Devang Patel3c91b052010-03-08 20:52:55 +00001771 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001772 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001773
Devang Patel53bb5c92009-11-10 23:06:00 +00001774 // Add variables to scope.
Devang Patele03161c2010-08-09 18:51:29 +00001775 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001776 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001777 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001778 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001779 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001780 }
1781
1782 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001783 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001784 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1785 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001786 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001787 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001788 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001789 }
Devang Patel193f7202009-11-24 01:14:22 +00001790
Jim Grosbach1e20b962010-07-21 21:21:52 +00001791 if (DS.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001792 addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001793
Devang Patel193f7202009-11-24 01:14:22 +00001794 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001795}
1796
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001797/// GetOrCreateSourceID - Look up the source id with the given directory and
1798/// source file names. If none currently exists, create a new id and insert it
1799/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1800/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +00001801
Rafael Espindola5c055632010-11-18 02:04:25 +00001802unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName){
Devang Patel1905a182010-09-16 20:57:49 +00001803 // If FE did not provide a file name, then assume stdin.
1804 if (FileName.empty())
Rafael Espindola5c055632010-11-18 02:04:25 +00001805 return GetOrCreateSourceID("<stdin>");
Devang Patel1905a182010-09-16 20:57:49 +00001806
Rafael Espindola5c055632010-11-18 02:04:25 +00001807 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
1808 if (Entry.getValue())
1809 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001810
Rafael Espindola5c055632010-11-18 02:04:25 +00001811 unsigned SrcId = SourceIdMap.size();
1812 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001813
Rafael Espindola5c055632010-11-18 02:04:25 +00001814 // Print out a .file directive to specify files for .loc directives.
1815 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001816
1817 return SrcId;
1818}
1819
Devang Patel6404e4e2009-12-15 19:16:48 +00001820/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1821DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel163a9f72010-05-10 22:49:55 +00001822 CompileUnit *TheCU = getCompileUnit(NS);
1823 DIE *NDie = TheCU->getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001824 if (NDie)
1825 return NDie;
1826 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel163a9f72010-05-10 22:49:55 +00001827 TheCU->insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +00001828 if (!NS.getName().empty())
1829 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patel81625742010-08-09 20:20:05 +00001830 addSourceLine(NDie, NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001831 addToContextOwner(NDie, NS.getContext());
1832 return NDie;
1833}
1834
Jim Grosbach1e20b962010-07-21 21:21:52 +00001835/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +00001836/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001837void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001838 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001839 StringRef FN = DIUnit.getFilename();
1840 StringRef Dir = DIUnit.getDirectory();
Rafael Espindola5c055632010-11-18 02:04:25 +00001841 unsigned ID = GetOrCreateSourceID(FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001842
1843 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001844 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001845 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001846 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001847 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001848 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +00001849 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1850 // simplifies debug range entries.
Devang Patel9b93b6b2010-06-28 22:22:47 +00001851 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +00001852 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +00001853 // compile unit in debug_line section.
Devang Patelae84d5b2010-08-31 23:50:19 +00001854 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
1855 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
1856 Asm->GetTempSymbol("section_line"));
1857 else
1858 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001859
Devang Patel65dbc902009-11-25 17:36:49 +00001860 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001861 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001862 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001863 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001864
Devang Patel65dbc902009-11-25 17:36:49 +00001865 StringRef Flags = DIUnit.getFlags();
1866 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001867 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001868
1869 unsigned RVer = DIUnit.getRunTimeVersion();
1870 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001871 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001872 dwarf::DW_FORM_data1, RVer);
1873
Devang Patel163a9f72010-05-10 22:49:55 +00001874 CompileUnit *NewCU = new CompileUnit(ID, Die);
1875 if (!FirstCU)
1876 FirstCU = NewCU;
1877 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001878}
1879
Devang Patel163a9f72010-05-10 22:49:55 +00001880/// getCompielUnit - Get CompileUnit DIE.
1881CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1882 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1883 DIDescriptor D(N);
1884 const MDNode *CUNode = NULL;
1885 if (D.isCompileUnit())
1886 CUNode = N;
1887 else if (D.isSubprogram())
1888 CUNode = DISubprogram(N).getCompileUnit();
1889 else if (D.isType())
1890 CUNode = DIType(N).getCompileUnit();
1891 else if (D.isGlobalVariable())
1892 CUNode = DIGlobalVariable(N).getCompileUnit();
1893 else if (D.isVariable())
1894 CUNode = DIVariable(N).getCompileUnit();
1895 else if (D.isNameSpace())
1896 CUNode = DINameSpace(N).getCompileUnit();
1897 else if (D.isFile())
1898 CUNode = DIFile(N).getCompileUnit();
1899 else
1900 return FirstCU;
1901
1902 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1903 = CUMap.find(CUNode);
1904 if (I == CUMap.end())
1905 return FirstCU;
1906 return I->second;
1907}
1908
Devang Patel0c4720c2010-08-23 18:25:56 +00001909/// isUnsignedDIType - Return true if type encoding is unsigned.
1910static bool isUnsignedDIType(DIType Ty) {
1911 DIDerivedType DTy(Ty);
1912 if (DTy.Verify())
1913 return isUnsignedDIType(DTy.getTypeDerivedFrom());
1914
1915 DIBasicType BTy(Ty);
1916 if (BTy.Verify()) {
1917 unsigned Encoding = BTy.getEncoding();
1918 if (Encoding == dwarf::DW_ATE_unsigned ||
1919 Encoding == dwarf::DW_ATE_unsigned_char)
1920 return true;
1921 }
1922 return false;
1923}
Devang Patel163a9f72010-05-10 22:49:55 +00001924
1925/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001926void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +00001927 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001928
Devang Patel905cf5e2009-09-04 23:59:07 +00001929 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +00001930 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +00001931 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001932
1933 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001934 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +00001935 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001936 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001937
Devang Patel9fa539c2010-08-10 01:37:23 +00001938 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +00001939 DIE *VariableDIE = new DIE(GV.getTag());
1940
1941 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001942
Devang Patel9fa539c2010-08-10 01:37:23 +00001943 // Add name.
1944 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1945 GV.getDisplayName());
1946 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +00001947 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel9fa539c2010-08-10 01:37:23 +00001948 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1949 getRealLinkageName(LinkageName));
1950 // Add type.
1951 addType(VariableDIE, GTy);
Devang Patel50d80e32010-04-13 20:35:04 +00001952 if (GTy.isCompositeType() && !GTy.getName().empty()
1953 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001954 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001955 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +00001956 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001957 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001958 // Add scoping info.
1959 if (!GV.isLocalToUnit()) {
1960 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1961 // Expose as global.
1962 TheCU->addGlobal(GV.getName(), VariableDIE);
1963 }
1964 // Add line number info.
1965 addSourceLine(VariableDIE, GV);
1966 // Add to map.
1967 TheCU->insertDIE(N, VariableDIE);
1968 // Add to context owner.
1969 DIDescriptor GVContext = GV.getContext();
1970 addToContextOwner(VariableDIE, GVContext);
1971 // Add location.
Devang Patel29368072010-08-10 07:11:13 +00001972 if (isGlobalVariable) {
1973 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1974 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1975 addLabel(Block, 0, dwarf::DW_FORM_udata,
1976 Asm->Mang->getSymbol(GV.getGlobal()));
1977 // Do not create specification DIE if context is either compile unit
1978 // or a subprogram.
1979 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1980 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1981 // Create specification DIE.
1982 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1983 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1984 dwarf::DW_FORM_ref4, VariableDIE);
1985 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1986 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1987 TheCU->addDie(VariableSpecDIE);
1988 } else {
1989 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1990 }
Devang Patel76a788c2011-01-06 21:39:25 +00001991 } else if (ConstantInt *CI =
1992 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1993 addConstantValue(VariableDIE, CI, isUnsignedDIType(GTy));
1994
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
Devang Patel2c4ceb12009-11-21 02:48:08 +00002084/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002085///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002086void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00002087 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +00002088 const Module *M = MMI->getModule();
Devang Patele9a1cca2010-08-02 17:32:15 +00002089 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +00002090 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2091 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2092 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2093 DISubprogram SP(AllSPs->getOperand(SI));
2094 if (!SP.Verify()) continue;
2095
2096 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +00002097 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +00002098 StringRef FName = SP.getLinkageName();
2099 if (FName.empty())
2100 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002101 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +00002102 if (!NMD) continue;
2103 unsigned E = NMD->getNumOperands();
2104 if (!E) continue;
2105 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Patele9a1cca2010-08-02 17:32:15 +00002106 DeadFnScopeMap[SP] = Scope;
Devang Patel4a1cad62010-06-28 18:25:03 +00002107 for (unsigned I = 0; I != E; ++I) {
2108 DIVariable DV(NMD->getOperand(I));
2109 if (!DV.Verify()) continue;
2110 Scope->addVariable(new DbgVariable(DV));
2111 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002112
Devang Patel4a1cad62010-06-28 18:25:03 +00002113 // Construct subprogram DIE and add variables DIEs.
2114 constructSubprogramDIE(SP);
2115 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patele03161c2010-08-09 18:51:29 +00002116 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel4a1cad62010-06-28 18:25:03 +00002117 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2118 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2119 if (VariableDIE)
2120 ScopeDIE->addChild(VariableDIE);
2121 }
2122 }
2123 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002124
Devang Patel53bb5c92009-11-10 23:06:00 +00002125 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2126 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2127 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2128 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002129 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00002130 }
2131
Devang Patele9f8f5e2010-05-07 20:54:48 +00002132 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00002133 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2134 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002135 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00002136 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00002137 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00002138 if (!NDie) continue;
2139 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00002140 }
2141
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002142 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002143 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002144 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002145 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002146 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002147
2148 // End text sections.
2149 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002150 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00002151 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002152 }
2153
2154 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002155 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002156
2157 // Emit function debug frame information
2158 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2159 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002160 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002161
2162 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002163 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002164
2165 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00002166 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002167
2168 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002169 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002170
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002171 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002172 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002173
Devang Patel193f7202009-11-24 01:14:22 +00002174 // Emit info into a debug pubtypes section.
2175 emitDebugPubTypes();
2176
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002177 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002178 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002179
2180 // Emit info into a debug aranges section.
2181 EmitDebugARanges();
2182
2183 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002184 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002185
2186 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002187 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002188
2189 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002190 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002191
Chris Lattnerbc733f52010-03-13 02:17:42 +00002192 // Emit info into a debug str section.
2193 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002194
Devang Patele9a1cca2010-08-02 17:32:15 +00002195 // clean up.
2196 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +00002197 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2198 E = CUMap.end(); I != E; ++I)
2199 delete I->second;
2200 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002201}
2202
Devang Patel53bb5c92009-11-10 23:06:00 +00002203/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002204DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002205 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002206
Devang Patel2db49d72010-05-07 18:11:54 +00002207 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00002208 if (AbsDbgVariable)
2209 return AbsDbgVariable;
2210
Devang Patel2db49d72010-05-07 18:11:54 +00002211 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002212 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00002213 if (!Scope)
2214 return NULL;
2215
Devang Patel26c1e562010-05-20 16:36:41 +00002216 AbsDbgVariable = new DbgVariable(Var);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002217 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002218 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00002219 return AbsDbgVariable;
2220}
2221
Devang Patelee432862010-05-20 19:57:06 +00002222/// collectVariableInfoFromMMITable - Collect variable information from
2223/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002224void
Devang Patelee432862010-05-20 19:57:06 +00002225DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2226 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002227 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patele717faa2009-10-06 01:26:37 +00002228 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2229 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2230 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00002231 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00002232 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00002233 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002234 DIVariable DV(Var);
2235 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002236
Chris Lattnerde4845c2010-04-02 19:42:39 +00002237 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002238 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002239 Scope = ConcreteScopes.lookup(IA);
2240 if (Scope == 0)
2241 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002242
Devang Patelfb0ee432009-11-10 23:20:04 +00002243 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002244 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00002245 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00002246
Devang Patel26c1e562010-05-20 16:36:41 +00002247 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2248 DbgVariable *RegVar = new DbgVariable(DV);
2249 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002250 Scope->addVariable(RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +00002251 if (AbsDbgVariable) {
2252 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2253 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2254 }
Devang Patele717faa2009-10-06 01:26:37 +00002255 }
Devang Patelee432862010-05-20 19:57:06 +00002256}
Devang Patel90a48ad2010-03-15 18:33:46 +00002257
Jim Grosbach1e20b962010-07-21 21:21:52 +00002258/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00002259/// DBG_VALUE instruction, is in a defined reg.
2260static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2261 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2262 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
2263 return true;
2264 return false;
2265}
2266
Devang Patelee432862010-05-20 19:57:06 +00002267/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002268void
Devang Patel78e127d2010-06-25 22:07:34 +00002269DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2270 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002271
Devang Patelee432862010-05-20 19:57:06 +00002272 /// collection info from MMI table.
2273 collectVariableInfoFromMMITable(MF, Processed);
2274
2275 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patel90a48ad2010-03-15 18:33:46 +00002276 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002277 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelee432862010-05-20 19:57:06 +00002278 I != E; ++I)
Devang Patel90a48ad2010-03-15 18:33:46 +00002279 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2280 II != IE; ++II) {
2281 const MachineInstr *MInsn = II;
Devang Patela36478f2011-01-11 21:42:10 +00002282 if (!MInsn->isDebugValue())
Devang Patel90a48ad2010-03-15 18:33:46 +00002283 continue;
Devang Patelee432862010-05-20 19:57:06 +00002284 DbgValues.push_back(MInsn);
2285 }
Devang Patel90a48ad2010-03-15 18:33:46 +00002286
Devang Patelc3f5f782010-05-25 23:40:22 +00002287 // This is a collection of DBV_VALUE instructions describing same variable.
2288 SmallVector<const MachineInstr *, 4> MultipleValues;
Devang Patelee432862010-05-20 19:57:06 +00002289 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2290 E = DbgValues.end(); I != E; ++I) {
2291 const MachineInstr *MInsn = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00002292 MultipleValues.clear();
2293 if (isDbgValueInDefinedReg(MInsn))
2294 MultipleValues.push_back(MInsn);
Devang Patelee432862010-05-20 19:57:06 +00002295 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2296 if (Processed.count(DV) != 0)
2297 continue;
2298
Devang Patel354eb7e2010-06-02 19:05:13 +00002299 const MachineInstr *PrevMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002300 for (SmallVector<const MachineInstr *, 8>::iterator MI = I+1,
Devang Patelc3f5f782010-05-25 23:40:22 +00002301 ME = DbgValues.end(); MI != ME; ++MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002302 const MDNode *Var =
Devang Patelc3f5f782010-05-25 23:40:22 +00002303 (*MI)->getOperand((*MI)->getNumOperands()-1).getMetadata();
Devang Patela36478f2011-01-11 21:42:10 +00002304 if (Var == DV &&
Devang Patel354eb7e2010-06-02 19:05:13 +00002305 !PrevMI->isIdenticalTo(*MI))
Devang Patelc3f5f782010-05-25 23:40:22 +00002306 MultipleValues.push_back(*MI);
Devang Patel354eb7e2010-06-02 19:05:13 +00002307 PrevMI = *MI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002308 }
2309
Devang Patela36478f2011-01-11 21:42:10 +00002310 DbgScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00002311 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2312 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelc0c5a262010-05-21 00:10:20 +00002313 Scope = CurrentFnDbgScope;
Devang Patela36478f2011-01-11 21:42:10 +00002314 else
2315 Scope = findDbgScope(MInsn);
Devang Patelee432862010-05-20 19:57:06 +00002316 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00002317 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00002318 continue;
2319
2320 Processed.insert(DV);
Devang Patelee432862010-05-20 19:57:06 +00002321 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patelee432862010-05-20 19:57:06 +00002322 Scope->addVariable(RegVar);
Devang Patelc0c5a262010-05-21 00:10:20 +00002323 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2324 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2325 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002326 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002327 if (MultipleValues.size() <= 1) {
2328 DbgVariableToDbgInstMap[RegVar] = MInsn;
2329 continue;
2330 }
2331
2332 // handle multiple DBG_VALUE instructions describing one variable.
Devang Patelc3f5f782010-05-25 23:40:22 +00002333 if (DotDebugLocEntries.empty())
Devang Patel80250682010-05-26 23:55:23 +00002334 RegVar->setDotDebugLocOffset(0);
2335 else
2336 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Devang Patele2df8422010-05-26 17:29:32 +00002337 const MachineInstr *Begin = NULL;
2338 const MachineInstr *End = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002339 for (SmallVector<const MachineInstr *, 4>::iterator
2340 MVI = MultipleValues.begin(), MVE = MultipleValues.end();
Devang Patel61409622010-07-07 20:12:52 +00002341 MVI != MVE; ++MVI) {
Devang Patele2df8422010-05-26 17:29:32 +00002342 if (!Begin) {
2343 Begin = *MVI;
2344 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002345 }
Devang Patele2df8422010-05-26 17:29:32 +00002346 End = *MVI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002347 MachineLocation MLoc;
Devang Patelb2cf5812010-08-04 18:40:52 +00002348 if (Begin->getNumOperands() == 3) {
2349 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2350 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2351 } else
2352 MLoc = Asm->getDebugValueLocation(Begin);
2353
Devang Patele2df8422010-05-26 17:29:32 +00002354 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2355 const MCSymbol *SLabel = getLabelBeforeInsn(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002356 if (MLoc.getReg())
2357 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
2358
Devang Patele2df8422010-05-26 17:29:32 +00002359 Begin = End;
2360 if (MVI + 1 == MVE) {
2361 // If End is the last instruction then its value is valid
Devang Patelc3f5f782010-05-25 23:40:22 +00002362 // until the end of the funtion.
Devang Patelb2cf5812010-08-04 18:40:52 +00002363 MachineLocation EMLoc;
2364 if (End->getNumOperands() == 3) {
2365 if (End->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2366 EMLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2367 } else
2368 EMLoc = Asm->getDebugValueLocation(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002369 if (EMLoc.getReg())
2370 DotDebugLocEntries.
2371 push_back(DotDebugLocEntry(SLabel, FunctionEndSym, EMLoc));
Devang Patelc3f5f782010-05-25 23:40:22 +00002372 }
2373 }
2374 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00002375 }
Devang Patel98e1cac2010-05-14 21:01:35 +00002376
2377 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00002378 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00002379 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00002380 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002381 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00002382 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00002383 continue;
2384 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2385 if (Scope)
Devang Patel26c1e562010-05-20 16:36:41 +00002386 Scope->addVariable(new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00002387 }
2388 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002389}
Devang Patel98e1cac2010-05-14 21:01:35 +00002390
Devang Patelc3f5f782010-05-25 23:40:22 +00002391/// getLabelBeforeInsn - Return Label preceding the instruction.
2392const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2393 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2394 LabelsBeforeInsn.find(MI);
2395 if (I == LabelsBeforeInsn.end())
2396 // FunctionBeginSym always preceeds all the instruction in current function.
2397 return FunctionBeginSym;
2398 return I->second;
2399}
2400
2401/// getLabelAfterInsn - Return Label immediately following the instruction.
2402const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2403 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2404 LabelsAfterInsn.find(MI);
2405 if (I == LabelsAfterInsn.end())
2406 return NULL;
2407 return I->second;
Devang Patele717faa2009-10-06 01:26:37 +00002408}
2409
Devang Patelcbbe2872010-10-26 17:49:02 +00002410/// beginInstruction - Process beginning of an instruction.
2411void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002412 if (InsnNeedsLabel.count(MI) == 0) {
2413 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patel553881b2010-03-29 17:20:31 +00002414 return;
Devang Patelc3f5f782010-05-25 23:40:22 +00002415 }
Devang Patelaead63c2010-03-29 22:59:58 +00002416
Devang Patelb2b31a62010-05-26 19:37:24 +00002417 // Check location.
2418 DebugLoc DL = MI->getDebugLoc();
2419 if (!DL.isUnknown()) {
2420 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2421 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Pateleac9c072010-04-27 19:46:33 +00002422 PrevInstLoc = DL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002423 LabelsBeforeInsn[MI] = PrevLabel;
2424 return;
Devang Pateleac9c072010-04-27 19:46:33 +00002425 }
Devang Patel553881b2010-03-29 17:20:31 +00002426
Jim Grosbach1e20b962010-07-21 21:21:52 +00002427 // If location is unknown then use temp label for this DBG_VALUE
Devang Patelb2b31a62010-05-26 19:37:24 +00002428 // instruction.
2429 if (MI->isDebugValue()) {
Devang Patel77051f52010-05-26 21:23:46 +00002430 PrevLabel = MMI->getContext().CreateTempSymbol();
2431 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00002432 LabelsBeforeInsn[MI] = PrevLabel;
2433 return;
2434 }
2435
2436 if (UnknownLocations) {
2437 PrevLabel = recordSourceLine(0, 0, 0);
2438 LabelsBeforeInsn[MI] = PrevLabel;
2439 return;
2440 }
2441
2442 assert (0 && "Instruction is not processed!");
Devang Patel0d20ac82009-10-06 01:50:42 +00002443}
2444
Devang Patelcbbe2872010-10-26 17:49:02 +00002445/// endInstruction - Process end of an instruction.
2446void DwarfDebug::endInstruction(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002447 if (InsnsEndScopeSet.count(MI) != 0) {
2448 // Emit a label if this instruction ends a scope.
2449 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2450 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002451 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002452 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002453}
2454
Devang Pateleac9c072010-04-27 19:46:33 +00002455/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002456DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel61409622010-07-07 20:12:52 +00002457 const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002458 if (!InlinedAt) {
2459 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2460 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002461 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002462 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2463 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002464 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002465 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002466 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002467 WScope->setParent(Parent);
2468 Parent->addScope(WScope);
2469 }
2470
2471 if (!WScope->getParent()) {
2472 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastingscad22ad2010-06-15 23:06:30 +00002473 // We used to check only for a linkage name, but that fails
2474 // since we began omitting the linkage name for private
2475 // functions. The new way is to check for the name in metadata,
2476 // but that's not supported in old .ll test cases. Ergo, we
2477 // check both.
Stuart Hastings215aa152010-06-11 20:08:44 +00002478 if (SPName == Asm->MF->getFunction()->getName() ||
2479 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Pateleac9c072010-04-27 19:46:33 +00002480 CurrentFnDbgScope = WScope;
2481 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002482
Devang Pateleac9c072010-04-27 19:46:33 +00002483 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002484 }
2485
Devang Patel78e127d2010-06-25 22:07:34 +00002486 getOrCreateAbstractScope(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00002487 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2488 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002489 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002490
2491 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2492 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2493 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002494 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002495 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002496 WScope->setParent(Parent);
2497 Parent->addScope(WScope);
2498
2499 ConcreteScopes[InlinedAt] = WScope;
Devang Pateleac9c072010-04-27 19:46:33 +00002500
2501 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002502}
2503
Devang Pateleac9c072010-04-27 19:46:33 +00002504/// hasValidLocation - Return true if debug location entry attached with
2505/// machine instruction encodes valid location info.
2506static bool hasValidLocation(LLVMContext &Ctx,
2507 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002508 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002509 DebugLoc DL = MInsn->getDebugLoc();
2510 if (DL.isUnknown()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002511
Devang Patele9f8f5e2010-05-07 20:54:48 +00002512 const MDNode *S = DL.getScope(Ctx);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002513
Devang Pateleac9c072010-04-27 19:46:33 +00002514 // There is no need to create another DIE for compile unit. For all
2515 // other scopes, create one DbgScope now. This will be translated
2516 // into a scope DIE at the end.
2517 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002518
Devang Pateleac9c072010-04-27 19:46:33 +00002519 Scope = S;
2520 InlinedAt = DL.getInlinedAt(Ctx);
2521 return true;
2522}
2523
2524/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2525/// hierarchy.
2526static void calculateDominanceGraph(DbgScope *Scope) {
2527 assert (Scope && "Unable to calculate scop edominance graph!");
2528 SmallVector<DbgScope *, 4> WorkStack;
2529 WorkStack.push_back(Scope);
2530 unsigned Counter = 0;
2531 while (!WorkStack.empty()) {
2532 DbgScope *WS = WorkStack.back();
2533 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2534 bool visitedChildren = false;
2535 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2536 SE = Children.end(); SI != SE; ++SI) {
2537 DbgScope *ChildScope = *SI;
2538 if (!ChildScope->getDFSOut()) {
2539 WorkStack.push_back(ChildScope);
2540 visitedChildren = true;
2541 ChildScope->setDFSIn(++Counter);
2542 break;
2543 }
2544 }
2545 if (!visitedChildren) {
2546 WorkStack.pop_back();
2547 WS->setDFSOut(++Counter);
2548 }
2549 }
2550}
2551
2552/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002553static
Devang Pateleac9c072010-04-27 19:46:33 +00002554void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2555 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2556{
2557#ifndef NDEBUG
2558 unsigned PrevDFSIn = 0;
2559 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2560 I != E; ++I) {
2561 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2562 II != IE; ++II) {
2563 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002564 const MDNode *Scope = NULL;
2565 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002566
2567 // Check if instruction has valid location information.
2568 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2569 dbgs() << " [ ";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002570 if (InlinedAt)
Devang Pateleac9c072010-04-27 19:46:33 +00002571 dbgs() << "*";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002572 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Pateleac9c072010-04-27 19:46:33 +00002573 MI2ScopeMap.find(MInsn);
2574 if (DI != MI2ScopeMap.end()) {
2575 DbgScope *S = DI->second;
2576 dbgs() << S->getDFSIn();
2577 PrevDFSIn = S->getDFSIn();
2578 } else
2579 dbgs() << PrevDFSIn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002580 } else
Devang Pateleac9c072010-04-27 19:46:33 +00002581 dbgs() << " [ x" << PrevDFSIn;
2582 dbgs() << " ]";
2583 MInsn->dump();
2584 }
2585 dbgs() << "\n";
2586 }
2587#endif
2588}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002589/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002590/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002591bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002592 // If scope information was extracted using .dbg intrinsics then there is not
2593 // any need to extract these information by scanning each instruction.
2594 if (!DbgScopeMap.empty())
2595 return false;
2596
Dan Gohman314bf7c2010-04-23 01:18:53 +00002597 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002598 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2599 SmallVector<DbgRange, 4> MIRanges;
2600 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002601 const MDNode *PrevScope = NULL;
2602 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002603 const MachineInstr *RangeBeginMI = NULL;
2604 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002605 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002606 I != E; ++I) {
2607 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2608 II != IE; ++II) {
2609 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002610 const MDNode *Scope = NULL;
2611 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002612
2613 // Check if instruction has valid location information.
2614 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2615 PrevMI = MInsn;
2616 continue;
2617 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002618
Devang Pateleac9c072010-04-27 19:46:33 +00002619 // If scope has not changed then skip this instruction.
2620 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2621 PrevMI = MInsn;
2622 continue;
2623 }
2624
Jim Grosbach1e20b962010-07-21 21:21:52 +00002625 if (RangeBeginMI) {
2626 // If we have alread seen a beginning of a instruction range and
Devang Pateleac9c072010-04-27 19:46:33 +00002627 // current instruction scope does not match scope of first instruction
2628 // in this range then create a new instruction range.
2629 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002630 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel61409622010-07-07 20:12:52 +00002631 PrevInlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002632 MIRanges.push_back(R);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002633 }
Devang Pateleac9c072010-04-27 19:46:33 +00002634
2635 // This is a beginning of a new instruction range.
2636 RangeBeginMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002637
Devang Pateleac9c072010-04-27 19:46:33 +00002638 // Reset previous markers.
2639 PrevMI = MInsn;
2640 PrevScope = Scope;
2641 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002642 }
2643 }
2644
Devang Pateleac9c072010-04-27 19:46:33 +00002645 // Create last instruction range.
2646 if (RangeBeginMI && PrevMI && PrevScope) {
2647 DbgRange R(RangeBeginMI, PrevMI);
2648 MIRanges.push_back(R);
2649 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002650 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002651
Devang Patel344130e2010-01-04 20:44:00 +00002652 if (!CurrentFnDbgScope)
2653 return false;
2654
Devang Pateleac9c072010-04-27 19:46:33 +00002655 calculateDominanceGraph(CurrentFnDbgScope);
2656 if (PrintDbgScope)
2657 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2658
2659 // Find ranges of instructions covered by each DbgScope;
2660 DbgScope *PrevDbgScope = NULL;
2661 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2662 RE = MIRanges.end(); RI != RE; ++RI) {
2663 const DbgRange &R = *RI;
2664 DbgScope *S = MI2ScopeMap.lookup(R.first);
2665 assert (S && "Lost DbgScope for a machine instruction!");
2666 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2667 PrevDbgScope->closeInsnRange(S);
2668 S->openInsnRange(R.first);
2669 S->extendInsnRange(R.second);
2670 PrevDbgScope = S;
2671 }
2672
2673 if (PrevDbgScope)
2674 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002675
Devang Patele37b0c62010-04-08 18:43:56 +00002676 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002677
2678 return !DbgScopeMap.empty();
2679}
2680
Jim Grosbach1e20b962010-07-21 21:21:52 +00002681/// identifyScopeMarkers() -
Devang Pateleac9c072010-04-27 19:46:33 +00002682/// Each DbgScope has first instruction and last instruction to mark beginning
2683/// and end of a scope respectively. Create an inverse map that list scopes
2684/// starts (and ends) with an instruction. One instruction may start (or end)
2685/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002686void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002687 SmallVector<DbgScope *, 4> WorkList;
2688 WorkList.push_back(CurrentFnDbgScope);
2689 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002690 DbgScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002691
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002692 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002693 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002694 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002695 SE = Children.end(); SI != SE; ++SI)
2696 WorkList.push_back(*SI);
2697
Devang Patel53bb5c92009-11-10 23:06:00 +00002698 if (S->isAbstractScope())
2699 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002700
Devang Pateleac9c072010-04-27 19:46:33 +00002701 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2702 if (Ranges.empty())
2703 continue;
2704 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2705 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002706 assert(RI->first && "DbgRange does not have first instruction!");
2707 assert(RI->second && "DbgRange does not have second instruction!");
Devang Pateleac9c072010-04-27 19:46:33 +00002708 InsnsEndScopeSet.insert(RI->second);
2709 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002710 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002711}
2712
Dan Gohman084751c2010-04-20 00:37:27 +00002713/// FindFirstDebugLoc - Find the first debug location in the function. This
2714/// is intended to be an approximation for the source position of the
2715/// beginning of the function.
2716static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2717 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2718 I != E; ++I)
2719 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2720 MBBI != MBBE; ++MBBI) {
2721 DebugLoc DL = MBBI->getDebugLoc();
2722 if (!DL.isUnknown())
2723 return DL;
2724 }
2725 return DebugLoc();
2726}
2727
Devang Patel9a31f0f2010-10-25 20:45:32 +00002728#ifndef NDEBUG
2729/// CheckLineNumbers - Count basicblocks whose instructions do not have any
2730/// line number information.
2731static void CheckLineNumbers(const MachineFunction *MF) {
2732 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2733 I != E; ++I) {
2734 bool FoundLineNo = false;
2735 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2736 II != IE; ++II) {
2737 const MachineInstr *MI = II;
2738 if (!MI->getDebugLoc().isUnknown()) {
2739 FoundLineNo = true;
2740 break;
2741 }
2742 }
Devang Patel4d7f9a02010-10-28 22:11:59 +00002743 if (!FoundLineNo && I->size())
Devang Patel9a31f0f2010-10-25 20:45:32 +00002744 ++BlocksWithoutLineNo;
2745 }
2746}
2747#endif
2748
Devang Patel2c4ceb12009-11-21 02:48:08 +00002749/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002750/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002751void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002752 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002753 if (!extractScopeInformation()) return;
Devang Patel60b35bd2009-10-06 18:37:31 +00002754
Devang Patel9a31f0f2010-10-25 20:45:32 +00002755#ifndef NDEBUG
2756 CheckLineNumbers(MF);
2757#endif
2758
Devang Pateleac9c072010-04-27 19:46:33 +00002759 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2760 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002761 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002762 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002763
2764 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2765 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002766 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002767 if (FDL.isUnknown()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002768
Devang Patele9f8f5e2010-05-07 20:54:48 +00002769 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings0db42712010-07-19 23:56:30 +00002770 const MDNode *TheScope = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002771
Chris Lattnerde4845c2010-04-02 19:42:39 +00002772 DISubprogram SP = getDISubprogram(Scope);
2773 unsigned Line, Col;
2774 if (SP.Verify()) {
2775 Line = SP.getLineNumber();
2776 Col = 0;
Stuart Hastings0db42712010-07-19 23:56:30 +00002777 TheScope = SP;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002778 } else {
2779 Line = FDL.getLine();
2780 Col = FDL.getCol();
Stuart Hastings0db42712010-07-19 23:56:30 +00002781 TheScope = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002782 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002783
Stuart Hastings0db42712010-07-19 23:56:30 +00002784 recordSourceLine(Line, Col, TheScope);
Devang Patelb2b31a62010-05-26 19:37:24 +00002785
Devang Patelb9abe9f2010-06-02 16:42:51 +00002786 /// ProcessedArgs - Collection of arguments already processed.
2787 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2788
Devang Patelb2b31a62010-05-26 19:37:24 +00002789 DebugLoc PrevLoc;
2790 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2791 I != E; ++I)
2792 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2793 II != IE; ++II) {
2794 const MachineInstr *MI = II;
2795 DebugLoc DL = MI->getDebugLoc();
2796 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002797 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2798 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2799 if (!DV.Verify()) continue;
Devang Patel55e97172010-05-27 16:47:30 +00002800 // If DBG_VALUE is for a local variable then it needs a label.
Devang Patelb7313e22010-12-06 22:48:22 +00002801 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
Devang Patelb2b31a62010-05-26 19:37:24 +00002802 InsnNeedsLabel.insert(MI);
Devang Patel55e97172010-05-27 16:47:30 +00002803 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel7fb231c2010-07-01 21:38:08 +00002804 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2805 describes(MF->getFunction()))
Devang Patel55e97172010-05-27 16:47:30 +00002806 InsnNeedsLabel.insert(MI);
2807 // DBG_VALUE indicating argument location change needs a label.
Devang Patelb7313e22010-12-06 22:48:22 +00002808 else if (!ProcessedArgs.insert(DV))
Devang Patelb2b31a62010-05-26 19:37:24 +00002809 InsnNeedsLabel.insert(MI);
2810 } else {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002811 // If location is unknown then instruction needs a location only if
Devang Patelb2b31a62010-05-26 19:37:24 +00002812 // UnknownLocations flag is set.
2813 if (DL.isUnknown()) {
2814 if (UnknownLocations && !PrevLoc.isUnknown())
2815 InsnNeedsLabel.insert(MI);
2816 } else if (DL != PrevLoc)
2817 // Otherwise, instruction needs a location only if it is new location.
2818 InsnNeedsLabel.insert(MI);
2819 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002820
Devang Patelb2b31a62010-05-26 19:37:24 +00002821 if (!DL.isUnknown() || UnknownLocations)
2822 PrevLoc = DL;
2823 }
2824
2825 PrevLabel = FunctionBeginSym;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002826}
2827
Devang Patel2c4ceb12009-11-21 02:48:08 +00002828/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002829///
Chris Lattnereec791a2010-01-26 23:18:02 +00002830void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002831 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002832
Devang Patel344130e2010-01-04 20:44:00 +00002833 if (CurrentFnDbgScope) {
Devang Patel65eb4822010-05-22 00:04:14 +00002834
Devang Patelc3f5f782010-05-25 23:40:22 +00002835 // Define end label for subprogram.
2836 FunctionEndSym = Asm->GetTempSymbol("func_end",
2837 Asm->getFunctionNumber());
2838 // Assumes in correct section after the entry point.
2839 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002840
Devang Patel78e127d2010-06-25 22:07:34 +00002841 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2842 collectVariableInfo(MF, ProcessedVars);
Devang Patel65eb4822010-05-22 00:04:14 +00002843
Devang Patel344130e2010-01-04 20:44:00 +00002844 // Construct abstract scopes.
2845 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel78e127d2010-06-25 22:07:34 +00002846 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel78e127d2010-06-25 22:07:34 +00002847 DISubprogram SP((*AI)->getScopeNode());
2848 if (SP.Verify()) {
2849 // Collect info for variables that were optimized out.
2850 StringRef FName = SP.getLinkageName();
2851 if (FName.empty())
2852 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002853 if (NamedMDNode *NMD =
2854 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
Devang Patel78e127d2010-06-25 22:07:34 +00002855 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002856 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00002857 if (!DV || !ProcessedVars.insert(DV))
2858 continue;
Devang Patel1d68d212010-06-30 00:11:08 +00002859 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel78e127d2010-06-25 22:07:34 +00002860 if (Scope)
2861 Scope->addVariable(new DbgVariable(DV));
2862 }
2863 }
2864 }
Devang Patel90e19aa2010-06-30 01:40:11 +00002865 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2866 constructScopeDIE(*AI);
Devang Patel78e127d2010-06-25 22:07:34 +00002867 }
Devang Patel61409622010-07-07 20:12:52 +00002868
Devang Patel9c488372010-05-04 06:15:30 +00002869 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002870
Devang Patel9c488372010-05-04 06:15:30 +00002871 if (!DisableFramePointerElim(*MF))
Jim Grosbach1e20b962010-07-21 21:21:52 +00002872 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
Devang Patel9c488372010-05-04 06:15:30 +00002873 dwarf::DW_FORM_flag, 1);
2874
2875
Chris Lattnerd38fee82010-04-05 00:13:49 +00002876 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002877 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002878 }
2879
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002880 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002881 CurrentFnDbgScope = NULL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002882 InsnNeedsLabel.clear();
Devang Patel26c1e562010-05-20 16:36:41 +00002883 DbgVariableToFrameIndexMap.clear();
2884 VarToAbstractVarMap.clear();
2885 DbgVariableToDbgInstMap.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002886 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002887 InsnsEndScopeSet.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002888 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002889 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002890 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002891 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002892 LabelsBeforeInsn.clear();
2893 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002894 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002895}
2896
Devang Patel26c1e562010-05-20 16:36:41 +00002897/// recordVariableFrameIndex - Record a variable's index.
2898void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2899 assert (V && "Invalid DbgVariable!");
2900 DbgVariableToFrameIndexMap[V] = Index;
2901}
2902
2903/// findVariableFrameIndex - Return true if frame index for the variable
2904/// is found. Update FI to hold value of the index.
2905bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2906 assert (V && "Invalid DbgVariable!");
2907 DenseMap<const DbgVariable *, int>::iterator I =
2908 DbgVariableToFrameIndexMap.find(V);
2909 if (I == DbgVariableToFrameIndexMap.end())
2910 return false;
2911 *FI = I->second;
2912 return true;
2913}
2914
Jim Grosbach1e20b962010-07-21 21:21:52 +00002915/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patelee432862010-05-20 19:57:06 +00002916/// instruction.
2917DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2918 DbgScope *Scope = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002919 LLVMContext &Ctx =
Devang Patelee432862010-05-20 19:57:06 +00002920 MInsn->getParent()->getParent()->getFunction()->getContext();
2921 DebugLoc DL = MInsn->getDebugLoc();
2922
Jim Grosbach1e20b962010-07-21 21:21:52 +00002923 if (DL.isUnknown())
Devang Patelee432862010-05-20 19:57:06 +00002924 return Scope;
2925
2926 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2927 Scope = ConcreteScopes.lookup(IA);
2928 if (Scope == 0)
2929 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002930
Devang Patelee432862010-05-20 19:57:06 +00002931 return Scope;
2932}
2933
2934
Chris Lattnerc6087842010-03-09 04:54:43 +00002935/// recordSourceLine - Register a source line with debug info. Returns the
2936/// unique label that was emitted and which provides correspondence to
2937/// the source line list.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002938MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel61409622010-07-07 20:12:52 +00002939 const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00002940 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002941
Dan Gohman1cc0d622010-05-05 23:41:32 +00002942 unsigned Src = 1;
2943 if (S) {
2944 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002945
Dan Gohman1cc0d622010-05-05 23:41:32 +00002946 if (Scope.isCompileUnit()) {
2947 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002948 Fn = CU.getFilename();
Devang Patel3cabc9d2010-10-28 17:30:52 +00002949 } else if (Scope.isFile()) {
2950 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00002951 Fn = F.getFilename();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002952 } else if (Scope.isSubprogram()) {
2953 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002954 Fn = SP.getFilename();
2955 } else if (Scope.isLexicalBlock()) {
2956 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002957 Fn = DB.getFilename();
2958 } else
2959 assert(0 && "Unexpected scope info");
2960
Rafael Espindola5c055632010-11-18 02:04:25 +00002961 Src = GetOrCreateSourceID(Fn);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002962 }
2963
Rafael Espindola5c055632010-11-18 02:04:25 +00002964 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, DWARF2_FLAG_IS_STMT,
2965 0, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002966
Rafael Espindola5c055632010-11-18 02:04:25 +00002967 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattnerc6087842010-03-09 04:54:43 +00002968 Asm->OutStreamer.EmitLabel(Label);
2969 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002970}
2971
Bill Wendling829e67b2009-05-20 23:22:40 +00002972//===----------------------------------------------------------------------===//
2973// Emit Methods
2974//===----------------------------------------------------------------------===//
2975
Devang Patel2c4ceb12009-11-21 02:48:08 +00002976/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002977///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002978unsigned
2979DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002980 // Get the children.
2981 const std::vector<DIE *> &Children = Die->getChildren();
2982
2983 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002984 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00002985 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00002986
2987 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002988 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002989
2990 // Get the abbreviation for this DIE.
2991 unsigned AbbrevNumber = Die->getAbbrevNumber();
2992 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2993
2994 // Set DIE offset
2995 Die->setOffset(Offset);
2996
2997 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002998 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002999
3000 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
3001 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3002
3003 // Size the DIE attribute values.
3004 for (unsigned i = 0, N = Values.size(); i < N; ++i)
3005 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00003006 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00003007
3008 // Size the DIE children if any.
3009 if (!Children.empty()) {
3010 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
3011 "Children flag not set");
3012
3013 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003014 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00003015
3016 // End of children marker.
3017 Offset += sizeof(int8_t);
3018 }
3019
3020 Die->setSize(Offset - Die->getOffset());
3021 return Offset;
3022}
3023
Devang Patel2c4ceb12009-11-21 02:48:08 +00003024/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00003025///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003026void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00003027 unsigned PrevOffset = 0;
3028 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3029 E = CUMap.end(); I != E; ++I) {
3030 // Compute size of compile unit header.
3031 static unsigned Offset = PrevOffset +
3032 sizeof(int32_t) + // Length of Compilation Unit Info
3033 sizeof(int16_t) + // DWARF version number
3034 sizeof(int32_t) + // Offset Into Abbrev. Section
3035 sizeof(int8_t); // Pointer Size (in bytes)
3036 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
3037 PrevOffset = Offset;
3038 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003039}
3040
Chris Lattner11b8f302010-04-04 23:02:02 +00003041/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
3042/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00003043static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00003044 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003045 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00003046 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003047
Chris Lattner9c69e285532010-04-04 22:59:04 +00003048 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
3049 Asm->OutStreamer.EmitLabel(TmpSym);
3050 return TmpSym;
3051}
3052
3053/// EmitSectionLabels - Emit initial Dwarf sections with a label at
3054/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00003055void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003056 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003057
Bill Wendling94d04b82009-05-20 23:21:38 +00003058 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003059 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003060 DwarfFrameSectionSym =
3061 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3062 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003063
Jim Grosbach1e20b962010-07-21 21:21:52 +00003064 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003065 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003066 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003067 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00003068 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003069
Chris Lattner9c69e285532010-04-04 22:59:04 +00003070 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00003071 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003072
Devang Patelaf608bd2010-08-24 00:06:12 +00003073 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00003074 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3075 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3076 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003077 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003078 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00003079 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3080 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00003081
Devang Patelc3f5f782010-05-25 23:40:22 +00003082 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3083 "section_debug_loc");
3084
Chris Lattner9c69e285532010-04-04 22:59:04 +00003085 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00003086 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003087}
3088
Devang Patel2c4ceb12009-11-21 02:48:08 +00003089/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00003090///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003091void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003092 // Get the abbreviation for this DIE.
3093 unsigned AbbrevNumber = Die->getAbbrevNumber();
3094 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3095
Bill Wendling94d04b82009-05-20 23:21:38 +00003096 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00003097 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00003098 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3099 Twine::utohexstr(Die->getOffset()) + ":0x" +
3100 Twine::utohexstr(Die->getSize()) + " " +
3101 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003102 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003103
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003104 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00003105 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3106
3107 // Emit the DIE attribute values.
3108 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3109 unsigned Attr = AbbrevData[i].getAttribute();
3110 unsigned Form = AbbrevData[i].getForm();
3111 assert(Form && "Too many attributes for DIE (check abbreviation)");
3112
Chris Lattner3f53c832010-04-04 18:52:31 +00003113 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00003114 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003115
Bill Wendling94d04b82009-05-20 23:21:38 +00003116 switch (Attr) {
3117 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00003118 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003119 break;
3120 case dwarf::DW_AT_abstract_origin: {
3121 DIEEntry *E = cast<DIEEntry>(Values[i]);
3122 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00003123 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00003124 Asm->EmitInt32(Addr);
3125 break;
3126 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003127 case dwarf::DW_AT_ranges: {
3128 // DW_AT_range Value encodes offset in debug_range section.
3129 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00003130
3131 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
3132 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
3133 V->getValue(),
3134 4);
3135 } else {
3136 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3137 V->getValue(),
3138 DwarfDebugRangeSectionSym,
3139 4);
3140 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003141 break;
3142 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003143 case dwarf::DW_AT_location: {
3144 if (UseDotDebugLocEntry.count(Die) != 0) {
3145 DIELabel *L = cast<DIELabel>(Values[i]);
3146 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3147 } else
3148 Values[i]->EmitValue(Asm, Form);
3149 break;
3150 }
Devang Patel2a361602010-09-29 19:08:08 +00003151 case dwarf::DW_AT_accessibility: {
3152 if (Asm->isVerbose()) {
3153 DIEInteger *V = cast<DIEInteger>(Values[i]);
3154 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
3155 }
3156 Values[i]->EmitValue(Asm, Form);
3157 break;
3158 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003159 default:
3160 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003161 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00003162 break;
3163 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003164 }
3165
3166 // Emit the DIE children if any.
3167 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3168 const std::vector<DIE *> &Children = Die->getChildren();
3169
3170 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003171 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00003172
Chris Lattner3f53c832010-04-04 18:52:31 +00003173 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00003174 Asm->OutStreamer.AddComment("End Of Children Mark");
3175 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003176 }
3177}
3178
Devang Patel8a241142009-12-09 18:24:21 +00003179/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003180///
Devang Patel8a241142009-12-09 18:24:21 +00003181void DwarfDebug::emitDebugInfo() {
3182 // Start debug info section.
3183 Asm->OutStreamer.SwitchSection(
3184 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00003185 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3186 E = CUMap.end(); I != E; ++I) {
3187 CompileUnit *TheCU = I->second;
3188 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00003189
Devang Patel163a9f72010-05-10 22:49:55 +00003190 // Emit the compile units header.
3191 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3192 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003193
Devang Patel163a9f72010-05-10 22:49:55 +00003194 // Emit size of content not including length itself
3195 unsigned ContentSize = Die->getSize() +
3196 sizeof(int16_t) + // DWARF version number
3197 sizeof(int32_t) + // Offset Into Abbrev. Section
3198 sizeof(int8_t) + // Pointer Size (in bytes)
3199 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003200
Devang Patel163a9f72010-05-10 22:49:55 +00003201 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3202 Asm->EmitInt32(ContentSize);
3203 Asm->OutStreamer.AddComment("DWARF version number");
3204 Asm->EmitInt16(dwarf::DWARF_VERSION);
3205 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3206 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3207 DwarfAbbrevSectionSym);
3208 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3209 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003210
Devang Patel163a9f72010-05-10 22:49:55 +00003211 emitDIE(Die);
3212 // FIXME - extra padding for gdb bug.
3213 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3214 Asm->EmitInt8(0);
3215 Asm->EmitInt8(0);
3216 Asm->EmitInt8(0);
3217 Asm->EmitInt8(0);
3218 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3219 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003220}
3221
Devang Patel2c4ceb12009-11-21 02:48:08 +00003222/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003223///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003224void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00003225 // Check to see if it is worth the effort.
3226 if (!Abbreviations.empty()) {
3227 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003228 Asm->OutStreamer.SwitchSection(
3229 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003230
Chris Lattnerc0215722010-04-04 19:25:43 +00003231 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003232
3233 // For each abbrevation.
3234 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3235 // Get abbreviation data
3236 const DIEAbbrev *Abbrev = Abbreviations[i];
3237
3238 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003239 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00003240
3241 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003242 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00003243 }
3244
3245 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003246 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00003247
Chris Lattnerc0215722010-04-04 19:25:43 +00003248 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003249 }
3250}
3251
Devang Patel2c4ceb12009-11-21 02:48:08 +00003252/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00003253/// the line matrix.
3254///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003255void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003256 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00003257 Asm->OutStreamer.AddComment("Extended Op");
3258 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003259
Chris Lattner233f52b2010-03-09 23:52:58 +00003260 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003261 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003262 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3263 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3264
3265 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003266
Chris Lattnerc0215722010-04-04 19:25:43 +00003267 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003268 Asm->getTargetData().getPointerSize(),
3269 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003270
3271 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00003272 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3273 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00003274 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00003275 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003276}
3277
Devang Patel2c4ceb12009-11-21 02:48:08 +00003278/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003279///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003280void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003281 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003282 return;
3283
Chris Lattnerd38fee82010-04-05 00:13:49 +00003284 int stackGrowth = Asm->getTargetData().getPointerSize();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003285 if (Asm->TM.getFrameLowering()->getStackGrowthDirection() ==
3286 TargetFrameLowering::StackGrowsDown)
Chris Lattnerd38fee82010-04-05 00:13:49 +00003287 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003288
3289 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003290 Asm->OutStreamer.SwitchSection(
3291 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003292
Chris Lattnerc0215722010-04-04 19:25:43 +00003293 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003294 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003295 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3296 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003297
Chris Lattnerc0215722010-04-04 19:25:43 +00003298 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003299 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003300 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003301 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003302 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003303 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003304 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003305 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3306 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003307 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003308 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003309 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Bill Wendling94d04b82009-05-20 23:21:38 +00003310 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003311
3312 std::vector<MachineMove> Moves;
Anton Korobeynikovd9e33852010-11-18 23:25:52 +00003313 TFI->getInitialFrameState(Moves);
Bill Wendling94d04b82009-05-20 23:21:38 +00003314
Chris Lattner02b86b92010-04-04 23:41:46 +00003315 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003316
Chris Lattner059ea132010-04-28 01:05:45 +00003317 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003318 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003319}
3320
Devang Patel2c4ceb12009-11-21 02:48:08 +00003321/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003322/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003323void DwarfDebug::
3324emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003325 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003326 return;
3327
3328 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003329 Asm->OutStreamer.SwitchSection(
3330 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003331
Chris Lattner233f52b2010-03-09 23:52:58 +00003332 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003333 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003334 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003335 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003336 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003337 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003338
Chris Lattner206d61e2010-03-13 07:26:18 +00003339 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003340
Chris Lattner233f52b2010-03-09 23:52:58 +00003341 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003342 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner6189ed12010-04-04 23:25:33 +00003343 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003344
Chris Lattner233f52b2010-03-09 23:52:58 +00003345 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003346 MCSymbol *FuncBeginSym =
3347 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003348 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003349 Asm->getTargetData().getPointerSize(),
3350 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003351
3352
Chris Lattner233f52b2010-03-09 23:52:58 +00003353 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003354 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003355 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003356
Chris Lattner02b86b92010-04-04 23:41:46 +00003357 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003358
Chris Lattner059ea132010-04-28 01:05:45 +00003359 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003360 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003361}
3362
Devang Patel8a241142009-12-09 18:24:21 +00003363/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3364///
3365void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00003366 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3367 E = CUMap.end(); I != E; ++I) {
3368 CompileUnit *TheCU = I->second;
3369 // Start the dwarf pubnames section.
3370 Asm->OutStreamer.SwitchSection(
3371 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003372
Devang Patel163a9f72010-05-10 22:49:55 +00003373 Asm->OutStreamer.AddComment("Length of Public Names Info");
3374 Asm->EmitLabelDifference(
3375 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3376 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003377
Devang Patel163a9f72010-05-10 22:49:55 +00003378 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3379 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003380
Devang Patel163a9f72010-05-10 22:49:55 +00003381 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003382 Asm->EmitInt16(dwarf::DWARF_VERSION);
3383
Devang Patel163a9f72010-05-10 22:49:55 +00003384 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003385 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00003386 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003387
Devang Patel163a9f72010-05-10 22:49:55 +00003388 Asm->OutStreamer.AddComment("Compilation Unit Length");
3389 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3390 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3391 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003392
Devang Patel163a9f72010-05-10 22:49:55 +00003393 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3394 for (StringMap<DIE*>::const_iterator
3395 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3396 const char *Name = GI->getKeyData();
3397 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003398
Devang Patel163a9f72010-05-10 22:49:55 +00003399 Asm->OutStreamer.AddComment("DIE offset");
3400 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003401
Devang Patel163a9f72010-05-10 22:49:55 +00003402 if (Asm->isVerbose())
3403 Asm->OutStreamer.AddComment("External Name");
3404 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3405 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003406
Devang Patel163a9f72010-05-10 22:49:55 +00003407 Asm->OutStreamer.AddComment("End Mark");
3408 Asm->EmitInt32(0);
3409 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3410 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003411 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003412}
3413
Devang Patel193f7202009-11-24 01:14:22 +00003414void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00003415 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3416 E = CUMap.end(); I != E; ++I) {
3417 CompileUnit *TheCU = I->second;
3418 // Start the dwarf pubnames section.
3419 Asm->OutStreamer.SwitchSection(
3420 Asm->getObjFileLowering().getDwarfPubTypesSection());
3421 Asm->OutStreamer.AddComment("Length of Public Types Info");
3422 Asm->EmitLabelDifference(
3423 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3424 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003425
Devang Patel163a9f72010-05-10 22:49:55 +00003426 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3427 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003428
Devang Patel163a9f72010-05-10 22:49:55 +00003429 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3430 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003431
Devang Patel163a9f72010-05-10 22:49:55 +00003432 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3433 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3434 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003435
Devang Patel163a9f72010-05-10 22:49:55 +00003436 Asm->OutStreamer.AddComment("Compilation Unit Length");
3437 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3438 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3439 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003440
Devang Patel163a9f72010-05-10 22:49:55 +00003441 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3442 for (StringMap<DIE*>::const_iterator
3443 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3444 const char *Name = GI->getKeyData();
3445 DIE * Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003446
Devang Patel163a9f72010-05-10 22:49:55 +00003447 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3448 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003449
Devang Patel163a9f72010-05-10 22:49:55 +00003450 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3451 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3452 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003453
Devang Patel163a9f72010-05-10 22:49:55 +00003454 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003455 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00003456 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3457 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003458 }
Devang Patel193f7202009-11-24 01:14:22 +00003459}
3460
Devang Patel2c4ceb12009-11-21 02:48:08 +00003461/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003462///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003463void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003464 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003465 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003466
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003467 // Start the dwarf str section.
3468 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003469 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003470
Chris Lattnerbc733f52010-03-13 02:17:42 +00003471 // Get all of the string pool entries and put them in an array by their ID so
3472 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003473 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00003474 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003475
Chris Lattnerbc733f52010-03-13 02:17:42 +00003476 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3477 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3478 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003479
Chris Lattnerbc733f52010-03-13 02:17:42 +00003480 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003481
Chris Lattnerbc733f52010-03-13 02:17:42 +00003482 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003483 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003484 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003485
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003486 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003487 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003488 }
3489}
3490
Devang Patel2c4ceb12009-11-21 02:48:08 +00003491/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003492///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003493void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00003494 if (DotDebugLocEntries.empty())
3495 return;
3496
Bill Wendling94d04b82009-05-20 23:21:38 +00003497 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003498 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00003499 Asm->getObjFileLowering().getDwarfLocSection());
3500 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00003501 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3502 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003503 for (SmallVector<DotDebugLocEntry, 4>::iterator
3504 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00003505 I != E; ++I, ++index) {
Devang Patelc3f5f782010-05-25 23:40:22 +00003506 DotDebugLocEntry Entry = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00003507 if (Entry.isEmpty()) {
3508 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3509 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00003510 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00003511 } else {
3512 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3513 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3514 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3515 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patelb2cf5812010-08-04 18:40:52 +00003516 if (int Offset = Entry.Loc.getOffset()) {
3517 // If the value is at a certain offset from frame register then
3518 // use DW_OP_fbreg.
3519 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patelc3f5f782010-05-25 23:40:22 +00003520 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patelb2cf5812010-08-04 18:40:52 +00003521 Asm->EmitInt16(1 + OffsetSize);
3522 Asm->OutStreamer.AddComment(
3523 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
3524 Asm->EmitInt8(dwarf::DW_OP_fbreg);
3525 Asm->OutStreamer.AddComment("Offset");
3526 Asm->EmitSLEB128(Offset);
Devang Patelc3f5f782010-05-25 23:40:22 +00003527 } else {
Devang Patelb2cf5812010-08-04 18:40:52 +00003528 if (Reg < 32) {
3529 Asm->OutStreamer.AddComment("Loc expr size");
3530 Asm->EmitInt16(1);
3531 Asm->OutStreamer.AddComment(
Devang Patela54e0cc2010-08-04 20:32:36 +00003532 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patelb2cf5812010-08-04 18:40:52 +00003533 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3534 } else {
3535 Asm->OutStreamer.AddComment("Loc expr size");
3536 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
3537 Asm->EmitInt8(dwarf::DW_OP_regx);
3538 Asm->EmitULEB128(Reg);
3539 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003540 }
3541 }
3542 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003543}
3544
3545/// EmitDebugARanges - Emit visible names into a debug aranges section.
3546///
3547void DwarfDebug::EmitDebugARanges() {
3548 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003549 Asm->OutStreamer.SwitchSection(
3550 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003551}
3552
Devang Patel2c4ceb12009-11-21 02:48:08 +00003553/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003554///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003555void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003556 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003557 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003558 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003559 unsigned char Size = Asm->getTargetData().getPointerSize();
3560 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00003561 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00003562 I != E; ++I) {
3563 if (*I)
3564 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003565 else
Devang Pateleac9c072010-04-27 19:46:33 +00003566 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003567 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003568}
3569
Devang Patel2c4ceb12009-11-21 02:48:08 +00003570/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003571///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003572void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003573 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003574 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003575 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003576 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003577 }
3578}
3579
Devang Patel2c4ceb12009-11-21 02:48:08 +00003580/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003581/// Section Header:
3582/// 1. length of section
3583/// 2. Dwarf version number
3584/// 3. address size.
3585///
3586/// Entries (one "entry" for each function that was inlined):
3587///
3588/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3589/// otherwise offset into __debug_str for regular function name.
3590/// 2. offset into __debug_str section for regular function name.
3591/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3592/// instances for the function.
3593///
3594/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3595/// inlined instance; the die_offset points to the inlined_subroutine die in the
3596/// __debug_info section, and the low_pc is the starting address for the
3597/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003598void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003599 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003600 return;
3601
Devang Patel163a9f72010-05-10 22:49:55 +00003602 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003603 return;
3604
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003605 Asm->OutStreamer.SwitchSection(
3606 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003607
Chris Lattner233f52b2010-03-09 23:52:58 +00003608 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003609 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3610 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003611
Chris Lattnerc0215722010-04-04 19:25:43 +00003612 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003613
Chris Lattner233f52b2010-03-09 23:52:58 +00003614 Asm->OutStreamer.AddComment("Dwarf Version");
3615 Asm->EmitInt16(dwarf::DWARF_VERSION);
3616 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003617 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003618
Devang Patele9f8f5e2010-05-07 20:54:48 +00003619 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003620 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003621
Devang Patele9f8f5e2010-05-07 20:54:48 +00003622 const MDNode *Node = *I;
3623 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003624 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003625 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003626 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003627 StringRef LName = SP.getLinkageName();
3628 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003629
Chris Lattner233f52b2010-03-09 23:52:58 +00003630 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003631 if (LName.empty()) {
3632 Asm->OutStreamer.EmitBytes(Name, 0);
3633 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003634 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003635 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3636 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003637
Chris Lattner233f52b2010-03-09 23:52:58 +00003638 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003639 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003640 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003641
Devang Patel53bb5c92009-11-10 23:06:00 +00003642 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003643 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003644 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003645 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003646
Chris Lattner3f53c832010-04-04 18:52:31 +00003647 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003648 Asm->OutStreamer.EmitSymbolValue(LI->first,
3649 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003650 }
3651 }
3652
Chris Lattnerc0215722010-04-04 19:25:43 +00003653 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003654}