blob: 775012b0531c70d7c0d0d86577672632397ecaca [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
Devang Patel30265c42010-07-07 20:12:52 +000047static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
48 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 ///
Benjamin Kramer96956ed2010-03-31 20:15:45 +0000119 void addGlobalType(StringRef Name, DIE *Die) {
Devang Patel04d2f2d2009-11-24 01:14:22 +0000120 GlobalTypes[Name] = Die;
121 }
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.
Devang Patel32cc43c2010-05-07 20:54:48 +0000134 DIEEntry *getDIEEntry(const MDNode *N) {
Devang Patel9a0339f2010-07-07 20:49:57 +0000135 DenseMap<const MDNode *, DIEEntry *>::iterator I = MDNodeToDIEEntryMap.find(N);
136 if (I == MDNodeToDIEEntryMap.end())
Devang Patel1f4690c2009-12-15 19:16:48 +0000137 return NULL;
138 return I->second;
139 }
Devang Patele064ad42009-11-20 21:37:22 +0000140
141 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patel32cc43c2010-05-07 20:54:48 +0000142 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Patel9a0339f2010-07-07 20:49:57 +0000143 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling2f921f82009-05-15 09:23:25 +0000144 }
145
Devang Patel930143b2009-11-21 02:48:08 +0000146 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling2f921f82009-05-15 09:23:25 +0000147 ///
Devang Patel930143b2009-11-21 02:48:08 +0000148 void addDie(DIE *Buffer) {
149 this->CUDie->addChild(Buffer);
Bill Wendling2f921f82009-05-15 09:23:25 +0000150 }
Devang Patel92e8c652009-11-21 00:31:03 +0000151
152 // getIndexTyDie - Get an anonymous type for index type.
153 DIE *getIndexTyDie() {
154 return IndexTyDie;
155 }
156
Jim Grosbach00e9c612009-11-22 19:20:36 +0000157 // setIndexTyDie - Set D as anonymous type for index which can be reused
158 // later.
Devang Patel92e8c652009-11-21 00:31:03 +0000159 void setIndexTyDie(DIE *D) {
160 IndexTyDie = D;
161 }
162
Bill Wendling2f921f82009-05-15 09:23:25 +0000163};
164
165//===----------------------------------------------------------------------===//
166/// DbgVariable - This class is used to track local variable information.
167///
Devang Patelf3d7c082009-11-16 21:53:40 +0000168class DbgVariable {
Bill Wendling2f921f82009-05-15 09:23:25 +0000169 DIVariable Var; // Variable Descriptor.
Devang Patel9fc11702010-05-25 23:40:22 +0000170 DIE *TheDIE; // Variable DIE.
171 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
Bill Wendling2f921f82009-05-15 09:23:25 +0000172public:
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000173 // AbsVar may be NULL.
Devang Patel9fc11702010-05-25 23:40:22 +0000174 DbgVariable(DIVariable V) : Var(V), TheDIE(0), DotDebugLocOffset(~0U) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000175
176 // Accessors.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000177 DIVariable getVariable() const { return Var; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000178 void setDIE(DIE *D) { TheDIE = D; }
179 DIE *getDIE() const { return TheDIE; }
Devang Patel9fc11702010-05-25 23:40:22 +0000180 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
181 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000182};
183
184//===----------------------------------------------------------------------===//
Devang Patel6c74a872010-04-27 19:46:33 +0000185/// DbgRange - This is used to track range of instructions with identical
186/// debug info scope.
187///
188typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
189
190//===----------------------------------------------------------------------===//
Bill Wendling2f921f82009-05-15 09:23:25 +0000191/// DbgScope - This class is used to track scope information.
192///
Devang Patelf3d7c082009-11-16 21:53:40 +0000193class DbgScope {
Bill Wendling2f921f82009-05-15 09:23:25 +0000194 DbgScope *Parent; // Parent to this scope.
Jim Grosbach042483e2009-11-21 23:12:12 +0000195 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel1973df22010-01-26 21:39:14 +0000196 // Location at which this scope is inlined.
Devang Patel32cc43c2010-05-07 20:54:48 +0000197 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000198 bool AbstractScope; // Abstract Scope
Devang Patel787f94c2009-10-01 18:25:23 +0000199 const MachineInstr *LastInsn; // Last instruction of this scope.
200 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patel6c74a872010-04-27 19:46:33 +0000201 unsigned DFSIn, DFSOut;
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000202 // Scopes defined in scope. Contents not owned.
203 SmallVector<DbgScope *, 4> Scopes;
204 // Variables declared in scope. Contents owned.
205 SmallVector<DbgVariable *, 8> Variables;
Devang Patel6c74a872010-04-27 19:46:33 +0000206 SmallVector<DbgRange, 4> Ranges;
Owen Anderson9becc182009-06-24 22:53:20 +0000207 // Private state for dump()
208 mutable unsigned IndentLevel;
Bill Wendling2f921f82009-05-15 09:23:25 +0000209public:
Devang Patel32cc43c2010-05-07 20:54:48 +0000210 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000211 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patel6c74a872010-04-27 19:46:33 +0000212 LastInsn(0), FirstInsn(0),
213 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000214 virtual ~DbgScope();
215
216 // Accessors.
217 DbgScope *getParent() const { return Parent; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000218 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000219 DIDescriptor getDesc() const { return Desc; }
Devang Patel32cc43c2010-05-07 20:54:48 +0000220 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
221 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000222 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
223 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel6c74a872010-04-27 19:46:33 +0000224 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
225
226 /// openInsnRange - This scope covers instruction range starting from MI.
227 void openInsnRange(const MachineInstr *MI) {
228 if (!FirstInsn)
229 FirstInsn = MI;
230
231 if (Parent)
232 Parent->openInsnRange(MI);
233 }
234
235 /// extendInsnRange - Extend the current instruction range covered by
236 /// this scope.
237 void extendInsnRange(const MachineInstr *MI) {
238 assert (FirstInsn && "MI Range is not open!");
239 LastInsn = MI;
240 if (Parent)
241 Parent->extendInsnRange(MI);
242 }
243
244 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
245 /// until now. This is used when a new scope is encountered while walking
246 /// machine instructions.
247 void closeInsnRange(DbgScope *NewScope = NULL) {
248 assert (LastInsn && "Last insn missing!");
249 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
250 FirstInsn = NULL;
251 LastInsn = NULL;
252 // If Parent dominates NewScope then do not close Parent's instruction
253 // range.
254 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
255 Parent->closeInsnRange(NewScope);
256 }
257
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000258 void setAbstractScope() { AbstractScope = true; }
259 bool isAbstractScope() const { return AbstractScope; }
Devang Patel6c74a872010-04-27 19:46:33 +0000260
261 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
262 unsigned getDFSOut() const { return DFSOut; }
263 void setDFSOut(unsigned O) { DFSOut = O; }
264 unsigned getDFSIn() const { return DFSIn; }
265 void setDFSIn(unsigned I) { DFSIn = I; }
266 bool dominates(const DbgScope *S) {
267 if (S == this)
268 return true;
269 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
270 return true;
271 return false;
272 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000273
Devang Patel930143b2009-11-21 02:48:08 +0000274 /// addScope - Add a scope to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000275 ///
Devang Patel930143b2009-11-21 02:48:08 +0000276 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000277
Devang Patel930143b2009-11-21 02:48:08 +0000278 /// addVariable - Add a variable to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000279 ///
Devang Patel930143b2009-11-21 02:48:08 +0000280 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000281
Bill Wendling2f921f82009-05-15 09:23:25 +0000282#ifndef NDEBUG
283 void dump() const;
284#endif
285};
Devang Patel6c74a872010-04-27 19:46:33 +0000286
Chris Lattnerf5d06362010-04-05 04:09:20 +0000287} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000288
289#ifndef NDEBUG
290void DbgScope::dump() const {
David Greenec230cb92009-12-24 00:31:35 +0000291 raw_ostream &err = dbgs();
Chris Lattner81e8e022009-08-23 00:51:00 +0000292 err.indent(IndentLevel);
Devang Patel32cc43c2010-05-07 20:54:48 +0000293 const MDNode *N = Desc;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000294 N->dump();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000295 if (AbstractScope)
296 err << "Abstract Scope\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000297
298 IndentLevel += 2;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000299 if (!Scopes.empty())
300 err << "Children ...\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000301 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
302 if (Scopes[i] != this)
303 Scopes[i]->dump();
304
305 IndentLevel -= 2;
306}
307#endif
308
Bill Wendling2f921f82009-05-15 09:23:25 +0000309DbgScope::~DbgScope() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000310 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
311 delete Variables[j];
Bill Wendling2f921f82009-05-15 09:23:25 +0000312}
313
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000314DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000315 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Chris Lattner196dbdc2010-04-05 03:52:55 +0000316 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel12563b32010-04-16 23:33:45 +0000317 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000318 NextStringPoolNumber = 0;
Chris Lattner6629ca92010-04-04 22:59:04 +0000319
Chris Lattnere58b5472010-04-04 23:10:38 +0000320 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
321 DwarfStrSectionSym = TextSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000322 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel24bc1b52010-06-29 20:17:53 +0000323 DwarfDebugLineSectionSym = CurrentLineSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000324 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman6e681a52010-06-18 15:56:31 +0000325 {
326 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
327 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000328 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000329}
330DwarfDebug::~DwarfDebug() {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000331 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
332 DIEBlocks[j]->~DIEBlock();
Bill Wendling2f921f82009-05-15 09:23:25 +0000333}
334
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000335MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
336 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
337 if (Entry.first) return Entry.first;
338
339 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000340 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000341}
342
343
Devang Patel930143b2009-11-21 02:48:08 +0000344/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000345///
Devang Patel930143b2009-11-21 02:48:08 +0000346void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000347 // Profile the node so that we can make it unique.
348 FoldingSetNodeID ID;
349 Abbrev.Profile(ID);
350
351 // Check the set for priors.
352 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
353
354 // If it's newly added.
355 if (InSet == &Abbrev) {
356 // Add to abbreviation list.
357 Abbreviations.push_back(&Abbrev);
358
359 // Assign the vector position + 1 as its number.
360 Abbrev.setNumber(Abbreviations.size());
361 } else {
362 // Assign existing abbreviation number.
363 Abbrev.setNumber(InSet->getNumber());
364 }
365}
366
Devang Patel930143b2009-11-21 02:48:08 +0000367/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendlingbcad77a2009-05-20 23:24:48 +0000368/// information entry.
Devang Patel930143b2009-11-21 02:48:08 +0000369DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000370 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000371 return Value;
372}
373
Devang Patel930143b2009-11-21 02:48:08 +0000374/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000375///
Devang Patel930143b2009-11-21 02:48:08 +0000376void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000377 unsigned Form, uint64_t Integer) {
378 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000379 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000380 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000381}
382
Devang Patel930143b2009-11-21 02:48:08 +0000383/// addSInt - Add an signed integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000384///
Devang Patel930143b2009-11-21 02:48:08 +0000385void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000386 unsigned Form, int64_t Integer) {
387 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000388 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000389 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000390}
391
Devang Patel8c339592009-12-02 15:25:16 +0000392/// addString - Add a string attribute data and value. DIEString only
393/// keeps string reference.
Devang Patel930143b2009-11-21 02:48:08 +0000394void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerc3f23b82010-01-23 03:11:46 +0000395 StringRef String) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000396 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel930143b2009-11-21 02:48:08 +0000397 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000398}
399
Devang Patel930143b2009-11-21 02:48:08 +0000400/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000401///
Devang Patel930143b2009-11-21 02:48:08 +0000402void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000403 const MCSymbol *Label) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000404 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel930143b2009-11-21 02:48:08 +0000405 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000406}
407
Devang Patel930143b2009-11-21 02:48:08 +0000408/// addDelta - Add a label delta attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000409///
Devang Patel930143b2009-11-21 02:48:08 +0000410void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000411 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000412 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel930143b2009-11-21 02:48:08 +0000413 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000414}
415
Chris Lattner3f3fb972010-04-05 05:24:55 +0000416/// addDIEEntry - Add a DIE attribute data and value.
417///
418void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
419 DIE *Entry) {
420 Die->addValue(Attribute, Form, createDIEEntry(Entry));
421}
422
423
Devang Patel930143b2009-11-21 02:48:08 +0000424/// addBlock - Add block data.
Bill Wendling2f921f82009-05-15 09:23:25 +0000425///
Devang Patel930143b2009-11-21 02:48:08 +0000426void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000427 DIEBlock *Block) {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000428 Block->ComputeSize(Asm);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000429 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel930143b2009-11-21 02:48:08 +0000430 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000431}
432
Devang Patel930143b2009-11-21 02:48:08 +0000433/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000434/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000435void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel0625af22010-05-07 23:33:41 +0000436 // Verify variable.
437 if (!V->Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000438 return;
439
440 unsigned Line = V->getLineNumber();
Devang Patel8119fe872010-03-08 22:02:50 +0000441 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
442 V->getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000443 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000444 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
445 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000446}
447
Devang Patel930143b2009-11-21 02:48:08 +0000448/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000449/// entry.
Devang Patelcbe7a852010-05-07 23:19:07 +0000450void DwarfDebug::addSourceLine(DIE *Die, const DIGlobalVariable *G) {
Devang Patel0625af22010-05-07 23:33:41 +0000451 // Verify global variable.
452 if (!G->Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000453 return;
454
455 unsigned Line = G->getLineNumber();
Devang Patel8119fe872010-03-08 22:02:50 +0000456 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
457 G->getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000458 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000459 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
460 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000461}
Devang Patelb2de5fa2009-08-31 22:47:13 +0000462
Devang Patel930143b2009-11-21 02:48:08 +0000463/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000464/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000465void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel0625af22010-05-07 23:33:41 +0000466 // Verify subprogram.
467 if (!SP->Verify())
Devang Patelb2de5fa2009-08-31 22:47:13 +0000468 return;
Caroline Tice183a5192009-09-11 18:25:54 +0000469 // If the line number is 0, don't add it.
470 if (SP->getLineNumber() == 0)
471 return;
472
Devang Patelb2de5fa2009-08-31 22:47:13 +0000473 unsigned Line = SP->getLineNumber();
Devang Patel8119fe872010-03-08 22:02:50 +0000474 if (!SP->getContext().Verify())
475 return;
Devang Patel834392f2010-03-24 21:30:35 +0000476 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
477 SP->getFilename());
Devang Patelb2de5fa2009-08-31 22:47:13 +0000478 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000479 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
480 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patelb2de5fa2009-08-31 22:47:13 +0000481}
482
Devang Patel930143b2009-11-21 02:48:08 +0000483/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000484/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000485void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel0625af22010-05-07 23:33:41 +0000486 // Verify type.
487 if (!Ty->Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000488 return;
489
490 unsigned Line = Ty->getLineNumber();
Devang Patel8119fe872010-03-08 22:02:50 +0000491 if (!Ty->getContext().Verify())
492 return;
493 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
494 Ty->getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000495 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000496 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
497 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000498}
499
Devang Patel1f4690c2009-12-15 19:16:48 +0000500/// addSourceLine - Add location information to specified debug information
501/// entry.
502void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
Devang Patel0625af22010-05-07 23:33:41 +0000503 // Verify namespace.
504 if (!NS->Verify())
Devang Patel1f4690c2009-12-15 19:16:48 +0000505 return;
506
507 unsigned Line = NS->getLineNumber();
508 StringRef FN = NS->getFilename();
509 StringRef Dir = NS->getDirectory();
510
511 unsigned FileID = GetOrCreateSourceID(Dir, FN);
512 assert(FileID && "Invalid file id");
513 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
514 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
515}
516
Caroline Ticec87c1e22009-08-31 21:19:37 +0000517/* Byref variables, in Blocks, are declared by the programmer as
518 "SomeType VarName;", but the compiler creates a
519 __Block_byref_x_VarName struct, and gives the variable VarName
520 either the struct, or a pointer to the struct, as its type. This
521 is necessary for various behind-the-scenes things the compiler
522 needs to do with by-reference variables in blocks.
523
524 However, as far as the original *programmer* is concerned, the
525 variable should still have type 'SomeType', as originally declared.
526
527 The following function dives into the __Block_byref_x_VarName
528 struct to find the original type of the variable. This will be
529 passed back to the code generating the type for the Debug
530 Information Entry for the variable 'VarName'. 'VarName' will then
531 have the original type 'SomeType' in its debug information.
532
533 The original type 'SomeType' will be the type of the field named
534 'VarName' inside the __Block_byref_x_VarName struct.
535
536 NOTE: In order for this to not completely fail on the debugger
537 side, the Debug Information Entry for the variable VarName needs to
538 have a DW_AT_location that tells the debugger how to unwind through
539 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel930143b2009-11-21 02:48:08 +0000540 value of the variable. The function addBlockByrefType does this. */
Caroline Ticec87c1e22009-08-31 21:19:37 +0000541
542/// Find the type the programmer originally declared the variable to be
543/// and return that type.
544///
Devang Patel930143b2009-11-21 02:48:08 +0000545DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticec87c1e22009-08-31 21:19:37 +0000546
547 DIType subType = Ty;
548 unsigned tag = Ty.getTag();
549
550 if (tag == dwarf::DW_TAG_pointer_type) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000551 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000552 subType = DTy.getTypeDerivedFrom();
553 }
554
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000555 DICompositeType blockStruct = DICompositeType(subType);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000556 DIArray Elements = blockStruct.getTypeArray();
557
Caroline Ticec87c1e22009-08-31 21:19:37 +0000558 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
559 DIDescriptor Element = Elements.getElement(i);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000560 DIDerivedType DT = DIDerivedType(Element);
Devang Patel2d9caf92009-11-25 17:36:49 +0000561 if (Name == DT.getName())
Caroline Ticec87c1e22009-08-31 21:19:37 +0000562 return (DT.getTypeDerivedFrom());
563 }
564
565 return Ty;
566}
567
Devang Patel930143b2009-11-21 02:48:08 +0000568/// addComplexAddress - Start with the address based on the location provided,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000569/// and generate the DWARF information necessary to find the actual variable
570/// given the extra address information encoded in the DIVariable, starting from
571/// the starting location. Add the DWARF information to the die.
572///
Devang Patel930143b2009-11-21 02:48:08 +0000573void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000574 unsigned Attribute,
575 const MachineLocation &Location) {
576 const DIVariable &VD = DV->getVariable();
577 DIType Ty = VD.getType();
578
579 // Decode the original location, and use that as the start of the byref
580 // variable's location.
Chris Lattner3a383cb2010-04-05 00:13:49 +0000581 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000582 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000583 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000584
585 if (Location.isReg()) {
586 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000587 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000588 } else {
589 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-11-21 02:48:08 +0000590 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
591 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000592 }
593 } else {
594 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000595 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000596 else {
Devang Patel930143b2009-11-21 02:48:08 +0000597 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
598 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000599 }
600
Devang Patel930143b2009-11-21 02:48:08 +0000601 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump14cf8ec2009-09-30 00:08:22 +0000602 }
603
604 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
605 uint64_t Element = VD.getAddrElement(i);
606
607 if (Element == DIFactory::OpPlus) {
Devang Patel930143b2009-11-21 02:48:08 +0000608 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
609 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump14cf8ec2009-09-30 00:08:22 +0000610 } else if (Element == DIFactory::OpDeref) {
Devang Patel930143b2009-11-21 02:48:08 +0000611 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000612 } else llvm_unreachable("unknown DIFactory Opcode");
613 }
614
615 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000616 addBlock(Die, Attribute, 0, Block);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000617}
618
Caroline Ticec87c1e22009-08-31 21:19:37 +0000619/* Byref variables, in Blocks, are declared by the programmer as "SomeType
620 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
621 gives the variable VarName either the struct, or a pointer to the struct, as
622 its type. This is necessary for various behind-the-scenes things the
623 compiler needs to do with by-reference variables in Blocks.
624
625 However, as far as the original *programmer* is concerned, the variable
626 should still have type 'SomeType', as originally declared.
627
Devang Patel930143b2009-11-21 02:48:08 +0000628 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000629 struct to find the original type of the variable, which is then assigned to
630 the variable's Debug Information Entry as its real type. So far, so good.
631 However now the debugger will expect the variable VarName to have the type
632 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000633 expression that explains to the debugger how to navigate through the
Caroline Ticec87c1e22009-08-31 21:19:37 +0000634 pointers and struct to find the actual variable of type SomeType.
635
636 The following function does just that. We start by getting
637 the "normal" location for the variable. This will be the location
638 of either the struct __Block_byref_x_VarName or the pointer to the
639 struct __Block_byref_x_VarName.
640
641 The struct will look something like:
642
643 struct __Block_byref_x_VarName {
644 ... <various fields>
645 struct __Block_byref_x_VarName *forwarding;
646 ... <various other fields>
647 SomeType VarName;
648 ... <maybe more fields>
649 };
650
651 If we are given the struct directly (as our starting point) we
652 need to tell the debugger to:
653
654 1). Add the offset of the forwarding field.
655
Dan Gohman4a618822010-02-10 16:03:48 +0000656 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000657 struct to use (the real one may have been copied onto the heap).
658
659 3). Add the offset for the field VarName, to find the actual variable.
660
661 If we started with a pointer to the struct, then we need to
662 dereference that pointer first, before the other steps.
663 Translating this into DWARF ops, we will need to append the following
664 to the current location description for the variable:
665
666 DW_OP_deref -- optional, if we start with a pointer
667 DW_OP_plus_uconst <forward_fld_offset>
668 DW_OP_deref
669 DW_OP_plus_uconst <varName_fld_offset>
670
671 That is what this function does. */
672
Devang Patel930143b2009-11-21 02:48:08 +0000673/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticec87c1e22009-08-31 21:19:37 +0000674/// provided, and generate the DWARF information necessary to find the
675/// actual Block variable (navigating the Block struct) based on the
676/// starting location. Add the DWARF information to the die. For
677/// more information, read large comment just above here.
678///
Devang Patel930143b2009-11-21 02:48:08 +0000679void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000680 unsigned Attribute,
681 const MachineLocation &Location) {
Caroline Ticec87c1e22009-08-31 21:19:37 +0000682 const DIVariable &VD = DV->getVariable();
683 DIType Ty = VD.getType();
684 DIType TmpTy = Ty;
685 unsigned Tag = Ty.getTag();
686 bool isPointer = false;
687
Devang Patel2d9caf92009-11-25 17:36:49 +0000688 StringRef varName = VD.getName();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000689
690 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000691 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000692 TmpTy = DTy.getTypeDerivedFrom();
693 isPointer = true;
694 }
695
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000696 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000697
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000698 // Find the __forwarding field and the variable field in the __Block_byref
699 // struct.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000700 DIArray Fields = blockStruct.getTypeArray();
701 DIDescriptor varField = DIDescriptor();
702 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000703
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000704 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
705 DIDescriptor Element = Fields.getElement(i);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000706 DIDerivedType DT = DIDerivedType(Element);
Devang Patel2d9caf92009-11-25 17:36:49 +0000707 StringRef fieldName = DT.getName();
708 if (fieldName == "__forwarding")
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000709 forwardingField = Element;
Devang Patel2d9caf92009-11-25 17:36:49 +0000710 else if (fieldName == varName)
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000711 varField = Element;
712 }
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000713
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000714 // Get the offsets for the forwarding field and the variable field.
Chris Lattner71696ef2010-03-31 06:06:37 +0000715 unsigned forwardingFieldOffset =
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000716 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner71696ef2010-03-31 06:06:37 +0000717 unsigned varFieldOffset =
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000718 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticec87c1e22009-08-31 21:19:37 +0000719
Mike Stump944fa252009-09-24 23:21:26 +0000720 // Decode the original location, and use that as the start of the byref
721 // variable's location.
Chris Lattner3a383cb2010-04-05 00:13:49 +0000722 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000723 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000724 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000725
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000726 if (Location.isReg()) {
727 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000728 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000729 else {
730 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-11-21 02:48:08 +0000731 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
732 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000733 }
734 } else {
735 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000736 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000737 else {
Devang Patel930143b2009-11-21 02:48:08 +0000738 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
739 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000740 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000741
Devang Patel930143b2009-11-21 02:48:08 +0000742 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000743 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000744
Mike Stump944fa252009-09-24 23:21:26 +0000745 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000746 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000747 if (isPointer)
Devang Patel930143b2009-11-21 02:48:08 +0000748 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000749
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000750 // Next add the offset for the '__forwarding' field:
751 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
752 // adding the offset if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000753 if (forwardingFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000754 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
755 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000756 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000757
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000758 // Now dereference the __forwarding field to get to the real __Block_byref
759 // struct: DW_OP_deref.
Devang Patel930143b2009-11-21 02:48:08 +0000760 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000761
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000762 // Now that we've got the real __Block_byref... struct, add the offset
763 // for the variable's field to get to the location of the actual variable:
764 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000765 if (varFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000766 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
767 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000768 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000769
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000770 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000771 addBlock(Die, Attribute, 0, Block);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000772}
773
Devang Patel930143b2009-11-21 02:48:08 +0000774/// addAddress - Add an address attribute to a die based on the location
Bill Wendling2f921f82009-05-15 09:23:25 +0000775/// provided.
Devang Patel930143b2009-11-21 02:48:08 +0000776void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000777 const MachineLocation &Location) {
Chris Lattner3a383cb2010-04-05 00:13:49 +0000778 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling2f921f82009-05-15 09:23:25 +0000779 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000780 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendling2f921f82009-05-15 09:23:25 +0000781
782 if (Location.isReg()) {
783 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000784 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000785 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000786 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
787 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000788 }
789 } else {
790 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000791 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000792 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000793 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
794 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000795 }
796
Devang Patel930143b2009-11-21 02:48:08 +0000797 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling2f921f82009-05-15 09:23:25 +0000798 }
799
Devang Patel930143b2009-11-21 02:48:08 +0000800 addBlock(Die, Attribute, 0, Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000801}
802
Devang Patel173b2b92010-04-28 01:03:09 +0000803/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patele1c53f22010-05-20 16:36:41 +0000804bool DwarfDebug::addRegisterAddress(DIE *Die, const MCSymbol *VS,
Devang Patel173b2b92010-04-28 01:03:09 +0000805 const MachineOperand &MO) {
806 assert (MO.isReg() && "Invalid machine operand!");
807 if (!MO.getReg())
808 return false;
809 MachineLocation Location;
810 Location.set(MO.getReg());
811 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patele1c53f22010-05-20 16:36:41 +0000812 if (VS)
Devang Patel173b2b92010-04-28 01:03:09 +0000813 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
814 return true;
815}
816
817/// addConstantValue - Add constant value entry in variable DIE.
Devang Patele1c53f22010-05-20 16:36:41 +0000818bool DwarfDebug::addConstantValue(DIE *Die, const MCSymbol *VS,
Devang Patel173b2b92010-04-28 01:03:09 +0000819 const MachineOperand &MO) {
820 assert (MO.isImm() && "Invalid machine operand!");
821 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
822 unsigned Imm = MO.getImm();
823 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
824 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patele1c53f22010-05-20 16:36:41 +0000825 if (VS)
Devang Patel173b2b92010-04-28 01:03:09 +0000826 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
827 return true;
828}
829
830/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patele1c53f22010-05-20 16:36:41 +0000831bool DwarfDebug::addConstantFPValue(DIE *Die, const MCSymbol *VS,
Devang Patel173b2b92010-04-28 01:03:09 +0000832 const MachineOperand &MO) {
833 assert (MO.isFPImm() && "Invalid machine operand!");
834 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
835 APFloat FPImm = MO.getFPImm()->getValueAPF();
836
837 // Get the raw data form of the floating point.
838 const APInt FltVal = FPImm.bitcastToAPInt();
839 const char *FltPtr = (const char*)FltVal.getRawData();
840
841 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
842 bool LittleEndian = Asm->getTargetData().isLittleEndian();
843 int Incr = (LittleEndian ? 1 : -1);
844 int Start = (LittleEndian ? 0 : NumBytes - 1);
845 int Stop = (LittleEndian ? NumBytes : -1);
846
847 // Output the constant to DWARF one byte at a time.
848 for (; Start != Stop; Start += Incr)
849 addUInt(Block, 0, dwarf::DW_FORM_data1,
850 (unsigned char)0xFF & FltPtr[Start]);
851
852 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patele1c53f22010-05-20 16:36:41 +0000853 if (VS)
854 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
Devang Patel173b2b92010-04-28 01:03:09 +0000855 return true;
856}
857
858
Devang Patel2b75ed22009-12-10 19:14:49 +0000859/// addToContextOwner - Add Die into the list of its context owner's children.
860void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000861 if (Context.isType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000862 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patel2b75ed22009-12-10 19:14:49 +0000863 ContextDIE->addChild(Die);
Devang Patel1f4690c2009-12-15 19:16:48 +0000864 } else if (Context.isNameSpace()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000865 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel1f4690c2009-12-15 19:16:48 +0000866 ContextDIE->addChild(Die);
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000867 } else if (Context.isSubprogram()) {
868 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context),
869 /*MakeDecl=*/false);
870 ContextDIE->addChild(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000871 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patel2b75ed22009-12-10 19:14:49 +0000872 ContextDIE->addChild(Die);
873 else
Devang Patel1a0df9a2010-05-10 22:49:55 +0000874 getCompileUnit(Context)->addDie(Die);
Devang Patel2b75ed22009-12-10 19:14:49 +0000875}
876
Devang Patelb5b60ea2009-12-10 18:05:33 +0000877/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
878/// given DIType.
879DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000880 CompileUnit *TypeCU = getCompileUnit(Ty);
881 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patelb5b60ea2009-12-10 18:05:33 +0000882 if (TyDIE)
883 return TyDIE;
884
885 // Create new type.
886 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000887 TypeCU->insertDIE(Ty, TyDIE);
Devang Patelb5b60ea2009-12-10 18:05:33 +0000888 if (Ty.isBasicType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000889 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000890 else if (Ty.isCompositeType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000891 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000892 else {
893 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000894 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000895 }
896
Devang Patel2b75ed22009-12-10 19:14:49 +0000897 addToContextOwner(TyDIE, Ty.getContext());
Devang Patelb5b60ea2009-12-10 18:05:33 +0000898 return TyDIE;
899}
900
Devang Patel930143b2009-11-21 02:48:08 +0000901/// addType - Add a new type attribute to the specified entity.
Devang Patel9ccfb642009-12-09 18:24:21 +0000902void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel8d6a2b72010-05-07 21:45:47 +0000903 if (!Ty.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000904 return;
905
906 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000907 CompileUnit *TypeCU = getCompileUnit(Ty);
908 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling2f921f82009-05-15 09:23:25 +0000909 // If it exists then use the existing value.
Devang Patel930143b2009-11-21 02:48:08 +0000910 if (Entry) {
911 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000912 return;
913 }
914
Bill Wendling2f921f82009-05-15 09:23:25 +0000915 // Construct type.
Devang Patelb5b60ea2009-12-10 18:05:33 +0000916 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling2f921f82009-05-15 09:23:25 +0000917
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000918 // Set up proxy.
919 Entry = createDIEEntry(Buffer);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000920 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000921
Devang Patel930143b2009-11-21 02:48:08 +0000922 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000923}
924
Devang Patel930143b2009-11-21 02:48:08 +0000925/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000926void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000927 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000928 StringRef Name = BTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000929 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +0000930 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling2f921f82009-05-15 09:23:25 +0000931 BTy.getEncoding());
932
933 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +0000934 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000935 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000936 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel930143b2009-11-21 02:48:08 +0000937 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000938}
939
Devang Patel930143b2009-11-21 02:48:08 +0000940/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000941void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000942 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000943 StringRef Name = DTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000944 uint64_t Size = DTy.getSizeInBits() >> 3;
945 unsigned Tag = DTy.getTag();
946
947 // FIXME - Workaround for templates.
948 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
949
950 Buffer.setTag(Tag);
951
952 // Map to main type, void will not have a type.
953 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel9ccfb642009-12-09 18:24:21 +0000954 addType(&Buffer, FromTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000955
956 // Add name if not anonymous or intermediate type.
Devang Patelae466ef2009-11-30 23:56:56 +0000957 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000958 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000959
960 // Add size if non-zero (derived types might be zero-sized.)
961 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +0000962 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000963
964 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb5b51592009-11-23 18:43:37 +0000965 if (!DTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +0000966 addSourceLine(&Buffer, &DTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000967}
968
Devang Patel930143b2009-11-21 02:48:08 +0000969/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000970void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000971 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000972 StringRef Name = CTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000973
974 uint64_t Size = CTy.getSizeInBits() >> 3;
975 unsigned Tag = CTy.getTag();
976 Buffer.setTag(Tag);
977
978 switch (Tag) {
979 case dwarf::DW_TAG_vector_type:
980 case dwarf::DW_TAG_array_type:
Devang Patel9ccfb642009-12-09 18:24:21 +0000981 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000982 break;
983 case dwarf::DW_TAG_enumeration_type: {
984 DIArray Elements = CTy.getTypeArray();
985
986 // Add enumerators to enumeration type.
987 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
988 DIE *ElemDie = NULL;
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000989 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3b548aa2010-03-08 20:52:55 +0000990 if (Enum.isEnumerator()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000991 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel930143b2009-11-21 02:48:08 +0000992 Buffer.addChild(ElemDie);
Devang Patelfafa1fe2009-10-09 17:51:49 +0000993 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000994 }
995 }
996 break;
997 case dwarf::DW_TAG_subroutine_type: {
998 // Add return type.
999 DIArray Elements = CTy.getTypeArray();
1000 DIDescriptor RTy = Elements.getElement(0);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001001 addType(&Buffer, DIType(RTy));
Bill Wendling2f921f82009-05-15 09:23:25 +00001002
1003 // Add prototype flag.
Devang Patel930143b2009-11-21 02:48:08 +00001004 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001005
1006 // Add arguments.
1007 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1008 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1009 DIDescriptor Ty = Elements.getElement(i);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001010 addType(Arg, DIType(Ty));
Devang Patel930143b2009-11-21 02:48:08 +00001011 Buffer.addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +00001012 }
1013 }
1014 break;
1015 case dwarf::DW_TAG_structure_type:
1016 case dwarf::DW_TAG_union_type:
1017 case dwarf::DW_TAG_class_type: {
1018 // Add elements to structure type.
1019 DIArray Elements = CTy.getTypeArray();
1020
1021 // A forward struct declared type may not have elements available.
Devang Patel3b548aa2010-03-08 20:52:55 +00001022 unsigned N = Elements.getNumElements();
1023 if (N == 0)
Bill Wendling2f921f82009-05-15 09:23:25 +00001024 break;
1025
1026 // Add elements to structure type.
Devang Patel3b548aa2010-03-08 20:52:55 +00001027 for (unsigned i = 0; i < N; ++i) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001028 DIDescriptor Element = Elements.getElement(i);
1029 DIE *ElemDie = NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001030 if (Element.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001031 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel3b548aa2010-03-08 20:52:55 +00001032 else if (Element.isVariable()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001033 DIVariable DV(Element);
Devang Patel160c92d2010-01-30 01:08:30 +00001034 ElemDie = new DIE(dwarf::DW_TAG_variable);
1035 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1036 DV.getName());
1037 addType(ElemDie, DV.getType());
1038 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1039 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1040 addSourceLine(ElemDie, &DV);
Devang Patel3b548aa2010-03-08 20:52:55 +00001041 } else if (Element.isDerivedType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001042 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3b548aa2010-03-08 20:52:55 +00001043 else
1044 continue;
Devang Patel930143b2009-11-21 02:48:08 +00001045 Buffer.addChild(ElemDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001046 }
1047
Devang Patel3082c012009-08-27 23:51:51 +00001048 if (CTy.isAppleBlockExtension())
Devang Patel930143b2009-11-21 02:48:08 +00001049 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001050
1051 unsigned RLang = CTy.getRunTimeLang();
1052 if (RLang)
Devang Patel930143b2009-11-21 02:48:08 +00001053 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling2f921f82009-05-15 09:23:25 +00001054 dwarf::DW_FORM_data1, RLang);
Devang Patel303a1be2010-01-26 21:16:06 +00001055
1056 DICompositeType ContainingType = CTy.getContainingType();
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001057 if (DIDescriptor(ContainingType).isCompositeType())
Devang Patel303a1be2010-01-26 21:16:06 +00001058 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001059 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastingsafe54f12010-06-11 20:08:44 +00001060 else {
1061 DIDescriptor Context = CTy.getContext();
1062 addToContextOwner(&Buffer, Context);
1063 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001064 break;
1065 }
1066 default:
1067 break;
1068 }
1069
1070 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +00001071 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001072 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +00001073
Devang Patel30265c42010-07-07 20:12:52 +00001074 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
1075 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1076 {
Bill Wendling2f921f82009-05-15 09:23:25 +00001077 // Add size if non-zero (derived types might be zero-sized.)
1078 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +00001079 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +00001080 else {
1081 // Add zero size if it is not a forward declaration.
1082 if (CTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +00001083 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001084 else
Devang Patel930143b2009-11-21 02:48:08 +00001085 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling2f921f82009-05-15 09:23:25 +00001086 }
1087
1088 // Add source line info if available.
1089 if (!CTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +00001090 addSourceLine(&Buffer, &CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001091 }
1092}
1093
Devang Patel930143b2009-11-21 02:48:08 +00001094/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1095void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling2f921f82009-05-15 09:23:25 +00001096 int64_t L = SR.getLo();
1097 int64_t H = SR.getHi();
1098 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1099
Devang Patel930143b2009-11-21 02:48:08 +00001100 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patelf691df32009-08-14 20:59:16 +00001101 if (L)
Devang Patel930143b2009-11-21 02:48:08 +00001102 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patel8f046022009-12-04 23:10:24 +00001103 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling2f921f82009-05-15 09:23:25 +00001104
Devang Patel930143b2009-11-21 02:48:08 +00001105 Buffer.addChild(DW_Subrange);
Bill Wendling2f921f82009-05-15 09:23:25 +00001106}
1107
Devang Patel930143b2009-11-21 02:48:08 +00001108/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +00001109void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling2f921f82009-05-15 09:23:25 +00001110 DICompositeType *CTy) {
1111 Buffer.setTag(dwarf::DW_TAG_array_type);
1112 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel930143b2009-11-21 02:48:08 +00001113 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001114
1115 // Emit derived type.
Devang Patel9ccfb642009-12-09 18:24:21 +00001116 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001117 DIArray Elements = CTy->getTypeArray();
1118
Devang Patel92e8c652009-11-21 00:31:03 +00001119 // Get an anonymous type for index type.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001120 CompileUnit *TheCU = getCompileUnit(*CTy);
1121 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel92e8c652009-11-21 00:31:03 +00001122 if (!IdxTy) {
1123 // Construct an anonymous type for index type.
1124 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +00001125 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1126 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel92e8c652009-11-21 00:31:03 +00001127 dwarf::DW_ATE_signed);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001128 TheCU->addDie(IdxTy);
1129 TheCU->setIndexTyDie(IdxTy);
Devang Patel92e8c652009-11-21 00:31:03 +00001130 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001131
1132 // Add subranges to array type.
1133 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1134 DIDescriptor Element = Elements.getElement(i);
1135 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001136 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001137 }
1138}
1139
Devang Patel930143b2009-11-21 02:48:08 +00001140/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3b548aa2010-03-08 20:52:55 +00001141DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001142 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3b548aa2010-03-08 20:52:55 +00001143 StringRef Name = ETy.getName();
Devang Patel930143b2009-11-21 02:48:08 +00001144 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3b548aa2010-03-08 20:52:55 +00001145 int64_t Value = ETy.getEnumValue();
Devang Patel930143b2009-11-21 02:48:08 +00001146 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +00001147 return Enumerator;
1148}
1149
Devang Patel43ef34d2010-01-05 01:46:14 +00001150/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1151/// printer to not emit usual symbol prefix before the symbol name is used then
1152/// return linkage name after skipping this special LLVM prefix.
1153static StringRef getRealLinkageName(StringRef LinkageName) {
1154 char One = '\1';
1155 if (LinkageName.startswith(StringRef(&One, 1)))
1156 return LinkageName.substr(1);
1157 return LinkageName;
1158}
1159
Devang Patel930143b2009-11-21 02:48:08 +00001160/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel9ccfb642009-12-09 18:24:21 +00001161DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach00e9c612009-11-22 19:20:36 +00001162 // If the global variable was optmized out then no need to create debug info
1163 // entry.
Devang Patelcc11371b2009-11-06 17:58:12 +00001164 if (!GV.getGlobal()) return NULL;
Devang Patel2d9caf92009-11-25 17:36:49 +00001165 if (GV.getDisplayName().empty()) return NULL;
Devang Patel06ce6502009-11-06 01:30:04 +00001166
Bill Wendling2f921f82009-05-15 09:23:25 +00001167 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach042483e2009-11-21 23:12:12 +00001168 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001169 GV.getDisplayName());
1170
Devang Patel2d9caf92009-11-25 17:36:49 +00001171 StringRef LinkageName = GV.getLinkageName();
Devang Patel43ef34d2010-01-05 01:46:14 +00001172 if (!LinkageName.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001173 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel43ef34d2010-01-05 01:46:14 +00001174 getRealLinkageName(LinkageName));
1175
Devang Patel9ccfb642009-12-09 18:24:21 +00001176 addType(GVDie, GV.getType());
Bill Wendling2f921f82009-05-15 09:23:25 +00001177 if (!GV.isLocalToUnit())
Devang Patel930143b2009-11-21 02:48:08 +00001178 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1179 addSourceLine(GVDie, &GV);
Devang Patel4144a822009-10-05 23:22:08 +00001180
Bill Wendling2f921f82009-05-15 09:23:25 +00001181 return GVDie;
1182}
1183
Devang Patel930143b2009-11-21 02:48:08 +00001184/// createMemberDIE - Create new member DIE.
Devang Patel9ccfb642009-12-09 18:24:21 +00001185DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001186 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel2d9caf92009-11-25 17:36:49 +00001187 StringRef Name = DT.getName();
1188 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001189 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel2d9caf92009-11-25 17:36:49 +00001190
Devang Patel9ccfb642009-12-09 18:24:21 +00001191 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001192
Devang Patel930143b2009-11-21 02:48:08 +00001193 addSourceLine(MemberDie, &DT);
Bill Wendling2f921f82009-05-15 09:23:25 +00001194
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001195 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel930143b2009-11-21 02:48:08 +00001196 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel67f56f02009-11-04 22:06:12 +00001197
Bill Wendling2f921f82009-05-15 09:23:25 +00001198 uint64_t Size = DT.getSizeInBits();
Devang Patelf05d5722009-11-04 23:48:00 +00001199 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling2f921f82009-05-15 09:23:25 +00001200
1201 if (Size != FieldSize) {
1202 // Handle bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001203 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1204 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling2f921f82009-05-15 09:23:25 +00001205
1206 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling2f921f82009-05-15 09:23:25 +00001207 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1208 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer2b459982010-01-07 17:50:57 +00001209 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling2f921f82009-05-15 09:23:25 +00001210 Offset -= FieldOffset;
1211
1212 // Maybe we need to work from the other end.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001213 if (Asm->getTargetData().isLittleEndian())
1214 Offset = FieldSize - (Offset + Size);
Devang Patel930143b2009-11-21 02:48:08 +00001215 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling2f921f82009-05-15 09:23:25 +00001216
Devang Patel67f56f02009-11-04 22:06:12 +00001217 // Here WD_AT_data_member_location points to the anonymous
1218 // field that includes this bit field.
Devang Patel930143b2009-11-21 02:48:08 +00001219 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001220
1221 } else
1222 // This is not a bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001223 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001224
Devang Pateld2316892010-02-03 20:08:48 +00001225 if (DT.getTag() == dwarf::DW_TAG_inheritance
1226 && DT.isVirtual()) {
1227
1228 // For C++, virtual base classes are not at fixed offset. Use following
1229 // expression to extract appropriate offset from vtable.
1230 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1231
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001232 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Pateld2316892010-02-03 20:08:48 +00001233 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1234 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1235 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1236 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1237 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1238 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1239 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1240
1241 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1242 VBaseLocationDie);
1243 } else
1244 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001245
1246 if (DT.isProtected())
Devang Pateleb57c592009-12-03 19:11:07 +00001247 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001248 dwarf::DW_ACCESS_protected);
1249 else if (DT.isPrivate())
Devang Pateleb57c592009-12-03 19:11:07 +00001250 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001251 dwarf::DW_ACCESS_private);
Devang Pateleb57c592009-12-03 19:11:07 +00001252 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1253 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1254 dwarf::DW_ACCESS_public);
1255 if (DT.isVirtual())
1256 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1257 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling2f921f82009-05-15 09:23:25 +00001258 return MemberDie;
1259}
1260
Devang Patel525dda02009-12-14 16:18:45 +00001261/// createSubprogramDIE - Create new DIE using SP.
1262DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001263 CompileUnit *SPCU = getCompileUnit(SP);
1264 DIE *SPDie = SPCU->getDIE(SP);
Devang Patel525dda02009-12-14 16:18:45 +00001265 if (SPDie)
1266 return SPDie;
1267
1268 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf200b392010-03-02 17:58:15 +00001269 // Constructors and operators for anonymous aggregates do not have names.
Devang Pateld0fa3042010-03-02 01:26:20 +00001270 if (!SP.getName().empty())
1271 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling2f921f82009-05-15 09:23:25 +00001272
Devang Patel2d9caf92009-11-25 17:36:49 +00001273 StringRef LinkageName = SP.getLinkageName();
Devang Patel43ef34d2010-01-05 01:46:14 +00001274 if (!LinkageName.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001275 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel43ef34d2010-01-05 01:46:14 +00001276 getRealLinkageName(LinkageName));
1277
Devang Patel930143b2009-11-21 02:48:08 +00001278 addSourceLine(SPDie, &SP);
Bill Wendling2f921f82009-05-15 09:23:25 +00001279
Bill Wendling2f921f82009-05-15 09:23:25 +00001280 // Add prototyped tag, if C or ObjC.
1281 unsigned Lang = SP.getCompileUnit().getLanguage();
1282 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1283 Lang == dwarf::DW_LANG_ObjC)
Devang Patel930143b2009-11-21 02:48:08 +00001284 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001285
1286 // Add Return Type.
Devang Patel236526d2009-12-03 01:25:38 +00001287 DICompositeType SPTy = SP.getType();
1288 DIArray Args = SPTy.getTypeArray();
Bill Wendling2f921f82009-05-15 09:23:25 +00001289 unsigned SPTag = SPTy.getTag();
Devang Pateleb57c592009-12-03 19:11:07 +00001290
Devang Patel3b548aa2010-03-08 20:52:55 +00001291 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel9ccfb642009-12-09 18:24:21 +00001292 addType(SPDie, SPTy);
Devang Patel236526d2009-12-03 01:25:38 +00001293 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001294 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel236526d2009-12-03 01:25:38 +00001295
Devang Pateleb57c592009-12-03 19:11:07 +00001296 unsigned VK = SP.getVirtuality();
1297 if (VK) {
1298 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001299 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Pateleb57c592009-12-03 19:11:07 +00001300 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1301 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1302 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel018b29b2010-01-19 06:19:05 +00001303 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001304 SP.getContainingType()));
Devang Pateleb57c592009-12-03 19:11:07 +00001305 }
1306
Devang Patel525dda02009-12-14 16:18:45 +00001307 if (MakeDecl || !SP.isDefinition()) {
Devang Patel930143b2009-11-21 02:48:08 +00001308 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001309
1310 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel236526d2009-12-03 01:25:38 +00001311 // be handled while processing variables.
1312 DICompositeType SPTy = SP.getType();
1313 DIArray Args = SPTy.getTypeArray();
1314 unsigned SPTag = SPTy.getTag();
1315
Bill Wendling2f921f82009-05-15 09:23:25 +00001316 if (SPTag == dwarf::DW_TAG_subroutine_type)
1317 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1318 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001319 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patel6efc8e52010-02-06 01:02:37 +00001320 addType(Arg, ATy);
1321 if (ATy.isArtificial())
1322 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel930143b2009-11-21 02:48:08 +00001323 SPDie->addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +00001324 }
1325 }
1326
Devang Patel999b4992010-02-03 19:57:19 +00001327 if (SP.isArtificial())
1328 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1329
Devang Patelb4e3b902010-04-30 19:38:23 +00001330 if (!SP.isLocalToUnit())
1331 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel61880932010-04-19 19:14:02 +00001332
Devang Patelb4e3b902010-04-30 19:38:23 +00001333 if (SP.isOptimized())
1334 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1335
Devang Patelb4e3b902010-04-30 19:38:23 +00001336 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001337 SPCU->insertDIE(SP, SPDie);
Devang Patelb4e3b902010-04-30 19:38:23 +00001338
Stuart Hastingsafe54f12010-06-11 20:08:44 +00001339 // Add to context owner.
1340 addToContextOwner(SPDie, SP.getContext());
1341
Bill Wendling2f921f82009-05-15 09:23:25 +00001342 return SPDie;
1343}
1344
Devang Patel32cc43c2010-05-07 20:54:48 +00001345DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattner8d2fe282010-03-31 05:36:29 +00001346 assert(N && "Invalid Scope encoding!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001347
1348 DbgScope *AScope = AbstractScopes.lookup(N);
1349 if (AScope)
1350 return AScope;
Jim Grosbach042483e2009-11-21 23:12:12 +00001351
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001352 DbgScope *Parent = NULL;
1353
1354 DIDescriptor Scope(N);
1355 if (Scope.isLexicalBlock()) {
1356 DILexicalBlock DB(N);
1357 DIDescriptor ParentDesc = DB.getContext();
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001358 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001359 }
1360
1361 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1362
1363 if (Parent)
Devang Patel930143b2009-11-21 02:48:08 +00001364 Parent->addScope(AScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001365 AScope->setAbstractScope();
1366 AbstractScopes[N] = AScope;
1367 if (DIDescriptor(N).isSubprogram())
1368 AbstractScopesList.push_back(AScope);
1369 return AScope;
1370}
Devang Patel75cc16c2009-10-01 20:31:14 +00001371
Devang Patel019922d2010-04-06 23:53:48 +00001372/// isSubprogramContext - Return true if Context is either a subprogram
1373/// or another context nested inside a subprogram.
Devang Patel32cc43c2010-05-07 20:54:48 +00001374static bool isSubprogramContext(const MDNode *Context) {
Devang Patel019922d2010-04-06 23:53:48 +00001375 if (!Context)
1376 return false;
1377 DIDescriptor D(Context);
1378 if (D.isSubprogram())
1379 return true;
1380 if (D.isType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001381 return isSubprogramContext(DIType(Context).getContext());
Devang Patel019922d2010-04-06 23:53:48 +00001382 return false;
1383}
1384
Jim Grosbach042483e2009-11-21 23:12:12 +00001385/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +00001386/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1387/// If there are global variables in this scope then create and insert
1388/// DIEs for these variables.
Devang Patel32cc43c2010-05-07 20:54:48 +00001389DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001390 CompileUnit *SPCU = getCompileUnit(SPNode);
1391 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +00001392
Chris Lattner3a383cb2010-04-05 00:13:49 +00001393 assert(SPDie && "Unable to find subprogram DIE!");
1394 DISubprogram SP(SPNode);
Chris Lattner2c3f4782010-03-13 07:26:18 +00001395
Chris Lattner3a383cb2010-04-05 00:13:49 +00001396 // There is not any need to generate specification DIE for a function
1397 // defined at compile unit level. If a function is defined inside another
1398 // function then gdb prefers the definition at top level and but does not
1399 // expect specification DIE in parent function. So avoid creating
1400 // specification DIE for a function defined inside a function.
1401 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel019922d2010-04-06 23:53:48 +00001402 !SP.getContext().isFile() &&
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001403 !isSubprogramContext(SP.getContext())) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00001404 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1405
1406 // Add arguments.
1407 DICompositeType SPTy = SP.getType();
1408 DIArray Args = SPTy.getTypeArray();
1409 unsigned SPTag = SPTy.getTag();
1410 if (SPTag == dwarf::DW_TAG_subroutine_type)
1411 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1412 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001413 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattner3a383cb2010-04-05 00:13:49 +00001414 addType(Arg, ATy);
1415 if (ATy.isArtificial())
1416 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1417 SPDie->addChild(Arg);
1418 }
1419 DIE *SPDeclDie = SPDie;
1420 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1421 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1422 SPDeclDie);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001423 SPCU->addDie(SPDie);
Chris Lattner3a383cb2010-04-05 00:13:49 +00001424 }
1425
Devang Patela37a95e2010-07-07 22:20:57 +00001426 // Pick up abstract subprogram DIE.
1427 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1428 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1429 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1430 dwarf::DW_FORM_ref4, AbsSPDIE);
1431 SPCU->addDie(SPDie);
1432 }
1433
Chris Lattner3a383cb2010-04-05 00:13:49 +00001434 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1435 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1436 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1437 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1438 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1439 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1440 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +00001441
Chris Lattner3a383cb2010-04-05 00:13:49 +00001442 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001443}
1444
Jim Grosbach042483e2009-11-21 23:12:12 +00001445/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +00001446/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1447DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001448
1449 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1450 if (Scope->isAbstractScope())
1451 return ScopeDIE;
1452
1453 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1454 if (Ranges.empty())
1455 return 0;
1456
1457 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1458 if (Ranges.size() > 1) {
1459 // .debug_range section has not been laid out yet. Emit offset in
1460 // .debug_range as a uint, size 4, for now. emitDIE will handle
1461 // DW_AT_ranges appropriately.
1462 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1463 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1464 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1465 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +00001466 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1467 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +00001468 }
1469 DebugRangeSymbols.push_back(NULL);
1470 DebugRangeSymbols.push_back(NULL);
1471 return ScopeDIE;
1472 }
1473
Devang Patel9fc11702010-05-25 23:40:22 +00001474 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1475 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001476
Devang Patel9fc11702010-05-25 23:40:22 +00001477 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001478
Chris Lattnere13c3722010-03-09 01:58:53 +00001479 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1480 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001481
Devang Patel6c74a872010-04-27 19:46:33 +00001482 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1483 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001484
1485 return ScopeDIE;
1486}
1487
Devang Patel930143b2009-11-21 02:48:08 +00001488/// constructInlinedScopeDIE - This scope represents inlined body of
1489/// a function. Construct DIE to represent this concrete inlined copy
1490/// of the function.
1491DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001492
1493 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1494 assert (Ranges.empty() == false
1495 && "DbgScope does not have instruction markers!");
1496
1497 // FIXME : .debug_inlined section specification does not clearly state how
1498 // to emit inlined scope that is split into multiple instruction ranges.
1499 // For now, use first instruction range and emit low_pc/high_pc pair and
1500 // corresponding .debug_inlined section entry for this pair.
1501 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +00001502 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1503 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001504
Devang Patel9fc11702010-05-25 23:40:22 +00001505 if (StartLabel == FunctionBeginSym || EndLabel == 0) {
Devang Patel6c74a872010-04-27 19:46:33 +00001506 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1507 return 0;
1508 }
Chris Lattnere13c3722010-03-09 01:58:53 +00001509 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001510 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +00001511 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001512 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +00001513
Devang Patel3b548aa2010-03-08 20:52:55 +00001514 if (!Scope->getScopeNode())
Devang Patelbc97f6b2010-03-08 19:20:38 +00001515 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001516 DIScope DS(Scope->getScopeNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001517 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1518
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001519 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001520 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1521 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001522 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel930143b2009-11-21 02:48:08 +00001523 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001524 dwarf::DW_FORM_ref4, OriginDIE);
1525
Chris Lattner085b6522010-03-09 00:31:02 +00001526 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnere13c3722010-03-09 01:58:53 +00001527 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001528
1529 InlinedSubprogramDIEs.insert(OriginDIE);
1530
1531 // Track the start label for this inlined function.
Devang Patel32cc43c2010-05-07 20:54:48 +00001532 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001533 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001534
1535 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001536 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +00001537 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001538 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001539 } else
Chris Lattner085b6522010-03-09 00:31:02 +00001540 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001541
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001542 DILocation DL(Scope->getInlinedAt());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001543 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel930143b2009-11-21 02:48:08 +00001544 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001545
1546 return ScopeDIE;
1547}
1548
Devang Patel930143b2009-11-21 02:48:08 +00001549
1550/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel9ccfb642009-12-09 18:24:21 +00001551DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001552 // Get the descriptor.
1553 const DIVariable &VD = DV->getVariable();
Devang Patel2d9caf92009-11-25 17:36:49 +00001554 StringRef Name = VD.getName();
1555 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +00001556 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001557
1558 // Translate tag to proper Dwarf tag. The result variable is dropped for
1559 // now.
1560 unsigned Tag;
1561 switch (VD.getTag()) {
1562 case dwarf::DW_TAG_return_variable:
1563 return NULL;
1564 case dwarf::DW_TAG_arg_variable:
1565 Tag = dwarf::DW_TAG_formal_parameter;
1566 break;
1567 case dwarf::DW_TAG_auto_variable: // fall thru
1568 default:
1569 Tag = dwarf::DW_TAG_variable;
1570 break;
1571 }
1572
1573 // Define variable debug information entry.
1574 DIE *VariableDie = new DIE(Tag);
1575
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001576 DIE *AbsDIE = NULL;
Devang Patele1c53f22010-05-20 16:36:41 +00001577 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1578 V2AVI = VarToAbstractVarMap.find(DV);
1579 if (V2AVI != VarToAbstractVarMap.end())
1580 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +00001581
Devang Patele1c53f22010-05-20 16:36:41 +00001582 if (AbsDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001583 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001584 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001585 else {
Devang Patel930143b2009-11-21 02:48:08 +00001586 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1587 addSourceLine(VariableDie, &VD);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001588
1589 // Add variable type.
Jim Grosbach042483e2009-11-21 23:12:12 +00001590 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001591 // addresses instead.
1592 if (VD.isBlockByrefVariable())
Devang Patel9ccfb642009-12-09 18:24:21 +00001593 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001594 else
Devang Patel9ccfb642009-12-09 18:24:21 +00001595 addType(VariableDie, VD.getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001596 }
1597
Devang Patel6efc8e52010-02-06 01:02:37 +00001598 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1599 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel9fc11702010-05-25 23:40:22 +00001600
1601 if (Scope->isAbstractScope()) {
1602 DV->setDIE(VariableDie);
1603 return VariableDie;
1604 }
1605
1606 // Add variable address.
1607
1608 unsigned Offset = DV->getDotDebugLocOffset();
1609 if (Offset != ~0U) {
1610 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1611 Asm->GetTempSymbol("debug_loc", Offset));
1612 DV->setDIE(VariableDie);
1613 UseDotDebugLocEntry.insert(VariableDie);
1614 return VariableDie;
1615 }
1616
1617 // Check if variable is described by a DBG_VALUE instruction.
1618 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1619 DbgVariableToDbgInstMap.find(DV);
1620 if (DVI != DbgVariableToDbgInstMap.end()) {
1621 const MachineInstr *DVInsn = DVI->second;
1622 const MCSymbol *DVLabel = findVariableLabel(DV);
1623 bool updated = false;
1624 // FIXME : Handle getNumOperands != 3
1625 if (DVInsn->getNumOperands() == 3) {
1626 if (DVInsn->getOperand(0).isReg())
Devang Patel30265c42010-07-07 20:12:52 +00001627 updated =
1628 addRegisterAddress(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patel9fc11702010-05-25 23:40:22 +00001629 else if (DVInsn->getOperand(0).isImm())
1630 updated = addConstantValue(VariableDie, DVLabel, DVInsn->getOperand(0));
1631 else if (DVInsn->getOperand(0).isFPImm())
Devang Patel30265c42010-07-07 20:12:52 +00001632 updated =
1633 addConstantFPValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patel9fc11702010-05-25 23:40:22 +00001634 } else {
1635 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1636 if (Location.getReg()) {
1637 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1638 if (DVLabel)
1639 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1640 DVLabel);
1641 updated = true;
1642 }
1643 }
1644 if (!updated) {
1645 // If variableDie is not updated then DBG_VALUE instruction does not
1646 // have valid variable info.
1647 delete VariableDie;
1648 return NULL;
1649 }
1650 DV->setDIE(VariableDie);
1651 return VariableDie;
1652 }
1653
1654 // .. else use frame index, if available.
1655 MachineLocation Location;
1656 unsigned FrameReg;
1657 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1658 int FI = 0;
1659 if (findVariableFrameIndex(DV, &FI)) {
1660 int Offset = RI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1661 Location.set(FrameReg, Offset);
1662
1663 if (VD.hasComplexAddress())
1664 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1665 else if (VD.isBlockByrefVariable())
1666 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1667 else
1668 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1669 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001670 DV->setDIE(VariableDie);
1671 return VariableDie;
1672
1673}
Devang Patel930143b2009-11-21 02:48:08 +00001674
Devang Patel04d2f2d2009-11-24 01:14:22 +00001675void DwarfDebug::addPubTypes(DISubprogram SP) {
1676 DICompositeType SPTy = SP.getType();
1677 unsigned SPTag = SPTy.getTag();
1678 if (SPTag != dwarf::DW_TAG_subroutine_type)
1679 return;
1680
1681 DIArray Args = SPTy.getTypeArray();
Devang Patel04d2f2d2009-11-24 01:14:22 +00001682 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001683 DIType ATy(Args.getElement(i));
Devang Patel8d6a2b72010-05-07 21:45:47 +00001684 if (!ATy.Verify())
Devang Patel04d2f2d2009-11-24 01:14:22 +00001685 continue;
1686 DICompositeType CATy = getDICompositeType(ATy);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001687 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel12d150e2010-04-13 20:35:04 +00001688 && !CATy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001689 CompileUnit *TheCU = getCompileUnit(CATy);
1690 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1691 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001692 }
1693 }
1694}
1695
Devang Patel930143b2009-11-21 02:48:08 +00001696/// constructScopeDIE - Construct a DIE for this scope.
1697DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +00001698 if (!Scope || !Scope->getScopeNode())
1699 return NULL;
1700
1701 DIScope DS(Scope->getScopeNode());
1702 DIE *ScopeDIE = NULL;
1703 if (Scope->getInlinedAt())
1704 ScopeDIE = constructInlinedScopeDIE(Scope);
1705 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +00001706 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +00001707 if (Scope->isAbstractScope()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001708 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +00001709 // Note down abstract DIE.
1710 if (ScopeDIE)
1711 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1712 }
Devang Patel3b548aa2010-03-08 20:52:55 +00001713 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001714 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +00001715 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001716 else
Devang Patel3b548aa2010-03-08 20:52:55 +00001717 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel23b2ae62010-03-29 22:59:58 +00001718 if (!ScopeDIE) return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001719
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001720 // Add variables to scope.
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001721 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001722 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel9ccfb642009-12-09 18:24:21 +00001723 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach042483e2009-11-21 23:12:12 +00001724 if (VariableDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001725 ScopeDIE->addChild(VariableDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001726 }
1727
1728 // Add nested scopes.
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001729 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001730 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1731 // Define the Scope debug information entry.
Devang Patel930143b2009-11-21 02:48:08 +00001732 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach042483e2009-11-21 23:12:12 +00001733 if (NestedDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001734 ScopeDIE->addChild(NestedDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001735 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001736
1737 if (DS.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001738 addPubTypes(DISubprogram(DS));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001739
1740 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001741}
1742
Bill Wendling2b128d72009-05-20 23:19:06 +00001743/// GetOrCreateSourceID - Look up the source id with the given directory and
1744/// source file names. If none currently exists, create a new id and insert it
1745/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1746/// maps as well.
Chris Lattner71696ef2010-03-31 06:06:37 +00001747unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendling2b128d72009-05-20 23:19:06 +00001748 unsigned DId;
1749 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1750 if (DI != DirectoryIdMap.end()) {
1751 DId = DI->getValue();
1752 } else {
1753 DId = DirectoryNames.size() + 1;
1754 DirectoryIdMap[DirName] = DId;
1755 DirectoryNames.push_back(DirName);
1756 }
1757
1758 unsigned FId;
1759 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1760 if (FI != SourceFileIdMap.end()) {
1761 FId = FI->getValue();
1762 } else {
1763 FId = SourceFileNames.size() + 1;
1764 SourceFileIdMap[FileName] = FId;
1765 SourceFileNames.push_back(FileName);
1766 }
1767
1768 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1769 SourceIdMap.find(std::make_pair(DId, FId));
1770 if (SI != SourceIdMap.end())
1771 return SI->second;
1772
1773 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1774 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1775 SourceIds.push_back(std::make_pair(DId, FId));
1776
1777 return SrcId;
1778}
1779
Devang Patel1f4690c2009-12-15 19:16:48 +00001780/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1781DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001782 CompileUnit *TheCU = getCompileUnit(NS);
1783 DIE *NDie = TheCU->getDIE(NS);
Devang Patel1f4690c2009-12-15 19:16:48 +00001784 if (NDie)
1785 return NDie;
1786 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001787 TheCU->insertDIE(NS, NDie);
Devang Patel1f4690c2009-12-15 19:16:48 +00001788 if (!NS.getName().empty())
1789 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1790 addSourceLine(NDie, &NS);
1791 addToContextOwner(NDie, NS.getContext());
1792 return NDie;
1793}
1794
Devang Patel1a0df9a2010-05-10 22:49:55 +00001795/// constructCompileUnit - Create new CompileUnit for the given
1796/// metadata node with tag DW_TAG_compile_unit.
Devang Patel32cc43c2010-05-07 20:54:48 +00001797void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001798 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +00001799 StringRef FN = DIUnit.getFilename();
1800 StringRef Dir = DIUnit.getDirectory();
Devang Patelb2969422009-09-29 18:40:58 +00001801 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendling2b128d72009-05-20 23:19:06 +00001802
1803 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel930143b2009-11-21 02:48:08 +00001804 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001805 DIUnit.getProducer());
Devang Patel930143b2009-11-21 02:48:08 +00001806 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendling2b128d72009-05-20 23:19:06 +00001807 DIUnit.getLanguage());
Devang Patel930143b2009-11-21 02:48:08 +00001808 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +00001809 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1810 // simplifies debug range entries.
Devang Patel1de21ec2010-06-28 22:22:47 +00001811 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +00001812 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel24bc1b52010-06-29 20:17:53 +00001813 // compile unit in debug_line section. This offset is calculated
1814 // during endMoudle().
1815 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +00001816
Devang Patel2d9caf92009-11-25 17:36:49 +00001817 if (!Dir.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001818 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +00001819 if (DIUnit.isOptimized())
Devang Patel930143b2009-11-21 02:48:08 +00001820 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +00001821
Devang Patel2d9caf92009-11-25 17:36:49 +00001822 StringRef Flags = DIUnit.getFlags();
1823 if (!Flags.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001824 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendling2b128d72009-05-20 23:19:06 +00001825
1826 unsigned RVer = DIUnit.getRunTimeVersion();
1827 if (RVer)
Devang Patel930143b2009-11-21 02:48:08 +00001828 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +00001829 dwarf::DW_FORM_data1, RVer);
1830
Devang Patel1a0df9a2010-05-10 22:49:55 +00001831 CompileUnit *NewCU = new CompileUnit(ID, Die);
1832 if (!FirstCU)
1833 FirstCU = NewCU;
1834 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendling2b128d72009-05-20 23:19:06 +00001835}
1836
Devang Patel1a0df9a2010-05-10 22:49:55 +00001837/// getCompielUnit - Get CompileUnit DIE.
1838CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1839 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1840 DIDescriptor D(N);
1841 const MDNode *CUNode = NULL;
1842 if (D.isCompileUnit())
1843 CUNode = N;
1844 else if (D.isSubprogram())
1845 CUNode = DISubprogram(N).getCompileUnit();
1846 else if (D.isType())
1847 CUNode = DIType(N).getCompileUnit();
1848 else if (D.isGlobalVariable())
1849 CUNode = DIGlobalVariable(N).getCompileUnit();
1850 else if (D.isVariable())
1851 CUNode = DIVariable(N).getCompileUnit();
1852 else if (D.isNameSpace())
1853 CUNode = DINameSpace(N).getCompileUnit();
1854 else if (D.isFile())
1855 CUNode = DIFile(N).getCompileUnit();
1856 else
1857 return FirstCU;
1858
1859 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1860 = CUMap.find(CUNode);
1861 if (I == CUMap.end())
1862 return FirstCU;
1863 return I->second;
1864}
1865
1866
1867/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00001868void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001869 DIGlobalVariable DI_GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001870
Devang Patelf5d53602009-09-04 23:59:07 +00001871 // If debug information is malformed then ignore it.
1872 if (DI_GV.Verify() == false)
1873 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001874
1875 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001876 CompileUnit *TheCU = getCompileUnit(N);
1877 if (TheCU->getDIE(DI_GV))
Devang Patel0751a282009-06-26 01:49:18 +00001878 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001879
Devang Patel9ccfb642009-12-09 18:24:21 +00001880 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel2eec32d2009-12-10 23:25:41 +00001881 if (!VariableDie)
1882 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001883
Bill Wendling2b128d72009-05-20 23:19:06 +00001884 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001885 TheCU->insertDIE(N, VariableDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001886
1887 // Add to context owner.
Devang Patel89880c82010-01-15 01:12:22 +00001888 DIDescriptor GVContext = DI_GV.getContext();
1889 // Do not create specification DIE if context is either compile unit
1890 // or a subprogram.
Chris Lattner71696ef2010-03-31 06:06:37 +00001891 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel019922d2010-04-06 23:53:48 +00001892 !GVContext.isFile() &&
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001893 !isSubprogramContext(GVContext)) {
Devang Patel1f4690c2009-12-15 19:16:48 +00001894 // Create specification DIE.
1895 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1896 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1897 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001898 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel1f4690c2009-12-15 19:16:48 +00001899 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner8dcf41e2010-03-08 22:31:46 +00001900 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner9e4cafe2010-03-12 21:09:07 +00001901 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel1f4690c2009-12-15 19:16:48 +00001902 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patelce25dd72010-02-09 01:58:33 +00001903 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001904 TheCU->addDie(VariableSpecDIE);
Devang Patel1f4690c2009-12-15 19:16:48 +00001905 } else {
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001906 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel1f4690c2009-12-15 19:16:48 +00001907 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner8dcf41e2010-03-08 22:31:46 +00001908 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner9e4cafe2010-03-12 21:09:07 +00001909 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel1f4690c2009-12-15 19:16:48 +00001910 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1911 }
Devang Patel89880c82010-01-15 01:12:22 +00001912 addToContextOwner(VariableDie, GVContext);
Devang Patel2b75ed22009-12-10 19:14:49 +00001913
Bill Wendling2b128d72009-05-20 23:19:06 +00001914 // Expose as global. FIXME - need to check external flag.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001915 TheCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001916
1917 DIType GTy = DI_GV.getType();
Devang Patel12d150e2010-04-13 20:35:04 +00001918 if (GTy.isCompositeType() && !GTy.getName().empty()
1919 && !GTy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001920 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001921 assert(Entry && "Missing global type!");
Devang Patel1a0df9a2010-05-10 22:49:55 +00001922 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001923 }
Devang Patel0751a282009-06-26 01:49:18 +00001924 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001925}
1926
Devang Patel1a0df9a2010-05-10 22:49:55 +00001927/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00001928void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001929 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +00001930
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001931 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001932 CompileUnit *TheCU = getCompileUnit(N);
1933 if (TheCU->getDIE(N))
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001934 return;
1935
Bill Wendling2b128d72009-05-20 23:19:06 +00001936 if (!SP.isDefinition())
1937 // This is a method declaration which will be handled while constructing
1938 // class type.
Devang Patel0751a282009-06-26 01:49:18 +00001939 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001940
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001941 DIE *SubprogramDie = createSubprogramDIE(SP);
1942
1943 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001944 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001945
1946 // Add to context owner.
Devang Patel1f4690c2009-12-15 19:16:48 +00001947 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +00001948
Bill Wendling2b128d72009-05-20 23:19:06 +00001949 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001950 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001951
Devang Patel0751a282009-06-26 01:49:18 +00001952 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001953}
1954
Devang Patel930143b2009-11-21 02:48:08 +00001955/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +00001956/// content. Create global DIEs and emit initial debug info sections.
1957/// This is inovked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +00001958void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +00001959 if (DisableDebugInfoPrinting)
1960 return;
1961
Devang Patel63524442009-07-30 18:56:46 +00001962 DebugInfoFinder DbgFinder;
1963 DbgFinder.processModule(*M);
Devang Patel0751a282009-06-26 01:49:18 +00001964
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001965 bool HasDebugInfo = false;
1966
1967 // Scan all the compile-units to see if there are any marked as the main unit.
1968 // if not, we do not generate debug info.
1969 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1970 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1971 if (DICompileUnit(*I).isMain()) {
1972 HasDebugInfo = true;
1973 break;
1974 }
1975 }
1976
1977 if (!HasDebugInfo) return;
1978
1979 // Tell MMI that we have debug info.
1980 MMI->setDebugInfoAvailability(true);
1981
Chris Lattnerd442aa32010-04-04 23:17:54 +00001982 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001983 EmitSectionLabels();
Chris Lattnerd442aa32010-04-04 23:17:54 +00001984
Bill Wendling2b128d72009-05-20 23:19:06 +00001985 // Create all the compile unit DIEs.
Devang Patel63524442009-07-30 18:56:46 +00001986 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1987 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001988 constructCompileUnit(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00001989
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001990 // Create DIEs for each subprogram.
Devang Patel63524442009-07-30 18:56:46 +00001991 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1992 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001993 constructSubprogramDIE(*I);
Devang Patel0751a282009-06-26 01:49:18 +00001994
Devang Patel2b75ed22009-12-10 19:14:49 +00001995 // Create DIEs for each global variable.
1996 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1997 E = DbgFinder.global_variable_end(); I != E; ++I)
1998 constructGlobalVariableDIE(*I);
1999
Bill Wendling2b128d72009-05-20 23:19:06 +00002000 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +00002001 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00002002
2003 // Print out .file directives to specify files for .loc directives. These are
2004 // printed out early so that they precede any .loc directives.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002005 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendling2b128d72009-05-20 23:19:06 +00002006 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2007 // Remember source id starts at 1.
2008 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002009 // FIXME: don't use sys::path for this! This should not depend on the
2010 // host.
Bill Wendling2b128d72009-05-20 23:19:06 +00002011 sys::Path FullPath(getSourceDirectoryName(Id.first));
2012 bool AppendOk =
2013 FullPath.appendComponent(getSourceFileName(Id.second));
2014 assert(AppendOk && "Could not append filename to directory!");
2015 AppendOk = false;
Chris Lattner601ef332010-01-25 18:58:59 +00002016 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendling2b128d72009-05-20 23:19:06 +00002017 }
2018 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002019}
2020
Devang Patel930143b2009-11-21 02:48:08 +00002021/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +00002022///
Devang Patel930143b2009-11-21 02:48:08 +00002023void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002024 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +00002025 const Module *M = MMI->getModule();
2026 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2027 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2028 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2029 DISubprogram SP(AllSPs->getOperand(SI));
2030 if (!SP.Verify()) continue;
2031
2032 // Collect info for variables that were optimized out.
2033 StringRef FName = SP.getLinkageName();
2034 if (FName.empty())
2035 FName = SP.getName();
2036 NamedMDNode *NMD =
2037 M->getNamedMetadata(Twine("llvm.dbg.lv.", getRealLinkageName(FName)));
2038 if (!NMD) continue;
2039 unsigned E = NMD->getNumOperands();
2040 if (!E) continue;
2041 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
2042 for (unsigned I = 0; I != E; ++I) {
2043 DIVariable DV(NMD->getOperand(I));
2044 if (!DV.Verify()) continue;
2045 Scope->addVariable(new DbgVariable(DV));
2046 }
2047
2048 // Construct subprogram DIE and add variables DIEs.
2049 constructSubprogramDIE(SP);
2050 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
2051 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
2052 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2053 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2054 if (VariableDIE)
2055 ScopeDIE->addChild(VariableDIE);
2056 }
2057 }
2058 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002059
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002060 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2061 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2062 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2063 DIE *ISP = *AI;
Devang Patel930143b2009-11-21 02:48:08 +00002064 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002065 }
2066
Devang Patel32cc43c2010-05-07 20:54:48 +00002067 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Pateleb57c592009-12-03 19:11:07 +00002068 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2069 DIE *SPDie = CI->first;
Devang Patel32cc43c2010-05-07 20:54:48 +00002070 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Pateleb57c592009-12-03 19:11:07 +00002071 if (!N) continue;
Devang Patel1a0df9a2010-05-10 22:49:55 +00002072 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Pateleb57c592009-12-03 19:11:07 +00002073 if (!NDie) continue;
2074 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Pateleb57c592009-12-03 19:11:07 +00002075 }
2076
Bill Wendling2b128d72009-05-20 23:19:06 +00002077 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002078 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002079 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002080 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002081 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +00002082
2083 // End text sections.
2084 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002085 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +00002086 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +00002087 }
2088
2089 // Emit common frame information.
Devang Patel930143b2009-11-21 02:48:08 +00002090 emitCommonDebugFrame();
Bill Wendling2b128d72009-05-20 23:19:06 +00002091
2092 // Emit function debug frame information
2093 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2094 E = DebugFrames.end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002095 emitFunctionDebugFrame(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00002096
2097 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +00002098 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +00002099
Devang Patel24bc1b52010-06-29 20:17:53 +00002100 // Emit source line correspondence into a debug line section.
2101 emitDebugLines();
2102
Bill Wendling2b128d72009-05-20 23:19:06 +00002103 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +00002104 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002105
2106 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +00002107 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +00002108
Bill Wendling2b128d72009-05-20 23:19:06 +00002109 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +00002110 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +00002111
Devang Patel04d2f2d2009-11-24 01:14:22 +00002112 // Emit info into a debug pubtypes section.
2113 emitDebugPubTypes();
2114
Bill Wendling2b128d72009-05-20 23:19:06 +00002115 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002116 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +00002117
2118 // Emit info into a debug aranges section.
2119 EmitDebugARanges();
2120
2121 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002122 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +00002123
2124 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002125 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002126
2127 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +00002128 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002129
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002130 // Emit info into a debug str section.
2131 emitDebugStr();
2132
Devang Patel1a0df9a2010-05-10 22:49:55 +00002133 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2134 E = CUMap.end(); I != E; ++I)
2135 delete I->second;
2136 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +00002137}
2138
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002139/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patele1c53f22010-05-20 16:36:41 +00002140DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattner915c5f92010-04-02 19:42:39 +00002141 DebugLoc ScopeLoc) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002142
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002143 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002144 if (AbsDbgVariable)
2145 return AbsDbgVariable;
2146
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002147 LLVMContext &Ctx = Var->getContext();
Chris Lattner915c5f92010-04-02 19:42:39 +00002148 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002149 if (!Scope)
2150 return NULL;
2151
Devang Patele1c53f22010-05-20 16:36:41 +00002152 AbsDbgVariable = new DbgVariable(Var);
Devang Patel930143b2009-11-21 02:48:08 +00002153 Scope->addVariable(AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002154 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002155 return AbsDbgVariable;
2156}
2157
Devang Patel490c8ab2010-05-20 19:57:06 +00002158/// collectVariableInfoFromMMITable - Collect variable information from
2159/// side table maintained by MMI.
2160void
2161DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2162 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002163 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patel475d32a2009-10-06 01:26:37 +00002164 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2165 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2166 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00002167 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002168 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00002169 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00002170 DIVariable DV(Var);
2171 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002172
Chris Lattner915c5f92010-04-02 19:42:39 +00002173 DbgScope *Scope = 0;
Devang Patel32cc43c2010-05-07 20:54:48 +00002174 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattner915c5f92010-04-02 19:42:39 +00002175 Scope = ConcreteScopes.lookup(IA);
2176 if (Scope == 0)
2177 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2178
Devang Patelcdb7d442009-11-10 23:20:04 +00002179 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00002180 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00002181 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002182
Devang Patele1c53f22010-05-20 16:36:41 +00002183 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2184 DbgVariable *RegVar = new DbgVariable(DV);
2185 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel930143b2009-11-21 02:48:08 +00002186 Scope->addVariable(RegVar);
Devang Patele1c53f22010-05-20 16:36:41 +00002187 if (AbsDbgVariable) {
2188 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2189 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2190 }
Devang Patel475d32a2009-10-06 01:26:37 +00002191 }
Devang Patel490c8ab2010-05-20 19:57:06 +00002192}
Devang Patela3e9c9c2010-03-15 18:33:46 +00002193
Devang Patel9fc11702010-05-25 23:40:22 +00002194/// isDbgValueInUndefinedReg - Return true if debug value, encoded by
2195/// DBG_VALUE instruction, is in undefined reg.
2196static bool isDbgValueInUndefinedReg(const MachineInstr *MI) {
2197 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2198 if (MI->getOperand(0).isReg() && !MI->getOperand(0).getReg())
2199 return true;
2200 return false;
2201}
2202
2203/// isDbgValueInDefinedReg - Return true if debug value, encoded by
2204/// DBG_VALUE instruction, is in a defined reg.
2205static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2206 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2207 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
2208 return true;
2209 return false;
2210}
2211
Devang Patel490c8ab2010-05-20 19:57:06 +00002212/// collectVariableInfo - Populate DbgScope entries with variables' info.
Devang Patel5c0f85c2010-06-25 22:07:34 +00002213void
2214DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2215 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel490c8ab2010-05-20 19:57:06 +00002216
2217 /// collection info from MMI table.
2218 collectVariableInfoFromMMITable(MF, Processed);
2219
2220 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patela3e9c9c2010-03-15 18:33:46 +00002221 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattner3a383cb2010-04-05 00:13:49 +00002222 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel490c8ab2010-05-20 19:57:06 +00002223 I != E; ++I)
Devang Patela3e9c9c2010-03-15 18:33:46 +00002224 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2225 II != IE; ++II) {
2226 const MachineInstr *MInsn = II;
Devang Patel9fc11702010-05-25 23:40:22 +00002227 if (!MInsn->isDebugValue() || isDbgValueInUndefinedReg(MInsn))
Devang Patela3e9c9c2010-03-15 18:33:46 +00002228 continue;
Devang Patel490c8ab2010-05-20 19:57:06 +00002229 DbgValues.push_back(MInsn);
2230 }
Devang Patela3e9c9c2010-03-15 18:33:46 +00002231
Devang Patel9fc11702010-05-25 23:40:22 +00002232 // This is a collection of DBV_VALUE instructions describing same variable.
2233 SmallVector<const MachineInstr *, 4> MultipleValues;
Devang Patel490c8ab2010-05-20 19:57:06 +00002234 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2235 E = DbgValues.end(); I != E; ++I) {
2236 const MachineInstr *MInsn = *I;
Devang Patel9fc11702010-05-25 23:40:22 +00002237 MultipleValues.clear();
2238 if (isDbgValueInDefinedReg(MInsn))
2239 MultipleValues.push_back(MInsn);
Devang Patel490c8ab2010-05-20 19:57:06 +00002240 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2241 if (Processed.count(DV) != 0)
2242 continue;
2243
Devang Patelc2254f62010-06-02 19:05:13 +00002244 const MachineInstr *PrevMI = MInsn;
Devang Patel9fc11702010-05-25 23:40:22 +00002245 for (SmallVector<const MachineInstr *, 8>::iterator MI = I+1,
2246 ME = DbgValues.end(); MI != ME; ++MI) {
2247 const MDNode *Var =
2248 (*MI)->getOperand((*MI)->getNumOperands()-1).getMetadata();
Devang Patelc2254f62010-06-02 19:05:13 +00002249 if (Var == DV && isDbgValueInDefinedReg(*MI) &&
2250 !PrevMI->isIdenticalTo(*MI))
Devang Patel9fc11702010-05-25 23:40:22 +00002251 MultipleValues.push_back(*MI);
Devang Patelc2254f62010-06-02 19:05:13 +00002252 PrevMI = *MI;
Devang Patel9fc11702010-05-25 23:40:22 +00002253 }
2254
Devang Patel490c8ab2010-05-20 19:57:06 +00002255 DbgScope *Scope = findDbgScope(MInsn);
Devang Patel7a9dedf2010-05-27 20:25:04 +00002256 bool CurFnArg = false;
2257 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2258 DISubprogram(DV.getContext()).describes(MF->getFunction()))
2259 CurFnArg = true;
2260 if (!Scope && CurFnArg)
Devang Patelfbd6c452010-05-21 00:10:20 +00002261 Scope = CurrentFnDbgScope;
Devang Patel490c8ab2010-05-20 19:57:06 +00002262 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00002263 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00002264 continue;
2265
2266 Processed.insert(DV);
Devang Patel490c8ab2010-05-20 19:57:06 +00002267 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel490c8ab2010-05-20 19:57:06 +00002268 Scope->addVariable(RegVar);
Devang Patel7a9dedf2010-05-27 20:25:04 +00002269 if (!CurFnArg)
Devang Patel9fc11702010-05-25 23:40:22 +00002270 DbgVariableLabelsMap[RegVar] = getLabelBeforeInsn(MInsn);
Devang Patelfbd6c452010-05-21 00:10:20 +00002271 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2272 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2273 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patela3e9c9c2010-03-15 18:33:46 +00002274 }
Devang Patel9fc11702010-05-25 23:40:22 +00002275 if (MultipleValues.size() <= 1) {
2276 DbgVariableToDbgInstMap[RegVar] = MInsn;
2277 continue;
2278 }
2279
2280 // handle multiple DBG_VALUE instructions describing one variable.
Devang Patel9fc11702010-05-25 23:40:22 +00002281 if (DotDebugLocEntries.empty())
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002282 RegVar->setDotDebugLocOffset(0);
2283 else
2284 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002285 const MachineInstr *Begin = NULL;
2286 const MachineInstr *End = NULL;
2287 for (SmallVector<const MachineInstr *, 4>::iterator
Devang Patel30265c42010-07-07 20:12:52 +00002288 MVI = MultipleValues.begin(), MVE = MultipleValues.end();
2289 MVI != MVE; ++MVI) {
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002290 if (!Begin) {
2291 Begin = *MVI;
2292 continue;
2293 }
2294 End = *MVI;
Devang Patel9fc11702010-05-25 23:40:22 +00002295 MachineLocation MLoc;
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002296 MLoc.set(Begin->getOperand(0).getReg(), 0);
2297 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2298 const MCSymbol *SLabel = getLabelBeforeInsn(End);
Devang Patel9fc11702010-05-25 23:40:22 +00002299 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002300 Begin = End;
2301 if (MVI + 1 == MVE) {
2302 // If End is the last instruction then its value is valid
Devang Patel9fc11702010-05-25 23:40:22 +00002303 // until the end of the funtion.
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002304 MLoc.set(End->getOperand(0).getReg(), 0);
Devang Patel9fc11702010-05-25 23:40:22 +00002305 DotDebugLocEntries.
2306 push_back(DotDebugLocEntry(SLabel, FunctionEndSym, MLoc));
2307 }
2308 }
2309 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00002310 }
Devang Patele0a94bf2010-05-14 21:01:35 +00002311
2312 // Collect info for variables that were optimized out.
Devang Patelad517352010-06-22 01:01:58 +00002313 const Function *F = MF->getFunction();
2314 const Module *M = F->getParent();
2315 if (NamedMDNode *NMD =
Devang Patelb6e058d2010-06-22 01:19:38 +00002316 M->getNamedMetadata(Twine("llvm.dbg.lv.",
2317 getRealLinkageName(F->getName())))) {
Devang Patele0a94bf2010-05-14 21:01:35 +00002318 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2319 DIVariable DV(cast_or_null<MDNode>(NMD->getOperand(i)));
Devang Patel490c8ab2010-05-20 19:57:06 +00002320 if (!DV || !Processed.insert(DV))
Devang Patele0a94bf2010-05-14 21:01:35 +00002321 continue;
2322 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2323 if (Scope)
Devang Patele1c53f22010-05-20 16:36:41 +00002324 Scope->addVariable(new DbgVariable(DV));
Devang Patele0a94bf2010-05-14 21:01:35 +00002325 }
2326 }
Devang Patel9fc11702010-05-25 23:40:22 +00002327}
Devang Patele0a94bf2010-05-14 21:01:35 +00002328
Devang Patel9fc11702010-05-25 23:40:22 +00002329/// getLabelBeforeInsn - Return Label preceding the instruction.
2330const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2331 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2332 LabelsBeforeInsn.find(MI);
2333 if (I == LabelsBeforeInsn.end())
2334 // FunctionBeginSym always preceeds all the instruction in current function.
2335 return FunctionBeginSym;
2336 return I->second;
2337}
2338
2339/// getLabelAfterInsn - Return Label immediately following the instruction.
2340const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2341 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2342 LabelsAfterInsn.find(MI);
2343 if (I == LabelsAfterInsn.end())
2344 return NULL;
2345 return I->second;
Devang Patel475d32a2009-10-06 01:26:37 +00002346}
2347
Devang Patelbd477be2010-03-29 17:20:31 +00002348/// beginScope - Process beginning of a scope.
2349void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patel002d54d2010-05-26 19:37:24 +00002350 if (InsnNeedsLabel.count(MI) == 0) {
2351 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patelbd477be2010-03-29 17:20:31 +00002352 return;
Devang Patel9fc11702010-05-25 23:40:22 +00002353 }
Devang Patel23b2ae62010-03-29 22:59:58 +00002354
Devang Patel002d54d2010-05-26 19:37:24 +00002355 // Check location.
2356 DebugLoc DL = MI->getDebugLoc();
2357 if (!DL.isUnknown()) {
2358 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2359 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Patel6c74a872010-04-27 19:46:33 +00002360 PrevInstLoc = DL;
Devang Patel002d54d2010-05-26 19:37:24 +00002361 LabelsBeforeInsn[MI] = PrevLabel;
2362 return;
Devang Patel6c74a872010-04-27 19:46:33 +00002363 }
Devang Patelbd477be2010-03-29 17:20:31 +00002364
Devang Patelacc32a52010-05-26 21:23:46 +00002365 // If location is unknown then use temp label for this DBG_VALUE
Devang Patel002d54d2010-05-26 19:37:24 +00002366 // instruction.
2367 if (MI->isDebugValue()) {
Devang Patelacc32a52010-05-26 21:23:46 +00002368 PrevLabel = MMI->getContext().CreateTempSymbol();
2369 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00002370 LabelsBeforeInsn[MI] = PrevLabel;
2371 return;
2372 }
2373
2374 if (UnknownLocations) {
2375 PrevLabel = recordSourceLine(0, 0, 0);
2376 LabelsBeforeInsn[MI] = PrevLabel;
2377 return;
2378 }
2379
2380 assert (0 && "Instruction is not processed!");
Devang Patel8db360d2009-10-06 01:50:42 +00002381}
2382
Devang Patel930143b2009-11-21 02:48:08 +00002383/// endScope - Process end of a scope.
2384void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel3ebd8932010-04-08 16:50:29 +00002385 if (InsnsEndScopeSet.count(MI) != 0) {
2386 // Emit a label if this instruction ends a scope.
2387 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2388 Asm->OutStreamer.EmitLabel(Label);
Devang Patel6c74a872010-04-27 19:46:33 +00002389 LabelsAfterInsn[MI] = Label;
Devang Patel3ebd8932010-04-08 16:50:29 +00002390 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002391}
2392
Devang Patel6c74a872010-04-27 19:46:33 +00002393/// getOrCreateDbgScope - Create DbgScope for the scope.
Devang Patel30265c42010-07-07 20:12:52 +00002394DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
2395 const MDNode *InlinedAt) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002396 if (!InlinedAt) {
2397 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2398 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002399 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002400 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2401 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patel6c74a872010-04-27 19:46:33 +00002402 if (DIDescriptor(Scope).isLexicalBlock()) {
2403 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002404 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Patel6c74a872010-04-27 19:46:33 +00002405 WScope->setParent(Parent);
2406 Parent->addScope(WScope);
2407 }
2408
2409 if (!WScope->getParent()) {
2410 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastings9b5005c2010-06-15 23:06:30 +00002411 // We used to check only for a linkage name, but that fails
2412 // since we began omitting the linkage name for private
2413 // functions. The new way is to check for the name in metadata,
2414 // but that's not supported in old .ll test cases. Ergo, we
2415 // check both.
Stuart Hastingsafe54f12010-06-11 20:08:44 +00002416 if (SPName == Asm->MF->getFunction()->getName() ||
2417 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Patel6c74a872010-04-27 19:46:33 +00002418 CurrentFnDbgScope = WScope;
2419 }
2420
2421 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002422 }
2423
Devang Patel5c0f85c2010-06-25 22:07:34 +00002424 getOrCreateAbstractScope(Scope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002425 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2426 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002427 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002428
2429 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2430 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2431 DILocation DL(InlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002432 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002433 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Patel6c74a872010-04-27 19:46:33 +00002434 WScope->setParent(Parent);
2435 Parent->addScope(WScope);
2436
2437 ConcreteScopes[InlinedAt] = WScope;
Devang Patel6c74a872010-04-27 19:46:33 +00002438
2439 return WScope;
Devang Patel8db360d2009-10-06 01:50:42 +00002440}
2441
Devang Patel6c74a872010-04-27 19:46:33 +00002442/// hasValidLocation - Return true if debug location entry attached with
2443/// machine instruction encodes valid location info.
2444static bool hasValidLocation(LLVMContext &Ctx,
2445 const MachineInstr *MInsn,
Devang Patel32cc43c2010-05-07 20:54:48 +00002446 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Patel6c74a872010-04-27 19:46:33 +00002447 DebugLoc DL = MInsn->getDebugLoc();
2448 if (DL.isUnknown()) return false;
2449
Devang Patel32cc43c2010-05-07 20:54:48 +00002450 const MDNode *S = DL.getScope(Ctx);
Devang Patel6c74a872010-04-27 19:46:33 +00002451
2452 // There is no need to create another DIE for compile unit. For all
2453 // other scopes, create one DbgScope now. This will be translated
2454 // into a scope DIE at the end.
2455 if (DIScope(S).isCompileUnit()) return false;
2456
2457 Scope = S;
2458 InlinedAt = DL.getInlinedAt(Ctx);
2459 return true;
2460}
2461
2462/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2463/// hierarchy.
2464static void calculateDominanceGraph(DbgScope *Scope) {
2465 assert (Scope && "Unable to calculate scop edominance graph!");
2466 SmallVector<DbgScope *, 4> WorkStack;
2467 WorkStack.push_back(Scope);
2468 unsigned Counter = 0;
2469 while (!WorkStack.empty()) {
2470 DbgScope *WS = WorkStack.back();
2471 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2472 bool visitedChildren = false;
2473 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2474 SE = Children.end(); SI != SE; ++SI) {
2475 DbgScope *ChildScope = *SI;
2476 if (!ChildScope->getDFSOut()) {
2477 WorkStack.push_back(ChildScope);
2478 visitedChildren = true;
2479 ChildScope->setDFSIn(++Counter);
2480 break;
2481 }
2482 }
2483 if (!visitedChildren) {
2484 WorkStack.pop_back();
2485 WS->setDFSOut(++Counter);
2486 }
2487 }
2488}
2489
2490/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2491static
2492void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2493 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2494{
2495#ifndef NDEBUG
2496 unsigned PrevDFSIn = 0;
2497 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2498 I != E; ++I) {
2499 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2500 II != IE; ++II) {
2501 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002502 const MDNode *Scope = NULL;
2503 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002504
2505 // Check if instruction has valid location information.
2506 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2507 dbgs() << " [ ";
2508 if (InlinedAt)
2509 dbgs() << "*";
2510 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2511 MI2ScopeMap.find(MInsn);
2512 if (DI != MI2ScopeMap.end()) {
2513 DbgScope *S = DI->second;
2514 dbgs() << S->getDFSIn();
2515 PrevDFSIn = S->getDFSIn();
2516 } else
2517 dbgs() << PrevDFSIn;
2518 } else
2519 dbgs() << " [ x" << PrevDFSIn;
2520 dbgs() << " ]";
2521 MInsn->dump();
2522 }
2523 dbgs() << "\n";
2524 }
2525#endif
2526}
Devang Patel930143b2009-11-21 02:48:08 +00002527/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner848c7d22010-03-31 05:39:57 +00002528/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner76555b52010-01-26 23:18:02 +00002529bool DwarfDebug::extractScopeInformation() {
Devang Patel75cc16c2009-10-01 20:31:14 +00002530 // If scope information was extracted using .dbg intrinsics then there is not
2531 // any need to extract these information by scanning each instruction.
2532 if (!DbgScopeMap.empty())
2533 return false;
2534
Dan Gohmane9135cb2010-04-23 01:18:53 +00002535 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6c74a872010-04-27 19:46:33 +00002536 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2537 SmallVector<DbgRange, 4> MIRanges;
2538 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patel32cc43c2010-05-07 20:54:48 +00002539 const MDNode *PrevScope = NULL;
2540 const MDNode *PrevInlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002541 const MachineInstr *RangeBeginMI = NULL;
2542 const MachineInstr *PrevMI = NULL;
Chris Lattner3a383cb2010-04-05 00:13:49 +00002543 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel75cc16c2009-10-01 20:31:14 +00002544 I != E; ++I) {
2545 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2546 II != IE; ++II) {
2547 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002548 const MDNode *Scope = NULL;
2549 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002550
2551 // Check if instruction has valid location information.
2552 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2553 PrevMI = MInsn;
2554 continue;
2555 }
Chris Lattner915c5f92010-04-02 19:42:39 +00002556
Devang Patel6c74a872010-04-27 19:46:33 +00002557 // If scope has not changed then skip this instruction.
2558 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2559 PrevMI = MInsn;
2560 continue;
2561 }
2562
2563 if (RangeBeginMI) {
2564 // If we have alread seen a beginning of a instruction range and
2565 // current instruction scope does not match scope of first instruction
2566 // in this range then create a new instruction range.
2567 DbgRange R(RangeBeginMI, PrevMI);
Devang Patel30265c42010-07-07 20:12:52 +00002568 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
2569 PrevInlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002570 MIRanges.push_back(R);
2571 }
2572
2573 // This is a beginning of a new instruction range.
2574 RangeBeginMI = MInsn;
Chris Lattner915c5f92010-04-02 19:42:39 +00002575
Devang Patel6c74a872010-04-27 19:46:33 +00002576 // Reset previous markers.
2577 PrevMI = MInsn;
2578 PrevScope = Scope;
2579 PrevInlinedAt = InlinedAt;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002580 }
2581 }
2582
Devang Patel6c74a872010-04-27 19:46:33 +00002583 // Create last instruction range.
2584 if (RangeBeginMI && PrevMI && PrevScope) {
2585 DbgRange R(RangeBeginMI, PrevMI);
2586 MIRanges.push_back(R);
2587 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel75cc16c2009-10-01 20:31:14 +00002588 }
Devang Patel6c74a872010-04-27 19:46:33 +00002589
Devang Patel530a0752010-01-04 20:44:00 +00002590 if (!CurrentFnDbgScope)
2591 return false;
2592
Devang Patel6c74a872010-04-27 19:46:33 +00002593 calculateDominanceGraph(CurrentFnDbgScope);
2594 if (PrintDbgScope)
2595 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2596
2597 // Find ranges of instructions covered by each DbgScope;
2598 DbgScope *PrevDbgScope = NULL;
2599 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2600 RE = MIRanges.end(); RI != RE; ++RI) {
2601 const DbgRange &R = *RI;
2602 DbgScope *S = MI2ScopeMap.lookup(R.first);
2603 assert (S && "Lost DbgScope for a machine instruction!");
2604 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2605 PrevDbgScope->closeInsnRange(S);
2606 S->openInsnRange(R.first);
2607 S->extendInsnRange(R.second);
2608 PrevDbgScope = S;
2609 }
2610
2611 if (PrevDbgScope)
2612 PrevDbgScope->closeInsnRange();
Devang Patel75cc16c2009-10-01 20:31:14 +00002613
Devang Patel359b0132010-04-08 18:43:56 +00002614 identifyScopeMarkers();
Devang Patelf1d5a1e2010-04-08 15:37:09 +00002615
2616 return !DbgScopeMap.empty();
2617}
2618
Devang Patel6c74a872010-04-27 19:46:33 +00002619/// identifyScopeMarkers() -
2620/// Each DbgScope has first instruction and last instruction to mark beginning
2621/// and end of a scope respectively. Create an inverse map that list scopes
2622/// starts (and ends) with an instruction. One instruction may start (or end)
2623/// multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00002624void DwarfDebug::identifyScopeMarkers() {
Devang Patel7771b7c2010-01-20 02:05:23 +00002625 SmallVector<DbgScope *, 4> WorkList;
2626 WorkList.push_back(CurrentFnDbgScope);
2627 while (!WorkList.empty()) {
Chris Lattner848c7d22010-03-31 05:39:57 +00002628 DbgScope *S = WorkList.pop_back_val();
Devang Patel6c74a872010-04-27 19:46:33 +00002629
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002630 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patel7771b7c2010-01-20 02:05:23 +00002631 if (!Children.empty())
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002632 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00002633 SE = Children.end(); SI != SE; ++SI)
2634 WorkList.push_back(*SI);
2635
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002636 if (S->isAbstractScope())
2637 continue;
Devang Patel6c74a872010-04-27 19:46:33 +00002638
2639 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2640 if (Ranges.empty())
2641 continue;
2642 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2643 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patela0813082010-05-19 21:58:28 +00002644 assert(RI->first && "DbgRange does not have first instruction!");
2645 assert(RI->second && "DbgRange does not have second instruction!");
Devang Patel6c74a872010-04-27 19:46:33 +00002646 InsnsEndScopeSet.insert(RI->second);
2647 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002648 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002649}
2650
Dan Gohman3df671a2010-04-20 00:37:27 +00002651/// FindFirstDebugLoc - Find the first debug location in the function. This
2652/// is intended to be an approximation for the source position of the
2653/// beginning of the function.
2654static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2655 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2656 I != E; ++I)
2657 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2658 MBBI != MBBE; ++MBBI) {
2659 DebugLoc DL = MBBI->getDebugLoc();
2660 if (!DL.isUnknown())
2661 return DL;
2662 }
2663 return DebugLoc();
2664}
2665
Devang Patel930143b2009-11-21 02:48:08 +00002666/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00002667/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00002668void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00002669 if (!MMI->hasDebugInfo()) return;
Bill Wendlingfcc14142010-04-07 09:28:04 +00002670 if (!extractScopeInformation()) return;
Devang Patel4598eb62009-10-06 18:37:31 +00002671
Devang Patel6c74a872010-04-27 19:46:33 +00002672 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2673 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00002674 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00002675 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00002676
2677 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2678 // function.
Dan Gohman3df671a2010-04-20 00:37:27 +00002679 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattner915c5f92010-04-02 19:42:39 +00002680 if (FDL.isUnknown()) return;
2681
Devang Patel32cc43c2010-05-07 20:54:48 +00002682 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Chris Lattner915c5f92010-04-02 19:42:39 +00002683
2684 DISubprogram SP = getDISubprogram(Scope);
2685 unsigned Line, Col;
2686 if (SP.Verify()) {
2687 Line = SP.getLineNumber();
2688 Col = 0;
2689 } else {
2690 Line = FDL.getLine();
2691 Col = FDL.getCol();
Bill Wendling2b128d72009-05-20 23:19:06 +00002692 }
Chris Lattner915c5f92010-04-02 19:42:39 +00002693
2694 recordSourceLine(Line, Col, Scope);
Devang Patel002d54d2010-05-26 19:37:24 +00002695
Devang Patel21ccf052010-06-02 16:42:51 +00002696 /// ProcessedArgs - Collection of arguments already processed.
2697 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2698
Devang Patel002d54d2010-05-26 19:37:24 +00002699 DebugLoc PrevLoc;
2700 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2701 I != E; ++I)
2702 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2703 II != IE; ++II) {
2704 const MachineInstr *MI = II;
2705 DebugLoc DL = MI->getDebugLoc();
2706 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00002707 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2708 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2709 if (!DV.Verify()) continue;
Devang Patel5e6b71c2010-05-27 16:47:30 +00002710 // If DBG_VALUE is for a local variable then it needs a label.
Devang Patel42939752010-07-01 21:38:08 +00002711 if (DV.getTag() != dwarf::DW_TAG_arg_variable
2712 && isDbgValueInUndefinedReg(MI) == false)
Devang Patel002d54d2010-05-26 19:37:24 +00002713 InsnNeedsLabel.insert(MI);
Devang Patel5e6b71c2010-05-27 16:47:30 +00002714 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel42939752010-07-01 21:38:08 +00002715 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2716 describes(MF->getFunction()))
Devang Patel5e6b71c2010-05-27 16:47:30 +00002717 InsnNeedsLabel.insert(MI);
2718 // DBG_VALUE indicating argument location change needs a label.
Devang Patelc657c622010-06-24 21:51:19 +00002719 else if (isDbgValueInUndefinedReg(MI) == false && !ProcessedArgs.insert(DV))
Devang Patel002d54d2010-05-26 19:37:24 +00002720 InsnNeedsLabel.insert(MI);
2721 } else {
2722 // If location is unknown then instruction needs a location only if
2723 // UnknownLocations flag is set.
2724 if (DL.isUnknown()) {
2725 if (UnknownLocations && !PrevLoc.isUnknown())
2726 InsnNeedsLabel.insert(MI);
2727 } else if (DL != PrevLoc)
2728 // Otherwise, instruction needs a location only if it is new location.
2729 InsnNeedsLabel.insert(MI);
2730 }
2731
2732 if (!DL.isUnknown() || UnknownLocations)
2733 PrevLoc = DL;
2734 }
2735
2736 PrevLabel = FunctionBeginSym;
Bill Wendling2b128d72009-05-20 23:19:06 +00002737}
2738
Devang Patel930143b2009-11-21 02:48:08 +00002739/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00002740///
Chris Lattner76555b52010-01-26 23:18:02 +00002741void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingfcc14142010-04-07 09:28:04 +00002742 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00002743
Devang Patel530a0752010-01-04 20:44:00 +00002744 if (CurrentFnDbgScope) {
Devang Patel4a8e6e82010-05-22 00:04:14 +00002745
Devang Patel9fc11702010-05-25 23:40:22 +00002746 // Define end label for subprogram.
2747 FunctionEndSym = Asm->GetTempSymbol("func_end",
2748 Asm->getFunctionNumber());
2749 // Assumes in correct section after the entry point.
2750 Asm->OutStreamer.EmitLabel(FunctionEndSym);
2751
Devang Patel5c0f85c2010-06-25 22:07:34 +00002752 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2753 collectVariableInfo(MF, ProcessedVars);
Devang Patel4a8e6e82010-05-22 00:04:14 +00002754
Devang Patel530a0752010-01-04 20:44:00 +00002755 // Get function line info.
2756 if (!Lines.empty()) {
2757 // Get section line info.
2758 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2759 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2760 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2761 // Append the function info to section info.
2762 SectionLineInfos.insert(SectionLineInfos.end(),
2763 Lines.begin(), Lines.end());
2764 }
2765
2766 // Construct abstract scopes.
2767 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel5c0f85c2010-06-25 22:07:34 +00002768 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel5c0f85c2010-06-25 22:07:34 +00002769 DISubprogram SP((*AI)->getScopeNode());
2770 if (SP.Verify()) {
2771 // Collect info for variables that were optimized out.
2772 StringRef FName = SP.getLinkageName();
2773 if (FName.empty())
2774 FName = SP.getName();
2775 const Module *M = MF->getFunction()->getParent();
2776 if (NamedMDNode *NMD =
2777 M->getNamedMetadata(Twine("llvm.dbg.lv.",
2778 getRealLinkageName(FName)))) {
2779 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2780 DIVariable DV(cast_or_null<MDNode>(NMD->getOperand(i)));
2781 if (!DV || !ProcessedVars.insert(DV))
2782 continue;
Devang Patel648df7b2010-06-30 00:11:08 +00002783 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel5c0f85c2010-06-25 22:07:34 +00002784 if (Scope)
2785 Scope->addVariable(new DbgVariable(DV));
2786 }
2787 }
2788 }
Devang Patelc5b31092010-06-30 01:40:11 +00002789 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2790 constructScopeDIE(*AI);
Devang Patel5c0f85c2010-06-25 22:07:34 +00002791 }
Devang Patel30265c42010-07-07 20:12:52 +00002792
Devang Patel075e9b52010-05-04 06:15:30 +00002793 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel530a0752010-01-04 20:44:00 +00002794
Devang Patel075e9b52010-05-04 06:15:30 +00002795 if (!DisableFramePointerElim(*MF))
2796 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2797 dwarf::DW_FORM_flag, 1);
2798
2799
Chris Lattner3a383cb2010-04-05 00:13:49 +00002800 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel530a0752010-01-04 20:44:00 +00002801 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00002802 }
2803
Bill Wendling2b128d72009-05-20 23:19:06 +00002804 // Clear debug info
Devang Patelfe189e62010-01-19 01:26:02 +00002805 CurrentFnDbgScope = NULL;
Devang Patel002d54d2010-05-26 19:37:24 +00002806 InsnNeedsLabel.clear();
Devang Patele1c53f22010-05-20 16:36:41 +00002807 DbgVariableToFrameIndexMap.clear();
2808 VarToAbstractVarMap.clear();
2809 DbgVariableToDbgInstMap.clear();
2810 DbgVariableLabelsMap.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002811 DeleteContainerSeconds(DbgScopeMap);
Devang Patel541019d2010-04-09 16:04:20 +00002812 InsnsEndScopeSet.clear();
Devang Patelfe189e62010-01-19 01:26:02 +00002813 ConcreteScopes.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002814 DeleteContainerSeconds(AbstractScopes);
Devang Patelfe189e62010-01-19 01:26:02 +00002815 AbstractScopesList.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002816 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00002817 LabelsBeforeInsn.clear();
2818 LabelsAfterInsn.clear();
Bill Wendling2b128d72009-05-20 23:19:06 +00002819 Lines.clear();
Devang Patel12563b32010-04-16 23:33:45 +00002820 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00002821}
2822
Devang Patele1c53f22010-05-20 16:36:41 +00002823/// recordVariableFrameIndex - Record a variable's index.
2824void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2825 assert (V && "Invalid DbgVariable!");
2826 DbgVariableToFrameIndexMap[V] = Index;
2827}
2828
2829/// findVariableFrameIndex - Return true if frame index for the variable
2830/// is found. Update FI to hold value of the index.
2831bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2832 assert (V && "Invalid DbgVariable!");
2833 DenseMap<const DbgVariable *, int>::iterator I =
2834 DbgVariableToFrameIndexMap.find(V);
2835 if (I == DbgVariableToFrameIndexMap.end())
2836 return false;
2837 *FI = I->second;
2838 return true;
2839}
2840
2841/// findVariableLabel - Find MCSymbol for the variable.
2842const MCSymbol *DwarfDebug::findVariableLabel(const DbgVariable *V) {
2843 DenseMap<const DbgVariable *, const MCSymbol *>::iterator I
2844 = DbgVariableLabelsMap.find(V);
2845 if (I == DbgVariableLabelsMap.end())
2846 return NULL;
2847 else return I->second;
2848}
2849
Devang Patel490c8ab2010-05-20 19:57:06 +00002850/// findDbgScope - Find DbgScope for the debug loc attached with an
2851/// instruction.
2852DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2853 DbgScope *Scope = NULL;
2854 LLVMContext &Ctx =
2855 MInsn->getParent()->getParent()->getFunction()->getContext();
2856 DebugLoc DL = MInsn->getDebugLoc();
2857
2858 if (DL.isUnknown())
2859 return Scope;
2860
2861 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2862 Scope = ConcreteScopes.lookup(IA);
2863 if (Scope == 0)
2864 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2865
2866 return Scope;
2867}
2868
2869
Chris Lattnerba35a672010-03-09 04:54:43 +00002870/// recordSourceLine - Register a source line with debug info. Returns the
2871/// unique label that was emitted and which provides correspondence to
2872/// the source line list.
Devang Patel30265c42010-07-07 20:12:52 +00002873MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
2874 const MDNode *S) {
Devang Patel2d9caf92009-11-25 17:36:49 +00002875 StringRef Dir;
2876 StringRef Fn;
Devang Patel2089d162009-10-05 18:03:19 +00002877
Dan Gohman50849c62010-05-05 23:41:32 +00002878 unsigned Src = 1;
2879 if (S) {
2880 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00002881
Dan Gohman50849c62010-05-05 23:41:32 +00002882 if (Scope.isCompileUnit()) {
2883 DICompileUnit CU(S);
2884 Dir = CU.getDirectory();
2885 Fn = CU.getFilename();
2886 } else if (Scope.isSubprogram()) {
2887 DISubprogram SP(S);
2888 Dir = SP.getDirectory();
2889 Fn = SP.getFilename();
2890 } else if (Scope.isLexicalBlock()) {
2891 DILexicalBlock DB(S);
2892 Dir = DB.getDirectory();
2893 Fn = DB.getFilename();
2894 } else
2895 assert(0 && "Unexpected scope info");
2896
2897 Src = GetOrCreateSourceID(Dir, Fn);
2898 }
2899
Stuart Hastingsafe54f12010-06-11 20:08:44 +00002900#if 0
2901 if (!Lines.empty()) {
2902 SrcLineInfo lastSrcLineInfo = Lines.back();
2903 // Emitting sequential line records with the same line number (but
2904 // different addresses) seems to confuse GDB. Avoid this.
2905 if (lastSrcLineInfo.getLine() == Line)
2906 return NULL;
2907 }
2908#endif
2909
Chris Lattner6e52e9d2010-03-14 08:36:50 +00002910 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattnerb4666f42010-03-14 08:15:55 +00002911 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendling2b128d72009-05-20 23:19:06 +00002912
Chris Lattnerba35a672010-03-09 04:54:43 +00002913 Asm->OutStreamer.EmitLabel(Label);
2914 return Label;
Bill Wendling2b128d72009-05-20 23:19:06 +00002915}
2916
Bill Wendling806535f2009-05-20 23:22:40 +00002917//===----------------------------------------------------------------------===//
2918// Emit Methods
2919//===----------------------------------------------------------------------===//
2920
Devang Patel930143b2009-11-21 02:48:08 +00002921/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00002922///
Jim Grosbach00e9c612009-11-22 19:20:36 +00002923unsigned
2924DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00002925 // Get the children.
2926 const std::vector<DIE *> &Children = Die->getChildren();
2927
2928 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00002929 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00002930 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00002931
2932 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00002933 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00002934
2935 // Get the abbreviation for this DIE.
2936 unsigned AbbrevNumber = Die->getAbbrevNumber();
2937 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2938
2939 // Set DIE offset
2940 Die->setOffset(Offset);
2941
2942 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00002943 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002944
2945 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2946 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2947
2948 // Size the DIE attribute values.
2949 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2950 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00002951 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00002952
2953 // Size the DIE children if any.
2954 if (!Children.empty()) {
2955 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2956 "Children flag not set");
2957
2958 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00002959 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00002960
2961 // End of children marker.
2962 Offset += sizeof(int8_t);
2963 }
2964
2965 Die->setSize(Offset - Die->getOffset());
2966 return Offset;
2967}
2968
Devang Patel930143b2009-11-21 02:48:08 +00002969/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00002970///
Devang Patel930143b2009-11-21 02:48:08 +00002971void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002972 unsigned PrevOffset = 0;
2973 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2974 E = CUMap.end(); I != E; ++I) {
2975 // Compute size of compile unit header.
2976 static unsigned Offset = PrevOffset +
2977 sizeof(int32_t) + // Length of Compilation Unit Info
2978 sizeof(int16_t) + // DWARF version number
2979 sizeof(int32_t) + // Offset Into Abbrev. Section
2980 sizeof(int8_t); // Pointer Size (in bytes)
2981 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
2982 PrevOffset = Offset;
2983 }
Bill Wendling480ff322009-05-20 23:21:38 +00002984}
2985
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002986/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2987/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00002988static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002989 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00002990 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002991 if (!SymbolStem) return 0;
2992
Chris Lattner6629ca92010-04-04 22:59:04 +00002993 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2994 Asm->OutStreamer.EmitLabel(TmpSym);
2995 return TmpSym;
2996}
2997
2998/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2999/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00003000void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003001 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00003002
Bill Wendling480ff322009-05-20 23:21:38 +00003003 // Dwarf sections base addresses.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003004 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6629ca92010-04-04 22:59:04 +00003005 DwarfFrameSectionSym =
3006 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3007 }
Bill Wendling480ff322009-05-20 23:21:38 +00003008
Chris Lattner6629ca92010-04-04 22:59:04 +00003009 DwarfInfoSectionSym =
3010 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
3011 DwarfAbbrevSectionSym =
3012 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003013 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner6629ca92010-04-04 22:59:04 +00003014
3015 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003016 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003017
Devang Patel24bc1b52010-06-29 20:17:53 +00003018 DwarfDebugLineSectionSym =
3019 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003020 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3021 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3022 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner6629ca92010-04-04 22:59:04 +00003023 DwarfStrSectionSym =
3024 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00003025 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3026 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00003027
Devang Patel9fc11702010-05-25 23:40:22 +00003028 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3029 "section_debug_loc");
3030
Chris Lattner6629ca92010-04-04 22:59:04 +00003031 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00003032 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003033}
3034
Devang Patel930143b2009-11-21 02:48:08 +00003035/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00003036///
Devang Patel930143b2009-11-21 02:48:08 +00003037void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00003038 // Get the abbreviation for this DIE.
3039 unsigned AbbrevNumber = Die->getAbbrevNumber();
3040 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3041
Bill Wendling480ff322009-05-20 23:21:38 +00003042 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00003043 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00003044 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3045 Twine::utohexstr(Die->getOffset()) + ":0x" +
3046 Twine::utohexstr(Die->getSize()) + " " +
3047 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00003048 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00003049
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00003050 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00003051 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3052
3053 // Emit the DIE attribute values.
3054 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3055 unsigned Attr = AbbrevData[i].getAttribute();
3056 unsigned Form = AbbrevData[i].getForm();
3057 assert(Form && "Too many attributes for DIE (check abbreviation)");
3058
Chris Lattner7bde8c02010-04-04 18:52:31 +00003059 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00003060 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
3061
Bill Wendling480ff322009-05-20 23:21:38 +00003062 switch (Attr) {
3063 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00003064 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003065 break;
3066 case dwarf::DW_AT_abstract_origin: {
3067 DIEEntry *E = cast<DIEEntry>(Values[i]);
3068 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003069 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00003070 Asm->EmitInt32(Addr);
3071 break;
3072 }
Devang Patel12563b32010-04-16 23:33:45 +00003073 case dwarf::DW_AT_ranges: {
3074 // DW_AT_range Value encodes offset in debug_range section.
3075 DIEInteger *V = cast<DIEInteger>(Values[i]);
3076 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3077 V->getValue(),
3078 DwarfDebugRangeSectionSym,
3079 4);
3080 break;
3081 }
Devang Patel24bc1b52010-06-29 20:17:53 +00003082 case dwarf::DW_AT_stmt_list: {
3083 Asm->EmitLabelDifference(CurrentLineSectionSym,
3084 DwarfDebugLineSectionSym, 4);
3085 break;
3086 }
Devang Patel9fc11702010-05-25 23:40:22 +00003087 case dwarf::DW_AT_location: {
3088 if (UseDotDebugLocEntry.count(Die) != 0) {
3089 DIELabel *L = cast<DIELabel>(Values[i]);
3090 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3091 } else
3092 Values[i]->EmitValue(Asm, Form);
3093 break;
3094 }
Bill Wendling480ff322009-05-20 23:21:38 +00003095 default:
3096 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003097 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00003098 break;
3099 }
Bill Wendling480ff322009-05-20 23:21:38 +00003100 }
3101
3102 // Emit the DIE children if any.
3103 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3104 const std::vector<DIE *> &Children = Die->getChildren();
3105
3106 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00003107 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00003108
Chris Lattner7bde8c02010-04-04 18:52:31 +00003109 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00003110 Asm->OutStreamer.AddComment("End Of Children Mark");
3111 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003112 }
3113}
3114
Devang Patel9ccfb642009-12-09 18:24:21 +00003115/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00003116///
Devang Patel9ccfb642009-12-09 18:24:21 +00003117void DwarfDebug::emitDebugInfo() {
3118 // Start debug info section.
3119 Asm->OutStreamer.SwitchSection(
3120 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00003121 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3122 E = CUMap.end(); I != E; ++I) {
3123 CompileUnit *TheCU = I->second;
3124 DIE *Die = TheCU->getCUDie();
3125
3126 // Emit the compile units header.
3127 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3128 TheCU->getID()));
3129
3130 // Emit size of content not including length itself
3131 unsigned ContentSize = Die->getSize() +
3132 sizeof(int16_t) + // DWARF version number
3133 sizeof(int32_t) + // Offset Into Abbrev. Section
3134 sizeof(int8_t) + // Pointer Size (in bytes)
3135 sizeof(int32_t); // FIXME - extra pad for gdb bug.
3136
3137 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3138 Asm->EmitInt32(ContentSize);
3139 Asm->OutStreamer.AddComment("DWARF version number");
3140 Asm->EmitInt16(dwarf::DWARF_VERSION);
3141 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3142 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3143 DwarfAbbrevSectionSym);
3144 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3145 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
3146
3147 emitDIE(Die);
3148 // FIXME - extra padding for gdb bug.
3149 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3150 Asm->EmitInt8(0);
3151 Asm->EmitInt8(0);
3152 Asm->EmitInt8(0);
3153 Asm->EmitInt8(0);
3154 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3155 }
Bill Wendling480ff322009-05-20 23:21:38 +00003156}
3157
Devang Patel930143b2009-11-21 02:48:08 +00003158/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00003159///
Devang Patel930143b2009-11-21 02:48:08 +00003160void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00003161 // Check to see if it is worth the effort.
3162 if (!Abbreviations.empty()) {
3163 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003164 Asm->OutStreamer.SwitchSection(
3165 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003166
Chris Lattnera179b522010-04-04 19:25:43 +00003167 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003168
3169 // For each abbrevation.
3170 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3171 // Get abbreviation data
3172 const DIEAbbrev *Abbrev = Abbreviations[i];
3173
3174 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00003175 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00003176
3177 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003178 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00003179 }
3180
3181 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00003182 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00003183
Chris Lattnera179b522010-04-04 19:25:43 +00003184 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003185 }
3186}
3187
Devang Patel930143b2009-11-21 02:48:08 +00003188/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00003189/// the line matrix.
3190///
Devang Patel930143b2009-11-21 02:48:08 +00003191void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00003192 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00003193 Asm->OutStreamer.AddComment("Extended Op");
3194 Asm->EmitInt8(0);
3195
3196 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003197 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00003198 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3199 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3200
3201 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00003202
Chris Lattnera179b522010-04-04 19:25:43 +00003203 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003204 Asm->getTargetData().getPointerSize(),
3205 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003206
3207 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00003208 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3209 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003210 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00003211 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003212}
3213
Devang Patel930143b2009-11-21 02:48:08 +00003214/// emitDebugLines - Emit source line information.
Bill Wendling480ff322009-05-20 23:21:38 +00003215///
Devang Patel930143b2009-11-21 02:48:08 +00003216void DwarfDebug::emitDebugLines() {
Bill Wendling480ff322009-05-20 23:21:38 +00003217 // If the target is using .loc/.file, the assembler will be emitting the
3218 // .debug_line table automatically.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003219 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling480ff322009-05-20 23:21:38 +00003220 return;
3221
3222 // Minimum line delta, thus ranging from -10..(255-10).
3223 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
3224 // Maximum line delta, thus ranging from -10..(255-10).
3225 const int MaxLineDelta = 255 + MinLineDelta;
3226
3227 // Start the dwarf line section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003228 Asm->OutStreamer.SwitchSection(
3229 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003230
3231 // Construct the section header.
Devang Patel24bc1b52010-06-29 20:17:53 +00003232 CurrentLineSectionSym = Asm->GetTempSymbol("section_line_begin");
3233 Asm->OutStreamer.EmitLabel(CurrentLineSectionSym);
Chris Lattner566cae92010-03-09 23:52:58 +00003234 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003235 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
3236 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnera179b522010-04-04 19:25:43 +00003237 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003238
Chris Lattner566cae92010-03-09 23:52:58 +00003239 Asm->OutStreamer.AddComment("DWARF version number");
3240 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling480ff322009-05-20 23:21:38 +00003241
Chris Lattner566cae92010-03-09 23:52:58 +00003242 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003243 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
3244 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnera179b522010-04-04 19:25:43 +00003245 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003246
Chris Lattner566cae92010-03-09 23:52:58 +00003247 Asm->OutStreamer.AddComment("Minimum Instruction Length");
3248 Asm->EmitInt8(1);
3249 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
3250 Asm->EmitInt8(1);
3251 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
3252 Asm->EmitInt8(MinLineDelta);
3253 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3254 Asm->EmitInt8(MaxLineDelta);
3255 Asm->OutStreamer.AddComment("Special Opcode Base");
3256 Asm->EmitInt8(-MinLineDelta);
Bill Wendling480ff322009-05-20 23:21:38 +00003257
3258 // Line number standard opcode encodings argument count
Chris Lattner566cae92010-03-09 23:52:58 +00003259 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3260 Asm->EmitInt8(0);
3261 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3262 Asm->EmitInt8(1);
3263 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3264 Asm->EmitInt8(1);
3265 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3266 Asm->EmitInt8(1);
3267 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3268 Asm->EmitInt8(1);
3269 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3270 Asm->EmitInt8(0);
3271 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3272 Asm->EmitInt8(0);
3273 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3274 Asm->EmitInt8(0);
3275 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3276 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003277
3278 // Emit directories.
3279 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003280 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner7bde8c02010-04-04 18:52:31 +00003281 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003282 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling480ff322009-05-20 23:21:38 +00003283 }
3284
Chris Lattner566cae92010-03-09 23:52:58 +00003285 Asm->OutStreamer.AddComment("End of directories");
3286 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003287
3288 // Emit files.
3289 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3290 // Remember source id starts at 1.
3291 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003292 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner7bde8c02010-04-04 18:52:31 +00003293 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003294 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
3295
Chris Lattner9efd1182010-04-04 19:09:29 +00003296 Asm->EmitULEB128(Id.first, "Directory #");
3297 Asm->EmitULEB128(0, "Mod date");
3298 Asm->EmitULEB128(0, "File size");
Bill Wendling480ff322009-05-20 23:21:38 +00003299 }
3300
Chris Lattner566cae92010-03-09 23:52:58 +00003301 Asm->OutStreamer.AddComment("End of files");
3302 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003303
Chris Lattnera179b522010-04-04 19:25:43 +00003304 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003305
3306 // A sequence for each text section.
3307 unsigned SecSrcLinesSize = SectionSourceLines.size();
3308
3309 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3310 // Isolate current sections line info.
3311 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3312
Bill Wendling480ff322009-05-20 23:21:38 +00003313 // Dwarf assumes we start with first line of first source file.
3314 unsigned Source = 1;
3315 unsigned Line = 1;
3316
3317 // Construct rows of the address, source, line, column matrix.
3318 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3319 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattnerb4666f42010-03-14 08:15:55 +00003320 MCSymbol *Label = LineInfo.getLabel();
Chris Lattneree95d4c2010-03-14 02:20:58 +00003321 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling480ff322009-05-20 23:21:38 +00003322
Chris Lattner3d72a672010-03-09 23:38:23 +00003323 if (Asm->isVerbose()) {
Chris Lattner7e998b72010-03-10 01:04:13 +00003324 std::pair<unsigned, unsigned> SrcID =
Bill Wendling480ff322009-05-20 23:21:38 +00003325 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner7e998b72010-03-10 01:04:13 +00003326 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattnera26fbe42010-03-10 02:29:31 +00003327 "/" +
3328 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner7e998b72010-03-10 01:04:13 +00003329 ":" + Twine(LineInfo.getLine()));
Bill Wendling480ff322009-05-20 23:21:38 +00003330 }
3331
3332 // Define the line address.
Chris Lattner566cae92010-03-09 23:52:58 +00003333 Asm->OutStreamer.AddComment("Extended Op");
3334 Asm->EmitInt8(0);
3335 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003336 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00003337
3338 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3339 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3340
3341 Asm->OutStreamer.AddComment("Location label");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003342 Asm->OutStreamer.EmitSymbolValue(Label,
3343 Asm->getTargetData().getPointerSize(),
Chris Lattneree95d4c2010-03-14 02:20:58 +00003344 0/*AddrSpace*/);
Chris Lattnerb245dfb2010-03-10 01:17:49 +00003345
Bill Wendling480ff322009-05-20 23:21:38 +00003346 // If change of source, then switch to the new source.
3347 if (Source != LineInfo.getSourceID()) {
3348 Source = LineInfo.getSourceID();
Chris Lattner566cae92010-03-09 23:52:58 +00003349 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3350 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner9efd1182010-04-04 19:09:29 +00003351 Asm->EmitULEB128(Source, "New Source");
Bill Wendling480ff322009-05-20 23:21:38 +00003352 }
3353
3354 // If change of line.
3355 if (Line != LineInfo.getLine()) {
3356 // Determine offset.
3357 int Offset = LineInfo.getLine() - Line;
3358 int Delta = Offset - MinLineDelta;
3359
3360 // Update line.
3361 Line = LineInfo.getLine();
3362
3363 // If delta is small enough and in range...
3364 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3365 // ... then use fast opcode.
Chris Lattner566cae92010-03-09 23:52:58 +00003366 Asm->OutStreamer.AddComment("Line Delta");
3367 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling480ff322009-05-20 23:21:38 +00003368 } else {
3369 // ... otherwise use long hand.
Chris Lattner566cae92010-03-09 23:52:58 +00003370 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling480ff322009-05-20 23:21:38 +00003371 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner9efd1182010-04-04 19:09:29 +00003372 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner566cae92010-03-09 23:52:58 +00003373 Asm->OutStreamer.AddComment("DW_LNS_copy");
3374 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling480ff322009-05-20 23:21:38 +00003375 }
3376 } else {
3377 // Copy the previous row (different address or source)
Chris Lattner566cae92010-03-09 23:52:58 +00003378 Asm->OutStreamer.AddComment("DW_LNS_copy");
3379 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling480ff322009-05-20 23:21:38 +00003380 }
3381 }
3382
Devang Patel930143b2009-11-21 02:48:08 +00003383 emitEndOfLineMatrix(j + 1);
Bill Wendling480ff322009-05-20 23:21:38 +00003384 }
3385
3386 if (SecSrcLinesSize == 0)
3387 // Because we're emitting a debug_line section, we still need a line
3388 // table. The linker and friends expect it to exist. If there's nothing to
3389 // put into it, emit an empty table.
Devang Patel930143b2009-11-21 02:48:08 +00003390 emitEndOfLineMatrix(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003391
Chris Lattnera179b522010-04-04 19:25:43 +00003392 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003393}
3394
Devang Patel930143b2009-11-21 02:48:08 +00003395/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling480ff322009-05-20 23:21:38 +00003396///
Devang Patel930143b2009-11-21 02:48:08 +00003397void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003398 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003399 return;
3400
Chris Lattner3a383cb2010-04-05 00:13:49 +00003401 int stackGrowth = Asm->getTargetData().getPointerSize();
3402 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3403 TargetFrameInfo::StackGrowsDown)
3404 stackGrowth *= -1;
Bill Wendling480ff322009-05-20 23:21:38 +00003405
3406 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003407 Asm->OutStreamer.SwitchSection(
3408 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003409
Chris Lattnera179b522010-04-04 19:25:43 +00003410 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner566cae92010-03-09 23:52:58 +00003411 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003412 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3413 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003414
Chris Lattnera179b522010-04-04 19:25:43 +00003415 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner566cae92010-03-09 23:52:58 +00003416 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling480ff322009-05-20 23:21:38 +00003417 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner566cae92010-03-09 23:52:58 +00003418 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling480ff322009-05-20 23:21:38 +00003419 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner566cae92010-03-09 23:52:58 +00003420 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003421 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner9efd1182010-04-04 19:09:29 +00003422 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3423 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner566cae92010-03-09 23:52:58 +00003424 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003425 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling480ff322009-05-20 23:21:38 +00003426 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling480ff322009-05-20 23:21:38 +00003427
3428 std::vector<MachineMove> Moves;
3429 RI->getInitialFrameState(Moves);
3430
Chris Lattneraabc6042010-04-04 23:41:46 +00003431 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003432
Chris Lattner9e06e532010-04-28 01:05:45 +00003433 Asm->EmitAlignment(2);
Chris Lattnera179b522010-04-04 19:25:43 +00003434 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003435}
3436
Devang Patel930143b2009-11-21 02:48:08 +00003437/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling480ff322009-05-20 23:21:38 +00003438/// section.
Chris Lattner2c3f4782010-03-13 07:26:18 +00003439void DwarfDebug::
3440emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003441 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003442 return;
3443
3444 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003445 Asm->OutStreamer.SwitchSection(
3446 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003447
Chris Lattner566cae92010-03-09 23:52:58 +00003448 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner2c3f4782010-03-13 07:26:18 +00003449 MCSymbol *DebugFrameBegin =
Chris Lattnera179b522010-04-04 19:25:43 +00003450 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003451 MCSymbol *DebugFrameEnd =
Chris Lattnera179b522010-04-04 19:25:43 +00003452 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerf1429f12010-04-04 19:58:12 +00003453 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003454
Chris Lattner2c3f4782010-03-13 07:26:18 +00003455 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling480ff322009-05-20 23:21:38 +00003456
Chris Lattner566cae92010-03-09 23:52:58 +00003457 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattner70a4fce2010-04-04 23:25:33 +00003458 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3459 DwarfFrameSectionSym);
Bill Wendling480ff322009-05-20 23:21:38 +00003460
Chris Lattner566cae92010-03-09 23:52:58 +00003461 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnera179b522010-04-04 19:25:43 +00003462 MCSymbol *FuncBeginSym =
3463 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner8811e122010-03-13 07:40:56 +00003464 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattner3a383cb2010-04-05 00:13:49 +00003465 Asm->getTargetData().getPointerSize(),
3466 0/*AddrSpace*/);
Chris Lattnerb245dfb2010-03-10 01:17:49 +00003467
3468
Chris Lattner566cae92010-03-09 23:52:58 +00003469 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003470 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003471 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003472
Chris Lattneraabc6042010-04-04 23:41:46 +00003473 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003474
Chris Lattner9e06e532010-04-28 01:05:45 +00003475 Asm->EmitAlignment(2);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003476 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling480ff322009-05-20 23:21:38 +00003477}
3478
Devang Patel9ccfb642009-12-09 18:24:21 +00003479/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3480///
3481void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003482 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3483 E = CUMap.end(); I != E; ++I) {
3484 CompileUnit *TheCU = I->second;
3485 // Start the dwarf pubnames section.
3486 Asm->OutStreamer.SwitchSection(
3487 Asm->getObjFileLowering().getDwarfPubNamesSection());
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003488
Devang Patel1a0df9a2010-05-10 22:49:55 +00003489 Asm->OutStreamer.AddComment("Length of Public Names Info");
3490 Asm->EmitLabelDifference(
3491 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3492 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
3493
3494 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3495 TheCU->getID()));
3496
3497 Asm->OutStreamer.AddComment("DWARF Version");
3498 Asm->EmitInt16(dwarf::DWARF_VERSION);
3499
3500 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3501 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3502 DwarfInfoSectionSym);
3503
3504 Asm->OutStreamer.AddComment("Compilation Unit Length");
3505 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3506 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3507 4);
3508
3509 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3510 for (StringMap<DIE*>::const_iterator
3511 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3512 const char *Name = GI->getKeyData();
3513 DIE *Entity = GI->second;
3514
3515 Asm->OutStreamer.AddComment("DIE offset");
3516 Asm->EmitInt32(Entity->getOffset());
3517
3518 if (Asm->isVerbose())
3519 Asm->OutStreamer.AddComment("External Name");
3520 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3521 }
3522
3523 Asm->OutStreamer.AddComment("End Mark");
3524 Asm->EmitInt32(0);
3525 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3526 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00003527 }
Bill Wendling480ff322009-05-20 23:21:38 +00003528}
3529
Devang Patel04d2f2d2009-11-24 01:14:22 +00003530void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003531 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3532 E = CUMap.end(); I != E; ++I) {
3533 CompileUnit *TheCU = I->second;
3534 // Start the dwarf pubnames section.
3535 Asm->OutStreamer.SwitchSection(
3536 Asm->getObjFileLowering().getDwarfPubTypesSection());
3537 Asm->OutStreamer.AddComment("Length of Public Types Info");
3538 Asm->EmitLabelDifference(
3539 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3540 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003541
Devang Patel1a0df9a2010-05-10 22:49:55 +00003542 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3543 TheCU->getID()));
3544
3545 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3546 Asm->EmitInt16(dwarf::DWARF_VERSION);
3547
3548 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3549 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3550 DwarfInfoSectionSym);
3551
3552 Asm->OutStreamer.AddComment("Compilation Unit Length");
3553 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3554 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3555 4);
3556
3557 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3558 for (StringMap<DIE*>::const_iterator
3559 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3560 const char *Name = GI->getKeyData();
3561 DIE * Entity = GI->second;
3562
3563 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3564 Asm->EmitInt32(Entity->getOffset());
3565
3566 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3567 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3568 }
3569
3570 Asm->OutStreamer.AddComment("End Mark");
3571 Asm->EmitInt32(0);
3572 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3573 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00003574 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00003575}
3576
Devang Patel930143b2009-11-21 02:48:08 +00003577/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00003578///
Devang Patel930143b2009-11-21 02:48:08 +00003579void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00003580 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00003581 if (StringPool.empty()) return;
3582
3583 // Start the dwarf str section.
3584 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003585 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003586
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003587 // Get all of the string pool entries and put them in an array by their ID so
3588 // we can sort them.
3589 SmallVector<std::pair<unsigned,
3590 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3591
3592 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3593 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3594 Entries.push_back(std::make_pair(I->second.second, &*I));
3595
3596 array_pod_sort(Entries.begin(), Entries.end());
3597
3598 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00003599 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003600 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner3d72a672010-03-09 23:38:23 +00003601
3602 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003603 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003604 }
3605}
3606
Devang Patel930143b2009-11-21 02:48:08 +00003607/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00003608///
Devang Patel930143b2009-11-21 02:48:08 +00003609void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003610 if (DotDebugLocEntries.empty())
3611 return;
3612
Bill Wendling480ff322009-05-20 23:21:38 +00003613 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003614 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00003615 Asm->getObjFileLowering().getDwarfLocSection());
3616 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003617 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3618 unsigned index = 1;
Devang Patel30265c42010-07-07 20:12:52 +00003619 for (SmallVector<DotDebugLocEntry, 4>::iterator
3620 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
3621 I != E; ++I, ++index) {
Devang Patel9fc11702010-05-25 23:40:22 +00003622 DotDebugLocEntry Entry = *I;
Devang Patel9fc11702010-05-25 23:40:22 +00003623 if (Entry.isEmpty()) {
3624 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3625 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003626 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00003627 } else {
3628 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3629 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3630 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3631 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
3632 if (Reg < 32) {
3633 Asm->OutStreamer.AddComment("Loc expr size");
3634 Asm->EmitInt16(1);
3635 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3636 } else {
3637 Asm->OutStreamer.AddComment("Loc expr size");
3638 Asm->EmitInt16(1+MCAsmInfo::getULEB128Size(Reg));
3639 Asm->EmitInt8(dwarf::DW_OP_regx);
3640 Asm->EmitULEB128(Reg);
3641 }
3642 }
3643 }
Bill Wendling480ff322009-05-20 23:21:38 +00003644}
3645
3646/// EmitDebugARanges - Emit visible names into a debug aranges section.
3647///
3648void DwarfDebug::EmitDebugARanges() {
3649 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003650 Asm->OutStreamer.SwitchSection(
3651 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003652}
3653
Devang Patel930143b2009-11-21 02:48:08 +00003654/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00003655///
Devang Patel930143b2009-11-21 02:48:08 +00003656void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00003657 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003658 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00003659 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00003660 unsigned char Size = Asm->getTargetData().getPointerSize();
3661 for (SmallVector<const MCSymbol *, 8>::iterator
3662 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3663 I != E; ++I) {
3664 if (*I)
3665 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00003666 else
Devang Patel6c74a872010-04-27 19:46:33 +00003667 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00003668 }
Bill Wendling480ff322009-05-20 23:21:38 +00003669}
3670
Devang Patel930143b2009-11-21 02:48:08 +00003671/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00003672///
Devang Patel930143b2009-11-21 02:48:08 +00003673void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00003674 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00003675 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00003676 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003677 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003678 }
3679}
3680
Devang Patel930143b2009-11-21 02:48:08 +00003681/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00003682/// Section Header:
3683/// 1. length of section
3684/// 2. Dwarf version number
3685/// 3. address size.
3686///
3687/// Entries (one "entry" for each function that was inlined):
3688///
3689/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3690/// otherwise offset into __debug_str for regular function name.
3691/// 2. offset into __debug_str section for regular function name.
3692/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3693/// instances for the function.
3694///
3695/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3696/// inlined instance; the die_offset points to the inlined_subroutine die in the
3697/// __debug_info section, and the low_pc is the starting address for the
3698/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00003699void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003700 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003701 return;
3702
Devang Patel1a0df9a2010-05-10 22:49:55 +00003703 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00003704 return;
3705
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003706 Asm->OutStreamer.SwitchSection(
3707 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003708
Chris Lattner566cae92010-03-09 23:52:58 +00003709 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003710 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3711 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003712
Chris Lattnera179b522010-04-04 19:25:43 +00003713 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003714
Chris Lattner566cae92010-03-09 23:52:58 +00003715 Asm->OutStreamer.AddComment("Dwarf Version");
3716 Asm->EmitInt16(dwarf::DWARF_VERSION);
3717 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003718 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003719
Devang Patel32cc43c2010-05-07 20:54:48 +00003720 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003721 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00003722
Devang Patel32cc43c2010-05-07 20:54:48 +00003723 const MDNode *Node = *I;
3724 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00003725 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003726 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00003727 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00003728 StringRef LName = SP.getLinkageName();
3729 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00003730
Chris Lattner566cae92010-03-09 23:52:58 +00003731 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003732 if (LName.empty()) {
3733 Asm->OutStreamer.EmitBytes(Name, 0);
3734 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3735 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00003736 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3737 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003738
Chris Lattner566cae92010-03-09 23:52:58 +00003739 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00003740 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00003741 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00003742
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003743 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00003744 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00003745 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00003746 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003747
Chris Lattner7bde8c02010-04-04 18:52:31 +00003748 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003749 Asm->OutStreamer.EmitSymbolValue(LI->first,
3750 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00003751 }
3752 }
3753
Chris Lattnera179b522010-04-04 19:25:43 +00003754 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003755}