blob: f2c7238f5fa782033f6bd2adfb27a80273a8b954 [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendling0310d762009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendling0310d762009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000032#include "llvm/Analysis/DebugInfo.h"
Devang Patel9a31f0f2010-10-25 20:45:32 +000033#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000034#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000035#include "llvm/ADT/StringExtras.h"
Devang Pateleac9c072010-04-27 19:46:33 +000036#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000039#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000040#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000041#include "llvm/Support/Timer.h"
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 Korobeynikov82f58742010-11-20 15:59:32 +0000597 const TargetFrameInfo *TFI = Asm->TM.getFrameInfo();
598 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 {
629 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000630 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
631 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000632 }
633 } else {
634 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000635 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000636 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000637 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
638 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000639 }
640
Devang Patel2c4ceb12009-11-21 02:48:08 +0000641 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000642 }
643
Devang Patel8bd11de2010-08-09 21:01:39 +0000644 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
645 uint64_t Element = DV->getAddrElement(i);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000646
647 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000648 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel8bd11de2010-08-09 21:01:39 +0000649 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000650 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000651 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000652 } else llvm_unreachable("unknown DIFactory Opcode");
653 }
654
655 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000656 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000657}
658
Caroline Ticedc8f6042009-08-31 21:19:37 +0000659/* Byref variables, in Blocks, are declared by the programmer as "SomeType
660 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
661 gives the variable VarName either the struct, or a pointer to the struct, as
662 its type. This is necessary for various behind-the-scenes things the
663 compiler needs to do with by-reference variables in Blocks.
664
665 However, as far as the original *programmer* is concerned, the variable
666 should still have type 'SomeType', as originally declared.
667
Devang Patel2c4ceb12009-11-21 02:48:08 +0000668 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000669 struct to find the original type of the variable, which is then assigned to
670 the variable's Debug Information Entry as its real type. So far, so good.
671 However now the debugger will expect the variable VarName to have the type
672 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000673 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000674 pointers and struct to find the actual variable of type SomeType.
675
676 The following function does just that. We start by getting
677 the "normal" location for the variable. This will be the location
678 of either the struct __Block_byref_x_VarName or the pointer to the
679 struct __Block_byref_x_VarName.
680
681 The struct will look something like:
682
683 struct __Block_byref_x_VarName {
684 ... <various fields>
685 struct __Block_byref_x_VarName *forwarding;
686 ... <various other fields>
687 SomeType VarName;
688 ... <maybe more fields>
689 };
690
691 If we are given the struct directly (as our starting point) we
692 need to tell the debugger to:
693
694 1). Add the offset of the forwarding field.
695
Dan Gohmanf451cb82010-02-10 16:03:48 +0000696 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000697 struct to use (the real one may have been copied onto the heap).
698
699 3). Add the offset for the field VarName, to find the actual variable.
700
701 If we started with a pointer to the struct, then we need to
702 dereference that pointer first, before the other steps.
703 Translating this into DWARF ops, we will need to append the following
704 to the current location description for the variable:
705
706 DW_OP_deref -- optional, if we start with a pointer
707 DW_OP_plus_uconst <forward_fld_offset>
708 DW_OP_deref
709 DW_OP_plus_uconst <varName_fld_offset>
710
711 That is what this function does. */
712
Devang Patel2c4ceb12009-11-21 02:48:08 +0000713/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000714/// provided, and generate the DWARF information necessary to find the
715/// actual Block variable (navigating the Block struct) based on the
716/// starting location. Add the DWARF information to the die. For
717/// more information, read large comment just above here.
718///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000719void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000720 unsigned Attribute,
721 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000722 DIType Ty = DV->getType();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000723 DIType TmpTy = Ty;
724 unsigned Tag = Ty.getTag();
725 bool isPointer = false;
726
Devang Patel8bd11de2010-08-09 21:01:39 +0000727 StringRef varName = DV->getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000728
729 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000730 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000731 TmpTy = DTy.getTypeDerivedFrom();
732 isPointer = true;
733 }
734
Devang Patel2db49d72010-05-07 18:11:54 +0000735 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000736
Daniel Dunbar00564992009-09-19 20:40:14 +0000737 // Find the __forwarding field and the variable field in the __Block_byref
738 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000739 DIArray Fields = blockStruct.getTypeArray();
740 DIDescriptor varField = DIDescriptor();
741 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000742
Daniel Dunbar00564992009-09-19 20:40:14 +0000743 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
744 DIDescriptor Element = Fields.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000745 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000746 StringRef fieldName = DT.getName();
747 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000748 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000749 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000750 varField = Element;
751 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000752
Daniel Dunbar00564992009-09-19 20:40:14 +0000753 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000754 unsigned forwardingFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000755 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000756 unsigned varFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000757 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000758
Mike Stump7e3720d2009-09-24 23:21:26 +0000759 // Decode the original location, and use that as the start of the byref
760 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000761 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000762 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000763 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000764
Daniel Dunbar00564992009-09-19 20:40:14 +0000765 if (Location.isReg()) {
766 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000767 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000768 else {
769 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000770 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
771 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000772 }
773 } else {
774 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000775 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000776 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000777 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
778 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000779 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000780
Devang Patel2c4ceb12009-11-21 02:48:08 +0000781 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000782 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000783
Mike Stump7e3720d2009-09-24 23:21:26 +0000784 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000785 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000786 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000787 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000788
Daniel Dunbar00564992009-09-19 20:40:14 +0000789 // Next add the offset for the '__forwarding' field:
790 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
791 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000792 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000793 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
794 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000795 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000796
Daniel Dunbar00564992009-09-19 20:40:14 +0000797 // Now dereference the __forwarding field to get to the real __Block_byref
798 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000799 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000800
Daniel Dunbar00564992009-09-19 20:40:14 +0000801 // Now that we've got the real __Block_byref... struct, add the offset
802 // for the variable's field to get to the location of the actual variable:
803 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000804 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000805 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
806 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000807 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000808
Daniel Dunbar00564992009-09-19 20:40:14 +0000809 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000810 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000811}
812
Devang Patel2c4ceb12009-11-21 02:48:08 +0000813/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000814/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000815void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000816 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000817 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000818 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000819 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelbe90c3a2010-09-22 21:10:38 +0000820
Devang Patelc8821042010-11-02 17:37:00 +0000821 if (RI->getFrameRegister(*Asm->MF) == Location.getReg()
Devang Patelbe90c3a2010-09-22 21:10:38 +0000822 && Location.getOffset()) {
823 // If variable offset is based in frame register then use fbreg.
824 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
825 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
826 addBlock(Die, Attribute, 0, Block);
827 return;
828 }
Bill Wendling0310d762009-05-15 09:23:25 +0000829
830 if (Location.isReg()) {
831 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000832 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000833 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000834 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
835 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000836 }
837 } else {
838 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000839 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000840 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000841 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
842 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000843 }
844
Devang Patel2c4ceb12009-11-21 02:48:08 +0000845 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000846 }
847
Devang Patel2c4ceb12009-11-21 02:48:08 +0000848 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000849}
850
Devang Patela43098d2010-04-28 01:03:09 +0000851/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patel522ad742010-11-12 23:20:42 +0000852bool DwarfDebug::addRegisterAddress(DIE *Die, const MachineOperand &MO) {
Devang Patela43098d2010-04-28 01:03:09 +0000853 assert (MO.isReg() && "Invalid machine operand!");
854 if (!MO.getReg())
855 return false;
856 MachineLocation Location;
857 Location.set(MO.getReg());
858 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patela43098d2010-04-28 01:03:09 +0000859 return true;
860}
861
862/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel522ad742010-11-12 23:20:42 +0000863bool DwarfDebug::addConstantValue(DIE *Die, const MachineOperand &MO) {
Devang Patela43098d2010-04-28 01:03:09 +0000864 assert (MO.isImm() && "Invalid machine operand!");
865 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
866 unsigned Imm = MO.getImm();
867 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
868 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patela43098d2010-04-28 01:03:09 +0000869 return true;
870}
871
872/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patel522ad742010-11-12 23:20:42 +0000873bool DwarfDebug::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Devang Patela43098d2010-04-28 01:03:09 +0000874 assert (MO.isFPImm() && "Invalid machine operand!");
875 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
876 APFloat FPImm = MO.getFPImm()->getValueAPF();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000877
Devang Patela43098d2010-04-28 01:03:09 +0000878 // Get the raw data form of the floating point.
879 const APInt FltVal = FPImm.bitcastToAPInt();
880 const char *FltPtr = (const char*)FltVal.getRawData();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000881
Devang Patela43098d2010-04-28 01:03:09 +0000882 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
883 bool LittleEndian = Asm->getTargetData().isLittleEndian();
884 int Incr = (LittleEndian ? 1 : -1);
885 int Start = (LittleEndian ? 0 : NumBytes - 1);
886 int Stop = (LittleEndian ? NumBytes : -1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000887
Devang Patela43098d2010-04-28 01:03:09 +0000888 // Output the constant to DWARF one byte at a time.
889 for (; Start != Stop; Start += Incr)
890 addUInt(Block, 0, dwarf::DW_FORM_data1,
891 (unsigned char)0xFF & FltPtr[Start]);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000892
Devang Patela43098d2010-04-28 01:03:09 +0000893 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000894 return true;
Devang Patela43098d2010-04-28 01:03:09 +0000895}
896
897
Devang Patelc366f832009-12-10 19:14:49 +0000898/// addToContextOwner - Add Die into the list of its context owner's children.
899void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000900 if (Context.isType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000901 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patelc366f832009-12-10 19:14:49 +0000902 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000903 } else if (Context.isNameSpace()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000904 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel6404e4e2009-12-15 19:16:48 +0000905 ContextDIE->addChild(Die);
Stuart Hastings215aa152010-06-11 20:08:44 +0000906 } else if (Context.isSubprogram()) {
Devang Patelee70fa72010-09-27 23:15:27 +0000907 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context));
Stuart Hastings215aa152010-06-11 20:08:44 +0000908 ContextDIE->addChild(Die);
Devang Patel163a9f72010-05-10 22:49:55 +0000909 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patelc366f832009-12-10 19:14:49 +0000910 ContextDIE->addChild(Die);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000911 else
Devang Patel163a9f72010-05-10 22:49:55 +0000912 getCompileUnit(Context)->addDie(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000913}
914
Devang Patel16ced732009-12-10 18:05:33 +0000915/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
916/// given DIType.
917DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel163a9f72010-05-10 22:49:55 +0000918 CompileUnit *TypeCU = getCompileUnit(Ty);
919 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel16ced732009-12-10 18:05:33 +0000920 if (TyDIE)
921 return TyDIE;
922
923 // Create new type.
924 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel163a9f72010-05-10 22:49:55 +0000925 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel16ced732009-12-10 18:05:33 +0000926 if (Ty.isBasicType())
Devang Patel2db49d72010-05-07 18:11:54 +0000927 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000928 else if (Ty.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000929 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000930 else {
931 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2db49d72010-05-07 18:11:54 +0000932 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000933 }
934
Devang Patelc366f832009-12-10 19:14:49 +0000935 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000936 return TyDIE;
937}
938
Devang Patel2c4ceb12009-11-21 02:48:08 +0000939/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000940void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9c004872010-05-07 21:45:47 +0000941 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000942 return;
943
944 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000945 CompileUnit *TypeCU = getCompileUnit(Ty);
946 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000947 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000948 if (Entry) {
949 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000950 return;
951 }
952
Bill Wendling0310d762009-05-15 09:23:25 +0000953 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000954 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000955
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000956 // Set up proxy.
957 Entry = createDIEEntry(Buffer);
Devang Patel163a9f72010-05-10 22:49:55 +0000958 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000959
Devang Patel2c4ceb12009-11-21 02:48:08 +0000960 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000961}
962
Devang Patel2c4ceb12009-11-21 02:48:08 +0000963/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000964void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000965 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000966 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000967 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000968 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000969 BTy.getEncoding());
970
971 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000972 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000973 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000974 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000975 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000976}
977
Devang Patel2c4ceb12009-11-21 02:48:08 +0000978/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000979void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000980 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000981 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000982 uint64_t Size = DTy.getSizeInBits() >> 3;
983 unsigned Tag = DTy.getTag();
984
985 // FIXME - Workaround for templates.
986 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
987
988 Buffer.setTag(Tag);
989
990 // Map to main type, void will not have a type.
991 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000992 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000993
994 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000995 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000996 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000997
998 // Add size if non-zero (derived types might be zero-sized.)
999 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001000 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001001
1002 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +00001003 if (!DTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001004 addSourceLine(&Buffer, DTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001005}
1006
Devang Patel2c4ceb12009-11-21 02:48:08 +00001007/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001008void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001009 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +00001010 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001011
1012 uint64_t Size = CTy.getSizeInBits() >> 3;
1013 unsigned Tag = CTy.getTag();
1014 Buffer.setTag(Tag);
1015
1016 switch (Tag) {
1017 case dwarf::DW_TAG_vector_type:
1018 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +00001019 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001020 break;
1021 case dwarf::DW_TAG_enumeration_type: {
1022 DIArray Elements = CTy.getTypeArray();
1023
1024 // Add enumerators to enumeration type.
1025 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1026 DIE *ElemDie = NULL;
Devang Patel2db49d72010-05-07 18:11:54 +00001027 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3c91b052010-03-08 20:52:55 +00001028 if (Enum.isEnumerator()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001029 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001030 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +00001031 }
Bill Wendling0310d762009-05-15 09:23:25 +00001032 }
1033 }
1034 break;
1035 case dwarf::DW_TAG_subroutine_type: {
1036 // Add return type.
1037 DIArray Elements = CTy.getTypeArray();
1038 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2db49d72010-05-07 18:11:54 +00001039 addType(&Buffer, DIType(RTy));
Bill Wendling0310d762009-05-15 09:23:25 +00001040
Devang Pateld6747df2010-10-06 20:50:40 +00001041 bool isPrototyped = true;
Bill Wendling0310d762009-05-15 09:23:25 +00001042 // Add arguments.
1043 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001044 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld6747df2010-10-06 20:50:40 +00001045 if (Ty.isUnspecifiedParameter()) {
1046 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
1047 Buffer.addChild(Arg);
1048 isPrototyped = false;
1049 } else {
1050 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1051 addType(Arg, DIType(Ty));
1052 Buffer.addChild(Arg);
1053 }
Bill Wendling0310d762009-05-15 09:23:25 +00001054 }
Devang Pateld6747df2010-10-06 20:50:40 +00001055 // Add prototype flag.
1056 if (isPrototyped)
1057 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001058 }
1059 break;
1060 case dwarf::DW_TAG_structure_type:
1061 case dwarf::DW_TAG_union_type:
1062 case dwarf::DW_TAG_class_type: {
1063 // Add elements to structure type.
1064 DIArray Elements = CTy.getTypeArray();
1065
1066 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +00001067 unsigned N = Elements.getNumElements();
1068 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +00001069 break;
1070
1071 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +00001072 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001073 DIDescriptor Element = Elements.getElement(i);
1074 DIE *ElemDie = NULL;
Devang Patel1a301232010-09-29 21:44:16 +00001075 if (Element.isSubprogram()) {
1076 DISubprogram SP(Element);
Devang Patel2db49d72010-05-07 18:11:54 +00001077 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel1a301232010-09-29 21:44:16 +00001078 if (SP.isProtected())
1079 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1080 dwarf::DW_ACCESS_protected);
1081 else if (SP.isPrivate())
1082 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1083 dwarf::DW_ACCESS_private);
1084 else
1085 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1086 dwarf::DW_ACCESS_public);
Devang Patel21ea1d52010-10-01 23:31:40 +00001087 if (SP.isExplicit())
1088 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
Devang Patel1a301232010-09-29 21:44:16 +00001089 }
Devang Patel3c91b052010-03-08 20:52:55 +00001090 else if (Element.isVariable()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001091 DIVariable DV(Element);
Devang Patel1ee0cb92010-01-30 01:08:30 +00001092 ElemDie = new DIE(dwarf::DW_TAG_variable);
1093 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1094 DV.getName());
1095 addType(ElemDie, DV.getType());
1096 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1097 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel81625742010-08-09 20:20:05 +00001098 addSourceLine(ElemDie, DV);
Devang Patel3c91b052010-03-08 20:52:55 +00001099 } else if (Element.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001100 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001101 else
1102 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001103 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001104 }
1105
Devang Patela1ba2692009-08-27 23:51:51 +00001106 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001107 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001108
1109 unsigned RLang = CTy.getRunTimeLang();
1110 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001111 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001112 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001113
1114 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel2db49d72010-05-07 18:11:54 +00001115 if (DIDescriptor(ContainingType).isCompositeType())
Jim Grosbach1e20b962010-07-21 21:21:52 +00001116 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel2db49d72010-05-07 18:11:54 +00001117 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastings215aa152010-06-11 20:08:44 +00001118 else {
1119 DIDescriptor Context = CTy.getContext();
1120 addToContextOwner(&Buffer, Context);
1121 }
Bill Wendling0310d762009-05-15 09:23:25 +00001122 break;
1123 }
1124 default:
1125 break;
1126 }
1127
1128 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001129 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001130 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001131
Jim Grosbach1e20b962010-07-21 21:21:52 +00001132 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
Devang Patel61409622010-07-07 20:12:52 +00001133 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1134 {
Bill Wendling0310d762009-05-15 09:23:25 +00001135 // Add size if non-zero (derived types might be zero-sized.)
1136 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001137 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001138 else {
1139 // Add zero size if it is not a forward declaration.
1140 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001141 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001142 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001143 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001144 }
1145
1146 // Add source line info if available.
1147 if (!CTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001148 addSourceLine(&Buffer, CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001149 }
1150}
1151
Devang Patel2c4ceb12009-11-21 02:48:08 +00001152/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1153void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001154 int64_t L = SR.getLo();
1155 int64_t H = SR.getHi();
1156 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1157
Devang Patel2c4ceb12009-11-21 02:48:08 +00001158 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001159 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001160 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001161 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001162
Devang Patel2c4ceb12009-11-21 02:48:08 +00001163 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001164}
1165
Devang Patel2c4ceb12009-11-21 02:48:08 +00001166/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001167void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001168 DICompositeType *CTy) {
1169 Buffer.setTag(dwarf::DW_TAG_array_type);
1170 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001171 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001172
1173 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001174 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001175 DIArray Elements = CTy->getTypeArray();
1176
Devang Patel6f01d9c2009-11-21 00:31:03 +00001177 // Get an anonymous type for index type.
Devang Patel163a9f72010-05-10 22:49:55 +00001178 CompileUnit *TheCU = getCompileUnit(*CTy);
1179 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001180 if (!IdxTy) {
1181 // Construct an anonymous type for index type.
1182 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001183 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1184 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001185 dwarf::DW_ATE_signed);
Devang Patel163a9f72010-05-10 22:49:55 +00001186 TheCU->addDie(IdxTy);
1187 TheCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001188 }
Bill Wendling0310d762009-05-15 09:23:25 +00001189
1190 // Add subranges to array type.
1191 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1192 DIDescriptor Element = Elements.getElement(i);
1193 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2db49d72010-05-07 18:11:54 +00001194 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001195 }
1196}
1197
Devang Patel2c4ceb12009-11-21 02:48:08 +00001198/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001199DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001200 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001201 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001202 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001203 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001204 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001205 return Enumerator;
1206}
1207
Jim Grosbach1e20b962010-07-21 21:21:52 +00001208/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +00001209/// printer to not emit usual symbol prefix before the symbol name is used then
1210/// return linkage name after skipping this special LLVM prefix.
1211static StringRef getRealLinkageName(StringRef LinkageName) {
1212 char One = '\1';
1213 if (LinkageName.startswith(StringRef(&One, 1)))
1214 return LinkageName.substr(1);
1215 return LinkageName;
1216}
1217
Devang Patel2c4ceb12009-11-21 02:48:08 +00001218/// createMemberDIE - Create new member DIE.
Devang Patelecbd8e82010-08-10 04:12:17 +00001219DIE *DwarfDebug::createMemberDIE(DIDerivedType DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001220 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001221 StringRef Name = DT.getName();
1222 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001223 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001224
Devang Patel8a241142009-12-09 18:24:21 +00001225 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001226
Devang Patel81625742010-08-09 20:20:05 +00001227 addSourceLine(MemberDie, DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001228
Benjamin Kramer345ef342010-03-31 19:34:01 +00001229 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001230 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001231
Bill Wendling0310d762009-05-15 09:23:25 +00001232 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001233 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001234
1235 if (Size != FieldSize) {
1236 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001237 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1238 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001239
1240 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001241 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1242 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001243 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001244 Offset -= FieldOffset;
1245
1246 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001247 if (Asm->getTargetData().isLittleEndian())
1248 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001249 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001250
Devang Patel33db5082009-11-04 22:06:12 +00001251 // Here WD_AT_data_member_location points to the anonymous
1252 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001253 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001254
1255 } else
1256 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001257 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001258
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001259 if (DT.getTag() == dwarf::DW_TAG_inheritance
1260 && DT.isVirtual()) {
1261
1262 // For C++, virtual base classes are not at fixed offset. Use following
1263 // expression to extract appropriate offset from vtable.
1264 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1265
Benjamin Kramer345ef342010-03-31 19:34:01 +00001266 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001267 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1268 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1269 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1270 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1271 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1272 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1273 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1274
Jim Grosbach1e20b962010-07-21 21:21:52 +00001275 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001276 VBaseLocationDie);
1277 } else
1278 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001279
1280 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001281 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001282 dwarf::DW_ACCESS_protected);
1283 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001284 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001285 dwarf::DW_ACCESS_private);
Devang Patel2a361602010-09-29 19:08:08 +00001286 // Otherwise C++ member and base classes are considered public.
1287 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
Devang Patel5d11eb02009-12-03 19:11:07 +00001288 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1289 dwarf::DW_ACCESS_public);
1290 if (DT.isVirtual())
1291 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1292 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001293 return MemberDie;
1294}
1295
Devang Patelffe966c2009-12-14 16:18:45 +00001296/// createSubprogramDIE - Create new DIE using SP.
Devang Patelee70fa72010-09-27 23:15:27 +00001297DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel163a9f72010-05-10 22:49:55 +00001298 CompileUnit *SPCU = getCompileUnit(SP);
1299 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +00001300 if (SPDie)
1301 return SPDie;
1302
1303 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001304 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001305 if (!SP.getName().empty())
1306 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001307
Devang Patel65dbc902009-11-25 17:36:49 +00001308 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001309 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001310 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001311 getRealLinkageName(LinkageName));
1312
Devang Patel81625742010-08-09 20:20:05 +00001313 addSourceLine(SPDie, SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001314
Devang Patel7b172c62010-10-07 22:03:01 +00001315 if (SP.isPrototyped())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001316 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001317
1318 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001319 DICompositeType SPTy = SP.getType();
1320 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001321 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001322
Devang Patel3c91b052010-03-08 20:52:55 +00001323 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001324 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001325 else
Devang Patel2db49d72010-05-07 18:11:54 +00001326 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001327
Devang Patel5d11eb02009-12-03 19:11:07 +00001328 unsigned VK = SP.getVirtuality();
1329 if (VK) {
1330 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001331 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001332 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1333 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1334 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001335 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +00001336 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001337 }
1338
Devang Patelee70fa72010-09-27 23:15:27 +00001339 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001340 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001341
1342 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001343 // be handled while processing variables.
1344 DICompositeType SPTy = SP.getType();
1345 DIArray Args = SPTy.getTypeArray();
1346 unsigned SPTag = SPTy.getTag();
1347
Bill Wendling0310d762009-05-15 09:23:25 +00001348 if (SPTag == dwarf::DW_TAG_subroutine_type)
1349 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1350 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001351 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelb4645642010-02-06 01:02:37 +00001352 addType(Arg, ATy);
1353 if (ATy.isArtificial())
1354 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001355 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001356 }
1357 }
1358
Devang Patel4e0d19d2010-02-03 19:57:19 +00001359 if (SP.isArtificial())
1360 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1361
Devang Patelccff8122010-04-30 19:38:23 +00001362 if (!SP.isLocalToUnit())
1363 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001364
Devang Patelccff8122010-04-30 19:38:23 +00001365 if (SP.isOptimized())
1366 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1367
Jim Grosbach91729002010-07-21 23:03:52 +00001368 if (unsigned isa = Asm->getISAEncoding()) {
1369 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1370 }
1371
Devang Patelccff8122010-04-30 19:38:23 +00001372 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel163a9f72010-05-10 22:49:55 +00001373 SPCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +00001374
Stuart Hastings215aa152010-06-11 20:08:44 +00001375 // Add to context owner.
1376 addToContextOwner(SPDie, SP.getContext());
1377
Bill Wendling0310d762009-05-15 09:23:25 +00001378 return SPDie;
1379}
1380
Devang Patele9f8f5e2010-05-07 20:54:48 +00001381DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001382 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001383
1384 DbgScope *AScope = AbstractScopes.lookup(N);
1385 if (AScope)
1386 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001387
Devang Patel53bb5c92009-11-10 23:06:00 +00001388 DbgScope *Parent = NULL;
1389
1390 DIDescriptor Scope(N);
1391 if (Scope.isLexicalBlock()) {
1392 DILexicalBlock DB(N);
1393 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +00001394 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +00001395 }
1396
1397 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1398
1399 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001400 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001401 AScope->setAbstractScope();
1402 AbstractScopes[N] = AScope;
1403 if (DIDescriptor(N).isSubprogram())
1404 AbstractScopesList.push_back(AScope);
1405 return AScope;
1406}
Devang Patelaf9e8472009-10-01 20:31:14 +00001407
Devang Patel5f094002010-04-06 23:53:48 +00001408/// isSubprogramContext - Return true if Context is either a subprogram
1409/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001410static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001411 if (!Context)
1412 return false;
1413 DIDescriptor D(Context);
1414 if (D.isSubprogram())
1415 return true;
1416 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001417 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +00001418 return false;
1419}
1420
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001421/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001422/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1423/// If there are global variables in this scope then create and insert
1424/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001425DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +00001426 CompileUnit *SPCU = getCompileUnit(SPNode);
1427 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +00001428
Chris Lattnerd38fee82010-04-05 00:13:49 +00001429 assert(SPDie && "Unable to find subprogram DIE!");
1430 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001431
Chris Lattnerd38fee82010-04-05 00:13:49 +00001432 // There is not any need to generate specification DIE for a function
1433 // defined at compile unit level. If a function is defined inside another
1434 // function then gdb prefers the definition at top level and but does not
Jim Grosbach1e20b962010-07-21 21:21:52 +00001435 // expect specification DIE in parent function. So avoid creating
Chris Lattnerd38fee82010-04-05 00:13:49 +00001436 // specification DIE for a function defined inside a function.
1437 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbach1e20b962010-07-21 21:21:52 +00001438 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001439 !isSubprogramContext(SP.getContext())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001440 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001441
1442 // Add arguments.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001443 DICompositeType SPTy = SP.getType();
1444 DIArray Args = SPTy.getTypeArray();
1445 unsigned SPTag = SPTy.getTag();
1446 if (SPTag == dwarf::DW_TAG_subroutine_type)
1447 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1448 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001449 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerd38fee82010-04-05 00:13:49 +00001450 addType(Arg, ATy);
1451 if (ATy.isArtificial())
1452 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1453 SPDie->addChild(Arg);
1454 }
1455 DIE *SPDeclDie = SPDie;
1456 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001457 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Chris Lattnerd38fee82010-04-05 00:13:49 +00001458 SPDeclDie);
Devang Patel163a9f72010-05-10 22:49:55 +00001459 SPCU->addDie(SPDie);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001460 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001461
Devang Patel8aa61472010-07-07 22:20:57 +00001462 // Pick up abstract subprogram DIE.
1463 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1464 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1465 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1466 dwarf::DW_FORM_ref4, AbsSPDIE);
1467 SPCU->addDie(SPDie);
1468 }
1469
Chris Lattnerd38fee82010-04-05 00:13:49 +00001470 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1471 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1472 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1473 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1474 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1475 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1476 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001477
Chris Lattnerd38fee82010-04-05 00:13:49 +00001478 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001479}
1480
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001481/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001482/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1483DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001484
1485 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1486 if (Scope->isAbstractScope())
1487 return ScopeDIE;
1488
1489 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1490 if (Ranges.empty())
1491 return 0;
1492
1493 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1494 if (Ranges.size() > 1) {
1495 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +00001496 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +00001497 // DW_AT_ranges appropriately.
1498 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1499 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1500 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1501 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001502 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1503 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +00001504 }
1505 DebugRangeSymbols.push_back(NULL);
1506 DebugRangeSymbols.push_back(NULL);
1507 return ScopeDIE;
1508 }
1509
Devang Patelc3f5f782010-05-25 23:40:22 +00001510 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1511 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001512
Devang Patelc3f5f782010-05-25 23:40:22 +00001513 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001514
Chris Lattnerb7db7332010-03-09 01:58:53 +00001515 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1516 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001517
Devang Pateleac9c072010-04-27 19:46:33 +00001518 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1519 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001520
1521 return ScopeDIE;
1522}
1523
Devang Patel2c4ceb12009-11-21 02:48:08 +00001524/// constructInlinedScopeDIE - This scope represents inlined body of
1525/// a function. Construct DIE to represent this concrete inlined copy
1526/// of the function.
1527DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001528
1529 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001530 assert (Ranges.empty() == false
Devang Pateleac9c072010-04-27 19:46:33 +00001531 && "DbgScope does not have instruction markers!");
1532
1533 // FIXME : .debug_inlined section specification does not clearly state how
1534 // to emit inlined scope that is split into multiple instruction ranges.
1535 // For now, use first instruction range and emit low_pc/high_pc pair and
1536 // corresponding .debug_inlined section entry for this pair.
1537 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +00001538 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1539 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001540
Devang Patel0afbf232010-07-08 22:39:20 +00001541 if (StartLabel == 0 || EndLabel == 0) {
Devang Pateleac9c072010-04-27 19:46:33 +00001542 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1543 return 0;
1544 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001545 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001546 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001547 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001548 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +00001549
Devang Patel3c91b052010-03-08 20:52:55 +00001550 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001551 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001552 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001553 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1554
Devang Patel2db49d72010-05-07 18:11:54 +00001555 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001556 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1557 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001558 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001559 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001560 dwarf::DW_FORM_ref4, OriginDIE);
1561
Chris Lattner6ed0f902010-03-09 00:31:02 +00001562 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001563 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001564
1565 InlinedSubprogramDIEs.insert(OriginDIE);
1566
1567 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001568 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +00001569 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001570
1571 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001572 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001573 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +00001574 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001575 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001576 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001577
Devang Patel53bb5c92009-11-10 23:06:00 +00001578 DILocation DL(Scope->getInlinedAt());
Devang Patel163a9f72010-05-10 22:49:55 +00001579 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001580 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001581
1582 return ScopeDIE;
1583}
1584
Devang Patel2c4ceb12009-11-21 02:48:08 +00001585
1586/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001587DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +00001588 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +00001589 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001590 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001591
1592 // Translate tag to proper Dwarf tag. The result variable is dropped for
1593 // now.
1594 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +00001595 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001596 case dwarf::DW_TAG_return_variable:
1597 return NULL;
1598 case dwarf::DW_TAG_arg_variable:
1599 Tag = dwarf::DW_TAG_formal_parameter;
1600 break;
1601 case dwarf::DW_TAG_auto_variable: // fall thru
1602 default:
1603 Tag = dwarf::DW_TAG_variable;
1604 break;
1605 }
1606
1607 // Define variable debug information entry.
1608 DIE *VariableDie = new DIE(Tag);
1609
Devang Patel53bb5c92009-11-10 23:06:00 +00001610 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +00001611 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1612 V2AVI = VarToAbstractVarMap.find(DV);
1613 if (V2AVI != VarToAbstractVarMap.end())
1614 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001615
Devang Patel26c1e562010-05-20 16:36:41 +00001616 if (AbsDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001617 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001618 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001619 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001620 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel8bd11de2010-08-09 21:01:39 +00001621 addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +00001622
1623 // Add variable type.
Devang Patel8bd11de2010-08-09 21:01:39 +00001624 addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001625 }
1626
Devang Patel8bd11de2010-08-09 21:01:39 +00001627 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patelb4645642010-02-06 01:02:37 +00001628 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel3cf763d2010-09-29 23:07:21 +00001629 else if (DIVariable(DV->getVariable()).isArtificial())
1630 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +00001631
1632 if (Scope->isAbstractScope()) {
1633 DV->setDIE(VariableDie);
1634 return VariableDie;
1635 }
1636
1637 // Add variable address.
1638
1639 unsigned Offset = DV->getDotDebugLocOffset();
1640 if (Offset != ~0U) {
1641 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1642 Asm->GetTempSymbol("debug_loc", Offset));
1643 DV->setDIE(VariableDie);
1644 UseDotDebugLocEntry.insert(VariableDie);
1645 return VariableDie;
1646 }
1647
1648 // Check if variable is described by a DBG_VALUE instruction.
1649 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1650 DbgVariableToDbgInstMap.find(DV);
1651 if (DVI != DbgVariableToDbgInstMap.end()) {
1652 const MachineInstr *DVInsn = DVI->second;
Devang Patelc3f5f782010-05-25 23:40:22 +00001653 bool updated = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001654 // FIXME : Handle getNumOperands != 3
Devang Patelc3f5f782010-05-25 23:40:22 +00001655 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +00001656 if (DVInsn->getOperand(0).isReg()) {
1657 const MachineOperand RegOp = DVInsn->getOperand(0);
1658 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1659 if (DVInsn->getOperand(1).isImm() &&
1660 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1661 addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
1662 updated = true;
1663 } else
Devang Patel522ad742010-11-12 23:20:42 +00001664 updated = addRegisterAddress(VariableDie, RegOp);
Devang Patel0b48ead2010-08-31 22:22:42 +00001665 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001666 else if (DVInsn->getOperand(0).isImm())
Devang Patel522ad742010-11-12 23:20:42 +00001667 updated = addConstantValue(VariableDie, DVInsn->getOperand(0));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001668 else if (DVInsn->getOperand(0).isFPImm())
1669 updated =
Devang Patel522ad742010-11-12 23:20:42 +00001670 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patelc3f5f782010-05-25 23:40:22 +00001671 } else {
1672 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1673 if (Location.getReg()) {
1674 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patelc3f5f782010-05-25 23:40:22 +00001675 updated = true;
1676 }
1677 }
1678 if (!updated) {
1679 // If variableDie is not updated then DBG_VALUE instruction does not
1680 // have valid variable info.
1681 delete VariableDie;
1682 return NULL;
1683 }
1684 DV->setDIE(VariableDie);
1685 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001686 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001687
1688 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +00001689 int FI = 0;
Devang Patel9e3bd2c2010-08-31 06:11:28 +00001690 if (findVariableFrameIndex(DV, &FI))
1691 addVariableAddress(DV, VariableDie, FI);
1692
Devang Patel53bb5c92009-11-10 23:06:00 +00001693 DV->setDIE(VariableDie);
1694 return VariableDie;
1695
1696}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001697
Devang Patel193f7202009-11-24 01:14:22 +00001698void DwarfDebug::addPubTypes(DISubprogram SP) {
1699 DICompositeType SPTy = SP.getType();
1700 unsigned SPTag = SPTy.getTag();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001701 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel193f7202009-11-24 01:14:22 +00001702 return;
1703
1704 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001705 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +00001706 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +00001707 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +00001708 continue;
1709 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +00001710 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +00001711 && !CATy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001712 CompileUnit *TheCU = getCompileUnit(CATy);
1713 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1714 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001715 }
1716 }
1717}
1718
Devang Patel2c4ceb12009-11-21 02:48:08 +00001719/// constructScopeDIE - Construct a DIE for this scope.
1720DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001721 if (!Scope || !Scope->getScopeNode())
1722 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001723
Devang Patel3c91b052010-03-08 20:52:55 +00001724 DIScope DS(Scope->getScopeNode());
1725 DIE *ScopeDIE = NULL;
1726 if (Scope->getInlinedAt())
1727 ScopeDIE = constructInlinedScopeDIE(Scope);
1728 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +00001729 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001730 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001731 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001732 // Note down abstract DIE.
1733 if (ScopeDIE)
1734 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1735 }
Devang Patel3c91b052010-03-08 20:52:55 +00001736 else
Devang Patel2db49d72010-05-07 18:11:54 +00001737 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001738 }
Devang Patelaead63c2010-03-29 22:59:58 +00001739 else
Devang Patel3c91b052010-03-08 20:52:55 +00001740 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001741 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001742
Devang Patel53bb5c92009-11-10 23:06:00 +00001743 // Add variables to scope.
Devang Patele03161c2010-08-09 18:51:29 +00001744 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001745 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001746 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001747 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001748 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001749 }
1750
1751 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001752 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001753 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1754 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001755 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001756 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001757 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001758 }
Devang Patel193f7202009-11-24 01:14:22 +00001759
Jim Grosbach1e20b962010-07-21 21:21:52 +00001760 if (DS.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001761 addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001762
Devang Patel193f7202009-11-24 01:14:22 +00001763 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001764}
1765
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001766/// GetOrCreateSourceID - Look up the source id with the given directory and
1767/// source file names. If none currently exists, create a new id and insert it
1768/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1769/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +00001770
Rafael Espindola5c055632010-11-18 02:04:25 +00001771unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName){
Devang Patel1905a182010-09-16 20:57:49 +00001772 // If FE did not provide a file name, then assume stdin.
1773 if (FileName.empty())
Rafael Espindola5c055632010-11-18 02:04:25 +00001774 return GetOrCreateSourceID("<stdin>");
Devang Patel1905a182010-09-16 20:57:49 +00001775
Rafael Espindola5c055632010-11-18 02:04:25 +00001776 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
1777 if (Entry.getValue())
1778 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001779
Rafael Espindola5c055632010-11-18 02:04:25 +00001780 unsigned SrcId = SourceIdMap.size();
1781 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001782
Rafael Espindola5c055632010-11-18 02:04:25 +00001783 // Print out a .file directive to specify files for .loc directives.
1784 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001785
1786 return SrcId;
1787}
1788
Devang Patel6404e4e2009-12-15 19:16:48 +00001789/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1790DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel163a9f72010-05-10 22:49:55 +00001791 CompileUnit *TheCU = getCompileUnit(NS);
1792 DIE *NDie = TheCU->getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001793 if (NDie)
1794 return NDie;
1795 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel163a9f72010-05-10 22:49:55 +00001796 TheCU->insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +00001797 if (!NS.getName().empty())
1798 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patel81625742010-08-09 20:20:05 +00001799 addSourceLine(NDie, NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001800 addToContextOwner(NDie, NS.getContext());
1801 return NDie;
1802}
1803
Jim Grosbach1e20b962010-07-21 21:21:52 +00001804/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +00001805/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001806void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001807 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001808 StringRef FN = DIUnit.getFilename();
1809 StringRef Dir = DIUnit.getDirectory();
Rafael Espindola5c055632010-11-18 02:04:25 +00001810 unsigned ID = GetOrCreateSourceID(FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001811
1812 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001813 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001814 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001815 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001816 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001817 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +00001818 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1819 // simplifies debug range entries.
Devang Patel9b93b6b2010-06-28 22:22:47 +00001820 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +00001821 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +00001822 // compile unit in debug_line section.
Devang Patelae84d5b2010-08-31 23:50:19 +00001823 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
1824 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
1825 Asm->GetTempSymbol("section_line"));
1826 else
1827 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001828
Devang Patel65dbc902009-11-25 17:36:49 +00001829 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001830 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001831 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001832 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001833
Devang Patel65dbc902009-11-25 17:36:49 +00001834 StringRef Flags = DIUnit.getFlags();
1835 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001836 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001837
1838 unsigned RVer = DIUnit.getRunTimeVersion();
1839 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001840 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001841 dwarf::DW_FORM_data1, RVer);
1842
Devang Patel163a9f72010-05-10 22:49:55 +00001843 CompileUnit *NewCU = new CompileUnit(ID, Die);
1844 if (!FirstCU)
1845 FirstCU = NewCU;
1846 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001847}
1848
Devang Patel163a9f72010-05-10 22:49:55 +00001849/// getCompielUnit - Get CompileUnit DIE.
1850CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1851 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1852 DIDescriptor D(N);
1853 const MDNode *CUNode = NULL;
1854 if (D.isCompileUnit())
1855 CUNode = N;
1856 else if (D.isSubprogram())
1857 CUNode = DISubprogram(N).getCompileUnit();
1858 else if (D.isType())
1859 CUNode = DIType(N).getCompileUnit();
1860 else if (D.isGlobalVariable())
1861 CUNode = DIGlobalVariable(N).getCompileUnit();
1862 else if (D.isVariable())
1863 CUNode = DIVariable(N).getCompileUnit();
1864 else if (D.isNameSpace())
1865 CUNode = DINameSpace(N).getCompileUnit();
1866 else if (D.isFile())
1867 CUNode = DIFile(N).getCompileUnit();
1868 else
1869 return FirstCU;
1870
1871 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1872 = CUMap.find(CUNode);
1873 if (I == CUMap.end())
1874 return FirstCU;
1875 return I->second;
1876}
1877
Devang Patel0c4720c2010-08-23 18:25:56 +00001878/// isUnsignedDIType - Return true if type encoding is unsigned.
1879static bool isUnsignedDIType(DIType Ty) {
1880 DIDerivedType DTy(Ty);
1881 if (DTy.Verify())
1882 return isUnsignedDIType(DTy.getTypeDerivedFrom());
1883
1884 DIBasicType BTy(Ty);
1885 if (BTy.Verify()) {
1886 unsigned Encoding = BTy.getEncoding();
1887 if (Encoding == dwarf::DW_ATE_unsigned ||
1888 Encoding == dwarf::DW_ATE_unsigned_char)
1889 return true;
1890 }
1891 return false;
1892}
Devang Patel163a9f72010-05-10 22:49:55 +00001893
1894/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001895void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +00001896 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001897
Devang Patel905cf5e2009-09-04 23:59:07 +00001898 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +00001899 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +00001900 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001901
1902 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001903 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +00001904 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001905 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001906
Devang Patel9fa539c2010-08-10 01:37:23 +00001907 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +00001908 DIE *VariableDIE = new DIE(GV.getTag());
1909
1910 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001911
Devang Patel9fa539c2010-08-10 01:37:23 +00001912 // Add name.
1913 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1914 GV.getDisplayName());
1915 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +00001916 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel9fa539c2010-08-10 01:37:23 +00001917 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1918 getRealLinkageName(LinkageName));
1919 // Add type.
1920 addType(VariableDIE, GTy);
Devang Patel50d80e32010-04-13 20:35:04 +00001921 if (GTy.isCompositeType() && !GTy.getName().empty()
1922 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001923 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001924 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +00001925 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001926 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001927 // Add scoping info.
1928 if (!GV.isLocalToUnit()) {
1929 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1930 // Expose as global.
1931 TheCU->addGlobal(GV.getName(), VariableDIE);
1932 }
1933 // Add line number info.
1934 addSourceLine(VariableDIE, GV);
1935 // Add to map.
1936 TheCU->insertDIE(N, VariableDIE);
1937 // Add to context owner.
1938 DIDescriptor GVContext = GV.getContext();
1939 addToContextOwner(VariableDIE, GVContext);
1940 // Add location.
Devang Patel29368072010-08-10 07:11:13 +00001941 if (isGlobalVariable) {
1942 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1943 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1944 addLabel(Block, 0, dwarf::DW_FORM_udata,
1945 Asm->Mang->getSymbol(GV.getGlobal()));
1946 // Do not create specification DIE if context is either compile unit
1947 // or a subprogram.
1948 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1949 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1950 // Create specification DIE.
1951 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1952 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1953 dwarf::DW_FORM_ref4, VariableDIE);
1954 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1955 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1956 TheCU->addDie(VariableSpecDIE);
1957 } else {
1958 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1959 }
1960 } else if (Constant *C = GV.getConstant()) {
1961 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
Devang Patel0c4720c2010-08-23 18:25:56 +00001962 if (isUnsignedDIType(GTy))
Devang Patel29368072010-08-10 07:11:13 +00001963 addUInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
1964 CI->getZExtValue());
1965 else
1966 addSInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1967 CI->getSExtValue());
Devang Patel29368072010-08-10 07:11:13 +00001968 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001969 }
Devang Patel13e16b62009-06-26 01:49:18 +00001970 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001971}
1972
Devang Patel163a9f72010-05-10 22:49:55 +00001973/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001974void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001975 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001976
Stuart Hastings639336e2010-04-06 21:38:29 +00001977 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001978 CompileUnit *TheCU = getCompileUnit(N);
1979 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +00001980 return;
1981
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001982 if (!SP.isDefinition())
1983 // This is a method declaration which will be handled while constructing
1984 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001985 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001986
Stuart Hastings639336e2010-04-06 21:38:29 +00001987 DIE *SubprogramDie = createSubprogramDIE(SP);
1988
1989 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00001990 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001991
1992 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001993 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001994
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001995 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +00001996 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001997
Devang Patel13e16b62009-06-26 01:49:18 +00001998 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001999}
2000
Devang Patel2c4ceb12009-11-21 02:48:08 +00002001/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00002002/// content. Create global DIEs and emit initial debug info sections.
2003/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00002004void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00002005 if (DisableDebugInfoPrinting)
2006 return;
2007
Devang Patel78ab9e22009-07-30 18:56:46 +00002008 DebugInfoFinder DbgFinder;
2009 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00002010
Chris Lattnerd850ac72010-04-05 02:19:28 +00002011 bool HasDebugInfo = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002012
Chris Lattnerd850ac72010-04-05 02:19:28 +00002013 // Scan all the compile-units to see if there are any marked as the main unit.
2014 // if not, we do not generate debug info.
2015 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2016 E = DbgFinder.compile_unit_end(); I != E; ++I) {
2017 if (DICompileUnit(*I).isMain()) {
2018 HasDebugInfo = true;
2019 break;
2020 }
2021 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002022
Chris Lattnerd850ac72010-04-05 02:19:28 +00002023 if (!HasDebugInfo) return;
2024
2025 // Tell MMI that we have debug info.
2026 MMI->setDebugInfoAvailability(true);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002027
Chris Lattnerbe15beb2010-04-04 23:17:54 +00002028 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00002029 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002030
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002031 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00002032 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2033 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002034 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002035
Devang Patel53bb5c92009-11-10 23:06:00 +00002036 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00002037 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
2038 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002039 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00002040
Devang Patelc366f832009-12-10 19:14:49 +00002041 // Create DIEs for each global variable.
2042 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
2043 E = DbgFinder.global_variable_end(); I != E; ++I)
2044 constructGlobalVariableDIE(*I);
2045
Devang Patele7e5a0f2010-08-10 20:01:20 +00002046 //getOrCreateTypeDIE
2047 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
2048 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2049 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2050
Devang Patel1a7ca032010-09-28 18:08:20 +00002051 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
2052 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2053 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2054
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002055 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00002056 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002057}
2058
Devang Patel2c4ceb12009-11-21 02:48:08 +00002059/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002060///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002061void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00002062 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +00002063 const Module *M = MMI->getModule();
Devang Patele9a1cca2010-08-02 17:32:15 +00002064 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +00002065 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2066 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2067 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2068 DISubprogram SP(AllSPs->getOperand(SI));
2069 if (!SP.Verify()) continue;
2070
2071 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +00002072 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +00002073 StringRef FName = SP.getLinkageName();
2074 if (FName.empty())
2075 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002076 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +00002077 if (!NMD) continue;
2078 unsigned E = NMD->getNumOperands();
2079 if (!E) continue;
2080 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Patele9a1cca2010-08-02 17:32:15 +00002081 DeadFnScopeMap[SP] = Scope;
Devang Patel4a1cad62010-06-28 18:25:03 +00002082 for (unsigned I = 0; I != E; ++I) {
2083 DIVariable DV(NMD->getOperand(I));
2084 if (!DV.Verify()) continue;
2085 Scope->addVariable(new DbgVariable(DV));
2086 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002087
Devang Patel4a1cad62010-06-28 18:25:03 +00002088 // Construct subprogram DIE and add variables DIEs.
2089 constructSubprogramDIE(SP);
2090 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patele03161c2010-08-09 18:51:29 +00002091 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel4a1cad62010-06-28 18:25:03 +00002092 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2093 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2094 if (VariableDIE)
2095 ScopeDIE->addChild(VariableDIE);
2096 }
2097 }
2098 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002099
Devang Patel53bb5c92009-11-10 23:06:00 +00002100 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2101 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2102 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2103 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002104 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00002105 }
2106
Devang Patele9f8f5e2010-05-07 20:54:48 +00002107 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00002108 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2109 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002110 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00002111 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00002112 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00002113 if (!NDie) continue;
2114 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00002115 }
2116
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002117 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002118 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002119 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002120 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002121 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002122
2123 // End text sections.
2124 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002125 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00002126 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002127 }
2128
2129 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002130 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002131
2132 // Emit function debug frame information
2133 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2134 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002135 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002136
2137 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002138 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002139
2140 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00002141 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002142
2143 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002144 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002145
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002146 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002147 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002148
Devang Patel193f7202009-11-24 01:14:22 +00002149 // Emit info into a debug pubtypes section.
2150 emitDebugPubTypes();
2151
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002152 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002153 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002154
2155 // Emit info into a debug aranges section.
2156 EmitDebugARanges();
2157
2158 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002159 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002160
2161 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002162 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002163
2164 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002165 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002166
Chris Lattnerbc733f52010-03-13 02:17:42 +00002167 // Emit info into a debug str section.
2168 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002169
Devang Patele9a1cca2010-08-02 17:32:15 +00002170 // clean up.
2171 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +00002172 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2173 E = CUMap.end(); I != E; ++I)
2174 delete I->second;
2175 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002176}
2177
Devang Patel53bb5c92009-11-10 23:06:00 +00002178/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002179DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002180 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002181
Devang Patel2db49d72010-05-07 18:11:54 +00002182 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00002183 if (AbsDbgVariable)
2184 return AbsDbgVariable;
2185
Devang Patel2db49d72010-05-07 18:11:54 +00002186 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002187 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00002188 if (!Scope)
2189 return NULL;
2190
Devang Patel26c1e562010-05-20 16:36:41 +00002191 AbsDbgVariable = new DbgVariable(Var);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002192 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002193 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00002194 return AbsDbgVariable;
2195}
2196
Devang Patelee432862010-05-20 19:57:06 +00002197/// collectVariableInfoFromMMITable - Collect variable information from
2198/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002199void
Devang Patelee432862010-05-20 19:57:06 +00002200DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2201 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002202 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patele717faa2009-10-06 01:26:37 +00002203 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2204 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2205 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00002206 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00002207 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00002208 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002209 DIVariable DV(Var);
2210 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002211
Chris Lattnerde4845c2010-04-02 19:42:39 +00002212 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002213 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002214 Scope = ConcreteScopes.lookup(IA);
2215 if (Scope == 0)
2216 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002217
Devang Patelfb0ee432009-11-10 23:20:04 +00002218 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002219 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00002220 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00002221
Devang Patel26c1e562010-05-20 16:36:41 +00002222 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2223 DbgVariable *RegVar = new DbgVariable(DV);
2224 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002225 Scope->addVariable(RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +00002226 if (AbsDbgVariable) {
2227 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2228 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2229 }
Devang Patele717faa2009-10-06 01:26:37 +00002230 }
Devang Patelee432862010-05-20 19:57:06 +00002231}
Devang Patel90a48ad2010-03-15 18:33:46 +00002232
Jim Grosbach1e20b962010-07-21 21:21:52 +00002233/// isDbgValueInUndefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00002234/// DBG_VALUE instruction, is in undefined reg.
2235static bool isDbgValueInUndefinedReg(const MachineInstr *MI) {
2236 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2237 if (MI->getOperand(0).isReg() && !MI->getOperand(0).getReg())
2238 return true;
2239 return false;
2240}
2241
Jim Grosbach1e20b962010-07-21 21:21:52 +00002242/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00002243/// DBG_VALUE instruction, is in a defined reg.
2244static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2245 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2246 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
2247 return true;
2248 return false;
2249}
2250
Devang Patelee432862010-05-20 19:57:06 +00002251/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002252void
Devang Patel78e127d2010-06-25 22:07:34 +00002253DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2254 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002255
Devang Patelee432862010-05-20 19:57:06 +00002256 /// collection info from MMI table.
2257 collectVariableInfoFromMMITable(MF, Processed);
2258
2259 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patel90a48ad2010-03-15 18:33:46 +00002260 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002261 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelee432862010-05-20 19:57:06 +00002262 I != E; ++I)
Devang Patel90a48ad2010-03-15 18:33:46 +00002263 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2264 II != IE; ++II) {
2265 const MachineInstr *MInsn = II;
Devang Patelc3f5f782010-05-25 23:40:22 +00002266 if (!MInsn->isDebugValue() || isDbgValueInUndefinedReg(MInsn))
Devang Patel90a48ad2010-03-15 18:33:46 +00002267 continue;
Devang Patelee432862010-05-20 19:57:06 +00002268 DbgValues.push_back(MInsn);
2269 }
Devang Patel90a48ad2010-03-15 18:33:46 +00002270
Devang Patelc3f5f782010-05-25 23:40:22 +00002271 // This is a collection of DBV_VALUE instructions describing same variable.
2272 SmallVector<const MachineInstr *, 4> MultipleValues;
Devang Patelee432862010-05-20 19:57:06 +00002273 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2274 E = DbgValues.end(); I != E; ++I) {
2275 const MachineInstr *MInsn = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00002276 MultipleValues.clear();
2277 if (isDbgValueInDefinedReg(MInsn))
2278 MultipleValues.push_back(MInsn);
Devang Patelee432862010-05-20 19:57:06 +00002279 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2280 if (Processed.count(DV) != 0)
2281 continue;
2282
Devang Patel354eb7e2010-06-02 19:05:13 +00002283 const MachineInstr *PrevMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002284 for (SmallVector<const MachineInstr *, 8>::iterator MI = I+1,
Devang Patelc3f5f782010-05-25 23:40:22 +00002285 ME = DbgValues.end(); MI != ME; ++MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002286 const MDNode *Var =
Devang Patelc3f5f782010-05-25 23:40:22 +00002287 (*MI)->getOperand((*MI)->getNumOperands()-1).getMetadata();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002288 if (Var == DV && isDbgValueInDefinedReg(*MI) &&
Devang Patel354eb7e2010-06-02 19:05:13 +00002289 !PrevMI->isIdenticalTo(*MI))
Devang Patelc3f5f782010-05-25 23:40:22 +00002290 MultipleValues.push_back(*MI);
Devang Patel354eb7e2010-06-02 19:05:13 +00002291 PrevMI = *MI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002292 }
2293
Devang Patelee432862010-05-20 19:57:06 +00002294 DbgScope *Scope = findDbgScope(MInsn);
Devang Pateld8720f42010-05-27 20:25:04 +00002295 bool CurFnArg = false;
2296 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2297 DISubprogram(DV.getContext()).describes(MF->getFunction()))
2298 CurFnArg = true;
2299 if (!Scope && CurFnArg)
Devang Patelc0c5a262010-05-21 00:10:20 +00002300 Scope = CurrentFnDbgScope;
Devang Patelee432862010-05-20 19:57:06 +00002301 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00002302 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00002303 continue;
2304
2305 Processed.insert(DV);
Devang Patelee432862010-05-20 19:57:06 +00002306 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patelee432862010-05-20 19:57:06 +00002307 Scope->addVariable(RegVar);
Devang Patelc0c5a262010-05-21 00:10:20 +00002308 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2309 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2310 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002311 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002312 if (MultipleValues.size() <= 1) {
2313 DbgVariableToDbgInstMap[RegVar] = MInsn;
2314 continue;
2315 }
2316
2317 // handle multiple DBG_VALUE instructions describing one variable.
Devang Patelc3f5f782010-05-25 23:40:22 +00002318 if (DotDebugLocEntries.empty())
Devang Patel80250682010-05-26 23:55:23 +00002319 RegVar->setDotDebugLocOffset(0);
2320 else
2321 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Devang Patele2df8422010-05-26 17:29:32 +00002322 const MachineInstr *Begin = NULL;
2323 const MachineInstr *End = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002324 for (SmallVector<const MachineInstr *, 4>::iterator
2325 MVI = MultipleValues.begin(), MVE = MultipleValues.end();
Devang Patel61409622010-07-07 20:12:52 +00002326 MVI != MVE; ++MVI) {
Devang Patele2df8422010-05-26 17:29:32 +00002327 if (!Begin) {
2328 Begin = *MVI;
2329 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002330 }
Devang Patele2df8422010-05-26 17:29:32 +00002331 End = *MVI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002332 MachineLocation MLoc;
Devang Patelb2cf5812010-08-04 18:40:52 +00002333 if (Begin->getNumOperands() == 3) {
2334 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2335 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2336 } else
2337 MLoc = Asm->getDebugValueLocation(Begin);
2338
Devang Patele2df8422010-05-26 17:29:32 +00002339 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2340 const MCSymbol *SLabel = getLabelBeforeInsn(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002341 if (MLoc.getReg())
2342 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
2343
Devang Patele2df8422010-05-26 17:29:32 +00002344 Begin = End;
2345 if (MVI + 1 == MVE) {
2346 // If End is the last instruction then its value is valid
Devang Patelc3f5f782010-05-25 23:40:22 +00002347 // until the end of the funtion.
Devang Patelb2cf5812010-08-04 18:40:52 +00002348 MachineLocation EMLoc;
2349 if (End->getNumOperands() == 3) {
2350 if (End->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2351 EMLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2352 } else
2353 EMLoc = Asm->getDebugValueLocation(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002354 if (EMLoc.getReg())
2355 DotDebugLocEntries.
2356 push_back(DotDebugLocEntry(SLabel, FunctionEndSym, EMLoc));
Devang Patelc3f5f782010-05-25 23:40:22 +00002357 }
2358 }
2359 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00002360 }
Devang Patel98e1cac2010-05-14 21:01:35 +00002361
2362 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00002363 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00002364 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00002365 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002366 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00002367 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00002368 continue;
2369 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2370 if (Scope)
Devang Patel26c1e562010-05-20 16:36:41 +00002371 Scope->addVariable(new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00002372 }
2373 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002374}
Devang Patel98e1cac2010-05-14 21:01:35 +00002375
Devang Patelc3f5f782010-05-25 23:40:22 +00002376/// getLabelBeforeInsn - Return Label preceding the instruction.
2377const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2378 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2379 LabelsBeforeInsn.find(MI);
2380 if (I == LabelsBeforeInsn.end())
2381 // FunctionBeginSym always preceeds all the instruction in current function.
2382 return FunctionBeginSym;
2383 return I->second;
2384}
2385
2386/// getLabelAfterInsn - Return Label immediately following the instruction.
2387const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2388 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2389 LabelsAfterInsn.find(MI);
2390 if (I == LabelsAfterInsn.end())
2391 return NULL;
2392 return I->second;
Devang Patele717faa2009-10-06 01:26:37 +00002393}
2394
Devang Patelcbbe2872010-10-26 17:49:02 +00002395/// beginInstruction - Process beginning of an instruction.
2396void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002397 if (InsnNeedsLabel.count(MI) == 0) {
2398 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patel553881b2010-03-29 17:20:31 +00002399 return;
Devang Patelc3f5f782010-05-25 23:40:22 +00002400 }
Devang Patelaead63c2010-03-29 22:59:58 +00002401
Devang Patelb2b31a62010-05-26 19:37:24 +00002402 // Check location.
2403 DebugLoc DL = MI->getDebugLoc();
2404 if (!DL.isUnknown()) {
2405 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2406 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Pateleac9c072010-04-27 19:46:33 +00002407 PrevInstLoc = DL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002408 LabelsBeforeInsn[MI] = PrevLabel;
2409 return;
Devang Pateleac9c072010-04-27 19:46:33 +00002410 }
Devang Patel553881b2010-03-29 17:20:31 +00002411
Jim Grosbach1e20b962010-07-21 21:21:52 +00002412 // If location is unknown then use temp label for this DBG_VALUE
Devang Patelb2b31a62010-05-26 19:37:24 +00002413 // instruction.
2414 if (MI->isDebugValue()) {
Devang Patel77051f52010-05-26 21:23:46 +00002415 PrevLabel = MMI->getContext().CreateTempSymbol();
2416 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00002417 LabelsBeforeInsn[MI] = PrevLabel;
2418 return;
2419 }
2420
2421 if (UnknownLocations) {
2422 PrevLabel = recordSourceLine(0, 0, 0);
2423 LabelsBeforeInsn[MI] = PrevLabel;
2424 return;
2425 }
2426
2427 assert (0 && "Instruction is not processed!");
Devang Patel0d20ac82009-10-06 01:50:42 +00002428}
2429
Devang Patelcbbe2872010-10-26 17:49:02 +00002430/// endInstruction - Process end of an instruction.
2431void DwarfDebug::endInstruction(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002432 if (InsnsEndScopeSet.count(MI) != 0) {
2433 // Emit a label if this instruction ends a scope.
2434 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2435 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002436 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002437 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002438}
2439
Devang Pateleac9c072010-04-27 19:46:33 +00002440/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002441DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel61409622010-07-07 20:12:52 +00002442 const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002443 if (!InlinedAt) {
2444 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2445 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002446 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002447 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2448 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002449 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002450 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002451 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002452 WScope->setParent(Parent);
2453 Parent->addScope(WScope);
2454 }
2455
2456 if (!WScope->getParent()) {
2457 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastingscad22ad2010-06-15 23:06:30 +00002458 // We used to check only for a linkage name, but that fails
2459 // since we began omitting the linkage name for private
2460 // functions. The new way is to check for the name in metadata,
2461 // but that's not supported in old .ll test cases. Ergo, we
2462 // check both.
Stuart Hastings215aa152010-06-11 20:08:44 +00002463 if (SPName == Asm->MF->getFunction()->getName() ||
2464 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Pateleac9c072010-04-27 19:46:33 +00002465 CurrentFnDbgScope = WScope;
2466 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002467
Devang Pateleac9c072010-04-27 19:46:33 +00002468 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002469 }
2470
Devang Patel78e127d2010-06-25 22:07:34 +00002471 getOrCreateAbstractScope(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00002472 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2473 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002474 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002475
2476 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2477 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2478 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002479 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002480 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002481 WScope->setParent(Parent);
2482 Parent->addScope(WScope);
2483
2484 ConcreteScopes[InlinedAt] = WScope;
Devang Pateleac9c072010-04-27 19:46:33 +00002485
2486 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002487}
2488
Devang Pateleac9c072010-04-27 19:46:33 +00002489/// hasValidLocation - Return true if debug location entry attached with
2490/// machine instruction encodes valid location info.
2491static bool hasValidLocation(LLVMContext &Ctx,
2492 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002493 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002494 DebugLoc DL = MInsn->getDebugLoc();
2495 if (DL.isUnknown()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002496
Devang Patele9f8f5e2010-05-07 20:54:48 +00002497 const MDNode *S = DL.getScope(Ctx);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002498
Devang Pateleac9c072010-04-27 19:46:33 +00002499 // There is no need to create another DIE for compile unit. For all
2500 // other scopes, create one DbgScope now. This will be translated
2501 // into a scope DIE at the end.
2502 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002503
Devang Pateleac9c072010-04-27 19:46:33 +00002504 Scope = S;
2505 InlinedAt = DL.getInlinedAt(Ctx);
2506 return true;
2507}
2508
2509/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2510/// hierarchy.
2511static void calculateDominanceGraph(DbgScope *Scope) {
2512 assert (Scope && "Unable to calculate scop edominance graph!");
2513 SmallVector<DbgScope *, 4> WorkStack;
2514 WorkStack.push_back(Scope);
2515 unsigned Counter = 0;
2516 while (!WorkStack.empty()) {
2517 DbgScope *WS = WorkStack.back();
2518 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2519 bool visitedChildren = false;
2520 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2521 SE = Children.end(); SI != SE; ++SI) {
2522 DbgScope *ChildScope = *SI;
2523 if (!ChildScope->getDFSOut()) {
2524 WorkStack.push_back(ChildScope);
2525 visitedChildren = true;
2526 ChildScope->setDFSIn(++Counter);
2527 break;
2528 }
2529 }
2530 if (!visitedChildren) {
2531 WorkStack.pop_back();
2532 WS->setDFSOut(++Counter);
2533 }
2534 }
2535}
2536
2537/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002538static
Devang Pateleac9c072010-04-27 19:46:33 +00002539void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2540 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2541{
2542#ifndef NDEBUG
2543 unsigned PrevDFSIn = 0;
2544 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2545 I != E; ++I) {
2546 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2547 II != IE; ++II) {
2548 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002549 const MDNode *Scope = NULL;
2550 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002551
2552 // Check if instruction has valid location information.
2553 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2554 dbgs() << " [ ";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002555 if (InlinedAt)
Devang Pateleac9c072010-04-27 19:46:33 +00002556 dbgs() << "*";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002557 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Pateleac9c072010-04-27 19:46:33 +00002558 MI2ScopeMap.find(MInsn);
2559 if (DI != MI2ScopeMap.end()) {
2560 DbgScope *S = DI->second;
2561 dbgs() << S->getDFSIn();
2562 PrevDFSIn = S->getDFSIn();
2563 } else
2564 dbgs() << PrevDFSIn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002565 } else
Devang Pateleac9c072010-04-27 19:46:33 +00002566 dbgs() << " [ x" << PrevDFSIn;
2567 dbgs() << " ]";
2568 MInsn->dump();
2569 }
2570 dbgs() << "\n";
2571 }
2572#endif
2573}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002574/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002575/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002576bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002577 // If scope information was extracted using .dbg intrinsics then there is not
2578 // any need to extract these information by scanning each instruction.
2579 if (!DbgScopeMap.empty())
2580 return false;
2581
Dan Gohman314bf7c2010-04-23 01:18:53 +00002582 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002583 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2584 SmallVector<DbgRange, 4> MIRanges;
2585 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002586 const MDNode *PrevScope = NULL;
2587 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002588 const MachineInstr *RangeBeginMI = NULL;
2589 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002590 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002591 I != E; ++I) {
2592 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2593 II != IE; ++II) {
2594 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002595 const MDNode *Scope = NULL;
2596 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002597
2598 // Check if instruction has valid location information.
2599 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2600 PrevMI = MInsn;
2601 continue;
2602 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002603
Devang Pateleac9c072010-04-27 19:46:33 +00002604 // If scope has not changed then skip this instruction.
2605 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2606 PrevMI = MInsn;
2607 continue;
2608 }
2609
Jim Grosbach1e20b962010-07-21 21:21:52 +00002610 if (RangeBeginMI) {
2611 // If we have alread seen a beginning of a instruction range and
Devang Pateleac9c072010-04-27 19:46:33 +00002612 // current instruction scope does not match scope of first instruction
2613 // in this range then create a new instruction range.
2614 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002615 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel61409622010-07-07 20:12:52 +00002616 PrevInlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002617 MIRanges.push_back(R);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002618 }
Devang Pateleac9c072010-04-27 19:46:33 +00002619
2620 // This is a beginning of a new instruction range.
2621 RangeBeginMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002622
Devang Pateleac9c072010-04-27 19:46:33 +00002623 // Reset previous markers.
2624 PrevMI = MInsn;
2625 PrevScope = Scope;
2626 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002627 }
2628 }
2629
Devang Pateleac9c072010-04-27 19:46:33 +00002630 // Create last instruction range.
2631 if (RangeBeginMI && PrevMI && PrevScope) {
2632 DbgRange R(RangeBeginMI, PrevMI);
2633 MIRanges.push_back(R);
2634 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002635 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002636
Devang Patel344130e2010-01-04 20:44:00 +00002637 if (!CurrentFnDbgScope)
2638 return false;
2639
Devang Pateleac9c072010-04-27 19:46:33 +00002640 calculateDominanceGraph(CurrentFnDbgScope);
2641 if (PrintDbgScope)
2642 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2643
2644 // Find ranges of instructions covered by each DbgScope;
2645 DbgScope *PrevDbgScope = NULL;
2646 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2647 RE = MIRanges.end(); RI != RE; ++RI) {
2648 const DbgRange &R = *RI;
2649 DbgScope *S = MI2ScopeMap.lookup(R.first);
2650 assert (S && "Lost DbgScope for a machine instruction!");
2651 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2652 PrevDbgScope->closeInsnRange(S);
2653 S->openInsnRange(R.first);
2654 S->extendInsnRange(R.second);
2655 PrevDbgScope = S;
2656 }
2657
2658 if (PrevDbgScope)
2659 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002660
Devang Patele37b0c62010-04-08 18:43:56 +00002661 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002662
2663 return !DbgScopeMap.empty();
2664}
2665
Jim Grosbach1e20b962010-07-21 21:21:52 +00002666/// identifyScopeMarkers() -
Devang Pateleac9c072010-04-27 19:46:33 +00002667/// Each DbgScope has first instruction and last instruction to mark beginning
2668/// and end of a scope respectively. Create an inverse map that list scopes
2669/// starts (and ends) with an instruction. One instruction may start (or end)
2670/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002671void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002672 SmallVector<DbgScope *, 4> WorkList;
2673 WorkList.push_back(CurrentFnDbgScope);
2674 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002675 DbgScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002676
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002677 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002678 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002679 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002680 SE = Children.end(); SI != SE; ++SI)
2681 WorkList.push_back(*SI);
2682
Devang Patel53bb5c92009-11-10 23:06:00 +00002683 if (S->isAbstractScope())
2684 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002685
Devang Pateleac9c072010-04-27 19:46:33 +00002686 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2687 if (Ranges.empty())
2688 continue;
2689 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2690 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002691 assert(RI->first && "DbgRange does not have first instruction!");
2692 assert(RI->second && "DbgRange does not have second instruction!");
Devang Pateleac9c072010-04-27 19:46:33 +00002693 InsnsEndScopeSet.insert(RI->second);
2694 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002695 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002696}
2697
Dan Gohman084751c2010-04-20 00:37:27 +00002698/// FindFirstDebugLoc - Find the first debug location in the function. This
2699/// is intended to be an approximation for the source position of the
2700/// beginning of the function.
2701static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2702 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2703 I != E; ++I)
2704 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2705 MBBI != MBBE; ++MBBI) {
2706 DebugLoc DL = MBBI->getDebugLoc();
2707 if (!DL.isUnknown())
2708 return DL;
2709 }
2710 return DebugLoc();
2711}
2712
Devang Patel9a31f0f2010-10-25 20:45:32 +00002713#ifndef NDEBUG
2714/// CheckLineNumbers - Count basicblocks whose instructions do not have any
2715/// line number information.
2716static void CheckLineNumbers(const MachineFunction *MF) {
2717 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2718 I != E; ++I) {
2719 bool FoundLineNo = false;
2720 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2721 II != IE; ++II) {
2722 const MachineInstr *MI = II;
2723 if (!MI->getDebugLoc().isUnknown()) {
2724 FoundLineNo = true;
2725 break;
2726 }
2727 }
Devang Patel4d7f9a02010-10-28 22:11:59 +00002728 if (!FoundLineNo && I->size())
Devang Patel9a31f0f2010-10-25 20:45:32 +00002729 ++BlocksWithoutLineNo;
2730 }
2731}
2732#endif
2733
Devang Patel2c4ceb12009-11-21 02:48:08 +00002734/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002735/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002736void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002737 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002738 if (!extractScopeInformation()) return;
Devang Patel60b35bd2009-10-06 18:37:31 +00002739
Devang Patel9a31f0f2010-10-25 20:45:32 +00002740#ifndef NDEBUG
2741 CheckLineNumbers(MF);
2742#endif
2743
Devang Pateleac9c072010-04-27 19:46:33 +00002744 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2745 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002746 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002747 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002748
2749 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2750 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002751 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002752 if (FDL.isUnknown()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002753
Devang Patele9f8f5e2010-05-07 20:54:48 +00002754 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings0db42712010-07-19 23:56:30 +00002755 const MDNode *TheScope = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002756
Chris Lattnerde4845c2010-04-02 19:42:39 +00002757 DISubprogram SP = getDISubprogram(Scope);
2758 unsigned Line, Col;
2759 if (SP.Verify()) {
2760 Line = SP.getLineNumber();
2761 Col = 0;
Stuart Hastings0db42712010-07-19 23:56:30 +00002762 TheScope = SP;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002763 } else {
2764 Line = FDL.getLine();
2765 Col = FDL.getCol();
Stuart Hastings0db42712010-07-19 23:56:30 +00002766 TheScope = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002767 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002768
Stuart Hastings0db42712010-07-19 23:56:30 +00002769 recordSourceLine(Line, Col, TheScope);
Devang Patelb2b31a62010-05-26 19:37:24 +00002770
Devang Patelb9abe9f2010-06-02 16:42:51 +00002771 /// ProcessedArgs - Collection of arguments already processed.
2772 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2773
Devang Patelb2b31a62010-05-26 19:37:24 +00002774 DebugLoc PrevLoc;
2775 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2776 I != E; ++I)
2777 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2778 II != IE; ++II) {
2779 const MachineInstr *MI = II;
2780 DebugLoc DL = MI->getDebugLoc();
2781 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002782 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2783 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2784 if (!DV.Verify()) continue;
Devang Patel55e97172010-05-27 16:47:30 +00002785 // If DBG_VALUE is for a local variable then it needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002786 if (DV.getTag() != dwarf::DW_TAG_arg_variable
Devang Patel7fb231c2010-07-01 21:38:08 +00002787 && isDbgValueInUndefinedReg(MI) == false)
Devang Patelb2b31a62010-05-26 19:37:24 +00002788 InsnNeedsLabel.insert(MI);
Devang Patel55e97172010-05-27 16:47:30 +00002789 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel7fb231c2010-07-01 21:38:08 +00002790 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2791 describes(MF->getFunction()))
Devang Patel55e97172010-05-27 16:47:30 +00002792 InsnNeedsLabel.insert(MI);
2793 // DBG_VALUE indicating argument location change needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002794 else if (isDbgValueInUndefinedReg(MI) == false
2795 && !ProcessedArgs.insert(DV))
Devang Patelb2b31a62010-05-26 19:37:24 +00002796 InsnNeedsLabel.insert(MI);
2797 } else {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002798 // If location is unknown then instruction needs a location only if
Devang Patelb2b31a62010-05-26 19:37:24 +00002799 // UnknownLocations flag is set.
2800 if (DL.isUnknown()) {
2801 if (UnknownLocations && !PrevLoc.isUnknown())
2802 InsnNeedsLabel.insert(MI);
2803 } else if (DL != PrevLoc)
2804 // Otherwise, instruction needs a location only if it is new location.
2805 InsnNeedsLabel.insert(MI);
2806 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002807
Devang Patelb2b31a62010-05-26 19:37:24 +00002808 if (!DL.isUnknown() || UnknownLocations)
2809 PrevLoc = DL;
2810 }
2811
2812 PrevLabel = FunctionBeginSym;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002813}
2814
Devang Patel2c4ceb12009-11-21 02:48:08 +00002815/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002816///
Chris Lattnereec791a2010-01-26 23:18:02 +00002817void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002818 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002819
Devang Patel344130e2010-01-04 20:44:00 +00002820 if (CurrentFnDbgScope) {
Devang Patel65eb4822010-05-22 00:04:14 +00002821
Devang Patelc3f5f782010-05-25 23:40:22 +00002822 // Define end label for subprogram.
2823 FunctionEndSym = Asm->GetTempSymbol("func_end",
2824 Asm->getFunctionNumber());
2825 // Assumes in correct section after the entry point.
2826 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002827
Devang Patel78e127d2010-06-25 22:07:34 +00002828 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2829 collectVariableInfo(MF, ProcessedVars);
Devang Patel65eb4822010-05-22 00:04:14 +00002830
Devang Patel344130e2010-01-04 20:44:00 +00002831 // Construct abstract scopes.
2832 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel78e127d2010-06-25 22:07:34 +00002833 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel78e127d2010-06-25 22:07:34 +00002834 DISubprogram SP((*AI)->getScopeNode());
2835 if (SP.Verify()) {
2836 // Collect info for variables that were optimized out.
2837 StringRef FName = SP.getLinkageName();
2838 if (FName.empty())
2839 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002840 if (NamedMDNode *NMD =
2841 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
Devang Patel78e127d2010-06-25 22:07:34 +00002842 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002843 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00002844 if (!DV || !ProcessedVars.insert(DV))
2845 continue;
Devang Patel1d68d212010-06-30 00:11:08 +00002846 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel78e127d2010-06-25 22:07:34 +00002847 if (Scope)
2848 Scope->addVariable(new DbgVariable(DV));
2849 }
2850 }
2851 }
Devang Patel90e19aa2010-06-30 01:40:11 +00002852 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2853 constructScopeDIE(*AI);
Devang Patel78e127d2010-06-25 22:07:34 +00002854 }
Devang Patel61409622010-07-07 20:12:52 +00002855
Devang Patel9c488372010-05-04 06:15:30 +00002856 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002857
Devang Patel9c488372010-05-04 06:15:30 +00002858 if (!DisableFramePointerElim(*MF))
Jim Grosbach1e20b962010-07-21 21:21:52 +00002859 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
Devang Patel9c488372010-05-04 06:15:30 +00002860 dwarf::DW_FORM_flag, 1);
2861
2862
Chris Lattnerd38fee82010-04-05 00:13:49 +00002863 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002864 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002865 }
2866
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002867 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002868 CurrentFnDbgScope = NULL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002869 InsnNeedsLabel.clear();
Devang Patel26c1e562010-05-20 16:36:41 +00002870 DbgVariableToFrameIndexMap.clear();
2871 VarToAbstractVarMap.clear();
2872 DbgVariableToDbgInstMap.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002873 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002874 InsnsEndScopeSet.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002875 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002876 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002877 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002878 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002879 LabelsBeforeInsn.clear();
2880 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002881 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002882}
2883
Devang Patel26c1e562010-05-20 16:36:41 +00002884/// recordVariableFrameIndex - Record a variable's index.
2885void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2886 assert (V && "Invalid DbgVariable!");
2887 DbgVariableToFrameIndexMap[V] = Index;
2888}
2889
2890/// findVariableFrameIndex - Return true if frame index for the variable
2891/// is found. Update FI to hold value of the index.
2892bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2893 assert (V && "Invalid DbgVariable!");
2894 DenseMap<const DbgVariable *, int>::iterator I =
2895 DbgVariableToFrameIndexMap.find(V);
2896 if (I == DbgVariableToFrameIndexMap.end())
2897 return false;
2898 *FI = I->second;
2899 return true;
2900}
2901
Jim Grosbach1e20b962010-07-21 21:21:52 +00002902/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patelee432862010-05-20 19:57:06 +00002903/// instruction.
2904DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2905 DbgScope *Scope = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002906 LLVMContext &Ctx =
Devang Patelee432862010-05-20 19:57:06 +00002907 MInsn->getParent()->getParent()->getFunction()->getContext();
2908 DebugLoc DL = MInsn->getDebugLoc();
2909
Jim Grosbach1e20b962010-07-21 21:21:52 +00002910 if (DL.isUnknown())
Devang Patelee432862010-05-20 19:57:06 +00002911 return Scope;
2912
2913 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2914 Scope = ConcreteScopes.lookup(IA);
2915 if (Scope == 0)
2916 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002917
Devang Patelee432862010-05-20 19:57:06 +00002918 return Scope;
2919}
2920
2921
Chris Lattnerc6087842010-03-09 04:54:43 +00002922/// recordSourceLine - Register a source line with debug info. Returns the
2923/// unique label that was emitted and which provides correspondence to
2924/// the source line list.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002925MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel61409622010-07-07 20:12:52 +00002926 const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00002927 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002928
Dan Gohman1cc0d622010-05-05 23:41:32 +00002929 unsigned Src = 1;
2930 if (S) {
2931 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002932
Dan Gohman1cc0d622010-05-05 23:41:32 +00002933 if (Scope.isCompileUnit()) {
2934 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002935 Fn = CU.getFilename();
Devang Patel3cabc9d2010-10-28 17:30:52 +00002936 } else if (Scope.isFile()) {
2937 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00002938 Fn = F.getFilename();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002939 } else if (Scope.isSubprogram()) {
2940 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002941 Fn = SP.getFilename();
2942 } else if (Scope.isLexicalBlock()) {
2943 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002944 Fn = DB.getFilename();
2945 } else
2946 assert(0 && "Unexpected scope info");
2947
Rafael Espindola5c055632010-11-18 02:04:25 +00002948 Src = GetOrCreateSourceID(Fn);
Dan Gohman1cc0d622010-05-05 23:41:32 +00002949 }
2950
Rafael Espindola5c055632010-11-18 02:04:25 +00002951 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, DWARF2_FLAG_IS_STMT,
2952 0, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002953
Rafael Espindola5c055632010-11-18 02:04:25 +00002954 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattnerc6087842010-03-09 04:54:43 +00002955 Asm->OutStreamer.EmitLabel(Label);
2956 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002957}
2958
Bill Wendling829e67b2009-05-20 23:22:40 +00002959//===----------------------------------------------------------------------===//
2960// Emit Methods
2961//===----------------------------------------------------------------------===//
2962
Devang Patel2c4ceb12009-11-21 02:48:08 +00002963/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002964///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002965unsigned
2966DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002967 // Get the children.
2968 const std::vector<DIE *> &Children = Die->getChildren();
2969
2970 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002971 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00002972 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00002973
2974 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002975 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002976
2977 // Get the abbreviation for this DIE.
2978 unsigned AbbrevNumber = Die->getAbbrevNumber();
2979 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2980
2981 // Set DIE offset
2982 Die->setOffset(Offset);
2983
2984 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002985 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002986
2987 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2988 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2989
2990 // Size the DIE attribute values.
2991 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2992 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00002993 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00002994
2995 // Size the DIE children if any.
2996 if (!Children.empty()) {
2997 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2998 "Children flag not set");
2999
3000 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003001 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00003002
3003 // End of children marker.
3004 Offset += sizeof(int8_t);
3005 }
3006
3007 Die->setSize(Offset - Die->getOffset());
3008 return Offset;
3009}
3010
Devang Patel2c4ceb12009-11-21 02:48:08 +00003011/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00003012///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003013void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00003014 unsigned PrevOffset = 0;
3015 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3016 E = CUMap.end(); I != E; ++I) {
3017 // Compute size of compile unit header.
3018 static unsigned Offset = PrevOffset +
3019 sizeof(int32_t) + // Length of Compilation Unit Info
3020 sizeof(int16_t) + // DWARF version number
3021 sizeof(int32_t) + // Offset Into Abbrev. Section
3022 sizeof(int8_t); // Pointer Size (in bytes)
3023 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
3024 PrevOffset = Offset;
3025 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003026}
3027
Chris Lattner11b8f302010-04-04 23:02:02 +00003028/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
3029/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00003030static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00003031 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003032 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00003033 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003034
Chris Lattner9c69e285532010-04-04 22:59:04 +00003035 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
3036 Asm->OutStreamer.EmitLabel(TmpSym);
3037 return TmpSym;
3038}
3039
3040/// EmitSectionLabels - Emit initial Dwarf sections with a label at
3041/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00003042void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003043 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003044
Bill Wendling94d04b82009-05-20 23:21:38 +00003045 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003046 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003047 DwarfFrameSectionSym =
3048 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3049 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003050
Jim Grosbach1e20b962010-07-21 21:21:52 +00003051 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003052 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003053 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003054 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00003055 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003056
Chris Lattner9c69e285532010-04-04 22:59:04 +00003057 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00003058 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003059
Devang Patelaf608bd2010-08-24 00:06:12 +00003060 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00003061 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3062 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3063 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003064 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003065 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00003066 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3067 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00003068
Devang Patelc3f5f782010-05-25 23:40:22 +00003069 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3070 "section_debug_loc");
3071
Chris Lattner9c69e285532010-04-04 22:59:04 +00003072 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00003073 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003074}
3075
Devang Patel2c4ceb12009-11-21 02:48:08 +00003076/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00003077///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003078void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003079 // Get the abbreviation for this DIE.
3080 unsigned AbbrevNumber = Die->getAbbrevNumber();
3081 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3082
Bill Wendling94d04b82009-05-20 23:21:38 +00003083 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00003084 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00003085 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3086 Twine::utohexstr(Die->getOffset()) + ":0x" +
3087 Twine::utohexstr(Die->getSize()) + " " +
3088 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003089 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003090
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003091 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00003092 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3093
3094 // Emit the DIE attribute values.
3095 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3096 unsigned Attr = AbbrevData[i].getAttribute();
3097 unsigned Form = AbbrevData[i].getForm();
3098 assert(Form && "Too many attributes for DIE (check abbreviation)");
3099
Chris Lattner3f53c832010-04-04 18:52:31 +00003100 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00003101 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003102
Bill Wendling94d04b82009-05-20 23:21:38 +00003103 switch (Attr) {
3104 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00003105 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003106 break;
3107 case dwarf::DW_AT_abstract_origin: {
3108 DIEEntry *E = cast<DIEEntry>(Values[i]);
3109 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00003110 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00003111 Asm->EmitInt32(Addr);
3112 break;
3113 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003114 case dwarf::DW_AT_ranges: {
3115 // DW_AT_range Value encodes offset in debug_range section.
3116 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00003117
3118 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
3119 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
3120 V->getValue(),
3121 4);
3122 } else {
3123 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3124 V->getValue(),
3125 DwarfDebugRangeSectionSym,
3126 4);
3127 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003128 break;
3129 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003130 case dwarf::DW_AT_location: {
3131 if (UseDotDebugLocEntry.count(Die) != 0) {
3132 DIELabel *L = cast<DIELabel>(Values[i]);
3133 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3134 } else
3135 Values[i]->EmitValue(Asm, Form);
3136 break;
3137 }
Devang Patel2a361602010-09-29 19:08:08 +00003138 case dwarf::DW_AT_accessibility: {
3139 if (Asm->isVerbose()) {
3140 DIEInteger *V = cast<DIEInteger>(Values[i]);
3141 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
3142 }
3143 Values[i]->EmitValue(Asm, Form);
3144 break;
3145 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003146 default:
3147 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003148 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00003149 break;
3150 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003151 }
3152
3153 // Emit the DIE children if any.
3154 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3155 const std::vector<DIE *> &Children = Die->getChildren();
3156
3157 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003158 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00003159
Chris Lattner3f53c832010-04-04 18:52:31 +00003160 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00003161 Asm->OutStreamer.AddComment("End Of Children Mark");
3162 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003163 }
3164}
3165
Devang Patel8a241142009-12-09 18:24:21 +00003166/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003167///
Devang Patel8a241142009-12-09 18:24:21 +00003168void DwarfDebug::emitDebugInfo() {
3169 // Start debug info section.
3170 Asm->OutStreamer.SwitchSection(
3171 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00003172 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3173 E = CUMap.end(); I != E; ++I) {
3174 CompileUnit *TheCU = I->second;
3175 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00003176
Devang Patel163a9f72010-05-10 22:49:55 +00003177 // Emit the compile units header.
3178 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3179 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003180
Devang Patel163a9f72010-05-10 22:49:55 +00003181 // Emit size of content not including length itself
3182 unsigned ContentSize = Die->getSize() +
3183 sizeof(int16_t) + // DWARF version number
3184 sizeof(int32_t) + // Offset Into Abbrev. Section
3185 sizeof(int8_t) + // Pointer Size (in bytes)
3186 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003187
Devang Patel163a9f72010-05-10 22:49:55 +00003188 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3189 Asm->EmitInt32(ContentSize);
3190 Asm->OutStreamer.AddComment("DWARF version number");
3191 Asm->EmitInt16(dwarf::DWARF_VERSION);
3192 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3193 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3194 DwarfAbbrevSectionSym);
3195 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3196 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003197
Devang Patel163a9f72010-05-10 22:49:55 +00003198 emitDIE(Die);
3199 // FIXME - extra padding for gdb bug.
3200 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3201 Asm->EmitInt8(0);
3202 Asm->EmitInt8(0);
3203 Asm->EmitInt8(0);
3204 Asm->EmitInt8(0);
3205 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3206 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003207}
3208
Devang Patel2c4ceb12009-11-21 02:48:08 +00003209/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003210///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003211void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00003212 // Check to see if it is worth the effort.
3213 if (!Abbreviations.empty()) {
3214 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003215 Asm->OutStreamer.SwitchSection(
3216 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003217
Chris Lattnerc0215722010-04-04 19:25:43 +00003218 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003219
3220 // For each abbrevation.
3221 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3222 // Get abbreviation data
3223 const DIEAbbrev *Abbrev = Abbreviations[i];
3224
3225 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003226 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00003227
3228 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003229 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00003230 }
3231
3232 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003233 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00003234
Chris Lattnerc0215722010-04-04 19:25:43 +00003235 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003236 }
3237}
3238
Devang Patel2c4ceb12009-11-21 02:48:08 +00003239/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00003240/// the line matrix.
3241///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003242void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003243 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00003244 Asm->OutStreamer.AddComment("Extended Op");
3245 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003246
Chris Lattner233f52b2010-03-09 23:52:58 +00003247 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003248 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003249 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3250 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3251
3252 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003253
Chris Lattnerc0215722010-04-04 19:25:43 +00003254 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003255 Asm->getTargetData().getPointerSize(),
3256 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003257
3258 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00003259 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3260 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00003261 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00003262 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003263}
3264
Devang Patel2c4ceb12009-11-21 02:48:08 +00003265/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003266///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003267void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003268 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003269 return;
3270
Chris Lattnerd38fee82010-04-05 00:13:49 +00003271 int stackGrowth = Asm->getTargetData().getPointerSize();
3272 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3273 TargetFrameInfo::StackGrowsDown)
3274 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003275
3276 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003277 Asm->OutStreamer.SwitchSection(
3278 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003279
Chris Lattnerc0215722010-04-04 19:25:43 +00003280 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003281 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003282 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3283 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003284
Chris Lattnerc0215722010-04-04 19:25:43 +00003285 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003286 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003287 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003288 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003289 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003290 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003291 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003292 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3293 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003294 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003295 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Anton Korobeynikovd9e33852010-11-18 23:25:52 +00003296 const TargetFrameInfo *TFI = Asm->TM.getFrameInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003297 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003298
3299 std::vector<MachineMove> Moves;
Anton Korobeynikovd9e33852010-11-18 23:25:52 +00003300 TFI->getInitialFrameState(Moves);
Bill Wendling94d04b82009-05-20 23:21:38 +00003301
Chris Lattner02b86b92010-04-04 23:41:46 +00003302 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003303
Chris Lattner059ea132010-04-28 01:05:45 +00003304 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003305 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003306}
3307
Devang Patel2c4ceb12009-11-21 02:48:08 +00003308/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003309/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003310void DwarfDebug::
3311emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003312 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003313 return;
3314
3315 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003316 Asm->OutStreamer.SwitchSection(
3317 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003318
Chris Lattner233f52b2010-03-09 23:52:58 +00003319 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003320 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003321 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003322 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003323 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003324 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003325
Chris Lattner206d61e2010-03-13 07:26:18 +00003326 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003327
Chris Lattner233f52b2010-03-09 23:52:58 +00003328 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003329 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner6189ed12010-04-04 23:25:33 +00003330 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003331
Chris Lattner233f52b2010-03-09 23:52:58 +00003332 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003333 MCSymbol *FuncBeginSym =
3334 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003335 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003336 Asm->getTargetData().getPointerSize(),
3337 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003338
3339
Chris Lattner233f52b2010-03-09 23:52:58 +00003340 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003341 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003342 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003343
Chris Lattner02b86b92010-04-04 23:41:46 +00003344 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003345
Chris Lattner059ea132010-04-28 01:05:45 +00003346 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003347 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003348}
3349
Devang Patel8a241142009-12-09 18:24:21 +00003350/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3351///
3352void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00003353 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3354 E = CUMap.end(); I != E; ++I) {
3355 CompileUnit *TheCU = I->second;
3356 // Start the dwarf pubnames section.
3357 Asm->OutStreamer.SwitchSection(
3358 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003359
Devang Patel163a9f72010-05-10 22:49:55 +00003360 Asm->OutStreamer.AddComment("Length of Public Names Info");
3361 Asm->EmitLabelDifference(
3362 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3363 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003364
Devang Patel163a9f72010-05-10 22:49:55 +00003365 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3366 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003367
Devang Patel163a9f72010-05-10 22:49:55 +00003368 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003369 Asm->EmitInt16(dwarf::DWARF_VERSION);
3370
Devang Patel163a9f72010-05-10 22:49:55 +00003371 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003372 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00003373 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003374
Devang Patel163a9f72010-05-10 22:49:55 +00003375 Asm->OutStreamer.AddComment("Compilation Unit Length");
3376 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3377 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3378 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003379
Devang Patel163a9f72010-05-10 22:49:55 +00003380 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3381 for (StringMap<DIE*>::const_iterator
3382 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3383 const char *Name = GI->getKeyData();
3384 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003385
Devang Patel163a9f72010-05-10 22:49:55 +00003386 Asm->OutStreamer.AddComment("DIE offset");
3387 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003388
Devang Patel163a9f72010-05-10 22:49:55 +00003389 if (Asm->isVerbose())
3390 Asm->OutStreamer.AddComment("External Name");
3391 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3392 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003393
Devang Patel163a9f72010-05-10 22:49:55 +00003394 Asm->OutStreamer.AddComment("End Mark");
3395 Asm->EmitInt32(0);
3396 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3397 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003398 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003399}
3400
Devang Patel193f7202009-11-24 01:14:22 +00003401void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00003402 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3403 E = CUMap.end(); I != E; ++I) {
3404 CompileUnit *TheCU = I->second;
3405 // Start the dwarf pubnames section.
3406 Asm->OutStreamer.SwitchSection(
3407 Asm->getObjFileLowering().getDwarfPubTypesSection());
3408 Asm->OutStreamer.AddComment("Length of Public Types Info");
3409 Asm->EmitLabelDifference(
3410 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3411 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003412
Devang Patel163a9f72010-05-10 22:49:55 +00003413 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3414 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003415
Devang Patel163a9f72010-05-10 22:49:55 +00003416 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3417 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003418
Devang Patel163a9f72010-05-10 22:49:55 +00003419 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3420 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3421 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003422
Devang Patel163a9f72010-05-10 22:49:55 +00003423 Asm->OutStreamer.AddComment("Compilation Unit Length");
3424 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3425 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3426 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003427
Devang Patel163a9f72010-05-10 22:49:55 +00003428 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3429 for (StringMap<DIE*>::const_iterator
3430 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3431 const char *Name = GI->getKeyData();
3432 DIE * Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003433
Devang Patel163a9f72010-05-10 22:49:55 +00003434 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3435 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003436
Devang Patel163a9f72010-05-10 22:49:55 +00003437 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3438 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3439 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003440
Devang Patel163a9f72010-05-10 22:49:55 +00003441 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003442 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00003443 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3444 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003445 }
Devang Patel193f7202009-11-24 01:14:22 +00003446}
3447
Devang Patel2c4ceb12009-11-21 02:48:08 +00003448/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003449///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003450void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003451 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003452 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003453
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003454 // Start the dwarf str section.
3455 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003456 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003457
Chris Lattnerbc733f52010-03-13 02:17:42 +00003458 // Get all of the string pool entries and put them in an array by their ID so
3459 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003460 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00003461 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003462
Chris Lattnerbc733f52010-03-13 02:17:42 +00003463 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3464 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3465 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003466
Chris Lattnerbc733f52010-03-13 02:17:42 +00003467 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003468
Chris Lattnerbc733f52010-03-13 02:17:42 +00003469 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003470 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003471 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003472
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003473 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003474 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003475 }
3476}
3477
Devang Patel2c4ceb12009-11-21 02:48:08 +00003478/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003479///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003480void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00003481 if (DotDebugLocEntries.empty())
3482 return;
3483
Bill Wendling94d04b82009-05-20 23:21:38 +00003484 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003485 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00003486 Asm->getObjFileLowering().getDwarfLocSection());
3487 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00003488 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3489 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003490 for (SmallVector<DotDebugLocEntry, 4>::iterator
3491 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00003492 I != E; ++I, ++index) {
Devang Patelc3f5f782010-05-25 23:40:22 +00003493 DotDebugLocEntry Entry = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00003494 if (Entry.isEmpty()) {
3495 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3496 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00003497 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00003498 } else {
3499 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3500 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3501 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3502 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patelb2cf5812010-08-04 18:40:52 +00003503 if (int Offset = Entry.Loc.getOffset()) {
3504 // If the value is at a certain offset from frame register then
3505 // use DW_OP_fbreg.
3506 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patelc3f5f782010-05-25 23:40:22 +00003507 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patelb2cf5812010-08-04 18:40:52 +00003508 Asm->EmitInt16(1 + OffsetSize);
3509 Asm->OutStreamer.AddComment(
3510 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
3511 Asm->EmitInt8(dwarf::DW_OP_fbreg);
3512 Asm->OutStreamer.AddComment("Offset");
3513 Asm->EmitSLEB128(Offset);
Devang Patelc3f5f782010-05-25 23:40:22 +00003514 } else {
Devang Patelb2cf5812010-08-04 18:40:52 +00003515 if (Reg < 32) {
3516 Asm->OutStreamer.AddComment("Loc expr size");
3517 Asm->EmitInt16(1);
3518 Asm->OutStreamer.AddComment(
Devang Patela54e0cc2010-08-04 20:32:36 +00003519 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patelb2cf5812010-08-04 18:40:52 +00003520 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3521 } else {
3522 Asm->OutStreamer.AddComment("Loc expr size");
3523 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
3524 Asm->EmitInt8(dwarf::DW_OP_regx);
3525 Asm->EmitULEB128(Reg);
3526 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003527 }
3528 }
3529 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003530}
3531
3532/// EmitDebugARanges - Emit visible names into a debug aranges section.
3533///
3534void DwarfDebug::EmitDebugARanges() {
3535 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003536 Asm->OutStreamer.SwitchSection(
3537 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003538}
3539
Devang Patel2c4ceb12009-11-21 02:48:08 +00003540/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003541///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003542void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003543 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003544 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003545 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003546 unsigned char Size = Asm->getTargetData().getPointerSize();
3547 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00003548 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00003549 I != E; ++I) {
3550 if (*I)
3551 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003552 else
Devang Pateleac9c072010-04-27 19:46:33 +00003553 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003554 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003555}
3556
Devang Patel2c4ceb12009-11-21 02:48:08 +00003557/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003558///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003559void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003560 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003561 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003562 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003563 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003564 }
3565}
3566
Devang Patel2c4ceb12009-11-21 02:48:08 +00003567/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003568/// Section Header:
3569/// 1. length of section
3570/// 2. Dwarf version number
3571/// 3. address size.
3572///
3573/// Entries (one "entry" for each function that was inlined):
3574///
3575/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3576/// otherwise offset into __debug_str for regular function name.
3577/// 2. offset into __debug_str section for regular function name.
3578/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3579/// instances for the function.
3580///
3581/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3582/// inlined instance; the die_offset points to the inlined_subroutine die in the
3583/// __debug_info section, and the low_pc is the starting address for the
3584/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003585void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003586 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003587 return;
3588
Devang Patel163a9f72010-05-10 22:49:55 +00003589 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003590 return;
3591
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003592 Asm->OutStreamer.SwitchSection(
3593 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003594
Chris Lattner233f52b2010-03-09 23:52:58 +00003595 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003596 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3597 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003598
Chris Lattnerc0215722010-04-04 19:25:43 +00003599 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003600
Chris Lattner233f52b2010-03-09 23:52:58 +00003601 Asm->OutStreamer.AddComment("Dwarf Version");
3602 Asm->EmitInt16(dwarf::DWARF_VERSION);
3603 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003604 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003605
Devang Patele9f8f5e2010-05-07 20:54:48 +00003606 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003607 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003608
Devang Patele9f8f5e2010-05-07 20:54:48 +00003609 const MDNode *Node = *I;
3610 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003611 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003612 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003613 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003614 StringRef LName = SP.getLinkageName();
3615 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003616
Chris Lattner233f52b2010-03-09 23:52:58 +00003617 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003618 if (LName.empty()) {
3619 Asm->OutStreamer.EmitBytes(Name, 0);
3620 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003621 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003622 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3623 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003624
Chris Lattner233f52b2010-03-09 23:52:58 +00003625 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003626 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003627 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003628
Devang Patel53bb5c92009-11-10 23:06:00 +00003629 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003630 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003631 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003632 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003633
Chris Lattner3f53c832010-04-04 18:52:31 +00003634 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003635 Asm->OutStreamer.EmitSymbolValue(LI->first,
3636 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003637 }
3638 }
3639
Chris Lattnerc0215722010-04-04 19:25:43 +00003640 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003641}