blob: 8356d1874cb87e1c71946716adef479bc2d30e21 [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendling0310d762009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendling0310d762009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000032#include "llvm/Analysis/DebugInfo.h"
Devang Patel9a31f0f2010-10-25 20:45:32 +000033#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000034#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000035#include "llvm/ADT/StringExtras.h"
Devang Pateleac9c072010-04-27 19:46:33 +000036#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000039#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000040#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000041#include "llvm/Support/Timer.h"
42#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000043using namespace llvm;
44
Devang Pateleac9c072010-04-27 19:46:33 +000045static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
46 cl::desc("Print DbgScope information for each machine instruction"));
47
Jim Grosbach1e20b962010-07-21 21:21:52 +000048static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000049 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000050 cl::desc("Disable debug info printing"));
51
Dan Gohman281d65d2010-05-07 01:08:53 +000052static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
53 cl::desc("Make an absense of debug location information explicit."),
54 cl::init(false));
55
Nick Lewyckya568d662010-10-26 00:51:57 +000056#ifndef NDEBUG
Devang Patel9a31f0f2010-10-25 20:45:32 +000057STATISTIC(BlocksWithoutLineNo, "Number of blocks without any line number");
Nick Lewyckya568d662010-10-26 00:51:57 +000058#endif
Devang Patel9a31f0f2010-10-25 20:45:32 +000059
Bill Wendling5f017e82010-04-07 09:28:04 +000060namespace {
61 const char *DWARFGroupName = "DWARF Emission";
62 const char *DbgTimerName = "DWARF Debug Writer";
63} // end anonymous namespace
64
Bill Wendling0310d762009-05-15 09:23:25 +000065//===----------------------------------------------------------------------===//
66
67/// Configuration values for initial hash set sizes (log2).
68///
Bill Wendling0310d762009-05-15 09:23:25 +000069static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000070
71namespace llvm {
72
73//===----------------------------------------------------------------------===//
74/// CompileUnit - This dwarf writer support class manages information associate
75/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000076class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000077 /// ID - File identifier for source.
78 ///
79 unsigned ID;
80
81 /// Die - Compile unit debug information entry.
82 ///
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000083 const OwningPtr<DIE> CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000084
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +000085 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel6f01d9c2009-11-21 00:31:03 +000086 DIE *IndexTyDie;
87
Devang Patel869aa462010-07-07 20:49:57 +000088 /// MDNodeToDieMap - Tracks the mapping of unit level debug informaton
Bill Wendling0310d762009-05-15 09:23:25 +000089 /// variables to debug information entries.
Devang Patel869aa462010-07-07 20:49:57 +000090 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000091
Devang Patel869aa462010-07-07 20:49:57 +000092 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug informaton
Bill Wendling0310d762009-05-15 09:23:25 +000093 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel869aa462010-07-07 20:49:57 +000094 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000095
96 /// Globals - A map of globally visible named entities for this unit.
97 ///
98 StringMap<DIE*> Globals;
99
Devang Patel193f7202009-11-24 01:14:22 +0000100 /// GlobalTypes - A map of globally visible types for this unit.
101 ///
102 StringMap<DIE*> GlobalTypes;
103
Bill Wendling0310d762009-05-15 09:23:25 +0000104public:
105 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000106 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000107
108 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +0000109 unsigned getID() const { return ID; }
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +0000110 DIE* getCUDie() const { return CUDie.get(); }
Devang Patel193f7202009-11-24 01:14:22 +0000111 const StringMap<DIE*> &getGlobals() const { return Globals; }
112 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +0000113
114 /// hasContent - Return true if this compile unit has something to write out.
115 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000116 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000117
Devang Patel2c4ceb12009-11-21 02:48:08 +0000118 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000119 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000120 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +0000121
Devang Patel193f7202009-11-24 01:14:22 +0000122 /// addGlobalType - Add a new global type to the compile unit.
123 ///
Jim Grosbach1e20b962010-07-21 21:21:52 +0000124 void addGlobalType(StringRef Name, DIE *Die) {
125 GlobalTypes[Name] = Die;
Devang Patel193f7202009-11-24 01:14:22 +0000126 }
127
Devang Patel017d1212009-11-20 21:37:22 +0000128 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000129 /// specified debug variable.
Devang Patel869aa462010-07-07 20:49:57 +0000130 DIE *getDIE(const MDNode *N) { return MDNodeToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000131
Devang Patel017d1212009-11-20 21:37:22 +0000132 /// insertDIE - Insert DIE into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000133 void insertDIE(const MDNode *N, DIE *D) {
Devang Patel869aa462010-07-07 20:49:57 +0000134 MDNodeToDieMap.insert(std::make_pair(N, D));
Devang Patel017d1212009-11-20 21:37:22 +0000135 }
Bill Wendling0310d762009-05-15 09:23:25 +0000136
Devang Patel017d1212009-11-20 21:37:22 +0000137 /// getDIEEntry - Returns the debug information entry for the speciefied
138 /// debug variable.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000139 DIEEntry *getDIEEntry(const MDNode *N) {
140 DenseMap<const MDNode *, DIEEntry *>::iterator I =
141 MDNodeToDIEEntryMap.find(N);
Devang Patel869aa462010-07-07 20:49:57 +0000142 if (I == MDNodeToDIEEntryMap.end())
Devang Patel6404e4e2009-12-15 19:16:48 +0000143 return NULL;
144 return I->second;
145 }
Devang Patel017d1212009-11-20 21:37:22 +0000146
147 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000148 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Patel869aa462010-07-07 20:49:57 +0000149 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000150 }
151
Devang Patel2c4ceb12009-11-21 02:48:08 +0000152 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000153 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000154 void addDie(DIE *Buffer) {
155 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000156 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000157
158 // getIndexTyDie - Get an anonymous type for index type.
159 DIE *getIndexTyDie() {
160 return IndexTyDie;
161 }
162
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000163 // setIndexTyDie - Set D as anonymous type for index which can be reused
164 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000165 void setIndexTyDie(DIE *D) {
166 IndexTyDie = D;
167 }
168
Bill Wendling0310d762009-05-15 09:23:25 +0000169};
170
171//===----------------------------------------------------------------------===//
172/// DbgVariable - This class is used to track local variable information.
173///
Devang Patelf76a3d62009-11-16 21:53:40 +0000174class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000175 DIVariable Var; // Variable Descriptor.
Devang Patelc3f5f782010-05-25 23:40:22 +0000176 DIE *TheDIE; // Variable DIE.
177 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
Bill Wendling0310d762009-05-15 09:23:25 +0000178public:
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000179 // AbsVar may be NULL.
Devang Patelc3f5f782010-05-25 23:40:22 +0000180 DbgVariable(DIVariable V) : Var(V), TheDIE(0), DotDebugLocOffset(~0U) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000181
182 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000183 DIVariable getVariable() const { return Var; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000184 void setDIE(DIE *D) { TheDIE = D; }
185 DIE *getDIE() const { return TheDIE; }
Devang Patelc3f5f782010-05-25 23:40:22 +0000186 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
187 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
Devang Patel8bd11de2010-08-09 21:01:39 +0000188 StringRef getName() const { return Var.getName(); }
189 unsigned getTag() const { return Var.getTag(); }
190 bool variableHasComplexAddress() const {
191 assert(Var.Verify() && "Invalid complex DbgVariable!");
192 return Var.hasComplexAddress();
193 }
194 bool isBlockByrefVariable() const {
195 assert(Var.Verify() && "Invalid complex DbgVariable!");
196 return Var.isBlockByrefVariable();
197 }
198 unsigned getNumAddrElements() const {
199 assert(Var.Verify() && "Invalid complex DbgVariable!");
200 return Var.getNumAddrElements();
201 }
202 uint64_t getAddrElement(unsigned i) const {
203 return Var.getAddrElement(i);
204 }
205 DIType getType() const {
206 DIType Ty = Var.getType();
207 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
208 // addresses instead.
209 if (Var.isBlockByrefVariable()) {
210 /* Byref variables, in Blocks, are declared by the programmer as
211 "SomeType VarName;", but the compiler creates a
212 __Block_byref_x_VarName struct, and gives the variable VarName
213 either the struct, or a pointer to the struct, as its type. This
214 is necessary for various behind-the-scenes things the compiler
215 needs to do with by-reference variables in blocks.
216
217 However, as far as the original *programmer* is concerned, the
218 variable should still have type 'SomeType', as originally declared.
219
220 The following function dives into the __Block_byref_x_VarName
221 struct to find the original type of the variable. This will be
222 passed back to the code generating the type for the Debug
223 Information Entry for the variable 'VarName'. 'VarName' will then
224 have the original type 'SomeType' in its debug information.
225
226 The original type 'SomeType' will be the type of the field named
227 'VarName' inside the __Block_byref_x_VarName struct.
228
229 NOTE: In order for this to not completely fail on the debugger
230 side, the Debug Information Entry for the variable VarName needs to
231 have a DW_AT_location that tells the debugger how to unwind through
232 the pointers and __Block_byref_x_VarName struct to find the actual
233 value of the variable. The function addBlockByrefType does this. */
234 DIType subType = Ty;
235 unsigned tag = Ty.getTag();
236
237 if (tag == dwarf::DW_TAG_pointer_type) {
238 DIDerivedType DTy = DIDerivedType(Ty);
239 subType = DTy.getTypeDerivedFrom();
240 }
241
242 DICompositeType blockStruct = DICompositeType(subType);
243 DIArray Elements = blockStruct.getTypeArray();
244
245 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
246 DIDescriptor Element = Elements.getElement(i);
247 DIDerivedType DT = DIDerivedType(Element);
248 if (getName() == DT.getName())
249 return (DT.getTypeDerivedFrom());
250 }
251 return Ty;
252 }
253 return Ty;
254 }
Bill Wendling0310d762009-05-15 09:23:25 +0000255};
256
257//===----------------------------------------------------------------------===//
Devang Pateleac9c072010-04-27 19:46:33 +0000258/// DbgRange - This is used to track range of instructions with identical
259/// debug info scope.
260///
261typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
262
263//===----------------------------------------------------------------------===//
Bill Wendling0310d762009-05-15 09:23:25 +0000264/// DbgScope - This class is used to track scope information.
265///
Devang Patelf76a3d62009-11-16 21:53:40 +0000266class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000267 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000268 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000269 // Location at which this scope is inlined.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000270 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000271 bool AbstractScope; // Abstract Scope
Devang Pateld38dd112009-10-01 18:25:23 +0000272 const MachineInstr *LastInsn; // Last instruction of this scope.
273 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Pateleac9c072010-04-27 19:46:33 +0000274 unsigned DFSIn, DFSOut;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000275 // Scopes defined in scope. Contents not owned.
276 SmallVector<DbgScope *, 4> Scopes;
277 // Variables declared in scope. Contents owned.
278 SmallVector<DbgVariable *, 8> Variables;
Devang Pateleac9c072010-04-27 19:46:33 +0000279 SmallVector<DbgRange, 4> Ranges;
Owen Anderson04c05f72009-06-24 22:53:20 +0000280 // Private state for dump()
281 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000282public:
Devang Patele9f8f5e2010-05-07 20:54:48 +0000283 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000284 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Pateleac9c072010-04-27 19:46:33 +0000285 LastInsn(0), FirstInsn(0),
286 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000287 virtual ~DbgScope();
288
289 // Accessors.
290 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000291 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000292 DIDescriptor getDesc() const { return Desc; }
Devang Patele9f8f5e2010-05-07 20:54:48 +0000293 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
294 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000295 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
Devang Patele03161c2010-08-09 18:51:29 +0000296 const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
Devang Pateleac9c072010-04-27 19:46:33 +0000297 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
298
299 /// openInsnRange - This scope covers instruction range starting from MI.
300 void openInsnRange(const MachineInstr *MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000301 if (!FirstInsn)
Devang Pateleac9c072010-04-27 19:46:33 +0000302 FirstInsn = MI;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000303
Devang Pateleac9c072010-04-27 19:46:33 +0000304 if (Parent)
305 Parent->openInsnRange(MI);
306 }
307
Jim Grosbach1e20b962010-07-21 21:21:52 +0000308 /// extendInsnRange - Extend the current instruction range covered by
Devang Pateleac9c072010-04-27 19:46:33 +0000309 /// this scope.
310 void extendInsnRange(const MachineInstr *MI) {
311 assert (FirstInsn && "MI Range is not open!");
312 LastInsn = MI;
313 if (Parent)
314 Parent->extendInsnRange(MI);
315 }
316
317 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
318 /// until now. This is used when a new scope is encountered while walking
319 /// machine instructions.
320 void closeInsnRange(DbgScope *NewScope = NULL) {
321 assert (LastInsn && "Last insn missing!");
322 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000323 FirstInsn = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +0000324 LastInsn = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000325 // If Parent dominates NewScope then do not close Parent's instruction
Devang Pateleac9c072010-04-27 19:46:33 +0000326 // range.
327 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
328 Parent->closeInsnRange(NewScope);
329 }
330
Devang Patel53bb5c92009-11-10 23:06:00 +0000331 void setAbstractScope() { AbstractScope = true; }
332 bool isAbstractScope() const { return AbstractScope; }
Devang Pateleac9c072010-04-27 19:46:33 +0000333
334 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
335 unsigned getDFSOut() const { return DFSOut; }
336 void setDFSOut(unsigned O) { DFSOut = O; }
337 unsigned getDFSIn() const { return DFSIn; }
338 void setDFSIn(unsigned I) { DFSIn = I; }
339 bool dominates(const DbgScope *S) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000340 if (S == this)
Devang Pateleac9c072010-04-27 19:46:33 +0000341 return true;
342 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
343 return true;
344 return false;
345 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000346
Devang Patel2c4ceb12009-11-21 02:48:08 +0000347 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000348 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000349 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000350
Devang Patel2c4ceb12009-11-21 02:48:08 +0000351 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000352 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000353 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000354
Bill Wendling0310d762009-05-15 09:23:25 +0000355#ifndef NDEBUG
356 void dump() const;
357#endif
358};
Devang Pateleac9c072010-04-27 19:46:33 +0000359
Chris Lattnerea761862010-04-05 04:09:20 +0000360} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000361
362#ifndef NDEBUG
363void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000364 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000365 err.indent(IndentLevel);
Devang Patele9f8f5e2010-05-07 20:54:48 +0000366 const MDNode *N = Desc;
Devang Patel53bb5c92009-11-10 23:06:00 +0000367 N->dump();
Devang Patel53bb5c92009-11-10 23:06:00 +0000368 if (AbstractScope)
369 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000370
371 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000372 if (!Scopes.empty())
373 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000374 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
375 if (Scopes[i] != this)
376 Scopes[i]->dump();
377
378 IndentLevel -= 2;
379}
380#endif
381
Bill Wendling0310d762009-05-15 09:23:25 +0000382DbgScope::~DbgScope() {
Bill Wendling0310d762009-05-15 09:23:25 +0000383 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
384 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000385}
386
Chris Lattner49cd6642010-04-05 05:11:15 +0000387DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000388 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000389 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelf2548ca2010-04-16 23:33:45 +0000390 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000391 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000392
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000393 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
394 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000395 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000396 FunctionBeginSym = FunctionEndSym = 0;
Devang Patelca76f6f2010-07-08 20:10:35 +0000397 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Dan Gohman03c3dc72010-06-18 15:56:31 +0000398 {
399 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
400 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000401 }
Bill Wendling0310d762009-05-15 09:23:25 +0000402}
403DwarfDebug::~DwarfDebug() {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000404 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
405 DIEBlocks[j]->~DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000406}
407
Chris Lattnerbc733f52010-03-13 02:17:42 +0000408MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
409 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
410 if (Entry.first) return Entry.first;
411
412 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000413 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000414}
415
416
Devang Patel2c4ceb12009-11-21 02:48:08 +0000417/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000418///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000419void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000420 // Profile the node so that we can make it unique.
421 FoldingSetNodeID ID;
422 Abbrev.Profile(ID);
423
424 // Check the set for priors.
425 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
426
427 // If it's newly added.
428 if (InSet == &Abbrev) {
429 // Add to abbreviation list.
430 Abbreviations.push_back(&Abbrev);
431
432 // Assign the vector position + 1 as its number.
433 Abbrev.setNumber(Abbreviations.size());
434 } else {
435 // Assign existing abbreviation number.
436 Abbrev.setNumber(InSet->getNumber());
437 }
438}
439
Devang Patel2c4ceb12009-11-21 02:48:08 +0000440/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000441/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000442DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000443 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000444 return Value;
445}
446
Devang Patel2c4ceb12009-11-21 02:48:08 +0000447/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000448///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000449void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000450 unsigned Form, uint64_t Integer) {
451 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000452 DIEValue *Value = Integer == 1 ?
Devang Patelca76f6f2010-07-08 20:10:35 +0000453 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000454 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000455}
456
Devang Patel2c4ceb12009-11-21 02:48:08 +0000457/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000458///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000459void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000460 unsigned Form, int64_t Integer) {
461 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000462 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000463 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000464}
465
Devang Patel69f57b12009-12-02 15:25:16 +0000466/// addString - Add a string attribute data and value. DIEString only
Jim Grosbach1e20b962010-07-21 21:21:52 +0000467/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000468void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner4cf202b2010-01-23 03:11:46 +0000469 StringRef String) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000470 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000471 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000472}
473
Devang Patel2c4ceb12009-11-21 02:48:08 +0000474/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000475///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000476void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000477 const MCSymbol *Label) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000478 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000479 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000480}
481
Devang Patel2c4ceb12009-11-21 02:48:08 +0000482/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000483///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000484void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000485 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000486 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000487 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000488}
489
Chris Lattner74e41f92010-04-05 05:24:55 +0000490/// addDIEEntry - Add a DIE attribute data and value.
491///
492void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
493 DIE *Entry) {
494 Die->addValue(Attribute, Form, createDIEEntry(Entry));
495}
496
497
Devang Patel2c4ceb12009-11-21 02:48:08 +0000498/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000499///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000500void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000501 DIEBlock *Block) {
Chris Lattnera37d5382010-04-05 00:18:22 +0000502 Block->ComputeSize(Asm);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000503 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000504 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000505}
506
Devang Patel2c4ceb12009-11-21 02:48:08 +0000507/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000508/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000509void DwarfDebug::addSourceLine(DIE *Die, DIVariable V) {
Devang Patelc665a512010-05-07 23:33:41 +0000510 // Verify variable.
Devang Patel81625742010-08-09 20:20:05 +0000511 if (!V.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000512 return;
513
Devang Patel81625742010-08-09 20:20:05 +0000514 unsigned Line = V.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000515 if (Line == 0)
516 return;
Devang Patel81625742010-08-09 20:20:05 +0000517 unsigned FileID = GetOrCreateSourceID(V.getContext().getDirectory(),
518 V.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000519 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000520 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
521 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000522}
523
Devang Patel2c4ceb12009-11-21 02:48:08 +0000524/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000525/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000526void DwarfDebug::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patelc665a512010-05-07 23:33:41 +0000527 // Verify global variable.
Devang Patel81625742010-08-09 20:20:05 +0000528 if (!G.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000529 return;
530
Devang Patel81625742010-08-09 20:20:05 +0000531 unsigned Line = G.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000532 if (Line == 0)
533 return;
Devang Patel81625742010-08-09 20:20:05 +0000534 unsigned FileID = GetOrCreateSourceID(G.getContext().getDirectory(),
535 G.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000536 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000537 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
538 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000539}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000540
Devang Patel2c4ceb12009-11-21 02:48:08 +0000541/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000542/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000543void DwarfDebug::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patelc665a512010-05-07 23:33:41 +0000544 // Verify subprogram.
Devang Patel81625742010-08-09 20:20:05 +0000545 if (!SP.Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000546 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000547 // If the line number is 0, don't add it.
Devang Patel81625742010-08-09 20:20:05 +0000548 if (SP.getLineNumber() == 0)
Caroline Ticec6f9d622009-09-11 18:25:54 +0000549 return;
550
Devang Patel81625742010-08-09 20:20:05 +0000551 unsigned Line = SP.getLineNumber();
552 if (!SP.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000553 return;
Devang Patel81625742010-08-09 20:20:05 +0000554 unsigned FileID = GetOrCreateSourceID(SP.getDirectory(),
555 SP.getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000556 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000557 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
558 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000559}
560
Devang Patel2c4ceb12009-11-21 02:48:08 +0000561/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000562/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000563void DwarfDebug::addSourceLine(DIE *Die, DIType Ty) {
Devang Patelc665a512010-05-07 23:33:41 +0000564 // Verify type.
Devang Patel81625742010-08-09 20:20:05 +0000565 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000566 return;
567
Devang Patel81625742010-08-09 20:20:05 +0000568 unsigned Line = Ty.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000569 if (Line == 0 || !Ty.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000570 return;
Devang Patelbc2bb9b2010-10-28 19:50:08 +0000571 unsigned FileID = GetOrCreateSourceID(Ty.getDirectory(),
572 Ty.getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000573 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000574 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
575 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000576}
577
Devang Patel6404e4e2009-12-15 19:16:48 +0000578/// addSourceLine - Add location information to specified debug information
579/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000580void DwarfDebug::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patelc665a512010-05-07 23:33:41 +0000581 // Verify namespace.
Devang Patel81625742010-08-09 20:20:05 +0000582 if (!NS.Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000583 return;
584
Devang Patel81625742010-08-09 20:20:05 +0000585 unsigned Line = NS.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000586 if (Line == 0)
587 return;
Devang Patel81625742010-08-09 20:20:05 +0000588 StringRef FN = NS.getFilename();
589 StringRef Dir = NS.getDirectory();
Devang Patel6404e4e2009-12-15 19:16:48 +0000590
591 unsigned FileID = GetOrCreateSourceID(Dir, FN);
592 assert(FileID && "Invalid file id");
593 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
594 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
595}
596
Devang Patel9e3bd2c2010-08-31 06:11:28 +0000597/// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
598/// on provided frame index.
599void DwarfDebug::addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI) {
600 MachineLocation Location;
601 unsigned FrameReg;
602 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
603 int Offset = RI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
604 Location.set(FrameReg, Offset);
605
Devang Patel8bd11de2010-08-09 21:01:39 +0000606 if (DV->variableHasComplexAddress())
607 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
608 else if (DV->isBlockByrefVariable())
609 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
610 else
611 addAddress(Die, dwarf::DW_AT_location, Location);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000612}
613
Devang Patel2c4ceb12009-11-21 02:48:08 +0000614/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000615/// and generate the DWARF information necessary to find the actual variable
616/// given the extra address information encoded in the DIVariable, starting from
617/// the starting location. Add the DWARF information to the die.
618///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000619void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000620 unsigned Attribute,
621 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000622 DIType Ty = DV->getType();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000623
624 // Decode the original location, and use that as the start of the byref
625 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000626 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000627 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000628 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000629
630 if (Location.isReg()) {
631 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000632 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000633 } else {
634 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000635 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
636 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000637 }
638 } else {
639 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000640 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000641 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000642 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
643 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000644 }
645
Devang Patel2c4ceb12009-11-21 02:48:08 +0000646 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000647 }
648
Devang Patel8bd11de2010-08-09 21:01:39 +0000649 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
650 uint64_t Element = DV->getAddrElement(i);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000651
652 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000653 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel8bd11de2010-08-09 21:01:39 +0000654 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000655 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000656 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000657 } else llvm_unreachable("unknown DIFactory Opcode");
658 }
659
660 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000661 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000662}
663
Caroline Ticedc8f6042009-08-31 21:19:37 +0000664/* Byref variables, in Blocks, are declared by the programmer as "SomeType
665 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
666 gives the variable VarName either the struct, or a pointer to the struct, as
667 its type. This is necessary for various behind-the-scenes things the
668 compiler needs to do with by-reference variables in Blocks.
669
670 However, as far as the original *programmer* is concerned, the variable
671 should still have type 'SomeType', as originally declared.
672
Devang Patel2c4ceb12009-11-21 02:48:08 +0000673 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000674 struct to find the original type of the variable, which is then assigned to
675 the variable's Debug Information Entry as its real type. So far, so good.
676 However now the debugger will expect the variable VarName to have the type
677 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000678 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000679 pointers and struct to find the actual variable of type SomeType.
680
681 The following function does just that. We start by getting
682 the "normal" location for the variable. This will be the location
683 of either the struct __Block_byref_x_VarName or the pointer to the
684 struct __Block_byref_x_VarName.
685
686 The struct will look something like:
687
688 struct __Block_byref_x_VarName {
689 ... <various fields>
690 struct __Block_byref_x_VarName *forwarding;
691 ... <various other fields>
692 SomeType VarName;
693 ... <maybe more fields>
694 };
695
696 If we are given the struct directly (as our starting point) we
697 need to tell the debugger to:
698
699 1). Add the offset of the forwarding field.
700
Dan Gohmanf451cb82010-02-10 16:03:48 +0000701 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000702 struct to use (the real one may have been copied onto the heap).
703
704 3). Add the offset for the field VarName, to find the actual variable.
705
706 If we started with a pointer to the struct, then we need to
707 dereference that pointer first, before the other steps.
708 Translating this into DWARF ops, we will need to append the following
709 to the current location description for the variable:
710
711 DW_OP_deref -- optional, if we start with a pointer
712 DW_OP_plus_uconst <forward_fld_offset>
713 DW_OP_deref
714 DW_OP_plus_uconst <varName_fld_offset>
715
716 That is what this function does. */
717
Devang Patel2c4ceb12009-11-21 02:48:08 +0000718/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000719/// provided, and generate the DWARF information necessary to find the
720/// actual Block variable (navigating the Block struct) based on the
721/// starting location. Add the DWARF information to the die. For
722/// more information, read large comment just above here.
723///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000724void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000725 unsigned Attribute,
726 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000727 DIType Ty = DV->getType();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000728 DIType TmpTy = Ty;
729 unsigned Tag = Ty.getTag();
730 bool isPointer = false;
731
Devang Patel8bd11de2010-08-09 21:01:39 +0000732 StringRef varName = DV->getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000733
734 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000735 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000736 TmpTy = DTy.getTypeDerivedFrom();
737 isPointer = true;
738 }
739
Devang Patel2db49d72010-05-07 18:11:54 +0000740 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000741
Daniel Dunbar00564992009-09-19 20:40:14 +0000742 // Find the __forwarding field and the variable field in the __Block_byref
743 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000744 DIArray Fields = blockStruct.getTypeArray();
745 DIDescriptor varField = DIDescriptor();
746 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000747
Daniel Dunbar00564992009-09-19 20:40:14 +0000748 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
749 DIDescriptor Element = Fields.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000750 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000751 StringRef fieldName = DT.getName();
752 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000753 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000754 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000755 varField = Element;
756 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000757
Daniel Dunbar00564992009-09-19 20:40:14 +0000758 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000759 unsigned forwardingFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000760 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000761 unsigned varFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000762 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000763
Mike Stump7e3720d2009-09-24 23:21:26 +0000764 // Decode the original location, and use that as the start of the byref
765 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000766 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000767 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000768 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000769
Daniel Dunbar00564992009-09-19 20:40:14 +0000770 if (Location.isReg()) {
771 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000772 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000773 else {
774 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000775 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
776 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000777 }
778 } else {
779 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000780 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000781 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000782 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
783 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000784 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000785
Devang Patel2c4ceb12009-11-21 02:48:08 +0000786 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000787 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000788
Mike Stump7e3720d2009-09-24 23:21:26 +0000789 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000790 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000791 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000792 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000793
Daniel Dunbar00564992009-09-19 20:40:14 +0000794 // Next add the offset for the '__forwarding' field:
795 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
796 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000797 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000798 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
799 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000800 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000801
Daniel Dunbar00564992009-09-19 20:40:14 +0000802 // Now dereference the __forwarding field to get to the real __Block_byref
803 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000804 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000805
Daniel Dunbar00564992009-09-19 20:40:14 +0000806 // Now that we've got the real __Block_byref... struct, add the offset
807 // for the variable's field to get to the location of the actual variable:
808 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000809 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000810 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
811 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000812 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000813
Daniel Dunbar00564992009-09-19 20:40:14 +0000814 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000815 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000816}
817
Devang Patel2c4ceb12009-11-21 02:48:08 +0000818/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000819/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000820void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000821 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000822 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000823 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000824 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelbe90c3a2010-09-22 21:10:38 +0000825
Devang Patelc8821042010-11-02 17:37:00 +0000826 if (RI->getFrameRegister(*Asm->MF) == Location.getReg()
Devang Patelbe90c3a2010-09-22 21:10:38 +0000827 && Location.getOffset()) {
828 // If variable offset is based in frame register then use fbreg.
829 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
830 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
831 addBlock(Die, Attribute, 0, Block);
832 return;
833 }
Bill Wendling0310d762009-05-15 09:23:25 +0000834
835 if (Location.isReg()) {
836 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000837 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000838 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000839 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
840 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000841 }
842 } else {
843 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000844 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000845 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000846 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
847 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000848 }
849
Devang Patel2c4ceb12009-11-21 02:48:08 +0000850 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000851 }
852
Devang Patel2c4ceb12009-11-21 02:48:08 +0000853 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000854}
855
Devang Patela43098d2010-04-28 01:03:09 +0000856/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patel26c1e562010-05-20 16:36:41 +0000857bool DwarfDebug::addRegisterAddress(DIE *Die, const MCSymbol *VS,
Devang Patela43098d2010-04-28 01:03:09 +0000858 const MachineOperand &MO) {
859 assert (MO.isReg() && "Invalid machine operand!");
860 if (!MO.getReg())
861 return false;
862 MachineLocation Location;
863 Location.set(MO.getReg());
864 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patel26c1e562010-05-20 16:36:41 +0000865 if (VS)
Devang Patela43098d2010-04-28 01:03:09 +0000866 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
867 return true;
868}
869
870/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel26c1e562010-05-20 16:36:41 +0000871bool DwarfDebug::addConstantValue(DIE *Die, const MCSymbol *VS,
Devang Patela43098d2010-04-28 01:03:09 +0000872 const MachineOperand &MO) {
873 assert (MO.isImm() && "Invalid machine operand!");
874 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
875 unsigned Imm = MO.getImm();
876 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
877 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel26c1e562010-05-20 16:36:41 +0000878 if (VS)
Devang Patela43098d2010-04-28 01:03:09 +0000879 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
880 return true;
881}
882
883/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patel26c1e562010-05-20 16:36:41 +0000884bool DwarfDebug::addConstantFPValue(DIE *Die, const MCSymbol *VS,
Devang Patela43098d2010-04-28 01:03:09 +0000885 const MachineOperand &MO) {
886 assert (MO.isFPImm() && "Invalid machine operand!");
887 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
888 APFloat FPImm = MO.getFPImm()->getValueAPF();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000889
Devang Patela43098d2010-04-28 01:03:09 +0000890 // Get the raw data form of the floating point.
891 const APInt FltVal = FPImm.bitcastToAPInt();
892 const char *FltPtr = (const char*)FltVal.getRawData();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000893
Devang Patela43098d2010-04-28 01:03:09 +0000894 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
895 bool LittleEndian = Asm->getTargetData().isLittleEndian();
896 int Incr = (LittleEndian ? 1 : -1);
897 int Start = (LittleEndian ? 0 : NumBytes - 1);
898 int Stop = (LittleEndian ? NumBytes : -1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000899
Devang Patela43098d2010-04-28 01:03:09 +0000900 // Output the constant to DWARF one byte at a time.
901 for (; Start != Stop; Start += Incr)
902 addUInt(Block, 0, dwarf::DW_FORM_data1,
903 (unsigned char)0xFF & FltPtr[Start]);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000904
Devang Patela43098d2010-04-28 01:03:09 +0000905 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel26c1e562010-05-20 16:36:41 +0000906 if (VS)
907 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000908 return true;
Devang Patela43098d2010-04-28 01:03:09 +0000909}
910
911
Devang Patelc366f832009-12-10 19:14:49 +0000912/// addToContextOwner - Add Die into the list of its context owner's children.
913void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000914 if (Context.isType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000915 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patelc366f832009-12-10 19:14:49 +0000916 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000917 } else if (Context.isNameSpace()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000918 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel6404e4e2009-12-15 19:16:48 +0000919 ContextDIE->addChild(Die);
Stuart Hastings215aa152010-06-11 20:08:44 +0000920 } else if (Context.isSubprogram()) {
Devang Patelee70fa72010-09-27 23:15:27 +0000921 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context));
Stuart Hastings215aa152010-06-11 20:08:44 +0000922 ContextDIE->addChild(Die);
Devang Patel163a9f72010-05-10 22:49:55 +0000923 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patelc366f832009-12-10 19:14:49 +0000924 ContextDIE->addChild(Die);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000925 else
Devang Patel163a9f72010-05-10 22:49:55 +0000926 getCompileUnit(Context)->addDie(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000927}
928
Devang Patel16ced732009-12-10 18:05:33 +0000929/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
930/// given DIType.
931DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel163a9f72010-05-10 22:49:55 +0000932 CompileUnit *TypeCU = getCompileUnit(Ty);
933 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel16ced732009-12-10 18:05:33 +0000934 if (TyDIE)
935 return TyDIE;
936
937 // Create new type.
938 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel163a9f72010-05-10 22:49:55 +0000939 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel16ced732009-12-10 18:05:33 +0000940 if (Ty.isBasicType())
Devang Patel2db49d72010-05-07 18:11:54 +0000941 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000942 else if (Ty.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000943 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000944 else {
945 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2db49d72010-05-07 18:11:54 +0000946 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000947 }
948
Devang Patelc366f832009-12-10 19:14:49 +0000949 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000950 return TyDIE;
951}
952
Devang Patel2c4ceb12009-11-21 02:48:08 +0000953/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000954void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9c004872010-05-07 21:45:47 +0000955 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000956 return;
957
958 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000959 CompileUnit *TypeCU = getCompileUnit(Ty);
960 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000961 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000962 if (Entry) {
963 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000964 return;
965 }
966
Bill Wendling0310d762009-05-15 09:23:25 +0000967 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000968 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000969
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000970 // Set up proxy.
971 Entry = createDIEEntry(Buffer);
Devang Patel163a9f72010-05-10 22:49:55 +0000972 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000973
Devang Patel2c4ceb12009-11-21 02:48:08 +0000974 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000975}
976
Devang Patel2c4ceb12009-11-21 02:48:08 +0000977/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000978void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000979 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000980 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000981 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000982 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000983 BTy.getEncoding());
984
985 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000986 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000987 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000988 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000989 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000990}
991
Devang Patel2c4ceb12009-11-21 02:48:08 +0000992/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000993void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000994 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000995 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000996 uint64_t Size = DTy.getSizeInBits() >> 3;
997 unsigned Tag = DTy.getTag();
998
999 // FIXME - Workaround for templates.
1000 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
1001
1002 Buffer.setTag(Tag);
1003
1004 // Map to main type, void will not have a type.
1005 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +00001006 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001007
1008 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +00001009 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001010 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001011
1012 // Add size if non-zero (derived types might be zero-sized.)
1013 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001014 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001015
1016 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +00001017 if (!DTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001018 addSourceLine(&Buffer, DTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001019}
1020
Devang Patel2c4ceb12009-11-21 02:48:08 +00001021/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001022void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001023 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +00001024 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001025
1026 uint64_t Size = CTy.getSizeInBits() >> 3;
1027 unsigned Tag = CTy.getTag();
1028 Buffer.setTag(Tag);
1029
1030 switch (Tag) {
1031 case dwarf::DW_TAG_vector_type:
1032 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +00001033 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001034 break;
1035 case dwarf::DW_TAG_enumeration_type: {
1036 DIArray Elements = CTy.getTypeArray();
1037
1038 // Add enumerators to enumeration type.
1039 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1040 DIE *ElemDie = NULL;
Devang Patel2db49d72010-05-07 18:11:54 +00001041 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3c91b052010-03-08 20:52:55 +00001042 if (Enum.isEnumerator()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001043 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001044 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +00001045 }
Bill Wendling0310d762009-05-15 09:23:25 +00001046 }
1047 }
1048 break;
1049 case dwarf::DW_TAG_subroutine_type: {
1050 // Add return type.
1051 DIArray Elements = CTy.getTypeArray();
1052 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2db49d72010-05-07 18:11:54 +00001053 addType(&Buffer, DIType(RTy));
Bill Wendling0310d762009-05-15 09:23:25 +00001054
Devang Pateld6747df2010-10-06 20:50:40 +00001055 bool isPrototyped = true;
Bill Wendling0310d762009-05-15 09:23:25 +00001056 // Add arguments.
1057 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001058 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld6747df2010-10-06 20:50:40 +00001059 if (Ty.isUnspecifiedParameter()) {
1060 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
1061 Buffer.addChild(Arg);
1062 isPrototyped = false;
1063 } else {
1064 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1065 addType(Arg, DIType(Ty));
1066 Buffer.addChild(Arg);
1067 }
Bill Wendling0310d762009-05-15 09:23:25 +00001068 }
Devang Pateld6747df2010-10-06 20:50:40 +00001069 // Add prototype flag.
1070 if (isPrototyped)
1071 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001072 }
1073 break;
1074 case dwarf::DW_TAG_structure_type:
1075 case dwarf::DW_TAG_union_type:
1076 case dwarf::DW_TAG_class_type: {
1077 // Add elements to structure type.
1078 DIArray Elements = CTy.getTypeArray();
1079
1080 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +00001081 unsigned N = Elements.getNumElements();
1082 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +00001083 break;
1084
1085 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +00001086 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001087 DIDescriptor Element = Elements.getElement(i);
1088 DIE *ElemDie = NULL;
Devang Patel1a301232010-09-29 21:44:16 +00001089 if (Element.isSubprogram()) {
1090 DISubprogram SP(Element);
Devang Patel2db49d72010-05-07 18:11:54 +00001091 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel1a301232010-09-29 21:44:16 +00001092 if (SP.isProtected())
1093 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1094 dwarf::DW_ACCESS_protected);
1095 else if (SP.isPrivate())
1096 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1097 dwarf::DW_ACCESS_private);
1098 else
1099 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1100 dwarf::DW_ACCESS_public);
Devang Patel21ea1d52010-10-01 23:31:40 +00001101 if (SP.isExplicit())
1102 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
Devang Patel1a301232010-09-29 21:44:16 +00001103 }
Devang Patel3c91b052010-03-08 20:52:55 +00001104 else if (Element.isVariable()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001105 DIVariable DV(Element);
Devang Patel1ee0cb92010-01-30 01:08:30 +00001106 ElemDie = new DIE(dwarf::DW_TAG_variable);
1107 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1108 DV.getName());
1109 addType(ElemDie, DV.getType());
1110 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1111 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel81625742010-08-09 20:20:05 +00001112 addSourceLine(ElemDie, DV);
Devang Patel3c91b052010-03-08 20:52:55 +00001113 } else if (Element.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001114 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001115 else
1116 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001117 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001118 }
1119
Devang Patela1ba2692009-08-27 23:51:51 +00001120 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001121 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001122
1123 unsigned RLang = CTy.getRunTimeLang();
1124 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001125 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001126 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001127
1128 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel2db49d72010-05-07 18:11:54 +00001129 if (DIDescriptor(ContainingType).isCompositeType())
Jim Grosbach1e20b962010-07-21 21:21:52 +00001130 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel2db49d72010-05-07 18:11:54 +00001131 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastings215aa152010-06-11 20:08:44 +00001132 else {
1133 DIDescriptor Context = CTy.getContext();
1134 addToContextOwner(&Buffer, Context);
1135 }
Bill Wendling0310d762009-05-15 09:23:25 +00001136 break;
1137 }
1138 default:
1139 break;
1140 }
1141
1142 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001143 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001144 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001145
Jim Grosbach1e20b962010-07-21 21:21:52 +00001146 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
Devang Patel61409622010-07-07 20:12:52 +00001147 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1148 {
Bill Wendling0310d762009-05-15 09:23:25 +00001149 // Add size if non-zero (derived types might be zero-sized.)
1150 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001151 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001152 else {
1153 // Add zero size if it is not a forward declaration.
1154 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001155 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001156 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001157 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001158 }
1159
1160 // Add source line info if available.
1161 if (!CTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001162 addSourceLine(&Buffer, CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001163 }
1164}
1165
Devang Patel2c4ceb12009-11-21 02:48:08 +00001166/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1167void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001168 int64_t L = SR.getLo();
1169 int64_t H = SR.getHi();
1170 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1171
Devang Patel2c4ceb12009-11-21 02:48:08 +00001172 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001173 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001174 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001175 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001176
Devang Patel2c4ceb12009-11-21 02:48:08 +00001177 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001178}
1179
Devang Patel2c4ceb12009-11-21 02:48:08 +00001180/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001181void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001182 DICompositeType *CTy) {
1183 Buffer.setTag(dwarf::DW_TAG_array_type);
1184 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001185 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001186
1187 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001188 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001189 DIArray Elements = CTy->getTypeArray();
1190
Devang Patel6f01d9c2009-11-21 00:31:03 +00001191 // Get an anonymous type for index type.
Devang Patel163a9f72010-05-10 22:49:55 +00001192 CompileUnit *TheCU = getCompileUnit(*CTy);
1193 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001194 if (!IdxTy) {
1195 // Construct an anonymous type for index type.
1196 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001197 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1198 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001199 dwarf::DW_ATE_signed);
Devang Patel163a9f72010-05-10 22:49:55 +00001200 TheCU->addDie(IdxTy);
1201 TheCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001202 }
Bill Wendling0310d762009-05-15 09:23:25 +00001203
1204 // Add subranges to array type.
1205 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1206 DIDescriptor Element = Elements.getElement(i);
1207 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2db49d72010-05-07 18:11:54 +00001208 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001209 }
1210}
1211
Devang Patel2c4ceb12009-11-21 02:48:08 +00001212/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001213DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001214 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001215 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001216 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001217 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001218 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001219 return Enumerator;
1220}
1221
Jim Grosbach1e20b962010-07-21 21:21:52 +00001222/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +00001223/// printer to not emit usual symbol prefix before the symbol name is used then
1224/// return linkage name after skipping this special LLVM prefix.
1225static StringRef getRealLinkageName(StringRef LinkageName) {
1226 char One = '\1';
1227 if (LinkageName.startswith(StringRef(&One, 1)))
1228 return LinkageName.substr(1);
1229 return LinkageName;
1230}
1231
Devang Patel2c4ceb12009-11-21 02:48:08 +00001232/// createMemberDIE - Create new member DIE.
Devang Patelecbd8e82010-08-10 04:12:17 +00001233DIE *DwarfDebug::createMemberDIE(DIDerivedType DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001234 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001235 StringRef Name = DT.getName();
1236 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001237 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001238
Devang Patel8a241142009-12-09 18:24:21 +00001239 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001240
Devang Patel81625742010-08-09 20:20:05 +00001241 addSourceLine(MemberDie, DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001242
Benjamin Kramer345ef342010-03-31 19:34:01 +00001243 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001244 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001245
Bill Wendling0310d762009-05-15 09:23:25 +00001246 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001247 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001248
1249 if (Size != FieldSize) {
1250 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001251 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1252 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001253
1254 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001255 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1256 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001257 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001258 Offset -= FieldOffset;
1259
1260 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001261 if (Asm->getTargetData().isLittleEndian())
1262 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001263 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001264
Devang Patel33db5082009-11-04 22:06:12 +00001265 // Here WD_AT_data_member_location points to the anonymous
1266 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001267 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001268
1269 } else
1270 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001271 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001272
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001273 if (DT.getTag() == dwarf::DW_TAG_inheritance
1274 && DT.isVirtual()) {
1275
1276 // For C++, virtual base classes are not at fixed offset. Use following
1277 // expression to extract appropriate offset from vtable.
1278 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1279
Benjamin Kramer345ef342010-03-31 19:34:01 +00001280 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001281 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1282 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1283 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1284 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1285 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1286 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1287 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1288
Jim Grosbach1e20b962010-07-21 21:21:52 +00001289 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001290 VBaseLocationDie);
1291 } else
1292 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001293
1294 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001295 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001296 dwarf::DW_ACCESS_protected);
1297 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001298 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001299 dwarf::DW_ACCESS_private);
Devang Patel2a361602010-09-29 19:08:08 +00001300 // Otherwise C++ member and base classes are considered public.
1301 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
Devang Patel5d11eb02009-12-03 19:11:07 +00001302 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1303 dwarf::DW_ACCESS_public);
1304 if (DT.isVirtual())
1305 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1306 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001307 return MemberDie;
1308}
1309
Devang Patelffe966c2009-12-14 16:18:45 +00001310/// createSubprogramDIE - Create new DIE using SP.
Devang Patelee70fa72010-09-27 23:15:27 +00001311DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel163a9f72010-05-10 22:49:55 +00001312 CompileUnit *SPCU = getCompileUnit(SP);
1313 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +00001314 if (SPDie)
1315 return SPDie;
1316
1317 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001318 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001319 if (!SP.getName().empty())
1320 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001321
Devang Patel65dbc902009-11-25 17:36:49 +00001322 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001323 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001324 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001325 getRealLinkageName(LinkageName));
1326
Devang Patel81625742010-08-09 20:20:05 +00001327 addSourceLine(SPDie, SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001328
Devang Patel7b172c62010-10-07 22:03:01 +00001329 if (SP.isPrototyped())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001330 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001331
1332 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001333 DICompositeType SPTy = SP.getType();
1334 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001335 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001336
Devang Patel3c91b052010-03-08 20:52:55 +00001337 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001338 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001339 else
Devang Patel2db49d72010-05-07 18:11:54 +00001340 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001341
Devang Patel5d11eb02009-12-03 19:11:07 +00001342 unsigned VK = SP.getVirtuality();
1343 if (VK) {
1344 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001345 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001346 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1347 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1348 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001349 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +00001350 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001351 }
1352
Devang Patelee70fa72010-09-27 23:15:27 +00001353 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001354 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001355
1356 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001357 // be handled while processing variables.
1358 DICompositeType SPTy = SP.getType();
1359 DIArray Args = SPTy.getTypeArray();
1360 unsigned SPTag = SPTy.getTag();
1361
Bill Wendling0310d762009-05-15 09:23:25 +00001362 if (SPTag == dwarf::DW_TAG_subroutine_type)
1363 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1364 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001365 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelb4645642010-02-06 01:02:37 +00001366 addType(Arg, ATy);
1367 if (ATy.isArtificial())
1368 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001369 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001370 }
1371 }
1372
Devang Patel4e0d19d2010-02-03 19:57:19 +00001373 if (SP.isArtificial())
1374 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1375
Devang Patelccff8122010-04-30 19:38:23 +00001376 if (!SP.isLocalToUnit())
1377 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001378
Devang Patelccff8122010-04-30 19:38:23 +00001379 if (SP.isOptimized())
1380 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1381
Jim Grosbach91729002010-07-21 23:03:52 +00001382 if (unsigned isa = Asm->getISAEncoding()) {
1383 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1384 }
1385
Devang Patelccff8122010-04-30 19:38:23 +00001386 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel163a9f72010-05-10 22:49:55 +00001387 SPCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +00001388
Stuart Hastings215aa152010-06-11 20:08:44 +00001389 // Add to context owner.
1390 addToContextOwner(SPDie, SP.getContext());
1391
Bill Wendling0310d762009-05-15 09:23:25 +00001392 return SPDie;
1393}
1394
Devang Patele9f8f5e2010-05-07 20:54:48 +00001395DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001396 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001397
1398 DbgScope *AScope = AbstractScopes.lookup(N);
1399 if (AScope)
1400 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001401
Devang Patel53bb5c92009-11-10 23:06:00 +00001402 DbgScope *Parent = NULL;
1403
1404 DIDescriptor Scope(N);
1405 if (Scope.isLexicalBlock()) {
1406 DILexicalBlock DB(N);
1407 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +00001408 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +00001409 }
1410
1411 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1412
1413 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001414 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001415 AScope->setAbstractScope();
1416 AbstractScopes[N] = AScope;
1417 if (DIDescriptor(N).isSubprogram())
1418 AbstractScopesList.push_back(AScope);
1419 return AScope;
1420}
Devang Patelaf9e8472009-10-01 20:31:14 +00001421
Devang Patel5f094002010-04-06 23:53:48 +00001422/// isSubprogramContext - Return true if Context is either a subprogram
1423/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001424static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001425 if (!Context)
1426 return false;
1427 DIDescriptor D(Context);
1428 if (D.isSubprogram())
1429 return true;
1430 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001431 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +00001432 return false;
1433}
1434
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001435/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001436/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1437/// If there are global variables in this scope then create and insert
1438/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001439DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +00001440 CompileUnit *SPCU = getCompileUnit(SPNode);
1441 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +00001442
Chris Lattnerd38fee82010-04-05 00:13:49 +00001443 assert(SPDie && "Unable to find subprogram DIE!");
1444 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001445
Chris Lattnerd38fee82010-04-05 00:13:49 +00001446 // There is not any need to generate specification DIE for a function
1447 // defined at compile unit level. If a function is defined inside another
1448 // function then gdb prefers the definition at top level and but does not
Jim Grosbach1e20b962010-07-21 21:21:52 +00001449 // expect specification DIE in parent function. So avoid creating
Chris Lattnerd38fee82010-04-05 00:13:49 +00001450 // specification DIE for a function defined inside a function.
1451 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbach1e20b962010-07-21 21:21:52 +00001452 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001453 !isSubprogramContext(SP.getContext())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001454 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001455
1456 // Add arguments.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001457 DICompositeType SPTy = SP.getType();
1458 DIArray Args = SPTy.getTypeArray();
1459 unsigned SPTag = SPTy.getTag();
1460 if (SPTag == dwarf::DW_TAG_subroutine_type)
1461 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1462 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001463 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerd38fee82010-04-05 00:13:49 +00001464 addType(Arg, ATy);
1465 if (ATy.isArtificial())
1466 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1467 SPDie->addChild(Arg);
1468 }
1469 DIE *SPDeclDie = SPDie;
1470 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001471 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Chris Lattnerd38fee82010-04-05 00:13:49 +00001472 SPDeclDie);
Devang Patel163a9f72010-05-10 22:49:55 +00001473 SPCU->addDie(SPDie);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001474 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001475
Devang Patel8aa61472010-07-07 22:20:57 +00001476 // Pick up abstract subprogram DIE.
1477 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1478 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1479 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1480 dwarf::DW_FORM_ref4, AbsSPDIE);
1481 SPCU->addDie(SPDie);
1482 }
1483
Chris Lattnerd38fee82010-04-05 00:13:49 +00001484 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1485 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1486 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1487 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1488 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1489 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1490 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001491
Chris Lattnerd38fee82010-04-05 00:13:49 +00001492 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001493}
1494
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001495/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001496/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1497DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001498
1499 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1500 if (Scope->isAbstractScope())
1501 return ScopeDIE;
1502
1503 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1504 if (Ranges.empty())
1505 return 0;
1506
1507 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1508 if (Ranges.size() > 1) {
1509 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +00001510 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +00001511 // DW_AT_ranges appropriately.
1512 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1513 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1514 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1515 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001516 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1517 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +00001518 }
1519 DebugRangeSymbols.push_back(NULL);
1520 DebugRangeSymbols.push_back(NULL);
1521 return ScopeDIE;
1522 }
1523
Devang Patelc3f5f782010-05-25 23:40:22 +00001524 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1525 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001526
Devang Patelc3f5f782010-05-25 23:40:22 +00001527 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001528
Chris Lattnerb7db7332010-03-09 01:58:53 +00001529 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1530 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001531
Devang Pateleac9c072010-04-27 19:46:33 +00001532 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1533 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001534
1535 return ScopeDIE;
1536}
1537
Devang Patel2c4ceb12009-11-21 02:48:08 +00001538/// constructInlinedScopeDIE - This scope represents inlined body of
1539/// a function. Construct DIE to represent this concrete inlined copy
1540/// of the function.
1541DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001542
1543 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001544 assert (Ranges.empty() == false
Devang Pateleac9c072010-04-27 19:46:33 +00001545 && "DbgScope does not have instruction markers!");
1546
1547 // FIXME : .debug_inlined section specification does not clearly state how
1548 // to emit inlined scope that is split into multiple instruction ranges.
1549 // For now, use first instruction range and emit low_pc/high_pc pair and
1550 // corresponding .debug_inlined section entry for this pair.
1551 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +00001552 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1553 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001554
Devang Patel0afbf232010-07-08 22:39:20 +00001555 if (StartLabel == 0 || EndLabel == 0) {
Devang Pateleac9c072010-04-27 19:46:33 +00001556 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1557 return 0;
1558 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001559 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001560 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001561 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001562 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +00001563
Devang Patel3c91b052010-03-08 20:52:55 +00001564 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001565 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001566 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001567 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1568
Devang Patel2db49d72010-05-07 18:11:54 +00001569 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001570 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1571 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001572 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001573 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001574 dwarf::DW_FORM_ref4, OriginDIE);
1575
Chris Lattner6ed0f902010-03-09 00:31:02 +00001576 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001577 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001578
1579 InlinedSubprogramDIEs.insert(OriginDIE);
1580
1581 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001582 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +00001583 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001584
1585 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001586 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001587 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +00001588 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001589 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001590 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001591
Devang Patel53bb5c92009-11-10 23:06:00 +00001592 DILocation DL(Scope->getInlinedAt());
Devang Patel163a9f72010-05-10 22:49:55 +00001593 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001594 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001595
1596 return ScopeDIE;
1597}
1598
Devang Patel2c4ceb12009-11-21 02:48:08 +00001599
1600/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001601DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +00001602 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +00001603 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001604 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001605
1606 // Translate tag to proper Dwarf tag. The result variable is dropped for
1607 // now.
1608 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +00001609 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001610 case dwarf::DW_TAG_return_variable:
1611 return NULL;
1612 case dwarf::DW_TAG_arg_variable:
1613 Tag = dwarf::DW_TAG_formal_parameter;
1614 break;
1615 case dwarf::DW_TAG_auto_variable: // fall thru
1616 default:
1617 Tag = dwarf::DW_TAG_variable;
1618 break;
1619 }
1620
1621 // Define variable debug information entry.
1622 DIE *VariableDie = new DIE(Tag);
1623
Devang Patel53bb5c92009-11-10 23:06:00 +00001624 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +00001625 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1626 V2AVI = VarToAbstractVarMap.find(DV);
1627 if (V2AVI != VarToAbstractVarMap.end())
1628 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001629
Devang Patel26c1e562010-05-20 16:36:41 +00001630 if (AbsDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001631 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001632 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001633 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001634 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel8bd11de2010-08-09 21:01:39 +00001635 addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +00001636
1637 // Add variable type.
Devang Patel8bd11de2010-08-09 21:01:39 +00001638 addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001639 }
1640
Devang Patel8bd11de2010-08-09 21:01:39 +00001641 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patelb4645642010-02-06 01:02:37 +00001642 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel3cf763d2010-09-29 23:07:21 +00001643 else if (DIVariable(DV->getVariable()).isArtificial())
1644 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +00001645
1646 if (Scope->isAbstractScope()) {
1647 DV->setDIE(VariableDie);
1648 return VariableDie;
1649 }
1650
1651 // Add variable address.
1652
1653 unsigned Offset = DV->getDotDebugLocOffset();
1654 if (Offset != ~0U) {
1655 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1656 Asm->GetTempSymbol("debug_loc", Offset));
1657 DV->setDIE(VariableDie);
1658 UseDotDebugLocEntry.insert(VariableDie);
1659 return VariableDie;
1660 }
1661
1662 // Check if variable is described by a DBG_VALUE instruction.
1663 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1664 DbgVariableToDbgInstMap.find(DV);
1665 if (DVI != DbgVariableToDbgInstMap.end()) {
1666 const MachineInstr *DVInsn = DVI->second;
1667 const MCSymbol *DVLabel = findVariableLabel(DV);
1668 bool updated = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001669 // FIXME : Handle getNumOperands != 3
Devang Patelc3f5f782010-05-25 23:40:22 +00001670 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +00001671 if (DVInsn->getOperand(0).isReg()) {
1672 const MachineOperand RegOp = DVInsn->getOperand(0);
1673 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1674 if (DVInsn->getOperand(1).isImm() &&
1675 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1676 addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
1677 updated = true;
1678 } else
1679 updated = addRegisterAddress(VariableDie, DVLabel, RegOp);
1680 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001681 else if (DVInsn->getOperand(0).isImm())
1682 updated = addConstantValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001683 else if (DVInsn->getOperand(0).isFPImm())
1684 updated =
Devang Patel61409622010-07-07 20:12:52 +00001685 addConstantFPValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patelc3f5f782010-05-25 23:40:22 +00001686 } else {
1687 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1688 if (Location.getReg()) {
1689 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1690 if (DVLabel)
1691 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1692 DVLabel);
1693 updated = true;
1694 }
1695 }
1696 if (!updated) {
1697 // If variableDie is not updated then DBG_VALUE instruction does not
1698 // have valid variable info.
1699 delete VariableDie;
1700 return NULL;
1701 }
1702 DV->setDIE(VariableDie);
1703 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001704 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001705
1706 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +00001707 int FI = 0;
Devang Patel9e3bd2c2010-08-31 06:11:28 +00001708 if (findVariableFrameIndex(DV, &FI))
1709 addVariableAddress(DV, VariableDie, FI);
1710
Devang Patel53bb5c92009-11-10 23:06:00 +00001711 DV->setDIE(VariableDie);
1712 return VariableDie;
1713
1714}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001715
Devang Patel193f7202009-11-24 01:14:22 +00001716void DwarfDebug::addPubTypes(DISubprogram SP) {
1717 DICompositeType SPTy = SP.getType();
1718 unsigned SPTag = SPTy.getTag();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001719 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel193f7202009-11-24 01:14:22 +00001720 return;
1721
1722 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001723 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +00001724 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +00001725 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +00001726 continue;
1727 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +00001728 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +00001729 && !CATy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001730 CompileUnit *TheCU = getCompileUnit(CATy);
1731 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1732 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001733 }
1734 }
1735}
1736
Devang Patel2c4ceb12009-11-21 02:48:08 +00001737/// constructScopeDIE - Construct a DIE for this scope.
1738DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001739 if (!Scope || !Scope->getScopeNode())
1740 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001741
Devang Patel3c91b052010-03-08 20:52:55 +00001742 DIScope DS(Scope->getScopeNode());
1743 DIE *ScopeDIE = NULL;
1744 if (Scope->getInlinedAt())
1745 ScopeDIE = constructInlinedScopeDIE(Scope);
1746 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +00001747 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001748 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001749 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001750 // Note down abstract DIE.
1751 if (ScopeDIE)
1752 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1753 }
Devang Patel3c91b052010-03-08 20:52:55 +00001754 else
Devang Patel2db49d72010-05-07 18:11:54 +00001755 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001756 }
Devang Patelaead63c2010-03-29 22:59:58 +00001757 else
Devang Patel3c91b052010-03-08 20:52:55 +00001758 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001759 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001760
Devang Patel53bb5c92009-11-10 23:06:00 +00001761 // Add variables to scope.
Devang Patele03161c2010-08-09 18:51:29 +00001762 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001763 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001764 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001765 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001766 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001767 }
1768
1769 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001770 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001771 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1772 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001773 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001774 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001775 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001776 }
Devang Patel193f7202009-11-24 01:14:22 +00001777
Jim Grosbach1e20b962010-07-21 21:21:52 +00001778 if (DS.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001779 addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001780
Devang Patel193f7202009-11-24 01:14:22 +00001781 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001782}
1783
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001784/// GetOrCreateSourceID - Look up the source id with the given directory and
1785/// source file names. If none currently exists, create a new id and insert it
1786/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1787/// maps as well.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001788unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001789 unsigned DId;
Devang Patel37032352010-07-27 20:51:15 +00001790 assert (DirName.empty() == false && "Invalid directory name!");
Devang Patel2f584852010-07-24 00:53:22 +00001791
Devang Patel1905a182010-09-16 20:57:49 +00001792 // If FE did not provide a file name, then assume stdin.
1793 if (FileName.empty())
1794 return GetOrCreateSourceID(DirName, "<stdin>");
1795
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001796 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1797 if (DI != DirectoryIdMap.end()) {
1798 DId = DI->getValue();
1799 } else {
1800 DId = DirectoryNames.size() + 1;
1801 DirectoryIdMap[DirName] = DId;
1802 DirectoryNames.push_back(DirName);
1803 }
1804
1805 unsigned FId;
1806 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1807 if (FI != SourceFileIdMap.end()) {
1808 FId = FI->getValue();
1809 } else {
1810 FId = SourceFileNames.size() + 1;
1811 SourceFileIdMap[FileName] = FId;
1812 SourceFileNames.push_back(FileName);
1813 }
1814
1815 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1816 SourceIdMap.find(std::make_pair(DId, FId));
1817 if (SI != SourceIdMap.end())
1818 return SI->second;
1819
1820 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1821 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1822 SourceIds.push_back(std::make_pair(DId, FId));
1823
1824 return SrcId;
1825}
1826
Devang Patel6404e4e2009-12-15 19:16:48 +00001827/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1828DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel163a9f72010-05-10 22:49:55 +00001829 CompileUnit *TheCU = getCompileUnit(NS);
1830 DIE *NDie = TheCU->getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001831 if (NDie)
1832 return NDie;
1833 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel163a9f72010-05-10 22:49:55 +00001834 TheCU->insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +00001835 if (!NS.getName().empty())
1836 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patel81625742010-08-09 20:20:05 +00001837 addSourceLine(NDie, NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001838 addToContextOwner(NDie, NS.getContext());
1839 return NDie;
1840}
1841
Jim Grosbach1e20b962010-07-21 21:21:52 +00001842/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +00001843/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001844void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001845 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001846 StringRef FN = DIUnit.getFilename();
1847 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001848 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001849
1850 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001851 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001852 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001853 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001854 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001855 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +00001856 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1857 // simplifies debug range entries.
Devang Patel9b93b6b2010-06-28 22:22:47 +00001858 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +00001859 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +00001860 // compile unit in debug_line section.
Devang Patelae84d5b2010-08-31 23:50:19 +00001861 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
1862 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
1863 Asm->GetTempSymbol("section_line"));
1864 else
1865 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001866
Devang Patel65dbc902009-11-25 17:36:49 +00001867 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001868 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001869 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001870 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001871
Devang Patel65dbc902009-11-25 17:36:49 +00001872 StringRef Flags = DIUnit.getFlags();
1873 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001874 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001875
1876 unsigned RVer = DIUnit.getRunTimeVersion();
1877 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001878 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001879 dwarf::DW_FORM_data1, RVer);
1880
Devang Patel163a9f72010-05-10 22:49:55 +00001881 CompileUnit *NewCU = new CompileUnit(ID, Die);
1882 if (!FirstCU)
1883 FirstCU = NewCU;
1884 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001885}
1886
Devang Patel163a9f72010-05-10 22:49:55 +00001887/// getCompielUnit - Get CompileUnit DIE.
1888CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1889 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1890 DIDescriptor D(N);
1891 const MDNode *CUNode = NULL;
1892 if (D.isCompileUnit())
1893 CUNode = N;
1894 else if (D.isSubprogram())
1895 CUNode = DISubprogram(N).getCompileUnit();
1896 else if (D.isType())
1897 CUNode = DIType(N).getCompileUnit();
1898 else if (D.isGlobalVariable())
1899 CUNode = DIGlobalVariable(N).getCompileUnit();
1900 else if (D.isVariable())
1901 CUNode = DIVariable(N).getCompileUnit();
1902 else if (D.isNameSpace())
1903 CUNode = DINameSpace(N).getCompileUnit();
1904 else if (D.isFile())
1905 CUNode = DIFile(N).getCompileUnit();
1906 else
1907 return FirstCU;
1908
1909 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1910 = CUMap.find(CUNode);
1911 if (I == CUMap.end())
1912 return FirstCU;
1913 return I->second;
1914}
1915
Devang Patel0c4720c2010-08-23 18:25:56 +00001916/// isUnsignedDIType - Return true if type encoding is unsigned.
1917static bool isUnsignedDIType(DIType Ty) {
1918 DIDerivedType DTy(Ty);
1919 if (DTy.Verify())
1920 return isUnsignedDIType(DTy.getTypeDerivedFrom());
1921
1922 DIBasicType BTy(Ty);
1923 if (BTy.Verify()) {
1924 unsigned Encoding = BTy.getEncoding();
1925 if (Encoding == dwarf::DW_ATE_unsigned ||
1926 Encoding == dwarf::DW_ATE_unsigned_char)
1927 return true;
1928 }
1929 return false;
1930}
Devang Patel163a9f72010-05-10 22:49:55 +00001931
1932/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001933void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +00001934 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001935
Devang Patel905cf5e2009-09-04 23:59:07 +00001936 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +00001937 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +00001938 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001939
1940 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001941 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +00001942 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001943 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001944
Devang Patel9fa539c2010-08-10 01:37:23 +00001945 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +00001946 DIE *VariableDIE = new DIE(GV.getTag());
1947
1948 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001949
Devang Patel9fa539c2010-08-10 01:37:23 +00001950 // Add name.
1951 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1952 GV.getDisplayName());
1953 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +00001954 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel9fa539c2010-08-10 01:37:23 +00001955 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1956 getRealLinkageName(LinkageName));
1957 // Add type.
1958 addType(VariableDIE, GTy);
Devang Patel50d80e32010-04-13 20:35:04 +00001959 if (GTy.isCompositeType() && !GTy.getName().empty()
1960 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001961 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001962 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +00001963 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001964 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001965 // Add scoping info.
1966 if (!GV.isLocalToUnit()) {
1967 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1968 // Expose as global.
1969 TheCU->addGlobal(GV.getName(), VariableDIE);
1970 }
1971 // Add line number info.
1972 addSourceLine(VariableDIE, GV);
1973 // Add to map.
1974 TheCU->insertDIE(N, VariableDIE);
1975 // Add to context owner.
1976 DIDescriptor GVContext = GV.getContext();
1977 addToContextOwner(VariableDIE, GVContext);
1978 // Add location.
Devang Patel29368072010-08-10 07:11:13 +00001979 if (isGlobalVariable) {
1980 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1981 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1982 addLabel(Block, 0, dwarf::DW_FORM_udata,
1983 Asm->Mang->getSymbol(GV.getGlobal()));
1984 // Do not create specification DIE if context is either compile unit
1985 // or a subprogram.
1986 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1987 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1988 // Create specification DIE.
1989 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1990 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1991 dwarf::DW_FORM_ref4, VariableDIE);
1992 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1993 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1994 TheCU->addDie(VariableSpecDIE);
1995 } else {
1996 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1997 }
1998 } else if (Constant *C = GV.getConstant()) {
1999 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
Devang Patel0c4720c2010-08-23 18:25:56 +00002000 if (isUnsignedDIType(GTy))
Devang Patel29368072010-08-10 07:11:13 +00002001 addUInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
2002 CI->getZExtValue());
2003 else
2004 addSInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
2005 CI->getSExtValue());
Devang Patel29368072010-08-10 07:11:13 +00002006 }
Devang Patel9fa539c2010-08-10 01:37:23 +00002007 }
Devang Patel13e16b62009-06-26 01:49:18 +00002008 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002009}
2010
Devang Patel163a9f72010-05-10 22:49:55 +00002011/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002012void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00002013 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002014
Stuart Hastings639336e2010-04-06 21:38:29 +00002015 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00002016 CompileUnit *TheCU = getCompileUnit(N);
2017 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +00002018 return;
2019
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002020 if (!SP.isDefinition())
2021 // This is a method declaration which will be handled while constructing
2022 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00002023 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002024
Stuart Hastings639336e2010-04-06 21:38:29 +00002025 DIE *SubprogramDie = createSubprogramDIE(SP);
2026
2027 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00002028 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002029
2030 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00002031 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00002032
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002033 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +00002034 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00002035
Devang Patel13e16b62009-06-26 01:49:18 +00002036 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002037}
2038
Devang Patel2c4ceb12009-11-21 02:48:08 +00002039/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00002040/// content. Create global DIEs and emit initial debug info sections.
2041/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00002042void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00002043 if (DisableDebugInfoPrinting)
2044 return;
2045
Devang Patel78ab9e22009-07-30 18:56:46 +00002046 DebugInfoFinder DbgFinder;
2047 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00002048
Chris Lattnerd850ac72010-04-05 02:19:28 +00002049 bool HasDebugInfo = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002050
Chris Lattnerd850ac72010-04-05 02:19:28 +00002051 // Scan all the compile-units to see if there are any marked as the main unit.
2052 // if not, we do not generate debug info.
2053 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2054 E = DbgFinder.compile_unit_end(); I != E; ++I) {
2055 if (DICompileUnit(*I).isMain()) {
2056 HasDebugInfo = true;
2057 break;
2058 }
2059 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002060
Chris Lattnerd850ac72010-04-05 02:19:28 +00002061 if (!HasDebugInfo) return;
2062
2063 // Tell MMI that we have debug info.
2064 MMI->setDebugInfoAvailability(true);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002065
Chris Lattnerbe15beb2010-04-04 23:17:54 +00002066 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00002067 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002068
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002069 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00002070 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2071 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002072 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002073
Devang Patel53bb5c92009-11-10 23:06:00 +00002074 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00002075 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
2076 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002077 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00002078
Devang Patelc366f832009-12-10 19:14:49 +00002079 // Create DIEs for each global variable.
2080 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
2081 E = DbgFinder.global_variable_end(); I != E; ++I)
2082 constructGlobalVariableDIE(*I);
2083
Devang Patele7e5a0f2010-08-10 20:01:20 +00002084 //getOrCreateTypeDIE
2085 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
2086 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2087 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2088
Devang Patel1a7ca032010-09-28 18:08:20 +00002089 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
2090 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2091 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2092
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002093 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00002094 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002095
2096 // Print out .file directives to specify files for .loc directives. These are
2097 // printed out early so that they precede any .loc directives.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002098 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002099 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2100 // Remember source id starts at 1.
2101 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002102 // FIXME: don't use sys::path for this! This should not depend on the
2103 // host.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002104 sys::Path FullPath(getSourceDirectoryName(Id.first));
2105 bool AppendOk =
2106 FullPath.appendComponent(getSourceFileName(Id.second));
2107 assert(AppendOk && "Could not append filename to directory!");
2108 AppendOk = false;
Chris Lattnera6594fc2010-01-25 18:58:59 +00002109 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002110 }
2111 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002112}
2113
Devang Patel2c4ceb12009-11-21 02:48:08 +00002114/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002115///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002116void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00002117 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +00002118 const Module *M = MMI->getModule();
Devang Patele9a1cca2010-08-02 17:32:15 +00002119 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +00002120 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2121 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2122 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2123 DISubprogram SP(AllSPs->getOperand(SI));
2124 if (!SP.Verify()) continue;
2125
2126 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +00002127 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +00002128 StringRef FName = SP.getLinkageName();
2129 if (FName.empty())
2130 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002131 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +00002132 if (!NMD) continue;
2133 unsigned E = NMD->getNumOperands();
2134 if (!E) continue;
2135 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Patele9a1cca2010-08-02 17:32:15 +00002136 DeadFnScopeMap[SP] = Scope;
Devang Patel4a1cad62010-06-28 18:25:03 +00002137 for (unsigned I = 0; I != E; ++I) {
2138 DIVariable DV(NMD->getOperand(I));
2139 if (!DV.Verify()) continue;
2140 Scope->addVariable(new DbgVariable(DV));
2141 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002142
Devang Patel4a1cad62010-06-28 18:25:03 +00002143 // Construct subprogram DIE and add variables DIEs.
2144 constructSubprogramDIE(SP);
2145 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patele03161c2010-08-09 18:51:29 +00002146 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel4a1cad62010-06-28 18:25:03 +00002147 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2148 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2149 if (VariableDIE)
2150 ScopeDIE->addChild(VariableDIE);
2151 }
2152 }
2153 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002154
Devang Patel53bb5c92009-11-10 23:06:00 +00002155 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2156 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2157 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2158 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002159 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00002160 }
2161
Devang Patele9f8f5e2010-05-07 20:54:48 +00002162 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00002163 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2164 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002165 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00002166 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00002167 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00002168 if (!NDie) continue;
2169 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00002170 }
2171
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002172 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002173 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002174 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002175 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002176 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002177
2178 // End text sections.
2179 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002180 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00002181 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002182 }
2183
2184 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002185 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002186
2187 // Emit function debug frame information
2188 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2189 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002190 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002191
2192 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002193 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002194
2195 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00002196 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002197
2198 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002199 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002200
Devang Patelaf608bd2010-08-24 00:06:12 +00002201 // Emit source line correspondence into a debug line section.
2202 emitDebugLines();
2203
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002204 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002205 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002206
Devang Patel193f7202009-11-24 01:14:22 +00002207 // Emit info into a debug pubtypes section.
2208 emitDebugPubTypes();
2209
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002210 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002211 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002212
2213 // Emit info into a debug aranges section.
2214 EmitDebugARanges();
2215
2216 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002217 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002218
2219 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002220 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002221
2222 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002223 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002224
Chris Lattnerbc733f52010-03-13 02:17:42 +00002225 // Emit info into a debug str section.
2226 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002227
Devang Patele9a1cca2010-08-02 17:32:15 +00002228 // clean up.
2229 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +00002230 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2231 E = CUMap.end(); I != E; ++I)
2232 delete I->second;
2233 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002234}
2235
Devang Patel53bb5c92009-11-10 23:06:00 +00002236/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002237DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002238 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002239
Devang Patel2db49d72010-05-07 18:11:54 +00002240 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00002241 if (AbsDbgVariable)
2242 return AbsDbgVariable;
2243
Devang Patel2db49d72010-05-07 18:11:54 +00002244 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002245 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00002246 if (!Scope)
2247 return NULL;
2248
Devang Patel26c1e562010-05-20 16:36:41 +00002249 AbsDbgVariable = new DbgVariable(Var);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002250 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002251 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00002252 return AbsDbgVariable;
2253}
2254
Devang Patelee432862010-05-20 19:57:06 +00002255/// collectVariableInfoFromMMITable - Collect variable information from
2256/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002257void
Devang Patelee432862010-05-20 19:57:06 +00002258DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2259 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002260 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patele717faa2009-10-06 01:26:37 +00002261 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2262 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2263 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00002264 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00002265 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00002266 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002267 DIVariable DV(Var);
2268 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002269
Chris Lattnerde4845c2010-04-02 19:42:39 +00002270 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002271 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002272 Scope = ConcreteScopes.lookup(IA);
2273 if (Scope == 0)
2274 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002275
Devang Patelfb0ee432009-11-10 23:20:04 +00002276 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002277 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00002278 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00002279
Devang Patel26c1e562010-05-20 16:36:41 +00002280 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2281 DbgVariable *RegVar = new DbgVariable(DV);
2282 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002283 Scope->addVariable(RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +00002284 if (AbsDbgVariable) {
2285 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2286 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2287 }
Devang Patele717faa2009-10-06 01:26:37 +00002288 }
Devang Patelee432862010-05-20 19:57:06 +00002289}
Devang Patel90a48ad2010-03-15 18:33:46 +00002290
Jim Grosbach1e20b962010-07-21 21:21:52 +00002291/// isDbgValueInUndefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00002292/// DBG_VALUE instruction, is in undefined reg.
2293static bool isDbgValueInUndefinedReg(const MachineInstr *MI) {
2294 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2295 if (MI->getOperand(0).isReg() && !MI->getOperand(0).getReg())
2296 return true;
2297 return false;
2298}
2299
Jim Grosbach1e20b962010-07-21 21:21:52 +00002300/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00002301/// DBG_VALUE instruction, is in a defined reg.
2302static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2303 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2304 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
2305 return true;
2306 return false;
2307}
2308
Devang Patelee432862010-05-20 19:57:06 +00002309/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002310void
Devang Patel78e127d2010-06-25 22:07:34 +00002311DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2312 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002313
Devang Patelee432862010-05-20 19:57:06 +00002314 /// collection info from MMI table.
2315 collectVariableInfoFromMMITable(MF, Processed);
2316
2317 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patel90a48ad2010-03-15 18:33:46 +00002318 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002319 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelee432862010-05-20 19:57:06 +00002320 I != E; ++I)
Devang Patel90a48ad2010-03-15 18:33:46 +00002321 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2322 II != IE; ++II) {
2323 const MachineInstr *MInsn = II;
Devang Patelc3f5f782010-05-25 23:40:22 +00002324 if (!MInsn->isDebugValue() || isDbgValueInUndefinedReg(MInsn))
Devang Patel90a48ad2010-03-15 18:33:46 +00002325 continue;
Devang Patelee432862010-05-20 19:57:06 +00002326 DbgValues.push_back(MInsn);
2327 }
Devang Patel90a48ad2010-03-15 18:33:46 +00002328
Devang Patelc3f5f782010-05-25 23:40:22 +00002329 // This is a collection of DBV_VALUE instructions describing same variable.
2330 SmallVector<const MachineInstr *, 4> MultipleValues;
Devang Patelee432862010-05-20 19:57:06 +00002331 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2332 E = DbgValues.end(); I != E; ++I) {
2333 const MachineInstr *MInsn = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00002334 MultipleValues.clear();
2335 if (isDbgValueInDefinedReg(MInsn))
2336 MultipleValues.push_back(MInsn);
Devang Patelee432862010-05-20 19:57:06 +00002337 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2338 if (Processed.count(DV) != 0)
2339 continue;
2340
Devang Patel354eb7e2010-06-02 19:05:13 +00002341 const MachineInstr *PrevMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002342 for (SmallVector<const MachineInstr *, 8>::iterator MI = I+1,
Devang Patelc3f5f782010-05-25 23:40:22 +00002343 ME = DbgValues.end(); MI != ME; ++MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002344 const MDNode *Var =
Devang Patelc3f5f782010-05-25 23:40:22 +00002345 (*MI)->getOperand((*MI)->getNumOperands()-1).getMetadata();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002346 if (Var == DV && isDbgValueInDefinedReg(*MI) &&
Devang Patel354eb7e2010-06-02 19:05:13 +00002347 !PrevMI->isIdenticalTo(*MI))
Devang Patelc3f5f782010-05-25 23:40:22 +00002348 MultipleValues.push_back(*MI);
Devang Patel354eb7e2010-06-02 19:05:13 +00002349 PrevMI = *MI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002350 }
2351
Devang Patelee432862010-05-20 19:57:06 +00002352 DbgScope *Scope = findDbgScope(MInsn);
Devang Pateld8720f42010-05-27 20:25:04 +00002353 bool CurFnArg = false;
2354 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2355 DISubprogram(DV.getContext()).describes(MF->getFunction()))
2356 CurFnArg = true;
2357 if (!Scope && CurFnArg)
Devang Patelc0c5a262010-05-21 00:10:20 +00002358 Scope = CurrentFnDbgScope;
Devang Patelee432862010-05-20 19:57:06 +00002359 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00002360 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00002361 continue;
2362
2363 Processed.insert(DV);
Devang Patelee432862010-05-20 19:57:06 +00002364 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patelee432862010-05-20 19:57:06 +00002365 Scope->addVariable(RegVar);
Devang Pateld8720f42010-05-27 20:25:04 +00002366 if (!CurFnArg)
Jim Grosbach1e20b962010-07-21 21:21:52 +00002367 DbgVariableLabelsMap[RegVar] = getLabelBeforeInsn(MInsn);
Devang Patelc0c5a262010-05-21 00:10:20 +00002368 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2369 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2370 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002371 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002372 if (MultipleValues.size() <= 1) {
2373 DbgVariableToDbgInstMap[RegVar] = MInsn;
2374 continue;
2375 }
2376
2377 // handle multiple DBG_VALUE instructions describing one variable.
Devang Patelc3f5f782010-05-25 23:40:22 +00002378 if (DotDebugLocEntries.empty())
Devang Patel80250682010-05-26 23:55:23 +00002379 RegVar->setDotDebugLocOffset(0);
2380 else
2381 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Devang Patele2df8422010-05-26 17:29:32 +00002382 const MachineInstr *Begin = NULL;
2383 const MachineInstr *End = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002384 for (SmallVector<const MachineInstr *, 4>::iterator
2385 MVI = MultipleValues.begin(), MVE = MultipleValues.end();
Devang Patel61409622010-07-07 20:12:52 +00002386 MVI != MVE; ++MVI) {
Devang Patele2df8422010-05-26 17:29:32 +00002387 if (!Begin) {
2388 Begin = *MVI;
2389 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002390 }
Devang Patele2df8422010-05-26 17:29:32 +00002391 End = *MVI;
Devang Patelc3f5f782010-05-25 23:40:22 +00002392 MachineLocation MLoc;
Devang Patelb2cf5812010-08-04 18:40:52 +00002393 if (Begin->getNumOperands() == 3) {
2394 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2395 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2396 } else
2397 MLoc = Asm->getDebugValueLocation(Begin);
2398
Devang Patele2df8422010-05-26 17:29:32 +00002399 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2400 const MCSymbol *SLabel = getLabelBeforeInsn(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002401 if (MLoc.getReg())
2402 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
2403
Devang Patele2df8422010-05-26 17:29:32 +00002404 Begin = End;
2405 if (MVI + 1 == MVE) {
2406 // If End is the last instruction then its value is valid
Devang Patelc3f5f782010-05-25 23:40:22 +00002407 // until the end of the funtion.
Devang Patelb2cf5812010-08-04 18:40:52 +00002408 MachineLocation EMLoc;
2409 if (End->getNumOperands() == 3) {
2410 if (End->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2411 EMLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2412 } else
2413 EMLoc = Asm->getDebugValueLocation(End);
Devang Patel5573a7d2010-08-04 22:07:27 +00002414 if (EMLoc.getReg())
2415 DotDebugLocEntries.
2416 push_back(DotDebugLocEntry(SLabel, FunctionEndSym, EMLoc));
Devang Patelc3f5f782010-05-25 23:40:22 +00002417 }
2418 }
2419 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00002420 }
Devang Patel98e1cac2010-05-14 21:01:35 +00002421
2422 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00002423 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00002424 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00002425 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002426 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00002427 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00002428 continue;
2429 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2430 if (Scope)
Devang Patel26c1e562010-05-20 16:36:41 +00002431 Scope->addVariable(new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00002432 }
2433 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002434}
Devang Patel98e1cac2010-05-14 21:01:35 +00002435
Devang Patelc3f5f782010-05-25 23:40:22 +00002436/// getLabelBeforeInsn - Return Label preceding the instruction.
2437const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2438 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2439 LabelsBeforeInsn.find(MI);
2440 if (I == LabelsBeforeInsn.end())
2441 // FunctionBeginSym always preceeds all the instruction in current function.
2442 return FunctionBeginSym;
2443 return I->second;
2444}
2445
2446/// getLabelAfterInsn - Return Label immediately following the instruction.
2447const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2448 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2449 LabelsAfterInsn.find(MI);
2450 if (I == LabelsAfterInsn.end())
2451 return NULL;
2452 return I->second;
Devang Patele717faa2009-10-06 01:26:37 +00002453}
2454
Devang Patelcbbe2872010-10-26 17:49:02 +00002455/// beginInstruction - Process beginning of an instruction.
2456void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002457 if (InsnNeedsLabel.count(MI) == 0) {
2458 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patel553881b2010-03-29 17:20:31 +00002459 return;
Devang Patelc3f5f782010-05-25 23:40:22 +00002460 }
Devang Patelaead63c2010-03-29 22:59:58 +00002461
Devang Patelb2b31a62010-05-26 19:37:24 +00002462 // Check location.
2463 DebugLoc DL = MI->getDebugLoc();
2464 if (!DL.isUnknown()) {
2465 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2466 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Pateleac9c072010-04-27 19:46:33 +00002467 PrevInstLoc = DL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002468 LabelsBeforeInsn[MI] = PrevLabel;
2469 return;
Devang Pateleac9c072010-04-27 19:46:33 +00002470 }
Devang Patel553881b2010-03-29 17:20:31 +00002471
Jim Grosbach1e20b962010-07-21 21:21:52 +00002472 // If location is unknown then use temp label for this DBG_VALUE
Devang Patelb2b31a62010-05-26 19:37:24 +00002473 // instruction.
2474 if (MI->isDebugValue()) {
Devang Patel77051f52010-05-26 21:23:46 +00002475 PrevLabel = MMI->getContext().CreateTempSymbol();
2476 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00002477 LabelsBeforeInsn[MI] = PrevLabel;
2478 return;
2479 }
2480
2481 if (UnknownLocations) {
2482 PrevLabel = recordSourceLine(0, 0, 0);
2483 LabelsBeforeInsn[MI] = PrevLabel;
2484 return;
2485 }
2486
2487 assert (0 && "Instruction is not processed!");
Devang Patel0d20ac82009-10-06 01:50:42 +00002488}
2489
Devang Patelcbbe2872010-10-26 17:49:02 +00002490/// endInstruction - Process end of an instruction.
2491void DwarfDebug::endInstruction(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002492 if (InsnsEndScopeSet.count(MI) != 0) {
2493 // Emit a label if this instruction ends a scope.
2494 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2495 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002496 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002497 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002498}
2499
Devang Pateleac9c072010-04-27 19:46:33 +00002500/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002501DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel61409622010-07-07 20:12:52 +00002502 const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002503 if (!InlinedAt) {
2504 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2505 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002506 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002507 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2508 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002509 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002510 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002511 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002512 WScope->setParent(Parent);
2513 Parent->addScope(WScope);
2514 }
2515
2516 if (!WScope->getParent()) {
2517 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastingscad22ad2010-06-15 23:06:30 +00002518 // We used to check only for a linkage name, but that fails
2519 // since we began omitting the linkage name for private
2520 // functions. The new way is to check for the name in metadata,
2521 // but that's not supported in old .ll test cases. Ergo, we
2522 // check both.
Stuart Hastings215aa152010-06-11 20:08:44 +00002523 if (SPName == Asm->MF->getFunction()->getName() ||
2524 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Pateleac9c072010-04-27 19:46:33 +00002525 CurrentFnDbgScope = WScope;
2526 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002527
Devang Pateleac9c072010-04-27 19:46:33 +00002528 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002529 }
2530
Devang Patel78e127d2010-06-25 22:07:34 +00002531 getOrCreateAbstractScope(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00002532 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2533 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002534 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002535
2536 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2537 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2538 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002539 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002540 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002541 WScope->setParent(Parent);
2542 Parent->addScope(WScope);
2543
2544 ConcreteScopes[InlinedAt] = WScope;
Devang Pateleac9c072010-04-27 19:46:33 +00002545
2546 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002547}
2548
Devang Pateleac9c072010-04-27 19:46:33 +00002549/// hasValidLocation - Return true if debug location entry attached with
2550/// machine instruction encodes valid location info.
2551static bool hasValidLocation(LLVMContext &Ctx,
2552 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002553 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002554 DebugLoc DL = MInsn->getDebugLoc();
2555 if (DL.isUnknown()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002556
Devang Patele9f8f5e2010-05-07 20:54:48 +00002557 const MDNode *S = DL.getScope(Ctx);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002558
Devang Pateleac9c072010-04-27 19:46:33 +00002559 // There is no need to create another DIE for compile unit. For all
2560 // other scopes, create one DbgScope now. This will be translated
2561 // into a scope DIE at the end.
2562 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002563
Devang Pateleac9c072010-04-27 19:46:33 +00002564 Scope = S;
2565 InlinedAt = DL.getInlinedAt(Ctx);
2566 return true;
2567}
2568
2569/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2570/// hierarchy.
2571static void calculateDominanceGraph(DbgScope *Scope) {
2572 assert (Scope && "Unable to calculate scop edominance graph!");
2573 SmallVector<DbgScope *, 4> WorkStack;
2574 WorkStack.push_back(Scope);
2575 unsigned Counter = 0;
2576 while (!WorkStack.empty()) {
2577 DbgScope *WS = WorkStack.back();
2578 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2579 bool visitedChildren = false;
2580 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2581 SE = Children.end(); SI != SE; ++SI) {
2582 DbgScope *ChildScope = *SI;
2583 if (!ChildScope->getDFSOut()) {
2584 WorkStack.push_back(ChildScope);
2585 visitedChildren = true;
2586 ChildScope->setDFSIn(++Counter);
2587 break;
2588 }
2589 }
2590 if (!visitedChildren) {
2591 WorkStack.pop_back();
2592 WS->setDFSOut(++Counter);
2593 }
2594 }
2595}
2596
2597/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002598static
Devang Pateleac9c072010-04-27 19:46:33 +00002599void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2600 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2601{
2602#ifndef NDEBUG
2603 unsigned PrevDFSIn = 0;
2604 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2605 I != E; ++I) {
2606 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2607 II != IE; ++II) {
2608 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002609 const MDNode *Scope = NULL;
2610 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002611
2612 // Check if instruction has valid location information.
2613 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2614 dbgs() << " [ ";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002615 if (InlinedAt)
Devang Pateleac9c072010-04-27 19:46:33 +00002616 dbgs() << "*";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002617 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Pateleac9c072010-04-27 19:46:33 +00002618 MI2ScopeMap.find(MInsn);
2619 if (DI != MI2ScopeMap.end()) {
2620 DbgScope *S = DI->second;
2621 dbgs() << S->getDFSIn();
2622 PrevDFSIn = S->getDFSIn();
2623 } else
2624 dbgs() << PrevDFSIn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002625 } else
Devang Pateleac9c072010-04-27 19:46:33 +00002626 dbgs() << " [ x" << PrevDFSIn;
2627 dbgs() << " ]";
2628 MInsn->dump();
2629 }
2630 dbgs() << "\n";
2631 }
2632#endif
2633}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002634/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002635/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002636bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002637 // If scope information was extracted using .dbg intrinsics then there is not
2638 // any need to extract these information by scanning each instruction.
2639 if (!DbgScopeMap.empty())
2640 return false;
2641
Dan Gohman314bf7c2010-04-23 01:18:53 +00002642 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002643 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2644 SmallVector<DbgRange, 4> MIRanges;
2645 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002646 const MDNode *PrevScope = NULL;
2647 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002648 const MachineInstr *RangeBeginMI = NULL;
2649 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002650 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002651 I != E; ++I) {
2652 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2653 II != IE; ++II) {
2654 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002655 const MDNode *Scope = NULL;
2656 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002657
2658 // Check if instruction has valid location information.
2659 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2660 PrevMI = MInsn;
2661 continue;
2662 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002663
Devang Pateleac9c072010-04-27 19:46:33 +00002664 // If scope has not changed then skip this instruction.
2665 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2666 PrevMI = MInsn;
2667 continue;
2668 }
2669
Jim Grosbach1e20b962010-07-21 21:21:52 +00002670 if (RangeBeginMI) {
2671 // If we have alread seen a beginning of a instruction range and
Devang Pateleac9c072010-04-27 19:46:33 +00002672 // current instruction scope does not match scope of first instruction
2673 // in this range then create a new instruction range.
2674 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002675 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel61409622010-07-07 20:12:52 +00002676 PrevInlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002677 MIRanges.push_back(R);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002678 }
Devang Pateleac9c072010-04-27 19:46:33 +00002679
2680 // This is a beginning of a new instruction range.
2681 RangeBeginMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002682
Devang Pateleac9c072010-04-27 19:46:33 +00002683 // Reset previous markers.
2684 PrevMI = MInsn;
2685 PrevScope = Scope;
2686 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002687 }
2688 }
2689
Devang Pateleac9c072010-04-27 19:46:33 +00002690 // Create last instruction range.
2691 if (RangeBeginMI && PrevMI && PrevScope) {
2692 DbgRange R(RangeBeginMI, PrevMI);
2693 MIRanges.push_back(R);
2694 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002695 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002696
Devang Patel344130e2010-01-04 20:44:00 +00002697 if (!CurrentFnDbgScope)
2698 return false;
2699
Devang Pateleac9c072010-04-27 19:46:33 +00002700 calculateDominanceGraph(CurrentFnDbgScope);
2701 if (PrintDbgScope)
2702 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2703
2704 // Find ranges of instructions covered by each DbgScope;
2705 DbgScope *PrevDbgScope = NULL;
2706 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2707 RE = MIRanges.end(); RI != RE; ++RI) {
2708 const DbgRange &R = *RI;
2709 DbgScope *S = MI2ScopeMap.lookup(R.first);
2710 assert (S && "Lost DbgScope for a machine instruction!");
2711 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2712 PrevDbgScope->closeInsnRange(S);
2713 S->openInsnRange(R.first);
2714 S->extendInsnRange(R.second);
2715 PrevDbgScope = S;
2716 }
2717
2718 if (PrevDbgScope)
2719 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002720
Devang Patele37b0c62010-04-08 18:43:56 +00002721 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002722
2723 return !DbgScopeMap.empty();
2724}
2725
Jim Grosbach1e20b962010-07-21 21:21:52 +00002726/// identifyScopeMarkers() -
Devang Pateleac9c072010-04-27 19:46:33 +00002727/// Each DbgScope has first instruction and last instruction to mark beginning
2728/// and end of a scope respectively. Create an inverse map that list scopes
2729/// starts (and ends) with an instruction. One instruction may start (or end)
2730/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002731void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002732 SmallVector<DbgScope *, 4> WorkList;
2733 WorkList.push_back(CurrentFnDbgScope);
2734 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002735 DbgScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002736
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002737 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002738 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002739 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002740 SE = Children.end(); SI != SE; ++SI)
2741 WorkList.push_back(*SI);
2742
Devang Patel53bb5c92009-11-10 23:06:00 +00002743 if (S->isAbstractScope())
2744 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002745
Devang Pateleac9c072010-04-27 19:46:33 +00002746 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2747 if (Ranges.empty())
2748 continue;
2749 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2750 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002751 assert(RI->first && "DbgRange does not have first instruction!");
2752 assert(RI->second && "DbgRange does not have second instruction!");
Devang Pateleac9c072010-04-27 19:46:33 +00002753 InsnsEndScopeSet.insert(RI->second);
2754 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002755 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002756}
2757
Dan Gohman084751c2010-04-20 00:37:27 +00002758/// FindFirstDebugLoc - Find the first debug location in the function. This
2759/// is intended to be an approximation for the source position of the
2760/// beginning of the function.
2761static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2762 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2763 I != E; ++I)
2764 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2765 MBBI != MBBE; ++MBBI) {
2766 DebugLoc DL = MBBI->getDebugLoc();
2767 if (!DL.isUnknown())
2768 return DL;
2769 }
2770 return DebugLoc();
2771}
2772
Devang Patel9a31f0f2010-10-25 20:45:32 +00002773#ifndef NDEBUG
2774/// CheckLineNumbers - Count basicblocks whose instructions do not have any
2775/// line number information.
2776static void CheckLineNumbers(const MachineFunction *MF) {
2777 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2778 I != E; ++I) {
2779 bool FoundLineNo = false;
2780 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2781 II != IE; ++II) {
2782 const MachineInstr *MI = II;
2783 if (!MI->getDebugLoc().isUnknown()) {
2784 FoundLineNo = true;
2785 break;
2786 }
2787 }
Devang Patel4d7f9a02010-10-28 22:11:59 +00002788 if (!FoundLineNo && I->size())
Devang Patel9a31f0f2010-10-25 20:45:32 +00002789 ++BlocksWithoutLineNo;
2790 }
2791}
2792#endif
2793
Devang Patel2c4ceb12009-11-21 02:48:08 +00002794/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002795/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002796void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002797 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002798 if (!extractScopeInformation()) return;
Devang Patel60b35bd2009-10-06 18:37:31 +00002799
Devang Patel9a31f0f2010-10-25 20:45:32 +00002800#ifndef NDEBUG
2801 CheckLineNumbers(MF);
2802#endif
2803
Devang Pateleac9c072010-04-27 19:46:33 +00002804 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2805 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002806 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002807 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002808
2809 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2810 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002811 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002812 if (FDL.isUnknown()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002813
Devang Patele9f8f5e2010-05-07 20:54:48 +00002814 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings0db42712010-07-19 23:56:30 +00002815 const MDNode *TheScope = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002816
Chris Lattnerde4845c2010-04-02 19:42:39 +00002817 DISubprogram SP = getDISubprogram(Scope);
2818 unsigned Line, Col;
2819 if (SP.Verify()) {
2820 Line = SP.getLineNumber();
2821 Col = 0;
Stuart Hastings0db42712010-07-19 23:56:30 +00002822 TheScope = SP;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002823 } else {
2824 Line = FDL.getLine();
2825 Col = FDL.getCol();
Stuart Hastings0db42712010-07-19 23:56:30 +00002826 TheScope = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002827 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002828
Stuart Hastings0db42712010-07-19 23:56:30 +00002829 recordSourceLine(Line, Col, TheScope);
Devang Patelb2b31a62010-05-26 19:37:24 +00002830
Devang Patelb9abe9f2010-06-02 16:42:51 +00002831 /// ProcessedArgs - Collection of arguments already processed.
2832 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2833
Devang Patelb2b31a62010-05-26 19:37:24 +00002834 DebugLoc PrevLoc;
2835 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2836 I != E; ++I)
2837 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2838 II != IE; ++II) {
2839 const MachineInstr *MI = II;
2840 DebugLoc DL = MI->getDebugLoc();
2841 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002842 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2843 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2844 if (!DV.Verify()) continue;
Devang Patel55e97172010-05-27 16:47:30 +00002845 // If DBG_VALUE is for a local variable then it needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002846 if (DV.getTag() != dwarf::DW_TAG_arg_variable
Devang Patel7fb231c2010-07-01 21:38:08 +00002847 && isDbgValueInUndefinedReg(MI) == false)
Devang Patelb2b31a62010-05-26 19:37:24 +00002848 InsnNeedsLabel.insert(MI);
Devang Patel55e97172010-05-27 16:47:30 +00002849 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel7fb231c2010-07-01 21:38:08 +00002850 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2851 describes(MF->getFunction()))
Devang Patel55e97172010-05-27 16:47:30 +00002852 InsnNeedsLabel.insert(MI);
2853 // DBG_VALUE indicating argument location change needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002854 else if (isDbgValueInUndefinedReg(MI) == false
2855 && !ProcessedArgs.insert(DV))
Devang Patelb2b31a62010-05-26 19:37:24 +00002856 InsnNeedsLabel.insert(MI);
2857 } else {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002858 // If location is unknown then instruction needs a location only if
Devang Patelb2b31a62010-05-26 19:37:24 +00002859 // UnknownLocations flag is set.
2860 if (DL.isUnknown()) {
2861 if (UnknownLocations && !PrevLoc.isUnknown())
2862 InsnNeedsLabel.insert(MI);
2863 } else if (DL != PrevLoc)
2864 // Otherwise, instruction needs a location only if it is new location.
2865 InsnNeedsLabel.insert(MI);
2866 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002867
Devang Patelb2b31a62010-05-26 19:37:24 +00002868 if (!DL.isUnknown() || UnknownLocations)
2869 PrevLoc = DL;
2870 }
2871
2872 PrevLabel = FunctionBeginSym;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002873}
2874
Devang Patel2c4ceb12009-11-21 02:48:08 +00002875/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002876///
Chris Lattnereec791a2010-01-26 23:18:02 +00002877void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002878 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002879
Devang Patel344130e2010-01-04 20:44:00 +00002880 if (CurrentFnDbgScope) {
Devang Patel65eb4822010-05-22 00:04:14 +00002881
Devang Patelc3f5f782010-05-25 23:40:22 +00002882 // Define end label for subprogram.
2883 FunctionEndSym = Asm->GetTempSymbol("func_end",
2884 Asm->getFunctionNumber());
2885 // Assumes in correct section after the entry point.
2886 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002887
Devang Patel78e127d2010-06-25 22:07:34 +00002888 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2889 collectVariableInfo(MF, ProcessedVars);
Devang Patel65eb4822010-05-22 00:04:14 +00002890
Devang Patel344130e2010-01-04 20:44:00 +00002891 // Get function line info.
2892 if (!Lines.empty()) {
2893 // Get section line info.
2894 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2895 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2896 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2897 // Append the function info to section info.
2898 SectionLineInfos.insert(SectionLineInfos.end(),
2899 Lines.begin(), Lines.end());
2900 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002901
Devang Patel344130e2010-01-04 20:44:00 +00002902 // Construct abstract scopes.
2903 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel78e127d2010-06-25 22:07:34 +00002904 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel78e127d2010-06-25 22:07:34 +00002905 DISubprogram SP((*AI)->getScopeNode());
2906 if (SP.Verify()) {
2907 // Collect info for variables that were optimized out.
2908 StringRef FName = SP.getLinkageName();
2909 if (FName.empty())
2910 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00002911 if (NamedMDNode *NMD =
2912 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
Devang Patel78e127d2010-06-25 22:07:34 +00002913 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002914 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00002915 if (!DV || !ProcessedVars.insert(DV))
2916 continue;
Devang Patel1d68d212010-06-30 00:11:08 +00002917 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel78e127d2010-06-25 22:07:34 +00002918 if (Scope)
2919 Scope->addVariable(new DbgVariable(DV));
2920 }
2921 }
2922 }
Devang Patel90e19aa2010-06-30 01:40:11 +00002923 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2924 constructScopeDIE(*AI);
Devang Patel78e127d2010-06-25 22:07:34 +00002925 }
Devang Patel61409622010-07-07 20:12:52 +00002926
Devang Patel9c488372010-05-04 06:15:30 +00002927 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002928
Devang Patel9c488372010-05-04 06:15:30 +00002929 if (!DisableFramePointerElim(*MF))
Jim Grosbach1e20b962010-07-21 21:21:52 +00002930 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
Devang Patel9c488372010-05-04 06:15:30 +00002931 dwarf::DW_FORM_flag, 1);
2932
2933
Chris Lattnerd38fee82010-04-05 00:13:49 +00002934 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002935 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002936 }
2937
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002938 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002939 CurrentFnDbgScope = NULL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002940 InsnNeedsLabel.clear();
Devang Patel26c1e562010-05-20 16:36:41 +00002941 DbgVariableToFrameIndexMap.clear();
2942 VarToAbstractVarMap.clear();
2943 DbgVariableToDbgInstMap.clear();
2944 DbgVariableLabelsMap.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002945 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002946 InsnsEndScopeSet.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002947 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002948 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002949 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002950 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002951 LabelsBeforeInsn.clear();
2952 LabelsAfterInsn.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002953 Lines.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002954 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002955}
2956
Devang Patel26c1e562010-05-20 16:36:41 +00002957/// recordVariableFrameIndex - Record a variable's index.
2958void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2959 assert (V && "Invalid DbgVariable!");
2960 DbgVariableToFrameIndexMap[V] = Index;
2961}
2962
2963/// findVariableFrameIndex - Return true if frame index for the variable
2964/// is found. Update FI to hold value of the index.
2965bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2966 assert (V && "Invalid DbgVariable!");
2967 DenseMap<const DbgVariable *, int>::iterator I =
2968 DbgVariableToFrameIndexMap.find(V);
2969 if (I == DbgVariableToFrameIndexMap.end())
2970 return false;
2971 *FI = I->second;
2972 return true;
2973}
2974
2975/// findVariableLabel - Find MCSymbol for the variable.
2976const MCSymbol *DwarfDebug::findVariableLabel(const DbgVariable *V) {
2977 DenseMap<const DbgVariable *, const MCSymbol *>::iterator I
2978 = DbgVariableLabelsMap.find(V);
2979 if (I == DbgVariableLabelsMap.end())
2980 return NULL;
2981 else return I->second;
2982}
2983
Jim Grosbach1e20b962010-07-21 21:21:52 +00002984/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patelee432862010-05-20 19:57:06 +00002985/// instruction.
2986DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2987 DbgScope *Scope = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002988 LLVMContext &Ctx =
Devang Patelee432862010-05-20 19:57:06 +00002989 MInsn->getParent()->getParent()->getFunction()->getContext();
2990 DebugLoc DL = MInsn->getDebugLoc();
2991
Jim Grosbach1e20b962010-07-21 21:21:52 +00002992 if (DL.isUnknown())
Devang Patelee432862010-05-20 19:57:06 +00002993 return Scope;
2994
2995 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2996 Scope = ConcreteScopes.lookup(IA);
2997 if (Scope == 0)
2998 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002999
Devang Patelee432862010-05-20 19:57:06 +00003000 return Scope;
3001}
3002
3003
Chris Lattnerc6087842010-03-09 04:54:43 +00003004/// recordSourceLine - Register a source line with debug info. Returns the
3005/// unique label that was emitted and which provides correspondence to
3006/// the source line list.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003007MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel61409622010-07-07 20:12:52 +00003008 const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00003009 StringRef Dir;
3010 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00003011
Dan Gohman1cc0d622010-05-05 23:41:32 +00003012 unsigned Src = 1;
3013 if (S) {
3014 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00003015
Dan Gohman1cc0d622010-05-05 23:41:32 +00003016 if (Scope.isCompileUnit()) {
3017 DICompileUnit CU(S);
3018 Dir = CU.getDirectory();
3019 Fn = CU.getFilename();
Devang Patel3cabc9d2010-10-28 17:30:52 +00003020 } else if (Scope.isFile()) {
3021 DIFile F(S);
3022 Dir = F.getDirectory();
3023 Fn = F.getFilename();
Dan Gohman1cc0d622010-05-05 23:41:32 +00003024 } else if (Scope.isSubprogram()) {
3025 DISubprogram SP(S);
3026 Dir = SP.getDirectory();
3027 Fn = SP.getFilename();
3028 } else if (Scope.isLexicalBlock()) {
3029 DILexicalBlock DB(S);
3030 Dir = DB.getDirectory();
3031 Fn = DB.getFilename();
3032 } else
3033 assert(0 && "Unexpected scope info");
3034
3035 Src = GetOrCreateSourceID(Dir, Fn);
3036 }
3037
Chris Lattner63d78362010-03-14 08:36:50 +00003038 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner25b68c62010-03-14 08:15:55 +00003039 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00003040
Chris Lattnerc6087842010-03-09 04:54:43 +00003041 Asm->OutStreamer.EmitLabel(Label);
3042 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00003043}
3044
Bill Wendling829e67b2009-05-20 23:22:40 +00003045//===----------------------------------------------------------------------===//
3046// Emit Methods
3047//===----------------------------------------------------------------------===//
3048
Devang Patel2c4ceb12009-11-21 02:48:08 +00003049/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00003050///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003051unsigned
3052DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003053 // Get the children.
3054 const std::vector<DIE *> &Children = Die->getChildren();
3055
3056 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003057 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00003058 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00003059
3060 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003061 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00003062
3063 // Get the abbreviation for this DIE.
3064 unsigned AbbrevNumber = Die->getAbbrevNumber();
3065 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3066
3067 // Set DIE offset
3068 Die->setOffset(Offset);
3069
3070 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00003071 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003072
3073 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
3074 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3075
3076 // Size the DIE attribute values.
3077 for (unsigned i = 0, N = Values.size(); i < N; ++i)
3078 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00003079 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00003080
3081 // Size the DIE children if any.
3082 if (!Children.empty()) {
3083 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
3084 "Children flag not set");
3085
3086 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003087 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00003088
3089 // End of children marker.
3090 Offset += sizeof(int8_t);
3091 }
3092
3093 Die->setSize(Offset - Die->getOffset());
3094 return Offset;
3095}
3096
Devang Patel2c4ceb12009-11-21 02:48:08 +00003097/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00003098///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003099void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00003100 unsigned PrevOffset = 0;
3101 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3102 E = CUMap.end(); I != E; ++I) {
3103 // Compute size of compile unit header.
3104 static unsigned Offset = PrevOffset +
3105 sizeof(int32_t) + // Length of Compilation Unit Info
3106 sizeof(int16_t) + // DWARF version number
3107 sizeof(int32_t) + // Offset Into Abbrev. Section
3108 sizeof(int8_t); // Pointer Size (in bytes)
3109 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
3110 PrevOffset = Offset;
3111 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003112}
3113
Chris Lattner11b8f302010-04-04 23:02:02 +00003114/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
3115/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00003116static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00003117 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003118 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00003119 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003120
Chris Lattner9c69e285532010-04-04 22:59:04 +00003121 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
3122 Asm->OutStreamer.EmitLabel(TmpSym);
3123 return TmpSym;
3124}
3125
3126/// EmitSectionLabels - Emit initial Dwarf sections with a label at
3127/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00003128void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003129 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003130
Bill Wendling94d04b82009-05-20 23:21:38 +00003131 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003132 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003133 DwarfFrameSectionSym =
3134 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3135 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003136
Jim Grosbach1e20b962010-07-21 21:21:52 +00003137 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003138 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003139 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003140 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00003141 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003142
Chris Lattner9c69e285532010-04-04 22:59:04 +00003143 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00003144 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003145
Devang Patelaf608bd2010-08-24 00:06:12 +00003146 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00003147 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3148 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3149 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003150 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003151 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00003152 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3153 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00003154
Devang Patelc3f5f782010-05-25 23:40:22 +00003155 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3156 "section_debug_loc");
3157
Chris Lattner9c69e285532010-04-04 22:59:04 +00003158 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00003159 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003160}
3161
Devang Patel2c4ceb12009-11-21 02:48:08 +00003162/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00003163///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003164void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003165 // Get the abbreviation for this DIE.
3166 unsigned AbbrevNumber = Die->getAbbrevNumber();
3167 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3168
Bill Wendling94d04b82009-05-20 23:21:38 +00003169 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00003170 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00003171 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3172 Twine::utohexstr(Die->getOffset()) + ":0x" +
3173 Twine::utohexstr(Die->getSize()) + " " +
3174 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003175 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003176
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003177 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00003178 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3179
3180 // Emit the DIE attribute values.
3181 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3182 unsigned Attr = AbbrevData[i].getAttribute();
3183 unsigned Form = AbbrevData[i].getForm();
3184 assert(Form && "Too many attributes for DIE (check abbreviation)");
3185
Chris Lattner3f53c832010-04-04 18:52:31 +00003186 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00003187 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003188
Bill Wendling94d04b82009-05-20 23:21:38 +00003189 switch (Attr) {
3190 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00003191 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003192 break;
3193 case dwarf::DW_AT_abstract_origin: {
3194 DIEEntry *E = cast<DIEEntry>(Values[i]);
3195 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00003196 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00003197 Asm->EmitInt32(Addr);
3198 break;
3199 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003200 case dwarf::DW_AT_ranges: {
3201 // DW_AT_range Value encodes offset in debug_range section.
3202 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00003203
3204 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
3205 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
3206 V->getValue(),
3207 4);
3208 } else {
3209 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3210 V->getValue(),
3211 DwarfDebugRangeSectionSym,
3212 4);
3213 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003214 break;
3215 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003216 case dwarf::DW_AT_location: {
3217 if (UseDotDebugLocEntry.count(Die) != 0) {
3218 DIELabel *L = cast<DIELabel>(Values[i]);
3219 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3220 } else
3221 Values[i]->EmitValue(Asm, Form);
3222 break;
3223 }
Devang Patel2a361602010-09-29 19:08:08 +00003224 case dwarf::DW_AT_accessibility: {
3225 if (Asm->isVerbose()) {
3226 DIEInteger *V = cast<DIEInteger>(Values[i]);
3227 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
3228 }
3229 Values[i]->EmitValue(Asm, Form);
3230 break;
3231 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003232 default:
3233 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003234 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00003235 break;
3236 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003237 }
3238
3239 // Emit the DIE children if any.
3240 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3241 const std::vector<DIE *> &Children = Die->getChildren();
3242
3243 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003244 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00003245
Chris Lattner3f53c832010-04-04 18:52:31 +00003246 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00003247 Asm->OutStreamer.AddComment("End Of Children Mark");
3248 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003249 }
3250}
3251
Devang Patel8a241142009-12-09 18:24:21 +00003252/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003253///
Devang Patel8a241142009-12-09 18:24:21 +00003254void DwarfDebug::emitDebugInfo() {
3255 // Start debug info section.
3256 Asm->OutStreamer.SwitchSection(
3257 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00003258 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3259 E = CUMap.end(); I != E; ++I) {
3260 CompileUnit *TheCU = I->second;
3261 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00003262
Devang Patel163a9f72010-05-10 22:49:55 +00003263 // Emit the compile units header.
3264 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3265 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003266
Devang Patel163a9f72010-05-10 22:49:55 +00003267 // Emit size of content not including length itself
3268 unsigned ContentSize = Die->getSize() +
3269 sizeof(int16_t) + // DWARF version number
3270 sizeof(int32_t) + // Offset Into Abbrev. Section
3271 sizeof(int8_t) + // Pointer Size (in bytes)
3272 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003273
Devang Patel163a9f72010-05-10 22:49:55 +00003274 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3275 Asm->EmitInt32(ContentSize);
3276 Asm->OutStreamer.AddComment("DWARF version number");
3277 Asm->EmitInt16(dwarf::DWARF_VERSION);
3278 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3279 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3280 DwarfAbbrevSectionSym);
3281 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3282 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003283
Devang Patel163a9f72010-05-10 22:49:55 +00003284 emitDIE(Die);
3285 // FIXME - extra padding for gdb bug.
3286 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3287 Asm->EmitInt8(0);
3288 Asm->EmitInt8(0);
3289 Asm->EmitInt8(0);
3290 Asm->EmitInt8(0);
3291 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3292 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003293}
3294
Devang Patel2c4ceb12009-11-21 02:48:08 +00003295/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003296///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003297void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00003298 // Check to see if it is worth the effort.
3299 if (!Abbreviations.empty()) {
3300 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003301 Asm->OutStreamer.SwitchSection(
3302 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003303
Chris Lattnerc0215722010-04-04 19:25:43 +00003304 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003305
3306 // For each abbrevation.
3307 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3308 // Get abbreviation data
3309 const DIEAbbrev *Abbrev = Abbreviations[i];
3310
3311 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003312 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00003313
3314 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003315 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00003316 }
3317
3318 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003319 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00003320
Chris Lattnerc0215722010-04-04 19:25:43 +00003321 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003322 }
3323}
3324
Devang Patel2c4ceb12009-11-21 02:48:08 +00003325/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00003326/// the line matrix.
3327///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003328void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003329 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00003330 Asm->OutStreamer.AddComment("Extended Op");
3331 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003332
Chris Lattner233f52b2010-03-09 23:52:58 +00003333 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003334 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003335 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3336 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3337
3338 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003339
Chris Lattnerc0215722010-04-04 19:25:43 +00003340 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003341 Asm->getTargetData().getPointerSize(),
3342 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003343
3344 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00003345 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3346 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00003347 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00003348 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003349}
3350
Devang Patel2c4ceb12009-11-21 02:48:08 +00003351/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00003352///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003353void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003354 // If the target is using .loc/.file, the assembler will be emitting the
3355 // .debug_line table automatically.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003356 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00003357 return;
3358
3359 // Minimum line delta, thus ranging from -10..(255-10).
3360 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
3361 // Maximum line delta, thus ranging from -10..(255-10).
3362 const int MaxLineDelta = 255 + MinLineDelta;
3363
3364 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003365 Asm->OutStreamer.SwitchSection(
3366 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003367
3368 // Construct the section header.
Chris Lattner233f52b2010-03-09 23:52:58 +00003369 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnera6437182010-04-04 19:58:12 +00003370 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
3371 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00003372 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003373
Chris Lattner233f52b2010-03-09 23:52:58 +00003374 Asm->OutStreamer.AddComment("DWARF version number");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003375 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00003376
Chris Lattner233f52b2010-03-09 23:52:58 +00003377 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnera6437182010-04-04 19:58:12 +00003378 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
3379 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00003380 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003381
Chris Lattner233f52b2010-03-09 23:52:58 +00003382 Asm->OutStreamer.AddComment("Minimum Instruction Length");
3383 Asm->EmitInt8(1);
3384 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
3385 Asm->EmitInt8(1);
3386 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
3387 Asm->EmitInt8(MinLineDelta);
3388 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3389 Asm->EmitInt8(MaxLineDelta);
3390 Asm->OutStreamer.AddComment("Special Opcode Base");
3391 Asm->EmitInt8(-MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003392
3393 // Line number standard opcode encodings argument count
Chris Lattner233f52b2010-03-09 23:52:58 +00003394 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3395 Asm->EmitInt8(0);
3396 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3397 Asm->EmitInt8(1);
3398 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3399 Asm->EmitInt8(1);
3400 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3401 Asm->EmitInt8(1);
3402 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3403 Asm->EmitInt8(1);
3404 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3405 Asm->EmitInt8(0);
3406 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3407 Asm->EmitInt8(0);
3408 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3409 Asm->EmitInt8(0);
3410 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3411 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003412
3413 // Emit directories.
3414 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00003415 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner3f53c832010-04-04 18:52:31 +00003416 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003417 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003418 }
3419
Chris Lattner233f52b2010-03-09 23:52:58 +00003420 Asm->OutStreamer.AddComment("End of directories");
3421 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003422
3423 // Emit files.
3424 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3425 // Remember source id starts at 1.
3426 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003427 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner3f53c832010-04-04 18:52:31 +00003428 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003429 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003430
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003431 Asm->EmitULEB128(Id.first, "Directory #");
3432 Asm->EmitULEB128(0, "Mod date");
3433 Asm->EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00003434 }
3435
Chris Lattner233f52b2010-03-09 23:52:58 +00003436 Asm->OutStreamer.AddComment("End of files");
3437 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003438
Chris Lattnerc0215722010-04-04 19:25:43 +00003439 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003440
3441 // A sequence for each text section.
3442 unsigned SecSrcLinesSize = SectionSourceLines.size();
3443
3444 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3445 // Isolate current sections line info.
3446 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3447
Bill Wendling94d04b82009-05-20 23:21:38 +00003448 // Dwarf assumes we start with first line of first source file.
3449 unsigned Source = 1;
3450 unsigned Line = 1;
3451
3452 // Construct rows of the address, source, line, column matrix.
3453 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3454 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner25b68c62010-03-14 08:15:55 +00003455 MCSymbol *Label = LineInfo.getLabel();
Chris Lattnerb9130602010-03-14 02:20:58 +00003456 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling94d04b82009-05-20 23:21:38 +00003457
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003458 if (Asm->isVerbose()) {
Chris Lattner188a87d2010-03-10 01:04:13 +00003459 std::pair<unsigned, unsigned> SrcID =
Bill Wendling94d04b82009-05-20 23:21:38 +00003460 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner188a87d2010-03-10 01:04:13 +00003461 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner49f618a2010-03-10 02:29:31 +00003462 "/" +
3463 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner188a87d2010-03-10 01:04:13 +00003464 ":" + Twine(LineInfo.getLine()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003465 }
3466
3467 // Define the line address.
Chris Lattner233f52b2010-03-09 23:52:58 +00003468 Asm->OutStreamer.AddComment("Extended Op");
3469 Asm->EmitInt8(0);
3470 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003471 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003472
3473 Asm->OutStreamer.AddComment("DW_LNE_set_address");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003474 Asm->EmitInt8(dwarf::DW_LNE_set_address);
Chris Lattner233f52b2010-03-09 23:52:58 +00003475
3476 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003477 Asm->OutStreamer.EmitSymbolValue(Label,
3478 Asm->getTargetData().getPointerSize(),
Chris Lattnerb9130602010-03-14 02:20:58 +00003479 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003480
Bill Wendling94d04b82009-05-20 23:21:38 +00003481 // If change of source, then switch to the new source.
3482 if (Source != LineInfo.getSourceID()) {
3483 Source = LineInfo.getSourceID();
Chris Lattner233f52b2010-03-09 23:52:58 +00003484 Asm->OutStreamer.AddComment("DW_LNS_set_file");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003485 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003486 Asm->EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00003487 }
3488
3489 // If change of line.
3490 if (Line != LineInfo.getLine()) {
3491 // Determine offset.
3492 int Offset = LineInfo.getLine() - Line;
3493 int Delta = Offset - MinLineDelta;
3494
3495 // Update line.
3496 Line = LineInfo.getLine();
3497
3498 // If delta is small enough and in range...
3499 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3500 // ... then use fast opcode.
Chris Lattner233f52b2010-03-09 23:52:58 +00003501 Asm->OutStreamer.AddComment("Line Delta");
3502 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003503 } else {
3504 // ... otherwise use long hand.
Chris Lattner233f52b2010-03-09 23:52:58 +00003505 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling94d04b82009-05-20 23:21:38 +00003506 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003507 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner233f52b2010-03-09 23:52:58 +00003508 Asm->OutStreamer.AddComment("DW_LNS_copy");
3509 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003510 }
3511 } else {
3512 // Copy the previous row (different address or source)
Chris Lattner233f52b2010-03-09 23:52:58 +00003513 Asm->OutStreamer.AddComment("DW_LNS_copy");
3514 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003515 }
3516 }
3517
Devang Patel2c4ceb12009-11-21 02:48:08 +00003518 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003519 }
3520
3521 if (SecSrcLinesSize == 0)
3522 // Because we're emitting a debug_line section, we still need a line
3523 // table. The linker and friends expect it to exist. If there's nothing to
3524 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003525 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003526
Chris Lattnerc0215722010-04-04 19:25:43 +00003527 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003528}
3529
Devang Patel2c4ceb12009-11-21 02:48:08 +00003530/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003531///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003532void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003533 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003534 return;
3535
Chris Lattnerd38fee82010-04-05 00:13:49 +00003536 int stackGrowth = Asm->getTargetData().getPointerSize();
3537 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3538 TargetFrameInfo::StackGrowsDown)
3539 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003540
3541 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003542 Asm->OutStreamer.SwitchSection(
3543 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003544
Chris Lattnerc0215722010-04-04 19:25:43 +00003545 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003546 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003547 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3548 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003549
Chris Lattnerc0215722010-04-04 19:25:43 +00003550 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003551 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003552 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003553 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003554 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003555 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003556 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003557 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3558 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003559 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003560 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003561 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003562
3563 std::vector<MachineMove> Moves;
3564 RI->getInitialFrameState(Moves);
3565
Chris Lattner02b86b92010-04-04 23:41:46 +00003566 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003567
Chris Lattner059ea132010-04-28 01:05:45 +00003568 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003569 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003570}
3571
Devang Patel2c4ceb12009-11-21 02:48:08 +00003572/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003573/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003574void DwarfDebug::
3575emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003576 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003577 return;
3578
3579 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003580 Asm->OutStreamer.SwitchSection(
3581 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003582
Chris Lattner233f52b2010-03-09 23:52:58 +00003583 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003584 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003585 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003586 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003587 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003588 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003589
Chris Lattner206d61e2010-03-13 07:26:18 +00003590 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003591
Chris Lattner233f52b2010-03-09 23:52:58 +00003592 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003593 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner6189ed12010-04-04 23:25:33 +00003594 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003595
Chris Lattner233f52b2010-03-09 23:52:58 +00003596 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003597 MCSymbol *FuncBeginSym =
3598 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003599 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003600 Asm->getTargetData().getPointerSize(),
3601 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003602
3603
Chris Lattner233f52b2010-03-09 23:52:58 +00003604 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003605 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003606 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003607
Chris Lattner02b86b92010-04-04 23:41:46 +00003608 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003609
Chris Lattner059ea132010-04-28 01:05:45 +00003610 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003611 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003612}
3613
Devang Patel8a241142009-12-09 18:24:21 +00003614/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3615///
3616void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00003617 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3618 E = CUMap.end(); I != E; ++I) {
3619 CompileUnit *TheCU = I->second;
3620 // Start the dwarf pubnames section.
3621 Asm->OutStreamer.SwitchSection(
3622 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003623
Devang Patel163a9f72010-05-10 22:49:55 +00003624 Asm->OutStreamer.AddComment("Length of Public Names Info");
3625 Asm->EmitLabelDifference(
3626 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3627 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003628
Devang Patel163a9f72010-05-10 22:49:55 +00003629 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3630 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003631
Devang Patel163a9f72010-05-10 22:49:55 +00003632 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003633 Asm->EmitInt16(dwarf::DWARF_VERSION);
3634
Devang Patel163a9f72010-05-10 22:49:55 +00003635 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003636 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00003637 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003638
Devang Patel163a9f72010-05-10 22:49:55 +00003639 Asm->OutStreamer.AddComment("Compilation Unit Length");
3640 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3641 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3642 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003643
Devang Patel163a9f72010-05-10 22:49:55 +00003644 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3645 for (StringMap<DIE*>::const_iterator
3646 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3647 const char *Name = GI->getKeyData();
3648 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003649
Devang Patel163a9f72010-05-10 22:49:55 +00003650 Asm->OutStreamer.AddComment("DIE offset");
3651 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003652
Devang Patel163a9f72010-05-10 22:49:55 +00003653 if (Asm->isVerbose())
3654 Asm->OutStreamer.AddComment("External Name");
3655 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3656 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003657
Devang Patel163a9f72010-05-10 22:49:55 +00003658 Asm->OutStreamer.AddComment("End Mark");
3659 Asm->EmitInt32(0);
3660 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3661 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003662 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003663}
3664
Devang Patel193f7202009-11-24 01:14:22 +00003665void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00003666 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3667 E = CUMap.end(); I != E; ++I) {
3668 CompileUnit *TheCU = I->second;
3669 // Start the dwarf pubnames section.
3670 Asm->OutStreamer.SwitchSection(
3671 Asm->getObjFileLowering().getDwarfPubTypesSection());
3672 Asm->OutStreamer.AddComment("Length of Public Types Info");
3673 Asm->EmitLabelDifference(
3674 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3675 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003676
Devang Patel163a9f72010-05-10 22:49:55 +00003677 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3678 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003679
Devang Patel163a9f72010-05-10 22:49:55 +00003680 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3681 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003682
Devang Patel163a9f72010-05-10 22:49:55 +00003683 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3684 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3685 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003686
Devang Patel163a9f72010-05-10 22:49:55 +00003687 Asm->OutStreamer.AddComment("Compilation Unit Length");
3688 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3689 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3690 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003691
Devang Patel163a9f72010-05-10 22:49:55 +00003692 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3693 for (StringMap<DIE*>::const_iterator
3694 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3695 const char *Name = GI->getKeyData();
3696 DIE * Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003697
Devang Patel163a9f72010-05-10 22:49:55 +00003698 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3699 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003700
Devang Patel163a9f72010-05-10 22:49:55 +00003701 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3702 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3703 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003704
Devang Patel163a9f72010-05-10 22:49:55 +00003705 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003706 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00003707 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3708 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003709 }
Devang Patel193f7202009-11-24 01:14:22 +00003710}
3711
Devang Patel2c4ceb12009-11-21 02:48:08 +00003712/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003713///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003714void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003715 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003716 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003717
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003718 // Start the dwarf str section.
3719 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003720 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003721
Chris Lattnerbc733f52010-03-13 02:17:42 +00003722 // Get all of the string pool entries and put them in an array by their ID so
3723 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003724 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00003725 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003726
Chris Lattnerbc733f52010-03-13 02:17:42 +00003727 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3728 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3729 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003730
Chris Lattnerbc733f52010-03-13 02:17:42 +00003731 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003732
Chris Lattnerbc733f52010-03-13 02:17:42 +00003733 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003734 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003735 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003736
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003737 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003738 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003739 }
3740}
3741
Devang Patel2c4ceb12009-11-21 02:48:08 +00003742/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003743///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003744void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00003745 if (DotDebugLocEntries.empty())
3746 return;
3747
Bill Wendling94d04b82009-05-20 23:21:38 +00003748 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003749 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00003750 Asm->getObjFileLowering().getDwarfLocSection());
3751 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00003752 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3753 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003754 for (SmallVector<DotDebugLocEntry, 4>::iterator
3755 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00003756 I != E; ++I, ++index) {
Devang Patelc3f5f782010-05-25 23:40:22 +00003757 DotDebugLocEntry Entry = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00003758 if (Entry.isEmpty()) {
3759 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3760 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00003761 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00003762 } else {
3763 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3764 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3765 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3766 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patelb2cf5812010-08-04 18:40:52 +00003767 if (int Offset = Entry.Loc.getOffset()) {
3768 // If the value is at a certain offset from frame register then
3769 // use DW_OP_fbreg.
3770 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patelc3f5f782010-05-25 23:40:22 +00003771 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patelb2cf5812010-08-04 18:40:52 +00003772 Asm->EmitInt16(1 + OffsetSize);
3773 Asm->OutStreamer.AddComment(
3774 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
3775 Asm->EmitInt8(dwarf::DW_OP_fbreg);
3776 Asm->OutStreamer.AddComment("Offset");
3777 Asm->EmitSLEB128(Offset);
Devang Patelc3f5f782010-05-25 23:40:22 +00003778 } else {
Devang Patelb2cf5812010-08-04 18:40:52 +00003779 if (Reg < 32) {
3780 Asm->OutStreamer.AddComment("Loc expr size");
3781 Asm->EmitInt16(1);
3782 Asm->OutStreamer.AddComment(
Devang Patela54e0cc2010-08-04 20:32:36 +00003783 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patelb2cf5812010-08-04 18:40:52 +00003784 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3785 } else {
3786 Asm->OutStreamer.AddComment("Loc expr size");
3787 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
3788 Asm->EmitInt8(dwarf::DW_OP_regx);
3789 Asm->EmitULEB128(Reg);
3790 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003791 }
3792 }
3793 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003794}
3795
3796/// EmitDebugARanges - Emit visible names into a debug aranges section.
3797///
3798void DwarfDebug::EmitDebugARanges() {
3799 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003800 Asm->OutStreamer.SwitchSection(
3801 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003802}
3803
Devang Patel2c4ceb12009-11-21 02:48:08 +00003804/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003805///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003806void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003807 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003808 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003809 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003810 unsigned char Size = Asm->getTargetData().getPointerSize();
3811 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00003812 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00003813 I != E; ++I) {
3814 if (*I)
3815 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003816 else
Devang Pateleac9c072010-04-27 19:46:33 +00003817 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003818 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003819}
3820
Devang Patel2c4ceb12009-11-21 02:48:08 +00003821/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003822///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003823void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003824 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003825 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003826 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003827 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003828 }
3829}
3830
Devang Patel2c4ceb12009-11-21 02:48:08 +00003831/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003832/// Section Header:
3833/// 1. length of section
3834/// 2. Dwarf version number
3835/// 3. address size.
3836///
3837/// Entries (one "entry" for each function that was inlined):
3838///
3839/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3840/// otherwise offset into __debug_str for regular function name.
3841/// 2. offset into __debug_str section for regular function name.
3842/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3843/// instances for the function.
3844///
3845/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3846/// inlined instance; the die_offset points to the inlined_subroutine die in the
3847/// __debug_info section, and the low_pc is the starting address for the
3848/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003849void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003850 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003851 return;
3852
Devang Patel163a9f72010-05-10 22:49:55 +00003853 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003854 return;
3855
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003856 Asm->OutStreamer.SwitchSection(
3857 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003858
Chris Lattner233f52b2010-03-09 23:52:58 +00003859 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003860 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3861 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003862
Chris Lattnerc0215722010-04-04 19:25:43 +00003863 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003864
Chris Lattner233f52b2010-03-09 23:52:58 +00003865 Asm->OutStreamer.AddComment("Dwarf Version");
3866 Asm->EmitInt16(dwarf::DWARF_VERSION);
3867 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003868 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003869
Devang Patele9f8f5e2010-05-07 20:54:48 +00003870 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003871 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003872
Devang Patele9f8f5e2010-05-07 20:54:48 +00003873 const MDNode *Node = *I;
3874 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003875 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003876 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003877 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003878 StringRef LName = SP.getLinkageName();
3879 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003880
Chris Lattner233f52b2010-03-09 23:52:58 +00003881 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003882 if (LName.empty()) {
3883 Asm->OutStreamer.EmitBytes(Name, 0);
3884 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003885 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003886 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3887 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003888
Chris Lattner233f52b2010-03-09 23:52:58 +00003889 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003890 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003891 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003892
Devang Patel53bb5c92009-11-10 23:06:00 +00003893 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003894 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003895 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003896 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003897
Chris Lattner3f53c832010-04-04 18:52:31 +00003898 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003899 Asm->OutStreamer.EmitSymbolValue(LI->first,
3900 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003901 }
3902 }
3903
Chris Lattnerc0215722010-04-04 19:25:43 +00003904 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003905}