blob: 69cdafc1b10fe4f0ed39e1493e16a2b144092d36 [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
Devang Patel9a31f0f2010-10-25 20:45:32 +000056STATISTIC(BlocksWithoutLineNo, "Number of blocks without any line number");
57
Bill Wendling5f017e82010-04-07 09:28:04 +000058namespace {
59 const char *DWARFGroupName = "DWARF Emission";
60 const char *DbgTimerName = "DWARF Debug Writer";
61} // end anonymous namespace
62
Bill Wendling0310d762009-05-15 09:23:25 +000063//===----------------------------------------------------------------------===//
64
65/// Configuration values for initial hash set sizes (log2).
66///
Bill Wendling0310d762009-05-15 09:23:25 +000067static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000068
69namespace llvm {
70
71//===----------------------------------------------------------------------===//
72/// CompileUnit - This dwarf writer support class manages information associate
73/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000074class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000075 /// ID - File identifier for source.
76 ///
77 unsigned ID;
78
79 /// Die - Compile unit debug information entry.
80 ///
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000081 const OwningPtr<DIE> CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000082
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +000083 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel6f01d9c2009-11-21 00:31:03 +000084 DIE *IndexTyDie;
85
Devang Patel869aa462010-07-07 20:49:57 +000086 /// MDNodeToDieMap - Tracks the mapping of unit level debug informaton
Bill Wendling0310d762009-05-15 09:23:25 +000087 /// variables to debug information entries.
Devang Patel869aa462010-07-07 20:49:57 +000088 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000089
Devang Patel869aa462010-07-07 20:49:57 +000090 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug informaton
Bill Wendling0310d762009-05-15 09:23:25 +000091 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel869aa462010-07-07 20:49:57 +000092 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000093
94 /// Globals - A map of globally visible named entities for this unit.
95 ///
96 StringMap<DIE*> Globals;
97
Devang Patel193f7202009-11-24 01:14:22 +000098 /// GlobalTypes - A map of globally visible types for this unit.
99 ///
100 StringMap<DIE*> GlobalTypes;
101
Bill Wendling0310d762009-05-15 09:23:25 +0000102public:
103 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000104 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000105
106 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +0000107 unsigned getID() const { return ID; }
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +0000108 DIE* getCUDie() const { return CUDie.get(); }
Devang Patel193f7202009-11-24 01:14:22 +0000109 const StringMap<DIE*> &getGlobals() const { return Globals; }
110 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +0000111
112 /// hasContent - Return true if this compile unit has something to write out.
113 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000114 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000115
Devang Patel2c4ceb12009-11-21 02:48:08 +0000116 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000117 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000118 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +0000119
Devang Patel193f7202009-11-24 01:14:22 +0000120 /// addGlobalType - Add a new global type to the compile unit.
121 ///
Jim Grosbach1e20b962010-07-21 21:21:52 +0000122 void addGlobalType(StringRef Name, DIE *Die) {
123 GlobalTypes[Name] = Die;
Devang Patel193f7202009-11-24 01:14:22 +0000124 }
125
Devang Patel017d1212009-11-20 21:37:22 +0000126 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000127 /// specified debug variable.
Devang Patel869aa462010-07-07 20:49:57 +0000128 DIE *getDIE(const MDNode *N) { return MDNodeToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000129
Devang Patel017d1212009-11-20 21:37:22 +0000130 /// insertDIE - Insert DIE into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000131 void insertDIE(const MDNode *N, DIE *D) {
Devang Patel869aa462010-07-07 20:49:57 +0000132 MDNodeToDieMap.insert(std::make_pair(N, D));
Devang Patel017d1212009-11-20 21:37:22 +0000133 }
Bill Wendling0310d762009-05-15 09:23:25 +0000134
Devang Patel017d1212009-11-20 21:37:22 +0000135 /// getDIEEntry - Returns the debug information entry for the speciefied
136 /// debug variable.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000137 DIEEntry *getDIEEntry(const MDNode *N) {
138 DenseMap<const MDNode *, DIEEntry *>::iterator I =
139 MDNodeToDIEEntryMap.find(N);
Devang Patel869aa462010-07-07 20:49:57 +0000140 if (I == MDNodeToDIEEntryMap.end())
Devang Patel6404e4e2009-12-15 19:16:48 +0000141 return NULL;
142 return I->second;
143 }
Devang Patel017d1212009-11-20 21:37:22 +0000144
145 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000146 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Patel869aa462010-07-07 20:49:57 +0000147 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000148 }
149
Devang Patel2c4ceb12009-11-21 02:48:08 +0000150 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000151 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000152 void addDie(DIE *Buffer) {
153 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000154 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000155
156 // getIndexTyDie - Get an anonymous type for index type.
157 DIE *getIndexTyDie() {
158 return IndexTyDie;
159 }
160
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000161 // setIndexTyDie - Set D as anonymous type for index which can be reused
162 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000163 void setIndexTyDie(DIE *D) {
164 IndexTyDie = D;
165 }
166
Bill Wendling0310d762009-05-15 09:23:25 +0000167};
168
169//===----------------------------------------------------------------------===//
170/// DbgVariable - This class is used to track local variable information.
171///
Devang Patelf76a3d62009-11-16 21:53:40 +0000172class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000173 DIVariable Var; // Variable Descriptor.
Devang Patelc3f5f782010-05-25 23:40:22 +0000174 DIE *TheDIE; // Variable DIE.
175 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
Bill Wendling0310d762009-05-15 09:23:25 +0000176public:
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000177 // AbsVar may be NULL.
Devang Patelc3f5f782010-05-25 23:40:22 +0000178 DbgVariable(DIVariable V) : Var(V), TheDIE(0), DotDebugLocOffset(~0U) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000179
180 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000181 DIVariable getVariable() const { return Var; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000182 void setDIE(DIE *D) { TheDIE = D; }
183 DIE *getDIE() const { return TheDIE; }
Devang Patelc3f5f782010-05-25 23:40:22 +0000184 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
185 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
Devang Patel8bd11de2010-08-09 21:01:39 +0000186 StringRef getName() const { return Var.getName(); }
187 unsigned getTag() const { return Var.getTag(); }
188 bool variableHasComplexAddress() const {
189 assert(Var.Verify() && "Invalid complex DbgVariable!");
190 return Var.hasComplexAddress();
191 }
192 bool isBlockByrefVariable() const {
193 assert(Var.Verify() && "Invalid complex DbgVariable!");
194 return Var.isBlockByrefVariable();
195 }
196 unsigned getNumAddrElements() const {
197 assert(Var.Verify() && "Invalid complex DbgVariable!");
198 return Var.getNumAddrElements();
199 }
200 uint64_t getAddrElement(unsigned i) const {
201 return Var.getAddrElement(i);
202 }
203 DIType getType() const {
204 DIType Ty = Var.getType();
205 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
206 // addresses instead.
207 if (Var.isBlockByrefVariable()) {
208 /* Byref variables, in Blocks, are declared by the programmer as
209 "SomeType VarName;", but the compiler creates a
210 __Block_byref_x_VarName struct, and gives the variable VarName
211 either the struct, or a pointer to the struct, as its type. This
212 is necessary for various behind-the-scenes things the compiler
213 needs to do with by-reference variables in blocks.
214
215 However, as far as the original *programmer* is concerned, the
216 variable should still have type 'SomeType', as originally declared.
217
218 The following function dives into the __Block_byref_x_VarName
219 struct to find the original type of the variable. This will be
220 passed back to the code generating the type for the Debug
221 Information Entry for the variable 'VarName'. 'VarName' will then
222 have the original type 'SomeType' in its debug information.
223
224 The original type 'SomeType' will be the type of the field named
225 'VarName' inside the __Block_byref_x_VarName struct.
226
227 NOTE: In order for this to not completely fail on the debugger
228 side, the Debug Information Entry for the variable VarName needs to
229 have a DW_AT_location that tells the debugger how to unwind through
230 the pointers and __Block_byref_x_VarName struct to find the actual
231 value of the variable. The function addBlockByrefType does this. */
232 DIType subType = Ty;
233 unsigned tag = Ty.getTag();
234
235 if (tag == dwarf::DW_TAG_pointer_type) {
236 DIDerivedType DTy = DIDerivedType(Ty);
237 subType = DTy.getTypeDerivedFrom();
238 }
239
240 DICompositeType blockStruct = DICompositeType(subType);
241 DIArray Elements = blockStruct.getTypeArray();
242
243 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
244 DIDescriptor Element = Elements.getElement(i);
245 DIDerivedType DT = DIDerivedType(Element);
246 if (getName() == DT.getName())
247 return (DT.getTypeDerivedFrom());
248 }
249 return Ty;
250 }
251 return Ty;
252 }
Bill Wendling0310d762009-05-15 09:23:25 +0000253};
254
255//===----------------------------------------------------------------------===//
Devang Pateleac9c072010-04-27 19:46:33 +0000256/// DbgRange - This is used to track range of instructions with identical
257/// debug info scope.
258///
259typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
260
261//===----------------------------------------------------------------------===//
Bill Wendling0310d762009-05-15 09:23:25 +0000262/// DbgScope - This class is used to track scope information.
263///
Devang Patelf76a3d62009-11-16 21:53:40 +0000264class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000265 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000266 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000267 // Location at which this scope is inlined.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000268 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000269 bool AbstractScope; // Abstract Scope
Devang Pateld38dd112009-10-01 18:25:23 +0000270 const MachineInstr *LastInsn; // Last instruction of this scope.
271 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Pateleac9c072010-04-27 19:46:33 +0000272 unsigned DFSIn, DFSOut;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000273 // Scopes defined in scope. Contents not owned.
274 SmallVector<DbgScope *, 4> Scopes;
275 // Variables declared in scope. Contents owned.
276 SmallVector<DbgVariable *, 8> Variables;
Devang Pateleac9c072010-04-27 19:46:33 +0000277 SmallVector<DbgRange, 4> Ranges;
Owen Anderson04c05f72009-06-24 22:53:20 +0000278 // Private state for dump()
279 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000280public:
Devang Patele9f8f5e2010-05-07 20:54:48 +0000281 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000282 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Pateleac9c072010-04-27 19:46:33 +0000283 LastInsn(0), FirstInsn(0),
284 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000285 virtual ~DbgScope();
286
287 // Accessors.
288 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000289 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000290 DIDescriptor getDesc() const { return Desc; }
Devang Patele9f8f5e2010-05-07 20:54:48 +0000291 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
292 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000293 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
Devang Patele03161c2010-08-09 18:51:29 +0000294 const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
Devang Pateleac9c072010-04-27 19:46:33 +0000295 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
296
297 /// openInsnRange - This scope covers instruction range starting from MI.
298 void openInsnRange(const MachineInstr *MI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000299 if (!FirstInsn)
Devang Pateleac9c072010-04-27 19:46:33 +0000300 FirstInsn = MI;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000301
Devang Pateleac9c072010-04-27 19:46:33 +0000302 if (Parent)
303 Parent->openInsnRange(MI);
304 }
305
Jim Grosbach1e20b962010-07-21 21:21:52 +0000306 /// extendInsnRange - Extend the current instruction range covered by
Devang Pateleac9c072010-04-27 19:46:33 +0000307 /// this scope.
308 void extendInsnRange(const MachineInstr *MI) {
309 assert (FirstInsn && "MI Range is not open!");
310 LastInsn = MI;
311 if (Parent)
312 Parent->extendInsnRange(MI);
313 }
314
315 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
316 /// until now. This is used when a new scope is encountered while walking
317 /// machine instructions.
318 void closeInsnRange(DbgScope *NewScope = NULL) {
319 assert (LastInsn && "Last insn missing!");
320 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000321 FirstInsn = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +0000322 LastInsn = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000323 // If Parent dominates NewScope then do not close Parent's instruction
Devang Pateleac9c072010-04-27 19:46:33 +0000324 // range.
325 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
326 Parent->closeInsnRange(NewScope);
327 }
328
Devang Patel53bb5c92009-11-10 23:06:00 +0000329 void setAbstractScope() { AbstractScope = true; }
330 bool isAbstractScope() const { return AbstractScope; }
Devang Pateleac9c072010-04-27 19:46:33 +0000331
332 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
333 unsigned getDFSOut() const { return DFSOut; }
334 void setDFSOut(unsigned O) { DFSOut = O; }
335 unsigned getDFSIn() const { return DFSIn; }
336 void setDFSIn(unsigned I) { DFSIn = I; }
337 bool dominates(const DbgScope *S) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000338 if (S == this)
Devang Pateleac9c072010-04-27 19:46:33 +0000339 return true;
340 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
341 return true;
342 return false;
343 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000344
Devang Patel2c4ceb12009-11-21 02:48:08 +0000345 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000346 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000347 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000348
Devang Patel2c4ceb12009-11-21 02:48:08 +0000349 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000350 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000351 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000352
Bill Wendling0310d762009-05-15 09:23:25 +0000353#ifndef NDEBUG
354 void dump() const;
355#endif
356};
Devang Pateleac9c072010-04-27 19:46:33 +0000357
Chris Lattnerea761862010-04-05 04:09:20 +0000358} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000359
360#ifndef NDEBUG
361void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000362 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000363 err.indent(IndentLevel);
Devang Patele9f8f5e2010-05-07 20:54:48 +0000364 const MDNode *N = Desc;
Devang Patel53bb5c92009-11-10 23:06:00 +0000365 N->dump();
Devang Patel53bb5c92009-11-10 23:06:00 +0000366 if (AbstractScope)
367 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000368
369 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000370 if (!Scopes.empty())
371 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000372 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
373 if (Scopes[i] != this)
374 Scopes[i]->dump();
375
376 IndentLevel -= 2;
377}
378#endif
379
Bill Wendling0310d762009-05-15 09:23:25 +0000380DbgScope::~DbgScope() {
Bill Wendling0310d762009-05-15 09:23:25 +0000381 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
382 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000383}
384
Chris Lattner49cd6642010-04-05 05:11:15 +0000385DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000386 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000387 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelf2548ca2010-04-16 23:33:45 +0000388 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000389 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000390
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000391 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
392 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000393 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000394 FunctionBeginSym = FunctionEndSym = 0;
Devang Patelca76f6f2010-07-08 20:10:35 +0000395 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Dan Gohman03c3dc72010-06-18 15:56:31 +0000396 {
397 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
398 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000399 }
Bill Wendling0310d762009-05-15 09:23:25 +0000400}
401DwarfDebug::~DwarfDebug() {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000402 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
403 DIEBlocks[j]->~DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000404}
405
Chris Lattnerbc733f52010-03-13 02:17:42 +0000406MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
407 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
408 if (Entry.first) return Entry.first;
409
410 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000411 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000412}
413
414
Devang Patel2c4ceb12009-11-21 02:48:08 +0000415/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000416///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000417void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000418 // Profile the node so that we can make it unique.
419 FoldingSetNodeID ID;
420 Abbrev.Profile(ID);
421
422 // Check the set for priors.
423 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
424
425 // If it's newly added.
426 if (InSet == &Abbrev) {
427 // Add to abbreviation list.
428 Abbreviations.push_back(&Abbrev);
429
430 // Assign the vector position + 1 as its number.
431 Abbrev.setNumber(Abbreviations.size());
432 } else {
433 // Assign existing abbreviation number.
434 Abbrev.setNumber(InSet->getNumber());
435 }
436}
437
Devang Patel2c4ceb12009-11-21 02:48:08 +0000438/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000439/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000440DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000441 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000442 return Value;
443}
444
Devang Patel2c4ceb12009-11-21 02:48:08 +0000445/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000446///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000447void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000448 unsigned Form, uint64_t Integer) {
449 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000450 DIEValue *Value = Integer == 1 ?
Devang Patelca76f6f2010-07-08 20:10:35 +0000451 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000452 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000453}
454
Devang Patel2c4ceb12009-11-21 02:48:08 +0000455/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000456///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000457void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000458 unsigned Form, int64_t Integer) {
459 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000460 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000461 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000462}
463
Devang Patel69f57b12009-12-02 15:25:16 +0000464/// addString - Add a string attribute data and value. DIEString only
Jim Grosbach1e20b962010-07-21 21:21:52 +0000465/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000466void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner4cf202b2010-01-23 03:11:46 +0000467 StringRef String) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000468 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000469 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000470}
471
Devang Patel2c4ceb12009-11-21 02:48:08 +0000472/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000473///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000474void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000475 const MCSymbol *Label) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000476 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000477 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000478}
479
Devang Patel2c4ceb12009-11-21 02:48:08 +0000480/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000481///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000482void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000483 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000484 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000485 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000486}
487
Chris Lattner74e41f92010-04-05 05:24:55 +0000488/// addDIEEntry - Add a DIE attribute data and value.
489///
490void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
491 DIE *Entry) {
492 Die->addValue(Attribute, Form, createDIEEntry(Entry));
493}
494
495
Devang Patel2c4ceb12009-11-21 02:48:08 +0000496/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000497///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000498void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000499 DIEBlock *Block) {
Chris Lattnera37d5382010-04-05 00:18:22 +0000500 Block->ComputeSize(Asm);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000501 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000502 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000503}
504
Devang Patel2c4ceb12009-11-21 02:48:08 +0000505/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000506/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000507void DwarfDebug::addSourceLine(DIE *Die, DIVariable V) {
Devang Patelc665a512010-05-07 23:33:41 +0000508 // Verify variable.
Devang Patel81625742010-08-09 20:20:05 +0000509 if (!V.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000510 return;
511
Devang Patel81625742010-08-09 20:20:05 +0000512 unsigned Line = V.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000513 if (Line == 0)
514 return;
Devang Patel81625742010-08-09 20:20:05 +0000515 unsigned FileID = GetOrCreateSourceID(V.getContext().getDirectory(),
516 V.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000517 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000518 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
519 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000520}
521
Devang Patel2c4ceb12009-11-21 02:48:08 +0000522/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000523/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000524void DwarfDebug::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patelc665a512010-05-07 23:33:41 +0000525 // Verify global variable.
Devang Patel81625742010-08-09 20:20:05 +0000526 if (!G.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000527 return;
528
Devang Patel81625742010-08-09 20:20:05 +0000529 unsigned Line = G.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000530 if (Line == 0)
531 return;
Devang Patel81625742010-08-09 20:20:05 +0000532 unsigned FileID = GetOrCreateSourceID(G.getContext().getDirectory(),
533 G.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000534 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000535 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
536 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000537}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000538
Devang Patel2c4ceb12009-11-21 02:48:08 +0000539/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000540/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000541void DwarfDebug::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patelc665a512010-05-07 23:33:41 +0000542 // Verify subprogram.
Devang Patel81625742010-08-09 20:20:05 +0000543 if (!SP.Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000544 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000545 // If the line number is 0, don't add it.
Devang Patel81625742010-08-09 20:20:05 +0000546 if (SP.getLineNumber() == 0)
Caroline Ticec6f9d622009-09-11 18:25:54 +0000547 return;
548
Devang Patel81625742010-08-09 20:20:05 +0000549 unsigned Line = SP.getLineNumber();
550 if (!SP.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000551 return;
Devang Patel81625742010-08-09 20:20:05 +0000552 unsigned FileID = GetOrCreateSourceID(SP.getDirectory(),
553 SP.getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000554 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000555 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
556 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000557}
558
Devang Patel2c4ceb12009-11-21 02:48:08 +0000559/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000560/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000561void DwarfDebug::addSourceLine(DIE *Die, DIType Ty) {
Devang Patelc665a512010-05-07 23:33:41 +0000562 // Verify type.
Devang Patel81625742010-08-09 20:20:05 +0000563 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000564 return;
565
Devang Patel81625742010-08-09 20:20:05 +0000566 unsigned Line = Ty.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000567 if (Line == 0 || !Ty.getContext().Verify())
Devang Patel77bf2952010-03-08 22:02:50 +0000568 return;
Devang Patel81625742010-08-09 20:20:05 +0000569 unsigned FileID = GetOrCreateSourceID(Ty.getContext().getDirectory(),
570 Ty.getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000571 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000572 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
573 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000574}
575
Devang Patel6404e4e2009-12-15 19:16:48 +0000576/// addSourceLine - Add location information to specified debug information
577/// entry.
Devang Patel85e95802010-08-10 04:09:06 +0000578void DwarfDebug::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patelc665a512010-05-07 23:33:41 +0000579 // Verify namespace.
Devang Patel81625742010-08-09 20:20:05 +0000580 if (!NS.Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000581 return;
582
Devang Patel81625742010-08-09 20:20:05 +0000583 unsigned Line = NS.getLineNumber();
Devang Patelb2bada32010-10-08 17:18:54 +0000584 if (Line == 0)
585 return;
Devang Patel81625742010-08-09 20:20:05 +0000586 StringRef FN = NS.getFilename();
587 StringRef Dir = NS.getDirectory();
Devang Patel6404e4e2009-12-15 19:16:48 +0000588
589 unsigned FileID = GetOrCreateSourceID(Dir, FN);
590 assert(FileID && "Invalid file id");
591 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
592 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
593}
594
Devang Patel9e3bd2c2010-08-31 06:11:28 +0000595/// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
596/// on provided frame index.
597void DwarfDebug::addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI) {
598 MachineLocation Location;
599 unsigned FrameReg;
600 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
601 int Offset = RI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
602 Location.set(FrameReg, Offset);
603
Devang Patel8bd11de2010-08-09 21:01:39 +0000604 if (DV->variableHasComplexAddress())
605 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
606 else if (DV->isBlockByrefVariable())
607 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
608 else
609 addAddress(Die, dwarf::DW_AT_location, Location);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000610}
611
Devang Patel2c4ceb12009-11-21 02:48:08 +0000612/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000613/// and generate the DWARF information necessary to find the actual variable
614/// given the extra address information encoded in the DIVariable, starting from
615/// the starting location. Add the DWARF information to the die.
616///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000617void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000618 unsigned Attribute,
619 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000620 DIType Ty = DV->getType();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000621
622 // Decode the original location, and use that as the start of the byref
623 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000624 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000625 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000626 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000627
628 if (Location.isReg()) {
629 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000630 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000631 } else {
632 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000633 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
634 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000635 }
636 } else {
637 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000638 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000639 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000640 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
641 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000642 }
643
Devang Patel2c4ceb12009-11-21 02:48:08 +0000644 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000645 }
646
Devang Patel8bd11de2010-08-09 21:01:39 +0000647 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
648 uint64_t Element = DV->getAddrElement(i);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000649
650 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000651 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel8bd11de2010-08-09 21:01:39 +0000652 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000653 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000654 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000655 } else llvm_unreachable("unknown DIFactory Opcode");
656 }
657
658 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000659 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000660}
661
Caroline Ticedc8f6042009-08-31 21:19:37 +0000662/* Byref variables, in Blocks, are declared by the programmer as "SomeType
663 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
664 gives the variable VarName either the struct, or a pointer to the struct, as
665 its type. This is necessary for various behind-the-scenes things the
666 compiler needs to do with by-reference variables in Blocks.
667
668 However, as far as the original *programmer* is concerned, the variable
669 should still have type 'SomeType', as originally declared.
670
Devang Patel2c4ceb12009-11-21 02:48:08 +0000671 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000672 struct to find the original type of the variable, which is then assigned to
673 the variable's Debug Information Entry as its real type. So far, so good.
674 However now the debugger will expect the variable VarName to have the type
675 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000676 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000677 pointers and struct to find the actual variable of type SomeType.
678
679 The following function does just that. We start by getting
680 the "normal" location for the variable. This will be the location
681 of either the struct __Block_byref_x_VarName or the pointer to the
682 struct __Block_byref_x_VarName.
683
684 The struct will look something like:
685
686 struct __Block_byref_x_VarName {
687 ... <various fields>
688 struct __Block_byref_x_VarName *forwarding;
689 ... <various other fields>
690 SomeType VarName;
691 ... <maybe more fields>
692 };
693
694 If we are given the struct directly (as our starting point) we
695 need to tell the debugger to:
696
697 1). Add the offset of the forwarding field.
698
Dan Gohmanf451cb82010-02-10 16:03:48 +0000699 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000700 struct to use (the real one may have been copied onto the heap).
701
702 3). Add the offset for the field VarName, to find the actual variable.
703
704 If we started with a pointer to the struct, then we need to
705 dereference that pointer first, before the other steps.
706 Translating this into DWARF ops, we will need to append the following
707 to the current location description for the variable:
708
709 DW_OP_deref -- optional, if we start with a pointer
710 DW_OP_plus_uconst <forward_fld_offset>
711 DW_OP_deref
712 DW_OP_plus_uconst <varName_fld_offset>
713
714 That is what this function does. */
715
Devang Patel2c4ceb12009-11-21 02:48:08 +0000716/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000717/// provided, and generate the DWARF information necessary to find the
718/// actual Block variable (navigating the Block struct) based on the
719/// starting location. Add the DWARF information to the die. For
720/// more information, read large comment just above here.
721///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000722void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000723 unsigned Attribute,
724 const MachineLocation &Location) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000725 DIType Ty = DV->getType();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000726 DIType TmpTy = Ty;
727 unsigned Tag = Ty.getTag();
728 bool isPointer = false;
729
Devang Patel8bd11de2010-08-09 21:01:39 +0000730 StringRef varName = DV->getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000731
732 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000733 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000734 TmpTy = DTy.getTypeDerivedFrom();
735 isPointer = true;
736 }
737
Devang Patel2db49d72010-05-07 18:11:54 +0000738 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000739
Daniel Dunbar00564992009-09-19 20:40:14 +0000740 // Find the __forwarding field and the variable field in the __Block_byref
741 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000742 DIArray Fields = blockStruct.getTypeArray();
743 DIDescriptor varField = DIDescriptor();
744 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000745
Daniel Dunbar00564992009-09-19 20:40:14 +0000746 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
747 DIDescriptor Element = Fields.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000748 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000749 StringRef fieldName = DT.getName();
750 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000751 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000752 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000753 varField = Element;
754 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000755
Daniel Dunbar00564992009-09-19 20:40:14 +0000756 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000757 unsigned forwardingFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000758 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000759 unsigned varFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000760 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000761
Mike Stump7e3720d2009-09-24 23:21:26 +0000762 // Decode the original location, and use that as the start of the byref
763 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000764 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000765 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000766 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000767
Daniel Dunbar00564992009-09-19 20:40:14 +0000768 if (Location.isReg()) {
769 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000770 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000771 else {
772 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000773 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
774 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000775 }
776 } else {
777 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000778 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000779 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000780 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
781 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000782 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000783
Devang Patel2c4ceb12009-11-21 02:48:08 +0000784 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000785 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000786
Mike Stump7e3720d2009-09-24 23:21:26 +0000787 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000788 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000789 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000790 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000791
Daniel Dunbar00564992009-09-19 20:40:14 +0000792 // Next add the offset for the '__forwarding' field:
793 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
794 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000795 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000796 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
797 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000798 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000799
Daniel Dunbar00564992009-09-19 20:40:14 +0000800 // Now dereference the __forwarding field to get to the real __Block_byref
801 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000802 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000803
Daniel Dunbar00564992009-09-19 20:40:14 +0000804 // Now that we've got the real __Block_byref... struct, add the offset
805 // for the variable's field to get to the location of the actual variable:
806 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000807 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000808 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
809 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000810 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000811
Daniel Dunbar00564992009-09-19 20:40:14 +0000812 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000813 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000814}
815
Devang Patel2c4ceb12009-11-21 02:48:08 +0000816/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000817/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000818void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000819 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000820 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000821 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000822 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelbe90c3a2010-09-22 21:10:38 +0000823 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
824
825 if (TRI->getFrameRegister(*Asm->MF) == Location.getReg()
826 && Location.getOffset()) {
827 // If variable offset is based in frame register then use fbreg.
828 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
829 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
830 addBlock(Die, Attribute, 0, Block);
831 return;
832 }
Bill Wendling0310d762009-05-15 09:23:25 +0000833
834 if (Location.isReg()) {
835 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000836 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000837 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000838 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
839 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000840 }
841 } else {
842 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000843 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000844 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000845 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
846 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000847 }
848
Devang Patel2c4ceb12009-11-21 02:48:08 +0000849 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000850 }
851
Devang Patel2c4ceb12009-11-21 02:48:08 +0000852 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000853}
854
Devang Patela43098d2010-04-28 01:03:09 +0000855/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patel26c1e562010-05-20 16:36:41 +0000856bool DwarfDebug::addRegisterAddress(DIE *Die, const MCSymbol *VS,
Devang Patela43098d2010-04-28 01:03:09 +0000857 const MachineOperand &MO) {
858 assert (MO.isReg() && "Invalid machine operand!");
859 if (!MO.getReg())
860 return false;
861 MachineLocation Location;
862 Location.set(MO.getReg());
863 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patel26c1e562010-05-20 16:36:41 +0000864 if (VS)
Devang Patela43098d2010-04-28 01:03:09 +0000865 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
866 return true;
867}
868
869/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel26c1e562010-05-20 16:36:41 +0000870bool DwarfDebug::addConstantValue(DIE *Die, const MCSymbol *VS,
Devang Patela43098d2010-04-28 01:03:09 +0000871 const MachineOperand &MO) {
872 assert (MO.isImm() && "Invalid machine operand!");
873 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
874 unsigned Imm = MO.getImm();
875 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
876 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel26c1e562010-05-20 16:36:41 +0000877 if (VS)
Devang Patela43098d2010-04-28 01:03:09 +0000878 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
879 return true;
880}
881
882/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patel26c1e562010-05-20 16:36:41 +0000883bool DwarfDebug::addConstantFPValue(DIE *Die, const MCSymbol *VS,
Devang Patela43098d2010-04-28 01:03:09 +0000884 const MachineOperand &MO) {
885 assert (MO.isFPImm() && "Invalid machine operand!");
886 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
887 APFloat FPImm = MO.getFPImm()->getValueAPF();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000888
Devang Patela43098d2010-04-28 01:03:09 +0000889 // Get the raw data form of the floating point.
890 const APInt FltVal = FPImm.bitcastToAPInt();
891 const char *FltPtr = (const char*)FltVal.getRawData();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000892
Devang Patela43098d2010-04-28 01:03:09 +0000893 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
894 bool LittleEndian = Asm->getTargetData().isLittleEndian();
895 int Incr = (LittleEndian ? 1 : -1);
896 int Start = (LittleEndian ? 0 : NumBytes - 1);
897 int Stop = (LittleEndian ? NumBytes : -1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000898
Devang Patela43098d2010-04-28 01:03:09 +0000899 // Output the constant to DWARF one byte at a time.
900 for (; Start != Stop; Start += Incr)
901 addUInt(Block, 0, dwarf::DW_FORM_data1,
902 (unsigned char)0xFF & FltPtr[Start]);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000903
Devang Patela43098d2010-04-28 01:03:09 +0000904 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel26c1e562010-05-20 16:36:41 +0000905 if (VS)
906 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000907 return true;
Devang Patela43098d2010-04-28 01:03:09 +0000908}
909
910
Devang Patelc366f832009-12-10 19:14:49 +0000911/// addToContextOwner - Add Die into the list of its context owner's children.
912void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000913 if (Context.isType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000914 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patelc366f832009-12-10 19:14:49 +0000915 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000916 } else if (Context.isNameSpace()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000917 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel6404e4e2009-12-15 19:16:48 +0000918 ContextDIE->addChild(Die);
Stuart Hastings215aa152010-06-11 20:08:44 +0000919 } else if (Context.isSubprogram()) {
Devang Patelee70fa72010-09-27 23:15:27 +0000920 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context));
Stuart Hastings215aa152010-06-11 20:08:44 +0000921 ContextDIE->addChild(Die);
Devang Patel163a9f72010-05-10 22:49:55 +0000922 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patelc366f832009-12-10 19:14:49 +0000923 ContextDIE->addChild(Die);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000924 else
Devang Patel163a9f72010-05-10 22:49:55 +0000925 getCompileUnit(Context)->addDie(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000926}
927
Devang Patel16ced732009-12-10 18:05:33 +0000928/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
929/// given DIType.
930DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel163a9f72010-05-10 22:49:55 +0000931 CompileUnit *TypeCU = getCompileUnit(Ty);
932 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel16ced732009-12-10 18:05:33 +0000933 if (TyDIE)
934 return TyDIE;
935
936 // Create new type.
937 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel163a9f72010-05-10 22:49:55 +0000938 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel16ced732009-12-10 18:05:33 +0000939 if (Ty.isBasicType())
Devang Patel2db49d72010-05-07 18:11:54 +0000940 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000941 else if (Ty.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000942 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000943 else {
944 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2db49d72010-05-07 18:11:54 +0000945 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000946 }
947
Devang Patelc366f832009-12-10 19:14:49 +0000948 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000949 return TyDIE;
950}
951
Devang Patel2c4ceb12009-11-21 02:48:08 +0000952/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000953void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9c004872010-05-07 21:45:47 +0000954 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000955 return;
956
957 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000958 CompileUnit *TypeCU = getCompileUnit(Ty);
959 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000960 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000961 if (Entry) {
962 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000963 return;
964 }
965
Bill Wendling0310d762009-05-15 09:23:25 +0000966 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000967 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000968
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000969 // Set up proxy.
970 Entry = createDIEEntry(Buffer);
Devang Patel163a9f72010-05-10 22:49:55 +0000971 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000972
Devang Patel2c4ceb12009-11-21 02:48:08 +0000973 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000974}
975
Devang Patel2c4ceb12009-11-21 02:48:08 +0000976/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000977void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000978 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000979 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000980 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000981 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000982 BTy.getEncoding());
983
984 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000985 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000986 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000987 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000988 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000989}
990
Devang Patel2c4ceb12009-11-21 02:48:08 +0000991/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000992void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000993 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000994 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000995 uint64_t Size = DTy.getSizeInBits() >> 3;
996 unsigned Tag = DTy.getTag();
997
998 // FIXME - Workaround for templates.
999 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
1000
1001 Buffer.setTag(Tag);
1002
1003 // Map to main type, void will not have a type.
1004 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +00001005 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001006
1007 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +00001008 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001009 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001010
1011 // Add size if non-zero (derived types might be zero-sized.)
1012 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001013 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001014
1015 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +00001016 if (!DTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001017 addSourceLine(&Buffer, DTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001018}
1019
Devang Patel2c4ceb12009-11-21 02:48:08 +00001020/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001021void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001022 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +00001023 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001024
1025 uint64_t Size = CTy.getSizeInBits() >> 3;
1026 unsigned Tag = CTy.getTag();
1027 Buffer.setTag(Tag);
1028
1029 switch (Tag) {
1030 case dwarf::DW_TAG_vector_type:
1031 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +00001032 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001033 break;
1034 case dwarf::DW_TAG_enumeration_type: {
1035 DIArray Elements = CTy.getTypeArray();
1036
1037 // Add enumerators to enumeration type.
1038 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1039 DIE *ElemDie = NULL;
Devang Patel2db49d72010-05-07 18:11:54 +00001040 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3c91b052010-03-08 20:52:55 +00001041 if (Enum.isEnumerator()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001042 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001043 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +00001044 }
Bill Wendling0310d762009-05-15 09:23:25 +00001045 }
1046 }
1047 break;
1048 case dwarf::DW_TAG_subroutine_type: {
1049 // Add return type.
1050 DIArray Elements = CTy.getTypeArray();
1051 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2db49d72010-05-07 18:11:54 +00001052 addType(&Buffer, DIType(RTy));
Bill Wendling0310d762009-05-15 09:23:25 +00001053
Devang Pateld6747df2010-10-06 20:50:40 +00001054 bool isPrototyped = true;
Bill Wendling0310d762009-05-15 09:23:25 +00001055 // Add arguments.
1056 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001057 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld6747df2010-10-06 20:50:40 +00001058 if (Ty.isUnspecifiedParameter()) {
1059 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
1060 Buffer.addChild(Arg);
1061 isPrototyped = false;
1062 } else {
1063 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1064 addType(Arg, DIType(Ty));
1065 Buffer.addChild(Arg);
1066 }
Bill Wendling0310d762009-05-15 09:23:25 +00001067 }
Devang Pateld6747df2010-10-06 20:50:40 +00001068 // Add prototype flag.
1069 if (isPrototyped)
1070 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001071 }
1072 break;
1073 case dwarf::DW_TAG_structure_type:
1074 case dwarf::DW_TAG_union_type:
1075 case dwarf::DW_TAG_class_type: {
1076 // Add elements to structure type.
1077 DIArray Elements = CTy.getTypeArray();
1078
1079 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +00001080 unsigned N = Elements.getNumElements();
1081 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +00001082 break;
1083
1084 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +00001085 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001086 DIDescriptor Element = Elements.getElement(i);
1087 DIE *ElemDie = NULL;
Devang Patel1a301232010-09-29 21:44:16 +00001088 if (Element.isSubprogram()) {
1089 DISubprogram SP(Element);
Devang Patel2db49d72010-05-07 18:11:54 +00001090 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel1a301232010-09-29 21:44:16 +00001091 if (SP.isProtected())
1092 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1093 dwarf::DW_ACCESS_protected);
1094 else if (SP.isPrivate())
1095 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1096 dwarf::DW_ACCESS_private);
1097 else
1098 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1099 dwarf::DW_ACCESS_public);
Devang Patel21ea1d52010-10-01 23:31:40 +00001100 if (SP.isExplicit())
1101 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
Devang Patel1a301232010-09-29 21:44:16 +00001102 }
Devang Patel3c91b052010-03-08 20:52:55 +00001103 else if (Element.isVariable()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001104 DIVariable DV(Element);
Devang Patel1ee0cb92010-01-30 01:08:30 +00001105 ElemDie = new DIE(dwarf::DW_TAG_variable);
1106 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1107 DV.getName());
1108 addType(ElemDie, DV.getType());
1109 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1110 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel81625742010-08-09 20:20:05 +00001111 addSourceLine(ElemDie, DV);
Devang Patel3c91b052010-03-08 20:52:55 +00001112 } else if (Element.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001113 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001114 else
1115 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001116 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001117 }
1118
Devang Patela1ba2692009-08-27 23:51:51 +00001119 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001120 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001121
1122 unsigned RLang = CTy.getRunTimeLang();
1123 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001124 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001125 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001126
1127 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel2db49d72010-05-07 18:11:54 +00001128 if (DIDescriptor(ContainingType).isCompositeType())
Jim Grosbach1e20b962010-07-21 21:21:52 +00001129 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel2db49d72010-05-07 18:11:54 +00001130 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastings215aa152010-06-11 20:08:44 +00001131 else {
1132 DIDescriptor Context = CTy.getContext();
1133 addToContextOwner(&Buffer, Context);
1134 }
Bill Wendling0310d762009-05-15 09:23:25 +00001135 break;
1136 }
1137 default:
1138 break;
1139 }
1140
1141 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001142 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001143 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001144
Jim Grosbach1e20b962010-07-21 21:21:52 +00001145 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
Devang Patel61409622010-07-07 20:12:52 +00001146 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1147 {
Bill Wendling0310d762009-05-15 09:23:25 +00001148 // Add size if non-zero (derived types might be zero-sized.)
1149 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001150 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001151 else {
1152 // Add zero size if it is not a forward declaration.
1153 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001154 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001155 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001156 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001157 }
1158
1159 // Add source line info if available.
1160 if (!CTy.isForwardDecl())
Devang Patel81625742010-08-09 20:20:05 +00001161 addSourceLine(&Buffer, CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001162 }
1163}
1164
Devang Patel2c4ceb12009-11-21 02:48:08 +00001165/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1166void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001167 int64_t L = SR.getLo();
1168 int64_t H = SR.getHi();
1169 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1170
Devang Patel2c4ceb12009-11-21 02:48:08 +00001171 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001172 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001173 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001174 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001175
Devang Patel2c4ceb12009-11-21 02:48:08 +00001176 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001177}
1178
Devang Patel2c4ceb12009-11-21 02:48:08 +00001179/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001180void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001181 DICompositeType *CTy) {
1182 Buffer.setTag(dwarf::DW_TAG_array_type);
1183 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001184 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001185
1186 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001187 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001188 DIArray Elements = CTy->getTypeArray();
1189
Devang Patel6f01d9c2009-11-21 00:31:03 +00001190 // Get an anonymous type for index type.
Devang Patel163a9f72010-05-10 22:49:55 +00001191 CompileUnit *TheCU = getCompileUnit(*CTy);
1192 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001193 if (!IdxTy) {
1194 // Construct an anonymous type for index type.
1195 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001196 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1197 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001198 dwarf::DW_ATE_signed);
Devang Patel163a9f72010-05-10 22:49:55 +00001199 TheCU->addDie(IdxTy);
1200 TheCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001201 }
Bill Wendling0310d762009-05-15 09:23:25 +00001202
1203 // Add subranges to array type.
1204 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1205 DIDescriptor Element = Elements.getElement(i);
1206 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2db49d72010-05-07 18:11:54 +00001207 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001208 }
1209}
1210
Devang Patel2c4ceb12009-11-21 02:48:08 +00001211/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001212DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001213 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001214 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001215 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001216 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001217 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001218 return Enumerator;
1219}
1220
Jim Grosbach1e20b962010-07-21 21:21:52 +00001221/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +00001222/// printer to not emit usual symbol prefix before the symbol name is used then
1223/// return linkage name after skipping this special LLVM prefix.
1224static StringRef getRealLinkageName(StringRef LinkageName) {
1225 char One = '\1';
1226 if (LinkageName.startswith(StringRef(&One, 1)))
1227 return LinkageName.substr(1);
1228 return LinkageName;
1229}
1230
Devang Patel2c4ceb12009-11-21 02:48:08 +00001231/// createMemberDIE - Create new member DIE.
Devang Patelecbd8e82010-08-10 04:12:17 +00001232DIE *DwarfDebug::createMemberDIE(DIDerivedType DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001233 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001234 StringRef Name = DT.getName();
1235 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001236 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001237
Devang Patel8a241142009-12-09 18:24:21 +00001238 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001239
Devang Patel81625742010-08-09 20:20:05 +00001240 addSourceLine(MemberDie, DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001241
Benjamin Kramer345ef342010-03-31 19:34:01 +00001242 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001243 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001244
Bill Wendling0310d762009-05-15 09:23:25 +00001245 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001246 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001247
1248 if (Size != FieldSize) {
1249 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001250 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1251 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001252
1253 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001254 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1255 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001256 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001257 Offset -= FieldOffset;
1258
1259 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001260 if (Asm->getTargetData().isLittleEndian())
1261 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001262 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001263
Devang Patel33db5082009-11-04 22:06:12 +00001264 // Here WD_AT_data_member_location points to the anonymous
1265 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001266 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001267
1268 } else
1269 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001270 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001271
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001272 if (DT.getTag() == dwarf::DW_TAG_inheritance
1273 && DT.isVirtual()) {
1274
1275 // For C++, virtual base classes are not at fixed offset. Use following
1276 // expression to extract appropriate offset from vtable.
1277 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1278
Benjamin Kramer345ef342010-03-31 19:34:01 +00001279 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001280 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1281 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1282 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1283 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1284 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1285 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1286 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1287
Jim Grosbach1e20b962010-07-21 21:21:52 +00001288 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001289 VBaseLocationDie);
1290 } else
1291 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001292
1293 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001294 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001295 dwarf::DW_ACCESS_protected);
1296 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001297 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001298 dwarf::DW_ACCESS_private);
Devang Patel2a361602010-09-29 19:08:08 +00001299 // Otherwise C++ member and base classes are considered public.
1300 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
Devang Patel5d11eb02009-12-03 19:11:07 +00001301 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1302 dwarf::DW_ACCESS_public);
1303 if (DT.isVirtual())
1304 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1305 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001306 return MemberDie;
1307}
1308
Devang Patelffe966c2009-12-14 16:18:45 +00001309/// createSubprogramDIE - Create new DIE using SP.
Devang Patelee70fa72010-09-27 23:15:27 +00001310DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel163a9f72010-05-10 22:49:55 +00001311 CompileUnit *SPCU = getCompileUnit(SP);
1312 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +00001313 if (SPDie)
1314 return SPDie;
1315
1316 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001317 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001318 if (!SP.getName().empty())
1319 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001320
Devang Patel65dbc902009-11-25 17:36:49 +00001321 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001322 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001323 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001324 getRealLinkageName(LinkageName));
1325
Devang Patel81625742010-08-09 20:20:05 +00001326 addSourceLine(SPDie, SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001327
Devang Patel7b172c62010-10-07 22:03:01 +00001328 if (SP.isPrototyped())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001329 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001330
1331 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001332 DICompositeType SPTy = SP.getType();
1333 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001334 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001335
Devang Patel3c91b052010-03-08 20:52:55 +00001336 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001337 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001338 else
Devang Patel2db49d72010-05-07 18:11:54 +00001339 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001340
Devang Patel5d11eb02009-12-03 19:11:07 +00001341 unsigned VK = SP.getVirtuality();
1342 if (VK) {
1343 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001344 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001345 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1346 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1347 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001348 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +00001349 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001350 }
1351
Devang Patelee70fa72010-09-27 23:15:27 +00001352 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001353 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001354
1355 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001356 // be handled while processing variables.
1357 DICompositeType SPTy = SP.getType();
1358 DIArray Args = SPTy.getTypeArray();
1359 unsigned SPTag = SPTy.getTag();
1360
Bill Wendling0310d762009-05-15 09:23:25 +00001361 if (SPTag == dwarf::DW_TAG_subroutine_type)
1362 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1363 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001364 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelb4645642010-02-06 01:02:37 +00001365 addType(Arg, ATy);
1366 if (ATy.isArtificial())
1367 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001368 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001369 }
1370 }
1371
Devang Patel4e0d19d2010-02-03 19:57:19 +00001372 if (SP.isArtificial())
1373 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1374
Devang Patelccff8122010-04-30 19:38:23 +00001375 if (!SP.isLocalToUnit())
1376 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001377
Devang Patelccff8122010-04-30 19:38:23 +00001378 if (SP.isOptimized())
1379 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1380
Jim Grosbach91729002010-07-21 23:03:52 +00001381 if (unsigned isa = Asm->getISAEncoding()) {
1382 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1383 }
1384
Devang Patelccff8122010-04-30 19:38:23 +00001385 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel163a9f72010-05-10 22:49:55 +00001386 SPCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +00001387
Stuart Hastings215aa152010-06-11 20:08:44 +00001388 // Add to context owner.
1389 addToContextOwner(SPDie, SP.getContext());
1390
Bill Wendling0310d762009-05-15 09:23:25 +00001391 return SPDie;
1392}
1393
Devang Patele9f8f5e2010-05-07 20:54:48 +00001394DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001395 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001396
1397 DbgScope *AScope = AbstractScopes.lookup(N);
1398 if (AScope)
1399 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001400
Devang Patel53bb5c92009-11-10 23:06:00 +00001401 DbgScope *Parent = NULL;
1402
1403 DIDescriptor Scope(N);
1404 if (Scope.isLexicalBlock()) {
1405 DILexicalBlock DB(N);
1406 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +00001407 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +00001408 }
1409
1410 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1411
1412 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001413 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001414 AScope->setAbstractScope();
1415 AbstractScopes[N] = AScope;
1416 if (DIDescriptor(N).isSubprogram())
1417 AbstractScopesList.push_back(AScope);
1418 return AScope;
1419}
Devang Patelaf9e8472009-10-01 20:31:14 +00001420
Devang Patel5f094002010-04-06 23:53:48 +00001421/// isSubprogramContext - Return true if Context is either a subprogram
1422/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001423static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001424 if (!Context)
1425 return false;
1426 DIDescriptor D(Context);
1427 if (D.isSubprogram())
1428 return true;
1429 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001430 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +00001431 return false;
1432}
1433
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001434/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001435/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1436/// If there are global variables in this scope then create and insert
1437/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001438DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +00001439 CompileUnit *SPCU = getCompileUnit(SPNode);
1440 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +00001441
Chris Lattnerd38fee82010-04-05 00:13:49 +00001442 assert(SPDie && "Unable to find subprogram DIE!");
1443 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001444
Chris Lattnerd38fee82010-04-05 00:13:49 +00001445 // There is not any need to generate specification DIE for a function
1446 // defined at compile unit level. If a function is defined inside another
1447 // function then gdb prefers the definition at top level and but does not
Jim Grosbach1e20b962010-07-21 21:21:52 +00001448 // expect specification DIE in parent function. So avoid creating
Chris Lattnerd38fee82010-04-05 00:13:49 +00001449 // specification DIE for a function defined inside a function.
1450 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbach1e20b962010-07-21 21:21:52 +00001451 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001452 !isSubprogramContext(SP.getContext())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001453 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001454
1455 // Add arguments.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001456 DICompositeType SPTy = SP.getType();
1457 DIArray Args = SPTy.getTypeArray();
1458 unsigned SPTag = SPTy.getTag();
1459 if (SPTag == dwarf::DW_TAG_subroutine_type)
1460 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1461 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001462 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerd38fee82010-04-05 00:13:49 +00001463 addType(Arg, ATy);
1464 if (ATy.isArtificial())
1465 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1466 SPDie->addChild(Arg);
1467 }
1468 DIE *SPDeclDie = SPDie;
1469 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001470 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Chris Lattnerd38fee82010-04-05 00:13:49 +00001471 SPDeclDie);
Devang Patel163a9f72010-05-10 22:49:55 +00001472 SPCU->addDie(SPDie);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001473 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001474
Devang Patel8aa61472010-07-07 22:20:57 +00001475 // Pick up abstract subprogram DIE.
1476 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1477 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1478 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1479 dwarf::DW_FORM_ref4, AbsSPDIE);
1480 SPCU->addDie(SPDie);
1481 }
1482
Chris Lattnerd38fee82010-04-05 00:13:49 +00001483 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1484 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1485 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1486 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1487 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1488 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1489 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001490
Chris Lattnerd38fee82010-04-05 00:13:49 +00001491 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001492}
1493
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001494/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001495/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1496DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001497
1498 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1499 if (Scope->isAbstractScope())
1500 return ScopeDIE;
1501
1502 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1503 if (Ranges.empty())
1504 return 0;
1505
1506 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1507 if (Ranges.size() > 1) {
1508 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +00001509 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +00001510 // DW_AT_ranges appropriately.
1511 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1512 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1513 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1514 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001515 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1516 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +00001517 }
1518 DebugRangeSymbols.push_back(NULL);
1519 DebugRangeSymbols.push_back(NULL);
1520 return ScopeDIE;
1521 }
1522
Devang Patelc3f5f782010-05-25 23:40:22 +00001523 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1524 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001525
Devang Patelc3f5f782010-05-25 23:40:22 +00001526 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001527
Chris Lattnerb7db7332010-03-09 01:58:53 +00001528 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1529 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001530
Devang Pateleac9c072010-04-27 19:46:33 +00001531 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1532 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001533
1534 return ScopeDIE;
1535}
1536
Devang Patel2c4ceb12009-11-21 02:48:08 +00001537/// constructInlinedScopeDIE - This scope represents inlined body of
1538/// a function. Construct DIE to represent this concrete inlined copy
1539/// of the function.
1540DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001541
1542 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001543 assert (Ranges.empty() == false
Devang Pateleac9c072010-04-27 19:46:33 +00001544 && "DbgScope does not have instruction markers!");
1545
1546 // FIXME : .debug_inlined section specification does not clearly state how
1547 // to emit inlined scope that is split into multiple instruction ranges.
1548 // For now, use first instruction range and emit low_pc/high_pc pair and
1549 // corresponding .debug_inlined section entry for this pair.
1550 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +00001551 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1552 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001553
Devang Patel0afbf232010-07-08 22:39:20 +00001554 if (StartLabel == 0 || EndLabel == 0) {
Devang Pateleac9c072010-04-27 19:46:33 +00001555 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1556 return 0;
1557 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001558 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001559 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001560 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001561 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +00001562
Devang Patel3c91b052010-03-08 20:52:55 +00001563 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001564 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001565 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001566 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1567
Devang Patel2db49d72010-05-07 18:11:54 +00001568 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001569 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1570 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001571 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001572 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001573 dwarf::DW_FORM_ref4, OriginDIE);
1574
Chris Lattner6ed0f902010-03-09 00:31:02 +00001575 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001576 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001577
1578 InlinedSubprogramDIEs.insert(OriginDIE);
1579
1580 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001581 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +00001582 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001583
1584 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001585 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001586 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +00001587 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001588 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001589 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001590
Devang Patel53bb5c92009-11-10 23:06:00 +00001591 DILocation DL(Scope->getInlinedAt());
Devang Patel163a9f72010-05-10 22:49:55 +00001592 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001593 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001594
1595 return ScopeDIE;
1596}
1597
Devang Patel2c4ceb12009-11-21 02:48:08 +00001598
1599/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001600DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +00001601 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +00001602 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001603 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001604
1605 // Translate tag to proper Dwarf tag. The result variable is dropped for
1606 // now.
1607 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +00001608 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001609 case dwarf::DW_TAG_return_variable:
1610 return NULL;
1611 case dwarf::DW_TAG_arg_variable:
1612 Tag = dwarf::DW_TAG_formal_parameter;
1613 break;
1614 case dwarf::DW_TAG_auto_variable: // fall thru
1615 default:
1616 Tag = dwarf::DW_TAG_variable;
1617 break;
1618 }
1619
1620 // Define variable debug information entry.
1621 DIE *VariableDie = new DIE(Tag);
1622
Devang Patel53bb5c92009-11-10 23:06:00 +00001623 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +00001624 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1625 V2AVI = VarToAbstractVarMap.find(DV);
1626 if (V2AVI != VarToAbstractVarMap.end())
1627 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001628
Devang Patel26c1e562010-05-20 16:36:41 +00001629 if (AbsDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001630 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001631 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001632 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001633 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel8bd11de2010-08-09 21:01:39 +00001634 addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +00001635
1636 // Add variable type.
Devang Patel8bd11de2010-08-09 21:01:39 +00001637 addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001638 }
1639
Devang Patel8bd11de2010-08-09 21:01:39 +00001640 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patelb4645642010-02-06 01:02:37 +00001641 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel3cf763d2010-09-29 23:07:21 +00001642 else if (DIVariable(DV->getVariable()).isArtificial())
1643 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +00001644
1645 if (Scope->isAbstractScope()) {
1646 DV->setDIE(VariableDie);
1647 return VariableDie;
1648 }
1649
1650 // Add variable address.
1651
1652 unsigned Offset = DV->getDotDebugLocOffset();
1653 if (Offset != ~0U) {
1654 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1655 Asm->GetTempSymbol("debug_loc", Offset));
1656 DV->setDIE(VariableDie);
1657 UseDotDebugLocEntry.insert(VariableDie);
1658 return VariableDie;
1659 }
1660
1661 // Check if variable is described by a DBG_VALUE instruction.
1662 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1663 DbgVariableToDbgInstMap.find(DV);
1664 if (DVI != DbgVariableToDbgInstMap.end()) {
1665 const MachineInstr *DVInsn = DVI->second;
1666 const MCSymbol *DVLabel = findVariableLabel(DV);
1667 bool updated = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001668 // FIXME : Handle getNumOperands != 3
Devang Patelc3f5f782010-05-25 23:40:22 +00001669 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +00001670 if (DVInsn->getOperand(0).isReg()) {
1671 const MachineOperand RegOp = DVInsn->getOperand(0);
1672 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1673 if (DVInsn->getOperand(1).isImm() &&
1674 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1675 addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
1676 updated = true;
1677 } else
1678 updated = addRegisterAddress(VariableDie, DVLabel, RegOp);
1679 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001680 else if (DVInsn->getOperand(0).isImm())
1681 updated = addConstantValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001682 else if (DVInsn->getOperand(0).isFPImm())
1683 updated =
Devang Patel61409622010-07-07 20:12:52 +00001684 addConstantFPValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patelc3f5f782010-05-25 23:40:22 +00001685 } else {
1686 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1687 if (Location.getReg()) {
1688 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1689 if (DVLabel)
1690 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1691 DVLabel);
1692 updated = true;
1693 }
1694 }
1695 if (!updated) {
1696 // If variableDie is not updated then DBG_VALUE instruction does not
1697 // have valid variable info.
1698 delete VariableDie;
1699 return NULL;
1700 }
1701 DV->setDIE(VariableDie);
1702 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001703 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001704
1705 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +00001706 int FI = 0;
Devang Patel9e3bd2c2010-08-31 06:11:28 +00001707 if (findVariableFrameIndex(DV, &FI))
1708 addVariableAddress(DV, VariableDie, FI);
1709
Devang Patel53bb5c92009-11-10 23:06:00 +00001710 DV->setDIE(VariableDie);
1711 return VariableDie;
1712
1713}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001714
Devang Patel193f7202009-11-24 01:14:22 +00001715void DwarfDebug::addPubTypes(DISubprogram SP) {
1716 DICompositeType SPTy = SP.getType();
1717 unsigned SPTag = SPTy.getTag();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001718 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel193f7202009-11-24 01:14:22 +00001719 return;
1720
1721 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001722 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +00001723 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +00001724 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +00001725 continue;
1726 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +00001727 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +00001728 && !CATy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001729 CompileUnit *TheCU = getCompileUnit(CATy);
1730 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1731 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001732 }
1733 }
1734}
1735
Devang Patel2c4ceb12009-11-21 02:48:08 +00001736/// constructScopeDIE - Construct a DIE for this scope.
1737DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001738 if (!Scope || !Scope->getScopeNode())
1739 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001740
Devang Patel3c91b052010-03-08 20:52:55 +00001741 DIScope DS(Scope->getScopeNode());
1742 DIE *ScopeDIE = NULL;
1743 if (Scope->getInlinedAt())
1744 ScopeDIE = constructInlinedScopeDIE(Scope);
1745 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +00001746 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001747 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001748 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +00001749 // Note down abstract DIE.
1750 if (ScopeDIE)
1751 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1752 }
Devang Patel3c91b052010-03-08 20:52:55 +00001753 else
Devang Patel2db49d72010-05-07 18:11:54 +00001754 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001755 }
Devang Patelaead63c2010-03-29 22:59:58 +00001756 else
Devang Patel3c91b052010-03-08 20:52:55 +00001757 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001758 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001759
Devang Patel53bb5c92009-11-10 23:06:00 +00001760 // Add variables to scope.
Devang Patele03161c2010-08-09 18:51:29 +00001761 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001762 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001763 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001764 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001765 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001766 }
1767
1768 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001769 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001770 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1771 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001772 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001773 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001774 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001775 }
Devang Patel193f7202009-11-24 01:14:22 +00001776
Jim Grosbach1e20b962010-07-21 21:21:52 +00001777 if (DS.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001778 addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001779
Devang Patel193f7202009-11-24 01:14:22 +00001780 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001781}
1782
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001783/// GetOrCreateSourceID - Look up the source id with the given directory and
1784/// source file names. If none currently exists, create a new id and insert it
1785/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1786/// maps as well.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001787unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001788 unsigned DId;
Devang Patel37032352010-07-27 20:51:15 +00001789 assert (DirName.empty() == false && "Invalid directory name!");
Devang Patel2f584852010-07-24 00:53:22 +00001790
Devang Patel1905a182010-09-16 20:57:49 +00001791 // If FE did not provide a file name, then assume stdin.
1792 if (FileName.empty())
1793 return GetOrCreateSourceID(DirName, "<stdin>");
1794
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001795 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1796 if (DI != DirectoryIdMap.end()) {
1797 DId = DI->getValue();
1798 } else {
1799 DId = DirectoryNames.size() + 1;
1800 DirectoryIdMap[DirName] = DId;
1801 DirectoryNames.push_back(DirName);
1802 }
1803
1804 unsigned FId;
1805 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1806 if (FI != SourceFileIdMap.end()) {
1807 FId = FI->getValue();
1808 } else {
1809 FId = SourceFileNames.size() + 1;
1810 SourceFileIdMap[FileName] = FId;
1811 SourceFileNames.push_back(FileName);
1812 }
1813
1814 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1815 SourceIdMap.find(std::make_pair(DId, FId));
1816 if (SI != SourceIdMap.end())
1817 return SI->second;
1818
1819 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1820 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1821 SourceIds.push_back(std::make_pair(DId, FId));
1822
1823 return SrcId;
1824}
1825
Devang Patel6404e4e2009-12-15 19:16:48 +00001826/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1827DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel163a9f72010-05-10 22:49:55 +00001828 CompileUnit *TheCU = getCompileUnit(NS);
1829 DIE *NDie = TheCU->getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001830 if (NDie)
1831 return NDie;
1832 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel163a9f72010-05-10 22:49:55 +00001833 TheCU->insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +00001834 if (!NS.getName().empty())
1835 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patel81625742010-08-09 20:20:05 +00001836 addSourceLine(NDie, NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001837 addToContextOwner(NDie, NS.getContext());
1838 return NDie;
1839}
1840
Jim Grosbach1e20b962010-07-21 21:21:52 +00001841/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +00001842/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001843void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001844 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001845 StringRef FN = DIUnit.getFilename();
1846 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001847 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001848
1849 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001850 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001851 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001852 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001853 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001854 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +00001855 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1856 // simplifies debug range entries.
Devang Patel9b93b6b2010-06-28 22:22:47 +00001857 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +00001858 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +00001859 // compile unit in debug_line section.
Devang Patelae84d5b2010-08-31 23:50:19 +00001860 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
1861 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
1862 Asm->GetTempSymbol("section_line"));
1863 else
1864 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001865
Devang Patel65dbc902009-11-25 17:36:49 +00001866 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001867 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001868 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001869 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001870
Devang Patel65dbc902009-11-25 17:36:49 +00001871 StringRef Flags = DIUnit.getFlags();
1872 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001873 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001874
1875 unsigned RVer = DIUnit.getRunTimeVersion();
1876 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001877 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001878 dwarf::DW_FORM_data1, RVer);
1879
Devang Patel163a9f72010-05-10 22:49:55 +00001880 CompileUnit *NewCU = new CompileUnit(ID, Die);
1881 if (!FirstCU)
1882 FirstCU = NewCU;
1883 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001884}
1885
Devang Patel163a9f72010-05-10 22:49:55 +00001886/// getCompielUnit - Get CompileUnit DIE.
1887CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1888 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1889 DIDescriptor D(N);
1890 const MDNode *CUNode = NULL;
1891 if (D.isCompileUnit())
1892 CUNode = N;
1893 else if (D.isSubprogram())
1894 CUNode = DISubprogram(N).getCompileUnit();
1895 else if (D.isType())
1896 CUNode = DIType(N).getCompileUnit();
1897 else if (D.isGlobalVariable())
1898 CUNode = DIGlobalVariable(N).getCompileUnit();
1899 else if (D.isVariable())
1900 CUNode = DIVariable(N).getCompileUnit();
1901 else if (D.isNameSpace())
1902 CUNode = DINameSpace(N).getCompileUnit();
1903 else if (D.isFile())
1904 CUNode = DIFile(N).getCompileUnit();
1905 else
1906 return FirstCU;
1907
1908 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1909 = CUMap.find(CUNode);
1910 if (I == CUMap.end())
1911 return FirstCU;
1912 return I->second;
1913}
1914
Devang Patel0c4720c2010-08-23 18:25:56 +00001915/// isUnsignedDIType - Return true if type encoding is unsigned.
1916static bool isUnsignedDIType(DIType Ty) {
1917 DIDerivedType DTy(Ty);
1918 if (DTy.Verify())
1919 return isUnsignedDIType(DTy.getTypeDerivedFrom());
1920
1921 DIBasicType BTy(Ty);
1922 if (BTy.Verify()) {
1923 unsigned Encoding = BTy.getEncoding();
1924 if (Encoding == dwarf::DW_ATE_unsigned ||
1925 Encoding == dwarf::DW_ATE_unsigned_char)
1926 return true;
1927 }
1928 return false;
1929}
Devang Patel163a9f72010-05-10 22:49:55 +00001930
1931/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001932void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +00001933 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001934
Devang Patel905cf5e2009-09-04 23:59:07 +00001935 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +00001936 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +00001937 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001938
1939 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001940 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +00001941 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001942 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001943
Devang Patel9fa539c2010-08-10 01:37:23 +00001944 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +00001945 DIE *VariableDIE = new DIE(GV.getTag());
1946
1947 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001948
Devang Patel9fa539c2010-08-10 01:37:23 +00001949 // Add name.
1950 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1951 GV.getDisplayName());
1952 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +00001953 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel9fa539c2010-08-10 01:37:23 +00001954 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1955 getRealLinkageName(LinkageName));
1956 // Add type.
1957 addType(VariableDIE, GTy);
Devang Patel50d80e32010-04-13 20:35:04 +00001958 if (GTy.isCompositeType() && !GTy.getName().empty()
1959 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001960 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001961 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +00001962 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001963 }
Devang Patel9fa539c2010-08-10 01:37:23 +00001964 // Add scoping info.
1965 if (!GV.isLocalToUnit()) {
1966 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1967 // Expose as global.
1968 TheCU->addGlobal(GV.getName(), VariableDIE);
1969 }
1970 // Add line number info.
1971 addSourceLine(VariableDIE, GV);
1972 // Add to map.
1973 TheCU->insertDIE(N, VariableDIE);
1974 // Add to context owner.
1975 DIDescriptor GVContext = GV.getContext();
1976 addToContextOwner(VariableDIE, GVContext);
1977 // Add location.
Devang Patel29368072010-08-10 07:11:13 +00001978 if (isGlobalVariable) {
1979 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1980 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1981 addLabel(Block, 0, dwarf::DW_FORM_udata,
1982 Asm->Mang->getSymbol(GV.getGlobal()));
1983 // Do not create specification DIE if context is either compile unit
1984 // or a subprogram.
1985 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1986 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1987 // Create specification DIE.
1988 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1989 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1990 dwarf::DW_FORM_ref4, VariableDIE);
1991 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1992 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1993 TheCU->addDie(VariableSpecDIE);
1994 } else {
1995 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1996 }
1997 } else if (Constant *C = GV.getConstant()) {
1998 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
Devang Patel0c4720c2010-08-23 18:25:56 +00001999 if (isUnsignedDIType(GTy))
Devang Patel29368072010-08-10 07:11:13 +00002000 addUInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
2001 CI->getZExtValue());
2002 else
2003 addSInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
2004 CI->getSExtValue());
Devang Patel29368072010-08-10 07:11:13 +00002005 }
Devang Patel9fa539c2010-08-10 01:37:23 +00002006 }
Devang Patel13e16b62009-06-26 01:49:18 +00002007 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002008}
2009
Devang Patel163a9f72010-05-10 22:49:55 +00002010/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002011void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00002012 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002013
Stuart Hastings639336e2010-04-06 21:38:29 +00002014 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00002015 CompileUnit *TheCU = getCompileUnit(N);
2016 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +00002017 return;
2018
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002019 if (!SP.isDefinition())
2020 // This is a method declaration which will be handled while constructing
2021 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00002022 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002023
Stuart Hastings639336e2010-04-06 21:38:29 +00002024 DIE *SubprogramDie = createSubprogramDIE(SP);
2025
2026 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00002027 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002028
2029 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00002030 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00002031
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002032 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +00002033 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00002034
Devang Patel13e16b62009-06-26 01:49:18 +00002035 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002036}
2037
Devang Patel2c4ceb12009-11-21 02:48:08 +00002038/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00002039/// content. Create global DIEs and emit initial debug info sections.
2040/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00002041void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00002042 if (DisableDebugInfoPrinting)
2043 return;
2044
Devang Patel78ab9e22009-07-30 18:56:46 +00002045 DebugInfoFinder DbgFinder;
2046 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00002047
Chris Lattnerd850ac72010-04-05 02:19:28 +00002048 bool HasDebugInfo = false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002049
Chris Lattnerd850ac72010-04-05 02:19:28 +00002050 // Scan all the compile-units to see if there are any marked as the main unit.
2051 // if not, we do not generate debug info.
2052 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2053 E = DbgFinder.compile_unit_end(); I != E; ++I) {
2054 if (DICompileUnit(*I).isMain()) {
2055 HasDebugInfo = true;
2056 break;
2057 }
2058 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002059
Chris Lattnerd850ac72010-04-05 02:19:28 +00002060 if (!HasDebugInfo) return;
2061
2062 // Tell MMI that we have debug info.
2063 MMI->setDebugInfoAvailability(true);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002064
Chris Lattnerbe15beb2010-04-04 23:17:54 +00002065 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00002066 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002067
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002068 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00002069 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2070 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002071 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002072
Devang Patel53bb5c92009-11-10 23:06:00 +00002073 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00002074 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
2075 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002076 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00002077
Devang Patelc366f832009-12-10 19:14:49 +00002078 // Create DIEs for each global variable.
2079 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
2080 E = DbgFinder.global_variable_end(); I != E; ++I)
2081 constructGlobalVariableDIE(*I);
2082
Devang Patele7e5a0f2010-08-10 20:01:20 +00002083 //getOrCreateTypeDIE
2084 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
2085 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2086 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2087
Devang Patel1a7ca032010-09-28 18:08:20 +00002088 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
2089 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2090 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2091
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002092 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00002093 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002094
2095 // Print out .file directives to specify files for .loc directives. These are
2096 // printed out early so that they precede any .loc directives.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002097 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002098 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2099 // Remember source id starts at 1.
2100 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002101 // FIXME: don't use sys::path for this! This should not depend on the
2102 // host.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002103 sys::Path FullPath(getSourceDirectoryName(Id.first));
2104 bool AppendOk =
2105 FullPath.appendComponent(getSourceFileName(Id.second));
2106 assert(AppendOk && "Could not append filename to directory!");
2107 AppendOk = false;
Chris Lattnera6594fc2010-01-25 18:58:59 +00002108 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002109 }
2110 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002111}
2112
Devang Patel2c4ceb12009-11-21 02:48:08 +00002113/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002114///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002115void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00002116 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +00002117 const Module *M = MMI->getModule();
Devang Patele9a1cca2010-08-02 17:32:15 +00002118 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +00002119 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2120 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2121 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2122 DISubprogram SP(AllSPs->getOperand(SI));
2123 if (!SP.Verify()) continue;
2124
2125 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +00002126 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +00002127 StringRef FName = SP.getLinkageName();
2128 if (FName.empty())
2129 FName = SP.getName();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002130 NamedMDNode *NMD =
Devang Patel4a1cad62010-06-28 18:25:03 +00002131 M->getNamedMetadata(Twine("llvm.dbg.lv.", getRealLinkageName(FName)));
2132 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();
2424 const Module *M = F->getParent();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002425 if (NamedMDNode *NMD =
2426 M->getNamedMetadata(Twine("llvm.dbg.lv.",
Devang Patela762b092010-06-22 01:19:38 +00002427 getRealLinkageName(F->getName())))) {
Devang Patel98e1cac2010-05-14 21:01:35 +00002428 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002429 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00002430 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00002431 continue;
2432 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2433 if (Scope)
Devang Patel26c1e562010-05-20 16:36:41 +00002434 Scope->addVariable(new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00002435 }
2436 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002437}
Devang Patel98e1cac2010-05-14 21:01:35 +00002438
Devang Patelc3f5f782010-05-25 23:40:22 +00002439/// getLabelBeforeInsn - Return Label preceding the instruction.
2440const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2441 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2442 LabelsBeforeInsn.find(MI);
2443 if (I == LabelsBeforeInsn.end())
2444 // FunctionBeginSym always preceeds all the instruction in current function.
2445 return FunctionBeginSym;
2446 return I->second;
2447}
2448
2449/// getLabelAfterInsn - Return Label immediately following the instruction.
2450const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2451 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2452 LabelsAfterInsn.find(MI);
2453 if (I == LabelsAfterInsn.end())
2454 return NULL;
2455 return I->second;
Devang Patele717faa2009-10-06 01:26:37 +00002456}
2457
Devang Patel553881b2010-03-29 17:20:31 +00002458/// beginScope - Process beginning of a scope.
2459void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002460 if (InsnNeedsLabel.count(MI) == 0) {
2461 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patel553881b2010-03-29 17:20:31 +00002462 return;
Devang Patelc3f5f782010-05-25 23:40:22 +00002463 }
Devang Patelaead63c2010-03-29 22:59:58 +00002464
Devang Patelb2b31a62010-05-26 19:37:24 +00002465 // Check location.
2466 DebugLoc DL = MI->getDebugLoc();
2467 if (!DL.isUnknown()) {
2468 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2469 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Pateleac9c072010-04-27 19:46:33 +00002470 PrevInstLoc = DL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002471 LabelsBeforeInsn[MI] = PrevLabel;
2472 return;
Devang Pateleac9c072010-04-27 19:46:33 +00002473 }
Devang Patel553881b2010-03-29 17:20:31 +00002474
Jim Grosbach1e20b962010-07-21 21:21:52 +00002475 // If location is unknown then use temp label for this DBG_VALUE
Devang Patelb2b31a62010-05-26 19:37:24 +00002476 // instruction.
2477 if (MI->isDebugValue()) {
Devang Patel77051f52010-05-26 21:23:46 +00002478 PrevLabel = MMI->getContext().CreateTempSymbol();
2479 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00002480 LabelsBeforeInsn[MI] = PrevLabel;
2481 return;
2482 }
2483
2484 if (UnknownLocations) {
2485 PrevLabel = recordSourceLine(0, 0, 0);
2486 LabelsBeforeInsn[MI] = PrevLabel;
2487 return;
2488 }
2489
2490 assert (0 && "Instruction is not processed!");
Devang Patel0d20ac82009-10-06 01:50:42 +00002491}
2492
Devang Patel2c4ceb12009-11-21 02:48:08 +00002493/// endScope - Process end of a scope.
2494void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002495 if (InsnsEndScopeSet.count(MI) != 0) {
2496 // Emit a label if this instruction ends a scope.
2497 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2498 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002499 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002500 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002501}
2502
Devang Pateleac9c072010-04-27 19:46:33 +00002503/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002504DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel61409622010-07-07 20:12:52 +00002505 const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002506 if (!InlinedAt) {
2507 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2508 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002509 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002510 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2511 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002512 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002513 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002514 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002515 WScope->setParent(Parent);
2516 Parent->addScope(WScope);
2517 }
2518
2519 if (!WScope->getParent()) {
2520 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastingscad22ad2010-06-15 23:06:30 +00002521 // We used to check only for a linkage name, but that fails
2522 // since we began omitting the linkage name for private
2523 // functions. The new way is to check for the name in metadata,
2524 // but that's not supported in old .ll test cases. Ergo, we
2525 // check both.
Stuart Hastings215aa152010-06-11 20:08:44 +00002526 if (SPName == Asm->MF->getFunction()->getName() ||
2527 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Pateleac9c072010-04-27 19:46:33 +00002528 CurrentFnDbgScope = WScope;
2529 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002530
Devang Pateleac9c072010-04-27 19:46:33 +00002531 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002532 }
2533
Devang Patel78e127d2010-06-25 22:07:34 +00002534 getOrCreateAbstractScope(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00002535 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2536 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002537 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002538
2539 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2540 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2541 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002542 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002543 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002544 WScope->setParent(Parent);
2545 Parent->addScope(WScope);
2546
2547 ConcreteScopes[InlinedAt] = WScope;
Devang Pateleac9c072010-04-27 19:46:33 +00002548
2549 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002550}
2551
Devang Pateleac9c072010-04-27 19:46:33 +00002552/// hasValidLocation - Return true if debug location entry attached with
2553/// machine instruction encodes valid location info.
2554static bool hasValidLocation(LLVMContext &Ctx,
2555 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002556 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002557 DebugLoc DL = MInsn->getDebugLoc();
2558 if (DL.isUnknown()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002559
Devang Patele9f8f5e2010-05-07 20:54:48 +00002560 const MDNode *S = DL.getScope(Ctx);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002561
Devang Pateleac9c072010-04-27 19:46:33 +00002562 // There is no need to create another DIE for compile unit. For all
2563 // other scopes, create one DbgScope now. This will be translated
2564 // into a scope DIE at the end.
2565 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002566
Devang Pateleac9c072010-04-27 19:46:33 +00002567 Scope = S;
2568 InlinedAt = DL.getInlinedAt(Ctx);
2569 return true;
2570}
2571
2572/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2573/// hierarchy.
2574static void calculateDominanceGraph(DbgScope *Scope) {
2575 assert (Scope && "Unable to calculate scop edominance graph!");
2576 SmallVector<DbgScope *, 4> WorkStack;
2577 WorkStack.push_back(Scope);
2578 unsigned Counter = 0;
2579 while (!WorkStack.empty()) {
2580 DbgScope *WS = WorkStack.back();
2581 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2582 bool visitedChildren = false;
2583 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2584 SE = Children.end(); SI != SE; ++SI) {
2585 DbgScope *ChildScope = *SI;
2586 if (!ChildScope->getDFSOut()) {
2587 WorkStack.push_back(ChildScope);
2588 visitedChildren = true;
2589 ChildScope->setDFSIn(++Counter);
2590 break;
2591 }
2592 }
2593 if (!visitedChildren) {
2594 WorkStack.pop_back();
2595 WS->setDFSOut(++Counter);
2596 }
2597 }
2598}
2599
2600/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002601static
Devang Pateleac9c072010-04-27 19:46:33 +00002602void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2603 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2604{
2605#ifndef NDEBUG
2606 unsigned PrevDFSIn = 0;
2607 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2608 I != E; ++I) {
2609 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2610 II != IE; ++II) {
2611 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002612 const MDNode *Scope = NULL;
2613 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002614
2615 // Check if instruction has valid location information.
2616 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2617 dbgs() << " [ ";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002618 if (InlinedAt)
Devang Pateleac9c072010-04-27 19:46:33 +00002619 dbgs() << "*";
Jim Grosbach1e20b962010-07-21 21:21:52 +00002620 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Pateleac9c072010-04-27 19:46:33 +00002621 MI2ScopeMap.find(MInsn);
2622 if (DI != MI2ScopeMap.end()) {
2623 DbgScope *S = DI->second;
2624 dbgs() << S->getDFSIn();
2625 PrevDFSIn = S->getDFSIn();
2626 } else
2627 dbgs() << PrevDFSIn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002628 } else
Devang Pateleac9c072010-04-27 19:46:33 +00002629 dbgs() << " [ x" << PrevDFSIn;
2630 dbgs() << " ]";
2631 MInsn->dump();
2632 }
2633 dbgs() << "\n";
2634 }
2635#endif
2636}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002637/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002638/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002639bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002640 // If scope information was extracted using .dbg intrinsics then there is not
2641 // any need to extract these information by scanning each instruction.
2642 if (!DbgScopeMap.empty())
2643 return false;
2644
Dan Gohman314bf7c2010-04-23 01:18:53 +00002645 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002646 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2647 SmallVector<DbgRange, 4> MIRanges;
2648 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002649 const MDNode *PrevScope = NULL;
2650 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002651 const MachineInstr *RangeBeginMI = NULL;
2652 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002653 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002654 I != E; ++I) {
2655 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2656 II != IE; ++II) {
2657 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002658 const MDNode *Scope = NULL;
2659 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002660
2661 // Check if instruction has valid location information.
2662 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2663 PrevMI = MInsn;
2664 continue;
2665 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002666
Devang Pateleac9c072010-04-27 19:46:33 +00002667 // If scope has not changed then skip this instruction.
2668 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2669 PrevMI = MInsn;
2670 continue;
2671 }
2672
Jim Grosbach1e20b962010-07-21 21:21:52 +00002673 if (RangeBeginMI) {
2674 // If we have alread seen a beginning of a instruction range and
Devang Pateleac9c072010-04-27 19:46:33 +00002675 // current instruction scope does not match scope of first instruction
2676 // in this range then create a new instruction range.
2677 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002678 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel61409622010-07-07 20:12:52 +00002679 PrevInlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002680 MIRanges.push_back(R);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002681 }
Devang Pateleac9c072010-04-27 19:46:33 +00002682
2683 // This is a beginning of a new instruction range.
2684 RangeBeginMI = MInsn;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002685
Devang Pateleac9c072010-04-27 19:46:33 +00002686 // Reset previous markers.
2687 PrevMI = MInsn;
2688 PrevScope = Scope;
2689 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002690 }
2691 }
2692
Devang Pateleac9c072010-04-27 19:46:33 +00002693 // Create last instruction range.
2694 if (RangeBeginMI && PrevMI && PrevScope) {
2695 DbgRange R(RangeBeginMI, PrevMI);
2696 MIRanges.push_back(R);
2697 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002698 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002699
Devang Patel344130e2010-01-04 20:44:00 +00002700 if (!CurrentFnDbgScope)
2701 return false;
2702
Devang Pateleac9c072010-04-27 19:46:33 +00002703 calculateDominanceGraph(CurrentFnDbgScope);
2704 if (PrintDbgScope)
2705 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2706
2707 // Find ranges of instructions covered by each DbgScope;
2708 DbgScope *PrevDbgScope = NULL;
2709 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2710 RE = MIRanges.end(); RI != RE; ++RI) {
2711 const DbgRange &R = *RI;
2712 DbgScope *S = MI2ScopeMap.lookup(R.first);
2713 assert (S && "Lost DbgScope for a machine instruction!");
2714 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2715 PrevDbgScope->closeInsnRange(S);
2716 S->openInsnRange(R.first);
2717 S->extendInsnRange(R.second);
2718 PrevDbgScope = S;
2719 }
2720
2721 if (PrevDbgScope)
2722 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002723
Devang Patele37b0c62010-04-08 18:43:56 +00002724 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002725
2726 return !DbgScopeMap.empty();
2727}
2728
Jim Grosbach1e20b962010-07-21 21:21:52 +00002729/// identifyScopeMarkers() -
Devang Pateleac9c072010-04-27 19:46:33 +00002730/// Each DbgScope has first instruction and last instruction to mark beginning
2731/// and end of a scope respectively. Create an inverse map that list scopes
2732/// starts (and ends) with an instruction. One instruction may start (or end)
2733/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002734void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002735 SmallVector<DbgScope *, 4> WorkList;
2736 WorkList.push_back(CurrentFnDbgScope);
2737 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002738 DbgScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002739
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002740 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002741 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002742 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002743 SE = Children.end(); SI != SE; ++SI)
2744 WorkList.push_back(*SI);
2745
Devang Patel53bb5c92009-11-10 23:06:00 +00002746 if (S->isAbstractScope())
2747 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002748
Devang Pateleac9c072010-04-27 19:46:33 +00002749 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2750 if (Ranges.empty())
2751 continue;
2752 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2753 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002754 assert(RI->first && "DbgRange does not have first instruction!");
2755 assert(RI->second && "DbgRange does not have second instruction!");
Devang Pateleac9c072010-04-27 19:46:33 +00002756 InsnsEndScopeSet.insert(RI->second);
2757 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002758 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002759}
2760
Dan Gohman084751c2010-04-20 00:37:27 +00002761/// FindFirstDebugLoc - Find the first debug location in the function. This
2762/// is intended to be an approximation for the source position of the
2763/// beginning of the function.
2764static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2765 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2766 I != E; ++I)
2767 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2768 MBBI != MBBE; ++MBBI) {
2769 DebugLoc DL = MBBI->getDebugLoc();
2770 if (!DL.isUnknown())
2771 return DL;
2772 }
2773 return DebugLoc();
2774}
2775
Devang Patel9a31f0f2010-10-25 20:45:32 +00002776#ifndef NDEBUG
2777/// CheckLineNumbers - Count basicblocks whose instructions do not have any
2778/// line number information.
2779static void CheckLineNumbers(const MachineFunction *MF) {
2780 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2781 I != E; ++I) {
2782 bool FoundLineNo = false;
2783 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2784 II != IE; ++II) {
2785 const MachineInstr *MI = II;
2786 if (!MI->getDebugLoc().isUnknown()) {
2787 FoundLineNo = true;
2788 break;
2789 }
2790 }
2791 if (!FoundLineNo)
2792 ++BlocksWithoutLineNo;
2793 }
2794}
2795#endif
2796
Devang Patel2c4ceb12009-11-21 02:48:08 +00002797/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002798/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002799void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002800 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002801 if (!extractScopeInformation()) return;
Devang Patel60b35bd2009-10-06 18:37:31 +00002802
Devang Patel9a31f0f2010-10-25 20:45:32 +00002803#ifndef NDEBUG
2804 CheckLineNumbers(MF);
2805#endif
2806
Devang Pateleac9c072010-04-27 19:46:33 +00002807 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2808 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002809 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002810 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002811
2812 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2813 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002814 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002815 if (FDL.isUnknown()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002816
Devang Patele9f8f5e2010-05-07 20:54:48 +00002817 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings0db42712010-07-19 23:56:30 +00002818 const MDNode *TheScope = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002819
Chris Lattnerde4845c2010-04-02 19:42:39 +00002820 DISubprogram SP = getDISubprogram(Scope);
2821 unsigned Line, Col;
2822 if (SP.Verify()) {
2823 Line = SP.getLineNumber();
2824 Col = 0;
Stuart Hastings0db42712010-07-19 23:56:30 +00002825 TheScope = SP;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002826 } else {
2827 Line = FDL.getLine();
2828 Col = FDL.getCol();
Stuart Hastings0db42712010-07-19 23:56:30 +00002829 TheScope = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002830 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002831
Stuart Hastings0db42712010-07-19 23:56:30 +00002832 recordSourceLine(Line, Col, TheScope);
Devang Patelb2b31a62010-05-26 19:37:24 +00002833
Devang Patelb9abe9f2010-06-02 16:42:51 +00002834 /// ProcessedArgs - Collection of arguments already processed.
2835 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2836
Devang Patelb2b31a62010-05-26 19:37:24 +00002837 DebugLoc PrevLoc;
2838 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2839 I != E; ++I)
2840 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2841 II != IE; ++II) {
2842 const MachineInstr *MI = II;
2843 DebugLoc DL = MI->getDebugLoc();
2844 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00002845 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2846 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2847 if (!DV.Verify()) continue;
Devang Patel55e97172010-05-27 16:47:30 +00002848 // If DBG_VALUE is for a local variable then it needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002849 if (DV.getTag() != dwarf::DW_TAG_arg_variable
Devang Patel7fb231c2010-07-01 21:38:08 +00002850 && isDbgValueInUndefinedReg(MI) == false)
Devang Patelb2b31a62010-05-26 19:37:24 +00002851 InsnNeedsLabel.insert(MI);
Devang Patel55e97172010-05-27 16:47:30 +00002852 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel7fb231c2010-07-01 21:38:08 +00002853 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2854 describes(MF->getFunction()))
Devang Patel55e97172010-05-27 16:47:30 +00002855 InsnNeedsLabel.insert(MI);
2856 // DBG_VALUE indicating argument location change needs a label.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002857 else if (isDbgValueInUndefinedReg(MI) == false
2858 && !ProcessedArgs.insert(DV))
Devang Patelb2b31a62010-05-26 19:37:24 +00002859 InsnNeedsLabel.insert(MI);
2860 } else {
Jim Grosbach1e20b962010-07-21 21:21:52 +00002861 // If location is unknown then instruction needs a location only if
Devang Patelb2b31a62010-05-26 19:37:24 +00002862 // UnknownLocations flag is set.
2863 if (DL.isUnknown()) {
2864 if (UnknownLocations && !PrevLoc.isUnknown())
2865 InsnNeedsLabel.insert(MI);
2866 } else if (DL != PrevLoc)
2867 // Otherwise, instruction needs a location only if it is new location.
2868 InsnNeedsLabel.insert(MI);
2869 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002870
Devang Patelb2b31a62010-05-26 19:37:24 +00002871 if (!DL.isUnknown() || UnknownLocations)
2872 PrevLoc = DL;
2873 }
2874
2875 PrevLabel = FunctionBeginSym;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002876}
2877
Devang Patel2c4ceb12009-11-21 02:48:08 +00002878/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002879///
Chris Lattnereec791a2010-01-26 23:18:02 +00002880void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002881 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002882
Devang Patel344130e2010-01-04 20:44:00 +00002883 if (CurrentFnDbgScope) {
Devang Patel65eb4822010-05-22 00:04:14 +00002884
Devang Patelc3f5f782010-05-25 23:40:22 +00002885 // Define end label for subprogram.
2886 FunctionEndSym = Asm->GetTempSymbol("func_end",
2887 Asm->getFunctionNumber());
2888 // Assumes in correct section after the entry point.
2889 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002890
Devang Patel78e127d2010-06-25 22:07:34 +00002891 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2892 collectVariableInfo(MF, ProcessedVars);
Devang Patel65eb4822010-05-22 00:04:14 +00002893
Devang Patel344130e2010-01-04 20:44:00 +00002894 // Get function line info.
2895 if (!Lines.empty()) {
2896 // Get section line info.
2897 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2898 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2899 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2900 // Append the function info to section info.
2901 SectionLineInfos.insert(SectionLineInfos.end(),
2902 Lines.begin(), Lines.end());
2903 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002904
Devang Patel344130e2010-01-04 20:44:00 +00002905 // Construct abstract scopes.
2906 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel78e127d2010-06-25 22:07:34 +00002907 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel78e127d2010-06-25 22:07:34 +00002908 DISubprogram SP((*AI)->getScopeNode());
2909 if (SP.Verify()) {
2910 // Collect info for variables that were optimized out.
2911 StringRef FName = SP.getLinkageName();
2912 if (FName.empty())
2913 FName = SP.getName();
2914 const Module *M = MF->getFunction()->getParent();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002915 if (NamedMDNode *NMD =
2916 M->getNamedMetadata(Twine("llvm.dbg.lv.",
Devang Patel78e127d2010-06-25 22:07:34 +00002917 getRealLinkageName(FName)))) {
2918 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00002919 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00002920 if (!DV || !ProcessedVars.insert(DV))
2921 continue;
Devang Patel1d68d212010-06-30 00:11:08 +00002922 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel78e127d2010-06-25 22:07:34 +00002923 if (Scope)
2924 Scope->addVariable(new DbgVariable(DV));
2925 }
2926 }
2927 }
Devang Patel90e19aa2010-06-30 01:40:11 +00002928 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2929 constructScopeDIE(*AI);
Devang Patel78e127d2010-06-25 22:07:34 +00002930 }
Devang Patel61409622010-07-07 20:12:52 +00002931
Devang Patel9c488372010-05-04 06:15:30 +00002932 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002933
Devang Patel9c488372010-05-04 06:15:30 +00002934 if (!DisableFramePointerElim(*MF))
Jim Grosbach1e20b962010-07-21 21:21:52 +00002935 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
Devang Patel9c488372010-05-04 06:15:30 +00002936 dwarf::DW_FORM_flag, 1);
2937
2938
Chris Lattnerd38fee82010-04-05 00:13:49 +00002939 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002940 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002941 }
2942
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002943 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002944 CurrentFnDbgScope = NULL;
Devang Patelb2b31a62010-05-26 19:37:24 +00002945 InsnNeedsLabel.clear();
Devang Patel26c1e562010-05-20 16:36:41 +00002946 DbgVariableToFrameIndexMap.clear();
2947 VarToAbstractVarMap.clear();
2948 DbgVariableToDbgInstMap.clear();
2949 DbgVariableLabelsMap.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002950 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002951 InsnsEndScopeSet.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002952 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002953 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002954 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002955 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002956 LabelsBeforeInsn.clear();
2957 LabelsAfterInsn.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002958 Lines.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002959 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002960}
2961
Devang Patel26c1e562010-05-20 16:36:41 +00002962/// recordVariableFrameIndex - Record a variable's index.
2963void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2964 assert (V && "Invalid DbgVariable!");
2965 DbgVariableToFrameIndexMap[V] = Index;
2966}
2967
2968/// findVariableFrameIndex - Return true if frame index for the variable
2969/// is found. Update FI to hold value of the index.
2970bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2971 assert (V && "Invalid DbgVariable!");
2972 DenseMap<const DbgVariable *, int>::iterator I =
2973 DbgVariableToFrameIndexMap.find(V);
2974 if (I == DbgVariableToFrameIndexMap.end())
2975 return false;
2976 *FI = I->second;
2977 return true;
2978}
2979
2980/// findVariableLabel - Find MCSymbol for the variable.
2981const MCSymbol *DwarfDebug::findVariableLabel(const DbgVariable *V) {
2982 DenseMap<const DbgVariable *, const MCSymbol *>::iterator I
2983 = DbgVariableLabelsMap.find(V);
2984 if (I == DbgVariableLabelsMap.end())
2985 return NULL;
2986 else return I->second;
2987}
2988
Jim Grosbach1e20b962010-07-21 21:21:52 +00002989/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patelee432862010-05-20 19:57:06 +00002990/// instruction.
2991DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2992 DbgScope *Scope = NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002993 LLVMContext &Ctx =
Devang Patelee432862010-05-20 19:57:06 +00002994 MInsn->getParent()->getParent()->getFunction()->getContext();
2995 DebugLoc DL = MInsn->getDebugLoc();
2996
Jim Grosbach1e20b962010-07-21 21:21:52 +00002997 if (DL.isUnknown())
Devang Patelee432862010-05-20 19:57:06 +00002998 return Scope;
2999
3000 if (const MDNode *IA = DL.getInlinedAt(Ctx))
3001 Scope = ConcreteScopes.lookup(IA);
3002 if (Scope == 0)
3003 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003004
Devang Patelee432862010-05-20 19:57:06 +00003005 return Scope;
3006}
3007
3008
Chris Lattnerc6087842010-03-09 04:54:43 +00003009/// recordSourceLine - Register a source line with debug info. Returns the
3010/// unique label that was emitted and which provides correspondence to
3011/// the source line list.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003012MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel61409622010-07-07 20:12:52 +00003013 const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00003014 StringRef Dir;
3015 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00003016
Dan Gohman1cc0d622010-05-05 23:41:32 +00003017 unsigned Src = 1;
3018 if (S) {
3019 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00003020
Dan Gohman1cc0d622010-05-05 23:41:32 +00003021 if (Scope.isCompileUnit()) {
3022 DICompileUnit CU(S);
3023 Dir = CU.getDirectory();
3024 Fn = CU.getFilename();
3025 } else if (Scope.isSubprogram()) {
3026 DISubprogram SP(S);
3027 Dir = SP.getDirectory();
3028 Fn = SP.getFilename();
3029 } else if (Scope.isLexicalBlock()) {
3030 DILexicalBlock DB(S);
3031 Dir = DB.getDirectory();
3032 Fn = DB.getFilename();
3033 } else
3034 assert(0 && "Unexpected scope info");
3035
3036 Src = GetOrCreateSourceID(Dir, Fn);
3037 }
3038
Chris Lattner63d78362010-03-14 08:36:50 +00003039 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner25b68c62010-03-14 08:15:55 +00003040 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00003041
Chris Lattnerc6087842010-03-09 04:54:43 +00003042 Asm->OutStreamer.EmitLabel(Label);
3043 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00003044}
3045
Bill Wendling829e67b2009-05-20 23:22:40 +00003046//===----------------------------------------------------------------------===//
3047// Emit Methods
3048//===----------------------------------------------------------------------===//
3049
Devang Patel2c4ceb12009-11-21 02:48:08 +00003050/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00003051///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003052unsigned
3053DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003054 // Get the children.
3055 const std::vector<DIE *> &Children = Die->getChildren();
3056
3057 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003058 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00003059 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00003060
3061 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003062 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00003063
3064 // Get the abbreviation for this DIE.
3065 unsigned AbbrevNumber = Die->getAbbrevNumber();
3066 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3067
3068 // Set DIE offset
3069 Die->setOffset(Offset);
3070
3071 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00003072 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003073
3074 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
3075 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3076
3077 // Size the DIE attribute values.
3078 for (unsigned i = 0, N = Values.size(); i < N; ++i)
3079 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00003080 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00003081
3082 // Size the DIE children if any.
3083 if (!Children.empty()) {
3084 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
3085 "Children flag not set");
3086
3087 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003088 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00003089
3090 // End of children marker.
3091 Offset += sizeof(int8_t);
3092 }
3093
3094 Die->setSize(Offset - Die->getOffset());
3095 return Offset;
3096}
3097
Devang Patel2c4ceb12009-11-21 02:48:08 +00003098/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00003099///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003100void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00003101 unsigned PrevOffset = 0;
3102 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3103 E = CUMap.end(); I != E; ++I) {
3104 // Compute size of compile unit header.
3105 static unsigned Offset = PrevOffset +
3106 sizeof(int32_t) + // Length of Compilation Unit Info
3107 sizeof(int16_t) + // DWARF version number
3108 sizeof(int32_t) + // Offset Into Abbrev. Section
3109 sizeof(int8_t); // Pointer Size (in bytes)
3110 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
3111 PrevOffset = Offset;
3112 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003113}
3114
Chris Lattner11b8f302010-04-04 23:02:02 +00003115/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
3116/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00003117static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00003118 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003119 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00003120 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003121
Chris Lattner9c69e285532010-04-04 22:59:04 +00003122 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
3123 Asm->OutStreamer.EmitLabel(TmpSym);
3124 return TmpSym;
3125}
3126
3127/// EmitSectionLabels - Emit initial Dwarf sections with a label at
3128/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00003129void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003130 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003131
Bill Wendling94d04b82009-05-20 23:21:38 +00003132 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003133 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00003134 DwarfFrameSectionSym =
3135 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3136 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003137
Jim Grosbach1e20b962010-07-21 21:21:52 +00003138 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003139 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003140 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003141 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00003142 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003143
Chris Lattner9c69e285532010-04-04 22:59:04 +00003144 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00003145 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003146
Devang Patelaf608bd2010-08-24 00:06:12 +00003147 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00003148 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3149 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3150 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003151 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00003152 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00003153 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3154 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00003155
Devang Patelc3f5f782010-05-25 23:40:22 +00003156 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3157 "section_debug_loc");
3158
Chris Lattner9c69e285532010-04-04 22:59:04 +00003159 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00003160 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003161}
3162
Devang Patel2c4ceb12009-11-21 02:48:08 +00003163/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00003164///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003165void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003166 // Get the abbreviation for this DIE.
3167 unsigned AbbrevNumber = Die->getAbbrevNumber();
3168 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3169
Bill Wendling94d04b82009-05-20 23:21:38 +00003170 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00003171 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00003172 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3173 Twine::utohexstr(Die->getOffset()) + ":0x" +
3174 Twine::utohexstr(Die->getSize()) + " " +
3175 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003176 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00003177
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00003178 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00003179 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3180
3181 // Emit the DIE attribute values.
3182 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3183 unsigned Attr = AbbrevData[i].getAttribute();
3184 unsigned Form = AbbrevData[i].getForm();
3185 assert(Form && "Too many attributes for DIE (check abbreviation)");
3186
Chris Lattner3f53c832010-04-04 18:52:31 +00003187 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00003188 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003189
Bill Wendling94d04b82009-05-20 23:21:38 +00003190 switch (Attr) {
3191 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00003192 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003193 break;
3194 case dwarf::DW_AT_abstract_origin: {
3195 DIEEntry *E = cast<DIEEntry>(Values[i]);
3196 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00003197 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00003198 Asm->EmitInt32(Addr);
3199 break;
3200 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003201 case dwarf::DW_AT_ranges: {
3202 // DW_AT_range Value encodes offset in debug_range section.
3203 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00003204
3205 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
3206 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
3207 V->getValue(),
3208 4);
3209 } else {
3210 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3211 V->getValue(),
3212 DwarfDebugRangeSectionSym,
3213 4);
3214 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003215 break;
3216 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003217 case dwarf::DW_AT_location: {
3218 if (UseDotDebugLocEntry.count(Die) != 0) {
3219 DIELabel *L = cast<DIELabel>(Values[i]);
3220 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3221 } else
3222 Values[i]->EmitValue(Asm, Form);
3223 break;
3224 }
Devang Patel2a361602010-09-29 19:08:08 +00003225 case dwarf::DW_AT_accessibility: {
3226 if (Asm->isVerbose()) {
3227 DIEInteger *V = cast<DIEInteger>(Values[i]);
3228 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
3229 }
3230 Values[i]->EmitValue(Asm, Form);
3231 break;
3232 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003233 default:
3234 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003235 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00003236 break;
3237 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003238 }
3239
3240 // Emit the DIE children if any.
3241 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3242 const std::vector<DIE *> &Children = Die->getChildren();
3243
3244 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00003245 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00003246
Chris Lattner3f53c832010-04-04 18:52:31 +00003247 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00003248 Asm->OutStreamer.AddComment("End Of Children Mark");
3249 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003250 }
3251}
3252
Devang Patel8a241142009-12-09 18:24:21 +00003253/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003254///
Devang Patel8a241142009-12-09 18:24:21 +00003255void DwarfDebug::emitDebugInfo() {
3256 // Start debug info section.
3257 Asm->OutStreamer.SwitchSection(
3258 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00003259 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3260 E = CUMap.end(); I != E; ++I) {
3261 CompileUnit *TheCU = I->second;
3262 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00003263
Devang Patel163a9f72010-05-10 22:49:55 +00003264 // Emit the compile units header.
3265 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3266 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003267
Devang Patel163a9f72010-05-10 22:49:55 +00003268 // Emit size of content not including length itself
3269 unsigned ContentSize = Die->getSize() +
3270 sizeof(int16_t) + // DWARF version number
3271 sizeof(int32_t) + // Offset Into Abbrev. Section
3272 sizeof(int8_t) + // Pointer Size (in bytes)
3273 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003274
Devang Patel163a9f72010-05-10 22:49:55 +00003275 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3276 Asm->EmitInt32(ContentSize);
3277 Asm->OutStreamer.AddComment("DWARF version number");
3278 Asm->EmitInt16(dwarf::DWARF_VERSION);
3279 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3280 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3281 DwarfAbbrevSectionSym);
3282 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3283 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003284
Devang Patel163a9f72010-05-10 22:49:55 +00003285 emitDIE(Die);
3286 // FIXME - extra padding for gdb bug.
3287 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3288 Asm->EmitInt8(0);
3289 Asm->EmitInt8(0);
3290 Asm->EmitInt8(0);
3291 Asm->EmitInt8(0);
3292 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3293 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003294}
3295
Devang Patel2c4ceb12009-11-21 02:48:08 +00003296/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003297///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003298void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00003299 // Check to see if it is worth the effort.
3300 if (!Abbreviations.empty()) {
3301 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003302 Asm->OutStreamer.SwitchSection(
3303 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003304
Chris Lattnerc0215722010-04-04 19:25:43 +00003305 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003306
3307 // For each abbrevation.
3308 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3309 // Get abbreviation data
3310 const DIEAbbrev *Abbrev = Abbreviations[i];
3311
3312 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003313 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00003314
3315 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003316 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00003317 }
3318
3319 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003320 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00003321
Chris Lattnerc0215722010-04-04 19:25:43 +00003322 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003323 }
3324}
3325
Devang Patel2c4ceb12009-11-21 02:48:08 +00003326/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00003327/// the line matrix.
3328///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003329void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003330 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00003331 Asm->OutStreamer.AddComment("Extended Op");
3332 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003333
Chris Lattner233f52b2010-03-09 23:52:58 +00003334 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003335 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003336 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3337 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3338
3339 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003340
Chris Lattnerc0215722010-04-04 19:25:43 +00003341 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003342 Asm->getTargetData().getPointerSize(),
3343 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003344
3345 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00003346 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3347 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00003348 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00003349 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003350}
3351
Devang Patel2c4ceb12009-11-21 02:48:08 +00003352/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00003353///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003354void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003355 // If the target is using .loc/.file, the assembler will be emitting the
3356 // .debug_line table automatically.
Chris Lattnerd38fee82010-04-05 00:13:49 +00003357 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00003358 return;
3359
3360 // Minimum line delta, thus ranging from -10..(255-10).
3361 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
3362 // Maximum line delta, thus ranging from -10..(255-10).
3363 const int MaxLineDelta = 255 + MinLineDelta;
3364
3365 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003366 Asm->OutStreamer.SwitchSection(
3367 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003368
3369 // Construct the section header.
Chris Lattner233f52b2010-03-09 23:52:58 +00003370 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnera6437182010-04-04 19:58:12 +00003371 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
3372 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00003373 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003374
Chris Lattner233f52b2010-03-09 23:52:58 +00003375 Asm->OutStreamer.AddComment("DWARF version number");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003376 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00003377
Chris Lattner233f52b2010-03-09 23:52:58 +00003378 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnera6437182010-04-04 19:58:12 +00003379 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
3380 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00003381 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003382
Chris Lattner233f52b2010-03-09 23:52:58 +00003383 Asm->OutStreamer.AddComment("Minimum Instruction Length");
3384 Asm->EmitInt8(1);
3385 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
3386 Asm->EmitInt8(1);
3387 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
3388 Asm->EmitInt8(MinLineDelta);
3389 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3390 Asm->EmitInt8(MaxLineDelta);
3391 Asm->OutStreamer.AddComment("Special Opcode Base");
3392 Asm->EmitInt8(-MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003393
3394 // Line number standard opcode encodings argument count
Chris Lattner233f52b2010-03-09 23:52:58 +00003395 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3396 Asm->EmitInt8(0);
3397 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3398 Asm->EmitInt8(1);
3399 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3400 Asm->EmitInt8(1);
3401 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3402 Asm->EmitInt8(1);
3403 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3404 Asm->EmitInt8(1);
3405 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3406 Asm->EmitInt8(0);
3407 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3408 Asm->EmitInt8(0);
3409 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3410 Asm->EmitInt8(0);
3411 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3412 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003413
3414 // Emit directories.
3415 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00003416 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner3f53c832010-04-04 18:52:31 +00003417 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003418 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003419 }
3420
Chris Lattner233f52b2010-03-09 23:52:58 +00003421 Asm->OutStreamer.AddComment("End of directories");
3422 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003423
3424 // Emit files.
3425 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3426 // Remember source id starts at 1.
3427 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003428 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner3f53c832010-04-04 18:52:31 +00003429 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003430 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003431
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003432 Asm->EmitULEB128(Id.first, "Directory #");
3433 Asm->EmitULEB128(0, "Mod date");
3434 Asm->EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00003435 }
3436
Chris Lattner233f52b2010-03-09 23:52:58 +00003437 Asm->OutStreamer.AddComment("End of files");
3438 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003439
Chris Lattnerc0215722010-04-04 19:25:43 +00003440 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003441
3442 // A sequence for each text section.
3443 unsigned SecSrcLinesSize = SectionSourceLines.size();
3444
3445 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3446 // Isolate current sections line info.
3447 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3448
Bill Wendling94d04b82009-05-20 23:21:38 +00003449 // Dwarf assumes we start with first line of first source file.
3450 unsigned Source = 1;
3451 unsigned Line = 1;
3452
3453 // Construct rows of the address, source, line, column matrix.
3454 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3455 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner25b68c62010-03-14 08:15:55 +00003456 MCSymbol *Label = LineInfo.getLabel();
Chris Lattnerb9130602010-03-14 02:20:58 +00003457 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling94d04b82009-05-20 23:21:38 +00003458
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003459 if (Asm->isVerbose()) {
Chris Lattner188a87d2010-03-10 01:04:13 +00003460 std::pair<unsigned, unsigned> SrcID =
Bill Wendling94d04b82009-05-20 23:21:38 +00003461 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner188a87d2010-03-10 01:04:13 +00003462 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner49f618a2010-03-10 02:29:31 +00003463 "/" +
3464 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner188a87d2010-03-10 01:04:13 +00003465 ":" + Twine(LineInfo.getLine()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003466 }
3467
3468 // Define the line address.
Chris Lattner233f52b2010-03-09 23:52:58 +00003469 Asm->OutStreamer.AddComment("Extended Op");
3470 Asm->EmitInt8(0);
3471 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003472 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003473
3474 Asm->OutStreamer.AddComment("DW_LNE_set_address");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003475 Asm->EmitInt8(dwarf::DW_LNE_set_address);
Chris Lattner233f52b2010-03-09 23:52:58 +00003476
3477 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003478 Asm->OutStreamer.EmitSymbolValue(Label,
3479 Asm->getTargetData().getPointerSize(),
Chris Lattnerb9130602010-03-14 02:20:58 +00003480 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003481
Bill Wendling94d04b82009-05-20 23:21:38 +00003482 // If change of source, then switch to the new source.
3483 if (Source != LineInfo.getSourceID()) {
3484 Source = LineInfo.getSourceID();
Chris Lattner233f52b2010-03-09 23:52:58 +00003485 Asm->OutStreamer.AddComment("DW_LNS_set_file");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003486 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003487 Asm->EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00003488 }
3489
3490 // If change of line.
3491 if (Line != LineInfo.getLine()) {
3492 // Determine offset.
3493 int Offset = LineInfo.getLine() - Line;
3494 int Delta = Offset - MinLineDelta;
3495
3496 // Update line.
3497 Line = LineInfo.getLine();
3498
3499 // If delta is small enough and in range...
3500 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3501 // ... then use fast opcode.
Chris Lattner233f52b2010-03-09 23:52:58 +00003502 Asm->OutStreamer.AddComment("Line Delta");
3503 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003504 } else {
3505 // ... otherwise use long hand.
Chris Lattner233f52b2010-03-09 23:52:58 +00003506 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling94d04b82009-05-20 23:21:38 +00003507 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003508 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner233f52b2010-03-09 23:52:58 +00003509 Asm->OutStreamer.AddComment("DW_LNS_copy");
3510 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003511 }
3512 } else {
3513 // Copy the previous row (different address or source)
Chris Lattner233f52b2010-03-09 23:52:58 +00003514 Asm->OutStreamer.AddComment("DW_LNS_copy");
3515 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003516 }
3517 }
3518
Devang Patel2c4ceb12009-11-21 02:48:08 +00003519 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003520 }
3521
3522 if (SecSrcLinesSize == 0)
3523 // Because we're emitting a debug_line section, we still need a line
3524 // table. The linker and friends expect it to exist. If there's nothing to
3525 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003526 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003527
Chris Lattnerc0215722010-04-04 19:25:43 +00003528 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003529}
3530
Devang Patel2c4ceb12009-11-21 02:48:08 +00003531/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003532///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003533void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003534 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003535 return;
3536
Chris Lattnerd38fee82010-04-05 00:13:49 +00003537 int stackGrowth = Asm->getTargetData().getPointerSize();
3538 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3539 TargetFrameInfo::StackGrowsDown)
3540 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003541
3542 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003543 Asm->OutStreamer.SwitchSection(
3544 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003545
Chris Lattnerc0215722010-04-04 19:25:43 +00003546 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003547 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003548 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3549 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003550
Chris Lattnerc0215722010-04-04 19:25:43 +00003551 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003552 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003553 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003554 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003555 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003556 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003557 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003558 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3559 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003560 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003561 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003562 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003563
3564 std::vector<MachineMove> Moves;
3565 RI->getInitialFrameState(Moves);
3566
Chris Lattner02b86b92010-04-04 23:41:46 +00003567 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003568
Chris Lattner059ea132010-04-28 01:05:45 +00003569 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003570 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003571}
3572
Devang Patel2c4ceb12009-11-21 02:48:08 +00003573/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003574/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003575void DwarfDebug::
3576emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003577 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003578 return;
3579
3580 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003581 Asm->OutStreamer.SwitchSection(
3582 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003583
Chris Lattner233f52b2010-03-09 23:52:58 +00003584 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003585 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003586 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003587 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003588 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003589 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003590
Chris Lattner206d61e2010-03-13 07:26:18 +00003591 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003592
Chris Lattner233f52b2010-03-09 23:52:58 +00003593 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003594 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner6189ed12010-04-04 23:25:33 +00003595 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003596
Chris Lattner233f52b2010-03-09 23:52:58 +00003597 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003598 MCSymbol *FuncBeginSym =
3599 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003600 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003601 Asm->getTargetData().getPointerSize(),
3602 0/*AddrSpace*/);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003603
3604
Chris Lattner233f52b2010-03-09 23:52:58 +00003605 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003606 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003607 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003608
Chris Lattner02b86b92010-04-04 23:41:46 +00003609 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003610
Chris Lattner059ea132010-04-28 01:05:45 +00003611 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003612 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003613}
3614
Devang Patel8a241142009-12-09 18:24:21 +00003615/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3616///
3617void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00003618 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3619 E = CUMap.end(); I != E; ++I) {
3620 CompileUnit *TheCU = I->second;
3621 // Start the dwarf pubnames section.
3622 Asm->OutStreamer.SwitchSection(
3623 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003624
Devang Patel163a9f72010-05-10 22:49:55 +00003625 Asm->OutStreamer.AddComment("Length of Public Names Info");
3626 Asm->EmitLabelDifference(
3627 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3628 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003629
Devang Patel163a9f72010-05-10 22:49:55 +00003630 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3631 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003632
Devang Patel163a9f72010-05-10 22:49:55 +00003633 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003634 Asm->EmitInt16(dwarf::DWARF_VERSION);
3635
Devang Patel163a9f72010-05-10 22:49:55 +00003636 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003637 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00003638 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003639
Devang Patel163a9f72010-05-10 22:49:55 +00003640 Asm->OutStreamer.AddComment("Compilation Unit Length");
3641 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3642 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3643 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003644
Devang Patel163a9f72010-05-10 22:49:55 +00003645 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3646 for (StringMap<DIE*>::const_iterator
3647 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3648 const char *Name = GI->getKeyData();
3649 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003650
Devang Patel163a9f72010-05-10 22:49:55 +00003651 Asm->OutStreamer.AddComment("DIE offset");
3652 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003653
Devang Patel163a9f72010-05-10 22:49:55 +00003654 if (Asm->isVerbose())
3655 Asm->OutStreamer.AddComment("External Name");
3656 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3657 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003658
Devang Patel163a9f72010-05-10 22:49:55 +00003659 Asm->OutStreamer.AddComment("End Mark");
3660 Asm->EmitInt32(0);
3661 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3662 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003663 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003664}
3665
Devang Patel193f7202009-11-24 01:14:22 +00003666void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00003667 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3668 E = CUMap.end(); I != E; ++I) {
3669 CompileUnit *TheCU = I->second;
3670 // Start the dwarf pubnames section.
3671 Asm->OutStreamer.SwitchSection(
3672 Asm->getObjFileLowering().getDwarfPubTypesSection());
3673 Asm->OutStreamer.AddComment("Length of Public Types Info");
3674 Asm->EmitLabelDifference(
3675 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3676 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003677
Devang Patel163a9f72010-05-10 22:49:55 +00003678 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3679 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003680
Devang Patel163a9f72010-05-10 22:49:55 +00003681 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3682 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003683
Devang Patel163a9f72010-05-10 22:49:55 +00003684 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3685 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3686 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003687
Devang Patel163a9f72010-05-10 22:49:55 +00003688 Asm->OutStreamer.AddComment("Compilation Unit Length");
3689 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3690 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3691 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003692
Devang Patel163a9f72010-05-10 22:49:55 +00003693 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3694 for (StringMap<DIE*>::const_iterator
3695 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3696 const char *Name = GI->getKeyData();
3697 DIE * Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003698
Devang Patel163a9f72010-05-10 22:49:55 +00003699 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3700 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003701
Devang Patel163a9f72010-05-10 22:49:55 +00003702 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3703 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3704 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00003705
Devang Patel163a9f72010-05-10 22:49:55 +00003706 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00003707 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00003708 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3709 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003710 }
Devang Patel193f7202009-11-24 01:14:22 +00003711}
3712
Devang Patel2c4ceb12009-11-21 02:48:08 +00003713/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003714///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003715void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003716 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003717 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003718
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003719 // Start the dwarf str section.
3720 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003721 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003722
Chris Lattnerbc733f52010-03-13 02:17:42 +00003723 // Get all of the string pool entries and put them in an array by their ID so
3724 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003725 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00003726 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003727
Chris Lattnerbc733f52010-03-13 02:17:42 +00003728 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3729 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3730 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00003731
Chris Lattnerbc733f52010-03-13 02:17:42 +00003732 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00003733
Chris Lattnerbc733f52010-03-13 02:17:42 +00003734 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003735 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003736 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00003737
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003738 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003739 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003740 }
3741}
3742
Devang Patel2c4ceb12009-11-21 02:48:08 +00003743/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003744///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003745void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00003746 if (DotDebugLocEntries.empty())
3747 return;
3748
Bill Wendling94d04b82009-05-20 23:21:38 +00003749 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003750 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00003751 Asm->getObjFileLowering().getDwarfLocSection());
3752 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00003753 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3754 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00003755 for (SmallVector<DotDebugLocEntry, 4>::iterator
3756 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00003757 I != E; ++I, ++index) {
Devang Patelc3f5f782010-05-25 23:40:22 +00003758 DotDebugLocEntry Entry = *I;
Devang Patelc3f5f782010-05-25 23:40:22 +00003759 if (Entry.isEmpty()) {
3760 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3761 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00003762 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00003763 } else {
3764 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3765 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3766 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3767 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patelb2cf5812010-08-04 18:40:52 +00003768 if (int Offset = Entry.Loc.getOffset()) {
3769 // If the value is at a certain offset from frame register then
3770 // use DW_OP_fbreg.
3771 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patelc3f5f782010-05-25 23:40:22 +00003772 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patelb2cf5812010-08-04 18:40:52 +00003773 Asm->EmitInt16(1 + OffsetSize);
3774 Asm->OutStreamer.AddComment(
3775 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
3776 Asm->EmitInt8(dwarf::DW_OP_fbreg);
3777 Asm->OutStreamer.AddComment("Offset");
3778 Asm->EmitSLEB128(Offset);
Devang Patelc3f5f782010-05-25 23:40:22 +00003779 } else {
Devang Patelb2cf5812010-08-04 18:40:52 +00003780 if (Reg < 32) {
3781 Asm->OutStreamer.AddComment("Loc expr size");
3782 Asm->EmitInt16(1);
3783 Asm->OutStreamer.AddComment(
Devang Patela54e0cc2010-08-04 20:32:36 +00003784 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patelb2cf5812010-08-04 18:40:52 +00003785 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3786 } else {
3787 Asm->OutStreamer.AddComment("Loc expr size");
3788 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
3789 Asm->EmitInt8(dwarf::DW_OP_regx);
3790 Asm->EmitULEB128(Reg);
3791 }
Devang Patelc3f5f782010-05-25 23:40:22 +00003792 }
3793 }
3794 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003795}
3796
3797/// EmitDebugARanges - Emit visible names into a debug aranges section.
3798///
3799void DwarfDebug::EmitDebugARanges() {
3800 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003801 Asm->OutStreamer.SwitchSection(
3802 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003803}
3804
Devang Patel2c4ceb12009-11-21 02:48:08 +00003805/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003806///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003807void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003808 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003809 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003810 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003811 unsigned char Size = Asm->getTargetData().getPointerSize();
3812 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00003813 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00003814 I != E; ++I) {
3815 if (*I)
3816 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003817 else
Devang Pateleac9c072010-04-27 19:46:33 +00003818 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003819 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003820}
3821
Devang Patel2c4ceb12009-11-21 02:48:08 +00003822/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003823///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003824void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003825 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003826 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003827 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003828 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003829 }
3830}
3831
Devang Patel2c4ceb12009-11-21 02:48:08 +00003832/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003833/// Section Header:
3834/// 1. length of section
3835/// 2. Dwarf version number
3836/// 3. address size.
3837///
3838/// Entries (one "entry" for each function that was inlined):
3839///
3840/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3841/// otherwise offset into __debug_str for regular function name.
3842/// 2. offset into __debug_str section for regular function name.
3843/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3844/// instances for the function.
3845///
3846/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3847/// inlined instance; the die_offset points to the inlined_subroutine die in the
3848/// __debug_info section, and the low_pc is the starting address for the
3849/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003850void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003851 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003852 return;
3853
Devang Patel163a9f72010-05-10 22:49:55 +00003854 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003855 return;
3856
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003857 Asm->OutStreamer.SwitchSection(
3858 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003859
Chris Lattner233f52b2010-03-09 23:52:58 +00003860 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003861 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3862 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003863
Chris Lattnerc0215722010-04-04 19:25:43 +00003864 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003865
Chris Lattner233f52b2010-03-09 23:52:58 +00003866 Asm->OutStreamer.AddComment("Dwarf Version");
3867 Asm->EmitInt16(dwarf::DWARF_VERSION);
3868 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003869 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003870
Devang Patele9f8f5e2010-05-07 20:54:48 +00003871 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003872 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003873
Devang Patele9f8f5e2010-05-07 20:54:48 +00003874 const MDNode *Node = *I;
3875 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003876 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003877 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003878 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003879 StringRef LName = SP.getLinkageName();
3880 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003881
Chris Lattner233f52b2010-03-09 23:52:58 +00003882 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003883 if (LName.empty()) {
3884 Asm->OutStreamer.EmitBytes(Name, 0);
3885 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00003886 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003887 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3888 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003889
Chris Lattner233f52b2010-03-09 23:52:58 +00003890 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003891 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003892 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003893
Devang Patel53bb5c92009-11-10 23:06:00 +00003894 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003895 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003896 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003897 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003898
Chris Lattner3f53c832010-04-04 18:52:31 +00003899 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003900 Asm->OutStreamer.EmitSymbolValue(LI->first,
3901 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003902 }
3903 }
3904
Chris Lattnerc0215722010-04-04 19:25:43 +00003905 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003906}