blob: 5a06ded351cb62d939290febefc81ec48da4581a [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);
Chris Lattner3a383cb2010-04-05 00:13:49 +00001392 assert(SPDie && "Unable to find subprogram DIE!");
1393 DISubprogram SP(SPNode);
Chris Lattner2c3f4782010-03-13 07:26:18 +00001394
Chris Lattner3a383cb2010-04-05 00:13:49 +00001395 // There is not any need to generate specification DIE for a function
1396 // defined at compile unit level. If a function is defined inside another
1397 // function then gdb prefers the definition at top level and but does not
1398 // expect specification DIE in parent function. So avoid creating
1399 // specification DIE for a function defined inside a function.
1400 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel019922d2010-04-06 23:53:48 +00001401 !SP.getContext().isFile() &&
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001402 !isSubprogramContext(SP.getContext())) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00001403 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1404
1405 // Add arguments.
1406 DICompositeType SPTy = SP.getType();
1407 DIArray Args = SPTy.getTypeArray();
1408 unsigned SPTag = SPTy.getTag();
1409 if (SPTag == dwarf::DW_TAG_subroutine_type)
1410 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1411 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001412 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattner3a383cb2010-04-05 00:13:49 +00001413 addType(Arg, ATy);
1414 if (ATy.isArtificial())
1415 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1416 SPDie->addChild(Arg);
1417 }
1418 DIE *SPDeclDie = SPDie;
1419 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1420 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1421 SPDeclDie);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001422 SPCU->addDie(SPDie);
Chris Lattner3a383cb2010-04-05 00:13:49 +00001423 }
1424
1425 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1426 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1427 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1428 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1429 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1430 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1431 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +00001432
Chris Lattner3a383cb2010-04-05 00:13:49 +00001433 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001434}
1435
Jim Grosbach042483e2009-11-21 23:12:12 +00001436/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +00001437/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1438DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001439
1440 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1441 if (Scope->isAbstractScope())
1442 return ScopeDIE;
1443
1444 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1445 if (Ranges.empty())
1446 return 0;
1447
1448 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1449 if (Ranges.size() > 1) {
1450 // .debug_range section has not been laid out yet. Emit offset in
1451 // .debug_range as a uint, size 4, for now. emitDIE will handle
1452 // DW_AT_ranges appropriately.
1453 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1454 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1455 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1456 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +00001457 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1458 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +00001459 }
1460 DebugRangeSymbols.push_back(NULL);
1461 DebugRangeSymbols.push_back(NULL);
1462 return ScopeDIE;
1463 }
1464
Devang Patel9fc11702010-05-25 23:40:22 +00001465 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1466 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001467
Devang Patel9fc11702010-05-25 23:40:22 +00001468 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001469
Chris Lattnere13c3722010-03-09 01:58:53 +00001470 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1471 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001472
Devang Patel6c74a872010-04-27 19:46:33 +00001473 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1474 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001475
1476 return ScopeDIE;
1477}
1478
Devang Patel930143b2009-11-21 02:48:08 +00001479/// constructInlinedScopeDIE - This scope represents inlined body of
1480/// a function. Construct DIE to represent this concrete inlined copy
1481/// of the function.
1482DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001483
1484 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1485 assert (Ranges.empty() == false
1486 && "DbgScope does not have instruction markers!");
1487
1488 // FIXME : .debug_inlined section specification does not clearly state how
1489 // to emit inlined scope that is split into multiple instruction ranges.
1490 // For now, use first instruction range and emit low_pc/high_pc pair and
1491 // corresponding .debug_inlined section entry for this pair.
1492 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +00001493 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1494 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001495
Devang Patel9fc11702010-05-25 23:40:22 +00001496 if (StartLabel == FunctionBeginSym || EndLabel == 0) {
Devang Patel6c74a872010-04-27 19:46:33 +00001497 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1498 return 0;
1499 }
Chris Lattnere13c3722010-03-09 01:58:53 +00001500 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001501 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +00001502 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001503 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +00001504
Devang Patel3b548aa2010-03-08 20:52:55 +00001505 if (!Scope->getScopeNode())
Devang Patelbc97f6b2010-03-08 19:20:38 +00001506 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001507 DIScope DS(Scope->getScopeNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001508 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1509
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001510 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001511 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1512 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001513 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel930143b2009-11-21 02:48:08 +00001514 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001515 dwarf::DW_FORM_ref4, OriginDIE);
1516
Chris Lattner085b6522010-03-09 00:31:02 +00001517 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnere13c3722010-03-09 01:58:53 +00001518 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001519
1520 InlinedSubprogramDIEs.insert(OriginDIE);
1521
1522 // Track the start label for this inlined function.
Devang Patel32cc43c2010-05-07 20:54:48 +00001523 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001524 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001525
1526 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001527 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +00001528 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001529 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001530 } else
Chris Lattner085b6522010-03-09 00:31:02 +00001531 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001532
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001533 DILocation DL(Scope->getInlinedAt());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001534 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel930143b2009-11-21 02:48:08 +00001535 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001536
1537 return ScopeDIE;
1538}
1539
Devang Patel930143b2009-11-21 02:48:08 +00001540
1541/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel9ccfb642009-12-09 18:24:21 +00001542DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001543 // Get the descriptor.
1544 const DIVariable &VD = DV->getVariable();
Devang Patel2d9caf92009-11-25 17:36:49 +00001545 StringRef Name = VD.getName();
1546 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +00001547 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001548
1549 // Translate tag to proper Dwarf tag. The result variable is dropped for
1550 // now.
1551 unsigned Tag;
1552 switch (VD.getTag()) {
1553 case dwarf::DW_TAG_return_variable:
1554 return NULL;
1555 case dwarf::DW_TAG_arg_variable:
1556 Tag = dwarf::DW_TAG_formal_parameter;
1557 break;
1558 case dwarf::DW_TAG_auto_variable: // fall thru
1559 default:
1560 Tag = dwarf::DW_TAG_variable;
1561 break;
1562 }
1563
1564 // Define variable debug information entry.
1565 DIE *VariableDie = new DIE(Tag);
1566
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001567 DIE *AbsDIE = NULL;
Devang Patele1c53f22010-05-20 16:36:41 +00001568 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1569 V2AVI = VarToAbstractVarMap.find(DV);
1570 if (V2AVI != VarToAbstractVarMap.end())
1571 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +00001572
Devang Patele1c53f22010-05-20 16:36:41 +00001573 if (AbsDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001574 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001575 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001576 else {
Devang Patel930143b2009-11-21 02:48:08 +00001577 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1578 addSourceLine(VariableDie, &VD);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001579
1580 // Add variable type.
Jim Grosbach042483e2009-11-21 23:12:12 +00001581 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001582 // addresses instead.
1583 if (VD.isBlockByrefVariable())
Devang Patel9ccfb642009-12-09 18:24:21 +00001584 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001585 else
Devang Patel9ccfb642009-12-09 18:24:21 +00001586 addType(VariableDie, VD.getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001587 }
1588
Devang Patel6efc8e52010-02-06 01:02:37 +00001589 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1590 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel9fc11702010-05-25 23:40:22 +00001591
1592 if (Scope->isAbstractScope()) {
1593 DV->setDIE(VariableDie);
1594 return VariableDie;
1595 }
1596
1597 // Add variable address.
1598
1599 unsigned Offset = DV->getDotDebugLocOffset();
1600 if (Offset != ~0U) {
1601 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1602 Asm->GetTempSymbol("debug_loc", Offset));
1603 DV->setDIE(VariableDie);
1604 UseDotDebugLocEntry.insert(VariableDie);
1605 return VariableDie;
1606 }
1607
1608 // Check if variable is described by a DBG_VALUE instruction.
1609 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1610 DbgVariableToDbgInstMap.find(DV);
1611 if (DVI != DbgVariableToDbgInstMap.end()) {
1612 const MachineInstr *DVInsn = DVI->second;
1613 const MCSymbol *DVLabel = findVariableLabel(DV);
1614 bool updated = false;
1615 // FIXME : Handle getNumOperands != 3
1616 if (DVInsn->getNumOperands() == 3) {
1617 if (DVInsn->getOperand(0).isReg())
Devang Patel30265c42010-07-07 20:12:52 +00001618 updated =
1619 addRegisterAddress(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patel9fc11702010-05-25 23:40:22 +00001620 else if (DVInsn->getOperand(0).isImm())
1621 updated = addConstantValue(VariableDie, DVLabel, DVInsn->getOperand(0));
1622 else if (DVInsn->getOperand(0).isFPImm())
Devang Patel30265c42010-07-07 20:12:52 +00001623 updated =
1624 addConstantFPValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patel9fc11702010-05-25 23:40:22 +00001625 } else {
1626 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1627 if (Location.getReg()) {
1628 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1629 if (DVLabel)
1630 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1631 DVLabel);
1632 updated = true;
1633 }
1634 }
1635 if (!updated) {
1636 // If variableDie is not updated then DBG_VALUE instruction does not
1637 // have valid variable info.
1638 delete VariableDie;
1639 return NULL;
1640 }
1641 DV->setDIE(VariableDie);
1642 return VariableDie;
1643 }
1644
1645 // .. else use frame index, if available.
1646 MachineLocation Location;
1647 unsigned FrameReg;
1648 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1649 int FI = 0;
1650 if (findVariableFrameIndex(DV, &FI)) {
1651 int Offset = RI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1652 Location.set(FrameReg, Offset);
1653
1654 if (VD.hasComplexAddress())
1655 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1656 else if (VD.isBlockByrefVariable())
1657 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1658 else
1659 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1660 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001661 DV->setDIE(VariableDie);
1662 return VariableDie;
1663
1664}
Devang Patel930143b2009-11-21 02:48:08 +00001665
Devang Patel04d2f2d2009-11-24 01:14:22 +00001666void DwarfDebug::addPubTypes(DISubprogram SP) {
1667 DICompositeType SPTy = SP.getType();
1668 unsigned SPTag = SPTy.getTag();
1669 if (SPTag != dwarf::DW_TAG_subroutine_type)
1670 return;
1671
1672 DIArray Args = SPTy.getTypeArray();
Devang Patel04d2f2d2009-11-24 01:14:22 +00001673 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001674 DIType ATy(Args.getElement(i));
Devang Patel8d6a2b72010-05-07 21:45:47 +00001675 if (!ATy.Verify())
Devang Patel04d2f2d2009-11-24 01:14:22 +00001676 continue;
1677 DICompositeType CATy = getDICompositeType(ATy);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001678 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel12d150e2010-04-13 20:35:04 +00001679 && !CATy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001680 CompileUnit *TheCU = getCompileUnit(CATy);
1681 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1682 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001683 }
1684 }
1685}
1686
Devang Patel930143b2009-11-21 02:48:08 +00001687/// constructScopeDIE - Construct a DIE for this scope.
1688DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +00001689 if (!Scope || !Scope->getScopeNode())
1690 return NULL;
1691
1692 DIScope DS(Scope->getScopeNode());
1693 DIE *ScopeDIE = NULL;
1694 if (Scope->getInlinedAt())
1695 ScopeDIE = constructInlinedScopeDIE(Scope);
1696 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +00001697 ProcessedSPNodes.insert(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +00001698 if (Scope->isAbstractScope())
Devang Patel1a0df9a2010-05-10 22:49:55 +00001699 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +00001700 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001701 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +00001702 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001703 else
Devang Patel3b548aa2010-03-08 20:52:55 +00001704 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel23b2ae62010-03-29 22:59:58 +00001705 if (!ScopeDIE) return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001706
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001707 // Add variables to scope.
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001708 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001709 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel9ccfb642009-12-09 18:24:21 +00001710 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach042483e2009-11-21 23:12:12 +00001711 if (VariableDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001712 ScopeDIE->addChild(VariableDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001713 }
1714
1715 // Add nested scopes.
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001716 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001717 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1718 // Define the Scope debug information entry.
Devang Patel930143b2009-11-21 02:48:08 +00001719 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach042483e2009-11-21 23:12:12 +00001720 if (NestedDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001721 ScopeDIE->addChild(NestedDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001722 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001723
1724 if (DS.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001725 addPubTypes(DISubprogram(DS));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001726
1727 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001728}
1729
Bill Wendling2b128d72009-05-20 23:19:06 +00001730/// GetOrCreateSourceID - Look up the source id with the given directory and
1731/// source file names. If none currently exists, create a new id and insert it
1732/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1733/// maps as well.
Chris Lattner71696ef2010-03-31 06:06:37 +00001734unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendling2b128d72009-05-20 23:19:06 +00001735 unsigned DId;
1736 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1737 if (DI != DirectoryIdMap.end()) {
1738 DId = DI->getValue();
1739 } else {
1740 DId = DirectoryNames.size() + 1;
1741 DirectoryIdMap[DirName] = DId;
1742 DirectoryNames.push_back(DirName);
1743 }
1744
1745 unsigned FId;
1746 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1747 if (FI != SourceFileIdMap.end()) {
1748 FId = FI->getValue();
1749 } else {
1750 FId = SourceFileNames.size() + 1;
1751 SourceFileIdMap[FileName] = FId;
1752 SourceFileNames.push_back(FileName);
1753 }
1754
1755 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1756 SourceIdMap.find(std::make_pair(DId, FId));
1757 if (SI != SourceIdMap.end())
1758 return SI->second;
1759
1760 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1761 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1762 SourceIds.push_back(std::make_pair(DId, FId));
1763
1764 return SrcId;
1765}
1766
Devang Patel1f4690c2009-12-15 19:16:48 +00001767/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1768DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001769 CompileUnit *TheCU = getCompileUnit(NS);
1770 DIE *NDie = TheCU->getDIE(NS);
Devang Patel1f4690c2009-12-15 19:16:48 +00001771 if (NDie)
1772 return NDie;
1773 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001774 TheCU->insertDIE(NS, NDie);
Devang Patel1f4690c2009-12-15 19:16:48 +00001775 if (!NS.getName().empty())
1776 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1777 addSourceLine(NDie, &NS);
1778 addToContextOwner(NDie, NS.getContext());
1779 return NDie;
1780}
1781
Devang Patel1a0df9a2010-05-10 22:49:55 +00001782/// constructCompileUnit - Create new CompileUnit for the given
1783/// metadata node with tag DW_TAG_compile_unit.
Devang Patel32cc43c2010-05-07 20:54:48 +00001784void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001785 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +00001786 StringRef FN = DIUnit.getFilename();
1787 StringRef Dir = DIUnit.getDirectory();
Devang Patelb2969422009-09-29 18:40:58 +00001788 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendling2b128d72009-05-20 23:19:06 +00001789
1790 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel930143b2009-11-21 02:48:08 +00001791 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001792 DIUnit.getProducer());
Devang Patel930143b2009-11-21 02:48:08 +00001793 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendling2b128d72009-05-20 23:19:06 +00001794 DIUnit.getLanguage());
Devang Patel930143b2009-11-21 02:48:08 +00001795 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +00001796 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1797 // simplifies debug range entries.
Devang Patel1de21ec2010-06-28 22:22:47 +00001798 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +00001799 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel24bc1b52010-06-29 20:17:53 +00001800 // compile unit in debug_line section. This offset is calculated
1801 // during endMoudle().
1802 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +00001803
Devang Patel2d9caf92009-11-25 17:36:49 +00001804 if (!Dir.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001805 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +00001806 if (DIUnit.isOptimized())
Devang Patel930143b2009-11-21 02:48:08 +00001807 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +00001808
Devang Patel2d9caf92009-11-25 17:36:49 +00001809 StringRef Flags = DIUnit.getFlags();
1810 if (!Flags.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001811 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendling2b128d72009-05-20 23:19:06 +00001812
1813 unsigned RVer = DIUnit.getRunTimeVersion();
1814 if (RVer)
Devang Patel930143b2009-11-21 02:48:08 +00001815 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +00001816 dwarf::DW_FORM_data1, RVer);
1817
Devang Patel1a0df9a2010-05-10 22:49:55 +00001818 CompileUnit *NewCU = new CompileUnit(ID, Die);
1819 if (!FirstCU)
1820 FirstCU = NewCU;
1821 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendling2b128d72009-05-20 23:19:06 +00001822}
1823
Devang Patel1a0df9a2010-05-10 22:49:55 +00001824/// getCompielUnit - Get CompileUnit DIE.
1825CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1826 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1827 DIDescriptor D(N);
1828 const MDNode *CUNode = NULL;
1829 if (D.isCompileUnit())
1830 CUNode = N;
1831 else if (D.isSubprogram())
1832 CUNode = DISubprogram(N).getCompileUnit();
1833 else if (D.isType())
1834 CUNode = DIType(N).getCompileUnit();
1835 else if (D.isGlobalVariable())
1836 CUNode = DIGlobalVariable(N).getCompileUnit();
1837 else if (D.isVariable())
1838 CUNode = DIVariable(N).getCompileUnit();
1839 else if (D.isNameSpace())
1840 CUNode = DINameSpace(N).getCompileUnit();
1841 else if (D.isFile())
1842 CUNode = DIFile(N).getCompileUnit();
1843 else
1844 return FirstCU;
1845
1846 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1847 = CUMap.find(CUNode);
1848 if (I == CUMap.end())
1849 return FirstCU;
1850 return I->second;
1851}
1852
1853
1854/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00001855void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001856 DIGlobalVariable DI_GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001857
Devang Patelf5d53602009-09-04 23:59:07 +00001858 // If debug information is malformed then ignore it.
1859 if (DI_GV.Verify() == false)
1860 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001861
1862 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001863 CompileUnit *TheCU = getCompileUnit(N);
1864 if (TheCU->getDIE(DI_GV))
Devang Patel0751a282009-06-26 01:49:18 +00001865 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001866
Devang Patel9ccfb642009-12-09 18:24:21 +00001867 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel2eec32d2009-12-10 23:25:41 +00001868 if (!VariableDie)
1869 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001870
Bill Wendling2b128d72009-05-20 23:19:06 +00001871 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001872 TheCU->insertDIE(N, VariableDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001873
1874 // Add to context owner.
Devang Patel89880c82010-01-15 01:12:22 +00001875 DIDescriptor GVContext = DI_GV.getContext();
1876 // Do not create specification DIE if context is either compile unit
1877 // or a subprogram.
Chris Lattner71696ef2010-03-31 06:06:37 +00001878 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel019922d2010-04-06 23:53:48 +00001879 !GVContext.isFile() &&
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001880 !isSubprogramContext(GVContext)) {
Devang Patel1f4690c2009-12-15 19:16:48 +00001881 // Create specification DIE.
1882 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1883 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1884 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001885 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel1f4690c2009-12-15 19:16:48 +00001886 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner8dcf41e2010-03-08 22:31:46 +00001887 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner9e4cafe2010-03-12 21:09:07 +00001888 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel1f4690c2009-12-15 19:16:48 +00001889 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patelce25dd72010-02-09 01:58:33 +00001890 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001891 TheCU->addDie(VariableSpecDIE);
Devang Patel1f4690c2009-12-15 19:16:48 +00001892 } else {
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001893 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel1f4690c2009-12-15 19:16:48 +00001894 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner8dcf41e2010-03-08 22:31:46 +00001895 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner9e4cafe2010-03-12 21:09:07 +00001896 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel1f4690c2009-12-15 19:16:48 +00001897 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1898 }
Devang Patel89880c82010-01-15 01:12:22 +00001899 addToContextOwner(VariableDie, GVContext);
Devang Patel2b75ed22009-12-10 19:14:49 +00001900
Bill Wendling2b128d72009-05-20 23:19:06 +00001901 // Expose as global. FIXME - need to check external flag.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001902 TheCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001903
1904 DIType GTy = DI_GV.getType();
Devang Patel12d150e2010-04-13 20:35:04 +00001905 if (GTy.isCompositeType() && !GTy.getName().empty()
1906 && !GTy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001907 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001908 assert(Entry && "Missing global type!");
Devang Patel1a0df9a2010-05-10 22:49:55 +00001909 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001910 }
Devang Patel0751a282009-06-26 01:49:18 +00001911 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001912}
1913
Devang Patel1a0df9a2010-05-10 22:49:55 +00001914/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00001915void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001916 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +00001917
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001918 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001919 CompileUnit *TheCU = getCompileUnit(N);
1920 if (TheCU->getDIE(N))
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001921 return;
1922
Bill Wendling2b128d72009-05-20 23:19:06 +00001923 if (!SP.isDefinition())
1924 // This is a method declaration which will be handled while constructing
1925 // class type.
Devang Patel0751a282009-06-26 01:49:18 +00001926 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001927
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00001928 DIE *SubprogramDie = createSubprogramDIE(SP);
1929
1930 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001931 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001932
1933 // Add to context owner.
Devang Patel1f4690c2009-12-15 19:16:48 +00001934 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +00001935
Bill Wendling2b128d72009-05-20 23:19:06 +00001936 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001937 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001938
Devang Patel0751a282009-06-26 01:49:18 +00001939 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001940}
1941
Devang Patel930143b2009-11-21 02:48:08 +00001942/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +00001943/// content. Create global DIEs and emit initial debug info sections.
1944/// This is inovked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +00001945void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +00001946 if (DisableDebugInfoPrinting)
1947 return;
1948
Devang Patel63524442009-07-30 18:56:46 +00001949 DebugInfoFinder DbgFinder;
1950 DbgFinder.processModule(*M);
Devang Patel0751a282009-06-26 01:49:18 +00001951
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001952 bool HasDebugInfo = false;
1953
1954 // Scan all the compile-units to see if there are any marked as the main unit.
1955 // if not, we do not generate debug info.
1956 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1957 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1958 if (DICompileUnit(*I).isMain()) {
1959 HasDebugInfo = true;
1960 break;
1961 }
1962 }
1963
1964 if (!HasDebugInfo) return;
1965
1966 // Tell MMI that we have debug info.
1967 MMI->setDebugInfoAvailability(true);
1968
Chris Lattnerd442aa32010-04-04 23:17:54 +00001969 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001970 EmitSectionLabels();
Chris Lattnerd442aa32010-04-04 23:17:54 +00001971
Bill Wendling2b128d72009-05-20 23:19:06 +00001972 // Create all the compile unit DIEs.
Devang Patel63524442009-07-30 18:56:46 +00001973 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1974 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001975 constructCompileUnit(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00001976
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001977 // Create DIEs for each subprogram.
Devang Patel63524442009-07-30 18:56:46 +00001978 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1979 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001980 constructSubprogramDIE(*I);
Devang Patel0751a282009-06-26 01:49:18 +00001981
Devang Patel2b75ed22009-12-10 19:14:49 +00001982 // Create DIEs for each global variable.
1983 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1984 E = DbgFinder.global_variable_end(); I != E; ++I)
1985 constructGlobalVariableDIE(*I);
1986
Bill Wendling2b128d72009-05-20 23:19:06 +00001987 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +00001988 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00001989
1990 // Print out .file directives to specify files for .loc directives. These are
1991 // printed out early so that they precede any .loc directives.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001992 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendling2b128d72009-05-20 23:19:06 +00001993 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1994 // Remember source id starts at 1.
1995 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001996 // FIXME: don't use sys::path for this! This should not depend on the
1997 // host.
Bill Wendling2b128d72009-05-20 23:19:06 +00001998 sys::Path FullPath(getSourceDirectoryName(Id.first));
1999 bool AppendOk =
2000 FullPath.appendComponent(getSourceFileName(Id.second));
2001 assert(AppendOk && "Could not append filename to directory!");
2002 AppendOk = false;
Chris Lattner601ef332010-01-25 18:58:59 +00002003 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendling2b128d72009-05-20 23:19:06 +00002004 }
2005 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002006}
2007
Devang Patel930143b2009-11-21 02:48:08 +00002008/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +00002009///
Devang Patel930143b2009-11-21 02:48:08 +00002010void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002011 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +00002012 const Module *M = MMI->getModule();
2013 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2014 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2015 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2016 DISubprogram SP(AllSPs->getOperand(SI));
2017 if (!SP.Verify()) continue;
2018
2019 // Collect info for variables that were optimized out.
2020 StringRef FName = SP.getLinkageName();
2021 if (FName.empty())
2022 FName = SP.getName();
2023 NamedMDNode *NMD =
2024 M->getNamedMetadata(Twine("llvm.dbg.lv.", getRealLinkageName(FName)));
2025 if (!NMD) continue;
2026 unsigned E = NMD->getNumOperands();
2027 if (!E) continue;
2028 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
2029 for (unsigned I = 0; I != E; ++I) {
2030 DIVariable DV(NMD->getOperand(I));
2031 if (!DV.Verify()) continue;
2032 Scope->addVariable(new DbgVariable(DV));
2033 }
2034
2035 // Construct subprogram DIE and add variables DIEs.
2036 constructSubprogramDIE(SP);
2037 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
2038 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
2039 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2040 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2041 if (VariableDIE)
2042 ScopeDIE->addChild(VariableDIE);
2043 }
2044 }
2045 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002046
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002047 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2048 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2049 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2050 DIE *ISP = *AI;
Devang Patel930143b2009-11-21 02:48:08 +00002051 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002052 }
2053
Devang Patel32cc43c2010-05-07 20:54:48 +00002054 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Pateleb57c592009-12-03 19:11:07 +00002055 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2056 DIE *SPDie = CI->first;
Devang Patel32cc43c2010-05-07 20:54:48 +00002057 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Pateleb57c592009-12-03 19:11:07 +00002058 if (!N) continue;
Devang Patel1a0df9a2010-05-10 22:49:55 +00002059 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Pateleb57c592009-12-03 19:11:07 +00002060 if (!NDie) continue;
2061 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Pateleb57c592009-12-03 19:11:07 +00002062 }
2063
Bill Wendling2b128d72009-05-20 23:19:06 +00002064 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002065 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002066 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002067 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002068 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +00002069
2070 // End text sections.
2071 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002072 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +00002073 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +00002074 }
2075
2076 // Emit common frame information.
Devang Patel930143b2009-11-21 02:48:08 +00002077 emitCommonDebugFrame();
Bill Wendling2b128d72009-05-20 23:19:06 +00002078
2079 // Emit function debug frame information
2080 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2081 E = DebugFrames.end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002082 emitFunctionDebugFrame(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00002083
2084 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +00002085 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +00002086
Devang Patel24bc1b52010-06-29 20:17:53 +00002087 // Emit source line correspondence into a debug line section.
2088 emitDebugLines();
2089
Bill Wendling2b128d72009-05-20 23:19:06 +00002090 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +00002091 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002092
2093 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +00002094 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +00002095
Bill Wendling2b128d72009-05-20 23:19:06 +00002096 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +00002097 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +00002098
Devang Patel04d2f2d2009-11-24 01:14:22 +00002099 // Emit info into a debug pubtypes section.
2100 emitDebugPubTypes();
2101
Bill Wendling2b128d72009-05-20 23:19:06 +00002102 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002103 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +00002104
2105 // Emit info into a debug aranges section.
2106 EmitDebugARanges();
2107
2108 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002109 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +00002110
2111 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002112 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002113
2114 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +00002115 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002116
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002117 // Emit info into a debug str section.
2118 emitDebugStr();
2119
Devang Patel1a0df9a2010-05-10 22:49:55 +00002120 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2121 E = CUMap.end(); I != E; ++I)
2122 delete I->second;
2123 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +00002124}
2125
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002126/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patele1c53f22010-05-20 16:36:41 +00002127DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattner915c5f92010-04-02 19:42:39 +00002128 DebugLoc ScopeLoc) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002129
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002130 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002131 if (AbsDbgVariable)
2132 return AbsDbgVariable;
2133
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002134 LLVMContext &Ctx = Var->getContext();
Chris Lattner915c5f92010-04-02 19:42:39 +00002135 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002136 if (!Scope)
2137 return NULL;
2138
Devang Patele1c53f22010-05-20 16:36:41 +00002139 AbsDbgVariable = new DbgVariable(Var);
Devang Patel930143b2009-11-21 02:48:08 +00002140 Scope->addVariable(AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002141 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002142 return AbsDbgVariable;
2143}
2144
Devang Patel490c8ab2010-05-20 19:57:06 +00002145/// collectVariableInfoFromMMITable - Collect variable information from
2146/// side table maintained by MMI.
2147void
2148DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2149 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002150 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patel475d32a2009-10-06 01:26:37 +00002151 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2152 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2153 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00002154 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002155 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00002156 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00002157 DIVariable DV(Var);
2158 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002159
Chris Lattner915c5f92010-04-02 19:42:39 +00002160 DbgScope *Scope = 0;
Devang Patel32cc43c2010-05-07 20:54:48 +00002161 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattner915c5f92010-04-02 19:42:39 +00002162 Scope = ConcreteScopes.lookup(IA);
2163 if (Scope == 0)
2164 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2165
Devang Patelcdb7d442009-11-10 23:20:04 +00002166 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00002167 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00002168 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002169
Devang Patele1c53f22010-05-20 16:36:41 +00002170 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2171 DbgVariable *RegVar = new DbgVariable(DV);
2172 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel930143b2009-11-21 02:48:08 +00002173 Scope->addVariable(RegVar);
Devang Patele1c53f22010-05-20 16:36:41 +00002174 if (AbsDbgVariable) {
2175 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2176 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2177 }
Devang Patel475d32a2009-10-06 01:26:37 +00002178 }
Devang Patel490c8ab2010-05-20 19:57:06 +00002179}
Devang Patela3e9c9c2010-03-15 18:33:46 +00002180
Devang Patel9fc11702010-05-25 23:40:22 +00002181/// isDbgValueInUndefinedReg - Return true if debug value, encoded by
2182/// DBG_VALUE instruction, is in undefined reg.
2183static bool isDbgValueInUndefinedReg(const MachineInstr *MI) {
2184 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2185 if (MI->getOperand(0).isReg() && !MI->getOperand(0).getReg())
2186 return true;
2187 return false;
2188}
2189
2190/// isDbgValueInDefinedReg - Return true if debug value, encoded by
2191/// DBG_VALUE instruction, is in a defined reg.
2192static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2193 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2194 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
2195 return true;
2196 return false;
2197}
2198
Devang Patel490c8ab2010-05-20 19:57:06 +00002199/// collectVariableInfo - Populate DbgScope entries with variables' info.
Devang Patel5c0f85c2010-06-25 22:07:34 +00002200void
2201DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2202 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel490c8ab2010-05-20 19:57:06 +00002203
2204 /// collection info from MMI table.
2205 collectVariableInfoFromMMITable(MF, Processed);
2206
2207 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patela3e9c9c2010-03-15 18:33:46 +00002208 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattner3a383cb2010-04-05 00:13:49 +00002209 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel490c8ab2010-05-20 19:57:06 +00002210 I != E; ++I)
Devang Patela3e9c9c2010-03-15 18:33:46 +00002211 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2212 II != IE; ++II) {
2213 const MachineInstr *MInsn = II;
Devang Patel9fc11702010-05-25 23:40:22 +00002214 if (!MInsn->isDebugValue() || isDbgValueInUndefinedReg(MInsn))
Devang Patela3e9c9c2010-03-15 18:33:46 +00002215 continue;
Devang Patel490c8ab2010-05-20 19:57:06 +00002216 DbgValues.push_back(MInsn);
2217 }
Devang Patela3e9c9c2010-03-15 18:33:46 +00002218
Devang Patel9fc11702010-05-25 23:40:22 +00002219 // This is a collection of DBV_VALUE instructions describing same variable.
2220 SmallVector<const MachineInstr *, 4> MultipleValues;
Devang Patel490c8ab2010-05-20 19:57:06 +00002221 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2222 E = DbgValues.end(); I != E; ++I) {
2223 const MachineInstr *MInsn = *I;
Devang Patel9fc11702010-05-25 23:40:22 +00002224 MultipleValues.clear();
2225 if (isDbgValueInDefinedReg(MInsn))
2226 MultipleValues.push_back(MInsn);
Devang Patel490c8ab2010-05-20 19:57:06 +00002227 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2228 if (Processed.count(DV) != 0)
2229 continue;
2230
Devang Patelc2254f62010-06-02 19:05:13 +00002231 const MachineInstr *PrevMI = MInsn;
Devang Patel9fc11702010-05-25 23:40:22 +00002232 for (SmallVector<const MachineInstr *, 8>::iterator MI = I+1,
2233 ME = DbgValues.end(); MI != ME; ++MI) {
2234 const MDNode *Var =
2235 (*MI)->getOperand((*MI)->getNumOperands()-1).getMetadata();
Devang Patelc2254f62010-06-02 19:05:13 +00002236 if (Var == DV && isDbgValueInDefinedReg(*MI) &&
2237 !PrevMI->isIdenticalTo(*MI))
Devang Patel9fc11702010-05-25 23:40:22 +00002238 MultipleValues.push_back(*MI);
Devang Patelc2254f62010-06-02 19:05:13 +00002239 PrevMI = *MI;
Devang Patel9fc11702010-05-25 23:40:22 +00002240 }
2241
Devang Patel490c8ab2010-05-20 19:57:06 +00002242 DbgScope *Scope = findDbgScope(MInsn);
Devang Patel7a9dedf2010-05-27 20:25:04 +00002243 bool CurFnArg = false;
2244 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2245 DISubprogram(DV.getContext()).describes(MF->getFunction()))
2246 CurFnArg = true;
2247 if (!Scope && CurFnArg)
Devang Patelfbd6c452010-05-21 00:10:20 +00002248 Scope = CurrentFnDbgScope;
Devang Patel490c8ab2010-05-20 19:57:06 +00002249 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00002250 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00002251 continue;
2252
2253 Processed.insert(DV);
Devang Patel490c8ab2010-05-20 19:57:06 +00002254 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel490c8ab2010-05-20 19:57:06 +00002255 Scope->addVariable(RegVar);
Devang Patel7a9dedf2010-05-27 20:25:04 +00002256 if (!CurFnArg)
Devang Patel9fc11702010-05-25 23:40:22 +00002257 DbgVariableLabelsMap[RegVar] = getLabelBeforeInsn(MInsn);
Devang Patelfbd6c452010-05-21 00:10:20 +00002258 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2259 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2260 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patela3e9c9c2010-03-15 18:33:46 +00002261 }
Devang Patel9fc11702010-05-25 23:40:22 +00002262 if (MultipleValues.size() <= 1) {
2263 DbgVariableToDbgInstMap[RegVar] = MInsn;
2264 continue;
2265 }
2266
2267 // handle multiple DBG_VALUE instructions describing one variable.
Devang Patel9fc11702010-05-25 23:40:22 +00002268 if (DotDebugLocEntries.empty())
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002269 RegVar->setDotDebugLocOffset(0);
2270 else
2271 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002272 const MachineInstr *Begin = NULL;
2273 const MachineInstr *End = NULL;
2274 for (SmallVector<const MachineInstr *, 4>::iterator
Devang Patel30265c42010-07-07 20:12:52 +00002275 MVI = MultipleValues.begin(), MVE = MultipleValues.end();
2276 MVI != MVE; ++MVI) {
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002277 if (!Begin) {
2278 Begin = *MVI;
2279 continue;
2280 }
2281 End = *MVI;
Devang Patel9fc11702010-05-25 23:40:22 +00002282 MachineLocation MLoc;
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002283 MLoc.set(Begin->getOperand(0).getReg(), 0);
2284 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2285 const MCSymbol *SLabel = getLabelBeforeInsn(End);
Devang Patel9fc11702010-05-25 23:40:22 +00002286 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002287 Begin = End;
2288 if (MVI + 1 == MVE) {
2289 // If End is the last instruction then its value is valid
Devang Patel9fc11702010-05-25 23:40:22 +00002290 // until the end of the funtion.
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002291 MLoc.set(End->getOperand(0).getReg(), 0);
Devang Patel9fc11702010-05-25 23:40:22 +00002292 DotDebugLocEntries.
2293 push_back(DotDebugLocEntry(SLabel, FunctionEndSym, MLoc));
2294 }
2295 }
2296 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00002297 }
Devang Patele0a94bf2010-05-14 21:01:35 +00002298
2299 // Collect info for variables that were optimized out.
Devang Patelad517352010-06-22 01:01:58 +00002300 const Function *F = MF->getFunction();
2301 const Module *M = F->getParent();
2302 if (NamedMDNode *NMD =
Devang Patelb6e058d2010-06-22 01:19:38 +00002303 M->getNamedMetadata(Twine("llvm.dbg.lv.",
2304 getRealLinkageName(F->getName())))) {
Devang Patele0a94bf2010-05-14 21:01:35 +00002305 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2306 DIVariable DV(cast_or_null<MDNode>(NMD->getOperand(i)));
Devang Patel490c8ab2010-05-20 19:57:06 +00002307 if (!DV || !Processed.insert(DV))
Devang Patele0a94bf2010-05-14 21:01:35 +00002308 continue;
2309 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2310 if (Scope)
Devang Patele1c53f22010-05-20 16:36:41 +00002311 Scope->addVariable(new DbgVariable(DV));
Devang Patele0a94bf2010-05-14 21:01:35 +00002312 }
2313 }
Devang Patel9fc11702010-05-25 23:40:22 +00002314}
Devang Patele0a94bf2010-05-14 21:01:35 +00002315
Devang Patel9fc11702010-05-25 23:40:22 +00002316/// getLabelBeforeInsn - Return Label preceding the instruction.
2317const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2318 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2319 LabelsBeforeInsn.find(MI);
2320 if (I == LabelsBeforeInsn.end())
2321 // FunctionBeginSym always preceeds all the instruction in current function.
2322 return FunctionBeginSym;
2323 return I->second;
2324}
2325
2326/// getLabelAfterInsn - Return Label immediately following the instruction.
2327const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2328 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2329 LabelsAfterInsn.find(MI);
2330 if (I == LabelsAfterInsn.end())
2331 return NULL;
2332 return I->second;
Devang Patel475d32a2009-10-06 01:26:37 +00002333}
2334
Devang Patelbd477be2010-03-29 17:20:31 +00002335/// beginScope - Process beginning of a scope.
2336void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patel002d54d2010-05-26 19:37:24 +00002337 if (InsnNeedsLabel.count(MI) == 0) {
2338 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patelbd477be2010-03-29 17:20:31 +00002339 return;
Devang Patel9fc11702010-05-25 23:40:22 +00002340 }
Devang Patel23b2ae62010-03-29 22:59:58 +00002341
Devang Patel002d54d2010-05-26 19:37:24 +00002342 // Check location.
2343 DebugLoc DL = MI->getDebugLoc();
2344 if (!DL.isUnknown()) {
2345 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2346 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Patel6c74a872010-04-27 19:46:33 +00002347 PrevInstLoc = DL;
Devang Patel002d54d2010-05-26 19:37:24 +00002348 LabelsBeforeInsn[MI] = PrevLabel;
2349 return;
Devang Patel6c74a872010-04-27 19:46:33 +00002350 }
Devang Patelbd477be2010-03-29 17:20:31 +00002351
Devang Patelacc32a52010-05-26 21:23:46 +00002352 // If location is unknown then use temp label for this DBG_VALUE
Devang Patel002d54d2010-05-26 19:37:24 +00002353 // instruction.
2354 if (MI->isDebugValue()) {
Devang Patelacc32a52010-05-26 21:23:46 +00002355 PrevLabel = MMI->getContext().CreateTempSymbol();
2356 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00002357 LabelsBeforeInsn[MI] = PrevLabel;
2358 return;
2359 }
2360
2361 if (UnknownLocations) {
2362 PrevLabel = recordSourceLine(0, 0, 0);
2363 LabelsBeforeInsn[MI] = PrevLabel;
2364 return;
2365 }
2366
2367 assert (0 && "Instruction is not processed!");
Devang Patel8db360d2009-10-06 01:50:42 +00002368}
2369
Devang Patel930143b2009-11-21 02:48:08 +00002370/// endScope - Process end of a scope.
2371void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel3ebd8932010-04-08 16:50:29 +00002372 if (InsnsEndScopeSet.count(MI) != 0) {
2373 // Emit a label if this instruction ends a scope.
2374 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2375 Asm->OutStreamer.EmitLabel(Label);
Devang Patel6c74a872010-04-27 19:46:33 +00002376 LabelsAfterInsn[MI] = Label;
Devang Patel3ebd8932010-04-08 16:50:29 +00002377 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002378}
2379
Devang Patel6c74a872010-04-27 19:46:33 +00002380/// getOrCreateDbgScope - Create DbgScope for the scope.
Devang Patel30265c42010-07-07 20:12:52 +00002381DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
2382 const MDNode *InlinedAt) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002383 if (!InlinedAt) {
2384 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2385 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002386 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002387 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2388 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patel6c74a872010-04-27 19:46:33 +00002389 if (DIDescriptor(Scope).isLexicalBlock()) {
2390 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002391 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Patel6c74a872010-04-27 19:46:33 +00002392 WScope->setParent(Parent);
2393 Parent->addScope(WScope);
2394 }
2395
2396 if (!WScope->getParent()) {
2397 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastings9b5005c2010-06-15 23:06:30 +00002398 // We used to check only for a linkage name, but that fails
2399 // since we began omitting the linkage name for private
2400 // functions. The new way is to check for the name in metadata,
2401 // but that's not supported in old .ll test cases. Ergo, we
2402 // check both.
Stuart Hastingsafe54f12010-06-11 20:08:44 +00002403 if (SPName == Asm->MF->getFunction()->getName() ||
2404 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Patel6c74a872010-04-27 19:46:33 +00002405 CurrentFnDbgScope = WScope;
2406 }
2407
2408 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002409 }
2410
Devang Patel5c0f85c2010-06-25 22:07:34 +00002411 getOrCreateAbstractScope(Scope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002412 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2413 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002414 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002415
2416 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2417 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2418 DILocation DL(InlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002419 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002420 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Patel6c74a872010-04-27 19:46:33 +00002421 WScope->setParent(Parent);
2422 Parent->addScope(WScope);
2423
2424 ConcreteScopes[InlinedAt] = WScope;
Devang Patel6c74a872010-04-27 19:46:33 +00002425
2426 return WScope;
Devang Patel8db360d2009-10-06 01:50:42 +00002427}
2428
Devang Patel6c74a872010-04-27 19:46:33 +00002429/// hasValidLocation - Return true if debug location entry attached with
2430/// machine instruction encodes valid location info.
2431static bool hasValidLocation(LLVMContext &Ctx,
2432 const MachineInstr *MInsn,
Devang Patel32cc43c2010-05-07 20:54:48 +00002433 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Patel6c74a872010-04-27 19:46:33 +00002434 DebugLoc DL = MInsn->getDebugLoc();
2435 if (DL.isUnknown()) return false;
2436
Devang Patel32cc43c2010-05-07 20:54:48 +00002437 const MDNode *S = DL.getScope(Ctx);
Devang Patel6c74a872010-04-27 19:46:33 +00002438
2439 // There is no need to create another DIE for compile unit. For all
2440 // other scopes, create one DbgScope now. This will be translated
2441 // into a scope DIE at the end.
2442 if (DIScope(S).isCompileUnit()) return false;
2443
2444 Scope = S;
2445 InlinedAt = DL.getInlinedAt(Ctx);
2446 return true;
2447}
2448
2449/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2450/// hierarchy.
2451static void calculateDominanceGraph(DbgScope *Scope) {
2452 assert (Scope && "Unable to calculate scop edominance graph!");
2453 SmallVector<DbgScope *, 4> WorkStack;
2454 WorkStack.push_back(Scope);
2455 unsigned Counter = 0;
2456 while (!WorkStack.empty()) {
2457 DbgScope *WS = WorkStack.back();
2458 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2459 bool visitedChildren = false;
2460 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2461 SE = Children.end(); SI != SE; ++SI) {
2462 DbgScope *ChildScope = *SI;
2463 if (!ChildScope->getDFSOut()) {
2464 WorkStack.push_back(ChildScope);
2465 visitedChildren = true;
2466 ChildScope->setDFSIn(++Counter);
2467 break;
2468 }
2469 }
2470 if (!visitedChildren) {
2471 WorkStack.pop_back();
2472 WS->setDFSOut(++Counter);
2473 }
2474 }
2475}
2476
2477/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2478static
2479void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2480 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2481{
2482#ifndef NDEBUG
2483 unsigned PrevDFSIn = 0;
2484 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2485 I != E; ++I) {
2486 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2487 II != IE; ++II) {
2488 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002489 const MDNode *Scope = NULL;
2490 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002491
2492 // Check if instruction has valid location information.
2493 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2494 dbgs() << " [ ";
2495 if (InlinedAt)
2496 dbgs() << "*";
2497 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2498 MI2ScopeMap.find(MInsn);
2499 if (DI != MI2ScopeMap.end()) {
2500 DbgScope *S = DI->second;
2501 dbgs() << S->getDFSIn();
2502 PrevDFSIn = S->getDFSIn();
2503 } else
2504 dbgs() << PrevDFSIn;
2505 } else
2506 dbgs() << " [ x" << PrevDFSIn;
2507 dbgs() << " ]";
2508 MInsn->dump();
2509 }
2510 dbgs() << "\n";
2511 }
2512#endif
2513}
Devang Patel930143b2009-11-21 02:48:08 +00002514/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner848c7d22010-03-31 05:39:57 +00002515/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner76555b52010-01-26 23:18:02 +00002516bool DwarfDebug::extractScopeInformation() {
Devang Patel75cc16c2009-10-01 20:31:14 +00002517 // If scope information was extracted using .dbg intrinsics then there is not
2518 // any need to extract these information by scanning each instruction.
2519 if (!DbgScopeMap.empty())
2520 return false;
2521
Dan Gohmane9135cb2010-04-23 01:18:53 +00002522 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6c74a872010-04-27 19:46:33 +00002523 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2524 SmallVector<DbgRange, 4> MIRanges;
2525 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patel32cc43c2010-05-07 20:54:48 +00002526 const MDNode *PrevScope = NULL;
2527 const MDNode *PrevInlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002528 const MachineInstr *RangeBeginMI = NULL;
2529 const MachineInstr *PrevMI = NULL;
Chris Lattner3a383cb2010-04-05 00:13:49 +00002530 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel75cc16c2009-10-01 20:31:14 +00002531 I != E; ++I) {
2532 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2533 II != IE; ++II) {
2534 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002535 const MDNode *Scope = NULL;
2536 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002537
2538 // Check if instruction has valid location information.
2539 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2540 PrevMI = MInsn;
2541 continue;
2542 }
Chris Lattner915c5f92010-04-02 19:42:39 +00002543
Devang Patel6c74a872010-04-27 19:46:33 +00002544 // If scope has not changed then skip this instruction.
2545 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2546 PrevMI = MInsn;
2547 continue;
2548 }
2549
2550 if (RangeBeginMI) {
2551 // If we have alread seen a beginning of a instruction range and
2552 // current instruction scope does not match scope of first instruction
2553 // in this range then create a new instruction range.
2554 DbgRange R(RangeBeginMI, PrevMI);
Devang Patel30265c42010-07-07 20:12:52 +00002555 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
2556 PrevInlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002557 MIRanges.push_back(R);
2558 }
2559
2560 // This is a beginning of a new instruction range.
2561 RangeBeginMI = MInsn;
Chris Lattner915c5f92010-04-02 19:42:39 +00002562
Devang Patel6c74a872010-04-27 19:46:33 +00002563 // Reset previous markers.
2564 PrevMI = MInsn;
2565 PrevScope = Scope;
2566 PrevInlinedAt = InlinedAt;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002567 }
2568 }
2569
Devang Patel6c74a872010-04-27 19:46:33 +00002570 // Create last instruction range.
2571 if (RangeBeginMI && PrevMI && PrevScope) {
2572 DbgRange R(RangeBeginMI, PrevMI);
2573 MIRanges.push_back(R);
2574 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel75cc16c2009-10-01 20:31:14 +00002575 }
Devang Patel6c74a872010-04-27 19:46:33 +00002576
Devang Patel530a0752010-01-04 20:44:00 +00002577 if (!CurrentFnDbgScope)
2578 return false;
2579
Devang Patel6c74a872010-04-27 19:46:33 +00002580 calculateDominanceGraph(CurrentFnDbgScope);
2581 if (PrintDbgScope)
2582 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2583
2584 // Find ranges of instructions covered by each DbgScope;
2585 DbgScope *PrevDbgScope = NULL;
2586 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2587 RE = MIRanges.end(); RI != RE; ++RI) {
2588 const DbgRange &R = *RI;
2589 DbgScope *S = MI2ScopeMap.lookup(R.first);
2590 assert (S && "Lost DbgScope for a machine instruction!");
2591 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2592 PrevDbgScope->closeInsnRange(S);
2593 S->openInsnRange(R.first);
2594 S->extendInsnRange(R.second);
2595 PrevDbgScope = S;
2596 }
2597
2598 if (PrevDbgScope)
2599 PrevDbgScope->closeInsnRange();
Devang Patel75cc16c2009-10-01 20:31:14 +00002600
Devang Patel359b0132010-04-08 18:43:56 +00002601 identifyScopeMarkers();
Devang Patelf1d5a1e2010-04-08 15:37:09 +00002602
2603 return !DbgScopeMap.empty();
2604}
2605
Devang Patel6c74a872010-04-27 19:46:33 +00002606/// identifyScopeMarkers() -
2607/// Each DbgScope has first instruction and last instruction to mark beginning
2608/// and end of a scope respectively. Create an inverse map that list scopes
2609/// starts (and ends) with an instruction. One instruction may start (or end)
2610/// multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00002611void DwarfDebug::identifyScopeMarkers() {
Devang Patel7771b7c2010-01-20 02:05:23 +00002612 SmallVector<DbgScope *, 4> WorkList;
2613 WorkList.push_back(CurrentFnDbgScope);
2614 while (!WorkList.empty()) {
Chris Lattner848c7d22010-03-31 05:39:57 +00002615 DbgScope *S = WorkList.pop_back_val();
Devang Patel6c74a872010-04-27 19:46:33 +00002616
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002617 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patel7771b7c2010-01-20 02:05:23 +00002618 if (!Children.empty())
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002619 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00002620 SE = Children.end(); SI != SE; ++SI)
2621 WorkList.push_back(*SI);
2622
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002623 if (S->isAbstractScope())
2624 continue;
Devang Patel6c74a872010-04-27 19:46:33 +00002625
2626 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2627 if (Ranges.empty())
2628 continue;
2629 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2630 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patela0813082010-05-19 21:58:28 +00002631 assert(RI->first && "DbgRange does not have first instruction!");
2632 assert(RI->second && "DbgRange does not have second instruction!");
Devang Patel6c74a872010-04-27 19:46:33 +00002633 InsnsEndScopeSet.insert(RI->second);
2634 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002635 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002636}
2637
Dan Gohman3df671a2010-04-20 00:37:27 +00002638/// FindFirstDebugLoc - Find the first debug location in the function. This
2639/// is intended to be an approximation for the source position of the
2640/// beginning of the function.
2641static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2642 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2643 I != E; ++I)
2644 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2645 MBBI != MBBE; ++MBBI) {
2646 DebugLoc DL = MBBI->getDebugLoc();
2647 if (!DL.isUnknown())
2648 return DL;
2649 }
2650 return DebugLoc();
2651}
2652
Devang Patel930143b2009-11-21 02:48:08 +00002653/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00002654/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00002655void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00002656 if (!MMI->hasDebugInfo()) return;
Bill Wendlingfcc14142010-04-07 09:28:04 +00002657 if (!extractScopeInformation()) return;
Devang Patel4598eb62009-10-06 18:37:31 +00002658
Devang Patel6c74a872010-04-27 19:46:33 +00002659 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2660 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00002661 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00002662 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00002663
2664 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2665 // function.
Dan Gohman3df671a2010-04-20 00:37:27 +00002666 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattner915c5f92010-04-02 19:42:39 +00002667 if (FDL.isUnknown()) return;
2668
Devang Patel32cc43c2010-05-07 20:54:48 +00002669 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Chris Lattner915c5f92010-04-02 19:42:39 +00002670
2671 DISubprogram SP = getDISubprogram(Scope);
2672 unsigned Line, Col;
2673 if (SP.Verify()) {
2674 Line = SP.getLineNumber();
2675 Col = 0;
2676 } else {
2677 Line = FDL.getLine();
2678 Col = FDL.getCol();
Bill Wendling2b128d72009-05-20 23:19:06 +00002679 }
Chris Lattner915c5f92010-04-02 19:42:39 +00002680
2681 recordSourceLine(Line, Col, Scope);
Devang Patel002d54d2010-05-26 19:37:24 +00002682
Devang Patel21ccf052010-06-02 16:42:51 +00002683 /// ProcessedArgs - Collection of arguments already processed.
2684 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2685
Devang Patel002d54d2010-05-26 19:37:24 +00002686 DebugLoc PrevLoc;
2687 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2688 I != E; ++I)
2689 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2690 II != IE; ++II) {
2691 const MachineInstr *MI = II;
2692 DebugLoc DL = MI->getDebugLoc();
2693 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00002694 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2695 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2696 if (!DV.Verify()) continue;
Devang Patel5e6b71c2010-05-27 16:47:30 +00002697 // If DBG_VALUE is for a local variable then it needs a label.
Devang Patel42939752010-07-01 21:38:08 +00002698 if (DV.getTag() != dwarf::DW_TAG_arg_variable
2699 && isDbgValueInUndefinedReg(MI) == false)
Devang Patel002d54d2010-05-26 19:37:24 +00002700 InsnNeedsLabel.insert(MI);
Devang Patel5e6b71c2010-05-27 16:47:30 +00002701 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel42939752010-07-01 21:38:08 +00002702 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2703 describes(MF->getFunction()))
Devang Patel5e6b71c2010-05-27 16:47:30 +00002704 InsnNeedsLabel.insert(MI);
2705 // DBG_VALUE indicating argument location change needs a label.
Devang Patelc657c622010-06-24 21:51:19 +00002706 else if (isDbgValueInUndefinedReg(MI) == false && !ProcessedArgs.insert(DV))
Devang Patel002d54d2010-05-26 19:37:24 +00002707 InsnNeedsLabel.insert(MI);
2708 } else {
2709 // If location is unknown then instruction needs a location only if
2710 // UnknownLocations flag is set.
2711 if (DL.isUnknown()) {
2712 if (UnknownLocations && !PrevLoc.isUnknown())
2713 InsnNeedsLabel.insert(MI);
2714 } else if (DL != PrevLoc)
2715 // Otherwise, instruction needs a location only if it is new location.
2716 InsnNeedsLabel.insert(MI);
2717 }
2718
2719 if (!DL.isUnknown() || UnknownLocations)
2720 PrevLoc = DL;
2721 }
2722
2723 PrevLabel = FunctionBeginSym;
Bill Wendling2b128d72009-05-20 23:19:06 +00002724}
2725
Devang Patel930143b2009-11-21 02:48:08 +00002726/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00002727///
Chris Lattner76555b52010-01-26 23:18:02 +00002728void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingfcc14142010-04-07 09:28:04 +00002729 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00002730
Devang Patel530a0752010-01-04 20:44:00 +00002731 if (CurrentFnDbgScope) {
Devang Patel4a8e6e82010-05-22 00:04:14 +00002732
Devang Patel9fc11702010-05-25 23:40:22 +00002733 // Define end label for subprogram.
2734 FunctionEndSym = Asm->GetTempSymbol("func_end",
2735 Asm->getFunctionNumber());
2736 // Assumes in correct section after the entry point.
2737 Asm->OutStreamer.EmitLabel(FunctionEndSym);
2738
Devang Patel5c0f85c2010-06-25 22:07:34 +00002739 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2740 collectVariableInfo(MF, ProcessedVars);
Devang Patel4a8e6e82010-05-22 00:04:14 +00002741
Devang Patel530a0752010-01-04 20:44:00 +00002742 // Get function line info.
2743 if (!Lines.empty()) {
2744 // Get section line info.
2745 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2746 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2747 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2748 // Append the function info to section info.
2749 SectionLineInfos.insert(SectionLineInfos.end(),
2750 Lines.begin(), Lines.end());
2751 }
2752
2753 // Construct abstract scopes.
2754 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel5c0f85c2010-06-25 22:07:34 +00002755 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel5c0f85c2010-06-25 22:07:34 +00002756 DISubprogram SP((*AI)->getScopeNode());
2757 if (SP.Verify()) {
2758 // Collect info for variables that were optimized out.
2759 StringRef FName = SP.getLinkageName();
2760 if (FName.empty())
2761 FName = SP.getName();
2762 const Module *M = MF->getFunction()->getParent();
2763 if (NamedMDNode *NMD =
2764 M->getNamedMetadata(Twine("llvm.dbg.lv.",
2765 getRealLinkageName(FName)))) {
2766 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2767 DIVariable DV(cast_or_null<MDNode>(NMD->getOperand(i)));
2768 if (!DV || !ProcessedVars.insert(DV))
2769 continue;
Devang Patel648df7b2010-06-30 00:11:08 +00002770 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel5c0f85c2010-06-25 22:07:34 +00002771 if (Scope)
2772 Scope->addVariable(new DbgVariable(DV));
2773 }
2774 }
2775 }
Devang Patelc5b31092010-06-30 01:40:11 +00002776 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2777 constructScopeDIE(*AI);
Devang Patel5c0f85c2010-06-25 22:07:34 +00002778 }
Devang Patel30265c42010-07-07 20:12:52 +00002779
Devang Patel075e9b52010-05-04 06:15:30 +00002780 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel530a0752010-01-04 20:44:00 +00002781
Devang Patel075e9b52010-05-04 06:15:30 +00002782 if (!DisableFramePointerElim(*MF))
2783 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2784 dwarf::DW_FORM_flag, 1);
2785
2786
Chris Lattner3a383cb2010-04-05 00:13:49 +00002787 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel530a0752010-01-04 20:44:00 +00002788 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00002789 }
2790
Bill Wendling2b128d72009-05-20 23:19:06 +00002791 // Clear debug info
Devang Patelfe189e62010-01-19 01:26:02 +00002792 CurrentFnDbgScope = NULL;
Devang Patel002d54d2010-05-26 19:37:24 +00002793 InsnNeedsLabel.clear();
Devang Patele1c53f22010-05-20 16:36:41 +00002794 DbgVariableToFrameIndexMap.clear();
2795 VarToAbstractVarMap.clear();
2796 DbgVariableToDbgInstMap.clear();
2797 DbgVariableLabelsMap.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002798 DeleteContainerSeconds(DbgScopeMap);
Devang Patel541019d2010-04-09 16:04:20 +00002799 InsnsEndScopeSet.clear();
Devang Patelfe189e62010-01-19 01:26:02 +00002800 ConcreteScopes.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002801 DeleteContainerSeconds(AbstractScopes);
Devang Patelfe189e62010-01-19 01:26:02 +00002802 AbstractScopesList.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002803 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00002804 LabelsBeforeInsn.clear();
2805 LabelsAfterInsn.clear();
Bill Wendling2b128d72009-05-20 23:19:06 +00002806 Lines.clear();
Devang Patel12563b32010-04-16 23:33:45 +00002807 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00002808}
2809
Devang Patele1c53f22010-05-20 16:36:41 +00002810/// recordVariableFrameIndex - Record a variable's index.
2811void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2812 assert (V && "Invalid DbgVariable!");
2813 DbgVariableToFrameIndexMap[V] = Index;
2814}
2815
2816/// findVariableFrameIndex - Return true if frame index for the variable
2817/// is found. Update FI to hold value of the index.
2818bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2819 assert (V && "Invalid DbgVariable!");
2820 DenseMap<const DbgVariable *, int>::iterator I =
2821 DbgVariableToFrameIndexMap.find(V);
2822 if (I == DbgVariableToFrameIndexMap.end())
2823 return false;
2824 *FI = I->second;
2825 return true;
2826}
2827
2828/// findVariableLabel - Find MCSymbol for the variable.
2829const MCSymbol *DwarfDebug::findVariableLabel(const DbgVariable *V) {
2830 DenseMap<const DbgVariable *, const MCSymbol *>::iterator I
2831 = DbgVariableLabelsMap.find(V);
2832 if (I == DbgVariableLabelsMap.end())
2833 return NULL;
2834 else return I->second;
2835}
2836
Devang Patel490c8ab2010-05-20 19:57:06 +00002837/// findDbgScope - Find DbgScope for the debug loc attached with an
2838/// instruction.
2839DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2840 DbgScope *Scope = NULL;
2841 LLVMContext &Ctx =
2842 MInsn->getParent()->getParent()->getFunction()->getContext();
2843 DebugLoc DL = MInsn->getDebugLoc();
2844
2845 if (DL.isUnknown())
2846 return Scope;
2847
2848 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2849 Scope = ConcreteScopes.lookup(IA);
2850 if (Scope == 0)
2851 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2852
2853 return Scope;
2854}
2855
2856
Chris Lattnerba35a672010-03-09 04:54:43 +00002857/// recordSourceLine - Register a source line with debug info. Returns the
2858/// unique label that was emitted and which provides correspondence to
2859/// the source line list.
Devang Patel30265c42010-07-07 20:12:52 +00002860MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
2861 const MDNode *S) {
Devang Patel2d9caf92009-11-25 17:36:49 +00002862 StringRef Dir;
2863 StringRef Fn;
Devang Patel2089d162009-10-05 18:03:19 +00002864
Dan Gohman50849c62010-05-05 23:41:32 +00002865 unsigned Src = 1;
2866 if (S) {
2867 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00002868
Dan Gohman50849c62010-05-05 23:41:32 +00002869 if (Scope.isCompileUnit()) {
2870 DICompileUnit CU(S);
2871 Dir = CU.getDirectory();
2872 Fn = CU.getFilename();
2873 } else if (Scope.isSubprogram()) {
2874 DISubprogram SP(S);
2875 Dir = SP.getDirectory();
2876 Fn = SP.getFilename();
2877 } else if (Scope.isLexicalBlock()) {
2878 DILexicalBlock DB(S);
2879 Dir = DB.getDirectory();
2880 Fn = DB.getFilename();
2881 } else
2882 assert(0 && "Unexpected scope info");
2883
2884 Src = GetOrCreateSourceID(Dir, Fn);
2885 }
2886
Stuart Hastingsafe54f12010-06-11 20:08:44 +00002887#if 0
2888 if (!Lines.empty()) {
2889 SrcLineInfo lastSrcLineInfo = Lines.back();
2890 // Emitting sequential line records with the same line number (but
2891 // different addresses) seems to confuse GDB. Avoid this.
2892 if (lastSrcLineInfo.getLine() == Line)
2893 return NULL;
2894 }
2895#endif
2896
Chris Lattner6e52e9d2010-03-14 08:36:50 +00002897 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattnerb4666f42010-03-14 08:15:55 +00002898 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendling2b128d72009-05-20 23:19:06 +00002899
Chris Lattnerba35a672010-03-09 04:54:43 +00002900 Asm->OutStreamer.EmitLabel(Label);
2901 return Label;
Bill Wendling2b128d72009-05-20 23:19:06 +00002902}
2903
Bill Wendling806535f2009-05-20 23:22:40 +00002904//===----------------------------------------------------------------------===//
2905// Emit Methods
2906//===----------------------------------------------------------------------===//
2907
Devang Patel930143b2009-11-21 02:48:08 +00002908/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00002909///
Jim Grosbach00e9c612009-11-22 19:20:36 +00002910unsigned
2911DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00002912 // Get the children.
2913 const std::vector<DIE *> &Children = Die->getChildren();
2914
2915 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00002916 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00002917 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00002918
2919 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00002920 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00002921
2922 // Get the abbreviation for this DIE.
2923 unsigned AbbrevNumber = Die->getAbbrevNumber();
2924 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2925
2926 // Set DIE offset
2927 Die->setOffset(Offset);
2928
2929 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00002930 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002931
2932 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2933 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2934
2935 // Size the DIE attribute values.
2936 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2937 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00002938 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00002939
2940 // Size the DIE children if any.
2941 if (!Children.empty()) {
2942 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2943 "Children flag not set");
2944
2945 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00002946 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00002947
2948 // End of children marker.
2949 Offset += sizeof(int8_t);
2950 }
2951
2952 Die->setSize(Offset - Die->getOffset());
2953 return Offset;
2954}
2955
Devang Patel930143b2009-11-21 02:48:08 +00002956/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00002957///
Devang Patel930143b2009-11-21 02:48:08 +00002958void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002959 unsigned PrevOffset = 0;
2960 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2961 E = CUMap.end(); I != E; ++I) {
2962 // Compute size of compile unit header.
2963 static unsigned Offset = PrevOffset +
2964 sizeof(int32_t) + // Length of Compilation Unit Info
2965 sizeof(int16_t) + // DWARF version number
2966 sizeof(int32_t) + // Offset Into Abbrev. Section
2967 sizeof(int8_t); // Pointer Size (in bytes)
2968 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
2969 PrevOffset = Offset;
2970 }
Bill Wendling480ff322009-05-20 23:21:38 +00002971}
2972
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002973/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2974/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00002975static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002976 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00002977 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00002978 if (!SymbolStem) return 0;
2979
Chris Lattner6629ca92010-04-04 22:59:04 +00002980 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2981 Asm->OutStreamer.EmitLabel(TmpSym);
2982 return TmpSym;
2983}
2984
2985/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2986/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00002987void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002988 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002989
Bill Wendling480ff322009-05-20 23:21:38 +00002990 // Dwarf sections base addresses.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002991 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6629ca92010-04-04 22:59:04 +00002992 DwarfFrameSectionSym =
2993 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2994 }
Bill Wendling480ff322009-05-20 23:21:38 +00002995
Chris Lattner6629ca92010-04-04 22:59:04 +00002996 DwarfInfoSectionSym =
2997 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2998 DwarfAbbrevSectionSym =
2999 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003000 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner6629ca92010-04-04 22:59:04 +00003001
3002 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003003 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003004
Devang Patel24bc1b52010-06-29 20:17:53 +00003005 DwarfDebugLineSectionSym =
3006 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003007 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3008 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3009 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner6629ca92010-04-04 22:59:04 +00003010 DwarfStrSectionSym =
3011 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00003012 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3013 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00003014
Devang Patel9fc11702010-05-25 23:40:22 +00003015 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3016 "section_debug_loc");
3017
Chris Lattner6629ca92010-04-04 22:59:04 +00003018 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00003019 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003020}
3021
Devang Patel930143b2009-11-21 02:48:08 +00003022/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00003023///
Devang Patel930143b2009-11-21 02:48:08 +00003024void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00003025 // Get the abbreviation for this DIE.
3026 unsigned AbbrevNumber = Die->getAbbrevNumber();
3027 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3028
Bill Wendling480ff322009-05-20 23:21:38 +00003029 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00003030 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00003031 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3032 Twine::utohexstr(Die->getOffset()) + ":0x" +
3033 Twine::utohexstr(Die->getSize()) + " " +
3034 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00003035 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00003036
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00003037 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00003038 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3039
3040 // Emit the DIE attribute values.
3041 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3042 unsigned Attr = AbbrevData[i].getAttribute();
3043 unsigned Form = AbbrevData[i].getForm();
3044 assert(Form && "Too many attributes for DIE (check abbreviation)");
3045
Chris Lattner7bde8c02010-04-04 18:52:31 +00003046 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00003047 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
3048
Bill Wendling480ff322009-05-20 23:21:38 +00003049 switch (Attr) {
3050 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00003051 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003052 break;
3053 case dwarf::DW_AT_abstract_origin: {
3054 DIEEntry *E = cast<DIEEntry>(Values[i]);
3055 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003056 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00003057 Asm->EmitInt32(Addr);
3058 break;
3059 }
Devang Patel12563b32010-04-16 23:33:45 +00003060 case dwarf::DW_AT_ranges: {
3061 // DW_AT_range Value encodes offset in debug_range section.
3062 DIEInteger *V = cast<DIEInteger>(Values[i]);
3063 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3064 V->getValue(),
3065 DwarfDebugRangeSectionSym,
3066 4);
3067 break;
3068 }
Devang Patel24bc1b52010-06-29 20:17:53 +00003069 case dwarf::DW_AT_stmt_list: {
3070 Asm->EmitLabelDifference(CurrentLineSectionSym,
3071 DwarfDebugLineSectionSym, 4);
3072 break;
3073 }
Devang Patel9fc11702010-05-25 23:40:22 +00003074 case dwarf::DW_AT_location: {
3075 if (UseDotDebugLocEntry.count(Die) != 0) {
3076 DIELabel *L = cast<DIELabel>(Values[i]);
3077 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3078 } else
3079 Values[i]->EmitValue(Asm, Form);
3080 break;
3081 }
Bill Wendling480ff322009-05-20 23:21:38 +00003082 default:
3083 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003084 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00003085 break;
3086 }
Bill Wendling480ff322009-05-20 23:21:38 +00003087 }
3088
3089 // Emit the DIE children if any.
3090 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3091 const std::vector<DIE *> &Children = Die->getChildren();
3092
3093 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00003094 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00003095
Chris Lattner7bde8c02010-04-04 18:52:31 +00003096 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00003097 Asm->OutStreamer.AddComment("End Of Children Mark");
3098 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003099 }
3100}
3101
Devang Patel9ccfb642009-12-09 18:24:21 +00003102/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00003103///
Devang Patel9ccfb642009-12-09 18:24:21 +00003104void DwarfDebug::emitDebugInfo() {
3105 // Start debug info section.
3106 Asm->OutStreamer.SwitchSection(
3107 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00003108 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3109 E = CUMap.end(); I != E; ++I) {
3110 CompileUnit *TheCU = I->second;
3111 DIE *Die = TheCU->getCUDie();
3112
3113 // Emit the compile units header.
3114 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3115 TheCU->getID()));
3116
3117 // Emit size of content not including length itself
3118 unsigned ContentSize = Die->getSize() +
3119 sizeof(int16_t) + // DWARF version number
3120 sizeof(int32_t) + // Offset Into Abbrev. Section
3121 sizeof(int8_t) + // Pointer Size (in bytes)
3122 sizeof(int32_t); // FIXME - extra pad for gdb bug.
3123
3124 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3125 Asm->EmitInt32(ContentSize);
3126 Asm->OutStreamer.AddComment("DWARF version number");
3127 Asm->EmitInt16(dwarf::DWARF_VERSION);
3128 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3129 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3130 DwarfAbbrevSectionSym);
3131 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3132 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
3133
3134 emitDIE(Die);
3135 // FIXME - extra padding for gdb bug.
3136 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3137 Asm->EmitInt8(0);
3138 Asm->EmitInt8(0);
3139 Asm->EmitInt8(0);
3140 Asm->EmitInt8(0);
3141 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3142 }
Bill Wendling480ff322009-05-20 23:21:38 +00003143}
3144
Devang Patel930143b2009-11-21 02:48:08 +00003145/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00003146///
Devang Patel930143b2009-11-21 02:48:08 +00003147void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00003148 // Check to see if it is worth the effort.
3149 if (!Abbreviations.empty()) {
3150 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003151 Asm->OutStreamer.SwitchSection(
3152 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003153
Chris Lattnera179b522010-04-04 19:25:43 +00003154 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003155
3156 // For each abbrevation.
3157 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3158 // Get abbreviation data
3159 const DIEAbbrev *Abbrev = Abbreviations[i];
3160
3161 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00003162 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00003163
3164 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003165 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00003166 }
3167
3168 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00003169 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00003170
Chris Lattnera179b522010-04-04 19:25:43 +00003171 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003172 }
3173}
3174
Devang Patel930143b2009-11-21 02:48:08 +00003175/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00003176/// the line matrix.
3177///
Devang Patel930143b2009-11-21 02:48:08 +00003178void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00003179 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00003180 Asm->OutStreamer.AddComment("Extended Op");
3181 Asm->EmitInt8(0);
3182
3183 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003184 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00003185 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3186 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3187
3188 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00003189
Chris Lattnera179b522010-04-04 19:25:43 +00003190 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003191 Asm->getTargetData().getPointerSize(),
3192 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003193
3194 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00003195 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3196 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003197 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00003198 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003199}
3200
Devang Patel930143b2009-11-21 02:48:08 +00003201/// emitDebugLines - Emit source line information.
Bill Wendling480ff322009-05-20 23:21:38 +00003202///
Devang Patel930143b2009-11-21 02:48:08 +00003203void DwarfDebug::emitDebugLines() {
Bill Wendling480ff322009-05-20 23:21:38 +00003204 // If the target is using .loc/.file, the assembler will be emitting the
3205 // .debug_line table automatically.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003206 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling480ff322009-05-20 23:21:38 +00003207 return;
3208
3209 // Minimum line delta, thus ranging from -10..(255-10).
3210 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
3211 // Maximum line delta, thus ranging from -10..(255-10).
3212 const int MaxLineDelta = 255 + MinLineDelta;
3213
3214 // Start the dwarf line section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003215 Asm->OutStreamer.SwitchSection(
3216 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003217
3218 // Construct the section header.
Devang Patel24bc1b52010-06-29 20:17:53 +00003219 CurrentLineSectionSym = Asm->GetTempSymbol("section_line_begin");
3220 Asm->OutStreamer.EmitLabel(CurrentLineSectionSym);
Chris Lattner566cae92010-03-09 23:52:58 +00003221 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003222 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
3223 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnera179b522010-04-04 19:25:43 +00003224 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003225
Chris Lattner566cae92010-03-09 23:52:58 +00003226 Asm->OutStreamer.AddComment("DWARF version number");
3227 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling480ff322009-05-20 23:21:38 +00003228
Chris Lattner566cae92010-03-09 23:52:58 +00003229 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003230 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
3231 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnera179b522010-04-04 19:25:43 +00003232 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003233
Chris Lattner566cae92010-03-09 23:52:58 +00003234 Asm->OutStreamer.AddComment("Minimum Instruction Length");
3235 Asm->EmitInt8(1);
3236 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
3237 Asm->EmitInt8(1);
3238 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
3239 Asm->EmitInt8(MinLineDelta);
3240 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3241 Asm->EmitInt8(MaxLineDelta);
3242 Asm->OutStreamer.AddComment("Special Opcode Base");
3243 Asm->EmitInt8(-MinLineDelta);
Bill Wendling480ff322009-05-20 23:21:38 +00003244
3245 // Line number standard opcode encodings argument count
Chris Lattner566cae92010-03-09 23:52:58 +00003246 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3247 Asm->EmitInt8(0);
3248 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3249 Asm->EmitInt8(1);
3250 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3251 Asm->EmitInt8(1);
3252 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3253 Asm->EmitInt8(1);
3254 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3255 Asm->EmitInt8(1);
3256 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3257 Asm->EmitInt8(0);
3258 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3259 Asm->EmitInt8(0);
3260 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3261 Asm->EmitInt8(0);
3262 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3263 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003264
3265 // Emit directories.
3266 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003267 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner7bde8c02010-04-04 18:52:31 +00003268 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003269 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling480ff322009-05-20 23:21:38 +00003270 }
3271
Chris Lattner566cae92010-03-09 23:52:58 +00003272 Asm->OutStreamer.AddComment("End of directories");
3273 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003274
3275 // Emit files.
3276 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3277 // Remember source id starts at 1.
3278 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003279 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner7bde8c02010-04-04 18:52:31 +00003280 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003281 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
3282
Chris Lattner9efd1182010-04-04 19:09:29 +00003283 Asm->EmitULEB128(Id.first, "Directory #");
3284 Asm->EmitULEB128(0, "Mod date");
3285 Asm->EmitULEB128(0, "File size");
Bill Wendling480ff322009-05-20 23:21:38 +00003286 }
3287
Chris Lattner566cae92010-03-09 23:52:58 +00003288 Asm->OutStreamer.AddComment("End of files");
3289 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003290
Chris Lattnera179b522010-04-04 19:25:43 +00003291 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003292
3293 // A sequence for each text section.
3294 unsigned SecSrcLinesSize = SectionSourceLines.size();
3295
3296 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3297 // Isolate current sections line info.
3298 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3299
Bill Wendling480ff322009-05-20 23:21:38 +00003300 // Dwarf assumes we start with first line of first source file.
3301 unsigned Source = 1;
3302 unsigned Line = 1;
3303
3304 // Construct rows of the address, source, line, column matrix.
3305 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3306 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattnerb4666f42010-03-14 08:15:55 +00003307 MCSymbol *Label = LineInfo.getLabel();
Chris Lattneree95d4c2010-03-14 02:20:58 +00003308 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling480ff322009-05-20 23:21:38 +00003309
Chris Lattner3d72a672010-03-09 23:38:23 +00003310 if (Asm->isVerbose()) {
Chris Lattner7e998b72010-03-10 01:04:13 +00003311 std::pair<unsigned, unsigned> SrcID =
Bill Wendling480ff322009-05-20 23:21:38 +00003312 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner7e998b72010-03-10 01:04:13 +00003313 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattnera26fbe42010-03-10 02:29:31 +00003314 "/" +
3315 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner7e998b72010-03-10 01:04:13 +00003316 ":" + Twine(LineInfo.getLine()));
Bill Wendling480ff322009-05-20 23:21:38 +00003317 }
3318
3319 // Define the line address.
Chris Lattner566cae92010-03-09 23:52:58 +00003320 Asm->OutStreamer.AddComment("Extended Op");
3321 Asm->EmitInt8(0);
3322 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003323 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00003324
3325 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3326 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3327
3328 Asm->OutStreamer.AddComment("Location label");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003329 Asm->OutStreamer.EmitSymbolValue(Label,
3330 Asm->getTargetData().getPointerSize(),
Chris Lattneree95d4c2010-03-14 02:20:58 +00003331 0/*AddrSpace*/);
Chris Lattnerb245dfb2010-03-10 01:17:49 +00003332
Bill Wendling480ff322009-05-20 23:21:38 +00003333 // If change of source, then switch to the new source.
3334 if (Source != LineInfo.getSourceID()) {
3335 Source = LineInfo.getSourceID();
Chris Lattner566cae92010-03-09 23:52:58 +00003336 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3337 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner9efd1182010-04-04 19:09:29 +00003338 Asm->EmitULEB128(Source, "New Source");
Bill Wendling480ff322009-05-20 23:21:38 +00003339 }
3340
3341 // If change of line.
3342 if (Line != LineInfo.getLine()) {
3343 // Determine offset.
3344 int Offset = LineInfo.getLine() - Line;
3345 int Delta = Offset - MinLineDelta;
3346
3347 // Update line.
3348 Line = LineInfo.getLine();
3349
3350 // If delta is small enough and in range...
3351 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3352 // ... then use fast opcode.
Chris Lattner566cae92010-03-09 23:52:58 +00003353 Asm->OutStreamer.AddComment("Line Delta");
3354 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling480ff322009-05-20 23:21:38 +00003355 } else {
3356 // ... otherwise use long hand.
Chris Lattner566cae92010-03-09 23:52:58 +00003357 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling480ff322009-05-20 23:21:38 +00003358 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner9efd1182010-04-04 19:09:29 +00003359 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner566cae92010-03-09 23:52:58 +00003360 Asm->OutStreamer.AddComment("DW_LNS_copy");
3361 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling480ff322009-05-20 23:21:38 +00003362 }
3363 } else {
3364 // Copy the previous row (different address or source)
Chris Lattner566cae92010-03-09 23:52:58 +00003365 Asm->OutStreamer.AddComment("DW_LNS_copy");
3366 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling480ff322009-05-20 23:21:38 +00003367 }
3368 }
3369
Devang Patel930143b2009-11-21 02:48:08 +00003370 emitEndOfLineMatrix(j + 1);
Bill Wendling480ff322009-05-20 23:21:38 +00003371 }
3372
3373 if (SecSrcLinesSize == 0)
3374 // Because we're emitting a debug_line section, we still need a line
3375 // table. The linker and friends expect it to exist. If there's nothing to
3376 // put into it, emit an empty table.
Devang Patel930143b2009-11-21 02:48:08 +00003377 emitEndOfLineMatrix(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003378
Chris Lattnera179b522010-04-04 19:25:43 +00003379 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003380}
3381
Devang Patel930143b2009-11-21 02:48:08 +00003382/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling480ff322009-05-20 23:21:38 +00003383///
Devang Patel930143b2009-11-21 02:48:08 +00003384void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003385 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003386 return;
3387
Chris Lattner3a383cb2010-04-05 00:13:49 +00003388 int stackGrowth = Asm->getTargetData().getPointerSize();
3389 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3390 TargetFrameInfo::StackGrowsDown)
3391 stackGrowth *= -1;
Bill Wendling480ff322009-05-20 23:21:38 +00003392
3393 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003394 Asm->OutStreamer.SwitchSection(
3395 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003396
Chris Lattnera179b522010-04-04 19:25:43 +00003397 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner566cae92010-03-09 23:52:58 +00003398 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003399 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3400 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003401
Chris Lattnera179b522010-04-04 19:25:43 +00003402 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner566cae92010-03-09 23:52:58 +00003403 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling480ff322009-05-20 23:21:38 +00003404 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner566cae92010-03-09 23:52:58 +00003405 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling480ff322009-05-20 23:21:38 +00003406 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner566cae92010-03-09 23:52:58 +00003407 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003408 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner9efd1182010-04-04 19:09:29 +00003409 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3410 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner566cae92010-03-09 23:52:58 +00003411 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003412 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling480ff322009-05-20 23:21:38 +00003413 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling480ff322009-05-20 23:21:38 +00003414
3415 std::vector<MachineMove> Moves;
3416 RI->getInitialFrameState(Moves);
3417
Chris Lattneraabc6042010-04-04 23:41:46 +00003418 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003419
Chris Lattner9e06e532010-04-28 01:05:45 +00003420 Asm->EmitAlignment(2);
Chris Lattnera179b522010-04-04 19:25:43 +00003421 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003422}
3423
Devang Patel930143b2009-11-21 02:48:08 +00003424/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling480ff322009-05-20 23:21:38 +00003425/// section.
Chris Lattner2c3f4782010-03-13 07:26:18 +00003426void DwarfDebug::
3427emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003428 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003429 return;
3430
3431 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003432 Asm->OutStreamer.SwitchSection(
3433 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003434
Chris Lattner566cae92010-03-09 23:52:58 +00003435 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner2c3f4782010-03-13 07:26:18 +00003436 MCSymbol *DebugFrameBegin =
Chris Lattnera179b522010-04-04 19:25:43 +00003437 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003438 MCSymbol *DebugFrameEnd =
Chris Lattnera179b522010-04-04 19:25:43 +00003439 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerf1429f12010-04-04 19:58:12 +00003440 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003441
Chris Lattner2c3f4782010-03-13 07:26:18 +00003442 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling480ff322009-05-20 23:21:38 +00003443
Chris Lattner566cae92010-03-09 23:52:58 +00003444 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattner70a4fce2010-04-04 23:25:33 +00003445 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3446 DwarfFrameSectionSym);
Bill Wendling480ff322009-05-20 23:21:38 +00003447
Chris Lattner566cae92010-03-09 23:52:58 +00003448 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnera179b522010-04-04 19:25:43 +00003449 MCSymbol *FuncBeginSym =
3450 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner8811e122010-03-13 07:40:56 +00003451 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattner3a383cb2010-04-05 00:13:49 +00003452 Asm->getTargetData().getPointerSize(),
3453 0/*AddrSpace*/);
Chris Lattnerb245dfb2010-03-10 01:17:49 +00003454
3455
Chris Lattner566cae92010-03-09 23:52:58 +00003456 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003457 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003458 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003459
Chris Lattneraabc6042010-04-04 23:41:46 +00003460 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003461
Chris Lattner9e06e532010-04-28 01:05:45 +00003462 Asm->EmitAlignment(2);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003463 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling480ff322009-05-20 23:21:38 +00003464}
3465
Devang Patel9ccfb642009-12-09 18:24:21 +00003466/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3467///
3468void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003469 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3470 E = CUMap.end(); I != E; ++I) {
3471 CompileUnit *TheCU = I->second;
3472 // Start the dwarf pubnames section.
3473 Asm->OutStreamer.SwitchSection(
3474 Asm->getObjFileLowering().getDwarfPubNamesSection());
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003475
Devang Patel1a0df9a2010-05-10 22:49:55 +00003476 Asm->OutStreamer.AddComment("Length of Public Names Info");
3477 Asm->EmitLabelDifference(
3478 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3479 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
3480
3481 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3482 TheCU->getID()));
3483
3484 Asm->OutStreamer.AddComment("DWARF Version");
3485 Asm->EmitInt16(dwarf::DWARF_VERSION);
3486
3487 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3488 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3489 DwarfInfoSectionSym);
3490
3491 Asm->OutStreamer.AddComment("Compilation Unit Length");
3492 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3493 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3494 4);
3495
3496 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3497 for (StringMap<DIE*>::const_iterator
3498 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3499 const char *Name = GI->getKeyData();
3500 DIE *Entity = GI->second;
3501
3502 Asm->OutStreamer.AddComment("DIE offset");
3503 Asm->EmitInt32(Entity->getOffset());
3504
3505 if (Asm->isVerbose())
3506 Asm->OutStreamer.AddComment("External Name");
3507 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3508 }
3509
3510 Asm->OutStreamer.AddComment("End Mark");
3511 Asm->EmitInt32(0);
3512 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3513 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00003514 }
Bill Wendling480ff322009-05-20 23:21:38 +00003515}
3516
Devang Patel04d2f2d2009-11-24 01:14:22 +00003517void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003518 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3519 E = CUMap.end(); I != E; ++I) {
3520 CompileUnit *TheCU = I->second;
3521 // Start the dwarf pubnames section.
3522 Asm->OutStreamer.SwitchSection(
3523 Asm->getObjFileLowering().getDwarfPubTypesSection());
3524 Asm->OutStreamer.AddComment("Length of Public Types Info");
3525 Asm->EmitLabelDifference(
3526 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3527 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003528
Devang Patel1a0df9a2010-05-10 22:49:55 +00003529 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3530 TheCU->getID()));
3531
3532 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3533 Asm->EmitInt16(dwarf::DWARF_VERSION);
3534
3535 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3536 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3537 DwarfInfoSectionSym);
3538
3539 Asm->OutStreamer.AddComment("Compilation Unit Length");
3540 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3541 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3542 4);
3543
3544 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3545 for (StringMap<DIE*>::const_iterator
3546 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3547 const char *Name = GI->getKeyData();
3548 DIE * Entity = GI->second;
3549
3550 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3551 Asm->EmitInt32(Entity->getOffset());
3552
3553 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3554 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3555 }
3556
3557 Asm->OutStreamer.AddComment("End Mark");
3558 Asm->EmitInt32(0);
3559 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3560 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00003561 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00003562}
3563
Devang Patel930143b2009-11-21 02:48:08 +00003564/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00003565///
Devang Patel930143b2009-11-21 02:48:08 +00003566void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00003567 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00003568 if (StringPool.empty()) return;
3569
3570 // Start the dwarf str section.
3571 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003572 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003573
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003574 // Get all of the string pool entries and put them in an array by their ID so
3575 // we can sort them.
3576 SmallVector<std::pair<unsigned,
3577 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3578
3579 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3580 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3581 Entries.push_back(std::make_pair(I->second.second, &*I));
3582
3583 array_pod_sort(Entries.begin(), Entries.end());
3584
3585 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00003586 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003587 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner3d72a672010-03-09 23:38:23 +00003588
3589 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003590 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003591 }
3592}
3593
Devang Patel930143b2009-11-21 02:48:08 +00003594/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00003595///
Devang Patel930143b2009-11-21 02:48:08 +00003596void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003597 if (DotDebugLocEntries.empty())
3598 return;
3599
Bill Wendling480ff322009-05-20 23:21:38 +00003600 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003601 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00003602 Asm->getObjFileLowering().getDwarfLocSection());
3603 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003604 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3605 unsigned index = 1;
Devang Patel30265c42010-07-07 20:12:52 +00003606 for (SmallVector<DotDebugLocEntry, 4>::iterator
3607 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
3608 I != E; ++I, ++index) {
Devang Patel9fc11702010-05-25 23:40:22 +00003609 DotDebugLocEntry Entry = *I;
Devang Patel9fc11702010-05-25 23:40:22 +00003610 if (Entry.isEmpty()) {
3611 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3612 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003613 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00003614 } else {
3615 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3616 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3617 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3618 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
3619 if (Reg < 32) {
3620 Asm->OutStreamer.AddComment("Loc expr size");
3621 Asm->EmitInt16(1);
3622 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3623 } else {
3624 Asm->OutStreamer.AddComment("Loc expr size");
3625 Asm->EmitInt16(1+MCAsmInfo::getULEB128Size(Reg));
3626 Asm->EmitInt8(dwarf::DW_OP_regx);
3627 Asm->EmitULEB128(Reg);
3628 }
3629 }
3630 }
Bill Wendling480ff322009-05-20 23:21:38 +00003631}
3632
3633/// EmitDebugARanges - Emit visible names into a debug aranges section.
3634///
3635void DwarfDebug::EmitDebugARanges() {
3636 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003637 Asm->OutStreamer.SwitchSection(
3638 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003639}
3640
Devang Patel930143b2009-11-21 02:48:08 +00003641/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00003642///
Devang Patel930143b2009-11-21 02:48:08 +00003643void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00003644 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003645 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00003646 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00003647 unsigned char Size = Asm->getTargetData().getPointerSize();
3648 for (SmallVector<const MCSymbol *, 8>::iterator
3649 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3650 I != E; ++I) {
3651 if (*I)
3652 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00003653 else
Devang Patel6c74a872010-04-27 19:46:33 +00003654 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00003655 }
Bill Wendling480ff322009-05-20 23:21:38 +00003656}
3657
Devang Patel930143b2009-11-21 02:48:08 +00003658/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00003659///
Devang Patel930143b2009-11-21 02:48:08 +00003660void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00003661 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00003662 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00003663 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003664 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003665 }
3666}
3667
Devang Patel930143b2009-11-21 02:48:08 +00003668/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00003669/// Section Header:
3670/// 1. length of section
3671/// 2. Dwarf version number
3672/// 3. address size.
3673///
3674/// Entries (one "entry" for each function that was inlined):
3675///
3676/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3677/// otherwise offset into __debug_str for regular function name.
3678/// 2. offset into __debug_str section for regular function name.
3679/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3680/// instances for the function.
3681///
3682/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3683/// inlined instance; the die_offset points to the inlined_subroutine die in the
3684/// __debug_info section, and the low_pc is the starting address for the
3685/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00003686void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003687 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003688 return;
3689
Devang Patel1a0df9a2010-05-10 22:49:55 +00003690 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00003691 return;
3692
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003693 Asm->OutStreamer.SwitchSection(
3694 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003695
Chris Lattner566cae92010-03-09 23:52:58 +00003696 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003697 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3698 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003699
Chris Lattnera179b522010-04-04 19:25:43 +00003700 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003701
Chris Lattner566cae92010-03-09 23:52:58 +00003702 Asm->OutStreamer.AddComment("Dwarf Version");
3703 Asm->EmitInt16(dwarf::DWARF_VERSION);
3704 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003705 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003706
Devang Patel32cc43c2010-05-07 20:54:48 +00003707 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003708 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00003709
Devang Patel32cc43c2010-05-07 20:54:48 +00003710 const MDNode *Node = *I;
3711 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00003712 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003713 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00003714 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00003715 StringRef LName = SP.getLinkageName();
3716 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00003717
Chris Lattner566cae92010-03-09 23:52:58 +00003718 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003719 if (LName.empty()) {
3720 Asm->OutStreamer.EmitBytes(Name, 0);
3721 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3722 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00003723 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3724 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003725
Chris Lattner566cae92010-03-09 23:52:58 +00003726 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00003727 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00003728 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00003729
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003730 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00003731 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00003732 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00003733 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003734
Chris Lattner7bde8c02010-04-04 18:52:31 +00003735 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003736 Asm->OutStreamer.EmitSymbolValue(LI->first,
3737 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00003738 }
3739 }
3740
Chris Lattnera179b522010-04-04 19:25:43 +00003741 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003742}