blob: f26047aee4349de7bbb05c9856184215370cc45a [file] [log] [blame]
Bill Wendling2f921f82009-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 Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling30346342010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greene829b3e82009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattnerf62e3ee2010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000032#include "llvm/Analysis/DebugInfo.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000033#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000034#include "llvm/ADT/StringExtras.h"
Devang Patel6c74a872010-04-27 19:46:33 +000035#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000038#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000040#include "llvm/Support/Timer.h"
41#include "llvm/System/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000042using namespace llvm;
43
Devang Patel6c74a872010-04-27 19:46:33 +000044static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
45 cl::desc("Print DbgScope information for each machine instruction"));
46
Jim Grosbacha8683bb2010-07-21 21:21:52 +000047static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000048 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000049 cl::desc("Disable debug info printing"));
50
Dan Gohman7421ae42010-05-07 01:08:53 +000051static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
52 cl::desc("Make an absense of debug location information explicit."),
53 cl::init(false));
54
Bill Wendlingfcc14142010-04-07 09:28:04 +000055namespace {
56 const char *DWARFGroupName = "DWARF Emission";
57 const char *DbgTimerName = "DWARF Debug Writer";
58} // end anonymous namespace
59
Bill Wendling2f921f82009-05-15 09:23:25 +000060//===----------------------------------------------------------------------===//
61
62/// Configuration values for initial hash set sizes (log2).
63///
Bill Wendling2f921f82009-05-15 09:23:25 +000064static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000065
66namespace llvm {
67
68//===----------------------------------------------------------------------===//
69/// CompileUnit - This dwarf writer support class manages information associate
70/// with a source file.
Nick Lewyckyb7993d62009-11-17 08:11:44 +000071class CompileUnit {
Bill Wendling2f921f82009-05-15 09:23:25 +000072 /// ID - File identifier for source.
73 ///
74 unsigned ID;
75
76 /// Die - Compile unit debug information entry.
77 ///
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000078 const OwningPtr<DIE> CUDie;
Bill Wendling2f921f82009-05-15 09:23:25 +000079
Jeffrey Yasskin0708de12010-03-11 18:29:55 +000080 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel92e8c652009-11-21 00:31:03 +000081 DIE *IndexTyDie;
82
Devang Patel9a0339f2010-07-07 20:49:57 +000083 /// MDNodeToDieMap - Tracks the mapping of unit level debug informaton
Bill Wendling2f921f82009-05-15 09:23:25 +000084 /// variables to debug information entries.
Devang Patel9a0339f2010-07-07 20:49:57 +000085 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
Bill Wendling2f921f82009-05-15 09:23:25 +000086
Devang Patel9a0339f2010-07-07 20:49:57 +000087 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug informaton
Bill Wendling2f921f82009-05-15 09:23:25 +000088 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel9a0339f2010-07-07 20:49:57 +000089 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
Bill Wendling2f921f82009-05-15 09:23:25 +000090
91 /// Globals - A map of globally visible named entities for this unit.
92 ///
93 StringMap<DIE*> Globals;
94
Devang Patel04d2f2d2009-11-24 01:14:22 +000095 /// GlobalTypes - A map of globally visible types for this unit.
96 ///
97 StringMap<DIE*> GlobalTypes;
98
Bill Wendling2f921f82009-05-15 09:23:25 +000099public:
100 CompileUnit(unsigned I, DIE *D)
Devang Patel930143b2009-11-21 02:48:08 +0000101 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000102
103 // Accessors.
Devang Patel04d2f2d2009-11-24 01:14:22 +0000104 unsigned getID() const { return ID; }
Jeffrey Yasskin0708de12010-03-11 18:29:55 +0000105 DIE* getCUDie() const { return CUDie.get(); }
Devang Patel04d2f2d2009-11-24 01:14:22 +0000106 const StringMap<DIE*> &getGlobals() const { return Globals; }
107 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000108
109 /// hasContent - Return true if this compile unit has something to write out.
110 ///
Devang Patel930143b2009-11-21 02:48:08 +0000111 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000112
Devang Patel930143b2009-11-21 02:48:08 +0000113 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling2f921f82009-05-15 09:23:25 +0000114 ///
Benjamin Kramer96956ed2010-03-31 20:15:45 +0000115 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000116
Devang Patel04d2f2d2009-11-24 01:14:22 +0000117 /// addGlobalType - Add a new global type to the compile unit.
118 ///
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000119 void addGlobalType(StringRef Name, DIE *Die) {
120 GlobalTypes[Name] = Die;
Devang Patel04d2f2d2009-11-24 01:14:22 +0000121 }
122
Devang Patele064ad42009-11-20 21:37:22 +0000123 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling2f921f82009-05-15 09:23:25 +0000124 /// specified debug variable.
Devang Patel9a0339f2010-07-07 20:49:57 +0000125 DIE *getDIE(const MDNode *N) { return MDNodeToDieMap.lookup(N); }
Jim Grosbach042483e2009-11-21 23:12:12 +0000126
Devang Patele064ad42009-11-20 21:37:22 +0000127 /// insertDIE - Insert DIE into the map.
Devang Patel32cc43c2010-05-07 20:54:48 +0000128 void insertDIE(const MDNode *N, DIE *D) {
Devang Patel9a0339f2010-07-07 20:49:57 +0000129 MDNodeToDieMap.insert(std::make_pair(N, D));
Devang Patele064ad42009-11-20 21:37:22 +0000130 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000131
Devang Patele064ad42009-11-20 21:37:22 +0000132 /// getDIEEntry - Returns the debug information entry for the speciefied
133 /// debug variable.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000134 DIEEntry *getDIEEntry(const MDNode *N) {
135 DenseMap<const MDNode *, DIEEntry *>::iterator I =
136 MDNodeToDIEEntryMap.find(N);
Devang Patel9a0339f2010-07-07 20:49:57 +0000137 if (I == MDNodeToDIEEntryMap.end())
Devang Patel1f4690c2009-12-15 19:16:48 +0000138 return NULL;
139 return I->second;
140 }
Devang Patele064ad42009-11-20 21:37:22 +0000141
142 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patel32cc43c2010-05-07 20:54:48 +0000143 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Patel9a0339f2010-07-07 20:49:57 +0000144 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling2f921f82009-05-15 09:23:25 +0000145 }
146
Devang Patel930143b2009-11-21 02:48:08 +0000147 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling2f921f82009-05-15 09:23:25 +0000148 ///
Devang Patel930143b2009-11-21 02:48:08 +0000149 void addDie(DIE *Buffer) {
150 this->CUDie->addChild(Buffer);
Bill Wendling2f921f82009-05-15 09:23:25 +0000151 }
Devang Patel92e8c652009-11-21 00:31:03 +0000152
153 // getIndexTyDie - Get an anonymous type for index type.
154 DIE *getIndexTyDie() {
155 return IndexTyDie;
156 }
157
Jim Grosbach00e9c612009-11-22 19:20:36 +0000158 // setIndexTyDie - Set D as anonymous type for index which can be reused
159 // later.
Devang Patel92e8c652009-11-21 00:31:03 +0000160 void setIndexTyDie(DIE *D) {
161 IndexTyDie = D;
162 }
163
Bill Wendling2f921f82009-05-15 09:23:25 +0000164};
165
166//===----------------------------------------------------------------------===//
167/// DbgVariable - This class is used to track local variable information.
168///
Devang Patelf3d7c082009-11-16 21:53:40 +0000169class DbgVariable {
Bill Wendling2f921f82009-05-15 09:23:25 +0000170 DIVariable Var; // Variable Descriptor.
Devang Patel9fc11702010-05-25 23:40:22 +0000171 DIE *TheDIE; // Variable DIE.
172 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
Bill Wendling2f921f82009-05-15 09:23:25 +0000173public:
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000174 // AbsVar may be NULL.
Devang Patel9fc11702010-05-25 23:40:22 +0000175 DbgVariable(DIVariable V) : Var(V), TheDIE(0), DotDebugLocOffset(~0U) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000176
177 // Accessors.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000178 DIVariable getVariable() const { return Var; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000179 void setDIE(DIE *D) { TheDIE = D; }
180 DIE *getDIE() const { return TheDIE; }
Devang Patel9fc11702010-05-25 23:40:22 +0000181 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
182 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000183 StringRef getName() const { return Var.getName(); }
184 unsigned getTag() const { return Var.getTag(); }
185 bool variableHasComplexAddress() const {
186 assert(Var.Verify() && "Invalid complex DbgVariable!");
187 return Var.hasComplexAddress();
188 }
189 bool isBlockByrefVariable() const {
190 assert(Var.Verify() && "Invalid complex DbgVariable!");
191 return Var.isBlockByrefVariable();
192 }
193 unsigned getNumAddrElements() const {
194 assert(Var.Verify() && "Invalid complex DbgVariable!");
195 return Var.getNumAddrElements();
196 }
197 uint64_t getAddrElement(unsigned i) const {
198 return Var.getAddrElement(i);
199 }
200 DIType getType() const {
201 DIType Ty = Var.getType();
202 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
203 // addresses instead.
204 if (Var.isBlockByrefVariable()) {
205 /* Byref variables, in Blocks, are declared by the programmer as
206 "SomeType VarName;", but the compiler creates a
207 __Block_byref_x_VarName struct, and gives the variable VarName
208 either the struct, or a pointer to the struct, as its type. This
209 is necessary for various behind-the-scenes things the compiler
210 needs to do with by-reference variables in blocks.
211
212 However, as far as the original *programmer* is concerned, the
213 variable should still have type 'SomeType', as originally declared.
214
215 The following function dives into the __Block_byref_x_VarName
216 struct to find the original type of the variable. This will be
217 passed back to the code generating the type for the Debug
218 Information Entry for the variable 'VarName'. 'VarName' will then
219 have the original type 'SomeType' in its debug information.
220
221 The original type 'SomeType' will be the type of the field named
222 'VarName' inside the __Block_byref_x_VarName struct.
223
224 NOTE: In order for this to not completely fail on the debugger
225 side, the Debug Information Entry for the variable VarName needs to
226 have a DW_AT_location that tells the debugger how to unwind through
227 the pointers and __Block_byref_x_VarName struct to find the actual
228 value of the variable. The function addBlockByrefType does this. */
229 DIType subType = Ty;
230 unsigned tag = Ty.getTag();
231
232 if (tag == dwarf::DW_TAG_pointer_type) {
233 DIDerivedType DTy = DIDerivedType(Ty);
234 subType = DTy.getTypeDerivedFrom();
235 }
236
237 DICompositeType blockStruct = DICompositeType(subType);
238 DIArray Elements = blockStruct.getTypeArray();
239
240 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
241 DIDescriptor Element = Elements.getElement(i);
242 DIDerivedType DT = DIDerivedType(Element);
243 if (getName() == DT.getName())
244 return (DT.getTypeDerivedFrom());
245 }
246 return Ty;
247 }
248 return Ty;
249 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000250};
251
252//===----------------------------------------------------------------------===//
Devang Patel6c74a872010-04-27 19:46:33 +0000253/// DbgRange - This is used to track range of instructions with identical
254/// debug info scope.
255///
256typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
257
258//===----------------------------------------------------------------------===//
Bill Wendling2f921f82009-05-15 09:23:25 +0000259/// DbgScope - This class is used to track scope information.
260///
Devang Patelf3d7c082009-11-16 21:53:40 +0000261class DbgScope {
Bill Wendling2f921f82009-05-15 09:23:25 +0000262 DbgScope *Parent; // Parent to this scope.
Jim Grosbach042483e2009-11-21 23:12:12 +0000263 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel1973df22010-01-26 21:39:14 +0000264 // Location at which this scope is inlined.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000265 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000266 bool AbstractScope; // Abstract Scope
Devang Patel787f94c2009-10-01 18:25:23 +0000267 const MachineInstr *LastInsn; // Last instruction of this scope.
268 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patel6c74a872010-04-27 19:46:33 +0000269 unsigned DFSIn, DFSOut;
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000270 // Scopes defined in scope. Contents not owned.
271 SmallVector<DbgScope *, 4> Scopes;
272 // Variables declared in scope. Contents owned.
273 SmallVector<DbgVariable *, 8> Variables;
Devang Patel6c74a872010-04-27 19:46:33 +0000274 SmallVector<DbgRange, 4> Ranges;
Owen Anderson9becc182009-06-24 22:53:20 +0000275 // Private state for dump()
276 mutable unsigned IndentLevel;
Bill Wendling2f921f82009-05-15 09:23:25 +0000277public:
Devang Patel32cc43c2010-05-07 20:54:48 +0000278 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000279 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patel6c74a872010-04-27 19:46:33 +0000280 LastInsn(0), FirstInsn(0),
281 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000282 virtual ~DbgScope();
283
284 // Accessors.
285 DbgScope *getParent() const { return Parent; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000286 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000287 DIDescriptor getDesc() const { return Desc; }
Devang Patel32cc43c2010-05-07 20:54:48 +0000288 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
289 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000290 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
Devang Patel406798a2010-08-09 18:51:29 +0000291 const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
Devang Patel6c74a872010-04-27 19:46:33 +0000292 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
293
294 /// openInsnRange - This scope covers instruction range starting from MI.
295 void openInsnRange(const MachineInstr *MI) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000296 if (!FirstInsn)
Devang Patel6c74a872010-04-27 19:46:33 +0000297 FirstInsn = MI;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000298
Devang Patel6c74a872010-04-27 19:46:33 +0000299 if (Parent)
300 Parent->openInsnRange(MI);
301 }
302
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000303 /// extendInsnRange - Extend the current instruction range covered by
Devang Patel6c74a872010-04-27 19:46:33 +0000304 /// this scope.
305 void extendInsnRange(const MachineInstr *MI) {
306 assert (FirstInsn && "MI Range is not open!");
307 LastInsn = MI;
308 if (Parent)
309 Parent->extendInsnRange(MI);
310 }
311
312 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
313 /// until now. This is used when a new scope is encountered while walking
314 /// machine instructions.
315 void closeInsnRange(DbgScope *NewScope = NULL) {
316 assert (LastInsn && "Last insn missing!");
317 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000318 FirstInsn = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +0000319 LastInsn = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000320 // If Parent dominates NewScope then do not close Parent's instruction
Devang Patel6c74a872010-04-27 19:46:33 +0000321 // range.
322 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
323 Parent->closeInsnRange(NewScope);
324 }
325
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000326 void setAbstractScope() { AbstractScope = true; }
327 bool isAbstractScope() const { return AbstractScope; }
Devang Patel6c74a872010-04-27 19:46:33 +0000328
329 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
330 unsigned getDFSOut() const { return DFSOut; }
331 void setDFSOut(unsigned O) { DFSOut = O; }
332 unsigned getDFSIn() const { return DFSIn; }
333 void setDFSIn(unsigned I) { DFSIn = I; }
334 bool dominates(const DbgScope *S) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000335 if (S == this)
Devang Patel6c74a872010-04-27 19:46:33 +0000336 return true;
337 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
338 return true;
339 return false;
340 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000341
Devang Patel930143b2009-11-21 02:48:08 +0000342 /// addScope - Add a scope to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000343 ///
Devang Patel930143b2009-11-21 02:48:08 +0000344 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000345
Devang Patel930143b2009-11-21 02:48:08 +0000346 /// addVariable - Add a variable to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000347 ///
Devang Patel930143b2009-11-21 02:48:08 +0000348 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000349
Bill Wendling2f921f82009-05-15 09:23:25 +0000350#ifndef NDEBUG
351 void dump() const;
352#endif
353};
Devang Patel6c74a872010-04-27 19:46:33 +0000354
Chris Lattnerf5d06362010-04-05 04:09:20 +0000355} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000356
357#ifndef NDEBUG
358void DbgScope::dump() const {
David Greenec230cb92009-12-24 00:31:35 +0000359 raw_ostream &err = dbgs();
Chris Lattner81e8e022009-08-23 00:51:00 +0000360 err.indent(IndentLevel);
Devang Patel32cc43c2010-05-07 20:54:48 +0000361 const MDNode *N = Desc;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000362 N->dump();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000363 if (AbstractScope)
364 err << "Abstract Scope\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000365
366 IndentLevel += 2;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000367 if (!Scopes.empty())
368 err << "Children ...\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000369 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
370 if (Scopes[i] != this)
371 Scopes[i]->dump();
372
373 IndentLevel -= 2;
374}
375#endif
376
Bill Wendling2f921f82009-05-15 09:23:25 +0000377DbgScope::~DbgScope() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000378 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
379 delete Variables[j];
Bill Wendling2f921f82009-05-15 09:23:25 +0000380}
381
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000382DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000383 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000384 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel12563b32010-04-16 23:33:45 +0000385 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000386 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000387
Chris Lattnere58b5472010-04-04 23:10:38 +0000388 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
389 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000390 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000391 FunctionBeginSym = FunctionEndSym = 0;
Devang Patel9c160e12010-07-08 20:10:35 +0000392 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Dan Gohman6e681a52010-06-18 15:56:31 +0000393 {
394 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
395 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000396 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000397}
398DwarfDebug::~DwarfDebug() {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000399 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
400 DIEBlocks[j]->~DIEBlock();
Bill Wendling2f921f82009-05-15 09:23:25 +0000401}
402
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000403MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
404 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
405 if (Entry.first) return Entry.first;
406
407 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000408 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000409}
410
411
Devang Patel930143b2009-11-21 02:48:08 +0000412/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000413///
Devang Patel930143b2009-11-21 02:48:08 +0000414void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000415 // Profile the node so that we can make it unique.
416 FoldingSetNodeID ID;
417 Abbrev.Profile(ID);
418
419 // Check the set for priors.
420 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
421
422 // If it's newly added.
423 if (InSet == &Abbrev) {
424 // Add to abbreviation list.
425 Abbreviations.push_back(&Abbrev);
426
427 // Assign the vector position + 1 as its number.
428 Abbrev.setNumber(Abbreviations.size());
429 } else {
430 // Assign existing abbreviation number.
431 Abbrev.setNumber(InSet->getNumber());
432 }
433}
434
Devang Patel930143b2009-11-21 02:48:08 +0000435/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendlingbcad77a2009-05-20 23:24:48 +0000436/// information entry.
Devang Patel930143b2009-11-21 02:48:08 +0000437DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000438 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000439 return Value;
440}
441
Devang Patel930143b2009-11-21 02:48:08 +0000442/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000443///
Devang Patel930143b2009-11-21 02:48:08 +0000444void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000445 unsigned Form, uint64_t Integer) {
446 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000447 DIEValue *Value = Integer == 1 ?
Devang Patel9c160e12010-07-08 20:10:35 +0000448 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000449 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000450}
451
Devang Patel930143b2009-11-21 02:48:08 +0000452/// addSInt - Add an signed integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000453///
Devang Patel930143b2009-11-21 02:48:08 +0000454void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000455 unsigned Form, int64_t Integer) {
456 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000457 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000458 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000459}
460
Devang Patel8c339592009-12-02 15:25:16 +0000461/// addString - Add a string attribute data and value. DIEString only
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000462/// keeps string reference.
Devang Patel930143b2009-11-21 02:48:08 +0000463void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerc3f23b82010-01-23 03:11:46 +0000464 StringRef String) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000465 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel930143b2009-11-21 02:48:08 +0000466 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000467}
468
Devang Patel930143b2009-11-21 02:48:08 +0000469/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000470///
Devang Patel930143b2009-11-21 02:48:08 +0000471void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000472 const MCSymbol *Label) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000473 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel930143b2009-11-21 02:48:08 +0000474 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000475}
476
Devang Patel930143b2009-11-21 02:48:08 +0000477/// addDelta - Add a label delta attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000478///
Devang Patel930143b2009-11-21 02:48:08 +0000479void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000480 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000481 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel930143b2009-11-21 02:48:08 +0000482 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000483}
484
Chris Lattner3f3fb972010-04-05 05:24:55 +0000485/// addDIEEntry - Add a DIE attribute data and value.
486///
487void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
488 DIE *Entry) {
489 Die->addValue(Attribute, Form, createDIEEntry(Entry));
490}
491
492
Devang Patel930143b2009-11-21 02:48:08 +0000493/// addBlock - Add block data.
Bill Wendling2f921f82009-05-15 09:23:25 +0000494///
Devang Patel930143b2009-11-21 02:48:08 +0000495void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000496 DIEBlock *Block) {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000497 Block->ComputeSize(Asm);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000498 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel930143b2009-11-21 02:48:08 +0000499 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000500}
501
Devang Patel930143b2009-11-21 02:48:08 +0000502/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000503/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000504void DwarfDebug::addSourceLine(DIE *Die, DIVariable V) {
Devang Patel0625af22010-05-07 23:33:41 +0000505 // Verify variable.
Devang Patelb6511a32010-08-09 20:20:05 +0000506 if (!V.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000507 return;
508
Devang Patelb6511a32010-08-09 20:20:05 +0000509 unsigned Line = V.getLineNumber();
510 unsigned FileID = GetOrCreateSourceID(V.getContext().getDirectory(),
511 V.getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000512 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000513 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
514 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000515}
516
Devang Patel930143b2009-11-21 02:48:08 +0000517/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000518/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000519void DwarfDebug::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patel0625af22010-05-07 23:33:41 +0000520 // Verify global variable.
Devang Patelb6511a32010-08-09 20:20:05 +0000521 if (!G.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000522 return;
523
Devang Patelb6511a32010-08-09 20:20:05 +0000524 unsigned Line = G.getLineNumber();
525 unsigned FileID = GetOrCreateSourceID(G.getContext().getDirectory(),
526 G.getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000527 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000528 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
529 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000530}
Devang Patelb2de5fa2009-08-31 22:47:13 +0000531
Devang Patel930143b2009-11-21 02:48:08 +0000532/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000533/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000534void DwarfDebug::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patel0625af22010-05-07 23:33:41 +0000535 // Verify subprogram.
Devang Patelb6511a32010-08-09 20:20:05 +0000536 if (!SP.Verify())
Devang Patelb2de5fa2009-08-31 22:47:13 +0000537 return;
Caroline Tice183a5192009-09-11 18:25:54 +0000538 // If the line number is 0, don't add it.
Devang Patelb6511a32010-08-09 20:20:05 +0000539 if (SP.getLineNumber() == 0)
Caroline Tice183a5192009-09-11 18:25:54 +0000540 return;
541
Devang Patelb6511a32010-08-09 20:20:05 +0000542 unsigned Line = SP.getLineNumber();
543 if (!SP.getContext().Verify())
Devang Patel8119fe872010-03-08 22:02:50 +0000544 return;
Devang Patelb6511a32010-08-09 20:20:05 +0000545 unsigned FileID = GetOrCreateSourceID(SP.getDirectory(),
546 SP.getFilename());
Devang Patelb2de5fa2009-08-31 22:47:13 +0000547 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000548 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
549 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patelb2de5fa2009-08-31 22:47:13 +0000550}
551
Devang Patel930143b2009-11-21 02:48:08 +0000552/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000553/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000554void DwarfDebug::addSourceLine(DIE *Die, DIType Ty) {
Devang Patel0625af22010-05-07 23:33:41 +0000555 // Verify type.
Devang Patelb6511a32010-08-09 20:20:05 +0000556 if (!Ty.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000557 return;
558
Devang Patelb6511a32010-08-09 20:20:05 +0000559 unsigned Line = Ty.getLineNumber();
560 if (!Ty.getContext().Verify())
Devang Patel8119fe872010-03-08 22:02:50 +0000561 return;
Devang Patelb6511a32010-08-09 20:20:05 +0000562 unsigned FileID = GetOrCreateSourceID(Ty.getContext().getDirectory(),
563 Ty.getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000564 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000565 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
566 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000567}
568
Devang Patel1f4690c2009-12-15 19:16:48 +0000569/// addSourceLine - Add location information to specified debug information
570/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000571void DwarfDebug::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patel0625af22010-05-07 23:33:41 +0000572 // Verify namespace.
Devang Patelb6511a32010-08-09 20:20:05 +0000573 if (!NS.Verify())
Devang Patel1f4690c2009-12-15 19:16:48 +0000574 return;
575
Devang Patelb6511a32010-08-09 20:20:05 +0000576 unsigned Line = NS.getLineNumber();
577 StringRef FN = NS.getFilename();
578 StringRef Dir = NS.getDirectory();
Devang Patel1f4690c2009-12-15 19:16:48 +0000579
580 unsigned FileID = GetOrCreateSourceID(Dir, FN);
581 assert(FileID && "Invalid file id");
582 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
583 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
584}
585
Devang Patel2cfc3af2010-08-31 06:11:28 +0000586/// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
587/// on provided frame index.
588void DwarfDebug::addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI) {
589 MachineLocation Location;
590 unsigned FrameReg;
591 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
592 int Offset = RI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
593 Location.set(FrameReg, Offset);
594
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000595 if (DV->variableHasComplexAddress())
596 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
597 else if (DV->isBlockByrefVariable())
598 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
599 else
600 addAddress(Die, dwarf::DW_AT_location, Location);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000601}
602
Devang Patel930143b2009-11-21 02:48:08 +0000603/// addComplexAddress - Start with the address based on the location provided,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000604/// and generate the DWARF information necessary to find the actual variable
605/// given the extra address information encoded in the DIVariable, starting from
606/// the starting location. Add the DWARF information to the die.
607///
Devang Patel930143b2009-11-21 02:48:08 +0000608void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000609 unsigned Attribute,
610 const MachineLocation &Location) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000611 DIType Ty = DV->getType();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000612
613 // Decode the original location, and use that as the start of the byref
614 // variable's location.
Chris Lattner3a383cb2010-04-05 00:13:49 +0000615 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000616 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000617 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000618
619 if (Location.isReg()) {
620 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000621 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000622 } else {
623 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-11-21 02:48:08 +0000624 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
625 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000626 }
627 } else {
628 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000629 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000630 else {
Devang Patel930143b2009-11-21 02:48:08 +0000631 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
632 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000633 }
634
Devang Patel930143b2009-11-21 02:48:08 +0000635 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump14cf8ec2009-09-30 00:08:22 +0000636 }
637
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000638 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
639 uint64_t Element = DV->getAddrElement(i);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000640
641 if (Element == DIFactory::OpPlus) {
Devang Patel930143b2009-11-21 02:48:08 +0000642 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000643 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
Mike Stump14cf8ec2009-09-30 00:08:22 +0000644 } else if (Element == DIFactory::OpDeref) {
Devang Patel930143b2009-11-21 02:48:08 +0000645 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000646 } else llvm_unreachable("unknown DIFactory Opcode");
647 }
648
649 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000650 addBlock(Die, Attribute, 0, Block);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000651}
652
Caroline Ticec87c1e22009-08-31 21:19:37 +0000653/* Byref variables, in Blocks, are declared by the programmer as "SomeType
654 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
655 gives the variable VarName either the struct, or a pointer to the struct, as
656 its type. This is necessary for various behind-the-scenes things the
657 compiler needs to do with by-reference variables in Blocks.
658
659 However, as far as the original *programmer* is concerned, the variable
660 should still have type 'SomeType', as originally declared.
661
Devang Patel930143b2009-11-21 02:48:08 +0000662 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000663 struct to find the original type of the variable, which is then assigned to
664 the variable's Debug Information Entry as its real type. So far, so good.
665 However now the debugger will expect the variable VarName to have the type
666 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000667 expression that explains to the debugger how to navigate through the
Caroline Ticec87c1e22009-08-31 21:19:37 +0000668 pointers and struct to find the actual variable of type SomeType.
669
670 The following function does just that. We start by getting
671 the "normal" location for the variable. This will be the location
672 of either the struct __Block_byref_x_VarName or the pointer to the
673 struct __Block_byref_x_VarName.
674
675 The struct will look something like:
676
677 struct __Block_byref_x_VarName {
678 ... <various fields>
679 struct __Block_byref_x_VarName *forwarding;
680 ... <various other fields>
681 SomeType VarName;
682 ... <maybe more fields>
683 };
684
685 If we are given the struct directly (as our starting point) we
686 need to tell the debugger to:
687
688 1). Add the offset of the forwarding field.
689
Dan Gohman4a618822010-02-10 16:03:48 +0000690 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000691 struct to use (the real one may have been copied onto the heap).
692
693 3). Add the offset for the field VarName, to find the actual variable.
694
695 If we started with a pointer to the struct, then we need to
696 dereference that pointer first, before the other steps.
697 Translating this into DWARF ops, we will need to append the following
698 to the current location description for the variable:
699
700 DW_OP_deref -- optional, if we start with a pointer
701 DW_OP_plus_uconst <forward_fld_offset>
702 DW_OP_deref
703 DW_OP_plus_uconst <varName_fld_offset>
704
705 That is what this function does. */
706
Devang Patel930143b2009-11-21 02:48:08 +0000707/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticec87c1e22009-08-31 21:19:37 +0000708/// provided, and generate the DWARF information necessary to find the
709/// actual Block variable (navigating the Block struct) based on the
710/// starting location. Add the DWARF information to the die. For
711/// more information, read large comment just above here.
712///
Devang Patel930143b2009-11-21 02:48:08 +0000713void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000714 unsigned Attribute,
715 const MachineLocation &Location) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000716 DIType Ty = DV->getType();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000717 DIType TmpTy = Ty;
718 unsigned Tag = Ty.getTag();
719 bool isPointer = false;
720
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000721 StringRef varName = DV->getName();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000722
723 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000724 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000725 TmpTy = DTy.getTypeDerivedFrom();
726 isPointer = true;
727 }
728
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000729 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000730
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000731 // Find the __forwarding field and the variable field in the __Block_byref
732 // struct.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000733 DIArray Fields = blockStruct.getTypeArray();
734 DIDescriptor varField = DIDescriptor();
735 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000736
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000737 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
738 DIDescriptor Element = Fields.getElement(i);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000739 DIDerivedType DT = DIDerivedType(Element);
Devang Patel2d9caf92009-11-25 17:36:49 +0000740 StringRef fieldName = DT.getName();
741 if (fieldName == "__forwarding")
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000742 forwardingField = Element;
Devang Patel2d9caf92009-11-25 17:36:49 +0000743 else if (fieldName == varName)
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000744 varField = Element;
745 }
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000746
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000747 // Get the offsets for the forwarding field and the variable field.
Chris Lattner71696ef2010-03-31 06:06:37 +0000748 unsigned forwardingFieldOffset =
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000749 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner71696ef2010-03-31 06:06:37 +0000750 unsigned varFieldOffset =
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000751 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticec87c1e22009-08-31 21:19:37 +0000752
Mike Stump944fa252009-09-24 23:21:26 +0000753 // Decode the original location, and use that as the start of the byref
754 // variable's location.
Chris Lattner3a383cb2010-04-05 00:13:49 +0000755 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000756 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000757 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000758
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000759 if (Location.isReg()) {
760 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000761 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000762 else {
763 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-11-21 02:48:08 +0000764 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
765 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000766 }
767 } else {
768 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000769 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000770 else {
Devang Patel930143b2009-11-21 02:48:08 +0000771 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
772 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000773 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000774
Devang Patel930143b2009-11-21 02:48:08 +0000775 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000776 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000777
Mike Stump944fa252009-09-24 23:21:26 +0000778 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000779 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000780 if (isPointer)
Devang Patel930143b2009-11-21 02:48:08 +0000781 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000782
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000783 // Next add the offset for the '__forwarding' field:
784 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
785 // adding the offset if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000786 if (forwardingFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000787 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
788 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000789 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000790
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000791 // Now dereference the __forwarding field to get to the real __Block_byref
792 // struct: DW_OP_deref.
Devang Patel930143b2009-11-21 02:48:08 +0000793 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000794
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000795 // Now that we've got the real __Block_byref... struct, add the offset
796 // for the variable's field to get to the location of the actual variable:
797 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000798 if (varFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000799 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
800 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000801 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000802
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000803 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000804 addBlock(Die, Attribute, 0, Block);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000805}
806
Devang Patel930143b2009-11-21 02:48:08 +0000807/// addAddress - Add an address attribute to a die based on the location
Bill Wendling2f921f82009-05-15 09:23:25 +0000808/// provided.
Devang Patel930143b2009-11-21 02:48:08 +0000809void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000810 const MachineLocation &Location) {
Chris Lattner3a383cb2010-04-05 00:13:49 +0000811 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling2f921f82009-05-15 09:23:25 +0000812 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000813 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel804fcd42010-09-22 21:10:38 +0000814 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
815
816 if (TRI->getFrameRegister(*Asm->MF) == Location.getReg()
817 && Location.getOffset()) {
818 // If variable offset is based in frame register then use fbreg.
819 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
820 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
821 addBlock(Die, Attribute, 0, Block);
822 return;
823 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000824
825 if (Location.isReg()) {
826 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000827 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000828 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000829 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
830 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000831 }
832 } else {
833 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000834 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000835 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000836 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
837 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000838 }
839
Devang Patel930143b2009-11-21 02:48:08 +0000840 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling2f921f82009-05-15 09:23:25 +0000841 }
842
Devang Patel930143b2009-11-21 02:48:08 +0000843 addBlock(Die, Attribute, 0, Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000844}
845
Devang Patel173b2b92010-04-28 01:03:09 +0000846/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patele1c53f22010-05-20 16:36:41 +0000847bool DwarfDebug::addRegisterAddress(DIE *Die, const MCSymbol *VS,
Devang Patel173b2b92010-04-28 01:03:09 +0000848 const MachineOperand &MO) {
849 assert (MO.isReg() && "Invalid machine operand!");
850 if (!MO.getReg())
851 return false;
852 MachineLocation Location;
853 Location.set(MO.getReg());
854 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patele1c53f22010-05-20 16:36:41 +0000855 if (VS)
Devang Patel173b2b92010-04-28 01:03:09 +0000856 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
857 return true;
858}
859
860/// addConstantValue - Add constant value entry in variable DIE.
Devang Patele1c53f22010-05-20 16:36:41 +0000861bool DwarfDebug::addConstantValue(DIE *Die, const MCSymbol *VS,
Devang Patel173b2b92010-04-28 01:03:09 +0000862 const MachineOperand &MO) {
863 assert (MO.isImm() && "Invalid machine operand!");
864 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
865 unsigned Imm = MO.getImm();
866 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
867 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patele1c53f22010-05-20 16:36:41 +0000868 if (VS)
Devang Patel173b2b92010-04-28 01:03:09 +0000869 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
870 return true;
871}
872
873/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patele1c53f22010-05-20 16:36:41 +0000874bool DwarfDebug::addConstantFPValue(DIE *Die, const MCSymbol *VS,
Devang Patel173b2b92010-04-28 01:03:09 +0000875 const MachineOperand &MO) {
876 assert (MO.isFPImm() && "Invalid machine operand!");
877 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
878 APFloat FPImm = MO.getFPImm()->getValueAPF();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000879
Devang Patel173b2b92010-04-28 01:03:09 +0000880 // Get the raw data form of the floating point.
881 const APInt FltVal = FPImm.bitcastToAPInt();
882 const char *FltPtr = (const char*)FltVal.getRawData();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000883
Devang Patel173b2b92010-04-28 01:03:09 +0000884 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
885 bool LittleEndian = Asm->getTargetData().isLittleEndian();
886 int Incr = (LittleEndian ? 1 : -1);
887 int Start = (LittleEndian ? 0 : NumBytes - 1);
888 int Stop = (LittleEndian ? NumBytes : -1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000889
Devang Patel173b2b92010-04-28 01:03:09 +0000890 // Output the constant to DWARF one byte at a time.
891 for (; Start != Stop; Start += Incr)
892 addUInt(Block, 0, dwarf::DW_FORM_data1,
893 (unsigned char)0xFF & FltPtr[Start]);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000894
Devang Patel173b2b92010-04-28 01:03:09 +0000895 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patele1c53f22010-05-20 16:36:41 +0000896 if (VS)
897 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000898 return true;
Devang Patel173b2b92010-04-28 01:03:09 +0000899}
900
901
Devang Patel2b75ed22009-12-10 19:14:49 +0000902/// addToContextOwner - Add Die into the list of its context owner's children.
903void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000904 if (Context.isType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000905 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patel2b75ed22009-12-10 19:14:49 +0000906 ContextDIE->addChild(Die);
Devang Patel1f4690c2009-12-15 19:16:48 +0000907 } else if (Context.isNameSpace()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000908 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel1f4690c2009-12-15 19:16:48 +0000909 ContextDIE->addChild(Die);
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000910 } else if (Context.isSubprogram()) {
Devang Patel185051c2010-09-27 23:15:27 +0000911 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context));
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000912 ContextDIE->addChild(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000913 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patel2b75ed22009-12-10 19:14:49 +0000914 ContextDIE->addChild(Die);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000915 else
Devang Patel1a0df9a2010-05-10 22:49:55 +0000916 getCompileUnit(Context)->addDie(Die);
Devang Patel2b75ed22009-12-10 19:14:49 +0000917}
918
Devang Patelb5b60ea2009-12-10 18:05:33 +0000919/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
920/// given DIType.
921DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000922 CompileUnit *TypeCU = getCompileUnit(Ty);
923 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patelb5b60ea2009-12-10 18:05:33 +0000924 if (TyDIE)
925 return TyDIE;
926
927 // Create new type.
928 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000929 TypeCU->insertDIE(Ty, TyDIE);
Devang Patelb5b60ea2009-12-10 18:05:33 +0000930 if (Ty.isBasicType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000931 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000932 else if (Ty.isCompositeType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000933 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000934 else {
935 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000936 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000937 }
938
Devang Patel2b75ed22009-12-10 19:14:49 +0000939 addToContextOwner(TyDIE, Ty.getContext());
Devang Patelb5b60ea2009-12-10 18:05:33 +0000940 return TyDIE;
941}
942
Devang Patel930143b2009-11-21 02:48:08 +0000943/// addType - Add a new type attribute to the specified entity.
Devang Patel9ccfb642009-12-09 18:24:21 +0000944void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel8d6a2b72010-05-07 21:45:47 +0000945 if (!Ty.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000946 return;
947
948 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000949 CompileUnit *TypeCU = getCompileUnit(Ty);
950 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling2f921f82009-05-15 09:23:25 +0000951 // If it exists then use the existing value.
Devang Patel930143b2009-11-21 02:48:08 +0000952 if (Entry) {
953 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000954 return;
955 }
956
Bill Wendling2f921f82009-05-15 09:23:25 +0000957 // Construct type.
Devang Patelb5b60ea2009-12-10 18:05:33 +0000958 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling2f921f82009-05-15 09:23:25 +0000959
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000960 // Set up proxy.
961 Entry = createDIEEntry(Buffer);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000962 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000963
Devang Patel930143b2009-11-21 02:48:08 +0000964 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000965}
966
Devang Patel930143b2009-11-21 02:48:08 +0000967/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000968void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000969 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000970 StringRef Name = BTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000971 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +0000972 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling2f921f82009-05-15 09:23:25 +0000973 BTy.getEncoding());
974
975 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +0000976 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000977 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000978 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel930143b2009-11-21 02:48:08 +0000979 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000980}
981
Devang Patel930143b2009-11-21 02:48:08 +0000982/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000983void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000984 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000985 StringRef Name = DTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000986 uint64_t Size = DTy.getSizeInBits() >> 3;
987 unsigned Tag = DTy.getTag();
988
989 // FIXME - Workaround for templates.
990 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
991
992 Buffer.setTag(Tag);
993
994 // Map to main type, void will not have a type.
995 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel9ccfb642009-12-09 18:24:21 +0000996 addType(&Buffer, FromTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000997
998 // Add name if not anonymous or intermediate type.
Devang Patelae466ef2009-11-30 23:56:56 +0000999 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001000 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +00001001
1002 // Add size if non-zero (derived types might be zero-sized.)
1003 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +00001004 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +00001005
1006 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb5b51592009-11-23 18:43:37 +00001007 if (!DTy.isForwardDecl())
Devang Patelb6511a32010-08-09 20:20:05 +00001008 addSourceLine(&Buffer, DTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001009}
1010
Devang Patel930143b2009-11-21 02:48:08 +00001011/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +00001012void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001013 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +00001014 StringRef Name = CTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +00001015
1016 uint64_t Size = CTy.getSizeInBits() >> 3;
1017 unsigned Tag = CTy.getTag();
1018 Buffer.setTag(Tag);
1019
1020 switch (Tag) {
1021 case dwarf::DW_TAG_vector_type:
1022 case dwarf::DW_TAG_array_type:
Devang Patel9ccfb642009-12-09 18:24:21 +00001023 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001024 break;
1025 case dwarf::DW_TAG_enumeration_type: {
1026 DIArray Elements = CTy.getTypeArray();
1027
1028 // Add enumerators to enumeration type.
1029 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1030 DIE *ElemDie = NULL;
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001031 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3b548aa2010-03-08 20:52:55 +00001032 if (Enum.isEnumerator()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001033 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel930143b2009-11-21 02:48:08 +00001034 Buffer.addChild(ElemDie);
Devang Patelfafa1fe2009-10-09 17:51:49 +00001035 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001036 }
1037 }
1038 break;
1039 case dwarf::DW_TAG_subroutine_type: {
1040 // Add return type.
1041 DIArray Elements = CTy.getTypeArray();
1042 DIDescriptor RTy = Elements.getElement(0);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001043 addType(&Buffer, DIType(RTy));
Bill Wendling2f921f82009-05-15 09:23:25 +00001044
1045 // Add prototype flag.
Devang Patel930143b2009-11-21 02:48:08 +00001046 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001047
1048 // Add arguments.
1049 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1050 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1051 DIDescriptor Ty = Elements.getElement(i);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001052 addType(Arg, DIType(Ty));
Devang Patel930143b2009-11-21 02:48:08 +00001053 Buffer.addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +00001054 }
1055 }
1056 break;
1057 case dwarf::DW_TAG_structure_type:
1058 case dwarf::DW_TAG_union_type:
1059 case dwarf::DW_TAG_class_type: {
1060 // Add elements to structure type.
1061 DIArray Elements = CTy.getTypeArray();
1062
1063 // A forward struct declared type may not have elements available.
Devang Patel3b548aa2010-03-08 20:52:55 +00001064 unsigned N = Elements.getNumElements();
1065 if (N == 0)
Bill Wendling2f921f82009-05-15 09:23:25 +00001066 break;
1067
1068 // Add elements to structure type.
Devang Patel3b548aa2010-03-08 20:52:55 +00001069 for (unsigned i = 0; i < N; ++i) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001070 DIDescriptor Element = Elements.getElement(i);
1071 DIE *ElemDie = NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001072 if (Element.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001073 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel3b548aa2010-03-08 20:52:55 +00001074 else if (Element.isVariable()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001075 DIVariable DV(Element);
Devang Patel160c92d2010-01-30 01:08:30 +00001076 ElemDie = new DIE(dwarf::DW_TAG_variable);
1077 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1078 DV.getName());
1079 addType(ElemDie, DV.getType());
1080 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1081 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patelb6511a32010-08-09 20:20:05 +00001082 addSourceLine(ElemDie, DV);
Devang Patel3b548aa2010-03-08 20:52:55 +00001083 } else if (Element.isDerivedType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001084 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3b548aa2010-03-08 20:52:55 +00001085 else
1086 continue;
Devang Patel930143b2009-11-21 02:48:08 +00001087 Buffer.addChild(ElemDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001088 }
1089
Devang Patel3082c012009-08-27 23:51:51 +00001090 if (CTy.isAppleBlockExtension())
Devang Patel930143b2009-11-21 02:48:08 +00001091 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001092
1093 unsigned RLang = CTy.getRunTimeLang();
1094 if (RLang)
Devang Patel930143b2009-11-21 02:48:08 +00001095 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling2f921f82009-05-15 09:23:25 +00001096 dwarf::DW_FORM_data1, RLang);
Devang Patel303a1be2010-01-26 21:16:06 +00001097
1098 DICompositeType ContainingType = CTy.getContainingType();
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001099 if (DIDescriptor(ContainingType).isCompositeType())
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001100 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001101 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastingsafe54f12010-06-11 20:08:44 +00001102 else {
1103 DIDescriptor Context = CTy.getContext();
1104 addToContextOwner(&Buffer, Context);
1105 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001106 break;
1107 }
1108 default:
1109 break;
1110 }
1111
1112 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +00001113 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001114 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +00001115
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001116 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
Devang Patel30265c42010-07-07 20:12:52 +00001117 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1118 {
Bill Wendling2f921f82009-05-15 09:23:25 +00001119 // Add size if non-zero (derived types might be zero-sized.)
1120 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +00001121 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +00001122 else {
1123 // Add zero size if it is not a forward declaration.
1124 if (CTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +00001125 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001126 else
Devang Patel930143b2009-11-21 02:48:08 +00001127 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling2f921f82009-05-15 09:23:25 +00001128 }
1129
1130 // Add source line info if available.
1131 if (!CTy.isForwardDecl())
Devang Patelb6511a32010-08-09 20:20:05 +00001132 addSourceLine(&Buffer, CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001133 }
1134}
1135
Devang Patel930143b2009-11-21 02:48:08 +00001136/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1137void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling2f921f82009-05-15 09:23:25 +00001138 int64_t L = SR.getLo();
1139 int64_t H = SR.getHi();
1140 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1141
Devang Patel930143b2009-11-21 02:48:08 +00001142 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patelf691df32009-08-14 20:59:16 +00001143 if (L)
Devang Patel930143b2009-11-21 02:48:08 +00001144 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patel8f046022009-12-04 23:10:24 +00001145 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling2f921f82009-05-15 09:23:25 +00001146
Devang Patel930143b2009-11-21 02:48:08 +00001147 Buffer.addChild(DW_Subrange);
Bill Wendling2f921f82009-05-15 09:23:25 +00001148}
1149
Devang Patel930143b2009-11-21 02:48:08 +00001150/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +00001151void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling2f921f82009-05-15 09:23:25 +00001152 DICompositeType *CTy) {
1153 Buffer.setTag(dwarf::DW_TAG_array_type);
1154 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel930143b2009-11-21 02:48:08 +00001155 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001156
1157 // Emit derived type.
Devang Patel9ccfb642009-12-09 18:24:21 +00001158 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001159 DIArray Elements = CTy->getTypeArray();
1160
Devang Patel92e8c652009-11-21 00:31:03 +00001161 // Get an anonymous type for index type.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001162 CompileUnit *TheCU = getCompileUnit(*CTy);
1163 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel92e8c652009-11-21 00:31:03 +00001164 if (!IdxTy) {
1165 // Construct an anonymous type for index type.
1166 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +00001167 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1168 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel92e8c652009-11-21 00:31:03 +00001169 dwarf::DW_ATE_signed);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001170 TheCU->addDie(IdxTy);
1171 TheCU->setIndexTyDie(IdxTy);
Devang Patel92e8c652009-11-21 00:31:03 +00001172 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001173
1174 // Add subranges to array type.
1175 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1176 DIDescriptor Element = Elements.getElement(i);
1177 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001178 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001179 }
1180}
1181
Devang Patel930143b2009-11-21 02:48:08 +00001182/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3b548aa2010-03-08 20:52:55 +00001183DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001184 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3b548aa2010-03-08 20:52:55 +00001185 StringRef Name = ETy.getName();
Devang Patel930143b2009-11-21 02:48:08 +00001186 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3b548aa2010-03-08 20:52:55 +00001187 int64_t Value = ETy.getEnumValue();
Devang Patel930143b2009-11-21 02:48:08 +00001188 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +00001189 return Enumerator;
1190}
1191
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001192/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-01-05 01:46:14 +00001193/// printer to not emit usual symbol prefix before the symbol name is used then
1194/// return linkage name after skipping this special LLVM prefix.
1195static StringRef getRealLinkageName(StringRef LinkageName) {
1196 char One = '\1';
1197 if (LinkageName.startswith(StringRef(&One, 1)))
1198 return LinkageName.substr(1);
1199 return LinkageName;
1200}
1201
Devang Patel930143b2009-11-21 02:48:08 +00001202/// createMemberDIE - Create new member DIE.
Devang Patel18ba0b42010-08-10 04:12:17 +00001203DIE *DwarfDebug::createMemberDIE(DIDerivedType DT) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001204 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel2d9caf92009-11-25 17:36:49 +00001205 StringRef Name = DT.getName();
1206 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001207 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001208
Devang Patel9ccfb642009-12-09 18:24:21 +00001209 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001210
Devang Patelb6511a32010-08-09 20:20:05 +00001211 addSourceLine(MemberDie, DT);
Bill Wendling2f921f82009-05-15 09:23:25 +00001212
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001213 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel930143b2009-11-21 02:48:08 +00001214 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel67f56f02009-11-04 22:06:12 +00001215
Bill Wendling2f921f82009-05-15 09:23:25 +00001216 uint64_t Size = DT.getSizeInBits();
Devang Patelf05d5722009-11-04 23:48:00 +00001217 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling2f921f82009-05-15 09:23:25 +00001218
1219 if (Size != FieldSize) {
1220 // Handle bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001221 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1222 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling2f921f82009-05-15 09:23:25 +00001223
1224 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling2f921f82009-05-15 09:23:25 +00001225 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1226 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer2b459982010-01-07 17:50:57 +00001227 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling2f921f82009-05-15 09:23:25 +00001228 Offset -= FieldOffset;
1229
1230 // Maybe we need to work from the other end.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001231 if (Asm->getTargetData().isLittleEndian())
1232 Offset = FieldSize - (Offset + Size);
Devang Patel930143b2009-11-21 02:48:08 +00001233 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling2f921f82009-05-15 09:23:25 +00001234
Devang Patel67f56f02009-11-04 22:06:12 +00001235 // Here WD_AT_data_member_location points to the anonymous
1236 // field that includes this bit field.
Devang Patel930143b2009-11-21 02:48:08 +00001237 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001238
1239 } else
1240 // This is not a bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001241 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001242
Devang Pateld2316892010-02-03 20:08:48 +00001243 if (DT.getTag() == dwarf::DW_TAG_inheritance
1244 && DT.isVirtual()) {
1245
1246 // For C++, virtual base classes are not at fixed offset. Use following
1247 // expression to extract appropriate offset from vtable.
1248 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1249
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001250 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Pateld2316892010-02-03 20:08:48 +00001251 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1252 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1253 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1254 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1255 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1256 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1257 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1258
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001259 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
Devang Pateld2316892010-02-03 20:08:48 +00001260 VBaseLocationDie);
1261 } else
1262 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001263
1264 if (DT.isProtected())
Devang Pateleb57c592009-12-03 19:11:07 +00001265 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001266 dwarf::DW_ACCESS_protected);
1267 else if (DT.isPrivate())
Devang Pateleb57c592009-12-03 19:11:07 +00001268 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001269 dwarf::DW_ACCESS_private);
Devang Pateleb57c592009-12-03 19:11:07 +00001270 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1271 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1272 dwarf::DW_ACCESS_public);
1273 if (DT.isVirtual())
1274 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1275 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling2f921f82009-05-15 09:23:25 +00001276 return MemberDie;
1277}
1278
Devang Patel525dda02009-12-14 16:18:45 +00001279/// createSubprogramDIE - Create new DIE using SP.
Devang Patel185051c2010-09-27 23:15:27 +00001280DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001281 CompileUnit *SPCU = getCompileUnit(SP);
1282 DIE *SPDie = SPCU->getDIE(SP);
Devang Patel525dda02009-12-14 16:18:45 +00001283 if (SPDie)
1284 return SPDie;
1285
1286 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf200b392010-03-02 17:58:15 +00001287 // Constructors and operators for anonymous aggregates do not have names.
Devang Pateld0fa3042010-03-02 01:26:20 +00001288 if (!SP.getName().empty())
1289 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling2f921f82009-05-15 09:23:25 +00001290
Devang Patel2d9caf92009-11-25 17:36:49 +00001291 StringRef LinkageName = SP.getLinkageName();
Devang Patel43ef34d2010-01-05 01:46:14 +00001292 if (!LinkageName.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001293 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel43ef34d2010-01-05 01:46:14 +00001294 getRealLinkageName(LinkageName));
1295
Devang Patelb6511a32010-08-09 20:20:05 +00001296 addSourceLine(SPDie, SP);
Bill Wendling2f921f82009-05-15 09:23:25 +00001297
Bill Wendling2f921f82009-05-15 09:23:25 +00001298 // Add prototyped tag, if C or ObjC.
1299 unsigned Lang = SP.getCompileUnit().getLanguage();
1300 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1301 Lang == dwarf::DW_LANG_ObjC)
Devang Patel930143b2009-11-21 02:48:08 +00001302 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001303
1304 // Add Return Type.
Devang Patel236526d2009-12-03 01:25:38 +00001305 DICompositeType SPTy = SP.getType();
1306 DIArray Args = SPTy.getTypeArray();
Bill Wendling2f921f82009-05-15 09:23:25 +00001307 unsigned SPTag = SPTy.getTag();
Devang Pateleb57c592009-12-03 19:11:07 +00001308
Devang Patel3b548aa2010-03-08 20:52:55 +00001309 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel9ccfb642009-12-09 18:24:21 +00001310 addType(SPDie, SPTy);
Devang Patel236526d2009-12-03 01:25:38 +00001311 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001312 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel236526d2009-12-03 01:25:38 +00001313
Devang Pateleb57c592009-12-03 19:11:07 +00001314 unsigned VK = SP.getVirtuality();
1315 if (VK) {
1316 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001317 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Pateleb57c592009-12-03 19:11:07 +00001318 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1319 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1320 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001321 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001322 SP.getContainingType()));
Devang Pateleb57c592009-12-03 19:11:07 +00001323 }
1324
Devang Patel185051c2010-09-27 23:15:27 +00001325 if (!SP.isDefinition()) {
Devang Patel930143b2009-11-21 02:48:08 +00001326 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001327
1328 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel236526d2009-12-03 01:25:38 +00001329 // be handled while processing variables.
1330 DICompositeType SPTy = SP.getType();
1331 DIArray Args = SPTy.getTypeArray();
1332 unsigned SPTag = SPTy.getTag();
1333
Bill Wendling2f921f82009-05-15 09:23:25 +00001334 if (SPTag == dwarf::DW_TAG_subroutine_type)
1335 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1336 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001337 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patel6efc8e52010-02-06 01:02:37 +00001338 addType(Arg, ATy);
1339 if (ATy.isArtificial())
1340 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel930143b2009-11-21 02:48:08 +00001341 SPDie->addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +00001342 }
1343 }
1344
Devang Patel999b4992010-02-03 19:57:19 +00001345 if (SP.isArtificial())
1346 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1347
Devang Patelb4e3b902010-04-30 19:38:23 +00001348 if (!SP.isLocalToUnit())
1349 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001350
Devang Patelb4e3b902010-04-30 19:38:23 +00001351 if (SP.isOptimized())
1352 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1353
Jim Grosbach965a73a2010-07-21 23:03:52 +00001354 if (unsigned isa = Asm->getISAEncoding()) {
1355 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1356 }
1357
Devang Patelb4e3b902010-04-30 19:38:23 +00001358 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001359 SPCU->insertDIE(SP, SPDie);
Devang Patelb4e3b902010-04-30 19:38:23 +00001360
Stuart Hastingsafe54f12010-06-11 20:08:44 +00001361 // Add to context owner.
1362 addToContextOwner(SPDie, SP.getContext());
1363
Bill Wendling2f921f82009-05-15 09:23:25 +00001364 return SPDie;
1365}
1366
Devang Patel32cc43c2010-05-07 20:54:48 +00001367DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattner8d2fe282010-03-31 05:36:29 +00001368 assert(N && "Invalid Scope encoding!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001369
1370 DbgScope *AScope = AbstractScopes.lookup(N);
1371 if (AScope)
1372 return AScope;
Jim Grosbach042483e2009-11-21 23:12:12 +00001373
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001374 DbgScope *Parent = NULL;
1375
1376 DIDescriptor Scope(N);
1377 if (Scope.isLexicalBlock()) {
1378 DILexicalBlock DB(N);
1379 DIDescriptor ParentDesc = DB.getContext();
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001380 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001381 }
1382
1383 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1384
1385 if (Parent)
Devang Patel930143b2009-11-21 02:48:08 +00001386 Parent->addScope(AScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001387 AScope->setAbstractScope();
1388 AbstractScopes[N] = AScope;
1389 if (DIDescriptor(N).isSubprogram())
1390 AbstractScopesList.push_back(AScope);
1391 return AScope;
1392}
Devang Patel75cc16c2009-10-01 20:31:14 +00001393
Devang Patel019922d2010-04-06 23:53:48 +00001394/// isSubprogramContext - Return true if Context is either a subprogram
1395/// or another context nested inside a subprogram.
Devang Patel32cc43c2010-05-07 20:54:48 +00001396static bool isSubprogramContext(const MDNode *Context) {
Devang Patel019922d2010-04-06 23:53:48 +00001397 if (!Context)
1398 return false;
1399 DIDescriptor D(Context);
1400 if (D.isSubprogram())
1401 return true;
1402 if (D.isType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001403 return isSubprogramContext(DIType(Context).getContext());
Devang Patel019922d2010-04-06 23:53:48 +00001404 return false;
1405}
1406
Jim Grosbach042483e2009-11-21 23:12:12 +00001407/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +00001408/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1409/// If there are global variables in this scope then create and insert
1410/// DIEs for these variables.
Devang Patel32cc43c2010-05-07 20:54:48 +00001411DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001412 CompileUnit *SPCU = getCompileUnit(SPNode);
1413 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +00001414
Chris Lattner3a383cb2010-04-05 00:13:49 +00001415 assert(SPDie && "Unable to find subprogram DIE!");
1416 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001417
Chris Lattner3a383cb2010-04-05 00:13:49 +00001418 // There is not any need to generate specification DIE for a function
1419 // defined at compile unit level. If a function is defined inside another
1420 // function then gdb prefers the definition at top level and but does not
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001421 // expect specification DIE in parent function. So avoid creating
Chris Lattner3a383cb2010-04-05 00:13:49 +00001422 // specification DIE for a function defined inside a function.
1423 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001424 !SP.getContext().isFile() &&
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001425 !isSubprogramContext(SP.getContext())) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00001426 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001427
1428 // Add arguments.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001429 DICompositeType SPTy = SP.getType();
1430 DIArray Args = SPTy.getTypeArray();
1431 unsigned SPTag = SPTy.getTag();
1432 if (SPTag == dwarf::DW_TAG_subroutine_type)
1433 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1434 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001435 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattner3a383cb2010-04-05 00:13:49 +00001436 addType(Arg, ATy);
1437 if (ATy.isArtificial())
1438 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1439 SPDie->addChild(Arg);
1440 }
1441 DIE *SPDeclDie = SPDie;
1442 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001443 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Chris Lattner3a383cb2010-04-05 00:13:49 +00001444 SPDeclDie);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001445 SPCU->addDie(SPDie);
Chris Lattner3a383cb2010-04-05 00:13:49 +00001446 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001447
Devang Patela37a95e2010-07-07 22:20:57 +00001448 // Pick up abstract subprogram DIE.
1449 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1450 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1451 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1452 dwarf::DW_FORM_ref4, AbsSPDIE);
1453 SPCU->addDie(SPDie);
1454 }
1455
Chris Lattner3a383cb2010-04-05 00:13:49 +00001456 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1457 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1458 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1459 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1460 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1461 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1462 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +00001463
Chris Lattner3a383cb2010-04-05 00:13:49 +00001464 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001465}
1466
Jim Grosbach042483e2009-11-21 23:12:12 +00001467/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +00001468/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1469DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001470
1471 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1472 if (Scope->isAbstractScope())
1473 return ScopeDIE;
1474
1475 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1476 if (Ranges.empty())
1477 return 0;
1478
1479 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1480 if (Ranges.size() > 1) {
1481 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001482 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +00001483 // DW_AT_ranges appropriately.
1484 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1485 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1486 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1487 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +00001488 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1489 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +00001490 }
1491 DebugRangeSymbols.push_back(NULL);
1492 DebugRangeSymbols.push_back(NULL);
1493 return ScopeDIE;
1494 }
1495
Devang Patel9fc11702010-05-25 23:40:22 +00001496 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1497 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001498
Devang Patel9fc11702010-05-25 23:40:22 +00001499 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001500
Chris Lattnere13c3722010-03-09 01:58:53 +00001501 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1502 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001503
Devang Patel6c74a872010-04-27 19:46:33 +00001504 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1505 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001506
1507 return ScopeDIE;
1508}
1509
Devang Patel930143b2009-11-21 02:48:08 +00001510/// constructInlinedScopeDIE - This scope represents inlined body of
1511/// a function. Construct DIE to represent this concrete inlined copy
1512/// of the function.
1513DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001514
1515 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001516 assert (Ranges.empty() == false
Devang Patel6c74a872010-04-27 19:46:33 +00001517 && "DbgScope does not have instruction markers!");
1518
1519 // FIXME : .debug_inlined section specification does not clearly state how
1520 // to emit inlined scope that is split into multiple instruction ranges.
1521 // For now, use first instruction range and emit low_pc/high_pc pair and
1522 // corresponding .debug_inlined section entry for this pair.
1523 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +00001524 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1525 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001526
Devang Patel4c6bd662010-07-08 22:39:20 +00001527 if (StartLabel == 0 || EndLabel == 0) {
Devang Patel6c74a872010-04-27 19:46:33 +00001528 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1529 return 0;
1530 }
Chris Lattnere13c3722010-03-09 01:58:53 +00001531 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001532 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +00001533 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001534 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +00001535
Devang Patel3b548aa2010-03-08 20:52:55 +00001536 if (!Scope->getScopeNode())
Devang Patelbc97f6b2010-03-08 19:20:38 +00001537 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001538 DIScope DS(Scope->getScopeNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001539 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1540
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001541 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001542 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1543 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001544 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel930143b2009-11-21 02:48:08 +00001545 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001546 dwarf::DW_FORM_ref4, OriginDIE);
1547
Chris Lattner085b6522010-03-09 00:31:02 +00001548 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnere13c3722010-03-09 01:58:53 +00001549 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001550
1551 InlinedSubprogramDIEs.insert(OriginDIE);
1552
1553 // Track the start label for this inlined function.
Devang Patel32cc43c2010-05-07 20:54:48 +00001554 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001555 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001556
1557 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001558 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +00001559 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001560 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001561 } else
Chris Lattner085b6522010-03-09 00:31:02 +00001562 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001563
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001564 DILocation DL(Scope->getInlinedAt());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001565 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel930143b2009-11-21 02:48:08 +00001566 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001567
1568 return ScopeDIE;
1569}
1570
Devang Patel930143b2009-11-21 02:48:08 +00001571
1572/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel9ccfb642009-12-09 18:24:21 +00001573DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001574 StringRef Name = DV->getName();
Devang Patel2d9caf92009-11-25 17:36:49 +00001575 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +00001576 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001577
1578 // Translate tag to proper Dwarf tag. The result variable is dropped for
1579 // now.
1580 unsigned Tag;
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001581 switch (DV->getTag()) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001582 case dwarf::DW_TAG_return_variable:
1583 return NULL;
1584 case dwarf::DW_TAG_arg_variable:
1585 Tag = dwarf::DW_TAG_formal_parameter;
1586 break;
1587 case dwarf::DW_TAG_auto_variable: // fall thru
1588 default:
1589 Tag = dwarf::DW_TAG_variable;
1590 break;
1591 }
1592
1593 // Define variable debug information entry.
1594 DIE *VariableDie = new DIE(Tag);
1595
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001596 DIE *AbsDIE = NULL;
Devang Patele1c53f22010-05-20 16:36:41 +00001597 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1598 V2AVI = VarToAbstractVarMap.find(DV);
1599 if (V2AVI != VarToAbstractVarMap.end())
1600 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +00001601
Devang Patele1c53f22010-05-20 16:36:41 +00001602 if (AbsDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001603 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001604 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001605 else {
Devang Patel930143b2009-11-21 02:48:08 +00001606 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001607 addSourceLine(VariableDie, DV->getVariable());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001608
1609 // Add variable type.
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001610 addType(VariableDie, DV->getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001611 }
1612
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001613 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel6efc8e52010-02-06 01:02:37 +00001614 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel9fc11702010-05-25 23:40:22 +00001615
1616 if (Scope->isAbstractScope()) {
1617 DV->setDIE(VariableDie);
1618 return VariableDie;
1619 }
1620
1621 // Add variable address.
1622
1623 unsigned Offset = DV->getDotDebugLocOffset();
1624 if (Offset != ~0U) {
1625 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1626 Asm->GetTempSymbol("debug_loc", Offset));
1627 DV->setDIE(VariableDie);
1628 UseDotDebugLocEntry.insert(VariableDie);
1629 return VariableDie;
1630 }
1631
1632 // Check if variable is described by a DBG_VALUE instruction.
1633 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1634 DbgVariableToDbgInstMap.find(DV);
1635 if (DVI != DbgVariableToDbgInstMap.end()) {
1636 const MachineInstr *DVInsn = DVI->second;
1637 const MCSymbol *DVLabel = findVariableLabel(DV);
1638 bool updated = false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001639 // FIXME : Handle getNumOperands != 3
Devang Patel9fc11702010-05-25 23:40:22 +00001640 if (DVInsn->getNumOperands() == 3) {
Devang Patel86ec8b32010-08-31 22:22:42 +00001641 if (DVInsn->getOperand(0).isReg()) {
1642 const MachineOperand RegOp = DVInsn->getOperand(0);
1643 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1644 if (DVInsn->getOperand(1).isImm() &&
1645 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1646 addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
1647 updated = true;
1648 } else
1649 updated = addRegisterAddress(VariableDie, DVLabel, RegOp);
1650 }
Devang Patel9fc11702010-05-25 23:40:22 +00001651 else if (DVInsn->getOperand(0).isImm())
1652 updated = addConstantValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001653 else if (DVInsn->getOperand(0).isFPImm())
1654 updated =
Devang Patel30265c42010-07-07 20:12:52 +00001655 addConstantFPValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patel9fc11702010-05-25 23:40:22 +00001656 } else {
1657 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1658 if (Location.getReg()) {
1659 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1660 if (DVLabel)
1661 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1662 DVLabel);
1663 updated = true;
1664 }
1665 }
1666 if (!updated) {
1667 // If variableDie is not updated then DBG_VALUE instruction does not
1668 // have valid variable info.
1669 delete VariableDie;
1670 return NULL;
1671 }
1672 DV->setDIE(VariableDie);
1673 return VariableDie;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001674 }
Devang Patel9fc11702010-05-25 23:40:22 +00001675
1676 // .. else use frame index, if available.
Devang Patel9fc11702010-05-25 23:40:22 +00001677 int FI = 0;
Devang Patel2cfc3af2010-08-31 06:11:28 +00001678 if (findVariableFrameIndex(DV, &FI))
1679 addVariableAddress(DV, VariableDie, FI);
1680
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001681 DV->setDIE(VariableDie);
1682 return VariableDie;
1683
1684}
Devang Patel930143b2009-11-21 02:48:08 +00001685
Devang Patel04d2f2d2009-11-24 01:14:22 +00001686void DwarfDebug::addPubTypes(DISubprogram SP) {
1687 DICompositeType SPTy = SP.getType();
1688 unsigned SPTag = SPTy.getTag();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001689 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel04d2f2d2009-11-24 01:14:22 +00001690 return;
1691
1692 DIArray Args = SPTy.getTypeArray();
Devang Patel04d2f2d2009-11-24 01:14:22 +00001693 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001694 DIType ATy(Args.getElement(i));
Devang Patel8d6a2b72010-05-07 21:45:47 +00001695 if (!ATy.Verify())
Devang Patel04d2f2d2009-11-24 01:14:22 +00001696 continue;
1697 DICompositeType CATy = getDICompositeType(ATy);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001698 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel12d150e2010-04-13 20:35:04 +00001699 && !CATy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001700 CompileUnit *TheCU = getCompileUnit(CATy);
1701 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1702 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001703 }
1704 }
1705}
1706
Devang Patel930143b2009-11-21 02:48:08 +00001707/// constructScopeDIE - Construct a DIE for this scope.
1708DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +00001709 if (!Scope || !Scope->getScopeNode())
1710 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001711
Devang Patel3b548aa2010-03-08 20:52:55 +00001712 DIScope DS(Scope->getScopeNode());
1713 DIE *ScopeDIE = NULL;
1714 if (Scope->getInlinedAt())
1715 ScopeDIE = constructInlinedScopeDIE(Scope);
1716 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +00001717 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +00001718 if (Scope->isAbstractScope()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001719 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +00001720 // Note down abstract DIE.
1721 if (ScopeDIE)
1722 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1723 }
Devang Patel3b548aa2010-03-08 20:52:55 +00001724 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001725 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +00001726 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001727 else
Devang Patel3b548aa2010-03-08 20:52:55 +00001728 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel23b2ae62010-03-29 22:59:58 +00001729 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001730
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001731 // Add variables to scope.
Devang Patel406798a2010-08-09 18:51:29 +00001732 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001733 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel9ccfb642009-12-09 18:24:21 +00001734 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach042483e2009-11-21 23:12:12 +00001735 if (VariableDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001736 ScopeDIE->addChild(VariableDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001737 }
1738
1739 // Add nested scopes.
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001740 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001741 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1742 // Define the Scope debug information entry.
Devang Patel930143b2009-11-21 02:48:08 +00001743 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach042483e2009-11-21 23:12:12 +00001744 if (NestedDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001745 ScopeDIE->addChild(NestedDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001746 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001747
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001748 if (DS.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001749 addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001750
Devang Patel04d2f2d2009-11-24 01:14:22 +00001751 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001752}
1753
Bill Wendling2b128d72009-05-20 23:19:06 +00001754/// GetOrCreateSourceID - Look up the source id with the given directory and
1755/// source file names. If none currently exists, create a new id and insert it
1756/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1757/// maps as well.
Chris Lattner71696ef2010-03-31 06:06:37 +00001758unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendling2b128d72009-05-20 23:19:06 +00001759 unsigned DId;
Devang Patel84a74772010-07-27 20:51:15 +00001760 assert (DirName.empty() == false && "Invalid directory name!");
Devang Patel498877d2010-07-24 00:53:22 +00001761
Devang Patel871d0b12010-09-16 20:57:49 +00001762 // If FE did not provide a file name, then assume stdin.
1763 if (FileName.empty())
1764 return GetOrCreateSourceID(DirName, "<stdin>");
1765
Bill Wendling2b128d72009-05-20 23:19:06 +00001766 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1767 if (DI != DirectoryIdMap.end()) {
1768 DId = DI->getValue();
1769 } else {
1770 DId = DirectoryNames.size() + 1;
1771 DirectoryIdMap[DirName] = DId;
1772 DirectoryNames.push_back(DirName);
1773 }
1774
1775 unsigned FId;
1776 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1777 if (FI != SourceFileIdMap.end()) {
1778 FId = FI->getValue();
1779 } else {
1780 FId = SourceFileNames.size() + 1;
1781 SourceFileIdMap[FileName] = FId;
1782 SourceFileNames.push_back(FileName);
1783 }
1784
1785 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1786 SourceIdMap.find(std::make_pair(DId, FId));
1787 if (SI != SourceIdMap.end())
1788 return SI->second;
1789
1790 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1791 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1792 SourceIds.push_back(std::make_pair(DId, FId));
1793
1794 return SrcId;
1795}
1796
Devang Patel1f4690c2009-12-15 19:16:48 +00001797/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1798DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001799 CompileUnit *TheCU = getCompileUnit(NS);
1800 DIE *NDie = TheCU->getDIE(NS);
Devang Patel1f4690c2009-12-15 19:16:48 +00001801 if (NDie)
1802 return NDie;
1803 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001804 TheCU->insertDIE(NS, NDie);
Devang Patel1f4690c2009-12-15 19:16:48 +00001805 if (!NS.getName().empty())
1806 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patelb6511a32010-08-09 20:20:05 +00001807 addSourceLine(NDie, NS);
Devang Patel1f4690c2009-12-15 19:16:48 +00001808 addToContextOwner(NDie, NS.getContext());
1809 return NDie;
1810}
1811
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001812/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +00001813/// metadata node with tag DW_TAG_compile_unit.
Devang Patel32cc43c2010-05-07 20:54:48 +00001814void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001815 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +00001816 StringRef FN = DIUnit.getFilename();
1817 StringRef Dir = DIUnit.getDirectory();
Devang Patelb2969422009-09-29 18:40:58 +00001818 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendling2b128d72009-05-20 23:19:06 +00001819
1820 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel930143b2009-11-21 02:48:08 +00001821 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001822 DIUnit.getProducer());
Devang Patel930143b2009-11-21 02:48:08 +00001823 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendling2b128d72009-05-20 23:19:06 +00001824 DIUnit.getLanguage());
Devang Patel930143b2009-11-21 02:48:08 +00001825 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +00001826 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1827 // simplifies debug range entries.
Devang Patel1de21ec2010-06-28 22:22:47 +00001828 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +00001829 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +00001830 // compile unit in debug_line section.
Devang Patelea636392010-08-31 23:50:19 +00001831 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
1832 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
1833 Asm->GetTempSymbol("section_line"));
1834 else
1835 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +00001836
Devang Patel2d9caf92009-11-25 17:36:49 +00001837 if (!Dir.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001838 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +00001839 if (DIUnit.isOptimized())
Devang Patel930143b2009-11-21 02:48:08 +00001840 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +00001841
Devang Patel2d9caf92009-11-25 17:36:49 +00001842 StringRef Flags = DIUnit.getFlags();
1843 if (!Flags.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001844 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendling2b128d72009-05-20 23:19:06 +00001845
1846 unsigned RVer = DIUnit.getRunTimeVersion();
1847 if (RVer)
Devang Patel930143b2009-11-21 02:48:08 +00001848 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +00001849 dwarf::DW_FORM_data1, RVer);
1850
Devang Patel1a0df9a2010-05-10 22:49:55 +00001851 CompileUnit *NewCU = new CompileUnit(ID, Die);
1852 if (!FirstCU)
1853 FirstCU = NewCU;
1854 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendling2b128d72009-05-20 23:19:06 +00001855}
1856
Devang Patel1a0df9a2010-05-10 22:49:55 +00001857/// getCompielUnit - Get CompileUnit DIE.
1858CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1859 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1860 DIDescriptor D(N);
1861 const MDNode *CUNode = NULL;
1862 if (D.isCompileUnit())
1863 CUNode = N;
1864 else if (D.isSubprogram())
1865 CUNode = DISubprogram(N).getCompileUnit();
1866 else if (D.isType())
1867 CUNode = DIType(N).getCompileUnit();
1868 else if (D.isGlobalVariable())
1869 CUNode = DIGlobalVariable(N).getCompileUnit();
1870 else if (D.isVariable())
1871 CUNode = DIVariable(N).getCompileUnit();
1872 else if (D.isNameSpace())
1873 CUNode = DINameSpace(N).getCompileUnit();
1874 else if (D.isFile())
1875 CUNode = DIFile(N).getCompileUnit();
1876 else
1877 return FirstCU;
1878
1879 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1880 = CUMap.find(CUNode);
1881 if (I == CUMap.end())
1882 return FirstCU;
1883 return I->second;
1884}
1885
Devang Patela8652672010-08-23 18:25:56 +00001886/// isUnsignedDIType - Return true if type encoding is unsigned.
1887static bool isUnsignedDIType(DIType Ty) {
1888 DIDerivedType DTy(Ty);
1889 if (DTy.Verify())
1890 return isUnsignedDIType(DTy.getTypeDerivedFrom());
1891
1892 DIBasicType BTy(Ty);
1893 if (BTy.Verify()) {
1894 unsigned Encoding = BTy.getEncoding();
1895 if (Encoding == dwarf::DW_ATE_unsigned ||
1896 Encoding == dwarf::DW_ATE_unsigned_char)
1897 return true;
1898 }
1899 return false;
1900}
Devang Patel1a0df9a2010-05-10 22:49:55 +00001901
1902/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00001903void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel469c12d22010-08-10 01:37:23 +00001904 DIGlobalVariable GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001905
Devang Patelf5d53602009-09-04 23:59:07 +00001906 // If debug information is malformed then ignore it.
Devang Patel469c12d22010-08-10 01:37:23 +00001907 if (GV.Verify() == false)
Devang Patelf5d53602009-09-04 23:59:07 +00001908 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001909
1910 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001911 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel469c12d22010-08-10 01:37:23 +00001912 if (TheCU->getDIE(GV))
Devang Patel0751a282009-06-26 01:49:18 +00001913 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001914
Devang Patel469c12d22010-08-10 01:37:23 +00001915 DIType GTy = GV.getType();
Devang Patelb2197462010-08-10 07:11:13 +00001916 DIE *VariableDIE = new DIE(GV.getTag());
1917
1918 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001919
Devang Patel469c12d22010-08-10 01:37:23 +00001920 // Add name.
1921 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1922 GV.getDisplayName());
1923 StringRef LinkageName = GV.getLinkageName();
Devang Patelb2197462010-08-10 07:11:13 +00001924 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel469c12d22010-08-10 01:37:23 +00001925 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1926 getRealLinkageName(LinkageName));
1927 // Add type.
1928 addType(VariableDIE, GTy);
Devang Patel12d150e2010-04-13 20:35:04 +00001929 if (GTy.isCompositeType() && !GTy.getName().empty()
1930 && !GTy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001931 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001932 assert(Entry && "Missing global type!");
Devang Patel1a0df9a2010-05-10 22:49:55 +00001933 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001934 }
Devang Patel469c12d22010-08-10 01:37:23 +00001935 // Add scoping info.
1936 if (!GV.isLocalToUnit()) {
1937 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1938 // Expose as global.
1939 TheCU->addGlobal(GV.getName(), VariableDIE);
1940 }
1941 // Add line number info.
1942 addSourceLine(VariableDIE, GV);
1943 // Add to map.
1944 TheCU->insertDIE(N, VariableDIE);
1945 // Add to context owner.
1946 DIDescriptor GVContext = GV.getContext();
1947 addToContextOwner(VariableDIE, GVContext);
1948 // Add location.
Devang Patelb2197462010-08-10 07:11:13 +00001949 if (isGlobalVariable) {
1950 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1951 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1952 addLabel(Block, 0, dwarf::DW_FORM_udata,
1953 Asm->Mang->getSymbol(GV.getGlobal()));
1954 // Do not create specification DIE if context is either compile unit
1955 // or a subprogram.
1956 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1957 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1958 // Create specification DIE.
1959 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1960 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1961 dwarf::DW_FORM_ref4, VariableDIE);
1962 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1963 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1964 TheCU->addDie(VariableSpecDIE);
1965 } else {
1966 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1967 }
1968 } else if (Constant *C = GV.getConstant()) {
1969 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
Devang Patela8652672010-08-23 18:25:56 +00001970 if (isUnsignedDIType(GTy))
Devang Patelb2197462010-08-10 07:11:13 +00001971 addUInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
1972 CI->getZExtValue());
1973 else
1974 addSInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1975 CI->getSExtValue());
Devang Patelb2197462010-08-10 07:11:13 +00001976 }
Devang Patel469c12d22010-08-10 01:37:23 +00001977 }
Devang Patel0751a282009-06-26 01:49:18 +00001978 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001979}
1980
Devang Patel1a0df9a2010-05-10 22:49:55 +00001981/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00001982void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001983 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +00001984
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001985 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001986 CompileUnit *TheCU = getCompileUnit(N);
1987 if (TheCU->getDIE(N))
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001988 return;
1989
Bill Wendling2b128d72009-05-20 23:19:06 +00001990 if (!SP.isDefinition())
1991 // This is a method declaration which will be handled while constructing
1992 // class type.
Devang Patel0751a282009-06-26 01:49:18 +00001993 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001994
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001995 DIE *SubprogramDie = createSubprogramDIE(SP);
1996
1997 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001998 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001999
2000 // Add to context owner.
Devang Patel1f4690c2009-12-15 19:16:48 +00002001 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +00002002
Bill Wendling2b128d72009-05-20 23:19:06 +00002003 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002004 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002005
Devang Patel0751a282009-06-26 01:49:18 +00002006 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00002007}
2008
Devang Patel930143b2009-11-21 02:48:08 +00002009/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +00002010/// content. Create global DIEs and emit initial debug info sections.
2011/// This is inovked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +00002012void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +00002013 if (DisableDebugInfoPrinting)
2014 return;
2015
Devang Patel63524442009-07-30 18:56:46 +00002016 DebugInfoFinder DbgFinder;
2017 DbgFinder.processModule(*M);
Devang Patel0751a282009-06-26 01:49:18 +00002018
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002019 bool HasDebugInfo = false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002020
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002021 // Scan all the compile-units to see if there are any marked as the main unit.
2022 // if not, we do not generate debug info.
2023 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2024 E = DbgFinder.compile_unit_end(); I != E; ++I) {
2025 if (DICompileUnit(*I).isMain()) {
2026 HasDebugInfo = true;
2027 break;
2028 }
2029 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002030
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002031 if (!HasDebugInfo) return;
2032
2033 // Tell MMI that we have debug info.
2034 MMI->setDebugInfoAvailability(true);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002035
Chris Lattnerd442aa32010-04-04 23:17:54 +00002036 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002037 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002038
Bill Wendling2b128d72009-05-20 23:19:06 +00002039 // Create all the compile unit DIEs.
Devang Patel63524442009-07-30 18:56:46 +00002040 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2041 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002042 constructCompileUnit(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00002043
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002044 // Create DIEs for each subprogram.
Devang Patel63524442009-07-30 18:56:46 +00002045 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
2046 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002047 constructSubprogramDIE(*I);
Devang Patel0751a282009-06-26 01:49:18 +00002048
Devang Patel2b75ed22009-12-10 19:14:49 +00002049 // Create DIEs for each global variable.
2050 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
2051 E = DbgFinder.global_variable_end(); I != E; ++I)
2052 constructGlobalVariableDIE(*I);
2053
Devang Patel8e06a5e2010-08-10 20:01:20 +00002054 //getOrCreateTypeDIE
2055 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
2056 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2057 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2058
Devang Patel7a554812010-09-28 18:08:20 +00002059 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
2060 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2061 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2062
Bill Wendling2b128d72009-05-20 23:19:06 +00002063 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +00002064 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00002065
2066 // Print out .file directives to specify files for .loc directives. These are
2067 // printed out early so that they precede any .loc directives.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002068 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendling2b128d72009-05-20 23:19:06 +00002069 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2070 // Remember source id starts at 1.
2071 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002072 // FIXME: don't use sys::path for this! This should not depend on the
2073 // host.
Bill Wendling2b128d72009-05-20 23:19:06 +00002074 sys::Path FullPath(getSourceDirectoryName(Id.first));
2075 bool AppendOk =
2076 FullPath.appendComponent(getSourceFileName(Id.second));
2077 assert(AppendOk && "Could not append filename to directory!");
2078 AppendOk = false;
Chris Lattner601ef332010-01-25 18:58:59 +00002079 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendling2b128d72009-05-20 23:19:06 +00002080 }
2081 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002082}
2083
Devang Patel930143b2009-11-21 02:48:08 +00002084/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +00002085///
Devang Patel930143b2009-11-21 02:48:08 +00002086void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002087 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +00002088 const Module *M = MMI->getModule();
Devang Pateld0701282010-08-02 17:32:15 +00002089 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +00002090 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2091 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2092 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2093 DISubprogram SP(AllSPs->getOperand(SI));
2094 if (!SP.Verify()) continue;
2095
2096 // Collect info for variables that were optimized out.
Devang Patel18efced2010-07-19 17:53:55 +00002097 if (!SP.isDefinition()) continue;
Devang Patelf3b2db62010-06-28 18:25:03 +00002098 StringRef FName = SP.getLinkageName();
2099 if (FName.empty())
2100 FName = SP.getName();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002101 NamedMDNode *NMD =
Devang Patelf3b2db62010-06-28 18:25:03 +00002102 M->getNamedMetadata(Twine("llvm.dbg.lv.", getRealLinkageName(FName)));
2103 if (!NMD) continue;
2104 unsigned E = NMD->getNumOperands();
2105 if (!E) continue;
2106 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Pateld0701282010-08-02 17:32:15 +00002107 DeadFnScopeMap[SP] = Scope;
Devang Patelf3b2db62010-06-28 18:25:03 +00002108 for (unsigned I = 0; I != E; ++I) {
2109 DIVariable DV(NMD->getOperand(I));
2110 if (!DV.Verify()) continue;
2111 Scope->addVariable(new DbgVariable(DV));
2112 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002113
Devang Patelf3b2db62010-06-28 18:25:03 +00002114 // Construct subprogram DIE and add variables DIEs.
2115 constructSubprogramDIE(SP);
2116 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patel406798a2010-08-09 18:51:29 +00002117 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patelf3b2db62010-06-28 18:25:03 +00002118 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2119 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2120 if (VariableDIE)
2121 ScopeDIE->addChild(VariableDIE);
2122 }
2123 }
2124 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002125
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002126 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2127 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2128 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2129 DIE *ISP = *AI;
Devang Patel930143b2009-11-21 02:48:08 +00002130 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002131 }
2132
Devang Patel32cc43c2010-05-07 20:54:48 +00002133 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Pateleb57c592009-12-03 19:11:07 +00002134 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2135 DIE *SPDie = CI->first;
Devang Patel32cc43c2010-05-07 20:54:48 +00002136 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Pateleb57c592009-12-03 19:11:07 +00002137 if (!N) continue;
Devang Patel1a0df9a2010-05-10 22:49:55 +00002138 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Pateleb57c592009-12-03 19:11:07 +00002139 if (!NDie) continue;
2140 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Pateleb57c592009-12-03 19:11:07 +00002141 }
2142
Bill Wendling2b128d72009-05-20 23:19:06 +00002143 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002144 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002145 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002146 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002147 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +00002148
2149 // End text sections.
2150 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002151 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +00002152 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +00002153 }
2154
2155 // Emit common frame information.
Devang Patel930143b2009-11-21 02:48:08 +00002156 emitCommonDebugFrame();
Bill Wendling2b128d72009-05-20 23:19:06 +00002157
2158 // Emit function debug frame information
2159 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2160 E = DebugFrames.end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002161 emitFunctionDebugFrame(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00002162
2163 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +00002164 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +00002165
2166 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +00002167 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002168
2169 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +00002170 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +00002171
Devang Patel4a213872010-08-24 00:06:12 +00002172 // Emit source line correspondence into a debug line section.
2173 emitDebugLines();
2174
Bill Wendling2b128d72009-05-20 23:19:06 +00002175 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +00002176 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +00002177
Devang Patel04d2f2d2009-11-24 01:14:22 +00002178 // Emit info into a debug pubtypes section.
2179 emitDebugPubTypes();
2180
Bill Wendling2b128d72009-05-20 23:19:06 +00002181 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002182 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +00002183
2184 // Emit info into a debug aranges section.
2185 EmitDebugARanges();
2186
2187 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002188 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +00002189
2190 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002191 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002192
2193 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +00002194 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002195
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002196 // Emit info into a debug str section.
2197 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002198
Devang Pateld0701282010-08-02 17:32:15 +00002199 // clean up.
2200 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002201 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2202 E = CUMap.end(); I != E; ++I)
2203 delete I->second;
2204 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +00002205}
2206
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002207/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002208DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattner915c5f92010-04-02 19:42:39 +00002209 DebugLoc ScopeLoc) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002210
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002211 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002212 if (AbsDbgVariable)
2213 return AbsDbgVariable;
2214
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002215 LLVMContext &Ctx = Var->getContext();
Chris Lattner915c5f92010-04-02 19:42:39 +00002216 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002217 if (!Scope)
2218 return NULL;
2219
Devang Patele1c53f22010-05-20 16:36:41 +00002220 AbsDbgVariable = new DbgVariable(Var);
Devang Patel930143b2009-11-21 02:48:08 +00002221 Scope->addVariable(AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002222 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002223 return AbsDbgVariable;
2224}
2225
Devang Patel490c8ab2010-05-20 19:57:06 +00002226/// collectVariableInfoFromMMITable - Collect variable information from
2227/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002228void
Devang Patel490c8ab2010-05-20 19:57:06 +00002229DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2230 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002231 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patel475d32a2009-10-06 01:26:37 +00002232 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2233 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2234 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00002235 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002236 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00002237 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00002238 DIVariable DV(Var);
2239 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002240
Chris Lattner915c5f92010-04-02 19:42:39 +00002241 DbgScope *Scope = 0;
Devang Patel32cc43c2010-05-07 20:54:48 +00002242 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattner915c5f92010-04-02 19:42:39 +00002243 Scope = ConcreteScopes.lookup(IA);
2244 if (Scope == 0)
2245 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002246
Devang Patelcdb7d442009-11-10 23:20:04 +00002247 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00002248 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00002249 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002250
Devang Patele1c53f22010-05-20 16:36:41 +00002251 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2252 DbgVariable *RegVar = new DbgVariable(DV);
2253 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel930143b2009-11-21 02:48:08 +00002254 Scope->addVariable(RegVar);
Devang Patele1c53f22010-05-20 16:36:41 +00002255 if (AbsDbgVariable) {
2256 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2257 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2258 }
Devang Patel475d32a2009-10-06 01:26:37 +00002259 }
Devang Patel490c8ab2010-05-20 19:57:06 +00002260}
Devang Patela3e9c9c2010-03-15 18:33:46 +00002261
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002262/// isDbgValueInUndefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +00002263/// DBG_VALUE instruction, is in undefined reg.
2264static bool isDbgValueInUndefinedReg(const MachineInstr *MI) {
2265 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2266 if (MI->getOperand(0).isReg() && !MI->getOperand(0).getReg())
2267 return true;
2268 return false;
2269}
2270
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002271/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +00002272/// DBG_VALUE instruction, is in a defined reg.
2273static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2274 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2275 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
2276 return true;
2277 return false;
2278}
2279
Devang Patel490c8ab2010-05-20 19:57:06 +00002280/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002281void
Devang Patel5c0f85c2010-06-25 22:07:34 +00002282DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2283 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002284
Devang Patel490c8ab2010-05-20 19:57:06 +00002285 /// collection info from MMI table.
2286 collectVariableInfoFromMMITable(MF, Processed);
2287
2288 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patela3e9c9c2010-03-15 18:33:46 +00002289 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattner3a383cb2010-04-05 00:13:49 +00002290 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel490c8ab2010-05-20 19:57:06 +00002291 I != E; ++I)
Devang Patela3e9c9c2010-03-15 18:33:46 +00002292 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2293 II != IE; ++II) {
2294 const MachineInstr *MInsn = II;
Devang Patel9fc11702010-05-25 23:40:22 +00002295 if (!MInsn->isDebugValue() || isDbgValueInUndefinedReg(MInsn))
Devang Patela3e9c9c2010-03-15 18:33:46 +00002296 continue;
Devang Patel490c8ab2010-05-20 19:57:06 +00002297 DbgValues.push_back(MInsn);
2298 }
Devang Patela3e9c9c2010-03-15 18:33:46 +00002299
Devang Patel9fc11702010-05-25 23:40:22 +00002300 // This is a collection of DBV_VALUE instructions describing same variable.
2301 SmallVector<const MachineInstr *, 4> MultipleValues;
Devang Patel490c8ab2010-05-20 19:57:06 +00002302 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2303 E = DbgValues.end(); I != E; ++I) {
2304 const MachineInstr *MInsn = *I;
Devang Patel9fc11702010-05-25 23:40:22 +00002305 MultipleValues.clear();
2306 if (isDbgValueInDefinedReg(MInsn))
2307 MultipleValues.push_back(MInsn);
Devang Patel490c8ab2010-05-20 19:57:06 +00002308 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2309 if (Processed.count(DV) != 0)
2310 continue;
2311
Devang Patelc2254f62010-06-02 19:05:13 +00002312 const MachineInstr *PrevMI = MInsn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002313 for (SmallVector<const MachineInstr *, 8>::iterator MI = I+1,
Devang Patel9fc11702010-05-25 23:40:22 +00002314 ME = DbgValues.end(); MI != ME; ++MI) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002315 const MDNode *Var =
Devang Patel9fc11702010-05-25 23:40:22 +00002316 (*MI)->getOperand((*MI)->getNumOperands()-1).getMetadata();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002317 if (Var == DV && isDbgValueInDefinedReg(*MI) &&
Devang Patelc2254f62010-06-02 19:05:13 +00002318 !PrevMI->isIdenticalTo(*MI))
Devang Patel9fc11702010-05-25 23:40:22 +00002319 MultipleValues.push_back(*MI);
Devang Patelc2254f62010-06-02 19:05:13 +00002320 PrevMI = *MI;
Devang Patel9fc11702010-05-25 23:40:22 +00002321 }
2322
Devang Patel490c8ab2010-05-20 19:57:06 +00002323 DbgScope *Scope = findDbgScope(MInsn);
Devang Patel7a9dedf2010-05-27 20:25:04 +00002324 bool CurFnArg = false;
2325 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2326 DISubprogram(DV.getContext()).describes(MF->getFunction()))
2327 CurFnArg = true;
2328 if (!Scope && CurFnArg)
Devang Patelfbd6c452010-05-21 00:10:20 +00002329 Scope = CurrentFnDbgScope;
Devang Patel490c8ab2010-05-20 19:57:06 +00002330 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00002331 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00002332 continue;
2333
2334 Processed.insert(DV);
Devang Patel490c8ab2010-05-20 19:57:06 +00002335 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel490c8ab2010-05-20 19:57:06 +00002336 Scope->addVariable(RegVar);
Devang Patel7a9dedf2010-05-27 20:25:04 +00002337 if (!CurFnArg)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002338 DbgVariableLabelsMap[RegVar] = getLabelBeforeInsn(MInsn);
Devang Patelfbd6c452010-05-21 00:10:20 +00002339 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2340 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2341 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patela3e9c9c2010-03-15 18:33:46 +00002342 }
Devang Patel9fc11702010-05-25 23:40:22 +00002343 if (MultipleValues.size() <= 1) {
2344 DbgVariableToDbgInstMap[RegVar] = MInsn;
2345 continue;
2346 }
2347
2348 // handle multiple DBG_VALUE instructions describing one variable.
Devang Patel9fc11702010-05-25 23:40:22 +00002349 if (DotDebugLocEntries.empty())
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002350 RegVar->setDotDebugLocOffset(0);
2351 else
2352 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002353 const MachineInstr *Begin = NULL;
2354 const MachineInstr *End = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002355 for (SmallVector<const MachineInstr *, 4>::iterator
2356 MVI = MultipleValues.begin(), MVE = MultipleValues.end();
Devang Patel30265c42010-07-07 20:12:52 +00002357 MVI != MVE; ++MVI) {
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002358 if (!Begin) {
2359 Begin = *MVI;
2360 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002361 }
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002362 End = *MVI;
Devang Patel9fc11702010-05-25 23:40:22 +00002363 MachineLocation MLoc;
Devang Patel0e60e672010-08-04 18:40:52 +00002364 if (Begin->getNumOperands() == 3) {
2365 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2366 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2367 } else
2368 MLoc = Asm->getDebugValueLocation(Begin);
2369
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002370 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2371 const MCSymbol *SLabel = getLabelBeforeInsn(End);
Devang Patel6c378ac2010-08-04 22:07:27 +00002372 if (MLoc.getReg())
2373 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
2374
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002375 Begin = End;
2376 if (MVI + 1 == MVE) {
2377 // If End is the last instruction then its value is valid
Devang Patel9fc11702010-05-25 23:40:22 +00002378 // until the end of the funtion.
Devang Patel0e60e672010-08-04 18:40:52 +00002379 MachineLocation EMLoc;
2380 if (End->getNumOperands() == 3) {
2381 if (End->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2382 EMLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2383 } else
2384 EMLoc = Asm->getDebugValueLocation(End);
Devang Patel6c378ac2010-08-04 22:07:27 +00002385 if (EMLoc.getReg())
2386 DotDebugLocEntries.
2387 push_back(DotDebugLocEntry(SLabel, FunctionEndSym, EMLoc));
Devang Patel9fc11702010-05-25 23:40:22 +00002388 }
2389 }
2390 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00002391 }
Devang Patele0a94bf2010-05-14 21:01:35 +00002392
2393 // Collect info for variables that were optimized out.
Devang Patelad517352010-06-22 01:01:58 +00002394 const Function *F = MF->getFunction();
2395 const Module *M = F->getParent();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002396 if (NamedMDNode *NMD =
2397 M->getNamedMetadata(Twine("llvm.dbg.lv.",
Devang Patelb6e058d2010-06-22 01:19:38 +00002398 getRealLinkageName(F->getName())))) {
Devang Patele0a94bf2010-05-14 21:01:35 +00002399 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00002400 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel490c8ab2010-05-20 19:57:06 +00002401 if (!DV || !Processed.insert(DV))
Devang Patele0a94bf2010-05-14 21:01:35 +00002402 continue;
2403 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2404 if (Scope)
Devang Patele1c53f22010-05-20 16:36:41 +00002405 Scope->addVariable(new DbgVariable(DV));
Devang Patele0a94bf2010-05-14 21:01:35 +00002406 }
2407 }
Devang Patel9fc11702010-05-25 23:40:22 +00002408}
Devang Patele0a94bf2010-05-14 21:01:35 +00002409
Devang Patel9fc11702010-05-25 23:40:22 +00002410/// getLabelBeforeInsn - Return Label preceding the instruction.
2411const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2412 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2413 LabelsBeforeInsn.find(MI);
2414 if (I == LabelsBeforeInsn.end())
2415 // FunctionBeginSym always preceeds all the instruction in current function.
2416 return FunctionBeginSym;
2417 return I->second;
2418}
2419
2420/// getLabelAfterInsn - Return Label immediately following the instruction.
2421const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2422 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2423 LabelsAfterInsn.find(MI);
2424 if (I == LabelsAfterInsn.end())
2425 return NULL;
2426 return I->second;
Devang Patel475d32a2009-10-06 01:26:37 +00002427}
2428
Devang Patelbd477be2010-03-29 17:20:31 +00002429/// beginScope - Process beginning of a scope.
2430void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patel002d54d2010-05-26 19:37:24 +00002431 if (InsnNeedsLabel.count(MI) == 0) {
2432 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patelbd477be2010-03-29 17:20:31 +00002433 return;
Devang Patel9fc11702010-05-25 23:40:22 +00002434 }
Devang Patel23b2ae62010-03-29 22:59:58 +00002435
Devang Patel002d54d2010-05-26 19:37:24 +00002436 // Check location.
2437 DebugLoc DL = MI->getDebugLoc();
2438 if (!DL.isUnknown()) {
2439 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2440 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Patel6c74a872010-04-27 19:46:33 +00002441 PrevInstLoc = DL;
Devang Patel002d54d2010-05-26 19:37:24 +00002442 LabelsBeforeInsn[MI] = PrevLabel;
2443 return;
Devang Patel6c74a872010-04-27 19:46:33 +00002444 }
Devang Patelbd477be2010-03-29 17:20:31 +00002445
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002446 // If location is unknown then use temp label for this DBG_VALUE
Devang Patel002d54d2010-05-26 19:37:24 +00002447 // instruction.
2448 if (MI->isDebugValue()) {
Devang Patelacc32a52010-05-26 21:23:46 +00002449 PrevLabel = MMI->getContext().CreateTempSymbol();
2450 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00002451 LabelsBeforeInsn[MI] = PrevLabel;
2452 return;
2453 }
2454
2455 if (UnknownLocations) {
2456 PrevLabel = recordSourceLine(0, 0, 0);
2457 LabelsBeforeInsn[MI] = PrevLabel;
2458 return;
2459 }
2460
2461 assert (0 && "Instruction is not processed!");
Devang Patel8db360d2009-10-06 01:50:42 +00002462}
2463
Devang Patel930143b2009-11-21 02:48:08 +00002464/// endScope - Process end of a scope.
2465void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel3ebd8932010-04-08 16:50:29 +00002466 if (InsnsEndScopeSet.count(MI) != 0) {
2467 // Emit a label if this instruction ends a scope.
2468 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2469 Asm->OutStreamer.EmitLabel(Label);
Devang Patel6c74a872010-04-27 19:46:33 +00002470 LabelsAfterInsn[MI] = Label;
Devang Patel3ebd8932010-04-08 16:50:29 +00002471 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002472}
2473
Devang Patel6c74a872010-04-27 19:46:33 +00002474/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002475DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel30265c42010-07-07 20:12:52 +00002476 const MDNode *InlinedAt) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002477 if (!InlinedAt) {
2478 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2479 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002480 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002481 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2482 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patel6c74a872010-04-27 19:46:33 +00002483 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002484 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002485 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Patel6c74a872010-04-27 19:46:33 +00002486 WScope->setParent(Parent);
2487 Parent->addScope(WScope);
2488 }
2489
2490 if (!WScope->getParent()) {
2491 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastings9b5005c2010-06-15 23:06:30 +00002492 // We used to check only for a linkage name, but that fails
2493 // since we began omitting the linkage name for private
2494 // functions. The new way is to check for the name in metadata,
2495 // but that's not supported in old .ll test cases. Ergo, we
2496 // check both.
Stuart Hastingsafe54f12010-06-11 20:08:44 +00002497 if (SPName == Asm->MF->getFunction()->getName() ||
2498 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Patel6c74a872010-04-27 19:46:33 +00002499 CurrentFnDbgScope = WScope;
2500 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002501
Devang Patel6c74a872010-04-27 19:46:33 +00002502 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002503 }
2504
Devang Patel5c0f85c2010-06-25 22:07:34 +00002505 getOrCreateAbstractScope(Scope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002506 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2507 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002508 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002509
2510 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2511 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2512 DILocation DL(InlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002513 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002514 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Patel6c74a872010-04-27 19:46:33 +00002515 WScope->setParent(Parent);
2516 Parent->addScope(WScope);
2517
2518 ConcreteScopes[InlinedAt] = WScope;
Devang Patel6c74a872010-04-27 19:46:33 +00002519
2520 return WScope;
Devang Patel8db360d2009-10-06 01:50:42 +00002521}
2522
Devang Patel6c74a872010-04-27 19:46:33 +00002523/// hasValidLocation - Return true if debug location entry attached with
2524/// machine instruction encodes valid location info.
2525static bool hasValidLocation(LLVMContext &Ctx,
2526 const MachineInstr *MInsn,
Devang Patel32cc43c2010-05-07 20:54:48 +00002527 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Patel6c74a872010-04-27 19:46:33 +00002528 DebugLoc DL = MInsn->getDebugLoc();
2529 if (DL.isUnknown()) return false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002530
Devang Patel32cc43c2010-05-07 20:54:48 +00002531 const MDNode *S = DL.getScope(Ctx);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002532
Devang Patel6c74a872010-04-27 19:46:33 +00002533 // There is no need to create another DIE for compile unit. For all
2534 // other scopes, create one DbgScope now. This will be translated
2535 // into a scope DIE at the end.
2536 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002537
Devang Patel6c74a872010-04-27 19:46:33 +00002538 Scope = S;
2539 InlinedAt = DL.getInlinedAt(Ctx);
2540 return true;
2541}
2542
2543/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2544/// hierarchy.
2545static void calculateDominanceGraph(DbgScope *Scope) {
2546 assert (Scope && "Unable to calculate scop edominance graph!");
2547 SmallVector<DbgScope *, 4> WorkStack;
2548 WorkStack.push_back(Scope);
2549 unsigned Counter = 0;
2550 while (!WorkStack.empty()) {
2551 DbgScope *WS = WorkStack.back();
2552 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2553 bool visitedChildren = false;
2554 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2555 SE = Children.end(); SI != SE; ++SI) {
2556 DbgScope *ChildScope = *SI;
2557 if (!ChildScope->getDFSOut()) {
2558 WorkStack.push_back(ChildScope);
2559 visitedChildren = true;
2560 ChildScope->setDFSIn(++Counter);
2561 break;
2562 }
2563 }
2564 if (!visitedChildren) {
2565 WorkStack.pop_back();
2566 WS->setDFSOut(++Counter);
2567 }
2568 }
2569}
2570
2571/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002572static
Devang Patel6c74a872010-04-27 19:46:33 +00002573void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2574 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2575{
2576#ifndef NDEBUG
2577 unsigned PrevDFSIn = 0;
2578 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2579 I != E; ++I) {
2580 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2581 II != IE; ++II) {
2582 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002583 const MDNode *Scope = NULL;
2584 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002585
2586 // Check if instruction has valid location information.
2587 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2588 dbgs() << " [ ";
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002589 if (InlinedAt)
Devang Patel6c74a872010-04-27 19:46:33 +00002590 dbgs() << "*";
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002591 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Patel6c74a872010-04-27 19:46:33 +00002592 MI2ScopeMap.find(MInsn);
2593 if (DI != MI2ScopeMap.end()) {
2594 DbgScope *S = DI->second;
2595 dbgs() << S->getDFSIn();
2596 PrevDFSIn = S->getDFSIn();
2597 } else
2598 dbgs() << PrevDFSIn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002599 } else
Devang Patel6c74a872010-04-27 19:46:33 +00002600 dbgs() << " [ x" << PrevDFSIn;
2601 dbgs() << " ]";
2602 MInsn->dump();
2603 }
2604 dbgs() << "\n";
2605 }
2606#endif
2607}
Devang Patel930143b2009-11-21 02:48:08 +00002608/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner848c7d22010-03-31 05:39:57 +00002609/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner76555b52010-01-26 23:18:02 +00002610bool DwarfDebug::extractScopeInformation() {
Devang Patel75cc16c2009-10-01 20:31:14 +00002611 // If scope information was extracted using .dbg intrinsics then there is not
2612 // any need to extract these information by scanning each instruction.
2613 if (!DbgScopeMap.empty())
2614 return false;
2615
Dan Gohmane9135cb2010-04-23 01:18:53 +00002616 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6c74a872010-04-27 19:46:33 +00002617 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2618 SmallVector<DbgRange, 4> MIRanges;
2619 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patel32cc43c2010-05-07 20:54:48 +00002620 const MDNode *PrevScope = NULL;
2621 const MDNode *PrevInlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002622 const MachineInstr *RangeBeginMI = NULL;
2623 const MachineInstr *PrevMI = NULL;
Chris Lattner3a383cb2010-04-05 00:13:49 +00002624 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel75cc16c2009-10-01 20:31:14 +00002625 I != E; ++I) {
2626 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2627 II != IE; ++II) {
2628 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002629 const MDNode *Scope = NULL;
2630 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002631
2632 // Check if instruction has valid location information.
2633 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2634 PrevMI = MInsn;
2635 continue;
2636 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002637
Devang Patel6c74a872010-04-27 19:46:33 +00002638 // If scope has not changed then skip this instruction.
2639 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2640 PrevMI = MInsn;
2641 continue;
2642 }
2643
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002644 if (RangeBeginMI) {
2645 // If we have alread seen a beginning of a instruction range and
Devang Patel6c74a872010-04-27 19:46:33 +00002646 // current instruction scope does not match scope of first instruction
2647 // in this range then create a new instruction range.
2648 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002649 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel30265c42010-07-07 20:12:52 +00002650 PrevInlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002651 MIRanges.push_back(R);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002652 }
Devang Patel6c74a872010-04-27 19:46:33 +00002653
2654 // This is a beginning of a new instruction range.
2655 RangeBeginMI = MInsn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002656
Devang Patel6c74a872010-04-27 19:46:33 +00002657 // Reset previous markers.
2658 PrevMI = MInsn;
2659 PrevScope = Scope;
2660 PrevInlinedAt = InlinedAt;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002661 }
2662 }
2663
Devang Patel6c74a872010-04-27 19:46:33 +00002664 // Create last instruction range.
2665 if (RangeBeginMI && PrevMI && PrevScope) {
2666 DbgRange R(RangeBeginMI, PrevMI);
2667 MIRanges.push_back(R);
2668 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel75cc16c2009-10-01 20:31:14 +00002669 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002670
Devang Patel530a0752010-01-04 20:44:00 +00002671 if (!CurrentFnDbgScope)
2672 return false;
2673
Devang Patel6c74a872010-04-27 19:46:33 +00002674 calculateDominanceGraph(CurrentFnDbgScope);
2675 if (PrintDbgScope)
2676 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2677
2678 // Find ranges of instructions covered by each DbgScope;
2679 DbgScope *PrevDbgScope = NULL;
2680 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2681 RE = MIRanges.end(); RI != RE; ++RI) {
2682 const DbgRange &R = *RI;
2683 DbgScope *S = MI2ScopeMap.lookup(R.first);
2684 assert (S && "Lost DbgScope for a machine instruction!");
2685 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2686 PrevDbgScope->closeInsnRange(S);
2687 S->openInsnRange(R.first);
2688 S->extendInsnRange(R.second);
2689 PrevDbgScope = S;
2690 }
2691
2692 if (PrevDbgScope)
2693 PrevDbgScope->closeInsnRange();
Devang Patel75cc16c2009-10-01 20:31:14 +00002694
Devang Patel359b0132010-04-08 18:43:56 +00002695 identifyScopeMarkers();
Devang Patelf1d5a1e2010-04-08 15:37:09 +00002696
2697 return !DbgScopeMap.empty();
2698}
2699
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002700/// identifyScopeMarkers() -
Devang Patel6c74a872010-04-27 19:46:33 +00002701/// Each DbgScope has first instruction and last instruction to mark beginning
2702/// and end of a scope respectively. Create an inverse map that list scopes
2703/// starts (and ends) with an instruction. One instruction may start (or end)
2704/// multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00002705void DwarfDebug::identifyScopeMarkers() {
Devang Patel7771b7c2010-01-20 02:05:23 +00002706 SmallVector<DbgScope *, 4> WorkList;
2707 WorkList.push_back(CurrentFnDbgScope);
2708 while (!WorkList.empty()) {
Chris Lattner848c7d22010-03-31 05:39:57 +00002709 DbgScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002710
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002711 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002712 if (!Children.empty())
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002713 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00002714 SE = Children.end(); SI != SE; ++SI)
2715 WorkList.push_back(*SI);
2716
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002717 if (S->isAbstractScope())
2718 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002719
Devang Patel6c74a872010-04-27 19:46:33 +00002720 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2721 if (Ranges.empty())
2722 continue;
2723 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2724 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002725 assert(RI->first && "DbgRange does not have first instruction!");
2726 assert(RI->second && "DbgRange does not have second instruction!");
Devang Patel6c74a872010-04-27 19:46:33 +00002727 InsnsEndScopeSet.insert(RI->second);
2728 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002729 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002730}
2731
Dan Gohman3df671a2010-04-20 00:37:27 +00002732/// FindFirstDebugLoc - Find the first debug location in the function. This
2733/// is intended to be an approximation for the source position of the
2734/// beginning of the function.
2735static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2736 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2737 I != E; ++I)
2738 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2739 MBBI != MBBE; ++MBBI) {
2740 DebugLoc DL = MBBI->getDebugLoc();
2741 if (!DL.isUnknown())
2742 return DL;
2743 }
2744 return DebugLoc();
2745}
2746
Devang Patel930143b2009-11-21 02:48:08 +00002747/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00002748/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00002749void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00002750 if (!MMI->hasDebugInfo()) return;
Bill Wendlingfcc14142010-04-07 09:28:04 +00002751 if (!extractScopeInformation()) return;
Devang Patel4598eb62009-10-06 18:37:31 +00002752
Devang Patel6c74a872010-04-27 19:46:33 +00002753 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2754 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00002755 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00002756 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00002757
2758 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2759 // function.
Dan Gohman3df671a2010-04-20 00:37:27 +00002760 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattner915c5f92010-04-02 19:42:39 +00002761 if (FDL.isUnknown()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002762
Devang Patel32cc43c2010-05-07 20:54:48 +00002763 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings61475c52010-07-19 23:56:30 +00002764 const MDNode *TheScope = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002765
Chris Lattner915c5f92010-04-02 19:42:39 +00002766 DISubprogram SP = getDISubprogram(Scope);
2767 unsigned Line, Col;
2768 if (SP.Verify()) {
2769 Line = SP.getLineNumber();
2770 Col = 0;
Stuart Hastings61475c52010-07-19 23:56:30 +00002771 TheScope = SP;
Chris Lattner915c5f92010-04-02 19:42:39 +00002772 } else {
2773 Line = FDL.getLine();
2774 Col = FDL.getCol();
Stuart Hastings61475c52010-07-19 23:56:30 +00002775 TheScope = Scope;
Bill Wendling2b128d72009-05-20 23:19:06 +00002776 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002777
Stuart Hastings61475c52010-07-19 23:56:30 +00002778 recordSourceLine(Line, Col, TheScope);
Devang Patel002d54d2010-05-26 19:37:24 +00002779
Devang Patel21ccf052010-06-02 16:42:51 +00002780 /// ProcessedArgs - Collection of arguments already processed.
2781 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2782
Devang Patel002d54d2010-05-26 19:37:24 +00002783 DebugLoc PrevLoc;
2784 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2785 I != E; ++I)
2786 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2787 II != IE; ++II) {
2788 const MachineInstr *MI = II;
2789 DebugLoc DL = MI->getDebugLoc();
2790 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00002791 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2792 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2793 if (!DV.Verify()) continue;
Devang Patel5e6b71c2010-05-27 16:47:30 +00002794 // If DBG_VALUE is for a local variable then it needs a label.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002795 if (DV.getTag() != dwarf::DW_TAG_arg_variable
Devang Patel42939752010-07-01 21:38:08 +00002796 && isDbgValueInUndefinedReg(MI) == false)
Devang Patel002d54d2010-05-26 19:37:24 +00002797 InsnNeedsLabel.insert(MI);
Devang Patel5e6b71c2010-05-27 16:47:30 +00002798 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel42939752010-07-01 21:38:08 +00002799 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2800 describes(MF->getFunction()))
Devang Patel5e6b71c2010-05-27 16:47:30 +00002801 InsnNeedsLabel.insert(MI);
2802 // DBG_VALUE indicating argument location change needs a label.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002803 else if (isDbgValueInUndefinedReg(MI) == false
2804 && !ProcessedArgs.insert(DV))
Devang Patel002d54d2010-05-26 19:37:24 +00002805 InsnNeedsLabel.insert(MI);
2806 } else {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002807 // If location is unknown then instruction needs a location only if
Devang Patel002d54d2010-05-26 19:37:24 +00002808 // UnknownLocations flag is set.
2809 if (DL.isUnknown()) {
2810 if (UnknownLocations && !PrevLoc.isUnknown())
2811 InsnNeedsLabel.insert(MI);
2812 } else if (DL != PrevLoc)
2813 // Otherwise, instruction needs a location only if it is new location.
2814 InsnNeedsLabel.insert(MI);
2815 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002816
Devang Patel002d54d2010-05-26 19:37:24 +00002817 if (!DL.isUnknown() || UnknownLocations)
2818 PrevLoc = DL;
2819 }
2820
2821 PrevLabel = FunctionBeginSym;
Bill Wendling2b128d72009-05-20 23:19:06 +00002822}
2823
Devang Patel930143b2009-11-21 02:48:08 +00002824/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00002825///
Chris Lattner76555b52010-01-26 23:18:02 +00002826void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingfcc14142010-04-07 09:28:04 +00002827 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00002828
Devang Patel530a0752010-01-04 20:44:00 +00002829 if (CurrentFnDbgScope) {
Devang Patel4a8e6e82010-05-22 00:04:14 +00002830
Devang Patel9fc11702010-05-25 23:40:22 +00002831 // Define end label for subprogram.
2832 FunctionEndSym = Asm->GetTempSymbol("func_end",
2833 Asm->getFunctionNumber());
2834 // Assumes in correct section after the entry point.
2835 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002836
Devang Patel5c0f85c2010-06-25 22:07:34 +00002837 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2838 collectVariableInfo(MF, ProcessedVars);
Devang Patel4a8e6e82010-05-22 00:04:14 +00002839
Devang Patel530a0752010-01-04 20:44:00 +00002840 // Get function line info.
2841 if (!Lines.empty()) {
2842 // Get section line info.
2843 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2844 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2845 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2846 // Append the function info to section info.
2847 SectionLineInfos.insert(SectionLineInfos.end(),
2848 Lines.begin(), Lines.end());
2849 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002850
Devang Patel530a0752010-01-04 20:44:00 +00002851 // Construct abstract scopes.
2852 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel5c0f85c2010-06-25 22:07:34 +00002853 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel5c0f85c2010-06-25 22:07:34 +00002854 DISubprogram SP((*AI)->getScopeNode());
2855 if (SP.Verify()) {
2856 // Collect info for variables that were optimized out.
2857 StringRef FName = SP.getLinkageName();
2858 if (FName.empty())
2859 FName = SP.getName();
2860 const Module *M = MF->getFunction()->getParent();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002861 if (NamedMDNode *NMD =
2862 M->getNamedMetadata(Twine("llvm.dbg.lv.",
Devang Patel5c0f85c2010-06-25 22:07:34 +00002863 getRealLinkageName(FName)))) {
2864 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00002865 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel5c0f85c2010-06-25 22:07:34 +00002866 if (!DV || !ProcessedVars.insert(DV))
2867 continue;
Devang Patel648df7b2010-06-30 00:11:08 +00002868 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel5c0f85c2010-06-25 22:07:34 +00002869 if (Scope)
2870 Scope->addVariable(new DbgVariable(DV));
2871 }
2872 }
2873 }
Devang Patelc5b31092010-06-30 01:40:11 +00002874 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2875 constructScopeDIE(*AI);
Devang Patel5c0f85c2010-06-25 22:07:34 +00002876 }
Devang Patel30265c42010-07-07 20:12:52 +00002877
Devang Patel075e9b52010-05-04 06:15:30 +00002878 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002879
Devang Patel075e9b52010-05-04 06:15:30 +00002880 if (!DisableFramePointerElim(*MF))
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002881 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
Devang Patel075e9b52010-05-04 06:15:30 +00002882 dwarf::DW_FORM_flag, 1);
2883
2884
Chris Lattner3a383cb2010-04-05 00:13:49 +00002885 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel530a0752010-01-04 20:44:00 +00002886 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00002887 }
2888
Bill Wendling2b128d72009-05-20 23:19:06 +00002889 // Clear debug info
Devang Patelfe189e62010-01-19 01:26:02 +00002890 CurrentFnDbgScope = NULL;
Devang Patel002d54d2010-05-26 19:37:24 +00002891 InsnNeedsLabel.clear();
Devang Patele1c53f22010-05-20 16:36:41 +00002892 DbgVariableToFrameIndexMap.clear();
2893 VarToAbstractVarMap.clear();
2894 DbgVariableToDbgInstMap.clear();
2895 DbgVariableLabelsMap.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002896 DeleteContainerSeconds(DbgScopeMap);
Devang Patel541019d2010-04-09 16:04:20 +00002897 InsnsEndScopeSet.clear();
Devang Patelfe189e62010-01-19 01:26:02 +00002898 ConcreteScopes.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002899 DeleteContainerSeconds(AbstractScopes);
Devang Patelfe189e62010-01-19 01:26:02 +00002900 AbstractScopesList.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002901 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00002902 LabelsBeforeInsn.clear();
2903 LabelsAfterInsn.clear();
Bill Wendling2b128d72009-05-20 23:19:06 +00002904 Lines.clear();
Devang Patel12563b32010-04-16 23:33:45 +00002905 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00002906}
2907
Devang Patele1c53f22010-05-20 16:36:41 +00002908/// recordVariableFrameIndex - Record a variable's index.
2909void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2910 assert (V && "Invalid DbgVariable!");
2911 DbgVariableToFrameIndexMap[V] = Index;
2912}
2913
2914/// findVariableFrameIndex - Return true if frame index for the variable
2915/// is found. Update FI to hold value of the index.
2916bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2917 assert (V && "Invalid DbgVariable!");
2918 DenseMap<const DbgVariable *, int>::iterator I =
2919 DbgVariableToFrameIndexMap.find(V);
2920 if (I == DbgVariableToFrameIndexMap.end())
2921 return false;
2922 *FI = I->second;
2923 return true;
2924}
2925
2926/// findVariableLabel - Find MCSymbol for the variable.
2927const MCSymbol *DwarfDebug::findVariableLabel(const DbgVariable *V) {
2928 DenseMap<const DbgVariable *, const MCSymbol *>::iterator I
2929 = DbgVariableLabelsMap.find(V);
2930 if (I == DbgVariableLabelsMap.end())
2931 return NULL;
2932 else return I->second;
2933}
2934
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002935/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patel490c8ab2010-05-20 19:57:06 +00002936/// instruction.
2937DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2938 DbgScope *Scope = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002939 LLVMContext &Ctx =
Devang Patel490c8ab2010-05-20 19:57:06 +00002940 MInsn->getParent()->getParent()->getFunction()->getContext();
2941 DebugLoc DL = MInsn->getDebugLoc();
2942
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002943 if (DL.isUnknown())
Devang Patel490c8ab2010-05-20 19:57:06 +00002944 return Scope;
2945
2946 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2947 Scope = ConcreteScopes.lookup(IA);
2948 if (Scope == 0)
2949 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002950
Devang Patel490c8ab2010-05-20 19:57:06 +00002951 return Scope;
2952}
2953
2954
Chris Lattnerba35a672010-03-09 04:54:43 +00002955/// recordSourceLine - Register a source line with debug info. Returns the
2956/// unique label that was emitted and which provides correspondence to
2957/// the source line list.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002958MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel30265c42010-07-07 20:12:52 +00002959 const MDNode *S) {
Devang Patel2d9caf92009-11-25 17:36:49 +00002960 StringRef Dir;
2961 StringRef Fn;
Devang Patel2089d162009-10-05 18:03:19 +00002962
Dan Gohman50849c62010-05-05 23:41:32 +00002963 unsigned Src = 1;
2964 if (S) {
2965 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00002966
Dan Gohman50849c62010-05-05 23:41:32 +00002967 if (Scope.isCompileUnit()) {
2968 DICompileUnit CU(S);
2969 Dir = CU.getDirectory();
2970 Fn = CU.getFilename();
2971 } else if (Scope.isSubprogram()) {
2972 DISubprogram SP(S);
2973 Dir = SP.getDirectory();
2974 Fn = SP.getFilename();
2975 } else if (Scope.isLexicalBlock()) {
2976 DILexicalBlock DB(S);
2977 Dir = DB.getDirectory();
2978 Fn = DB.getFilename();
2979 } else
2980 assert(0 && "Unexpected scope info");
2981
2982 Src = GetOrCreateSourceID(Dir, Fn);
2983 }
2984
Chris Lattner6e52e9d2010-03-14 08:36:50 +00002985 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattnerb4666f42010-03-14 08:15:55 +00002986 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendling2b128d72009-05-20 23:19:06 +00002987
Chris Lattnerba35a672010-03-09 04:54:43 +00002988 Asm->OutStreamer.EmitLabel(Label);
2989 return Label;
Bill Wendling2b128d72009-05-20 23:19:06 +00002990}
2991
Bill Wendling806535f2009-05-20 23:22:40 +00002992//===----------------------------------------------------------------------===//
2993// Emit Methods
2994//===----------------------------------------------------------------------===//
2995
Devang Patel930143b2009-11-21 02:48:08 +00002996/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00002997///
Jim Grosbach00e9c612009-11-22 19:20:36 +00002998unsigned
2999DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00003000 // Get the children.
3001 const std::vector<DIE *> &Children = Die->getChildren();
3002
3003 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00003004 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00003005 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00003006
3007 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00003008 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00003009
3010 // Get the abbreviation for this DIE.
3011 unsigned AbbrevNumber = Die->getAbbrevNumber();
3012 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3013
3014 // Set DIE offset
3015 Die->setOffset(Offset);
3016
3017 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00003018 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00003019
3020 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
3021 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3022
3023 // Size the DIE attribute values.
3024 for (unsigned i = 0, N = Values.size(); i < N; ++i)
3025 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00003026 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00003027
3028 // Size the DIE children if any.
3029 if (!Children.empty()) {
3030 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
3031 "Children flag not set");
3032
3033 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00003034 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00003035
3036 // End of children marker.
3037 Offset += sizeof(int8_t);
3038 }
3039
3040 Die->setSize(Offset - Die->getOffset());
3041 return Offset;
3042}
3043
Devang Patel930143b2009-11-21 02:48:08 +00003044/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00003045///
Devang Patel930143b2009-11-21 02:48:08 +00003046void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003047 unsigned PrevOffset = 0;
3048 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3049 E = CUMap.end(); I != E; ++I) {
3050 // Compute size of compile unit header.
3051 static unsigned Offset = PrevOffset +
3052 sizeof(int32_t) + // Length of Compilation Unit Info
3053 sizeof(int16_t) + // DWARF version number
3054 sizeof(int32_t) + // Offset Into Abbrev. Section
3055 sizeof(int8_t); // Pointer Size (in bytes)
3056 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
3057 PrevOffset = Offset;
3058 }
Bill Wendling480ff322009-05-20 23:21:38 +00003059}
3060
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003061/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
3062/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00003063static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003064 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00003065 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003066 if (!SymbolStem) return 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003067
Chris Lattner6629ca92010-04-04 22:59:04 +00003068 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
3069 Asm->OutStreamer.EmitLabel(TmpSym);
3070 return TmpSym;
3071}
3072
3073/// EmitSectionLabels - Emit initial Dwarf sections with a label at
3074/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00003075void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003076 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00003077
Bill Wendling480ff322009-05-20 23:21:38 +00003078 // Dwarf sections base addresses.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003079 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6629ca92010-04-04 22:59:04 +00003080 DwarfFrameSectionSym =
3081 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3082 }
Bill Wendling480ff322009-05-20 23:21:38 +00003083
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003084 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00003085 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003086 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00003087 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003088 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003089
Chris Lattner6629ca92010-04-04 22:59:04 +00003090 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003091 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003092
Devang Patel4a213872010-08-24 00:06:12 +00003093 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003094 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3095 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3096 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003097 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00003098 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00003099 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3100 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00003101
Devang Patel9fc11702010-05-25 23:40:22 +00003102 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3103 "section_debug_loc");
3104
Chris Lattner6629ca92010-04-04 22:59:04 +00003105 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00003106 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003107}
3108
Devang Patel930143b2009-11-21 02:48:08 +00003109/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00003110///
Devang Patel930143b2009-11-21 02:48:08 +00003111void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00003112 // Get the abbreviation for this DIE.
3113 unsigned AbbrevNumber = Die->getAbbrevNumber();
3114 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3115
Bill Wendling480ff322009-05-20 23:21:38 +00003116 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00003117 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00003118 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3119 Twine::utohexstr(Die->getOffset()) + ":0x" +
3120 Twine::utohexstr(Die->getSize()) + " " +
3121 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00003122 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00003123
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00003124 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00003125 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3126
3127 // Emit the DIE attribute values.
3128 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3129 unsigned Attr = AbbrevData[i].getAttribute();
3130 unsigned Form = AbbrevData[i].getForm();
3131 assert(Form && "Too many attributes for DIE (check abbreviation)");
3132
Chris Lattner7bde8c02010-04-04 18:52:31 +00003133 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00003134 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003135
Bill Wendling480ff322009-05-20 23:21:38 +00003136 switch (Attr) {
3137 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00003138 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003139 break;
3140 case dwarf::DW_AT_abstract_origin: {
3141 DIEEntry *E = cast<DIEEntry>(Values[i]);
3142 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003143 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00003144 Asm->EmitInt32(Addr);
3145 break;
3146 }
Devang Patel12563b32010-04-16 23:33:45 +00003147 case dwarf::DW_AT_ranges: {
3148 // DW_AT_range Value encodes offset in debug_range section.
3149 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00003150
3151 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
3152 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
3153 V->getValue(),
3154 4);
3155 } else {
3156 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3157 V->getValue(),
3158 DwarfDebugRangeSectionSym,
3159 4);
3160 }
Devang Patel12563b32010-04-16 23:33:45 +00003161 break;
3162 }
Devang Patel9fc11702010-05-25 23:40:22 +00003163 case dwarf::DW_AT_location: {
3164 if (UseDotDebugLocEntry.count(Die) != 0) {
3165 DIELabel *L = cast<DIELabel>(Values[i]);
3166 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3167 } else
3168 Values[i]->EmitValue(Asm, Form);
3169 break;
3170 }
Bill Wendling480ff322009-05-20 23:21:38 +00003171 default:
3172 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003173 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00003174 break;
3175 }
Bill Wendling480ff322009-05-20 23:21:38 +00003176 }
3177
3178 // Emit the DIE children if any.
3179 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3180 const std::vector<DIE *> &Children = Die->getChildren();
3181
3182 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00003183 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00003184
Chris Lattner7bde8c02010-04-04 18:52:31 +00003185 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00003186 Asm->OutStreamer.AddComment("End Of Children Mark");
3187 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003188 }
3189}
3190
Devang Patel9ccfb642009-12-09 18:24:21 +00003191/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00003192///
Devang Patel9ccfb642009-12-09 18:24:21 +00003193void DwarfDebug::emitDebugInfo() {
3194 // Start debug info section.
3195 Asm->OutStreamer.SwitchSection(
3196 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00003197 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3198 E = CUMap.end(); I != E; ++I) {
3199 CompileUnit *TheCU = I->second;
3200 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003201
Devang Patel1a0df9a2010-05-10 22:49:55 +00003202 // Emit the compile units header.
3203 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3204 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003205
Devang Patel1a0df9a2010-05-10 22:49:55 +00003206 // Emit size of content not including length itself
3207 unsigned ContentSize = Die->getSize() +
3208 sizeof(int16_t) + // DWARF version number
3209 sizeof(int32_t) + // Offset Into Abbrev. Section
3210 sizeof(int8_t) + // Pointer Size (in bytes)
3211 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003212
Devang Patel1a0df9a2010-05-10 22:49:55 +00003213 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3214 Asm->EmitInt32(ContentSize);
3215 Asm->OutStreamer.AddComment("DWARF version number");
3216 Asm->EmitInt16(dwarf::DWARF_VERSION);
3217 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3218 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3219 DwarfAbbrevSectionSym);
3220 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3221 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003222
Devang Patel1a0df9a2010-05-10 22:49:55 +00003223 emitDIE(Die);
3224 // FIXME - extra padding for gdb bug.
3225 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3226 Asm->EmitInt8(0);
3227 Asm->EmitInt8(0);
3228 Asm->EmitInt8(0);
3229 Asm->EmitInt8(0);
3230 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3231 }
Bill Wendling480ff322009-05-20 23:21:38 +00003232}
3233
Devang Patel930143b2009-11-21 02:48:08 +00003234/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00003235///
Devang Patel930143b2009-11-21 02:48:08 +00003236void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00003237 // Check to see if it is worth the effort.
3238 if (!Abbreviations.empty()) {
3239 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003240 Asm->OutStreamer.SwitchSection(
3241 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003242
Chris Lattnera179b522010-04-04 19:25:43 +00003243 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003244
3245 // For each abbrevation.
3246 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3247 // Get abbreviation data
3248 const DIEAbbrev *Abbrev = Abbreviations[i];
3249
3250 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00003251 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00003252
3253 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003254 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00003255 }
3256
3257 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00003258 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00003259
Chris Lattnera179b522010-04-04 19:25:43 +00003260 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003261 }
3262}
3263
Devang Patel930143b2009-11-21 02:48:08 +00003264/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00003265/// the line matrix.
3266///
Devang Patel930143b2009-11-21 02:48:08 +00003267void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00003268 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00003269 Asm->OutStreamer.AddComment("Extended Op");
3270 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003271
Chris Lattner566cae92010-03-09 23:52:58 +00003272 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003273 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00003274 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3275 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3276
3277 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00003278
Chris Lattnera179b522010-04-04 19:25:43 +00003279 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003280 Asm->getTargetData().getPointerSize(),
3281 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003282
3283 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00003284 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3285 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003286 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00003287 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003288}
3289
Devang Patel930143b2009-11-21 02:48:08 +00003290/// emitDebugLines - Emit source line information.
Bill Wendling480ff322009-05-20 23:21:38 +00003291///
Devang Patel930143b2009-11-21 02:48:08 +00003292void DwarfDebug::emitDebugLines() {
Bill Wendling480ff322009-05-20 23:21:38 +00003293 // If the target is using .loc/.file, the assembler will be emitting the
3294 // .debug_line table automatically.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003295 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling480ff322009-05-20 23:21:38 +00003296 return;
3297
3298 // Minimum line delta, thus ranging from -10..(255-10).
3299 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
3300 // Maximum line delta, thus ranging from -10..(255-10).
3301 const int MaxLineDelta = 255 + MinLineDelta;
3302
3303 // Start the dwarf line section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003304 Asm->OutStreamer.SwitchSection(
3305 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003306
3307 // Construct the section header.
Chris Lattner566cae92010-03-09 23:52:58 +00003308 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003309 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
3310 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnera179b522010-04-04 19:25:43 +00003311 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003312
Chris Lattner566cae92010-03-09 23:52:58 +00003313 Asm->OutStreamer.AddComment("DWARF version number");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003314 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling480ff322009-05-20 23:21:38 +00003315
Chris Lattner566cae92010-03-09 23:52:58 +00003316 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003317 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
3318 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnera179b522010-04-04 19:25:43 +00003319 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003320
Chris Lattner566cae92010-03-09 23:52:58 +00003321 Asm->OutStreamer.AddComment("Minimum Instruction Length");
3322 Asm->EmitInt8(1);
3323 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
3324 Asm->EmitInt8(1);
3325 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
3326 Asm->EmitInt8(MinLineDelta);
3327 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3328 Asm->EmitInt8(MaxLineDelta);
3329 Asm->OutStreamer.AddComment("Special Opcode Base");
3330 Asm->EmitInt8(-MinLineDelta);
Bill Wendling480ff322009-05-20 23:21:38 +00003331
3332 // Line number standard opcode encodings argument count
Chris Lattner566cae92010-03-09 23:52:58 +00003333 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3334 Asm->EmitInt8(0);
3335 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3336 Asm->EmitInt8(1);
3337 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3338 Asm->EmitInt8(1);
3339 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3340 Asm->EmitInt8(1);
3341 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3342 Asm->EmitInt8(1);
3343 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3344 Asm->EmitInt8(0);
3345 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3346 Asm->EmitInt8(0);
3347 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3348 Asm->EmitInt8(0);
3349 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3350 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003351
3352 // Emit directories.
3353 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003354 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner7bde8c02010-04-04 18:52:31 +00003355 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003356 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling480ff322009-05-20 23:21:38 +00003357 }
3358
Chris Lattner566cae92010-03-09 23:52:58 +00003359 Asm->OutStreamer.AddComment("End of directories");
3360 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003361
3362 // Emit files.
3363 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3364 // Remember source id starts at 1.
3365 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003366 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner7bde8c02010-04-04 18:52:31 +00003367 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003368 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003369
Chris Lattner9efd1182010-04-04 19:09:29 +00003370 Asm->EmitULEB128(Id.first, "Directory #");
3371 Asm->EmitULEB128(0, "Mod date");
3372 Asm->EmitULEB128(0, "File size");
Bill Wendling480ff322009-05-20 23:21:38 +00003373 }
3374
Chris Lattner566cae92010-03-09 23:52:58 +00003375 Asm->OutStreamer.AddComment("End of files");
3376 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003377
Chris Lattnera179b522010-04-04 19:25:43 +00003378 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003379
3380 // A sequence for each text section.
3381 unsigned SecSrcLinesSize = SectionSourceLines.size();
3382
3383 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3384 // Isolate current sections line info.
3385 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3386
Bill Wendling480ff322009-05-20 23:21:38 +00003387 // Dwarf assumes we start with first line of first source file.
3388 unsigned Source = 1;
3389 unsigned Line = 1;
3390
3391 // Construct rows of the address, source, line, column matrix.
3392 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3393 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattnerb4666f42010-03-14 08:15:55 +00003394 MCSymbol *Label = LineInfo.getLabel();
Chris Lattneree95d4c2010-03-14 02:20:58 +00003395 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling480ff322009-05-20 23:21:38 +00003396
Chris Lattner3d72a672010-03-09 23:38:23 +00003397 if (Asm->isVerbose()) {
Chris Lattner7e998b72010-03-10 01:04:13 +00003398 std::pair<unsigned, unsigned> SrcID =
Bill Wendling480ff322009-05-20 23:21:38 +00003399 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner7e998b72010-03-10 01:04:13 +00003400 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattnera26fbe42010-03-10 02:29:31 +00003401 "/" +
3402 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner7e998b72010-03-10 01:04:13 +00003403 ":" + Twine(LineInfo.getLine()));
Bill Wendling480ff322009-05-20 23:21:38 +00003404 }
3405
3406 // Define the line address.
Chris Lattner566cae92010-03-09 23:52:58 +00003407 Asm->OutStreamer.AddComment("Extended Op");
3408 Asm->EmitInt8(0);
3409 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003410 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00003411
3412 Asm->OutStreamer.AddComment("DW_LNE_set_address");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003413 Asm->EmitInt8(dwarf::DW_LNE_set_address);
Chris Lattner566cae92010-03-09 23:52:58 +00003414
3415 Asm->OutStreamer.AddComment("Location label");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003416 Asm->OutStreamer.EmitSymbolValue(Label,
3417 Asm->getTargetData().getPointerSize(),
Chris Lattneree95d4c2010-03-14 02:20:58 +00003418 0/*AddrSpace*/);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003419
Bill Wendling480ff322009-05-20 23:21:38 +00003420 // If change of source, then switch to the new source.
3421 if (Source != LineInfo.getSourceID()) {
3422 Source = LineInfo.getSourceID();
Chris Lattner566cae92010-03-09 23:52:58 +00003423 Asm->OutStreamer.AddComment("DW_LNS_set_file");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003424 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner9efd1182010-04-04 19:09:29 +00003425 Asm->EmitULEB128(Source, "New Source");
Bill Wendling480ff322009-05-20 23:21:38 +00003426 }
3427
3428 // If change of line.
3429 if (Line != LineInfo.getLine()) {
3430 // Determine offset.
3431 int Offset = LineInfo.getLine() - Line;
3432 int Delta = Offset - MinLineDelta;
3433
3434 // Update line.
3435 Line = LineInfo.getLine();
3436
3437 // If delta is small enough and in range...
3438 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3439 // ... then use fast opcode.
Chris Lattner566cae92010-03-09 23:52:58 +00003440 Asm->OutStreamer.AddComment("Line Delta");
3441 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling480ff322009-05-20 23:21:38 +00003442 } else {
3443 // ... otherwise use long hand.
Chris Lattner566cae92010-03-09 23:52:58 +00003444 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling480ff322009-05-20 23:21:38 +00003445 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner9efd1182010-04-04 19:09:29 +00003446 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner566cae92010-03-09 23:52:58 +00003447 Asm->OutStreamer.AddComment("DW_LNS_copy");
3448 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling480ff322009-05-20 23:21:38 +00003449 }
3450 } else {
3451 // Copy the previous row (different address or source)
Chris Lattner566cae92010-03-09 23:52:58 +00003452 Asm->OutStreamer.AddComment("DW_LNS_copy");
3453 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling480ff322009-05-20 23:21:38 +00003454 }
3455 }
3456
Devang Patel930143b2009-11-21 02:48:08 +00003457 emitEndOfLineMatrix(j + 1);
Bill Wendling480ff322009-05-20 23:21:38 +00003458 }
3459
3460 if (SecSrcLinesSize == 0)
3461 // Because we're emitting a debug_line section, we still need a line
3462 // table. The linker and friends expect it to exist. If there's nothing to
3463 // put into it, emit an empty table.
Devang Patel930143b2009-11-21 02:48:08 +00003464 emitEndOfLineMatrix(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003465
Chris Lattnera179b522010-04-04 19:25:43 +00003466 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003467}
3468
Devang Patel930143b2009-11-21 02:48:08 +00003469/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling480ff322009-05-20 23:21:38 +00003470///
Devang Patel930143b2009-11-21 02:48:08 +00003471void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003472 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003473 return;
3474
Chris Lattner3a383cb2010-04-05 00:13:49 +00003475 int stackGrowth = Asm->getTargetData().getPointerSize();
3476 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3477 TargetFrameInfo::StackGrowsDown)
3478 stackGrowth *= -1;
Bill Wendling480ff322009-05-20 23:21:38 +00003479
3480 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003481 Asm->OutStreamer.SwitchSection(
3482 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003483
Chris Lattnera179b522010-04-04 19:25:43 +00003484 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner566cae92010-03-09 23:52:58 +00003485 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003486 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3487 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003488
Chris Lattnera179b522010-04-04 19:25:43 +00003489 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner566cae92010-03-09 23:52:58 +00003490 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling480ff322009-05-20 23:21:38 +00003491 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner566cae92010-03-09 23:52:58 +00003492 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling480ff322009-05-20 23:21:38 +00003493 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner566cae92010-03-09 23:52:58 +00003494 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003495 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner9efd1182010-04-04 19:09:29 +00003496 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3497 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner566cae92010-03-09 23:52:58 +00003498 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003499 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling480ff322009-05-20 23:21:38 +00003500 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling480ff322009-05-20 23:21:38 +00003501
3502 std::vector<MachineMove> Moves;
3503 RI->getInitialFrameState(Moves);
3504
Chris Lattneraabc6042010-04-04 23:41:46 +00003505 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003506
Chris Lattner9e06e532010-04-28 01:05:45 +00003507 Asm->EmitAlignment(2);
Chris Lattnera179b522010-04-04 19:25:43 +00003508 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003509}
3510
Devang Patel930143b2009-11-21 02:48:08 +00003511/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling480ff322009-05-20 23:21:38 +00003512/// section.
Chris Lattner2c3f4782010-03-13 07:26:18 +00003513void DwarfDebug::
3514emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003515 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003516 return;
3517
3518 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003519 Asm->OutStreamer.SwitchSection(
3520 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003521
Chris Lattner566cae92010-03-09 23:52:58 +00003522 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner2c3f4782010-03-13 07:26:18 +00003523 MCSymbol *DebugFrameBegin =
Chris Lattnera179b522010-04-04 19:25:43 +00003524 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003525 MCSymbol *DebugFrameEnd =
Chris Lattnera179b522010-04-04 19:25:43 +00003526 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerf1429f12010-04-04 19:58:12 +00003527 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003528
Chris Lattner2c3f4782010-03-13 07:26:18 +00003529 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling480ff322009-05-20 23:21:38 +00003530
Chris Lattner566cae92010-03-09 23:52:58 +00003531 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003532 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner70a4fce2010-04-04 23:25:33 +00003533 DwarfFrameSectionSym);
Bill Wendling480ff322009-05-20 23:21:38 +00003534
Chris Lattner566cae92010-03-09 23:52:58 +00003535 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnera179b522010-04-04 19:25:43 +00003536 MCSymbol *FuncBeginSym =
3537 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner8811e122010-03-13 07:40:56 +00003538 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattner3a383cb2010-04-05 00:13:49 +00003539 Asm->getTargetData().getPointerSize(),
3540 0/*AddrSpace*/);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003541
3542
Chris Lattner566cae92010-03-09 23:52:58 +00003543 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003544 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003545 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003546
Chris Lattneraabc6042010-04-04 23:41:46 +00003547 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003548
Chris Lattner9e06e532010-04-28 01:05:45 +00003549 Asm->EmitAlignment(2);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003550 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling480ff322009-05-20 23:21:38 +00003551}
3552
Devang Patel9ccfb642009-12-09 18:24:21 +00003553/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3554///
3555void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003556 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3557 E = CUMap.end(); I != E; ++I) {
3558 CompileUnit *TheCU = I->second;
3559 // Start the dwarf pubnames section.
3560 Asm->OutStreamer.SwitchSection(
3561 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003562
Devang Patel1a0df9a2010-05-10 22:49:55 +00003563 Asm->OutStreamer.AddComment("Length of Public Names Info");
3564 Asm->EmitLabelDifference(
3565 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3566 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003567
Devang Patel1a0df9a2010-05-10 22:49:55 +00003568 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3569 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003570
Devang Patel1a0df9a2010-05-10 22:49:55 +00003571 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003572 Asm->EmitInt16(dwarf::DWARF_VERSION);
3573
Devang Patel1a0df9a2010-05-10 22:49:55 +00003574 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003575 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00003576 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003577
Devang Patel1a0df9a2010-05-10 22:49:55 +00003578 Asm->OutStreamer.AddComment("Compilation Unit Length");
3579 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3580 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3581 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003582
Devang Patel1a0df9a2010-05-10 22:49:55 +00003583 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3584 for (StringMap<DIE*>::const_iterator
3585 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3586 const char *Name = GI->getKeyData();
3587 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003588
Devang Patel1a0df9a2010-05-10 22:49:55 +00003589 Asm->OutStreamer.AddComment("DIE offset");
3590 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003591
Devang Patel1a0df9a2010-05-10 22:49:55 +00003592 if (Asm->isVerbose())
3593 Asm->OutStreamer.AddComment("External Name");
3594 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3595 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003596
Devang Patel1a0df9a2010-05-10 22:49:55 +00003597 Asm->OutStreamer.AddComment("End Mark");
3598 Asm->EmitInt32(0);
3599 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3600 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00003601 }
Bill Wendling480ff322009-05-20 23:21:38 +00003602}
3603
Devang Patel04d2f2d2009-11-24 01:14:22 +00003604void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003605 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3606 E = CUMap.end(); I != E; ++I) {
3607 CompileUnit *TheCU = I->second;
3608 // Start the dwarf pubnames section.
3609 Asm->OutStreamer.SwitchSection(
3610 Asm->getObjFileLowering().getDwarfPubTypesSection());
3611 Asm->OutStreamer.AddComment("Length of Public Types Info");
3612 Asm->EmitLabelDifference(
3613 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3614 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003615
Devang Patel1a0df9a2010-05-10 22:49:55 +00003616 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3617 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003618
Devang Patel1a0df9a2010-05-10 22:49:55 +00003619 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3620 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003621
Devang Patel1a0df9a2010-05-10 22:49:55 +00003622 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3623 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3624 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003625
Devang Patel1a0df9a2010-05-10 22:49:55 +00003626 Asm->OutStreamer.AddComment("Compilation Unit Length");
3627 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3628 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3629 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003630
Devang Patel1a0df9a2010-05-10 22:49:55 +00003631 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3632 for (StringMap<DIE*>::const_iterator
3633 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3634 const char *Name = GI->getKeyData();
3635 DIE * Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003636
Devang Patel1a0df9a2010-05-10 22:49:55 +00003637 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3638 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003639
Devang Patel1a0df9a2010-05-10 22:49:55 +00003640 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3641 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3642 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003643
Devang Patel1a0df9a2010-05-10 22:49:55 +00003644 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003645 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00003646 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3647 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00003648 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00003649}
3650
Devang Patel930143b2009-11-21 02:48:08 +00003651/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00003652///
Devang Patel930143b2009-11-21 02:48:08 +00003653void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00003654 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00003655 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003656
Chris Lattner3d72a672010-03-09 23:38:23 +00003657 // Start the dwarf str section.
3658 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003659 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003660
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003661 // Get all of the string pool entries and put them in an array by their ID so
3662 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003663 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003664 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003665
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003666 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3667 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3668 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003669
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003670 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003671
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003672 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00003673 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003674 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003675
Chris Lattner3d72a672010-03-09 23:38:23 +00003676 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003677 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003678 }
3679}
3680
Devang Patel930143b2009-11-21 02:48:08 +00003681/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00003682///
Devang Patel930143b2009-11-21 02:48:08 +00003683void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003684 if (DotDebugLocEntries.empty())
3685 return;
3686
Bill Wendling480ff322009-05-20 23:21:38 +00003687 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003688 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00003689 Asm->getObjFileLowering().getDwarfLocSection());
3690 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003691 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3692 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003693 for (SmallVector<DotDebugLocEntry, 4>::iterator
3694 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00003695 I != E; ++I, ++index) {
Devang Patel9fc11702010-05-25 23:40:22 +00003696 DotDebugLocEntry Entry = *I;
Devang Patel9fc11702010-05-25 23:40:22 +00003697 if (Entry.isEmpty()) {
3698 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3699 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003700 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00003701 } else {
3702 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3703 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3704 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3705 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patel0e60e672010-08-04 18:40:52 +00003706 if (int Offset = Entry.Loc.getOffset()) {
3707 // If the value is at a certain offset from frame register then
3708 // use DW_OP_fbreg.
3709 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patel9fc11702010-05-25 23:40:22 +00003710 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patel0e60e672010-08-04 18:40:52 +00003711 Asm->EmitInt16(1 + OffsetSize);
3712 Asm->OutStreamer.AddComment(
3713 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
3714 Asm->EmitInt8(dwarf::DW_OP_fbreg);
3715 Asm->OutStreamer.AddComment("Offset");
3716 Asm->EmitSLEB128(Offset);
Devang Patel9fc11702010-05-25 23:40:22 +00003717 } else {
Devang Patel0e60e672010-08-04 18:40:52 +00003718 if (Reg < 32) {
3719 Asm->OutStreamer.AddComment("Loc expr size");
3720 Asm->EmitInt16(1);
3721 Asm->OutStreamer.AddComment(
Devang Patel6d21f612010-08-04 20:32:36 +00003722 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patel0e60e672010-08-04 18:40:52 +00003723 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3724 } else {
3725 Asm->OutStreamer.AddComment("Loc expr size");
3726 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
3727 Asm->EmitInt8(dwarf::DW_OP_regx);
3728 Asm->EmitULEB128(Reg);
3729 }
Devang Patel9fc11702010-05-25 23:40:22 +00003730 }
3731 }
3732 }
Bill Wendling480ff322009-05-20 23:21:38 +00003733}
3734
3735/// EmitDebugARanges - Emit visible names into a debug aranges section.
3736///
3737void DwarfDebug::EmitDebugARanges() {
3738 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003739 Asm->OutStreamer.SwitchSection(
3740 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003741}
3742
Devang Patel930143b2009-11-21 02:48:08 +00003743/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00003744///
Devang Patel930143b2009-11-21 02:48:08 +00003745void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00003746 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003747 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00003748 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00003749 unsigned char Size = Asm->getTargetData().getPointerSize();
3750 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003751 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00003752 I != E; ++I) {
3753 if (*I)
3754 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00003755 else
Devang Patel6c74a872010-04-27 19:46:33 +00003756 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00003757 }
Bill Wendling480ff322009-05-20 23:21:38 +00003758}
3759
Devang Patel930143b2009-11-21 02:48:08 +00003760/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00003761///
Devang Patel930143b2009-11-21 02:48:08 +00003762void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00003763 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00003764 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00003765 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003766 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003767 }
3768}
3769
Devang Patel930143b2009-11-21 02:48:08 +00003770/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00003771/// Section Header:
3772/// 1. length of section
3773/// 2. Dwarf version number
3774/// 3. address size.
3775///
3776/// Entries (one "entry" for each function that was inlined):
3777///
3778/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3779/// otherwise offset into __debug_str for regular function name.
3780/// 2. offset into __debug_str section for regular function name.
3781/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3782/// instances for the function.
3783///
3784/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3785/// inlined instance; the die_offset points to the inlined_subroutine die in the
3786/// __debug_info section, and the low_pc is the starting address for the
3787/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00003788void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003789 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003790 return;
3791
Devang Patel1a0df9a2010-05-10 22:49:55 +00003792 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00003793 return;
3794
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003795 Asm->OutStreamer.SwitchSection(
3796 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003797
Chris Lattner566cae92010-03-09 23:52:58 +00003798 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003799 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3800 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003801
Chris Lattnera179b522010-04-04 19:25:43 +00003802 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003803
Chris Lattner566cae92010-03-09 23:52:58 +00003804 Asm->OutStreamer.AddComment("Dwarf Version");
3805 Asm->EmitInt16(dwarf::DWARF_VERSION);
3806 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003807 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003808
Devang Patel32cc43c2010-05-07 20:54:48 +00003809 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003810 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00003811
Devang Patel32cc43c2010-05-07 20:54:48 +00003812 const MDNode *Node = *I;
3813 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00003814 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003815 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00003816 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00003817 StringRef LName = SP.getLinkageName();
3818 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00003819
Chris Lattner566cae92010-03-09 23:52:58 +00003820 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003821 if (LName.empty()) {
3822 Asm->OutStreamer.EmitBytes(Name, 0);
3823 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003824 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00003825 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3826 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003827
Chris Lattner566cae92010-03-09 23:52:58 +00003828 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00003829 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00003830 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00003831
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003832 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00003833 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00003834 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00003835 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003836
Chris Lattner7bde8c02010-04-04 18:52:31 +00003837 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003838 Asm->OutStreamer.EmitSymbolValue(LI->first,
3839 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00003840 }
3841 }
3842
Chris Lattnera179b522010-04-04 19:25:43 +00003843 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003844}