blob: f9c11d5824f9ebce8cd8c003f9f15fa033c6aa96 [file] [log] [blame]
Bill Wendlingb12b3d72009-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 Lattner2ab0c442010-03-09 00:39:24 +000013
Devang Patel15e723d2009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner855b6bb2010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling989a27e2010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greened87baff2009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner73266f92009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner31a54742010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner017c7f92010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel91474172010-04-19 19:14:02 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner855b6bb2010-04-05 05:24:55 +000032#include "llvm/Analysis/DebugInfo.h"
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +000033#include "llvm/ADT/STLExtras.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000034#include "llvm/ADT/StringExtras.h"
Devang Patelce7bf012010-04-27 19:46:33 +000035#include "llvm/Support/CommandLine.h"
Daniel Dunbarc74255d2009-10-13 06:47:08 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
Devang Patelae89d3b2010-01-26 21:39:14 +000038#include "llvm/Support/ValueHandle.h"
Chris Lattnerad653482010-01-22 22:09:00 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000040#include "llvm/Support/Timer.h"
41#include "llvm/System/Path.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000042using namespace llvm;
43
Devang Patelce7bf012010-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
47static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
48 cl::desc("Disable debug info printing"));
49
Dan Gohman77c565e2010-05-07 01:08:53 +000050static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
51 cl::desc("Make an absense of debug location information explicit."),
52 cl::init(false));
53
Bill Wendlingce3c6252010-04-07 09:28:04 +000054namespace {
55 const char *DWARFGroupName = "DWARF Emission";
56 const char *DbgTimerName = "DWARF Debug Writer";
57} // end anonymous namespace
58
Bill Wendlingb12b3d72009-05-15 09:23:25 +000059//===----------------------------------------------------------------------===//
60
61/// Configuration values for initial hash set sizes (log2).
62///
Bill Wendlingb12b3d72009-05-15 09:23:25 +000063static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-05-15 09:23:25 +000064
65namespace llvm {
66
67//===----------------------------------------------------------------------===//
68/// CompileUnit - This dwarf writer support class manages information associate
69/// with a source file.
Nick Lewyckyee68f452009-11-17 08:11:44 +000070class CompileUnit {
Bill Wendlingb12b3d72009-05-15 09:23:25 +000071 /// ID - File identifier for source.
72 ///
73 unsigned ID;
74
75 /// Die - Compile unit debug information entry.
76 ///
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +000077 const OwningPtr<DIE> CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000078
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000079 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel1233f912009-11-21 00:31:03 +000080 DIE *IndexTyDie;
81
Bill Wendlingb12b3d72009-05-15 09:23:25 +000082 /// GVToDieMap - Tracks the mapping of unit level debug informaton
83 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000084 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel9c371c62010-05-07 20:54:48 +000085 DenseMap<const MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000086
87 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
88 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel15e723d2009-08-28 23:24:31 +000089 /// FIXME : Rename
Devang Patel9c371c62010-05-07 20:54:48 +000090 DenseMap<const MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000091
92 /// Globals - A map of globally visible named entities for this unit.
93 ///
94 StringMap<DIE*> Globals;
95
Devang Patelec13b4f2009-11-24 01:14:22 +000096 /// GlobalTypes - A map of globally visible types for this unit.
97 ///
98 StringMap<DIE*> GlobalTypes;
99
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000100public:
101 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +0000102 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000103
104 // Accessors.
Devang Patelec13b4f2009-11-24 01:14:22 +0000105 unsigned getID() const { return ID; }
Jeffrey Yasskinc3545712010-03-11 18:29:55 +0000106 DIE* getCUDie() const { return CUDie.get(); }
Devang Patelec13b4f2009-11-24 01:14:22 +0000107 const StringMap<DIE*> &getGlobals() const { return Globals; }
108 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000109
110 /// hasContent - Return true if this compile unit has something to write out.
111 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000112 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000113
Devang Patelc50078e2009-11-21 02:48:08 +0000114 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000115 ///
Benjamin Kramer648940b2010-03-31 20:15:45 +0000116 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000117
Devang Patelec13b4f2009-11-24 01:14:22 +0000118 /// addGlobalType - Add a new global type to the compile unit.
119 ///
Benjamin Kramer648940b2010-03-31 20:15:45 +0000120 void addGlobalType(StringRef Name, DIE *Die) {
Devang Patelec13b4f2009-11-24 01:14:22 +0000121 GlobalTypes[Name] = Die;
122 }
123
Devang Pateld90672c2009-11-20 21:37:22 +0000124 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000125 /// specified debug variable.
Devang Patel9c371c62010-05-07 20:54:48 +0000126 DIE *getDIE(const MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +0000127
Devang Pateld90672c2009-11-20 21:37:22 +0000128 /// insertDIE - Insert DIE into the map.
Devang Patel9c371c62010-05-07 20:54:48 +0000129 void insertDIE(const MDNode *N, DIE *D) {
Devang Pateld90672c2009-11-20 21:37:22 +0000130 GVToDieMap.insert(std::make_pair(N, D));
131 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000132
Devang Pateld90672c2009-11-20 21:37:22 +0000133 /// getDIEEntry - Returns the debug information entry for the speciefied
134 /// debug variable.
Devang Patel9c371c62010-05-07 20:54:48 +0000135 DIEEntry *getDIEEntry(const MDNode *N) {
136 DenseMap<const MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel8287d662009-12-15 19:16:48 +0000137 if (I == GVToDIEEntryMap.end())
138 return NULL;
139 return I->second;
140 }
Devang Pateld90672c2009-11-20 21:37:22 +0000141
142 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patel9c371c62010-05-07 20:54:48 +0000143 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Pateld90672c2009-11-20 21:37:22 +0000144 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000145 }
146
Devang Patelc50078e2009-11-21 02:48:08 +0000147 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000148 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000149 void addDie(DIE *Buffer) {
150 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000151 }
Devang Patel1233f912009-11-21 00:31:03 +0000152
153 // getIndexTyDie - Get an anonymous type for index type.
154 DIE *getIndexTyDie() {
155 return IndexTyDie;
156 }
157
Jim Grosbachb23f2422009-11-22 19:20:36 +0000158 // setIndexTyDie - Set D as anonymous type for index which can be reused
159 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000160 void setIndexTyDie(DIE *D) {
161 IndexTyDie = D;
162 }
163
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000164};
165
166//===----------------------------------------------------------------------===//
167/// DbgVariable - This class is used to track local variable information.
168///
Devang Patel4cb32c32009-11-16 21:53:40 +0000169class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000170 DIVariable Var; // Variable Descriptor.
171 unsigned FrameIndex; // Variable frame index.
Devang Patel14da02f2010-03-15 18:33:46 +0000172 const MachineInstr *DbgValueMInsn; // DBG_VALUE
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000173 // DbgValueLabel - DBG_VALUE is effective from this label.
174 MCSymbol *DbgValueLabel;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000175 DbgVariable *const AbstractVar; // Abstract variable for this variable.
Devang Patel90a0fe32009-11-10 23:06:00 +0000176 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000177public:
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000178 // AbsVar may be NULL.
179 DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar)
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000180 : Var(V), FrameIndex(I), DbgValueMInsn(0),
181 DbgValueLabel(0), AbstractVar(AbsVar), TheDIE(0) {}
Devang Patel14da02f2010-03-15 18:33:46 +0000182 DbgVariable(DIVariable V, const MachineInstr *MI, DbgVariable *AbsVar)
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000183 : Var(V), FrameIndex(0), DbgValueMInsn(MI), DbgValueLabel(0),
184 AbstractVar(AbsVar), TheDIE(0)
Devang Patel14da02f2010-03-15 18:33:46 +0000185 {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000186
187 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000188 DIVariable getVariable() const { return Var; }
189 unsigned getFrameIndex() const { return FrameIndex; }
Devang Patel14da02f2010-03-15 18:33:46 +0000190 const MachineInstr *getDbgValue() const { return DbgValueMInsn; }
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000191 MCSymbol *getDbgValueLabel() const { return DbgValueLabel; }
192 void setDbgValueLabel(MCSymbol *L) { DbgValueLabel = L; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000193 DbgVariable *getAbstractVariable() const { return AbstractVar; }
194 void setDIE(DIE *D) { TheDIE = D; }
195 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000196};
197
198//===----------------------------------------------------------------------===//
Devang Patelce7bf012010-04-27 19:46:33 +0000199/// DbgRange - This is used to track range of instructions with identical
200/// debug info scope.
201///
202typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
203
204//===----------------------------------------------------------------------===//
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000205/// DbgScope - This class is used to track scope information.
206///
Devang Patel4cb32c32009-11-16 21:53:40 +0000207class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000208 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000209 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelae89d3b2010-01-26 21:39:14 +0000210 // Location at which this scope is inlined.
Devang Patel9c371c62010-05-07 20:54:48 +0000211 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel90a0fe32009-11-10 23:06:00 +0000212 bool AbstractScope; // Abstract Scope
Devang Patel90ecd192009-10-01 18:25:23 +0000213 const MachineInstr *LastInsn; // Last instruction of this scope.
214 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patelce7bf012010-04-27 19:46:33 +0000215 unsigned DFSIn, DFSOut;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000216 // Scopes defined in scope. Contents not owned.
217 SmallVector<DbgScope *, 4> Scopes;
218 // Variables declared in scope. Contents owned.
219 SmallVector<DbgVariable *, 8> Variables;
Devang Patelce7bf012010-04-27 19:46:33 +0000220 SmallVector<DbgRange, 4> Ranges;
Owen Anderson696d4862009-06-24 22:53:20 +0000221 // Private state for dump()
222 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000223public:
Devang Patel9c371c62010-05-07 20:54:48 +0000224 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000225 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patelce7bf012010-04-27 19:46:33 +0000226 LastInsn(0), FirstInsn(0),
227 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000228 virtual ~DbgScope();
229
230 // Accessors.
231 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000232 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000233 DIDescriptor getDesc() const { return Desc; }
Devang Patel9c371c62010-05-07 20:54:48 +0000234 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
235 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000236 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
237 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patelce7bf012010-04-27 19:46:33 +0000238 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
239
240 /// openInsnRange - This scope covers instruction range starting from MI.
241 void openInsnRange(const MachineInstr *MI) {
242 if (!FirstInsn)
243 FirstInsn = MI;
244
245 if (Parent)
246 Parent->openInsnRange(MI);
247 }
248
249 /// extendInsnRange - Extend the current instruction range covered by
250 /// this scope.
251 void extendInsnRange(const MachineInstr *MI) {
252 assert (FirstInsn && "MI Range is not open!");
253 LastInsn = MI;
254 if (Parent)
255 Parent->extendInsnRange(MI);
256 }
257
258 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
259 /// until now. This is used when a new scope is encountered while walking
260 /// machine instructions.
261 void closeInsnRange(DbgScope *NewScope = NULL) {
262 assert (LastInsn && "Last insn missing!");
263 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
264 FirstInsn = NULL;
265 LastInsn = NULL;
266 // If Parent dominates NewScope then do not close Parent's instruction
267 // range.
268 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
269 Parent->closeInsnRange(NewScope);
270 }
271
Devang Patel90a0fe32009-11-10 23:06:00 +0000272 void setAbstractScope() { AbstractScope = true; }
273 bool isAbstractScope() const { return AbstractScope; }
Devang Patelce7bf012010-04-27 19:46:33 +0000274
275 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
276 unsigned getDFSOut() const { return DFSOut; }
277 void setDFSOut(unsigned O) { DFSOut = O; }
278 unsigned getDFSIn() const { return DFSIn; }
279 void setDFSIn(unsigned I) { DFSIn = I; }
280 bool dominates(const DbgScope *S) {
281 if (S == this)
282 return true;
283 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
284 return true;
285 return false;
286 }
Devang Patel90a0fe32009-11-10 23:06:00 +0000287
Devang Patelc50078e2009-11-21 02:48:08 +0000288 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000289 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000290 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000291
Devang Patelc50078e2009-11-21 02:48:08 +0000292 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000293 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000294 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000295
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000296#ifndef NDEBUG
297 void dump() const;
298#endif
299};
Devang Patelce7bf012010-04-27 19:46:33 +0000300
Chris Lattner8b9430c2010-04-05 04:09:20 +0000301} // end llvm namespace
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000302
303#ifndef NDEBUG
304void DbgScope::dump() const {
David Greenedbc06132009-12-24 00:31:35 +0000305 raw_ostream &err = dbgs();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000306 err.indent(IndentLevel);
Devang Patel9c371c62010-05-07 20:54:48 +0000307 const MDNode *N = Desc;
Devang Patel90a0fe32009-11-10 23:06:00 +0000308 N->dump();
Devang Patel90a0fe32009-11-10 23:06:00 +0000309 if (AbstractScope)
310 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000311
312 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000313 if (!Scopes.empty())
314 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000315 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
316 if (Scopes[i] != this)
317 Scopes[i]->dump();
318
319 IndentLevel -= 2;
320}
321#endif
322
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000323DbgScope::~DbgScope() {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000324 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
325 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000326}
327
Chris Lattnerec9a1f42010-04-05 05:11:15 +0000328DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000329 : Asm(A), MMI(Asm->MMI), ModuleCU(0),
Chris Lattner2cb53302010-04-05 03:52:55 +0000330 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel146c6f72010-04-16 23:33:45 +0000331 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattner0f093f82010-03-13 02:17:42 +0000332 NextStringPoolNumber = 0;
Chris Lattner66143d22010-04-04 22:59:04 +0000333
Chris Lattner094932e2010-04-04 23:10:38 +0000334 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
335 DwarfStrSectionSym = TextSectionSym = 0;
Devang Patelce7bf012010-04-27 19:46:33 +0000336 DwarfDebugRangeSectionSym = 0;
337 FunctionBeginSym = 0;
Edwin Törökc669cb62010-04-07 10:44:46 +0000338 if (TimePassesIsEnabled) {
339 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
340 beginModule(M);
341 } else {
342 beginModule(M);
343 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000344}
345DwarfDebug::~DwarfDebug() {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000346 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
347 DIEBlocks[j]->~DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000348}
349
Chris Lattner0f093f82010-03-13 02:17:42 +0000350MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
351 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
352 if (Entry.first) return Entry.first;
353
354 Entry.second = NextStringPoolNumber++;
Chris Lattnerb93558d2010-04-04 19:25:43 +0000355 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattner0f093f82010-03-13 02:17:42 +0000356}
357
358
Devang Patelc50078e2009-11-21 02:48:08 +0000359/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000360///
Devang Patelc50078e2009-11-21 02:48:08 +0000361void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000362 // Profile the node so that we can make it unique.
363 FoldingSetNodeID ID;
364 Abbrev.Profile(ID);
365
366 // Check the set for priors.
367 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
368
369 // If it's newly added.
370 if (InSet == &Abbrev) {
371 // Add to abbreviation list.
372 Abbreviations.push_back(&Abbrev);
373
374 // Assign the vector position + 1 as its number.
375 Abbrev.setNumber(Abbreviations.size());
376 } else {
377 // Assign existing abbreviation number.
378 Abbrev.setNumber(InSet->getNumber());
379 }
380}
381
Devang Patelc50078e2009-11-21 02:48:08 +0000382/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000383/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000384DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000385 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000386 return Value;
387}
388
Devang Patelc50078e2009-11-21 02:48:08 +0000389/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000390///
Devang Patelc50078e2009-11-21 02:48:08 +0000391void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000392 unsigned Form, uint64_t Integer) {
393 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000394 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000395 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000396}
397
Devang Patelc50078e2009-11-21 02:48:08 +0000398/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000399///
Devang Patelc50078e2009-11-21 02:48:08 +0000400void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000401 unsigned Form, int64_t Integer) {
402 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000403 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000404 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000405}
406
Devang Pateldf0f2152009-12-02 15:25:16 +0000407/// addString - Add a string attribute data and value. DIEString only
408/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000409void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnercc564642010-01-23 03:11:46 +0000410 StringRef String) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000411 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000412 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000413}
414
Devang Patelc50078e2009-11-21 02:48:08 +0000415/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000416///
Devang Patelc50078e2009-11-21 02:48:08 +0000417void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000418 const MCSymbol *Label) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000419 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000420 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000421}
422
Devang Patelc50078e2009-11-21 02:48:08 +0000423/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000424///
Devang Patelc50078e2009-11-21 02:48:08 +0000425void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000426 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000427 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000428 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000429}
430
Chris Lattner855b6bb2010-04-05 05:24:55 +0000431/// addDIEEntry - Add a DIE attribute data and value.
432///
433void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
434 DIE *Entry) {
435 Die->addValue(Attribute, Form, createDIEEntry(Entry));
436}
437
438
Devang Patelc50078e2009-11-21 02:48:08 +0000439/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000440///
Devang Patelc50078e2009-11-21 02:48:08 +0000441void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000442 DIEBlock *Block) {
Chris Lattner352c8e22010-04-05 00:18:22 +0000443 Block->ComputeSize(Asm);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000444 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patelc50078e2009-11-21 02:48:08 +0000445 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000446}
447
Devang Patelc50078e2009-11-21 02:48:08 +0000448/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000449/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000450void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000451 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000452 if (!V->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000453 return;
454
455 unsigned Line = V->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000456 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
457 V->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000458 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000461}
462
Devang Patelc50078e2009-11-21 02:48:08 +0000463/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000464/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000465void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000466 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000467 if (!G->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000468 return;
469
470 unsigned Line = G->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000471 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
472 G->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000473 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000474 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
475 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000476}
Devang Patel318d70d2009-08-31 22:47:13 +0000477
Devang Patelc50078e2009-11-21 02:48:08 +0000478/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000479/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000480void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-08-31 22:47:13 +0000481 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000482 if (!SP->getCompileUnit().Verify())
Devang Patel318d70d2009-08-31 22:47:13 +0000483 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000484 // If the line number is 0, don't add it.
485 if (SP->getLineNumber() == 0)
486 return;
487
Devang Patel318d70d2009-08-31 22:47:13 +0000488 unsigned Line = SP->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000489 if (!SP->getContext().Verify())
490 return;
Devang Patele485a5d2010-03-24 21:30:35 +0000491 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
492 SP->getFilename());
Devang Patel318d70d2009-08-31 22:47:13 +0000493 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000494 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
495 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000496}
497
Devang Patelc50078e2009-11-21 02:48:08 +0000498/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000499/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000500void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000501 // If there is no compile unit specified, don't add a line #.
502 DICompileUnit CU = Ty->getCompileUnit();
Devang Patel9e592492010-03-08 20:52:55 +0000503 if (!CU.Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000504 return;
505
506 unsigned Line = Ty->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000507 if (!Ty->getContext().Verify())
508 return;
509 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
510 Ty->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000511 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000512 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
513 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000514}
515
Devang Patel8287d662009-12-15 19:16:48 +0000516/// addSourceLine - Add location information to specified debug information
517/// entry.
518void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
519 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000520 if (!NS->getCompileUnit().Verify())
Devang Patel8287d662009-12-15 19:16:48 +0000521 return;
522
523 unsigned Line = NS->getLineNumber();
524 StringRef FN = NS->getFilename();
525 StringRef Dir = NS->getDirectory();
526
527 unsigned FileID = GetOrCreateSourceID(Dir, FN);
528 assert(FileID && "Invalid file id");
529 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
530 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
531}
532
Caroline Tice248d5572009-08-31 21:19:37 +0000533/* Byref variables, in Blocks, are declared by the programmer as
534 "SomeType VarName;", but the compiler creates a
535 __Block_byref_x_VarName struct, and gives the variable VarName
536 either the struct, or a pointer to the struct, as its type. This
537 is necessary for various behind-the-scenes things the compiler
538 needs to do with by-reference variables in blocks.
539
540 However, as far as the original *programmer* is concerned, the
541 variable should still have type 'SomeType', as originally declared.
542
543 The following function dives into the __Block_byref_x_VarName
544 struct to find the original type of the variable. This will be
545 passed back to the code generating the type for the Debug
546 Information Entry for the variable 'VarName'. 'VarName' will then
547 have the original type 'SomeType' in its debug information.
548
549 The original type 'SomeType' will be the type of the field named
550 'VarName' inside the __Block_byref_x_VarName struct.
551
552 NOTE: In order for this to not completely fail on the debugger
553 side, the Debug Information Entry for the variable VarName needs to
554 have a DW_AT_location that tells the debugger how to unwind through
555 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000556 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000557
558/// Find the type the programmer originally declared the variable to be
559/// and return that type.
560///
Devang Patelc50078e2009-11-21 02:48:08 +0000561DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000562
563 DIType subType = Ty;
564 unsigned tag = Ty.getTag();
565
566 if (tag == dwarf::DW_TAG_pointer_type) {
Devang Patel19302aa2010-05-07 18:11:54 +0000567 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Tice248d5572009-08-31 21:19:37 +0000568 subType = DTy.getTypeDerivedFrom();
569 }
570
Devang Patel19302aa2010-05-07 18:11:54 +0000571 DICompositeType blockStruct = DICompositeType(subType);
Caroline Tice248d5572009-08-31 21:19:37 +0000572 DIArray Elements = blockStruct.getTypeArray();
573
Caroline Tice248d5572009-08-31 21:19:37 +0000574 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
575 DIDescriptor Element = Elements.getElement(i);
Devang Patel19302aa2010-05-07 18:11:54 +0000576 DIDerivedType DT = DIDerivedType(Element);
Devang Patel7f75bbe2009-11-25 17:36:49 +0000577 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000578 return (DT.getTypeDerivedFrom());
579 }
580
581 return Ty;
582}
583
Devang Patelc50078e2009-11-21 02:48:08 +0000584/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000585/// and generate the DWARF information necessary to find the actual variable
586/// given the extra address information encoded in the DIVariable, starting from
587/// the starting location. Add the DWARF information to the die.
588///
Devang Patelc50078e2009-11-21 02:48:08 +0000589void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000590 unsigned Attribute,
591 const MachineLocation &Location) {
592 const DIVariable &VD = DV->getVariable();
593 DIType Ty = VD.getType();
594
595 // Decode the original location, and use that as the start of the byref
596 // variable's location.
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000597 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000598 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000599 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000600
601 if (Location.isReg()) {
602 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000603 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000604 } else {
605 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000606 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
607 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000608 }
609 } else {
610 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000611 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000612 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000613 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
614 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000615 }
616
Devang Patelc50078e2009-11-21 02:48:08 +0000617 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000618 }
619
620 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
621 uint64_t Element = VD.getAddrElement(i);
622
623 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000624 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
625 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000626 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000627 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000628 } else llvm_unreachable("unknown DIFactory Opcode");
629 }
630
631 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000632 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000633}
634
Caroline Tice248d5572009-08-31 21:19:37 +0000635/* Byref variables, in Blocks, are declared by the programmer as "SomeType
636 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
637 gives the variable VarName either the struct, or a pointer to the struct, as
638 its type. This is necessary for various behind-the-scenes things the
639 compiler needs to do with by-reference variables in Blocks.
640
641 However, as far as the original *programmer* is concerned, the variable
642 should still have type 'SomeType', as originally declared.
643
Devang Patelc50078e2009-11-21 02:48:08 +0000644 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000645 struct to find the original type of the variable, which is then assigned to
646 the variable's Debug Information Entry as its real type. So far, so good.
647 However now the debugger will expect the variable VarName to have the type
648 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000649 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000650 pointers and struct to find the actual variable of type SomeType.
651
652 The following function does just that. We start by getting
653 the "normal" location for the variable. This will be the location
654 of either the struct __Block_byref_x_VarName or the pointer to the
655 struct __Block_byref_x_VarName.
656
657 The struct will look something like:
658
659 struct __Block_byref_x_VarName {
660 ... <various fields>
661 struct __Block_byref_x_VarName *forwarding;
662 ... <various other fields>
663 SomeType VarName;
664 ... <maybe more fields>
665 };
666
667 If we are given the struct directly (as our starting point) we
668 need to tell the debugger to:
669
670 1). Add the offset of the forwarding field.
671
Dan Gohmandf1a7ff2010-02-10 16:03:48 +0000672 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000673 struct to use (the real one may have been copied onto the heap).
674
675 3). Add the offset for the field VarName, to find the actual variable.
676
677 If we started with a pointer to the struct, then we need to
678 dereference that pointer first, before the other steps.
679 Translating this into DWARF ops, we will need to append the following
680 to the current location description for the variable:
681
682 DW_OP_deref -- optional, if we start with a pointer
683 DW_OP_plus_uconst <forward_fld_offset>
684 DW_OP_deref
685 DW_OP_plus_uconst <varName_fld_offset>
686
687 That is what this function does. */
688
Devang Patelc50078e2009-11-21 02:48:08 +0000689/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000690/// provided, and generate the DWARF information necessary to find the
691/// actual Block variable (navigating the Block struct) based on the
692/// starting location. Add the DWARF information to the die. For
693/// more information, read large comment just above here.
694///
Devang Patelc50078e2009-11-21 02:48:08 +0000695void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000696 unsigned Attribute,
697 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000698 const DIVariable &VD = DV->getVariable();
699 DIType Ty = VD.getType();
700 DIType TmpTy = Ty;
701 unsigned Tag = Ty.getTag();
702 bool isPointer = false;
703
Devang Patel7f75bbe2009-11-25 17:36:49 +0000704 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000705
706 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel19302aa2010-05-07 18:11:54 +0000707 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Tice248d5572009-08-31 21:19:37 +0000708 TmpTy = DTy.getTypeDerivedFrom();
709 isPointer = true;
710 }
711
Devang Patel19302aa2010-05-07 18:11:54 +0000712 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Tice248d5572009-08-31 21:19:37 +0000713
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000714 // Find the __forwarding field and the variable field in the __Block_byref
715 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000716 DIArray Fields = blockStruct.getTypeArray();
717 DIDescriptor varField = DIDescriptor();
718 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000719
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000720 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
721 DIDescriptor Element = Fields.getElement(i);
Devang Patel19302aa2010-05-07 18:11:54 +0000722 DIDerivedType DT = DIDerivedType(Element);
Devang Patel7f75bbe2009-11-25 17:36:49 +0000723 StringRef fieldName = DT.getName();
724 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000725 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000726 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000727 varField = Element;
728 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000729
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000730 // Get the offsets for the forwarding field and the variable field.
Chris Lattner5df82552010-03-31 06:06:37 +0000731 unsigned forwardingFieldOffset =
Devang Patel19302aa2010-05-07 18:11:54 +0000732 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner5df82552010-03-31 06:06:37 +0000733 unsigned varFieldOffset =
Devang Patel19302aa2010-05-07 18:11:54 +0000734 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000735
Mike Stump2fd84e22009-09-24 23:21:26 +0000736 // Decode the original location, and use that as the start of the byref
737 // variable's location.
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000738 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000739 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000740 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000741
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000742 if (Location.isReg()) {
743 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000744 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000745 else {
746 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000747 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
748 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000749 }
750 } else {
751 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000752 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000753 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000754 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
755 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000756 }
Caroline Tice248d5572009-08-31 21:19:37 +0000757
Devang Patelc50078e2009-11-21 02:48:08 +0000758 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000759 }
Caroline Tice248d5572009-08-31 21:19:37 +0000760
Mike Stump2fd84e22009-09-24 23:21:26 +0000761 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000762 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000763 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000764 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000765
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000766 // Next add the offset for the '__forwarding' field:
767 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
768 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000769 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000770 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
771 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000772 }
Caroline Tice248d5572009-08-31 21:19:37 +0000773
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000774 // Now dereference the __forwarding field to get to the real __Block_byref
775 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000776 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000777
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000778 // Now that we've got the real __Block_byref... struct, add the offset
779 // for the variable's field to get to the location of the actual variable:
780 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000781 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000782 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
783 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000784 }
Caroline Tice248d5572009-08-31 21:19:37 +0000785
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000786 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000787 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000788}
789
Devang Patelc50078e2009-11-21 02:48:08 +0000790/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000791/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000792void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000793 const MachineLocation &Location) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000794 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000795 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000796 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000797
798 if (Location.isReg()) {
799 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000800 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000801 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000802 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
803 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000804 }
805 } else {
806 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000807 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000808 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000809 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
810 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000811 }
812
Devang Patelc50078e2009-11-21 02:48:08 +0000813 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000814 }
815
Devang Patelc50078e2009-11-21 02:48:08 +0000816 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000817}
818
Devang Patel7e625392010-04-28 01:03:09 +0000819/// addRegisterAddress - Add register location entry in variable DIE.
820bool DwarfDebug::addRegisterAddress(DIE *Die, DbgVariable *DV,
821 const MachineOperand &MO) {
822 assert (MO.isReg() && "Invalid machine operand!");
823 if (!MO.getReg())
824 return false;
825 MachineLocation Location;
826 Location.set(MO.getReg());
827 addAddress(Die, dwarf::DW_AT_location, Location);
828 if (MCSymbol *VS = DV->getDbgValueLabel())
829 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
830 return true;
831}
832
833/// addConstantValue - Add constant value entry in variable DIE.
834bool DwarfDebug::addConstantValue(DIE *Die, DbgVariable *DV,
835 const MachineOperand &MO) {
836 assert (MO.isImm() && "Invalid machine operand!");
837 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
838 unsigned Imm = MO.getImm();
839 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
840 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
841 if (MCSymbol *VS = DV->getDbgValueLabel())
842 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
843 return true;
844}
845
846/// addConstantFPValue - Add constant value entry in variable DIE.
847bool DwarfDebug::addConstantFPValue(DIE *Die, DbgVariable *DV,
848 const MachineOperand &MO) {
849 assert (MO.isFPImm() && "Invalid machine operand!");
850 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
851 APFloat FPImm = MO.getFPImm()->getValueAPF();
852
853 // Get the raw data form of the floating point.
854 const APInt FltVal = FPImm.bitcastToAPInt();
855 const char *FltPtr = (const char*)FltVal.getRawData();
856
857 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
858 bool LittleEndian = Asm->getTargetData().isLittleEndian();
859 int Incr = (LittleEndian ? 1 : -1);
860 int Start = (LittleEndian ? 0 : NumBytes - 1);
861 int Stop = (LittleEndian ? NumBytes : -1);
862
863 // Output the constant to DWARF one byte at a time.
864 for (; Start != Stop; Start += Incr)
865 addUInt(Block, 0, dwarf::DW_FORM_data1,
866 (unsigned char)0xFF & FltPtr[Start]);
867
868 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
869
870 if (MCSymbol *VS = DV->getDbgValueLabel())
871 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
872 VS);
873 return true;
874}
875
876
Devang Patel1a8f9a82009-12-10 19:14:49 +0000877/// addToContextOwner - Add Die into the list of its context owner's children.
878void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel9e592492010-03-08 20:52:55 +0000879 if (Context.isType()) {
Devang Patel19302aa2010-05-07 18:11:54 +0000880 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patel1a8f9a82009-12-10 19:14:49 +0000881 ContextDIE->addChild(Die);
Devang Patel8287d662009-12-15 19:16:48 +0000882 } else if (Context.isNameSpace()) {
Devang Patel19302aa2010-05-07 18:11:54 +0000883 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel8287d662009-12-15 19:16:48 +0000884 ContextDIE->addChild(Die);
Devang Patel19302aa2010-05-07 18:11:54 +0000885 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context))
Devang Patel1a8f9a82009-12-10 19:14:49 +0000886 ContextDIE->addChild(Die);
887 else
888 ModuleCU->addDie(Die);
889}
890
Devang Patel7f139c12009-12-10 18:05:33 +0000891/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
892/// given DIType.
893DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel19302aa2010-05-07 18:11:54 +0000894 DIE *TyDIE = ModuleCU->getDIE(Ty);
Devang Patel7f139c12009-12-10 18:05:33 +0000895 if (TyDIE)
896 return TyDIE;
897
898 // Create new type.
899 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel19302aa2010-05-07 18:11:54 +0000900 ModuleCU->insertDIE(Ty, TyDIE);
Devang Patel7f139c12009-12-10 18:05:33 +0000901 if (Ty.isBasicType())
Devang Patel19302aa2010-05-07 18:11:54 +0000902 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel7f139c12009-12-10 18:05:33 +0000903 else if (Ty.isCompositeType())
Devang Patel19302aa2010-05-07 18:11:54 +0000904 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel7f139c12009-12-10 18:05:33 +0000905 else {
906 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel19302aa2010-05-07 18:11:54 +0000907 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel7f139c12009-12-10 18:05:33 +0000908 }
909
Devang Patel1a8f9a82009-12-10 19:14:49 +0000910 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel7f139c12009-12-10 18:05:33 +0000911 return TyDIE;
912}
913
Devang Patelc50078e2009-11-21 02:48:08 +0000914/// addType - Add a new type attribute to the specified entity.
Devang Patelfe0be132009-12-09 18:24:21 +0000915void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9e592492010-03-08 20:52:55 +0000916 if (!Ty.isValid())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000917 return;
918
919 // Check for pre-existence.
Devang Patel19302aa2010-05-07 18:11:54 +0000920 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000921 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000922 if (Entry) {
923 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000924 return;
925 }
926
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000927 // Construct type.
Devang Patel7f139c12009-12-10 18:05:33 +0000928 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000929
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000930 // Set up proxy.
931 Entry = createDIEEntry(Buffer);
Devang Patel19302aa2010-05-07 18:11:54 +0000932 ModuleCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000933
Devang Patelc50078e2009-11-21 02:48:08 +0000934 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000935}
936
Devang Patelc50078e2009-11-21 02:48:08 +0000937/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patelfe0be132009-12-09 18:24:21 +0000938void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000939 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000940 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000941 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000942 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000943 BTy.getEncoding());
944
945 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000946 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000947 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000948 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000949 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000950}
951
Devang Patelc50078e2009-11-21 02:48:08 +0000952/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patelfe0be132009-12-09 18:24:21 +0000953void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000954 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000955 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000956 uint64_t Size = DTy.getSizeInBits() >> 3;
957 unsigned Tag = DTy.getTag();
958
959 // FIXME - Workaround for templates.
960 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
961
962 Buffer.setTag(Tag);
963
964 // Map to main type, void will not have a type.
965 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelfe0be132009-12-09 18:24:21 +0000966 addType(&Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000967
968 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000969 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000970 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000971
972 // Add size if non-zero (derived types might be zero-sized.)
973 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000974 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000975
976 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000977 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000978 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000979}
980
Devang Patelc50078e2009-11-21 02:48:08 +0000981/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000982void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000983 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000984 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000985
986 uint64_t Size = CTy.getSizeInBits() >> 3;
987 unsigned Tag = CTy.getTag();
988 Buffer.setTag(Tag);
989
990 switch (Tag) {
991 case dwarf::DW_TAG_vector_type:
992 case dwarf::DW_TAG_array_type:
Devang Patelfe0be132009-12-09 18:24:21 +0000993 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000994 break;
995 case dwarf::DW_TAG_enumeration_type: {
996 DIArray Elements = CTy.getTypeArray();
997
998 // Add enumerators to enumeration type.
999 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1000 DIE *ElemDie = NULL;
Devang Patel19302aa2010-05-07 18:11:54 +00001001 DIDescriptor Enum(Elements.getElement(i));
Devang Patel9e592492010-03-08 20:52:55 +00001002 if (Enum.isEnumerator()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001003 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patelc50078e2009-11-21 02:48:08 +00001004 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +00001005 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001006 }
1007 }
1008 break;
1009 case dwarf::DW_TAG_subroutine_type: {
1010 // Add return type.
1011 DIArray Elements = CTy.getTypeArray();
1012 DIDescriptor RTy = Elements.getElement(0);
Devang Patel19302aa2010-05-07 18:11:54 +00001013 addType(&Buffer, DIType(RTy));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001014
1015 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001016 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001017
1018 // Add arguments.
1019 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1020 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1021 DIDescriptor Ty = Elements.getElement(i);
Devang Patel19302aa2010-05-07 18:11:54 +00001022 addType(Arg, DIType(Ty));
Devang Patelc50078e2009-11-21 02:48:08 +00001023 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001024 }
1025 }
1026 break;
1027 case dwarf::DW_TAG_structure_type:
1028 case dwarf::DW_TAG_union_type:
1029 case dwarf::DW_TAG_class_type: {
1030 // Add elements to structure type.
1031 DIArray Elements = CTy.getTypeArray();
1032
1033 // A forward struct declared type may not have elements available.
Devang Patel9e592492010-03-08 20:52:55 +00001034 unsigned N = Elements.getNumElements();
1035 if (N == 0)
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001036 break;
1037
1038 // Add elements to structure type.
Devang Patel9e592492010-03-08 20:52:55 +00001039 for (unsigned i = 0; i < N; ++i) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001040 DIDescriptor Element = Elements.getElement(i);
1041 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001042 if (Element.isSubprogram())
Devang Patel19302aa2010-05-07 18:11:54 +00001043 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel9e592492010-03-08 20:52:55 +00001044 else if (Element.isVariable()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001045 DIVariable DV(Element);
Devang Patel423dfa02010-01-30 01:08:30 +00001046 ElemDie = new DIE(dwarf::DW_TAG_variable);
1047 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1048 DV.getName());
1049 addType(ElemDie, DV.getType());
1050 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1051 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1052 addSourceLine(ElemDie, &DV);
Devang Patel9e592492010-03-08 20:52:55 +00001053 } else if (Element.isDerivedType())
Devang Patel19302aa2010-05-07 18:11:54 +00001054 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel9e592492010-03-08 20:52:55 +00001055 else
1056 continue;
Devang Patelc50078e2009-11-21 02:48:08 +00001057 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001058 }
1059
Devang Patel20b32102009-08-27 23:51:51 +00001060 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +00001061 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001062
1063 unsigned RLang = CTy.getRunTimeLang();
1064 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +00001065 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001066 dwarf::DW_FORM_data1, RLang);
Devang Patel5ae52402010-01-26 21:16:06 +00001067
1068 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel19302aa2010-05-07 18:11:54 +00001069 if (DIDescriptor(ContainingType).isCompositeType())
Devang Patel5ae52402010-01-26 21:16:06 +00001070 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel19302aa2010-05-07 18:11:54 +00001071 getOrCreateTypeDIE(DIType(ContainingType)));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001072 break;
1073 }
1074 default:
1075 break;
1076 }
1077
1078 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001079 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001080 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001081
Devang Pateldc73c372010-01-29 18:34:58 +00001082 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001083 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1084 // Add size if non-zero (derived types might be zero-sized.)
1085 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +00001086 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001087 else {
1088 // Add zero size if it is not a forward declaration.
1089 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001090 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001091 else
Devang Patelc50078e2009-11-21 02:48:08 +00001092 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001093 }
1094
1095 // Add source line info if available.
1096 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001097 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001098 }
1099}
1100
Devang Patelc50078e2009-11-21 02:48:08 +00001101/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1102void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001103 int64_t L = SR.getLo();
1104 int64_t H = SR.getHi();
1105 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1106
Devang Patelc50078e2009-11-21 02:48:08 +00001107 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +00001108 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +00001109 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +00001110 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001111
Devang Patelc50078e2009-11-21 02:48:08 +00001112 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001113}
1114
Devang Patelc50078e2009-11-21 02:48:08 +00001115/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +00001116void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001117 DICompositeType *CTy) {
1118 Buffer.setTag(dwarf::DW_TAG_array_type);
1119 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001120 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001121
1122 // Emit derived type.
Devang Patelfe0be132009-12-09 18:24:21 +00001123 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001124 DIArray Elements = CTy->getTypeArray();
1125
Devang Patel1233f912009-11-21 00:31:03 +00001126 // Get an anonymous type for index type.
Devang Patelfe0be132009-12-09 18:24:21 +00001127 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel1233f912009-11-21 00:31:03 +00001128 if (!IdxTy) {
1129 // Construct an anonymous type for index type.
1130 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +00001131 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1132 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +00001133 dwarf::DW_ATE_signed);
Devang Patelfe0be132009-12-09 18:24:21 +00001134 ModuleCU->addDie(IdxTy);
1135 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +00001136 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001137
1138 // Add subranges to array type.
1139 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1140 DIDescriptor Element = Elements.getElement(i);
1141 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel19302aa2010-05-07 18:11:54 +00001142 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001143 }
1144}
1145
Devang Patelc50078e2009-11-21 02:48:08 +00001146/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel9e592492010-03-08 20:52:55 +00001147DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001148 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel9e592492010-03-08 20:52:55 +00001149 StringRef Name = ETy.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001150 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel9e592492010-03-08 20:52:55 +00001151 int64_t Value = ETy.getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001152 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001153 return Enumerator;
1154}
1155
Devang Patel141e1c42010-01-05 01:46:14 +00001156/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1157/// printer to not emit usual symbol prefix before the symbol name is used then
1158/// return linkage name after skipping this special LLVM prefix.
1159static StringRef getRealLinkageName(StringRef LinkageName) {
1160 char One = '\1';
1161 if (LinkageName.startswith(StringRef(&One, 1)))
1162 return LinkageName.substr(1);
1163 return LinkageName;
1164}
1165
Devang Patelc50078e2009-11-21 02:48:08 +00001166/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patelfe0be132009-12-09 18:24:21 +00001167DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001168 // If the global variable was optmized out then no need to create debug info
1169 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001170 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001171 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001172
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001173 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001174 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001175 GV.getDisplayName());
1176
Devang Patel7f75bbe2009-11-25 17:36:49 +00001177 StringRef LinkageName = GV.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001178 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001179 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001180 getRealLinkageName(LinkageName));
1181
Devang Patelfe0be132009-12-09 18:24:21 +00001182 addType(GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001183 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001184 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1185 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001186
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001187 return GVDie;
1188}
1189
Devang Patelc50078e2009-11-21 02:48:08 +00001190/// createMemberDIE - Create new member DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001191DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001192 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001193 StringRef Name = DT.getName();
1194 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001195 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001196
Devang Patelfe0be132009-12-09 18:24:21 +00001197 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001198
Devang Patelc50078e2009-11-21 02:48:08 +00001199 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001200
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001201 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001202 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001203
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001204 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001205 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001206
1207 if (Size != FieldSize) {
1208 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001209 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1210 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001211
1212 uint64_t Offset = DT.getOffsetInBits();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001213 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1214 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer631e3e32010-01-07 17:50:57 +00001215 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001216 Offset -= FieldOffset;
1217
1218 // Maybe we need to work from the other end.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001219 if (Asm->getTargetData().isLittleEndian())
1220 Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001221 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001222
Devang Patel7d9fe582009-11-04 22:06:12 +00001223 // Here WD_AT_data_member_location points to the anonymous
1224 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001225 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001226
1227 } else
1228 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001229 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001230
Devang Patel84aba942010-02-03 20:08:48 +00001231 if (DT.getTag() == dwarf::DW_TAG_inheritance
1232 && DT.isVirtual()) {
1233
1234 // For C++, virtual base classes are not at fixed offset. Use following
1235 // expression to extract appropriate offset from vtable.
1236 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1237
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001238 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel84aba942010-02-03 20:08:48 +00001239 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1240 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1241 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1242 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1243 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1244 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1245 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1246
1247 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1248 VBaseLocationDie);
1249 } else
1250 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001251
1252 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001253 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001254 dwarf::DW_ACCESS_protected);
1255 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001256 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001257 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-12-03 19:11:07 +00001258 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1259 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1260 dwarf::DW_ACCESS_public);
1261 if (DT.isVirtual())
1262 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1263 dwarf::DW_VIRTUALITY_virtual);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001264 return MemberDie;
1265}
1266
Devang Patel814a12c2009-12-14 16:18:45 +00001267/// createSubprogramDIE - Create new DIE using SP.
1268DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
Devang Patel19302aa2010-05-07 18:11:54 +00001269 DIE *SPDie = ModuleCU->getDIE(SP);
Devang Patel814a12c2009-12-14 16:18:45 +00001270 if (SPDie)
1271 return SPDie;
1272
1273 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel36cd9f82010-03-02 17:58:15 +00001274 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel0ab194f2010-03-02 01:26:20 +00001275 if (!SP.getName().empty())
1276 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001277
Devang Patel7f75bbe2009-11-25 17:36:49 +00001278 StringRef LinkageName = SP.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001279 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001280 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001281 getRealLinkageName(LinkageName));
1282
Devang Patelc50078e2009-11-21 02:48:08 +00001283 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001284
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001285 // Add prototyped tag, if C or ObjC.
1286 unsigned Lang = SP.getCompileUnit().getLanguage();
1287 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1288 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001289 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001290
1291 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001292 DICompositeType SPTy = SP.getType();
1293 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001294 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001295
Devang Patel9e592492010-03-08 20:52:55 +00001296 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelfe0be132009-12-09 18:24:21 +00001297 addType(SPDie, SPTy);
Devang Patelc1df8792009-12-03 01:25:38 +00001298 else
Devang Patel19302aa2010-05-07 18:11:54 +00001299 addType(SPDie, DIType(Args.getElement(0)));
Devang Patelc1df8792009-12-03 01:25:38 +00001300
Devang Patel188c85d2009-12-03 19:11:07 +00001301 unsigned VK = SP.getVirtuality();
1302 if (VK) {
1303 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001304 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel188c85d2009-12-03 19:11:07 +00001305 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1306 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1307 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel7d707f92010-01-19 06:19:05 +00001308 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel19302aa2010-05-07 18:11:54 +00001309 SP.getContainingType()));
Devang Patel188c85d2009-12-03 19:11:07 +00001310 }
1311
Devang Patel814a12c2009-12-14 16:18:45 +00001312 if (MakeDecl || !SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001313 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001314
1315 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-12-03 01:25:38 +00001316 // be handled while processing variables.
1317 DICompositeType SPTy = SP.getType();
1318 DIArray Args = SPTy.getTypeArray();
1319 unsigned SPTag = SPTy.getTag();
1320
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001321 if (SPTag == dwarf::DW_TAG_subroutine_type)
1322 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1323 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel19302aa2010-05-07 18:11:54 +00001324 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelc5dc7252010-02-06 01:02:37 +00001325 addType(Arg, ATy);
1326 if (ATy.isArtificial())
1327 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc50078e2009-11-21 02:48:08 +00001328 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001329 }
1330 }
1331
Devang Patelbf7d00a2010-02-03 19:57:19 +00001332 if (SP.isArtificial())
1333 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1334
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001335 if (!SP.isLocalToUnit())
1336 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel91474172010-04-19 19:14:02 +00001337
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001338 if (SP.isOptimized())
1339 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1340
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001341 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel19302aa2010-05-07 18:11:54 +00001342 ModuleCU->insertDIE(SP, SPDie);
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001343
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001344 return SPDie;
1345}
1346
Devang Patel9c371c62010-05-07 20:54:48 +00001347DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattner8143ce82010-03-31 05:36:29 +00001348 assert(N && "Invalid Scope encoding!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001349
1350 DbgScope *AScope = AbstractScopes.lookup(N);
1351 if (AScope)
1352 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001353
Devang Patel90a0fe32009-11-10 23:06:00 +00001354 DbgScope *Parent = NULL;
1355
1356 DIDescriptor Scope(N);
1357 if (Scope.isLexicalBlock()) {
1358 DILexicalBlock DB(N);
1359 DIDescriptor ParentDesc = DB.getContext();
Devang Patel19302aa2010-05-07 18:11:54 +00001360 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel90a0fe32009-11-10 23:06:00 +00001361 }
1362
1363 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1364
1365 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001366 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001367 AScope->setAbstractScope();
1368 AbstractScopes[N] = AScope;
1369 if (DIDescriptor(N).isSubprogram())
1370 AbstractScopesList.push_back(AScope);
1371 return AScope;
1372}
Devang Patel6a260102009-10-01 20:31:14 +00001373
Devang Patel75c10622010-04-06 23:53:48 +00001374/// isSubprogramContext - Return true if Context is either a subprogram
1375/// or another context nested inside a subprogram.
Devang Patel9c371c62010-05-07 20:54:48 +00001376static bool isSubprogramContext(const MDNode *Context) {
Devang Patel75c10622010-04-06 23:53:48 +00001377 if (!Context)
1378 return false;
1379 DIDescriptor D(Context);
1380 if (D.isSubprogram())
1381 return true;
1382 if (D.isType())
Devang Patel19302aa2010-05-07 18:11:54 +00001383 return isSubprogramContext(DIType(Context).getContext());
Devang Patel75c10622010-04-06 23:53:48 +00001384 return false;
1385}
1386
Jim Grosbach652b7432009-11-21 23:12:12 +00001387/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001388/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1389/// If there are global variables in this scope then create and insert
1390/// DIEs for these variables.
Devang Patel9c371c62010-05-07 20:54:48 +00001391DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001392 DIE *SPDie = ModuleCU->getDIE(SPNode);
1393 assert(SPDie && "Unable to find subprogram DIE!");
1394 DISubprogram SP(SPNode);
Chris Lattnerdd21da82010-03-13 07:26:18 +00001395
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001396 // There is not any need to generate specification DIE for a function
1397 // defined at compile unit level. If a function is defined inside another
1398 // function then gdb prefers the definition at top level and but does not
1399 // expect specification DIE in parent function. So avoid creating
1400 // specification DIE for a function defined inside a function.
1401 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel75c10622010-04-06 23:53:48 +00001402 !SP.getContext().isFile() &&
Devang Patel19302aa2010-05-07 18:11:54 +00001403 !isSubprogramContext(SP.getContext())) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001404 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1405
1406 // Add arguments.
1407 DICompositeType SPTy = SP.getType();
1408 DIArray Args = SPTy.getTypeArray();
1409 unsigned SPTag = SPTy.getTag();
1410 if (SPTag == dwarf::DW_TAG_subroutine_type)
1411 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1412 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel19302aa2010-05-07 18:11:54 +00001413 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001414 addType(Arg, ATy);
1415 if (ATy.isArtificial())
1416 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1417 SPDie->addChild(Arg);
1418 }
1419 DIE *SPDeclDie = SPDie;
1420 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1421 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1422 SPDeclDie);
1423 ModuleCU->addDie(SPDie);
1424 }
1425
1426 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1427 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1428 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1429 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1430 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1431 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1432 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelc5dc7252010-02-06 01:02:37 +00001433
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001434 return SPDie;
Devang Patel90a0fe32009-11-10 23:06:00 +00001435}
1436
Jim Grosbach652b7432009-11-21 23:12:12 +00001437/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001438/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1439DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patelce7bf012010-04-27 19:46:33 +00001440
1441 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1442 if (Scope->isAbstractScope())
1443 return ScopeDIE;
1444
1445 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1446 if (Ranges.empty())
1447 return 0;
1448
1449 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1450 if (Ranges.size() > 1) {
1451 // .debug_range section has not been laid out yet. Emit offset in
1452 // .debug_range as a uint, size 4, for now. emitDIE will handle
1453 // DW_AT_ranges appropriately.
1454 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1455 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1456 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1457 RE = Ranges.end(); RI != RE; ++RI) {
1458 DebugRangeSymbols.push_back(LabelsBeforeInsn.lookup(RI->first));
1459 DebugRangeSymbols.push_back(LabelsAfterInsn.lookup(RI->second));
1460 }
1461 DebugRangeSymbols.push_back(NULL);
1462 DebugRangeSymbols.push_back(NULL);
1463 return ScopeDIE;
1464 }
1465
1466 MCSymbol *Start = LabelsBeforeInsn.lookup(RI->first);
1467 MCSymbol *End = LabelsAfterInsn.lookup(RI->second);
1468
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001469 if (Start == 0 || End == 0) return 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00001470
Chris Lattner855e8f52010-03-09 01:58:53 +00001471 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1472 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattner5e423432010-03-09 01:51:43 +00001473
Devang Patelce7bf012010-04-27 19:46:33 +00001474 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1475 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel90a0fe32009-11-10 23:06:00 +00001476
1477 return ScopeDIE;
1478}
1479
Devang Patelc50078e2009-11-21 02:48:08 +00001480/// constructInlinedScopeDIE - This scope represents inlined body of
1481/// a function. Construct DIE to represent this concrete inlined copy
1482/// of the function.
1483DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patelce7bf012010-04-27 19:46:33 +00001484
1485 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1486 assert (Ranges.empty() == false
1487 && "DbgScope does not have instruction markers!");
1488
1489 // FIXME : .debug_inlined section specification does not clearly state how
1490 // to emit inlined scope that is split into multiple instruction ranges.
1491 // For now, use first instruction range and emit low_pc/high_pc pair and
1492 // corresponding .debug_inlined section entry for this pair.
1493 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1494 MCSymbol *StartLabel = LabelsBeforeInsn.lookup(RI->first);
1495 MCSymbol *EndLabel = LabelsAfterInsn.lookup(RI->second);
1496
1497 if (StartLabel == 0 || EndLabel == 0) {
1498 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1499 return 0;
1500 }
Chris Lattner855e8f52010-03-09 01:58:53 +00001501 assert(StartLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001502 "Invalid starting label for an inlined scope!");
Chris Lattner855e8f52010-03-09 01:58:53 +00001503 assert(EndLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001504 "Invalid end label for an inlined scope!");
Devang Patelce7bf012010-04-27 19:46:33 +00001505
Devang Patel9e592492010-03-08 20:52:55 +00001506 if (!Scope->getScopeNode())
Devang Patelbe149872010-03-08 19:20:38 +00001507 return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001508 DIScope DS(Scope->getScopeNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001509 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1510
Devang Patel19302aa2010-05-07 18:11:54 +00001511 DISubprogram InlinedSP = getDISubprogram(DS);
1512 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP);
Chris Lattner8143ce82010-03-31 05:36:29 +00001513 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001514 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001515 dwarf::DW_FORM_ref4, OriginDIE);
1516
Chris Lattneread58652010-03-09 00:31:02 +00001517 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattner855e8f52010-03-09 01:58:53 +00001518 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel90a0fe32009-11-10 23:06:00 +00001519
1520 InlinedSubprogramDIEs.insert(OriginDIE);
1521
1522 // Track the start label for this inlined function.
Devang Patel9c371c62010-05-07 20:54:48 +00001523 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel19302aa2010-05-07 18:11:54 +00001524 I = InlineInfo.find(InlinedSP);
Devang Patel90a0fe32009-11-10 23:06:00 +00001525
1526 if (I == InlineInfo.end()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001527 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbachb23f2422009-11-22 19:20:36 +00001528 ScopeDIE));
Devang Patel19302aa2010-05-07 18:11:54 +00001529 InlinedSPNodes.push_back(InlinedSP);
Devang Patel90a0fe32009-11-10 23:06:00 +00001530 } else
Chris Lattneread58652010-03-09 00:31:02 +00001531 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001532
Devang Patel90a0fe32009-11-10 23:06:00 +00001533 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001534 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1535 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001536
1537 return ScopeDIE;
1538}
1539
Devang Patelc50078e2009-11-21 02:48:08 +00001540
1541/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001542DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001543 // Get the descriptor.
1544 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001545 StringRef Name = VD.getName();
1546 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001547 return NULL;
Devang Patel90a0fe32009-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
1567
1568 DIE *AbsDIE = NULL;
1569 if (DbgVariable *AV = DV->getAbstractVariable())
1570 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001571
Devang Patel90a0fe32009-11-10 23:06:00 +00001572 if (AbsDIE) {
1573 DIScope DS(Scope->getScopeNode());
Devang Patel19302aa2010-05-07 18:11:54 +00001574 DISubprogram InlinedSP = getDISubprogram(DS);
1575 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP);
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001576 (void) OriginSPDIE;
Chris Lattner8143ce82010-03-31 05:36:29 +00001577 assert(OriginSPDIE && "Unable to find Origin DIE for the SP!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001578 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
Chris Lattner8143ce82010-03-31 05:36:29 +00001579 assert(AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001580 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001581 dwarf::DW_FORM_ref4, AbsDIE);
1582 }
1583 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001584 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1585 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001586
1587 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001588 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001589 // addresses instead.
1590 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001591 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001592 else
Devang Patelfe0be132009-12-09 18:24:21 +00001593 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001594 }
1595
1596 // Add variable address.
1597 if (!Scope->isAbstractScope()) {
Devang Patel14da02f2010-03-15 18:33:46 +00001598 // Check if variable is described by DBG_VALUE instruction.
Devang Patel7e625392010-04-28 01:03:09 +00001599 if (const MachineInstr *DVInsn = DV->getDbgValue()) {
1600 bool updated = false;
1601 // FIXME : Handle getNumOperands != 3
1602 if (DVInsn->getNumOperands() == 3) {
1603 if (DVInsn->getOperand(0).isReg())
1604 updated = addRegisterAddress(VariableDie, DV, DVInsn->getOperand(0));
1605 else if (DVInsn->getOperand(0).isImm())
1606 updated = addConstantValue(VariableDie, DV, DVInsn->getOperand(0));
1607 else if (DVInsn->getOperand(0).isFPImm())
1608 updated = addConstantFPValue(VariableDie, DV, DVInsn->getOperand(0));
Devang Patel7c3efb12010-04-28 01:39:28 +00001609 } else {
1610 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1611 if (Location.getReg()) {
1612 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1613 if (MCSymbol *VS = DV->getDbgValueLabel())
1614 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1615 VS);
1616 updated = true;
1617 }
Devang Patel7e625392010-04-28 01:03:09 +00001618 }
1619 if (!updated) {
1620 // If variableDie is not updated then DBG_VALUE instruction does not
1621 // have valid variable info.
1622 delete VariableDie;
1623 return NULL;
1624 }
1625 }
1626 else {
Devang Patel14da02f2010-03-15 18:33:46 +00001627 MachineLocation Location;
1628 unsigned FrameReg;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001629 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1630 int Offset = RI->getFrameIndexReference(*Asm->MF, DV->getFrameIndex(),
Chris Lattner5df82552010-03-31 06:06:37 +00001631 FrameReg);
Devang Patel14da02f2010-03-15 18:33:46 +00001632 Location.set(FrameReg, Offset);
1633
1634 if (VD.hasComplexAddress())
1635 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1636 else if (VD.isBlockByrefVariable())
1637 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1638 else
1639 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1640 }
Devang Patel90a0fe32009-11-10 23:06:00 +00001641 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001642
1643 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1644 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001645 DV->setDIE(VariableDie);
1646 return VariableDie;
1647
1648}
Devang Patelc50078e2009-11-21 02:48:08 +00001649
Devang Patelec13b4f2009-11-24 01:14:22 +00001650void DwarfDebug::addPubTypes(DISubprogram SP) {
1651 DICompositeType SPTy = SP.getType();
1652 unsigned SPTag = SPTy.getTag();
1653 if (SPTag != dwarf::DW_TAG_subroutine_type)
1654 return;
1655
1656 DIArray Args = SPTy.getTypeArray();
Devang Patelec13b4f2009-11-24 01:14:22 +00001657 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel19302aa2010-05-07 18:11:54 +00001658 DIType ATy(Args.getElement(i));
Devang Patel9e592492010-03-08 20:52:55 +00001659 if (!ATy.isValid())
Devang Patelec13b4f2009-11-24 01:14:22 +00001660 continue;
1661 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel19302aa2010-05-07 18:11:54 +00001662 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel21171c22010-04-13 20:35:04 +00001663 && !CATy.isForwardDecl()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001664 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy))
Devang Patelec13b4f2009-11-24 01:14:22 +00001665 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1666 }
1667 }
1668}
1669
Devang Patelc50078e2009-11-21 02:48:08 +00001670/// constructScopeDIE - Construct a DIE for this scope.
1671DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel9e592492010-03-08 20:52:55 +00001672 if (!Scope || !Scope->getScopeNode())
1673 return NULL;
1674
1675 DIScope DS(Scope->getScopeNode());
1676 DIE *ScopeDIE = NULL;
1677 if (Scope->getInlinedAt())
1678 ScopeDIE = constructInlinedScopeDIE(Scope);
1679 else if (DS.isSubprogram()) {
1680 if (Scope->isAbstractScope())
Devang Patel19302aa2010-05-07 18:11:54 +00001681 ScopeDIE = ModuleCU->getDIE(DS);
Devang Patel9e592492010-03-08 20:52:55 +00001682 else
Devang Patel19302aa2010-05-07 18:11:54 +00001683 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel9e592492010-03-08 20:52:55 +00001684 }
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001685 else
Devang Patel9e592492010-03-08 20:52:55 +00001686 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001687 if (!ScopeDIE) return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001688
Devang Patel90a0fe32009-11-10 23:06:00 +00001689 // Add variables to scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001690 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel90a0fe32009-11-10 23:06:00 +00001691 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelfe0be132009-12-09 18:24:21 +00001692 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +00001693 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001694 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001695 }
1696
1697 // Add nested scopes.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001698 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel90a0fe32009-11-10 23:06:00 +00001699 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1700 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001701 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001702 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001703 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001704 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001705
1706 if (DS.isSubprogram())
Devang Patel19302aa2010-05-07 18:11:54 +00001707 addPubTypes(DISubprogram(DS));
Devang Patelec13b4f2009-11-24 01:14:22 +00001708
1709 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001710}
1711
Bill Wendlingf5839192009-05-20 23:19:06 +00001712/// GetOrCreateSourceID - Look up the source id with the given directory and
1713/// source file names. If none currently exists, create a new id and insert it
1714/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1715/// maps as well.
Chris Lattner5df82552010-03-31 06:06:37 +00001716unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf5839192009-05-20 23:19:06 +00001717 unsigned DId;
1718 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1719 if (DI != DirectoryIdMap.end()) {
1720 DId = DI->getValue();
1721 } else {
1722 DId = DirectoryNames.size() + 1;
1723 DirectoryIdMap[DirName] = DId;
1724 DirectoryNames.push_back(DirName);
1725 }
1726
1727 unsigned FId;
1728 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1729 if (FI != SourceFileIdMap.end()) {
1730 FId = FI->getValue();
1731 } else {
1732 FId = SourceFileNames.size() + 1;
1733 SourceFileIdMap[FileName] = FId;
1734 SourceFileNames.push_back(FileName);
1735 }
1736
1737 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1738 SourceIdMap.find(std::make_pair(DId, FId));
1739 if (SI != SourceIdMap.end())
1740 return SI->second;
1741
1742 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1743 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1744 SourceIds.push_back(std::make_pair(DId, FId));
1745
1746 return SrcId;
1747}
1748
Devang Patel8287d662009-12-15 19:16:48 +00001749/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1750DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel19302aa2010-05-07 18:11:54 +00001751 DIE *NDie = ModuleCU->getDIE(NS);
Devang Patel8287d662009-12-15 19:16:48 +00001752 if (NDie)
1753 return NDie;
1754 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel19302aa2010-05-07 18:11:54 +00001755 ModuleCU->insertDIE(NS, NDie);
Devang Patel8287d662009-12-15 19:16:48 +00001756 if (!NS.getName().empty())
1757 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1758 addSourceLine(NDie, &NS);
1759 addToContextOwner(NDie, NS.getContext());
1760 return NDie;
1761}
1762
Devang Patel9c371c62010-05-07 20:54:48 +00001763void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001764 DICompileUnit DIUnit(N);
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001765 // Use first compile unit marked as isMain as the compile unit for this
1766 // module.
1767 if (ModuleCU || !DIUnit.isMain())
1768 return;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001769 StringRef FN = DIUnit.getFilename();
1770 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001771 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001772
1773 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001774 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001775 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001776 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001777 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001778 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel325691a2010-04-26 22:54:28 +00001779 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1780 // simplifies debug range entries.
1781 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_data4, 0);
Devang Patel7ac39832010-03-22 23:11:36 +00001782 // DW_AT_stmt_list is a offset of line number information for this
1783 // compile unit in debug_line section. It is always zero when only one
1784 // compile unit is emitted in one object file.
1785 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf5839192009-05-20 23:19:06 +00001786
Devang Patel7f75bbe2009-11-25 17:36:49 +00001787 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001788 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001789 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001790 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001791
Devang Patel7f75bbe2009-11-25 17:36:49 +00001792 StringRef Flags = DIUnit.getFlags();
1793 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001794 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001795
1796 unsigned RVer = DIUnit.getRunTimeVersion();
1797 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001798 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001799 dwarf::DW_FORM_data1, RVer);
1800
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001801 assert(!ModuleCU &&
1802 "ModuleCU assigned since the top of constructCompileUnit");
1803 ModuleCU = new CompileUnit(ID, Die);
Bill Wendlingf5839192009-05-20 23:19:06 +00001804}
1805
Devang Patel9c371c62010-05-07 20:54:48 +00001806void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001807 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001808
Devang Patel0c03f062009-09-04 23:59:07 +00001809 // If debug information is malformed then ignore it.
1810 if (DI_GV.Verify() == false)
1811 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001812
1813 // Check for pre-existence.
Devang Patel19302aa2010-05-07 18:11:54 +00001814 if (ModuleCU->getDIE(DI_GV))
Devang Patel166f8432009-06-26 01:49:18 +00001815 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001816
Devang Patelfe0be132009-12-09 18:24:21 +00001817 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001818 if (!VariableDie)
1819 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001820
Bill Wendlingf5839192009-05-20 23:19:06 +00001821 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001822 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001823
1824 // Add to context owner.
Devang Pateldcc0dce2010-01-15 01:12:22 +00001825 DIDescriptor GVContext = DI_GV.getContext();
1826 // Do not create specification DIE if context is either compile unit
1827 // or a subprogram.
Chris Lattner5df82552010-03-31 06:06:37 +00001828 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel75c10622010-04-06 23:53:48 +00001829 !GVContext.isFile() &&
Devang Patel19302aa2010-05-07 18:11:54 +00001830 !isSubprogramContext(GVContext)) {
Devang Patel8287d662009-12-15 19:16:48 +00001831 // Create specification DIE.
1832 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1833 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1834 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001835 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001836 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001837 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001838 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001839 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patela0a98582010-02-09 01:58:33 +00001840 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel8287d662009-12-15 19:16:48 +00001841 ModuleCU->addDie(VariableSpecDIE);
1842 } else {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001843 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001844 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001845 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001846 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001847 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1848 }
Devang Pateldcc0dce2010-01-15 01:12:22 +00001849 addToContextOwner(VariableDie, GVContext);
Devang Patel1a8f9a82009-12-10 19:14:49 +00001850
Bill Wendlingf5839192009-05-20 23:19:06 +00001851 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001852 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001853
1854 DIType GTy = DI_GV.getType();
Devang Patel21171c22010-04-13 20:35:04 +00001855 if (GTy.isCompositeType() && !GTy.getName().empty()
1856 && !GTy.isForwardDecl()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001857 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy);
Chris Lattner8143ce82010-03-31 05:36:29 +00001858 assert(Entry && "Missing global type!");
Devang Patelec13b4f2009-11-24 01:14:22 +00001859 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1860 }
Devang Patel166f8432009-06-26 01:49:18 +00001861 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001862}
1863
Devang Patel9c371c62010-05-07 20:54:48 +00001864void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001865 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001866
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001867 // Check for pre-existence.
1868 if (ModuleCU->getDIE(N))
1869 return;
1870
Bill Wendlingf5839192009-05-20 23:19:06 +00001871 if (!SP.isDefinition())
1872 // This is a method declaration which will be handled while constructing
1873 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001874 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001875
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001876 DIE *SubprogramDie = createSubprogramDIE(SP);
1877
1878 // Add to map.
1879 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001880
1881 // Add to context owner.
Devang Patel8287d662009-12-15 19:16:48 +00001882 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patelde2d3682009-12-08 23:21:45 +00001883
Bill Wendlingf5839192009-05-20 23:19:06 +00001884 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001885 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001886
Devang Patel166f8432009-06-26 01:49:18 +00001887 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001888}
1889
Devang Patelc50078e2009-11-21 02:48:08 +00001890/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001891/// content. Create global DIEs and emit initial debug info sections.
1892/// This is inovked by the target AsmPrinter.
Chris Lattner75e113c2010-04-04 07:48:20 +00001893void DwarfDebug::beginModule(Module *M) {
Devang Patelce7bf012010-04-27 19:46:33 +00001894 if (DisableDebugInfoPrinting)
1895 return;
1896
Devang Patelfda766d2009-07-30 18:56:46 +00001897 DebugInfoFinder DbgFinder;
1898 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001899
Chris Lattnera52b6172010-04-05 02:19:28 +00001900 bool HasDebugInfo = false;
1901
1902 // Scan all the compile-units to see if there are any marked as the main unit.
1903 // if not, we do not generate debug info.
1904 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1905 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1906 if (DICompileUnit(*I).isMain()) {
1907 HasDebugInfo = true;
1908 break;
1909 }
1910 }
1911
1912 if (!HasDebugInfo) return;
1913
1914 // Tell MMI that we have debug info.
1915 MMI->setDebugInfoAvailability(true);
1916
Chris Lattner706afc02010-04-04 23:17:54 +00001917 // Emit initial sections.
Chris Lattnera52b6172010-04-05 02:19:28 +00001918 EmitSectionLabels();
Chris Lattner706afc02010-04-04 23:17:54 +00001919
Bill Wendlingf5839192009-05-20 23:19:06 +00001920 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001921 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1922 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001923 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001924
Devang Patel90a0fe32009-11-10 23:06:00 +00001925 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001926 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1927 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001928 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001929
Devang Patel1a8f9a82009-12-10 19:14:49 +00001930 // Create DIEs for each global variable.
1931 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1932 E = DbgFinder.global_variable_end(); I != E; ++I)
1933 constructGlobalVariableDIE(*I);
1934
Bill Wendlingf5839192009-05-20 23:19:06 +00001935 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001936 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001937
1938 // Print out .file directives to specify files for .loc directives. These are
1939 // printed out early so that they precede any .loc directives.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001940 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001941 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1942 // Remember source id starts at 1.
1943 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerad653482010-01-22 22:09:00 +00001944 // FIXME: don't use sys::path for this! This should not depend on the
1945 // host.
Bill Wendlingf5839192009-05-20 23:19:06 +00001946 sys::Path FullPath(getSourceDirectoryName(Id.first));
1947 bool AppendOk =
1948 FullPath.appendComponent(getSourceFileName(Id.second));
1949 assert(AppendOk && "Could not append filename to directory!");
1950 AppendOk = false;
Chris Lattner59be4ea2010-01-25 18:58:59 +00001951 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001952 }
1953 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001954}
1955
Devang Patelc50078e2009-11-21 02:48:08 +00001956/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001957///
Devang Patelc50078e2009-11-21 02:48:08 +00001958void DwarfDebug::endModule() {
Bill Wendlingce3c6252010-04-07 09:28:04 +00001959 if (!ModuleCU) return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001960
Devang Patel90a0fe32009-11-10 23:06:00 +00001961 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1962 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1963 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1964 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001965 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001966 }
1967
Devang Patel9c371c62010-05-07 20:54:48 +00001968 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel188c85d2009-12-03 19:11:07 +00001969 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1970 DIE *SPDie = CI->first;
Devang Patel9c371c62010-05-07 20:54:48 +00001971 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel188c85d2009-12-03 19:11:07 +00001972 if (!N) continue;
1973 DIE *NDie = ModuleCU->getDIE(N);
1974 if (!NDie) continue;
1975 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel188c85d2009-12-03 19:11:07 +00001976 }
1977
Bill Wendlingf5839192009-05-20 23:19:06 +00001978 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001979 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00001980 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner73266f92009-08-19 05:49:37 +00001981 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00001982 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf5839192009-05-20 23:19:06 +00001983
1984 // End text sections.
1985 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001986 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerb93558d2010-04-04 19:25:43 +00001987 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf5839192009-05-20 23:19:06 +00001988 }
1989
1990 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001991 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001992
1993 // Emit function debug frame information
1994 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1995 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001996 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001997
1998 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001999 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00002000
2001 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00002002 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002003
2004 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00002005 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00002006
2007 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00002008 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00002009
2010 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00002011 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00002012
Devang Patelec13b4f2009-11-24 01:14:22 +00002013 // Emit info into a debug pubtypes section.
2014 emitDebugPubTypes();
2015
Bill Wendlingf5839192009-05-20 23:19:06 +00002016 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00002017 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00002018
2019 // Emit info into a debug aranges section.
2020 EmitDebugARanges();
2021
2022 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00002023 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00002024
2025 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00002026 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002027
2028 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00002029 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002030
Chris Lattner0f093f82010-03-13 02:17:42 +00002031 // Emit info into a debug str section.
2032 emitDebugStr();
2033
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00002034 delete ModuleCU;
2035 ModuleCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf5839192009-05-20 23:19:06 +00002036}
2037
Devang Patel90a0fe32009-11-10 23:06:00 +00002038/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00002039DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2040 unsigned FrameIdx,
Chris Lattnerb9692a72010-04-02 19:42:39 +00002041 DebugLoc ScopeLoc) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002042
Devang Patel19302aa2010-05-07 18:11:54 +00002043 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00002044 if (AbsDbgVariable)
2045 return AbsDbgVariable;
2046
Devang Patel19302aa2010-05-07 18:11:54 +00002047 LLVMContext &Ctx = Var->getContext();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002048 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel90a0fe32009-11-10 23:06:00 +00002049 if (!Scope)
2050 return NULL;
2051
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002052 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
2053 NULL /* No more-abstract variable*/);
Devang Patelc50078e2009-11-21 02:48:08 +00002054 Scope->addVariable(AbsDbgVariable);
Devang Patel19302aa2010-05-07 18:11:54 +00002055 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel90a0fe32009-11-10 23:06:00 +00002056 return AbsDbgVariable;
2057}
2058
Devang Patel14da02f2010-03-15 18:33:46 +00002059/// findAbstractVariable - Find abstract variable, if any, associated with Var.
2060/// FIXME : Refactor findAbstractVariable.
2061DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2062 const MachineInstr *MI,
Chris Lattnerb9692a72010-04-02 19:42:39 +00002063 DebugLoc ScopeLoc) {
Devang Patel14da02f2010-03-15 18:33:46 +00002064
Devang Patel19302aa2010-05-07 18:11:54 +00002065 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel14da02f2010-03-15 18:33:46 +00002066 if (AbsDbgVariable)
2067 return AbsDbgVariable;
2068
Devang Patel19302aa2010-05-07 18:11:54 +00002069 LLVMContext &Ctx = Var->getContext();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002070 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel14da02f2010-03-15 18:33:46 +00002071 if (!Scope)
2072 return NULL;
2073
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002074 AbsDbgVariable = new DbgVariable(Var, MI,
Devang Patel14da02f2010-03-15 18:33:46 +00002075 NULL /* No more-abstract variable*/);
2076 Scope->addVariable(AbsDbgVariable);
Devang Patel19302aa2010-05-07 18:11:54 +00002077 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002078 DbgValueStartMap[MI] = AbsDbgVariable;
Devang Patel14da02f2010-03-15 18:33:46 +00002079 return AbsDbgVariable;
2080}
2081
Devang Patelc50078e2009-11-21 02:48:08 +00002082/// collectVariableInfo - Populate DbgScope entries with variables' info.
2083void DwarfDebug::collectVariableInfo() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002084 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002085
Devang Patel84139992009-10-06 01:26:37 +00002086 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2087 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2088 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel9c371c62010-05-07 20:54:48 +00002089 const MDNode *Var = VI->first;
Devang Patel90a0fe32009-11-10 23:06:00 +00002090 if (!Var) continue;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002091 DIVariable DV(Var);
2092 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002093
Chris Lattnerb9692a72010-04-02 19:42:39 +00002094 DbgScope *Scope = 0;
Devang Patel9c371c62010-05-07 20:54:48 +00002095 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerb9692a72010-04-02 19:42:39 +00002096 Scope = ConcreteScopes.lookup(IA);
2097 if (Scope == 0)
2098 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2099
Devang Patelbad42262009-11-10 23:20:04 +00002100 // If variable scope is not found then skip this variable.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002101 if (Scope == 0)
Devang Patelbad42262009-11-10 23:20:04 +00002102 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00002103
Chris Lattnerb9692a72010-04-02 19:42:39 +00002104 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second);
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002105 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Devang Patelc50078e2009-11-21 02:48:08 +00002106 Scope->addVariable(RegVar);
Devang Patel84139992009-10-06 01:26:37 +00002107 }
Devang Patel14da02f2010-03-15 18:33:46 +00002108
2109 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002110 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel14da02f2010-03-15 18:33:46 +00002111 I != E; ++I) {
2112 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2113 II != IE; ++II) {
2114 const MachineInstr *MInsn = II;
Chris Lattner202f10b2010-03-31 05:39:57 +00002115 if (!MInsn->isDebugValue())
Devang Patel14da02f2010-03-15 18:33:46 +00002116 continue;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002117
Devang Patel7f78e212010-04-27 20:54:45 +00002118 // Ignore Undef values.
Devang Patelbecba812010-04-27 22:04:41 +00002119 if (MInsn->getOperand(0).isReg() && !MInsn->getOperand(0).getReg())
Devang Patel7f78e212010-04-27 20:54:45 +00002120 continue;
2121
Dan Gohman8db62492010-04-17 16:43:55 +00002122 DIVariable DV(
Devang Patel9c371c62010-05-07 20:54:48 +00002123 const_cast<const MDNode *>(MInsn->getOperand(MInsn->getNumOperands() - 1)
Dan Gohman8db62492010-04-17 16:43:55 +00002124 .getMetadata()));
Devang Patel14da02f2010-03-15 18:33:46 +00002125 if (DV.getTag() == dwarf::DW_TAG_arg_variable) {
2126 // FIXME Handle inlined subroutine arguments.
2127 DbgVariable *ArgVar = new DbgVariable(DV, MInsn, NULL);
2128 CurrentFnDbgScope->addVariable(ArgVar);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002129 DbgValueStartMap[MInsn] = ArgVar;
Devang Patel14da02f2010-03-15 18:33:46 +00002130 continue;
2131 }
2132
2133 DebugLoc DL = MInsn->getDebugLoc();
2134 if (DL.isUnknown()) continue;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002135 DbgScope *Scope = 0;
Devang Patel9c371c62010-05-07 20:54:48 +00002136 if (const MDNode *IA = DL.getInlinedAt(Ctx))
Chris Lattnerb9692a72010-04-02 19:42:39 +00002137 Scope = ConcreteScopes.lookup(IA);
2138 if (Scope == 0)
2139 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2140
Devang Patel14da02f2010-03-15 18:33:46 +00002141 // If variable scope is not found then skip this variable.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002142 if (Scope == 0)
Devang Patel14da02f2010-03-15 18:33:46 +00002143 continue;
2144
Chris Lattnerb9692a72010-04-02 19:42:39 +00002145 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
Devang Patel14da02f2010-03-15 18:33:46 +00002146 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002147 DbgValueStartMap[MInsn] = RegVar;
Devang Patel14da02f2010-03-15 18:33:46 +00002148 Scope->addVariable(RegVar);
2149 }
2150 }
Devang Patel84139992009-10-06 01:26:37 +00002151}
2152
Devang Patelabc2b352010-03-29 17:20:31 +00002153/// beginScope - Process beginning of a scope.
2154void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patelabc2b352010-03-29 17:20:31 +00002155 // Check location.
2156 DebugLoc DL = MI->getDebugLoc();
Dan Gohman5d0dda52010-05-05 23:41:32 +00002157 if (DL.isUnknown()) {
Dan Gohman77c565e2010-05-07 01:08:53 +00002158 if (UnknownLocations) {
2159 // This instruction has no debug location. If the preceding instruction
2160 // did, emit debug location information to indicate that the debug
2161 // location is now unknown.
2162 MCSymbol *Label = NULL;
2163 if (DL == PrevInstLoc)
2164 Label = PrevLabel;
2165 else {
2166 Label = recordSourceLine(DL.getLine(), DL.getCol(), 0);
2167 PrevInstLoc = DL;
2168 PrevLabel = Label;
2169 }
Dan Gohmanf79f0f72010-05-06 00:29:41 +00002170
Dan Gohman77c565e2010-05-07 01:08:53 +00002171 // If this instruction begins a scope then note down corresponding label.
2172 if (InsnsBeginScopeSet.count(MI) != 0)
2173 LabelsBeforeInsn[MI] = Label;
2174 }
Dan Gohmanf79f0f72010-05-06 00:29:41 +00002175
Devang Patelabc2b352010-03-29 17:20:31 +00002176 return;
Dan Gohman5d0dda52010-05-05 23:41:32 +00002177 }
Devang Patelabc2b352010-03-29 17:20:31 +00002178
Devang Patel9c371c62010-05-07 20:54:48 +00002179 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerb9692a72010-04-02 19:42:39 +00002180
2181 // FIXME: Should only verify each scope once!
2182 if (!DIScope(Scope).Verify())
Devang Patelabc2b352010-03-29 17:20:31 +00002183 return;
Devang Patelabc2b352010-03-29 17:20:31 +00002184
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002185 // DBG_VALUE instruction establishes new value.
Chris Lattner202f10b2010-03-31 05:39:57 +00002186 if (MI->isDebugValue()) {
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002187 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2188 = DbgValueStartMap.find(MI);
2189 if (DI != DbgValueStartMap.end()) {
Devang Patelce7bf012010-04-27 19:46:33 +00002190 MCSymbol *Label = NULL;
2191 if (DL == PrevInstLoc)
2192 Label = PrevLabel;
2193 else {
2194 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2195 PrevInstLoc = DL;
2196 PrevLabel = Label;
2197 }
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002198 DI->second->setDbgValueLabel(Label);
2199 }
Devang Patel98ca2372010-03-30 18:07:00 +00002200 return;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002201 }
2202
Devang Patelabc2b352010-03-29 17:20:31 +00002203 // Emit a label to indicate location change. This is used for line
Devang Patelce7bf012010-04-27 19:46:33 +00002204 // table even if this instruction does not start a new scope.
2205 MCSymbol *Label = NULL;
2206 if (DL == PrevInstLoc)
2207 Label = PrevLabel;
2208 else {
2209 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2210 PrevInstLoc = DL;
2211 PrevLabel = Label;
2212 }
Devang Patelabc2b352010-03-29 17:20:31 +00002213
Devang Patel6f878382010-04-08 16:50:29 +00002214 // If this instruction begins a scope then note down corresponding label.
2215 if (InsnsBeginScopeSet.count(MI) != 0)
Devang Patelce7bf012010-04-27 19:46:33 +00002216 LabelsBeforeInsn[MI] = Label;
Devang Patel393a46d2009-10-06 01:50:42 +00002217}
2218
Devang Patelc50078e2009-11-21 02:48:08 +00002219/// endScope - Process end of a scope.
2220void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel6f878382010-04-08 16:50:29 +00002221 if (InsnsEndScopeSet.count(MI) != 0) {
2222 // Emit a label if this instruction ends a scope.
2223 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2224 Asm->OutStreamer.EmitLabel(Label);
Devang Patelce7bf012010-04-27 19:46:33 +00002225 LabelsAfterInsn[MI] = Label;
Devang Patel6f878382010-04-08 16:50:29 +00002226 }
Devang Patel90a0fe32009-11-10 23:06:00 +00002227}
2228
Devang Patelce7bf012010-04-27 19:46:33 +00002229/// getOrCreateDbgScope - Create DbgScope for the scope.
Devang Patel9c371c62010-05-07 20:54:48 +00002230DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002231 if (!InlinedAt) {
2232 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2233 if (WScope)
Devang Patelce7bf012010-04-27 19:46:33 +00002234 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002235 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2236 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patelce7bf012010-04-27 19:46:33 +00002237 if (DIDescriptor(Scope).isLexicalBlock()) {
2238 DbgScope *Parent =
Devang Patel19302aa2010-05-07 18:11:54 +00002239 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Patelce7bf012010-04-27 19:46:33 +00002240 WScope->setParent(Parent);
2241 Parent->addScope(WScope);
2242 }
2243
2244 if (!WScope->getParent()) {
2245 StringRef SPName = DISubprogram(Scope).getLinkageName();
2246 if (SPName == Asm->MF->getFunction()->getName())
2247 CurrentFnDbgScope = WScope;
2248 }
2249
2250 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002251 }
2252
2253 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2254 if (WScope)
Devang Patelce7bf012010-04-27 19:46:33 +00002255 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002256
2257 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2258 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2259 DILocation DL(InlinedAt);
Devang Patelce7bf012010-04-27 19:46:33 +00002260 DbgScope *Parent =
Devang Patel19302aa2010-05-07 18:11:54 +00002261 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Patelce7bf012010-04-27 19:46:33 +00002262 WScope->setParent(Parent);
2263 Parent->addScope(WScope);
2264
2265 ConcreteScopes[InlinedAt] = WScope;
2266 getOrCreateAbstractScope(Scope);
2267
2268 return WScope;
Devang Patel393a46d2009-10-06 01:50:42 +00002269}
2270
Devang Patelce7bf012010-04-27 19:46:33 +00002271/// hasValidLocation - Return true if debug location entry attached with
2272/// machine instruction encodes valid location info.
2273static bool hasValidLocation(LLVMContext &Ctx,
2274 const MachineInstr *MInsn,
Devang Patel9c371c62010-05-07 20:54:48 +00002275 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Patelce7bf012010-04-27 19:46:33 +00002276 if (MInsn->isDebugValue())
2277 return false;
2278 DebugLoc DL = MInsn->getDebugLoc();
2279 if (DL.isUnknown()) return false;
2280
Devang Patel9c371c62010-05-07 20:54:48 +00002281 const MDNode *S = DL.getScope(Ctx);
Devang Patelce7bf012010-04-27 19:46:33 +00002282
2283 // There is no need to create another DIE for compile unit. For all
2284 // other scopes, create one DbgScope now. This will be translated
2285 // into a scope DIE at the end.
2286 if (DIScope(S).isCompileUnit()) return false;
2287
2288 Scope = S;
2289 InlinedAt = DL.getInlinedAt(Ctx);
2290 return true;
2291}
2292
2293/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2294/// hierarchy.
2295static void calculateDominanceGraph(DbgScope *Scope) {
2296 assert (Scope && "Unable to calculate scop edominance graph!");
2297 SmallVector<DbgScope *, 4> WorkStack;
2298 WorkStack.push_back(Scope);
2299 unsigned Counter = 0;
2300 while (!WorkStack.empty()) {
2301 DbgScope *WS = WorkStack.back();
2302 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2303 bool visitedChildren = false;
2304 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2305 SE = Children.end(); SI != SE; ++SI) {
2306 DbgScope *ChildScope = *SI;
2307 if (!ChildScope->getDFSOut()) {
2308 WorkStack.push_back(ChildScope);
2309 visitedChildren = true;
2310 ChildScope->setDFSIn(++Counter);
2311 break;
2312 }
2313 }
2314 if (!visitedChildren) {
2315 WorkStack.pop_back();
2316 WS->setDFSOut(++Counter);
2317 }
2318 }
2319}
2320
2321/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2322static
2323void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2324 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2325{
2326#ifndef NDEBUG
2327 unsigned PrevDFSIn = 0;
2328 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2329 I != E; ++I) {
2330 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2331 II != IE; ++II) {
2332 const MachineInstr *MInsn = II;
Devang Patel9c371c62010-05-07 20:54:48 +00002333 const MDNode *Scope = NULL;
2334 const MDNode *InlinedAt = NULL;
Devang Patelce7bf012010-04-27 19:46:33 +00002335
2336 // Check if instruction has valid location information.
2337 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2338 dbgs() << " [ ";
2339 if (InlinedAt)
2340 dbgs() << "*";
2341 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2342 MI2ScopeMap.find(MInsn);
2343 if (DI != MI2ScopeMap.end()) {
2344 DbgScope *S = DI->second;
2345 dbgs() << S->getDFSIn();
2346 PrevDFSIn = S->getDFSIn();
2347 } else
2348 dbgs() << PrevDFSIn;
2349 } else
2350 dbgs() << " [ x" << PrevDFSIn;
2351 dbgs() << " ]";
2352 MInsn->dump();
2353 }
2354 dbgs() << "\n";
2355 }
2356#endif
2357}
Devang Patelc50078e2009-11-21 02:48:08 +00002358/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner202f10b2010-03-31 05:39:57 +00002359/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner69a76972010-01-26 23:18:02 +00002360bool DwarfDebug::extractScopeInformation() {
Devang Patel6a260102009-10-01 20:31:14 +00002361 // If scope information was extracted using .dbg intrinsics then there is not
2362 // any need to extract these information by scanning each instruction.
2363 if (!DbgScopeMap.empty())
2364 return false;
2365
Dan Gohman30573dd2010-04-23 01:18:53 +00002366 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelce7bf012010-04-27 19:46:33 +00002367 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2368 SmallVector<DbgRange, 4> MIRanges;
2369 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patel9c371c62010-05-07 20:54:48 +00002370 const MDNode *PrevScope = NULL;
2371 const MDNode *PrevInlinedAt = NULL;
Devang Patelce7bf012010-04-27 19:46:33 +00002372 const MachineInstr *RangeBeginMI = NULL;
2373 const MachineInstr *PrevMI = NULL;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002374 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel6a260102009-10-01 20:31:14 +00002375 I != E; ++I) {
2376 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2377 II != IE; ++II) {
2378 const MachineInstr *MInsn = II;
Devang Patel9c371c62010-05-07 20:54:48 +00002379 const MDNode *Scope = NULL;
2380 const MDNode *InlinedAt = NULL;
Devang Patelce7bf012010-04-27 19:46:33 +00002381
2382 // Check if instruction has valid location information.
2383 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2384 PrevMI = MInsn;
2385 continue;
2386 }
Chris Lattnerb9692a72010-04-02 19:42:39 +00002387
Devang Patelce7bf012010-04-27 19:46:33 +00002388 // If scope has not changed then skip this instruction.
2389 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2390 PrevMI = MInsn;
2391 continue;
2392 }
2393
2394 if (RangeBeginMI) {
2395 // If we have alread seen a beginning of a instruction range and
2396 // current instruction scope does not match scope of first instruction
2397 // in this range then create a new instruction range.
2398 DbgRange R(RangeBeginMI, PrevMI);
2399 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
2400 MIRanges.push_back(R);
2401 }
2402
2403 // This is a beginning of a new instruction range.
2404 RangeBeginMI = MInsn;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002405
Devang Patelce7bf012010-04-27 19:46:33 +00002406 // Reset previous markers.
2407 PrevMI = MInsn;
2408 PrevScope = Scope;
2409 PrevInlinedAt = InlinedAt;
Devang Patel90a0fe32009-11-10 23:06:00 +00002410 }
2411 }
2412
Devang Patelce7bf012010-04-27 19:46:33 +00002413 // Create last instruction range.
2414 if (RangeBeginMI && PrevMI && PrevScope) {
2415 DbgRange R(RangeBeginMI, PrevMI);
2416 MIRanges.push_back(R);
2417 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel6a260102009-10-01 20:31:14 +00002418 }
Devang Patelce7bf012010-04-27 19:46:33 +00002419
Devang Patel98e77302010-01-04 20:44:00 +00002420 if (!CurrentFnDbgScope)
2421 return false;
2422
Devang Patelce7bf012010-04-27 19:46:33 +00002423 calculateDominanceGraph(CurrentFnDbgScope);
2424 if (PrintDbgScope)
2425 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2426
2427 // Find ranges of instructions covered by each DbgScope;
2428 DbgScope *PrevDbgScope = NULL;
2429 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2430 RE = MIRanges.end(); RI != RE; ++RI) {
2431 const DbgRange &R = *RI;
2432 DbgScope *S = MI2ScopeMap.lookup(R.first);
2433 assert (S && "Lost DbgScope for a machine instruction!");
2434 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2435 PrevDbgScope->closeInsnRange(S);
2436 S->openInsnRange(R.first);
2437 S->extendInsnRange(R.second);
2438 PrevDbgScope = S;
2439 }
2440
2441 if (PrevDbgScope)
2442 PrevDbgScope->closeInsnRange();
Devang Patel6a260102009-10-01 20:31:14 +00002443
Devang Patelf63b2262010-04-08 18:43:56 +00002444 identifyScopeMarkers();
Devang Patel0d8f3b72010-04-08 15:37:09 +00002445
2446 return !DbgScopeMap.empty();
2447}
2448
Devang Patelce7bf012010-04-27 19:46:33 +00002449/// identifyScopeMarkers() -
2450/// Each DbgScope has first instruction and last instruction to mark beginning
2451/// and end of a scope respectively. Create an inverse map that list scopes
2452/// starts (and ends) with an instruction. One instruction may start (or end)
2453/// multiple scopes. Ignore scopes that are not reachable.
Devang Patelf63b2262010-04-08 18:43:56 +00002454void DwarfDebug::identifyScopeMarkers() {
Devang Patelfd311df2010-01-20 02:05:23 +00002455 SmallVector<DbgScope *, 4> WorkList;
2456 WorkList.push_back(CurrentFnDbgScope);
2457 while (!WorkList.empty()) {
Chris Lattner202f10b2010-03-31 05:39:57 +00002458 DbgScope *S = WorkList.pop_back_val();
Devang Patelce7bf012010-04-27 19:46:33 +00002459
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002460 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patelfd311df2010-01-20 02:05:23 +00002461 if (!Children.empty())
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002462 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patelfd311df2010-01-20 02:05:23 +00002463 SE = Children.end(); SI != SE; ++SI)
2464 WorkList.push_back(*SI);
2465
Devang Patel90a0fe32009-11-10 23:06:00 +00002466 if (S->isAbstractScope())
2467 continue;
Devang Patelce7bf012010-04-27 19:46:33 +00002468
2469 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2470 if (Ranges.empty())
2471 continue;
2472 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2473 RE = Ranges.end(); RI != RE; ++RI) {
2474 assert(RI->first && "DbgRange does not have first instruction!");
2475 assert(RI->second && "DbgRange does not have second instruction!");
2476 InsnsBeginScopeSet.insert(RI->first);
2477 InsnsEndScopeSet.insert(RI->second);
2478 }
Devang Patel6a260102009-10-01 20:31:14 +00002479 }
Devang Patel6a260102009-10-01 20:31:14 +00002480}
2481
Dan Gohmand0e9b672010-04-20 00:37:27 +00002482/// FindFirstDebugLoc - Find the first debug location in the function. This
2483/// is intended to be an approximation for the source position of the
2484/// beginning of the function.
2485static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2486 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2487 I != E; ++I)
2488 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2489 MBBI != MBBE; ++MBBI) {
2490 DebugLoc DL = MBBI->getDebugLoc();
2491 if (!DL.isUnknown())
2492 return DL;
2493 }
2494 return DebugLoc();
2495}
2496
Devang Patelc50078e2009-11-21 02:48:08 +00002497/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002498/// emitted immediately after the function entry point.
Chris Lattner69a76972010-01-26 23:18:02 +00002499void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner2cb53302010-04-05 03:52:55 +00002500 if (!MMI->hasDebugInfo()) return;
Bill Wendlingce3c6252010-04-07 09:28:04 +00002501 if (!extractScopeInformation()) return;
Chris Lattnerb49c9f82010-03-29 20:38:20 +00002502
Devang Patelc50078e2009-11-21 02:48:08 +00002503 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002504
Devang Patelce7bf012010-04-27 19:46:33 +00002505 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2506 Asm->getFunctionNumber());
Bill Wendlingf5839192009-05-20 23:19:06 +00002507 // Assumes in correct section after the entry point.
Devang Patelce7bf012010-04-27 19:46:33 +00002508 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf5839192009-05-20 23:19:06 +00002509
2510 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2511 // function.
Dan Gohmand0e9b672010-04-20 00:37:27 +00002512 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerb9692a72010-04-02 19:42:39 +00002513 if (FDL.isUnknown()) return;
2514
Devang Patel9c371c62010-05-07 20:54:48 +00002515 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Chris Lattnerb9692a72010-04-02 19:42:39 +00002516
2517 DISubprogram SP = getDISubprogram(Scope);
2518 unsigned Line, Col;
2519 if (SP.Verify()) {
2520 Line = SP.getLineNumber();
2521 Col = 0;
2522 } else {
2523 Line = FDL.getLine();
2524 Col = FDL.getCol();
Bill Wendlingf5839192009-05-20 23:19:06 +00002525 }
Chris Lattnerb9692a72010-04-02 19:42:39 +00002526
2527 recordSourceLine(Line, Col, Scope);
Bill Wendlingf5839192009-05-20 23:19:06 +00002528}
2529
Devang Patelc50078e2009-11-21 02:48:08 +00002530/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002531///
Chris Lattner69a76972010-01-26 23:18:02 +00002532void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingce3c6252010-04-07 09:28:04 +00002533 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002534
Devang Patel98e77302010-01-04 20:44:00 +00002535 if (CurrentFnDbgScope) {
2536 // Define end label for subprogram.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002537 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2538 Asm->getFunctionNumber()));
Devang Patel98e77302010-01-04 20:44:00 +00002539
2540 // Get function line info.
2541 if (!Lines.empty()) {
2542 // Get section line info.
2543 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2544 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2545 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2546 // Append the function info to section info.
2547 SectionLineInfos.insert(SectionLineInfos.end(),
2548 Lines.begin(), Lines.end());
2549 }
2550
2551 // Construct abstract scopes.
2552 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2553 AE = AbstractScopesList.end(); AI != AE; ++AI)
2554 constructScopeDIE(*AI);
2555
Devang Patelab4b3ce2010-05-04 06:15:30 +00002556 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel98e77302010-01-04 20:44:00 +00002557
Devang Patelab4b3ce2010-05-04 06:15:30 +00002558 if (!DisableFramePointerElim(*MF))
2559 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2560 dwarf::DW_FORM_flag, 1);
2561
2562
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002563 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel98e77302010-01-04 20:44:00 +00002564 MMI->getFrameMoves()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002565 }
2566
Bill Wendlingf5839192009-05-20 23:19:06 +00002567 // Clear debug info
Devang Patel387e9c12010-01-19 01:26:02 +00002568 CurrentFnDbgScope = NULL;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002569 DeleteContainerSeconds(DbgScopeMap);
Devang Patelfd0ebd52010-04-09 16:04:20 +00002570 InsnsBeginScopeSet.clear();
2571 InsnsEndScopeSet.clear();
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002572 DbgValueStartMap.clear();
Devang Patel387e9c12010-01-19 01:26:02 +00002573 ConcreteScopes.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002574 DeleteContainerSeconds(AbstractScopes);
Devang Patel387e9c12010-01-19 01:26:02 +00002575 AbstractScopesList.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002576 AbstractVariables.clear();
Devang Patelce7bf012010-04-27 19:46:33 +00002577 LabelsBeforeInsn.clear();
2578 LabelsAfterInsn.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002579 Lines.clear();
Devang Patel146c6f72010-04-16 23:33:45 +00002580 PrevLabel = NULL;
Bill Wendlingf5839192009-05-20 23:19:06 +00002581}
2582
Chris Lattner597b0fc2010-03-09 04:54:43 +00002583/// recordSourceLine - Register a source line with debug info. Returns the
2584/// unique label that was emitted and which provides correspondence to
2585/// the source line list.
Devang Patel9c371c62010-05-07 20:54:48 +00002586MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S) {
Devang Patel7f75bbe2009-11-25 17:36:49 +00002587 StringRef Dir;
2588 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002589
Dan Gohman5d0dda52010-05-05 23:41:32 +00002590 unsigned Src = 1;
2591 if (S) {
2592 DIDescriptor Scope(S);
Devang Patel946d0ae2009-10-05 18:03:19 +00002593
Dan Gohman5d0dda52010-05-05 23:41:32 +00002594 if (Scope.isCompileUnit()) {
2595 DICompileUnit CU(S);
2596 Dir = CU.getDirectory();
2597 Fn = CU.getFilename();
2598 } else if (Scope.isSubprogram()) {
2599 DISubprogram SP(S);
2600 Dir = SP.getDirectory();
2601 Fn = SP.getFilename();
2602 } else if (Scope.isLexicalBlock()) {
2603 DILexicalBlock DB(S);
2604 Dir = DB.getDirectory();
2605 Fn = DB.getFilename();
2606 } else
2607 assert(0 && "Unexpected scope info");
2608
2609 Src = GetOrCreateSourceID(Dir, Fn);
2610 }
2611
Chris Lattner54e56f22010-03-14 08:36:50 +00002612 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002613 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf5839192009-05-20 23:19:06 +00002614
Chris Lattner597b0fc2010-03-09 04:54:43 +00002615 Asm->OutStreamer.EmitLabel(Label);
2616 return Label;
Bill Wendlingf5839192009-05-20 23:19:06 +00002617}
2618
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002619//===----------------------------------------------------------------------===//
2620// Emit Methods
2621//===----------------------------------------------------------------------===//
2622
Devang Patelc50078e2009-11-21 02:48:08 +00002623/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002624///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002625unsigned
2626DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002627 // Get the children.
2628 const std::vector<DIE *> &Children = Die->getChildren();
2629
2630 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002631 if (!Last && !Children.empty())
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00002632 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling55fccda2009-05-20 23:21:38 +00002633
2634 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002635 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002636
2637 // Get the abbreviation for this DIE.
2638 unsigned AbbrevNumber = Die->getAbbrevNumber();
2639 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2640
2641 // Set DIE offset
2642 Die->setOffset(Offset);
2643
2644 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002645 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002646
2647 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2648 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2649
2650 // Size the DIE attribute values.
2651 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2652 // Size attribute value.
Chris Lattner352c8e22010-04-05 00:18:22 +00002653 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling55fccda2009-05-20 23:21:38 +00002654
2655 // Size the DIE children if any.
2656 if (!Children.empty()) {
2657 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2658 "Children flag not set");
2659
2660 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002661 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002662
2663 // End of children marker.
2664 Offset += sizeof(int8_t);
2665 }
2666
2667 Die->setSize(Offset - Die->getOffset());
2668 return Offset;
2669}
2670
Devang Patelc50078e2009-11-21 02:48:08 +00002671/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002672///
Devang Patelc50078e2009-11-21 02:48:08 +00002673void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002674 // Compute size of compile unit header.
2675 static unsigned Offset =
2676 sizeof(int32_t) + // Length of Compilation Unit Info
2677 sizeof(int16_t) + // DWARF version number
2678 sizeof(int32_t) + // Offset Into Abbrev. Section
2679 sizeof(int8_t); // Pointer Size (in bytes)
2680
Devang Patelc50078e2009-11-21 02:48:08 +00002681 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002682}
2683
Chris Lattner733c69d2010-04-04 23:02:02 +00002684/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2685/// temporary label to it if SymbolStem is specified.
Chris Lattner66143d22010-04-04 22:59:04 +00002686static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner733c69d2010-04-04 23:02:02 +00002687 const char *SymbolStem = 0) {
Chris Lattner66143d22010-04-04 22:59:04 +00002688 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner733c69d2010-04-04 23:02:02 +00002689 if (!SymbolStem) return 0;
2690
Chris Lattner66143d22010-04-04 22:59:04 +00002691 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2692 Asm->OutStreamer.EmitLabel(TmpSym);
2693 return TmpSym;
2694}
2695
2696/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2697/// the start of each one.
Chris Lattnerd91fd8c2010-04-04 22:33:59 +00002698void DwarfDebug::EmitSectionLabels() {
Chris Lattner73266f92009-08-19 05:49:37 +00002699 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002700
Bill Wendling55fccda2009-05-20 23:21:38 +00002701 // Dwarf sections base addresses.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002702 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner66143d22010-04-04 22:59:04 +00002703 DwarfFrameSectionSym =
2704 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2705 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002706
Chris Lattner66143d22010-04-04 22:59:04 +00002707 DwarfInfoSectionSym =
2708 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2709 DwarfAbbrevSectionSym =
2710 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner733c69d2010-04-04 23:02:02 +00002711 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002712
2713 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner733c69d2010-04-04 23:02:02 +00002714 EmitSectionSym(Asm, MacroInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002715
Chris Lattner733c69d2010-04-04 23:02:02 +00002716 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2717 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2718 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2719 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002720 DwarfStrSectionSym =
2721 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel146c6f72010-04-16 23:33:45 +00002722 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2723 "debug_range");
Bill Wendling55fccda2009-05-20 23:21:38 +00002724
Chris Lattner66143d22010-04-04 22:59:04 +00002725 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner094932e2010-04-04 23:10:38 +00002726 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002727}
2728
Devang Patelc50078e2009-11-21 02:48:08 +00002729/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002730///
Devang Patelc50078e2009-11-21 02:48:08 +00002731void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002732 // Get the abbreviation for this DIE.
2733 unsigned AbbrevNumber = Die->getAbbrevNumber();
2734 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2735
Bill Wendling55fccda2009-05-20 23:21:38 +00002736 // Emit the code (index) for the abbreviation.
Chris Lattner97c69b72010-04-04 18:52:31 +00002737 if (Asm->isVerbose())
Chris Lattnerbcc79432010-01-22 23:18:42 +00002738 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2739 Twine::utohexstr(Die->getOffset()) + ":0x" +
2740 Twine::utohexstr(Die->getSize()) + " " +
2741 dwarf::TagString(Abbrev->getTag()));
Chris Lattner26be1c12010-04-04 19:09:29 +00002742 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002743
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002744 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling55fccda2009-05-20 23:21:38 +00002745 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2746
2747 // Emit the DIE attribute values.
2748 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2749 unsigned Attr = AbbrevData[i].getAttribute();
2750 unsigned Form = AbbrevData[i].getForm();
2751 assert(Form && "Too many attributes for DIE (check abbreviation)");
2752
Chris Lattner97c69b72010-04-04 18:52:31 +00002753 if (Asm->isVerbose())
Chris Lattner91e06b42010-01-24 18:54:17 +00002754 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2755
Bill Wendling55fccda2009-05-20 23:21:38 +00002756 switch (Attr) {
2757 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002758 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002759 break;
2760 case dwarf::DW_AT_abstract_origin: {
2761 DIEEntry *E = cast<DIEEntry>(Values[i]);
2762 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002763 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002764 Asm->EmitInt32(Addr);
2765 break;
2766 }
Devang Patel146c6f72010-04-16 23:33:45 +00002767 case dwarf::DW_AT_ranges: {
2768 // DW_AT_range Value encodes offset in debug_range section.
2769 DIEInteger *V = cast<DIEInteger>(Values[i]);
2770 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2771 V->getValue(),
2772 DwarfDebugRangeSectionSym,
2773 4);
2774 break;
2775 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002776 default:
2777 // Emit an attribute using the defined form.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002778 Values[i]->EmitValue(Asm, Form);
Bill Wendling55fccda2009-05-20 23:21:38 +00002779 break;
2780 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002781 }
2782
2783 // Emit the DIE children if any.
2784 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2785 const std::vector<DIE *> &Children = Die->getChildren();
2786
2787 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002788 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002789
Chris Lattner97c69b72010-04-04 18:52:31 +00002790 if (Asm->isVerbose())
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002791 Asm->OutStreamer.AddComment("End Of Children Mark");
2792 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002793 }
2794}
2795
Devang Patelfe0be132009-12-09 18:24:21 +00002796/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002797///
Devang Patelfe0be132009-12-09 18:24:21 +00002798void DwarfDebug::emitDebugInfo() {
2799 // Start debug info section.
2800 Asm->OutStreamer.SwitchSection(
2801 Asm->getObjFileLowering().getDwarfInfoSection());
2802 DIE *Die = ModuleCU->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002803
2804 // Emit the compile units header.
Chris Lattnerb93558d2010-04-04 19:25:43 +00002805 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2806 ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002807
2808 // Emit size of content not including length itself
2809 unsigned ContentSize = Die->getSize() +
2810 sizeof(int16_t) + // DWARF version number
2811 sizeof(int32_t) + // Offset Into Abbrev. Section
2812 sizeof(int8_t) + // Pointer Size (in bytes)
2813 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2814
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002815 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2816 Asm->EmitInt32(ContentSize);
2817 Asm->OutStreamer.AddComment("DWARF version number");
2818 Asm->EmitInt16(dwarf::DWARF_VERSION);
2819 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Chris Lattnerc06b4742010-04-04 23:25:33 +00002820 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2821 DwarfAbbrevSectionSym);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002822 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002823 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00002824
Devang Patelc50078e2009-11-21 02:48:08 +00002825 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002826 // FIXME - extra padding for gdb bug.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002827 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2828 Asm->EmitInt8(0);
2829 Asm->EmitInt8(0);
2830 Asm->EmitInt8(0);
2831 Asm->EmitInt8(0);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002832 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002833}
2834
Devang Patelc50078e2009-11-21 02:48:08 +00002835/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002836///
Devang Patelc50078e2009-11-21 02:48:08 +00002837void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002838 // Check to see if it is worth the effort.
2839 if (!Abbreviations.empty()) {
2840 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002841 Asm->OutStreamer.SwitchSection(
2842 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002843
Chris Lattnerb93558d2010-04-04 19:25:43 +00002844 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002845
2846 // For each abbrevation.
2847 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2848 // Get abbreviation data
2849 const DIEAbbrev *Abbrev = Abbreviations[i];
2850
2851 // Emit the abbrevations code (base 1 index.)
Chris Lattner26be1c12010-04-04 19:09:29 +00002852 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling55fccda2009-05-20 23:21:38 +00002853
2854 // Emit the abbreviations data.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002855 Abbrev->Emit(Asm);
Bill Wendling55fccda2009-05-20 23:21:38 +00002856 }
2857
2858 // Mark end of abbreviations.
Chris Lattner26be1c12010-04-04 19:09:29 +00002859 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002860
Chris Lattnerb93558d2010-04-04 19:25:43 +00002861 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002862 }
2863}
2864
Devang Patelc50078e2009-11-21 02:48:08 +00002865/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002866/// the line matrix.
2867///
Devang Patelc50078e2009-11-21 02:48:08 +00002868void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002869 // Define last address of section.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002870 Asm->OutStreamer.AddComment("Extended Op");
2871 Asm->EmitInt8(0);
2872
2873 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002874 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002875 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2876 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2877
2878 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnereb159d62010-03-10 01:17:49 +00002879
Chris Lattnerb93558d2010-04-04 19:25:43 +00002880 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002881 Asm->getTargetData().getPointerSize(),
2882 0/*AddrSpace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00002883
2884 // Mark end of matrix.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002885 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2886 Asm->EmitInt8(0);
Chris Lattnerad653482010-01-22 22:09:00 +00002887 Asm->EmitInt8(1);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002888 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002889}
2890
Devang Patelc50078e2009-11-21 02:48:08 +00002891/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002892///
Devang Patelc50078e2009-11-21 02:48:08 +00002893void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002894 // If the target is using .loc/.file, the assembler will be emitting the
2895 // .debug_line table automatically.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002896 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002897 return;
2898
2899 // Minimum line delta, thus ranging from -10..(255-10).
2900 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2901 // Maximum line delta, thus ranging from -10..(255-10).
2902 const int MaxLineDelta = 255 + MinLineDelta;
2903
2904 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002905 Asm->OutStreamer.SwitchSection(
2906 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002907
2908 // Construct the section header.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002909 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002910 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2911 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002912 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002913
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002914 Asm->OutStreamer.AddComment("DWARF version number");
2915 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00002916
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002917 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002918 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2919 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002920 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002921
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002922 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2923 Asm->EmitInt8(1);
2924 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2925 Asm->EmitInt8(1);
2926 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2927 Asm->EmitInt8(MinLineDelta);
2928 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
2929 Asm->EmitInt8(MaxLineDelta);
2930 Asm->OutStreamer.AddComment("Special Opcode Base");
2931 Asm->EmitInt8(-MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00002932
2933 // Line number standard opcode encodings argument count
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002934 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
2935 Asm->EmitInt8(0);
2936 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
2937 Asm->EmitInt8(1);
2938 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
2939 Asm->EmitInt8(1);
2940 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
2941 Asm->EmitInt8(1);
2942 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
2943 Asm->EmitInt8(1);
2944 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
2945 Asm->EmitInt8(0);
2946 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
2947 Asm->EmitInt8(0);
2948 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
2949 Asm->EmitInt8(0);
2950 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
2951 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002952
2953 // Emit directories.
2954 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnercc564642010-01-23 03:11:46 +00002955 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner97c69b72010-04-04 18:52:31 +00002956 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattnercc564642010-01-23 03:11:46 +00002957 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002958 }
2959
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002960 Asm->OutStreamer.AddComment("End of directories");
2961 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002962
2963 // Emit files.
2964 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2965 // Remember source id starts at 1.
2966 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnercc564642010-01-23 03:11:46 +00002967 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner97c69b72010-04-04 18:52:31 +00002968 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattnercc564642010-01-23 03:11:46 +00002969 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2970
Chris Lattner26be1c12010-04-04 19:09:29 +00002971 Asm->EmitULEB128(Id.first, "Directory #");
2972 Asm->EmitULEB128(0, "Mod date");
2973 Asm->EmitULEB128(0, "File size");
Bill Wendling55fccda2009-05-20 23:21:38 +00002974 }
2975
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002976 Asm->OutStreamer.AddComment("End of files");
2977 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002978
Chris Lattnerb93558d2010-04-04 19:25:43 +00002979 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002980
2981 // A sequence for each text section.
2982 unsigned SecSrcLinesSize = SectionSourceLines.size();
2983
2984 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2985 // Isolate current sections line info.
2986 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2987
Bill Wendling55fccda2009-05-20 23:21:38 +00002988 // Dwarf assumes we start with first line of first source file.
2989 unsigned Source = 1;
2990 unsigned Line = 1;
2991
2992 // Construct rows of the address, source, line, column matrix.
2993 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2994 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002995 MCSymbol *Label = LineInfo.getLabel();
Chris Lattner31ae74d2010-03-14 02:20:58 +00002996 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling55fccda2009-05-20 23:21:38 +00002997
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002998 if (Asm->isVerbose()) {
Chris Lattneree3b40f2010-03-10 01:04:13 +00002999 std::pair<unsigned, unsigned> SrcID =
Bill Wendling55fccda2009-05-20 23:21:38 +00003000 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattneree3b40f2010-03-10 01:04:13 +00003001 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner48fb0b12010-03-10 02:29:31 +00003002 "/" +
3003 Twine(getSourceFileName(SrcID.second)) +
Chris Lattneree3b40f2010-03-10 01:04:13 +00003004 ":" + Twine(LineInfo.getLine()));
Bill Wendling55fccda2009-05-20 23:21:38 +00003005 }
3006
3007 // Define the line address.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003008 Asm->OutStreamer.AddComment("Extended Op");
3009 Asm->EmitInt8(0);
3010 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003011 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003012
3013 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3014 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3015
3016 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003017 Asm->OutStreamer.EmitSymbolValue(Label,
3018 Asm->getTargetData().getPointerSize(),
Chris Lattner31ae74d2010-03-14 02:20:58 +00003019 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00003020
Bill Wendling55fccda2009-05-20 23:21:38 +00003021 // If change of source, then switch to the new source.
3022 if (Source != LineInfo.getSourceID()) {
3023 Source = LineInfo.getSourceID();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003024 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3025 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner26be1c12010-04-04 19:09:29 +00003026 Asm->EmitULEB128(Source, "New Source");
Bill Wendling55fccda2009-05-20 23:21:38 +00003027 }
3028
3029 // If change of line.
3030 if (Line != LineInfo.getLine()) {
3031 // Determine offset.
3032 int Offset = LineInfo.getLine() - Line;
3033 int Delta = Offset - MinLineDelta;
3034
3035 // Update line.
3036 Line = LineInfo.getLine();
3037
3038 // If delta is small enough and in range...
3039 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3040 // ... then use fast opcode.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003041 Asm->OutStreamer.AddComment("Line Delta");
3042 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00003043 } else {
3044 // ... otherwise use long hand.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003045 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling55fccda2009-05-20 23:21:38 +00003046 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner26be1c12010-04-04 19:09:29 +00003047 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003048 Asm->OutStreamer.AddComment("DW_LNS_copy");
3049 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00003050 }
3051 } else {
3052 // Copy the previous row (different address or source)
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003053 Asm->OutStreamer.AddComment("DW_LNS_copy");
3054 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00003055 }
3056 }
3057
Devang Patelc50078e2009-11-21 02:48:08 +00003058 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003059 }
3060
3061 if (SecSrcLinesSize == 0)
3062 // Because we're emitting a debug_line section, we still need a line
3063 // table. The linker and friends expect it to exist. If there's nothing to
3064 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00003065 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003066
Chris Lattnerb93558d2010-04-04 19:25:43 +00003067 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003068}
3069
Devang Patelc50078e2009-11-21 02:48:08 +00003070/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003071///
Devang Patelc50078e2009-11-21 02:48:08 +00003072void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003073 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003074 return;
3075
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003076 int stackGrowth = Asm->getTargetData().getPointerSize();
3077 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3078 TargetFrameInfo::StackGrowsDown)
3079 stackGrowth *= -1;
Bill Wendling55fccda2009-05-20 23:21:38 +00003080
3081 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00003082 Asm->OutStreamer.SwitchSection(
3083 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003084
Chris Lattnerb93558d2010-04-04 19:25:43 +00003085 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003086 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003087 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3088 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003089
Chris Lattnerb93558d2010-04-04 19:25:43 +00003090 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003091 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling55fccda2009-05-20 23:21:38 +00003092 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003093 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00003094 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003095 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnercc564642010-01-23 03:11:46 +00003096 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner26be1c12010-04-04 19:09:29 +00003097 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3098 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003099 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003100 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling55fccda2009-05-20 23:21:38 +00003101 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling55fccda2009-05-20 23:21:38 +00003102
3103 std::vector<MachineMove> Moves;
3104 RI->getInitialFrameState(Moves);
3105
Chris Lattner193fec02010-04-04 23:41:46 +00003106 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00003107
Chris Lattner7101e232010-04-28 01:05:45 +00003108 Asm->EmitAlignment(2);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003109 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003110}
3111
Devang Patelc50078e2009-11-21 02:48:08 +00003112/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00003113/// section.
Chris Lattnerdd21da82010-03-13 07:26:18 +00003114void DwarfDebug::
3115emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003116 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003117 return;
3118
3119 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00003120 Asm->OutStreamer.SwitchSection(
3121 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003122
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003123 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattnerdd21da82010-03-13 07:26:18 +00003124 MCSymbol *DebugFrameBegin =
Chris Lattnerb93558d2010-04-04 19:25:43 +00003125 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattnerdd21da82010-03-13 07:26:18 +00003126 MCSymbol *DebugFrameEnd =
Chris Lattnerb93558d2010-04-04 19:25:43 +00003127 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003128 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003129
Chris Lattnerdd21da82010-03-13 07:26:18 +00003130 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling55fccda2009-05-20 23:21:38 +00003131
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003132 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003133 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3134 DwarfFrameSectionSym);
Bill Wendling55fccda2009-05-20 23:21:38 +00003135
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003136 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerb93558d2010-04-04 19:25:43 +00003137 MCSymbol *FuncBeginSym =
3138 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner314e3362010-03-13 07:40:56 +00003139 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003140 Asm->getTargetData().getPointerSize(),
3141 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00003142
3143
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003144 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003145 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003146 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00003147
Chris Lattner193fec02010-04-04 23:41:46 +00003148 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00003149
Chris Lattner7101e232010-04-28 01:05:45 +00003150 Asm->EmitAlignment(2);
Chris Lattnerdd21da82010-03-13 07:26:18 +00003151 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling55fccda2009-05-20 23:21:38 +00003152}
3153
Devang Patelfe0be132009-12-09 18:24:21 +00003154/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3155///
3156void DwarfDebug::emitDebugPubNames() {
3157 // Start the dwarf pubnames section.
3158 Asm->OutStreamer.SwitchSection(
3159 Asm->getObjFileLowering().getDwarfPubNamesSection());
3160
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003161 Asm->OutStreamer.AddComment("Length of Public Names Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003162 Asm->EmitLabelDifference(
3163 Asm->GetTempSymbol("pubnames_end", ModuleCU->getID()),
3164 Asm->GetTempSymbol("pubnames_begin", ModuleCU->getID()), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003165
Chris Lattnerb93558d2010-04-04 19:25:43 +00003166 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3167 ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00003168
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003169 Asm->OutStreamer.AddComment("DWARF Version");
3170 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00003171
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003172 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003173 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3174 DwarfInfoSectionSym);
Bill Wendling55fccda2009-05-20 23:21:38 +00003175
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003176 Asm->OutStreamer.AddComment("Compilation Unit Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003177 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3178 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3179 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003180
Devang Patelfe0be132009-12-09 18:24:21 +00003181 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00003182 for (StringMap<DIE*>::const_iterator
3183 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3184 const char *Name = GI->getKeyData();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003185 DIE *Entity = GI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00003186
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003187 Asm->OutStreamer.AddComment("DIE offset");
3188 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00003189
Chris Lattner97c69b72010-04-04 18:52:31 +00003190 if (Asm->isVerbose())
Chris Lattnercc564642010-01-23 03:11:46 +00003191 Asm->OutStreamer.AddComment("External Name");
3192 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003193 }
3194
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003195 Asm->OutStreamer.AddComment("End Mark");
3196 Asm->EmitInt32(0);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003197 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3198 ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00003199}
3200
Devang Patelec13b4f2009-11-24 01:14:22 +00003201void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00003202 // Start the dwarf pubnames section.
3203 Asm->OutStreamer.SwitchSection(
3204 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003205 Asm->OutStreamer.AddComment("Length of Public Types Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003206 Asm->EmitLabelDifference(
3207 Asm->GetTempSymbol("pubtypes_end", ModuleCU->getID()),
3208 Asm->GetTempSymbol("pubtypes_begin", ModuleCU->getID()), 4);
Devang Patelec13b4f2009-11-24 01:14:22 +00003209
Chris Lattnerb93558d2010-04-04 19:25:43 +00003210 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3211 ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00003212
Chris Lattner97c69b72010-04-04 18:52:31 +00003213 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
Devang Patel40b6cda2010-02-02 03:47:27 +00003214 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patelec13b4f2009-11-24 01:14:22 +00003215
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003216 Asm->OutStreamer.AddComment("Offset of Compilation ModuleCU Info");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003217 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3218 DwarfInfoSectionSym);
Devang Patelec13b4f2009-11-24 01:14:22 +00003219
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003220 Asm->OutStreamer.AddComment("Compilation ModuleCU Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003221 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3222 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3223 4);
Devang Patelec13b4f2009-11-24 01:14:22 +00003224
3225 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
3226 for (StringMap<DIE*>::const_iterator
3227 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3228 const char *Name = GI->getKeyData();
3229 DIE * Entity = GI->second;
3230
Chris Lattner97c69b72010-04-04 18:52:31 +00003231 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Devang Patel40b6cda2010-02-02 03:47:27 +00003232 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00003233
Chris Lattner97c69b72010-04-04 18:52:31 +00003234 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Devang Patel0ceb4892010-02-02 03:37:03 +00003235 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00003236 }
3237
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003238 Asm->OutStreamer.AddComment("End Mark");
3239 Asm->EmitInt32(0);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003240 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3241 ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00003242}
3243
Devang Patelc50078e2009-11-21 02:48:08 +00003244/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003245///
Devang Patelc50078e2009-11-21 02:48:08 +00003246void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003247 // Check to see if it is worth the effort.
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003248 if (StringPool.empty()) return;
3249
3250 // Start the dwarf str section.
3251 Asm->OutStreamer.SwitchSection(
Chris Lattner73266f92009-08-19 05:49:37 +00003252 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003253
Chris Lattner0f093f82010-03-13 02:17:42 +00003254 // Get all of the string pool entries and put them in an array by their ID so
3255 // we can sort them.
3256 SmallVector<std::pair<unsigned,
3257 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3258
3259 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3260 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3261 Entries.push_back(std::make_pair(I->second.second, &*I));
3262
3263 array_pod_sort(Entries.begin(), Entries.end());
3264
3265 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003266 // Emit a label for reference from debug information entries.
Chris Lattner0f093f82010-03-13 02:17:42 +00003267 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003268
3269 // Emit the string itself.
Chris Lattner0f093f82010-03-13 02:17:42 +00003270 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00003271 }
3272}
3273
Devang Patelc50078e2009-11-21 02:48:08 +00003274/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003275///
Devang Patelc50078e2009-11-21 02:48:08 +00003276void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003277 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00003278 Asm->OutStreamer.SwitchSection(
3279 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003280}
3281
3282/// EmitDebugARanges - Emit visible names into a debug aranges section.
3283///
3284void DwarfDebug::EmitDebugARanges() {
3285 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003286 Asm->OutStreamer.SwitchSection(
3287 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003288}
3289
Devang Patelc50078e2009-11-21 02:48:08 +00003290/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003291///
Devang Patelc50078e2009-11-21 02:48:08 +00003292void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003293 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003294 Asm->OutStreamer.SwitchSection(
Devang Patel146c6f72010-04-16 23:33:45 +00003295 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patelce7bf012010-04-27 19:46:33 +00003296 unsigned char Size = Asm->getTargetData().getPointerSize();
3297 for (SmallVector<const MCSymbol *, 8>::iterator
3298 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3299 I != E; ++I) {
3300 if (*I)
3301 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel146c6f72010-04-16 23:33:45 +00003302 else
Devang Patelce7bf012010-04-27 19:46:33 +00003303 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel146c6f72010-04-16 23:33:45 +00003304 }
Bill Wendling55fccda2009-05-20 23:21:38 +00003305}
3306
Devang Patelc50078e2009-11-21 02:48:08 +00003307/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003308///
Devang Patelc50078e2009-11-21 02:48:08 +00003309void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00003310 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00003311 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00003312 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00003313 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00003314 }
3315}
3316
Devang Patelc50078e2009-11-21 02:48:08 +00003317/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00003318/// Section Header:
3319/// 1. length of section
3320/// 2. Dwarf version number
3321/// 3. address size.
3322///
3323/// Entries (one "entry" for each function that was inlined):
3324///
3325/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3326/// otherwise offset into __debug_str for regular function name.
3327/// 2. offset into __debug_str section for regular function name.
3328/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3329/// instances for the function.
3330///
3331/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3332/// inlined instance; the die_offset points to the inlined_subroutine die in the
3333/// __debug_info section, and the low_pc is the starting address for the
3334/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00003335void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003336 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003337 return;
3338
Devang Patel5a3d37f2009-06-29 20:45:18 +00003339 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00003340 return;
3341
Chris Lattner73266f92009-08-19 05:49:37 +00003342 Asm->OutStreamer.SwitchSection(
3343 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerad653482010-01-22 22:09:00 +00003344
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003345 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003346 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3347 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003348
Chris Lattnerb93558d2010-04-04 19:25:43 +00003349 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003350
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003351 Asm->OutStreamer.AddComment("Dwarf Version");
3352 Asm->EmitInt16(dwarf::DWARF_VERSION);
3353 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003354 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00003355
Devang Patel9c371c62010-05-07 20:54:48 +00003356 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel90a0fe32009-11-10 23:06:00 +00003357 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00003358
Devang Patel9c371c62010-05-07 20:54:48 +00003359 const MDNode *Node = *I;
3360 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbachb23f2422009-11-22 19:20:36 +00003361 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00003362 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00003363 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00003364 StringRef LName = SP.getLinkageName();
3365 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00003366
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003367 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnercc564642010-01-23 03:11:46 +00003368 if (LName.empty()) {
3369 Asm->OutStreamer.EmitBytes(Name, 0);
3370 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3371 } else
Chris Lattnerc06b4742010-04-04 23:25:33 +00003372 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3373 DwarfStrSectionSym);
Devang Patel90a0fe32009-11-10 23:06:00 +00003374
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003375 Asm->OutStreamer.AddComment("Function name");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003376 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner26be1c12010-04-04 19:09:29 +00003377 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling55fccda2009-05-20 23:21:38 +00003378
Devang Patel90a0fe32009-11-10 23:06:00 +00003379 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00003380 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner97c69b72010-04-04 18:52:31 +00003381 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattneread58652010-03-09 00:31:02 +00003382 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00003383
Chris Lattner97c69b72010-04-04 18:52:31 +00003384 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003385 Asm->OutStreamer.EmitSymbolValue(LI->first,
3386 Asm->getTargetData().getPointerSize(),0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003387 }
3388 }
3389
Chris Lattnerb93558d2010-04-04 19:25:43 +00003390 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003391}