blob: 902b152c8e7496470befb7a75c8ff426c8963ed5 [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.
Devang Patel90a0fe32009-11-10 23:06:00 +0000171 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000172public:
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000173 // AbsVar may be NULL.
Devang Patelf0e70332010-05-20 16:36:41 +0000174 DbgVariable(DIVariable V) : Var(V), TheDIE(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000175
176 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000177 DIVariable getVariable() const { return Var; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000178 void setDIE(DIE *D) { TheDIE = D; }
179 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000180};
181
182//===----------------------------------------------------------------------===//
Devang Patelce7bf012010-04-27 19:46:33 +0000183/// DbgRange - This is used to track range of instructions with identical
184/// debug info scope.
185///
186typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
187
188//===----------------------------------------------------------------------===//
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000189/// DbgScope - This class is used to track scope information.
190///
Devang Patel4cb32c32009-11-16 21:53:40 +0000191class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000192 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000193 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelae89d3b2010-01-26 21:39:14 +0000194 // Location at which this scope is inlined.
Devang Patel9c371c62010-05-07 20:54:48 +0000195 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel90a0fe32009-11-10 23:06:00 +0000196 bool AbstractScope; // Abstract Scope
Devang Patel90ecd192009-10-01 18:25:23 +0000197 const MachineInstr *LastInsn; // Last instruction of this scope.
198 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patelce7bf012010-04-27 19:46:33 +0000199 unsigned DFSIn, DFSOut;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000200 // Scopes defined in scope. Contents not owned.
201 SmallVector<DbgScope *, 4> Scopes;
202 // Variables declared in scope. Contents owned.
203 SmallVector<DbgVariable *, 8> Variables;
Devang Patelce7bf012010-04-27 19:46:33 +0000204 SmallVector<DbgRange, 4> Ranges;
Owen Anderson696d4862009-06-24 22:53:20 +0000205 // Private state for dump()
206 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000207public:
Devang Patel9c371c62010-05-07 20:54:48 +0000208 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000209 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patelce7bf012010-04-27 19:46:33 +0000210 LastInsn(0), FirstInsn(0),
211 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000212 virtual ~DbgScope();
213
214 // Accessors.
215 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000216 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000217 DIDescriptor getDesc() const { return Desc; }
Devang Patel9c371c62010-05-07 20:54:48 +0000218 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
219 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000220 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
221 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patelce7bf012010-04-27 19:46:33 +0000222 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
223
224 /// openInsnRange - This scope covers instruction range starting from MI.
225 void openInsnRange(const MachineInstr *MI) {
226 if (!FirstInsn)
227 FirstInsn = MI;
228
229 if (Parent)
230 Parent->openInsnRange(MI);
231 }
232
233 /// extendInsnRange - Extend the current instruction range covered by
234 /// this scope.
235 void extendInsnRange(const MachineInstr *MI) {
236 assert (FirstInsn && "MI Range is not open!");
237 LastInsn = MI;
238 if (Parent)
239 Parent->extendInsnRange(MI);
240 }
241
242 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
243 /// until now. This is used when a new scope is encountered while walking
244 /// machine instructions.
245 void closeInsnRange(DbgScope *NewScope = NULL) {
246 assert (LastInsn && "Last insn missing!");
247 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
248 FirstInsn = NULL;
249 LastInsn = NULL;
250 // If Parent dominates NewScope then do not close Parent's instruction
251 // range.
252 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
253 Parent->closeInsnRange(NewScope);
254 }
255
Devang Patel90a0fe32009-11-10 23:06:00 +0000256 void setAbstractScope() { AbstractScope = true; }
257 bool isAbstractScope() const { return AbstractScope; }
Devang Patelce7bf012010-04-27 19:46:33 +0000258
259 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
260 unsigned getDFSOut() const { return DFSOut; }
261 void setDFSOut(unsigned O) { DFSOut = O; }
262 unsigned getDFSIn() const { return DFSIn; }
263 void setDFSIn(unsigned I) { DFSIn = I; }
264 bool dominates(const DbgScope *S) {
265 if (S == this)
266 return true;
267 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
268 return true;
269 return false;
270 }
Devang Patel90a0fe32009-11-10 23:06:00 +0000271
Devang Patelc50078e2009-11-21 02:48:08 +0000272 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000273 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000274 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000275
Devang Patelc50078e2009-11-21 02:48:08 +0000276 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000277 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000278 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000279
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000280#ifndef NDEBUG
281 void dump() const;
282#endif
283};
Devang Patelce7bf012010-04-27 19:46:33 +0000284
Chris Lattner8b9430c2010-04-05 04:09:20 +0000285} // end llvm namespace
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000286
287#ifndef NDEBUG
288void DbgScope::dump() const {
David Greenedbc06132009-12-24 00:31:35 +0000289 raw_ostream &err = dbgs();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000290 err.indent(IndentLevel);
Devang Patel9c371c62010-05-07 20:54:48 +0000291 const MDNode *N = Desc;
Devang Patel90a0fe32009-11-10 23:06:00 +0000292 N->dump();
Devang Patel90a0fe32009-11-10 23:06:00 +0000293 if (AbstractScope)
294 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000295
296 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000297 if (!Scopes.empty())
298 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000299 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
300 if (Scopes[i] != this)
301 Scopes[i]->dump();
302
303 IndentLevel -= 2;
304}
305#endif
306
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000307DbgScope::~DbgScope() {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000308 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
309 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000310}
311
Chris Lattnerec9a1f42010-04-05 05:11:15 +0000312DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel7e5a86e2010-05-10 22:49:55 +0000313 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Chris Lattner2cb53302010-04-05 03:52:55 +0000314 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel146c6f72010-04-16 23:33:45 +0000315 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattner0f093f82010-03-13 02:17:42 +0000316 NextStringPoolNumber = 0;
Chris Lattner66143d22010-04-04 22:59:04 +0000317
Chris Lattner094932e2010-04-04 23:10:38 +0000318 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
319 DwarfStrSectionSym = TextSectionSym = 0;
Devang Patelce7bf012010-04-27 19:46:33 +0000320 DwarfDebugRangeSectionSym = 0;
321 FunctionBeginSym = 0;
Edwin Törökc669cb62010-04-07 10:44:46 +0000322 if (TimePassesIsEnabled) {
323 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
324 beginModule(M);
325 } else {
326 beginModule(M);
327 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000328}
329DwarfDebug::~DwarfDebug() {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000330 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
331 DIEBlocks[j]->~DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000332}
333
Chris Lattner0f093f82010-03-13 02:17:42 +0000334MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
335 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
336 if (Entry.first) return Entry.first;
337
338 Entry.second = NextStringPoolNumber++;
Chris Lattnerb93558d2010-04-04 19:25:43 +0000339 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattner0f093f82010-03-13 02:17:42 +0000340}
341
342
Devang Patelc50078e2009-11-21 02:48:08 +0000343/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000344///
Devang Patelc50078e2009-11-21 02:48:08 +0000345void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000346 // Profile the node so that we can make it unique.
347 FoldingSetNodeID ID;
348 Abbrev.Profile(ID);
349
350 // Check the set for priors.
351 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
352
353 // If it's newly added.
354 if (InSet == &Abbrev) {
355 // Add to abbreviation list.
356 Abbreviations.push_back(&Abbrev);
357
358 // Assign the vector position + 1 as its number.
359 Abbrev.setNumber(Abbreviations.size());
360 } else {
361 // Assign existing abbreviation number.
362 Abbrev.setNumber(InSet->getNumber());
363 }
364}
365
Devang Patelc50078e2009-11-21 02:48:08 +0000366/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000367/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000368DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000369 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000370 return Value;
371}
372
Devang Patelc50078e2009-11-21 02:48:08 +0000373/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000374///
Devang Patelc50078e2009-11-21 02:48:08 +0000375void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000376 unsigned Form, uint64_t Integer) {
377 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000378 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000379 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000380}
381
Devang Patelc50078e2009-11-21 02:48:08 +0000382/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000383///
Devang Patelc50078e2009-11-21 02:48:08 +0000384void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000385 unsigned Form, int64_t Integer) {
386 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000387 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000388 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000389}
390
Devang Pateldf0f2152009-12-02 15:25:16 +0000391/// addString - Add a string attribute data and value. DIEString only
392/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000393void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnercc564642010-01-23 03:11:46 +0000394 StringRef String) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000395 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000396 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000397}
398
Devang Patelc50078e2009-11-21 02:48:08 +0000399/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000400///
Devang Patelc50078e2009-11-21 02:48:08 +0000401void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000402 const MCSymbol *Label) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000403 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000404 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000405}
406
Devang Patelc50078e2009-11-21 02:48:08 +0000407/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000408///
Devang Patelc50078e2009-11-21 02:48:08 +0000409void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000410 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000411 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000412 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000413}
414
Chris Lattner855b6bb2010-04-05 05:24:55 +0000415/// addDIEEntry - Add a DIE attribute data and value.
416///
417void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
418 DIE *Entry) {
419 Die->addValue(Attribute, Form, createDIEEntry(Entry));
420}
421
422
Devang Patelc50078e2009-11-21 02:48:08 +0000423/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000424///
Devang Patelc50078e2009-11-21 02:48:08 +0000425void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000426 DIEBlock *Block) {
Chris Lattner352c8e22010-04-05 00:18:22 +0000427 Block->ComputeSize(Asm);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000428 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patelc50078e2009-11-21 02:48:08 +0000429 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000430}
431
Devang Patelc50078e2009-11-21 02:48:08 +0000432/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000433/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000434void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel532a9a82010-05-07 23:33:41 +0000435 // Verify variable.
436 if (!V->Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000437 return;
438
439 unsigned Line = V->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000440 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
441 V->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000442 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000443 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
444 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000445}
446
Devang Patelc50078e2009-11-21 02:48:08 +0000447/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000448/// entry.
Devang Patel4942c4b2010-05-07 23:19:07 +0000449void DwarfDebug::addSourceLine(DIE *Die, const DIGlobalVariable *G) {
Devang Patel532a9a82010-05-07 23:33:41 +0000450 // Verify global variable.
451 if (!G->Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000452 return;
453
454 unsigned Line = G->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000455 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
456 G->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000457 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000458 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
459 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000460}
Devang Patel318d70d2009-08-31 22:47:13 +0000461
Devang Patelc50078e2009-11-21 02:48:08 +0000462/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000463/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000464void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel532a9a82010-05-07 23:33:41 +0000465 // Verify subprogram.
466 if (!SP->Verify())
Devang Patel318d70d2009-08-31 22:47:13 +0000467 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000468 // If the line number is 0, don't add it.
469 if (SP->getLineNumber() == 0)
470 return;
471
Devang Patel318d70d2009-08-31 22:47:13 +0000472 unsigned Line = SP->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000473 if (!SP->getContext().Verify())
474 return;
Devang Patele485a5d2010-03-24 21:30:35 +0000475 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
476 SP->getFilename());
Devang Patel318d70d2009-08-31 22:47:13 +0000477 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000478 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
479 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000480}
481
Devang Patelc50078e2009-11-21 02:48:08 +0000482/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000483/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000484void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel532a9a82010-05-07 23:33:41 +0000485 // Verify type.
486 if (!Ty->Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000487 return;
488
489 unsigned Line = Ty->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000490 if (!Ty->getContext().Verify())
491 return;
492 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
493 Ty->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000494 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000495 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
496 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000497}
498
Devang Patel8287d662009-12-15 19:16:48 +0000499/// addSourceLine - Add location information to specified debug information
500/// entry.
501void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
Devang Patel532a9a82010-05-07 23:33:41 +0000502 // Verify namespace.
503 if (!NS->Verify())
Devang Patel8287d662009-12-15 19:16:48 +0000504 return;
505
506 unsigned Line = NS->getLineNumber();
507 StringRef FN = NS->getFilename();
508 StringRef Dir = NS->getDirectory();
509
510 unsigned FileID = GetOrCreateSourceID(Dir, FN);
511 assert(FileID && "Invalid file id");
512 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
513 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
514}
515
Caroline Tice248d5572009-08-31 21:19:37 +0000516/* Byref variables, in Blocks, are declared by the programmer as
517 "SomeType VarName;", but the compiler creates a
518 __Block_byref_x_VarName struct, and gives the variable VarName
519 either the struct, or a pointer to the struct, as its type. This
520 is necessary for various behind-the-scenes things the compiler
521 needs to do with by-reference variables in blocks.
522
523 However, as far as the original *programmer* is concerned, the
524 variable should still have type 'SomeType', as originally declared.
525
526 The following function dives into the __Block_byref_x_VarName
527 struct to find the original type of the variable. This will be
528 passed back to the code generating the type for the Debug
529 Information Entry for the variable 'VarName'. 'VarName' will then
530 have the original type 'SomeType' in its debug information.
531
532 The original type 'SomeType' will be the type of the field named
533 'VarName' inside the __Block_byref_x_VarName struct.
534
535 NOTE: In order for this to not completely fail on the debugger
536 side, the Debug Information Entry for the variable VarName needs to
537 have a DW_AT_location that tells the debugger how to unwind through
538 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000539 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000540
541/// Find the type the programmer originally declared the variable to be
542/// and return that type.
543///
Devang Patelc50078e2009-11-21 02:48:08 +0000544DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000545
546 DIType subType = Ty;
547 unsigned tag = Ty.getTag();
548
549 if (tag == dwarf::DW_TAG_pointer_type) {
Devang Patel19302aa2010-05-07 18:11:54 +0000550 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Tice248d5572009-08-31 21:19:37 +0000551 subType = DTy.getTypeDerivedFrom();
552 }
553
Devang Patel19302aa2010-05-07 18:11:54 +0000554 DICompositeType blockStruct = DICompositeType(subType);
Caroline Tice248d5572009-08-31 21:19:37 +0000555 DIArray Elements = blockStruct.getTypeArray();
556
Caroline Tice248d5572009-08-31 21:19:37 +0000557 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
558 DIDescriptor Element = Elements.getElement(i);
Devang Patel19302aa2010-05-07 18:11:54 +0000559 DIDerivedType DT = DIDerivedType(Element);
Devang Patel7f75bbe2009-11-25 17:36:49 +0000560 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000561 return (DT.getTypeDerivedFrom());
562 }
563
564 return Ty;
565}
566
Devang Patelc50078e2009-11-21 02:48:08 +0000567/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000568/// and generate the DWARF information necessary to find the actual variable
569/// given the extra address information encoded in the DIVariable, starting from
570/// the starting location. Add the DWARF information to the die.
571///
Devang Patelc50078e2009-11-21 02:48:08 +0000572void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000573 unsigned Attribute,
574 const MachineLocation &Location) {
575 const DIVariable &VD = DV->getVariable();
576 DIType Ty = VD.getType();
577
578 // Decode the original location, and use that as the start of the byref
579 // variable's location.
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000580 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000581 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000582 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000583
584 if (Location.isReg()) {
585 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000586 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000587 } else {
588 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000589 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
590 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000591 }
592 } else {
593 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000594 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000595 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000596 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
597 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000598 }
599
Devang Patelc50078e2009-11-21 02:48:08 +0000600 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000601 }
602
603 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
604 uint64_t Element = VD.getAddrElement(i);
605
606 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000607 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
608 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000609 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000610 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000611 } else llvm_unreachable("unknown DIFactory Opcode");
612 }
613
614 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000615 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000616}
617
Caroline Tice248d5572009-08-31 21:19:37 +0000618/* Byref variables, in Blocks, are declared by the programmer as "SomeType
619 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
620 gives the variable VarName either the struct, or a pointer to the struct, as
621 its type. This is necessary for various behind-the-scenes things the
622 compiler needs to do with by-reference variables in Blocks.
623
624 However, as far as the original *programmer* is concerned, the variable
625 should still have type 'SomeType', as originally declared.
626
Devang Patelc50078e2009-11-21 02:48:08 +0000627 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000628 struct to find the original type of the variable, which is then assigned to
629 the variable's Debug Information Entry as its real type. So far, so good.
630 However now the debugger will expect the variable VarName to have the type
631 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000632 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000633 pointers and struct to find the actual variable of type SomeType.
634
635 The following function does just that. We start by getting
636 the "normal" location for the variable. This will be the location
637 of either the struct __Block_byref_x_VarName or the pointer to the
638 struct __Block_byref_x_VarName.
639
640 The struct will look something like:
641
642 struct __Block_byref_x_VarName {
643 ... <various fields>
644 struct __Block_byref_x_VarName *forwarding;
645 ... <various other fields>
646 SomeType VarName;
647 ... <maybe more fields>
648 };
649
650 If we are given the struct directly (as our starting point) we
651 need to tell the debugger to:
652
653 1). Add the offset of the forwarding field.
654
Dan Gohmandf1a7ff2010-02-10 16:03:48 +0000655 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000656 struct to use (the real one may have been copied onto the heap).
657
658 3). Add the offset for the field VarName, to find the actual variable.
659
660 If we started with a pointer to the struct, then we need to
661 dereference that pointer first, before the other steps.
662 Translating this into DWARF ops, we will need to append the following
663 to the current location description for the variable:
664
665 DW_OP_deref -- optional, if we start with a pointer
666 DW_OP_plus_uconst <forward_fld_offset>
667 DW_OP_deref
668 DW_OP_plus_uconst <varName_fld_offset>
669
670 That is what this function does. */
671
Devang Patelc50078e2009-11-21 02:48:08 +0000672/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000673/// provided, and generate the DWARF information necessary to find the
674/// actual Block variable (navigating the Block struct) based on the
675/// starting location. Add the DWARF information to the die. For
676/// more information, read large comment just above here.
677///
Devang Patelc50078e2009-11-21 02:48:08 +0000678void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000679 unsigned Attribute,
680 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000681 const DIVariable &VD = DV->getVariable();
682 DIType Ty = VD.getType();
683 DIType TmpTy = Ty;
684 unsigned Tag = Ty.getTag();
685 bool isPointer = false;
686
Devang Patel7f75bbe2009-11-25 17:36:49 +0000687 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000688
689 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel19302aa2010-05-07 18:11:54 +0000690 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Tice248d5572009-08-31 21:19:37 +0000691 TmpTy = DTy.getTypeDerivedFrom();
692 isPointer = true;
693 }
694
Devang Patel19302aa2010-05-07 18:11:54 +0000695 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Tice248d5572009-08-31 21:19:37 +0000696
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000697 // Find the __forwarding field and the variable field in the __Block_byref
698 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000699 DIArray Fields = blockStruct.getTypeArray();
700 DIDescriptor varField = DIDescriptor();
701 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000702
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000703 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
704 DIDescriptor Element = Fields.getElement(i);
Devang Patel19302aa2010-05-07 18:11:54 +0000705 DIDerivedType DT = DIDerivedType(Element);
Devang Patel7f75bbe2009-11-25 17:36:49 +0000706 StringRef fieldName = DT.getName();
707 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000708 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000709 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000710 varField = Element;
711 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000712
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000713 // Get the offsets for the forwarding field and the variable field.
Chris Lattner5df82552010-03-31 06:06:37 +0000714 unsigned forwardingFieldOffset =
Devang Patel19302aa2010-05-07 18:11:54 +0000715 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner5df82552010-03-31 06:06:37 +0000716 unsigned varFieldOffset =
Devang Patel19302aa2010-05-07 18:11:54 +0000717 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000718
Mike Stump2fd84e22009-09-24 23:21:26 +0000719 // Decode the original location, and use that as the start of the byref
720 // variable's location.
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000721 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000722 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000723 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000724
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000725 if (Location.isReg()) {
726 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000727 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000728 else {
729 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000730 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
731 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000732 }
733 } else {
734 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000735 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000736 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000737 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
738 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000739 }
Caroline Tice248d5572009-08-31 21:19:37 +0000740
Devang Patelc50078e2009-11-21 02:48:08 +0000741 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000742 }
Caroline Tice248d5572009-08-31 21:19:37 +0000743
Mike Stump2fd84e22009-09-24 23:21:26 +0000744 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000745 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000746 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000747 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000748
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000749 // Next add the offset for the '__forwarding' field:
750 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
751 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000752 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000753 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
754 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000755 }
Caroline Tice248d5572009-08-31 21:19:37 +0000756
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000757 // Now dereference the __forwarding field to get to the real __Block_byref
758 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000759 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000760
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000761 // Now that we've got the real __Block_byref... struct, add the offset
762 // for the variable's field to get to the location of the actual variable:
763 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000764 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000765 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
766 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000767 }
Caroline Tice248d5572009-08-31 21:19:37 +0000768
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000769 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000770 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000771}
772
Devang Patelc50078e2009-11-21 02:48:08 +0000773/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000774/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000775void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000776 const MachineLocation &Location) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000777 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000778 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000779 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000780
781 if (Location.isReg()) {
782 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000783 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000784 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000785 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
786 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000787 }
788 } else {
789 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000790 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000791 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000792 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
793 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000794 }
795
Devang Patelc50078e2009-11-21 02:48:08 +0000796 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000797 }
798
Devang Patelc50078e2009-11-21 02:48:08 +0000799 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000800}
801
Devang Patel7e625392010-04-28 01:03:09 +0000802/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patelf0e70332010-05-20 16:36:41 +0000803bool DwarfDebug::addRegisterAddress(DIE *Die, const MCSymbol *VS,
Devang Patel7e625392010-04-28 01:03:09 +0000804 const MachineOperand &MO) {
805 assert (MO.isReg() && "Invalid machine operand!");
806 if (!MO.getReg())
807 return false;
808 MachineLocation Location;
809 Location.set(MO.getReg());
810 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patelf0e70332010-05-20 16:36:41 +0000811 if (VS)
Devang Patel7e625392010-04-28 01:03:09 +0000812 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
813 return true;
814}
815
816/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelf0e70332010-05-20 16:36:41 +0000817bool DwarfDebug::addConstantValue(DIE *Die, const MCSymbol *VS,
Devang Patel7e625392010-04-28 01:03:09 +0000818 const MachineOperand &MO) {
819 assert (MO.isImm() && "Invalid machine operand!");
820 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
821 unsigned Imm = MO.getImm();
822 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
823 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patelf0e70332010-05-20 16:36:41 +0000824 if (VS)
Devang Patel7e625392010-04-28 01:03:09 +0000825 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
826 return true;
827}
828
829/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patelf0e70332010-05-20 16:36:41 +0000830bool DwarfDebug::addConstantFPValue(DIE *Die, const MCSymbol *VS,
Devang Patel7e625392010-04-28 01:03:09 +0000831 const MachineOperand &MO) {
832 assert (MO.isFPImm() && "Invalid machine operand!");
833 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
834 APFloat FPImm = MO.getFPImm()->getValueAPF();
835
836 // Get the raw data form of the floating point.
837 const APInt FltVal = FPImm.bitcastToAPInt();
838 const char *FltPtr = (const char*)FltVal.getRawData();
839
840 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
841 bool LittleEndian = Asm->getTargetData().isLittleEndian();
842 int Incr = (LittleEndian ? 1 : -1);
843 int Start = (LittleEndian ? 0 : NumBytes - 1);
844 int Stop = (LittleEndian ? NumBytes : -1);
845
846 // Output the constant to DWARF one byte at a time.
847 for (; Start != Stop; Start += Incr)
848 addUInt(Block, 0, dwarf::DW_FORM_data1,
849 (unsigned char)0xFF & FltPtr[Start]);
850
851 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patelf0e70332010-05-20 16:36:41 +0000852 if (VS)
853 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
Devang Patel7e625392010-04-28 01:03:09 +0000854 return true;
855}
856
857
Devang Patel1a8f9a82009-12-10 19:14:49 +0000858/// addToContextOwner - Add Die into the list of its context owner's children.
859void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel9e592492010-03-08 20:52:55 +0000860 if (Context.isType()) {
Devang Patel19302aa2010-05-07 18:11:54 +0000861 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patel1a8f9a82009-12-10 19:14:49 +0000862 ContextDIE->addChild(Die);
Devang Patel8287d662009-12-15 19:16:48 +0000863 } else if (Context.isNameSpace()) {
Devang Patel19302aa2010-05-07 18:11:54 +0000864 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel8287d662009-12-15 19:16:48 +0000865 ContextDIE->addChild(Die);
Devang Patel7e5a86e2010-05-10 22:49:55 +0000866 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patel1a8f9a82009-12-10 19:14:49 +0000867 ContextDIE->addChild(Die);
868 else
Devang Patel7e5a86e2010-05-10 22:49:55 +0000869 getCompileUnit(Context)->addDie(Die);
Devang Patel1a8f9a82009-12-10 19:14:49 +0000870}
871
Devang Patel7f139c12009-12-10 18:05:33 +0000872/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
873/// given DIType.
874DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel7e5a86e2010-05-10 22:49:55 +0000875 CompileUnit *TypeCU = getCompileUnit(Ty);
876 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel7f139c12009-12-10 18:05:33 +0000877 if (TyDIE)
878 return TyDIE;
879
880 // Create new type.
881 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel7e5a86e2010-05-10 22:49:55 +0000882 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel7f139c12009-12-10 18:05:33 +0000883 if (Ty.isBasicType())
Devang Patel19302aa2010-05-07 18:11:54 +0000884 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel7f139c12009-12-10 18:05:33 +0000885 else if (Ty.isCompositeType())
Devang Patel19302aa2010-05-07 18:11:54 +0000886 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel7f139c12009-12-10 18:05:33 +0000887 else {
888 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel19302aa2010-05-07 18:11:54 +0000889 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel7f139c12009-12-10 18:05:33 +0000890 }
891
Devang Patel1a8f9a82009-12-10 19:14:49 +0000892 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel7f139c12009-12-10 18:05:33 +0000893 return TyDIE;
894}
895
Devang Patelc50078e2009-11-21 02:48:08 +0000896/// addType - Add a new type attribute to the specified entity.
Devang Patelfe0be132009-12-09 18:24:21 +0000897void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9738af92010-05-07 21:45:47 +0000898 if (!Ty.Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000899 return;
900
901 // Check for pre-existence.
Devang Patel7e5a86e2010-05-10 22:49:55 +0000902 CompileUnit *TypeCU = getCompileUnit(Ty);
903 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000904 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000905 if (Entry) {
906 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000907 return;
908 }
909
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000910 // Construct type.
Devang Patel7f139c12009-12-10 18:05:33 +0000911 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000912
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000913 // Set up proxy.
914 Entry = createDIEEntry(Buffer);
Devang Patel7e5a86e2010-05-10 22:49:55 +0000915 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000916
Devang Patelc50078e2009-11-21 02:48:08 +0000917 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000918}
919
Devang Patelc50078e2009-11-21 02:48:08 +0000920/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patelfe0be132009-12-09 18:24:21 +0000921void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000922 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000923 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000924 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000925 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000926 BTy.getEncoding());
927
928 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000929 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000930 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000931 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000932 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000933}
934
Devang Patelc50078e2009-11-21 02:48:08 +0000935/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patelfe0be132009-12-09 18:24:21 +0000936void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000937 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000938 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000939 uint64_t Size = DTy.getSizeInBits() >> 3;
940 unsigned Tag = DTy.getTag();
941
942 // FIXME - Workaround for templates.
943 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
944
945 Buffer.setTag(Tag);
946
947 // Map to main type, void will not have a type.
948 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelfe0be132009-12-09 18:24:21 +0000949 addType(&Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000950
951 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000952 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000953 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000954
955 // Add size if non-zero (derived types might be zero-sized.)
956 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000957 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000958
959 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000960 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000961 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000962}
963
Devang Patelc50078e2009-11-21 02:48:08 +0000964/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000965void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000966 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000967 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000968
969 uint64_t Size = CTy.getSizeInBits() >> 3;
970 unsigned Tag = CTy.getTag();
971 Buffer.setTag(Tag);
972
973 switch (Tag) {
974 case dwarf::DW_TAG_vector_type:
975 case dwarf::DW_TAG_array_type:
Devang Patelfe0be132009-12-09 18:24:21 +0000976 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000977 break;
978 case dwarf::DW_TAG_enumeration_type: {
979 DIArray Elements = CTy.getTypeArray();
980
981 // Add enumerators to enumeration type.
982 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
983 DIE *ElemDie = NULL;
Devang Patel19302aa2010-05-07 18:11:54 +0000984 DIDescriptor Enum(Elements.getElement(i));
Devang Patel9e592492010-03-08 20:52:55 +0000985 if (Enum.isEnumerator()) {
Devang Patel19302aa2010-05-07 18:11:54 +0000986 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patelc50078e2009-11-21 02:48:08 +0000987 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000988 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000989 }
990 }
991 break;
992 case dwarf::DW_TAG_subroutine_type: {
993 // Add return type.
994 DIArray Elements = CTy.getTypeArray();
995 DIDescriptor RTy = Elements.getElement(0);
Devang Patel19302aa2010-05-07 18:11:54 +0000996 addType(&Buffer, DIType(RTy));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000997
998 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000999 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001000
1001 // Add arguments.
1002 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1003 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1004 DIDescriptor Ty = Elements.getElement(i);
Devang Patel19302aa2010-05-07 18:11:54 +00001005 addType(Arg, DIType(Ty));
Devang Patelc50078e2009-11-21 02:48:08 +00001006 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001007 }
1008 }
1009 break;
1010 case dwarf::DW_TAG_structure_type:
1011 case dwarf::DW_TAG_union_type:
1012 case dwarf::DW_TAG_class_type: {
1013 // Add elements to structure type.
1014 DIArray Elements = CTy.getTypeArray();
1015
1016 // A forward struct declared type may not have elements available.
Devang Patel9e592492010-03-08 20:52:55 +00001017 unsigned N = Elements.getNumElements();
1018 if (N == 0)
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001019 break;
1020
1021 // Add elements to structure type.
Devang Patel9e592492010-03-08 20:52:55 +00001022 for (unsigned i = 0; i < N; ++i) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001023 DIDescriptor Element = Elements.getElement(i);
1024 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001025 if (Element.isSubprogram())
Devang Patel19302aa2010-05-07 18:11:54 +00001026 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel9e592492010-03-08 20:52:55 +00001027 else if (Element.isVariable()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001028 DIVariable DV(Element);
Devang Patel423dfa02010-01-30 01:08:30 +00001029 ElemDie = new DIE(dwarf::DW_TAG_variable);
1030 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1031 DV.getName());
1032 addType(ElemDie, DV.getType());
1033 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1034 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1035 addSourceLine(ElemDie, &DV);
Devang Patel9e592492010-03-08 20:52:55 +00001036 } else if (Element.isDerivedType())
Devang Patel19302aa2010-05-07 18:11:54 +00001037 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel9e592492010-03-08 20:52:55 +00001038 else
1039 continue;
Devang Patelc50078e2009-11-21 02:48:08 +00001040 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001041 }
1042
Devang Patel20b32102009-08-27 23:51:51 +00001043 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +00001044 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001045
1046 unsigned RLang = CTy.getRunTimeLang();
1047 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +00001048 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001049 dwarf::DW_FORM_data1, RLang);
Devang Patel5ae52402010-01-26 21:16:06 +00001050
1051 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel19302aa2010-05-07 18:11:54 +00001052 if (DIDescriptor(ContainingType).isCompositeType())
Devang Patel5ae52402010-01-26 21:16:06 +00001053 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel19302aa2010-05-07 18:11:54 +00001054 getOrCreateTypeDIE(DIType(ContainingType)));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001055 break;
1056 }
1057 default:
1058 break;
1059 }
1060
1061 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001062 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001063 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001064
Devang Pateldc73c372010-01-29 18:34:58 +00001065 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001066 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1067 // Add size if non-zero (derived types might be zero-sized.)
1068 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +00001069 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001070 else {
1071 // Add zero size if it is not a forward declaration.
1072 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001073 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001074 else
Devang Patelc50078e2009-11-21 02:48:08 +00001075 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001076 }
1077
1078 // Add source line info if available.
1079 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001080 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001081 }
1082}
1083
Devang Patelc50078e2009-11-21 02:48:08 +00001084/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1085void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001086 int64_t L = SR.getLo();
1087 int64_t H = SR.getHi();
1088 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1089
Devang Patelc50078e2009-11-21 02:48:08 +00001090 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +00001091 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +00001092 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +00001093 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001094
Devang Patelc50078e2009-11-21 02:48:08 +00001095 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001096}
1097
Devang Patelc50078e2009-11-21 02:48:08 +00001098/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +00001099void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001100 DICompositeType *CTy) {
1101 Buffer.setTag(dwarf::DW_TAG_array_type);
1102 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001103 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001104
1105 // Emit derived type.
Devang Patelfe0be132009-12-09 18:24:21 +00001106 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001107 DIArray Elements = CTy->getTypeArray();
1108
Devang Patel1233f912009-11-21 00:31:03 +00001109 // Get an anonymous type for index type.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001110 CompileUnit *TheCU = getCompileUnit(*CTy);
1111 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel1233f912009-11-21 00:31:03 +00001112 if (!IdxTy) {
1113 // Construct an anonymous type for index type.
1114 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +00001115 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1116 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +00001117 dwarf::DW_ATE_signed);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001118 TheCU->addDie(IdxTy);
1119 TheCU->setIndexTyDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +00001120 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001121
1122 // Add subranges to array type.
1123 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1124 DIDescriptor Element = Elements.getElement(i);
1125 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel19302aa2010-05-07 18:11:54 +00001126 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001127 }
1128}
1129
Devang Patelc50078e2009-11-21 02:48:08 +00001130/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel9e592492010-03-08 20:52:55 +00001131DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001132 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel9e592492010-03-08 20:52:55 +00001133 StringRef Name = ETy.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001134 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel9e592492010-03-08 20:52:55 +00001135 int64_t Value = ETy.getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001136 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001137 return Enumerator;
1138}
1139
Devang Patel141e1c42010-01-05 01:46:14 +00001140/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1141/// printer to not emit usual symbol prefix before the symbol name is used then
1142/// return linkage name after skipping this special LLVM prefix.
1143static StringRef getRealLinkageName(StringRef LinkageName) {
1144 char One = '\1';
1145 if (LinkageName.startswith(StringRef(&One, 1)))
1146 return LinkageName.substr(1);
1147 return LinkageName;
1148}
1149
Devang Patelc50078e2009-11-21 02:48:08 +00001150/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patelfe0be132009-12-09 18:24:21 +00001151DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001152 // If the global variable was optmized out then no need to create debug info
1153 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001154 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001155 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001156
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001157 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001158 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001159 GV.getDisplayName());
1160
Devang Patel7f75bbe2009-11-25 17:36:49 +00001161 StringRef LinkageName = GV.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001162 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001163 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001164 getRealLinkageName(LinkageName));
1165
Devang Patelfe0be132009-12-09 18:24:21 +00001166 addType(GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001167 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001168 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1169 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001170
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001171 return GVDie;
1172}
1173
Devang Patelc50078e2009-11-21 02:48:08 +00001174/// createMemberDIE - Create new member DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001175DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001176 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001177 StringRef Name = DT.getName();
1178 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001179 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001180
Devang Patelfe0be132009-12-09 18:24:21 +00001181 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001182
Devang Patelc50078e2009-11-21 02:48:08 +00001183 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001184
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001185 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001186 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001187
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001188 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001189 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001190
1191 if (Size != FieldSize) {
1192 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001193 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1194 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001195
1196 uint64_t Offset = DT.getOffsetInBits();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001197 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1198 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer631e3e32010-01-07 17:50:57 +00001199 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001200 Offset -= FieldOffset;
1201
1202 // Maybe we need to work from the other end.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001203 if (Asm->getTargetData().isLittleEndian())
1204 Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001205 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001206
Devang Patel7d9fe582009-11-04 22:06:12 +00001207 // Here WD_AT_data_member_location points to the anonymous
1208 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001209 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001210
1211 } else
1212 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001213 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001214
Devang Patel84aba942010-02-03 20:08:48 +00001215 if (DT.getTag() == dwarf::DW_TAG_inheritance
1216 && DT.isVirtual()) {
1217
1218 // For C++, virtual base classes are not at fixed offset. Use following
1219 // expression to extract appropriate offset from vtable.
1220 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1221
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001222 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel84aba942010-02-03 20:08:48 +00001223 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1224 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1225 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1226 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1227 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1228 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1229 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1230
1231 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1232 VBaseLocationDie);
1233 } else
1234 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001235
1236 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001237 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001238 dwarf::DW_ACCESS_protected);
1239 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001240 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001241 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-12-03 19:11:07 +00001242 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1243 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1244 dwarf::DW_ACCESS_public);
1245 if (DT.isVirtual())
1246 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1247 dwarf::DW_VIRTUALITY_virtual);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001248 return MemberDie;
1249}
1250
Devang Patel814a12c2009-12-14 16:18:45 +00001251/// createSubprogramDIE - Create new DIE using SP.
1252DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001253 CompileUnit *SPCU = getCompileUnit(SP);
1254 DIE *SPDie = SPCU->getDIE(SP);
Devang Patel814a12c2009-12-14 16:18:45 +00001255 if (SPDie)
1256 return SPDie;
1257
1258 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel36cd9f82010-03-02 17:58:15 +00001259 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel0ab194f2010-03-02 01:26:20 +00001260 if (!SP.getName().empty())
1261 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001262
Devang Patel7f75bbe2009-11-25 17:36:49 +00001263 StringRef LinkageName = SP.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001264 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001265 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001266 getRealLinkageName(LinkageName));
1267
Devang Patelc50078e2009-11-21 02:48:08 +00001268 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001269
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001270 // Add prototyped tag, if C or ObjC.
1271 unsigned Lang = SP.getCompileUnit().getLanguage();
1272 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1273 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001274 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001275
1276 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001277 DICompositeType SPTy = SP.getType();
1278 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001279 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001280
Devang Patel9e592492010-03-08 20:52:55 +00001281 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelfe0be132009-12-09 18:24:21 +00001282 addType(SPDie, SPTy);
Devang Patelc1df8792009-12-03 01:25:38 +00001283 else
Devang Patel19302aa2010-05-07 18:11:54 +00001284 addType(SPDie, DIType(Args.getElement(0)));
Devang Patelc1df8792009-12-03 01:25:38 +00001285
Devang Patel188c85d2009-12-03 19:11:07 +00001286 unsigned VK = SP.getVirtuality();
1287 if (VK) {
1288 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001289 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel188c85d2009-12-03 19:11:07 +00001290 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1291 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1292 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel7d707f92010-01-19 06:19:05 +00001293 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel19302aa2010-05-07 18:11:54 +00001294 SP.getContainingType()));
Devang Patel188c85d2009-12-03 19:11:07 +00001295 }
1296
Devang Patel814a12c2009-12-14 16:18:45 +00001297 if (MakeDecl || !SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001298 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001299
1300 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-12-03 01:25:38 +00001301 // be handled while processing variables.
1302 DICompositeType SPTy = SP.getType();
1303 DIArray Args = SPTy.getTypeArray();
1304 unsigned SPTag = SPTy.getTag();
1305
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001306 if (SPTag == dwarf::DW_TAG_subroutine_type)
1307 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1308 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel19302aa2010-05-07 18:11:54 +00001309 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelc5dc7252010-02-06 01:02:37 +00001310 addType(Arg, ATy);
1311 if (ATy.isArtificial())
1312 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc50078e2009-11-21 02:48:08 +00001313 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001314 }
1315 }
1316
Devang Patelbf7d00a2010-02-03 19:57:19 +00001317 if (SP.isArtificial())
1318 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1319
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001320 if (!SP.isLocalToUnit())
1321 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel91474172010-04-19 19:14:02 +00001322
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001323 if (SP.isOptimized())
1324 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1325
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001326 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001327 SPCU->insertDIE(SP, SPDie);
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001328
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001329 return SPDie;
1330}
1331
Devang Patel9c371c62010-05-07 20:54:48 +00001332DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattner8143ce82010-03-31 05:36:29 +00001333 assert(N && "Invalid Scope encoding!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001334
1335 DbgScope *AScope = AbstractScopes.lookup(N);
1336 if (AScope)
1337 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001338
Devang Patel90a0fe32009-11-10 23:06:00 +00001339 DbgScope *Parent = NULL;
1340
1341 DIDescriptor Scope(N);
1342 if (Scope.isLexicalBlock()) {
1343 DILexicalBlock DB(N);
1344 DIDescriptor ParentDesc = DB.getContext();
Devang Patel19302aa2010-05-07 18:11:54 +00001345 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel90a0fe32009-11-10 23:06:00 +00001346 }
1347
1348 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1349
1350 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001351 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001352 AScope->setAbstractScope();
1353 AbstractScopes[N] = AScope;
1354 if (DIDescriptor(N).isSubprogram())
1355 AbstractScopesList.push_back(AScope);
1356 return AScope;
1357}
Devang Patel6a260102009-10-01 20:31:14 +00001358
Devang Patel75c10622010-04-06 23:53:48 +00001359/// isSubprogramContext - Return true if Context is either a subprogram
1360/// or another context nested inside a subprogram.
Devang Patel9c371c62010-05-07 20:54:48 +00001361static bool isSubprogramContext(const MDNode *Context) {
Devang Patel75c10622010-04-06 23:53:48 +00001362 if (!Context)
1363 return false;
1364 DIDescriptor D(Context);
1365 if (D.isSubprogram())
1366 return true;
1367 if (D.isType())
Devang Patel19302aa2010-05-07 18:11:54 +00001368 return isSubprogramContext(DIType(Context).getContext());
Devang Patel75c10622010-04-06 23:53:48 +00001369 return false;
1370}
1371
Jim Grosbach652b7432009-11-21 23:12:12 +00001372/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001373/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1374/// If there are global variables in this scope then create and insert
1375/// DIEs for these variables.
Devang Patel9c371c62010-05-07 20:54:48 +00001376DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001377 CompileUnit *SPCU = getCompileUnit(SPNode);
1378 DIE *SPDie = SPCU->getDIE(SPNode);
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001379 assert(SPDie && "Unable to find subprogram DIE!");
1380 DISubprogram SP(SPNode);
Chris Lattnerdd21da82010-03-13 07:26:18 +00001381
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001382 // There is not any need to generate specification DIE for a function
1383 // defined at compile unit level. If a function is defined inside another
1384 // function then gdb prefers the definition at top level and but does not
1385 // expect specification DIE in parent function. So avoid creating
1386 // specification DIE for a function defined inside a function.
1387 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel75c10622010-04-06 23:53:48 +00001388 !SP.getContext().isFile() &&
Devang Patel19302aa2010-05-07 18:11:54 +00001389 !isSubprogramContext(SP.getContext())) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001390 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1391
1392 // Add arguments.
1393 DICompositeType SPTy = SP.getType();
1394 DIArray Args = SPTy.getTypeArray();
1395 unsigned SPTag = SPTy.getTag();
1396 if (SPTag == dwarf::DW_TAG_subroutine_type)
1397 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1398 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel19302aa2010-05-07 18:11:54 +00001399 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001400 addType(Arg, ATy);
1401 if (ATy.isArtificial())
1402 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1403 SPDie->addChild(Arg);
1404 }
1405 DIE *SPDeclDie = SPDie;
1406 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1407 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1408 SPDeclDie);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001409 SPCU->addDie(SPDie);
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001410 }
1411
1412 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1413 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1414 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1415 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1416 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1417 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1418 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelc5dc7252010-02-06 01:02:37 +00001419
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001420 return SPDie;
Devang Patel90a0fe32009-11-10 23:06:00 +00001421}
1422
Jim Grosbach652b7432009-11-21 23:12:12 +00001423/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001424/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1425DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patelce7bf012010-04-27 19:46:33 +00001426
1427 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1428 if (Scope->isAbstractScope())
1429 return ScopeDIE;
1430
1431 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1432 if (Ranges.empty())
1433 return 0;
1434
1435 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1436 if (Ranges.size() > 1) {
1437 // .debug_range section has not been laid out yet. Emit offset in
1438 // .debug_range as a uint, size 4, for now. emitDIE will handle
1439 // DW_AT_ranges appropriately.
1440 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1441 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1442 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1443 RE = Ranges.end(); RI != RE; ++RI) {
1444 DebugRangeSymbols.push_back(LabelsBeforeInsn.lookup(RI->first));
1445 DebugRangeSymbols.push_back(LabelsAfterInsn.lookup(RI->second));
1446 }
1447 DebugRangeSymbols.push_back(NULL);
1448 DebugRangeSymbols.push_back(NULL);
1449 return ScopeDIE;
1450 }
1451
1452 MCSymbol *Start = LabelsBeforeInsn.lookup(RI->first);
1453 MCSymbol *End = LabelsAfterInsn.lookup(RI->second);
1454
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001455 if (Start == 0 || End == 0) return 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00001456
Chris Lattner855e8f52010-03-09 01:58:53 +00001457 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1458 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattner5e423432010-03-09 01:51:43 +00001459
Devang Patelce7bf012010-04-27 19:46:33 +00001460 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1461 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel90a0fe32009-11-10 23:06:00 +00001462
1463 return ScopeDIE;
1464}
1465
Devang Patelc50078e2009-11-21 02:48:08 +00001466/// constructInlinedScopeDIE - This scope represents inlined body of
1467/// a function. Construct DIE to represent this concrete inlined copy
1468/// of the function.
1469DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patelce7bf012010-04-27 19:46:33 +00001470
1471 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1472 assert (Ranges.empty() == false
1473 && "DbgScope does not have instruction markers!");
1474
1475 // FIXME : .debug_inlined section specification does not clearly state how
1476 // to emit inlined scope that is split into multiple instruction ranges.
1477 // For now, use first instruction range and emit low_pc/high_pc pair and
1478 // corresponding .debug_inlined section entry for this pair.
1479 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1480 MCSymbol *StartLabel = LabelsBeforeInsn.lookup(RI->first);
1481 MCSymbol *EndLabel = LabelsAfterInsn.lookup(RI->second);
1482
1483 if (StartLabel == 0 || EndLabel == 0) {
1484 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1485 return 0;
1486 }
Chris Lattner855e8f52010-03-09 01:58:53 +00001487 assert(StartLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001488 "Invalid starting label for an inlined scope!");
Chris Lattner855e8f52010-03-09 01:58:53 +00001489 assert(EndLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001490 "Invalid end label for an inlined scope!");
Devang Patelce7bf012010-04-27 19:46:33 +00001491
Devang Patel9e592492010-03-08 20:52:55 +00001492 if (!Scope->getScopeNode())
Devang Patelbe149872010-03-08 19:20:38 +00001493 return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001494 DIScope DS(Scope->getScopeNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001495 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1496
Devang Patel19302aa2010-05-07 18:11:54 +00001497 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001498 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1499 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattner8143ce82010-03-31 05:36:29 +00001500 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001501 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001502 dwarf::DW_FORM_ref4, OriginDIE);
1503
Chris Lattneread58652010-03-09 00:31:02 +00001504 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattner855e8f52010-03-09 01:58:53 +00001505 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel90a0fe32009-11-10 23:06:00 +00001506
1507 InlinedSubprogramDIEs.insert(OriginDIE);
1508
1509 // Track the start label for this inlined function.
Devang Patel9c371c62010-05-07 20:54:48 +00001510 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel19302aa2010-05-07 18:11:54 +00001511 I = InlineInfo.find(InlinedSP);
Devang Patel90a0fe32009-11-10 23:06:00 +00001512
1513 if (I == InlineInfo.end()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001514 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbachb23f2422009-11-22 19:20:36 +00001515 ScopeDIE));
Devang Patel19302aa2010-05-07 18:11:54 +00001516 InlinedSPNodes.push_back(InlinedSP);
Devang Patel90a0fe32009-11-10 23:06:00 +00001517 } else
Chris Lattneread58652010-03-09 00:31:02 +00001518 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001519
Devang Patel90a0fe32009-11-10 23:06:00 +00001520 DILocation DL(Scope->getInlinedAt());
Devang Patel7e5a86e2010-05-10 22:49:55 +00001521 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patelc50078e2009-11-21 02:48:08 +00001522 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001523
1524 return ScopeDIE;
1525}
1526
Devang Patelc50078e2009-11-21 02:48:08 +00001527
1528/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001529DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001530 // Get the descriptor.
1531 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001532 StringRef Name = VD.getName();
1533 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001534 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001535
1536 // Translate tag to proper Dwarf tag. The result variable is dropped for
1537 // now.
1538 unsigned Tag;
1539 switch (VD.getTag()) {
1540 case dwarf::DW_TAG_return_variable:
1541 return NULL;
1542 case dwarf::DW_TAG_arg_variable:
1543 Tag = dwarf::DW_TAG_formal_parameter;
1544 break;
1545 case dwarf::DW_TAG_auto_variable: // fall thru
1546 default:
1547 Tag = dwarf::DW_TAG_variable;
1548 break;
1549 }
1550
1551 // Define variable debug information entry.
1552 DIE *VariableDie = new DIE(Tag);
1553
Devang Patel90a0fe32009-11-10 23:06:00 +00001554 DIE *AbsDIE = NULL;
Devang Patelf0e70332010-05-20 16:36:41 +00001555 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1556 V2AVI = VarToAbstractVarMap.find(DV);
1557 if (V2AVI != VarToAbstractVarMap.end())
1558 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001559
Devang Patelf0e70332010-05-20 16:36:41 +00001560 if (AbsDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001561 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001562 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001563 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001564 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1565 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001566
1567 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001568 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001569 // addresses instead.
1570 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001571 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001572 else
Devang Patelfe0be132009-12-09 18:24:21 +00001573 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001574 }
1575
1576 // Add variable address.
1577 if (!Scope->isAbstractScope()) {
Devang Patel14da02f2010-03-15 18:33:46 +00001578 // Check if variable is described by DBG_VALUE instruction.
Devang Patelf0e70332010-05-20 16:36:41 +00001579 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1580 DbgVariableToDbgInstMap.find(DV);
1581 if (DVI != DbgVariableToDbgInstMap.end()) {
1582 const MachineInstr *DVInsn = DVI->second;
1583 const MCSymbol *DVLabel = findVariableLabel(DV);
Devang Patel7e625392010-04-28 01:03:09 +00001584 bool updated = false;
1585 // FIXME : Handle getNumOperands != 3
1586 if (DVInsn->getNumOperands() == 3) {
1587 if (DVInsn->getOperand(0).isReg())
Devang Patelf0e70332010-05-20 16:36:41 +00001588 updated = addRegisterAddress(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patel7e625392010-04-28 01:03:09 +00001589 else if (DVInsn->getOperand(0).isImm())
Devang Patelf0e70332010-05-20 16:36:41 +00001590 updated = addConstantValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patel7e625392010-04-28 01:03:09 +00001591 else if (DVInsn->getOperand(0).isFPImm())
Devang Patelf0e70332010-05-20 16:36:41 +00001592 updated = addConstantFPValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patel7c3efb12010-04-28 01:39:28 +00001593 } else {
1594 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1595 if (Location.getReg()) {
1596 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patelf0e70332010-05-20 16:36:41 +00001597 if (DVLabel)
Devang Patel7c3efb12010-04-28 01:39:28 +00001598 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
Devang Patelf0e70332010-05-20 16:36:41 +00001599 DVLabel);
Devang Patel7c3efb12010-04-28 01:39:28 +00001600 updated = true;
1601 }
Devang Patel7e625392010-04-28 01:03:09 +00001602 }
1603 if (!updated) {
1604 // If variableDie is not updated then DBG_VALUE instruction does not
1605 // have valid variable info.
1606 delete VariableDie;
1607 return NULL;
1608 }
1609 }
1610 else {
Devang Patel14da02f2010-03-15 18:33:46 +00001611 MachineLocation Location;
1612 unsigned FrameReg;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001613 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Devang Patelf0e70332010-05-20 16:36:41 +00001614 int FI = 0;
1615 if (findVariableFrameIndex(DV, &FI)) {
1616 int Offset = RI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patelfad01682010-05-14 21:01:35 +00001617 Location.set(FrameReg, Offset);
1618
1619 if (VD.hasComplexAddress())
1620 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1621 else if (VD.isBlockByrefVariable())
1622 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1623 else
1624 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1625 }
Devang Patel14da02f2010-03-15 18:33:46 +00001626 }
Devang Patel90a0fe32009-11-10 23:06:00 +00001627 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001628
1629 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1630 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001631 DV->setDIE(VariableDie);
1632 return VariableDie;
1633
1634}
Devang Patelc50078e2009-11-21 02:48:08 +00001635
Devang Patelec13b4f2009-11-24 01:14:22 +00001636void DwarfDebug::addPubTypes(DISubprogram SP) {
1637 DICompositeType SPTy = SP.getType();
1638 unsigned SPTag = SPTy.getTag();
1639 if (SPTag != dwarf::DW_TAG_subroutine_type)
1640 return;
1641
1642 DIArray Args = SPTy.getTypeArray();
Devang Patelec13b4f2009-11-24 01:14:22 +00001643 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel19302aa2010-05-07 18:11:54 +00001644 DIType ATy(Args.getElement(i));
Devang Patel9738af92010-05-07 21:45:47 +00001645 if (!ATy.Verify())
Devang Patelec13b4f2009-11-24 01:14:22 +00001646 continue;
1647 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel19302aa2010-05-07 18:11:54 +00001648 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel21171c22010-04-13 20:35:04 +00001649 && !CATy.isForwardDecl()) {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001650 CompileUnit *TheCU = getCompileUnit(CATy);
1651 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1652 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patelec13b4f2009-11-24 01:14:22 +00001653 }
1654 }
1655}
1656
Devang Patelc50078e2009-11-21 02:48:08 +00001657/// constructScopeDIE - Construct a DIE for this scope.
1658DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel9e592492010-03-08 20:52:55 +00001659 if (!Scope || !Scope->getScopeNode())
1660 return NULL;
1661
1662 DIScope DS(Scope->getScopeNode());
1663 DIE *ScopeDIE = NULL;
1664 if (Scope->getInlinedAt())
1665 ScopeDIE = constructInlinedScopeDIE(Scope);
1666 else if (DS.isSubprogram()) {
1667 if (Scope->isAbstractScope())
Devang Patel7e5a86e2010-05-10 22:49:55 +00001668 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel9e592492010-03-08 20:52:55 +00001669 else
Devang Patel19302aa2010-05-07 18:11:54 +00001670 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel9e592492010-03-08 20:52:55 +00001671 }
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001672 else
Devang Patel9e592492010-03-08 20:52:55 +00001673 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001674 if (!ScopeDIE) return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001675
Devang Patel90a0fe32009-11-10 23:06:00 +00001676 // Add variables to scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001677 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel90a0fe32009-11-10 23:06:00 +00001678 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelfe0be132009-12-09 18:24:21 +00001679 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +00001680 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001681 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001682 }
1683
1684 // Add nested scopes.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001685 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel90a0fe32009-11-10 23:06:00 +00001686 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1687 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001688 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001689 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001690 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001691 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001692
1693 if (DS.isSubprogram())
Devang Patel19302aa2010-05-07 18:11:54 +00001694 addPubTypes(DISubprogram(DS));
Devang Patelec13b4f2009-11-24 01:14:22 +00001695
1696 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001697}
1698
Bill Wendlingf5839192009-05-20 23:19:06 +00001699/// GetOrCreateSourceID - Look up the source id with the given directory and
1700/// source file names. If none currently exists, create a new id and insert it
1701/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1702/// maps as well.
Chris Lattner5df82552010-03-31 06:06:37 +00001703unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf5839192009-05-20 23:19:06 +00001704 unsigned DId;
1705 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1706 if (DI != DirectoryIdMap.end()) {
1707 DId = DI->getValue();
1708 } else {
1709 DId = DirectoryNames.size() + 1;
1710 DirectoryIdMap[DirName] = DId;
1711 DirectoryNames.push_back(DirName);
1712 }
1713
1714 unsigned FId;
1715 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1716 if (FI != SourceFileIdMap.end()) {
1717 FId = FI->getValue();
1718 } else {
1719 FId = SourceFileNames.size() + 1;
1720 SourceFileIdMap[FileName] = FId;
1721 SourceFileNames.push_back(FileName);
1722 }
1723
1724 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1725 SourceIdMap.find(std::make_pair(DId, FId));
1726 if (SI != SourceIdMap.end())
1727 return SI->second;
1728
1729 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1730 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1731 SourceIds.push_back(std::make_pair(DId, FId));
1732
1733 return SrcId;
1734}
1735
Devang Patel8287d662009-12-15 19:16:48 +00001736/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1737DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001738 CompileUnit *TheCU = getCompileUnit(NS);
1739 DIE *NDie = TheCU->getDIE(NS);
Devang Patel8287d662009-12-15 19:16:48 +00001740 if (NDie)
1741 return NDie;
1742 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001743 TheCU->insertDIE(NS, NDie);
Devang Patel8287d662009-12-15 19:16:48 +00001744 if (!NS.getName().empty())
1745 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1746 addSourceLine(NDie, &NS);
1747 addToContextOwner(NDie, NS.getContext());
1748 return NDie;
1749}
1750
Devang Patel7e5a86e2010-05-10 22:49:55 +00001751/// constructCompileUnit - Create new CompileUnit for the given
1752/// metadata node with tag DW_TAG_compile_unit.
Devang Patel9c371c62010-05-07 20:54:48 +00001753void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001754 DICompileUnit DIUnit(N);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001755 StringRef FN = DIUnit.getFilename();
1756 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001757 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001758
1759 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001760 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001761 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001762 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001763 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001764 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel325691a2010-04-26 22:54:28 +00001765 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1766 // simplifies debug range entries.
1767 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_data4, 0);
Devang Patel7ac39832010-03-22 23:11:36 +00001768 // DW_AT_stmt_list is a offset of line number information for this
1769 // compile unit in debug_line section. It is always zero when only one
1770 // compile unit is emitted in one object file.
1771 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf5839192009-05-20 23:19:06 +00001772
Devang Patel7f75bbe2009-11-25 17:36:49 +00001773 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001774 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001775 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001776 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001777
Devang Patel7f75bbe2009-11-25 17:36:49 +00001778 StringRef Flags = DIUnit.getFlags();
1779 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001780 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001781
1782 unsigned RVer = DIUnit.getRunTimeVersion();
1783 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001784 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001785 dwarf::DW_FORM_data1, RVer);
1786
Devang Patel7e5a86e2010-05-10 22:49:55 +00001787 CompileUnit *NewCU = new CompileUnit(ID, Die);
1788 if (!FirstCU)
1789 FirstCU = NewCU;
1790 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf5839192009-05-20 23:19:06 +00001791}
1792
Devang Patel7e5a86e2010-05-10 22:49:55 +00001793/// getCompielUnit - Get CompileUnit DIE.
1794CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1795 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1796 DIDescriptor D(N);
1797 const MDNode *CUNode = NULL;
1798 if (D.isCompileUnit())
1799 CUNode = N;
1800 else if (D.isSubprogram())
1801 CUNode = DISubprogram(N).getCompileUnit();
1802 else if (D.isType())
1803 CUNode = DIType(N).getCompileUnit();
1804 else if (D.isGlobalVariable())
1805 CUNode = DIGlobalVariable(N).getCompileUnit();
1806 else if (D.isVariable())
1807 CUNode = DIVariable(N).getCompileUnit();
1808 else if (D.isNameSpace())
1809 CUNode = DINameSpace(N).getCompileUnit();
1810 else if (D.isFile())
1811 CUNode = DIFile(N).getCompileUnit();
1812 else
1813 return FirstCU;
1814
1815 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1816 = CUMap.find(CUNode);
1817 if (I == CUMap.end())
1818 return FirstCU;
1819 return I->second;
1820}
1821
1822
1823/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel9c371c62010-05-07 20:54:48 +00001824void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001825 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001826
Devang Patel0c03f062009-09-04 23:59:07 +00001827 // If debug information is malformed then ignore it.
1828 if (DI_GV.Verify() == false)
1829 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001830
1831 // Check for pre-existence.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001832 CompileUnit *TheCU = getCompileUnit(N);
1833 if (TheCU->getDIE(DI_GV))
Devang Patel166f8432009-06-26 01:49:18 +00001834 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001835
Devang Patelfe0be132009-12-09 18:24:21 +00001836 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001837 if (!VariableDie)
1838 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001839
Bill Wendlingf5839192009-05-20 23:19:06 +00001840 // Add to map.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001841 TheCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001842
1843 // Add to context owner.
Devang Pateldcc0dce2010-01-15 01:12:22 +00001844 DIDescriptor GVContext = DI_GV.getContext();
1845 // Do not create specification DIE if context is either compile unit
1846 // or a subprogram.
Chris Lattner5df82552010-03-31 06:06:37 +00001847 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel75c10622010-04-06 23:53:48 +00001848 !GVContext.isFile() &&
Devang Patel19302aa2010-05-07 18:11:54 +00001849 !isSubprogramContext(GVContext)) {
Devang Patel8287d662009-12-15 19:16:48 +00001850 // Create specification DIE.
1851 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1852 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1853 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001854 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001855 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001856 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001857 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001858 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patela0a98582010-02-09 01:58:33 +00001859 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001860 TheCU->addDie(VariableSpecDIE);
Devang Patel8287d662009-12-15 19:16:48 +00001861 } else {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001862 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001863 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001864 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001865 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001866 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1867 }
Devang Pateldcc0dce2010-01-15 01:12:22 +00001868 addToContextOwner(VariableDie, GVContext);
Devang Patel1a8f9a82009-12-10 19:14:49 +00001869
Bill Wendlingf5839192009-05-20 23:19:06 +00001870 // Expose as global. FIXME - need to check external flag.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001871 TheCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001872
1873 DIType GTy = DI_GV.getType();
Devang Patel21171c22010-04-13 20:35:04 +00001874 if (GTy.isCompositeType() && !GTy.getName().empty()
1875 && !GTy.isForwardDecl()) {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001876 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattner8143ce82010-03-31 05:36:29 +00001877 assert(Entry && "Missing global type!");
Devang Patel7e5a86e2010-05-10 22:49:55 +00001878 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patelec13b4f2009-11-24 01:14:22 +00001879 }
Devang Patel166f8432009-06-26 01:49:18 +00001880 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001881}
1882
Devang Patel7e5a86e2010-05-10 22:49:55 +00001883/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel9c371c62010-05-07 20:54:48 +00001884void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001885 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001886
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001887 // Check for pre-existence.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001888 CompileUnit *TheCU = getCompileUnit(N);
1889 if (TheCU->getDIE(N))
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001890 return;
1891
Bill Wendlingf5839192009-05-20 23:19:06 +00001892 if (!SP.isDefinition())
1893 // This is a method declaration which will be handled while constructing
1894 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001895 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001896
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001897 DIE *SubprogramDie = createSubprogramDIE(SP);
1898
1899 // Add to map.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001900 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001901
1902 // Add to context owner.
Devang Patel8287d662009-12-15 19:16:48 +00001903 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patelde2d3682009-12-08 23:21:45 +00001904
Bill Wendlingf5839192009-05-20 23:19:06 +00001905 // Expose as global.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001906 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001907
Devang Patel166f8432009-06-26 01:49:18 +00001908 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001909}
1910
Devang Patelc50078e2009-11-21 02:48:08 +00001911/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001912/// content. Create global DIEs and emit initial debug info sections.
1913/// This is inovked by the target AsmPrinter.
Chris Lattner75e113c2010-04-04 07:48:20 +00001914void DwarfDebug::beginModule(Module *M) {
Devang Patelce7bf012010-04-27 19:46:33 +00001915 if (DisableDebugInfoPrinting)
1916 return;
1917
Devang Patelfda766d2009-07-30 18:56:46 +00001918 DebugInfoFinder DbgFinder;
1919 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001920
Chris Lattnera52b6172010-04-05 02:19:28 +00001921 bool HasDebugInfo = false;
1922
1923 // Scan all the compile-units to see if there are any marked as the main unit.
1924 // if not, we do not generate debug info.
1925 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1926 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1927 if (DICompileUnit(*I).isMain()) {
1928 HasDebugInfo = true;
1929 break;
1930 }
1931 }
1932
1933 if (!HasDebugInfo) return;
1934
1935 // Tell MMI that we have debug info.
1936 MMI->setDebugInfoAvailability(true);
1937
Chris Lattner706afc02010-04-04 23:17:54 +00001938 // Emit initial sections.
Chris Lattnera52b6172010-04-05 02:19:28 +00001939 EmitSectionLabels();
Chris Lattner706afc02010-04-04 23:17:54 +00001940
Bill Wendlingf5839192009-05-20 23:19:06 +00001941 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001942 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1943 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001944 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001945
Devang Patel90a0fe32009-11-10 23:06:00 +00001946 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001947 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1948 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001949 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001950
Devang Patel1a8f9a82009-12-10 19:14:49 +00001951 // Create DIEs for each global variable.
1952 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1953 E = DbgFinder.global_variable_end(); I != E; ++I)
1954 constructGlobalVariableDIE(*I);
1955
Bill Wendlingf5839192009-05-20 23:19:06 +00001956 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001957 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001958
1959 // Print out .file directives to specify files for .loc directives. These are
1960 // printed out early so that they precede any .loc directives.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001961 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001962 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1963 // Remember source id starts at 1.
1964 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerad653482010-01-22 22:09:00 +00001965 // FIXME: don't use sys::path for this! This should not depend on the
1966 // host.
Bill Wendlingf5839192009-05-20 23:19:06 +00001967 sys::Path FullPath(getSourceDirectoryName(Id.first));
1968 bool AppendOk =
1969 FullPath.appendComponent(getSourceFileName(Id.second));
1970 assert(AppendOk && "Could not append filename to directory!");
1971 AppendOk = false;
Chris Lattner59be4ea2010-01-25 18:58:59 +00001972 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001973 }
1974 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001975}
1976
Devang Patelc50078e2009-11-21 02:48:08 +00001977/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001978///
Devang Patelc50078e2009-11-21 02:48:08 +00001979void DwarfDebug::endModule() {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001980 if (!FirstCU) return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001981
Devang Patel90a0fe32009-11-10 23:06:00 +00001982 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1983 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1984 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1985 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001986 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001987 }
1988
Devang Patel9c371c62010-05-07 20:54:48 +00001989 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel188c85d2009-12-03 19:11:07 +00001990 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1991 DIE *SPDie = CI->first;
Devang Patel9c371c62010-05-07 20:54:48 +00001992 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel188c85d2009-12-03 19:11:07 +00001993 if (!N) continue;
Devang Patel7e5a86e2010-05-10 22:49:55 +00001994 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel188c85d2009-12-03 19:11:07 +00001995 if (!NDie) continue;
1996 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel188c85d2009-12-03 19:11:07 +00001997 }
1998
Bill Wendlingf5839192009-05-20 23:19:06 +00001999 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00002000 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00002001 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner73266f92009-08-19 05:49:37 +00002002 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00002003 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf5839192009-05-20 23:19:06 +00002004
2005 // End text sections.
2006 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00002007 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002008 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf5839192009-05-20 23:19:06 +00002009 }
2010
2011 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00002012 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00002013
2014 // Emit function debug frame information
2015 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2016 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00002017 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00002018
2019 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00002020 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00002021
2022 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00002023 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002024
2025 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00002026 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00002027
2028 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00002029 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00002030
2031 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00002032 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00002033
Devang Patelec13b4f2009-11-24 01:14:22 +00002034 // Emit info into a debug pubtypes section.
2035 emitDebugPubTypes();
2036
Bill Wendlingf5839192009-05-20 23:19:06 +00002037 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00002038 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00002039
2040 // Emit info into a debug aranges section.
2041 EmitDebugARanges();
2042
2043 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00002044 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00002045
2046 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00002047 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002048
2049 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00002050 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002051
Chris Lattner0f093f82010-03-13 02:17:42 +00002052 // Emit info into a debug str section.
2053 emitDebugStr();
2054
Devang Patel7e5a86e2010-05-10 22:49:55 +00002055 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2056 E = CUMap.end(); I != E; ++I)
2057 delete I->second;
2058 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf5839192009-05-20 23:19:06 +00002059}
2060
Devang Patel90a0fe32009-11-10 23:06:00 +00002061/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelf0e70332010-05-20 16:36:41 +00002062DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattnerb9692a72010-04-02 19:42:39 +00002063 DebugLoc ScopeLoc) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002064
Devang Patel19302aa2010-05-07 18:11:54 +00002065 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel90a0fe32009-11-10 23:06:00 +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 Patel90a0fe32009-11-10 23:06:00 +00002071 if (!Scope)
2072 return NULL;
2073
Devang Patelf0e70332010-05-20 16:36:41 +00002074 AbsDbgVariable = new DbgVariable(Var);
Devang Patelc50078e2009-11-21 02:48:08 +00002075 Scope->addVariable(AbsDbgVariable);
Devang Patel19302aa2010-05-07 18:11:54 +00002076 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel90a0fe32009-11-10 23:06:00 +00002077 return AbsDbgVariable;
2078}
2079
Devang Patel6da145f2010-05-20 19:57:06 +00002080/// collectVariableInfoFromMMITable - Collect variable information from
2081/// side table maintained by MMI.
2082void
2083DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2084 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002085 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
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;
Devang Patelfad01682010-05-14 21:01:35 +00002091 Processed.insert(Var);
Chris Lattnerb9692a72010-04-02 19:42:39 +00002092 DIVariable DV(Var);
2093 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002094
Chris Lattnerb9692a72010-04-02 19:42:39 +00002095 DbgScope *Scope = 0;
Devang Patel9c371c62010-05-07 20:54:48 +00002096 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerb9692a72010-04-02 19:42:39 +00002097 Scope = ConcreteScopes.lookup(IA);
2098 if (Scope == 0)
2099 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2100
Devang Patelbad42262009-11-10 23:20:04 +00002101 // If variable scope is not found then skip this variable.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002102 if (Scope == 0)
Devang Patelbad42262009-11-10 23:20:04 +00002103 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00002104
Devang Patelf0e70332010-05-20 16:36:41 +00002105 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2106 DbgVariable *RegVar = new DbgVariable(DV);
2107 recordVariableFrameIndex(RegVar, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00002108 Scope->addVariable(RegVar);
Devang Patelf0e70332010-05-20 16:36:41 +00002109 if (AbsDbgVariable) {
2110 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2111 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2112 }
Devang Patel84139992009-10-06 01:26:37 +00002113 }
Devang Patel6da145f2010-05-20 19:57:06 +00002114}
Devang Patel14da02f2010-03-15 18:33:46 +00002115
Devang Patel6da145f2010-05-20 19:57:06 +00002116/// collectVariableInfo - Populate DbgScope entries with variables' info.
2117void DwarfDebug::collectVariableInfo(const MachineFunction *MF) {
2118 SmallPtrSet<const MDNode *, 16> Processed;
2119
2120 /// collection info from MMI table.
2121 collectVariableInfoFromMMITable(MF, Processed);
2122
2123 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patel14da02f2010-03-15 18:33:46 +00002124 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002125 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel6da145f2010-05-20 19:57:06 +00002126 I != E; ++I)
Devang Patel14da02f2010-03-15 18:33:46 +00002127 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2128 II != IE; ++II) {
2129 const MachineInstr *MInsn = II;
Chris Lattner202f10b2010-03-31 05:39:57 +00002130 if (!MInsn->isDebugValue())
Devang Patel14da02f2010-03-15 18:33:46 +00002131 continue;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002132
Devang Patel7f78e212010-04-27 20:54:45 +00002133 // Ignore Undef values.
Devang Patelbecba812010-04-27 22:04:41 +00002134 if (MInsn->getOperand(0).isReg() && !MInsn->getOperand(0).getReg())
Devang Patel7f78e212010-04-27 20:54:45 +00002135 continue;
2136
Devang Patel6da145f2010-05-20 19:57:06 +00002137 DbgValues.push_back(MInsn);
2138 }
Devang Patel14da02f2010-03-15 18:33:46 +00002139
Devang Patel6da145f2010-05-20 19:57:06 +00002140 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2141 E = DbgValues.end(); I != E; ++I) {
2142 const MachineInstr *MInsn = *I;
Devang Patel14da02f2010-03-15 18:33:46 +00002143
Devang Patel6da145f2010-05-20 19:57:06 +00002144 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2145 if (Processed.count(DV) != 0)
2146 continue;
2147
2148 if (DV.getTag() == dwarf::DW_TAG_arg_variable) {
2149 // FIXME Handle inlined subroutine arguments.
2150 DbgVariable *ArgVar = new DbgVariable(DV);
2151 CurrentFnDbgScope->addVariable(ArgVar);
2152 DbgValueStartMap[MInsn] = ArgVar;
2153 DbgVariableToDbgInstMap[ArgVar] = MInsn;
Devang Patelfad01682010-05-14 21:01:35 +00002154 Processed.insert(DV);
Devang Patel6da145f2010-05-20 19:57:06 +00002155 continue;
2156 }
2157
2158 DbgScope *Scope = findDbgScope(MInsn);
2159 // If variable scope is not found then skip this variable.
2160 if (Scope == 0)
2161 continue;
2162
2163 Processed.insert(DV);
2164 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn->getDebugLoc());
2165 DbgVariable *RegVar = new DbgVariable(DV);
2166 DbgValueStartMap[MInsn] = RegVar;
2167 DbgVariableToDbgInstMap[RegVar] = MInsn;
2168 Scope->addVariable(RegVar);
2169 if (AbsDbgVariable) {
2170 DbgVariableToDbgInstMap[AbsDbgVariable] = MInsn;
2171 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
Devang Patel14da02f2010-03-15 18:33:46 +00002172 }
2173 }
Devang Patelfad01682010-05-14 21:01:35 +00002174
2175 // Collect info for variables that were optimized out.
2176 if (NamedMDNode *NMD =
2177 MF->getFunction()->getParent()->getNamedMetadata("llvm.dbg.lv")) {
2178 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2179 DIVariable DV(cast_or_null<MDNode>(NMD->getOperand(i)));
Devang Patel6da145f2010-05-20 19:57:06 +00002180 if (!DV || !Processed.insert(DV))
Devang Patelfad01682010-05-14 21:01:35 +00002181 continue;
2182 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2183 if (Scope)
Devang Patelf0e70332010-05-20 16:36:41 +00002184 Scope->addVariable(new DbgVariable(DV));
Devang Patelfad01682010-05-14 21:01:35 +00002185 }
2186 }
2187
Devang Patel84139992009-10-06 01:26:37 +00002188}
2189
Devang Patelabc2b352010-03-29 17:20:31 +00002190/// beginScope - Process beginning of a scope.
2191void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patelabc2b352010-03-29 17:20:31 +00002192 // Check location.
2193 DebugLoc DL = MI->getDebugLoc();
Dan Gohman5d0dda52010-05-05 23:41:32 +00002194 if (DL.isUnknown()) {
Dan Gohman77c565e2010-05-07 01:08:53 +00002195 if (UnknownLocations) {
2196 // This instruction has no debug location. If the preceding instruction
2197 // did, emit debug location information to indicate that the debug
2198 // location is now unknown.
2199 MCSymbol *Label = NULL;
2200 if (DL == PrevInstLoc)
2201 Label = PrevLabel;
2202 else {
2203 Label = recordSourceLine(DL.getLine(), DL.getCol(), 0);
2204 PrevInstLoc = DL;
2205 PrevLabel = Label;
Devang Patela5db75c2010-05-19 21:26:53 +00002206 }
Devang Patelf25c5832010-05-19 21:58:28 +00002207
Devang Patelf0e70332010-05-20 16:36:41 +00002208 // If this instruction begins a scope then note down corresponding label
2209 // even if previous label is reused.
Devang Patelf25c5832010-05-19 21:58:28 +00002210 if (InsnsBeginScopeSet.count(MI) != 0)
2211 LabelsBeforeInsn[MI] = Label;
Dan Gohman77c565e2010-05-07 01:08:53 +00002212 }
Dan Gohmanf79f0f72010-05-06 00:29:41 +00002213
Devang Patelabc2b352010-03-29 17:20:31 +00002214 return;
Dan Gohman5d0dda52010-05-05 23:41:32 +00002215 }
Devang Patelabc2b352010-03-29 17:20:31 +00002216
Devang Patel9c371c62010-05-07 20:54:48 +00002217 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerb9692a72010-04-02 19:42:39 +00002218
2219 // FIXME: Should only verify each scope once!
2220 if (!DIScope(Scope).Verify())
Devang Patelabc2b352010-03-29 17:20:31 +00002221 return;
Devang Patelabc2b352010-03-29 17:20:31 +00002222
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002223 // DBG_VALUE instruction establishes new value.
Chris Lattner202f10b2010-03-31 05:39:57 +00002224 if (MI->isDebugValue()) {
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002225 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2226 = DbgValueStartMap.find(MI);
2227 if (DI != DbgValueStartMap.end()) {
Devang Patelce7bf012010-04-27 19:46:33 +00002228 MCSymbol *Label = NULL;
2229 if (DL == PrevInstLoc)
2230 Label = PrevLabel;
2231 else {
2232 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2233 PrevInstLoc = DL;
2234 PrevLabel = Label;
2235 }
Devang Patelf0e70332010-05-20 16:36:41 +00002236 DbgVariableLabelsMap[DI->second] = Label;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002237 }
Devang Patel98ca2372010-03-30 18:07:00 +00002238 return;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002239 }
2240
Devang Patelabc2b352010-03-29 17:20:31 +00002241 // Emit a label to indicate location change. This is used for line
Devang Patelce7bf012010-04-27 19:46:33 +00002242 // table even if this instruction does not start a new scope.
2243 MCSymbol *Label = NULL;
2244 if (DL == PrevInstLoc)
2245 Label = PrevLabel;
2246 else {
2247 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2248 PrevInstLoc = DL;
2249 PrevLabel = Label;
2250 }
Devang Patelabc2b352010-03-29 17:20:31 +00002251
Devang Patelf0e70332010-05-20 16:36:41 +00002252 // If this instruction begins a scope then note down corresponding label
2253 // even if previous label is reused.
Devang Patelf25c5832010-05-19 21:58:28 +00002254 if (InsnsBeginScopeSet.count(MI) != 0)
2255 LabelsBeforeInsn[MI] = Label;
Devang Patel393a46d2009-10-06 01:50:42 +00002256}
2257
Devang Patelc50078e2009-11-21 02:48:08 +00002258/// endScope - Process end of a scope.
2259void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel6f878382010-04-08 16:50:29 +00002260 if (InsnsEndScopeSet.count(MI) != 0) {
2261 // Emit a label if this instruction ends a scope.
2262 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2263 Asm->OutStreamer.EmitLabel(Label);
Devang Patelce7bf012010-04-27 19:46:33 +00002264 LabelsAfterInsn[MI] = Label;
Devang Patel6f878382010-04-08 16:50:29 +00002265 }
Devang Patel90a0fe32009-11-10 23:06:00 +00002266}
2267
Devang Patelce7bf012010-04-27 19:46:33 +00002268/// getOrCreateDbgScope - Create DbgScope for the scope.
Devang Patel9c371c62010-05-07 20:54:48 +00002269DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002270 if (!InlinedAt) {
2271 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2272 if (WScope)
Devang Patelce7bf012010-04-27 19:46:33 +00002273 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002274 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2275 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patelce7bf012010-04-27 19:46:33 +00002276 if (DIDescriptor(Scope).isLexicalBlock()) {
2277 DbgScope *Parent =
Devang Patel19302aa2010-05-07 18:11:54 +00002278 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Patelce7bf012010-04-27 19:46:33 +00002279 WScope->setParent(Parent);
2280 Parent->addScope(WScope);
2281 }
2282
2283 if (!WScope->getParent()) {
2284 StringRef SPName = DISubprogram(Scope).getLinkageName();
2285 if (SPName == Asm->MF->getFunction()->getName())
2286 CurrentFnDbgScope = WScope;
2287 }
2288
2289 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002290 }
2291
2292 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2293 if (WScope)
Devang Patelce7bf012010-04-27 19:46:33 +00002294 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002295
2296 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2297 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2298 DILocation DL(InlinedAt);
Devang Patelce7bf012010-04-27 19:46:33 +00002299 DbgScope *Parent =
Devang Patel19302aa2010-05-07 18:11:54 +00002300 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Patelce7bf012010-04-27 19:46:33 +00002301 WScope->setParent(Parent);
2302 Parent->addScope(WScope);
2303
2304 ConcreteScopes[InlinedAt] = WScope;
2305 getOrCreateAbstractScope(Scope);
2306
2307 return WScope;
Devang Patel393a46d2009-10-06 01:50:42 +00002308}
2309
Devang Patelce7bf012010-04-27 19:46:33 +00002310/// hasValidLocation - Return true if debug location entry attached with
2311/// machine instruction encodes valid location info.
2312static bool hasValidLocation(LLVMContext &Ctx,
2313 const MachineInstr *MInsn,
Devang Patel9c371c62010-05-07 20:54:48 +00002314 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Patelce7bf012010-04-27 19:46:33 +00002315 if (MInsn->isDebugValue())
2316 return false;
2317 DebugLoc DL = MInsn->getDebugLoc();
2318 if (DL.isUnknown()) return false;
2319
Devang Patel9c371c62010-05-07 20:54:48 +00002320 const MDNode *S = DL.getScope(Ctx);
Devang Patelce7bf012010-04-27 19:46:33 +00002321
2322 // There is no need to create another DIE for compile unit. For all
2323 // other scopes, create one DbgScope now. This will be translated
2324 // into a scope DIE at the end.
2325 if (DIScope(S).isCompileUnit()) return false;
2326
2327 Scope = S;
2328 InlinedAt = DL.getInlinedAt(Ctx);
2329 return true;
2330}
2331
2332/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2333/// hierarchy.
2334static void calculateDominanceGraph(DbgScope *Scope) {
2335 assert (Scope && "Unable to calculate scop edominance graph!");
2336 SmallVector<DbgScope *, 4> WorkStack;
2337 WorkStack.push_back(Scope);
2338 unsigned Counter = 0;
2339 while (!WorkStack.empty()) {
2340 DbgScope *WS = WorkStack.back();
2341 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2342 bool visitedChildren = false;
2343 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2344 SE = Children.end(); SI != SE; ++SI) {
2345 DbgScope *ChildScope = *SI;
2346 if (!ChildScope->getDFSOut()) {
2347 WorkStack.push_back(ChildScope);
2348 visitedChildren = true;
2349 ChildScope->setDFSIn(++Counter);
2350 break;
2351 }
2352 }
2353 if (!visitedChildren) {
2354 WorkStack.pop_back();
2355 WS->setDFSOut(++Counter);
2356 }
2357 }
2358}
2359
2360/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2361static
2362void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2363 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2364{
2365#ifndef NDEBUG
2366 unsigned PrevDFSIn = 0;
2367 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2368 I != E; ++I) {
2369 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2370 II != IE; ++II) {
2371 const MachineInstr *MInsn = II;
Devang Patel9c371c62010-05-07 20:54:48 +00002372 const MDNode *Scope = NULL;
2373 const MDNode *InlinedAt = NULL;
Devang Patelce7bf012010-04-27 19:46:33 +00002374
2375 // Check if instruction has valid location information.
2376 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2377 dbgs() << " [ ";
2378 if (InlinedAt)
2379 dbgs() << "*";
2380 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2381 MI2ScopeMap.find(MInsn);
2382 if (DI != MI2ScopeMap.end()) {
2383 DbgScope *S = DI->second;
2384 dbgs() << S->getDFSIn();
2385 PrevDFSIn = S->getDFSIn();
2386 } else
2387 dbgs() << PrevDFSIn;
2388 } else
2389 dbgs() << " [ x" << PrevDFSIn;
2390 dbgs() << " ]";
2391 MInsn->dump();
2392 }
2393 dbgs() << "\n";
2394 }
2395#endif
2396}
Devang Patelc50078e2009-11-21 02:48:08 +00002397/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner202f10b2010-03-31 05:39:57 +00002398/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner69a76972010-01-26 23:18:02 +00002399bool DwarfDebug::extractScopeInformation() {
Devang Patel6a260102009-10-01 20:31:14 +00002400 // If scope information was extracted using .dbg intrinsics then there is not
2401 // any need to extract these information by scanning each instruction.
2402 if (!DbgScopeMap.empty())
2403 return false;
2404
Dan Gohman30573dd2010-04-23 01:18:53 +00002405 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelce7bf012010-04-27 19:46:33 +00002406 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2407 SmallVector<DbgRange, 4> MIRanges;
2408 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patel9c371c62010-05-07 20:54:48 +00002409 const MDNode *PrevScope = NULL;
2410 const MDNode *PrevInlinedAt = NULL;
Devang Patelce7bf012010-04-27 19:46:33 +00002411 const MachineInstr *RangeBeginMI = NULL;
2412 const MachineInstr *PrevMI = NULL;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002413 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel6a260102009-10-01 20:31:14 +00002414 I != E; ++I) {
2415 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2416 II != IE; ++II) {
2417 const MachineInstr *MInsn = II;
Devang Patel9c371c62010-05-07 20:54:48 +00002418 const MDNode *Scope = NULL;
2419 const MDNode *InlinedAt = NULL;
Devang Patelce7bf012010-04-27 19:46:33 +00002420
2421 // Check if instruction has valid location information.
2422 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2423 PrevMI = MInsn;
2424 continue;
2425 }
Chris Lattnerb9692a72010-04-02 19:42:39 +00002426
Devang Patelce7bf012010-04-27 19:46:33 +00002427 // If scope has not changed then skip this instruction.
2428 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2429 PrevMI = MInsn;
2430 continue;
2431 }
2432
2433 if (RangeBeginMI) {
2434 // If we have alread seen a beginning of a instruction range and
2435 // current instruction scope does not match scope of first instruction
2436 // in this range then create a new instruction range.
2437 DbgRange R(RangeBeginMI, PrevMI);
2438 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
2439 MIRanges.push_back(R);
2440 }
2441
2442 // This is a beginning of a new instruction range.
2443 RangeBeginMI = MInsn;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002444
Devang Patelce7bf012010-04-27 19:46:33 +00002445 // Reset previous markers.
2446 PrevMI = MInsn;
2447 PrevScope = Scope;
2448 PrevInlinedAt = InlinedAt;
Devang Patel90a0fe32009-11-10 23:06:00 +00002449 }
2450 }
2451
Devang Patelce7bf012010-04-27 19:46:33 +00002452 // Create last instruction range.
2453 if (RangeBeginMI && PrevMI && PrevScope) {
2454 DbgRange R(RangeBeginMI, PrevMI);
2455 MIRanges.push_back(R);
2456 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel6a260102009-10-01 20:31:14 +00002457 }
Devang Patelce7bf012010-04-27 19:46:33 +00002458
Devang Patel98e77302010-01-04 20:44:00 +00002459 if (!CurrentFnDbgScope)
2460 return false;
2461
Devang Patelce7bf012010-04-27 19:46:33 +00002462 calculateDominanceGraph(CurrentFnDbgScope);
2463 if (PrintDbgScope)
2464 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2465
2466 // Find ranges of instructions covered by each DbgScope;
2467 DbgScope *PrevDbgScope = NULL;
2468 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2469 RE = MIRanges.end(); RI != RE; ++RI) {
2470 const DbgRange &R = *RI;
2471 DbgScope *S = MI2ScopeMap.lookup(R.first);
2472 assert (S && "Lost DbgScope for a machine instruction!");
2473 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2474 PrevDbgScope->closeInsnRange(S);
2475 S->openInsnRange(R.first);
2476 S->extendInsnRange(R.second);
2477 PrevDbgScope = S;
2478 }
2479
2480 if (PrevDbgScope)
2481 PrevDbgScope->closeInsnRange();
Devang Patel6a260102009-10-01 20:31:14 +00002482
Devang Patelf63b2262010-04-08 18:43:56 +00002483 identifyScopeMarkers();
Devang Patel0d8f3b72010-04-08 15:37:09 +00002484
2485 return !DbgScopeMap.empty();
2486}
2487
Devang Patelce7bf012010-04-27 19:46:33 +00002488/// identifyScopeMarkers() -
2489/// Each DbgScope has first instruction and last instruction to mark beginning
2490/// and end of a scope respectively. Create an inverse map that list scopes
2491/// starts (and ends) with an instruction. One instruction may start (or end)
2492/// multiple scopes. Ignore scopes that are not reachable.
Devang Patelf63b2262010-04-08 18:43:56 +00002493void DwarfDebug::identifyScopeMarkers() {
Devang Patelfd311df2010-01-20 02:05:23 +00002494 SmallVector<DbgScope *, 4> WorkList;
2495 WorkList.push_back(CurrentFnDbgScope);
2496 while (!WorkList.empty()) {
Chris Lattner202f10b2010-03-31 05:39:57 +00002497 DbgScope *S = WorkList.pop_back_val();
Devang Patelce7bf012010-04-27 19:46:33 +00002498
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002499 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patelfd311df2010-01-20 02:05:23 +00002500 if (!Children.empty())
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002501 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patelfd311df2010-01-20 02:05:23 +00002502 SE = Children.end(); SI != SE; ++SI)
2503 WorkList.push_back(*SI);
2504
Devang Patel90a0fe32009-11-10 23:06:00 +00002505 if (S->isAbstractScope())
2506 continue;
Devang Patelce7bf012010-04-27 19:46:33 +00002507
2508 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2509 if (Ranges.empty())
2510 continue;
2511 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2512 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelf25c5832010-05-19 21:58:28 +00002513 assert(RI->first && "DbgRange does not have first instruction!");
2514 assert(RI->second && "DbgRange does not have second instruction!");
2515 InsnsBeginScopeSet.insert(RI->first);
Devang Patelce7bf012010-04-27 19:46:33 +00002516 InsnsEndScopeSet.insert(RI->second);
2517 }
Devang Patel6a260102009-10-01 20:31:14 +00002518 }
Devang Patel6a260102009-10-01 20:31:14 +00002519}
2520
Dan Gohmand0e9b672010-04-20 00:37:27 +00002521/// FindFirstDebugLoc - Find the first debug location in the function. This
2522/// is intended to be an approximation for the source position of the
2523/// beginning of the function.
2524static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2525 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2526 I != E; ++I)
2527 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2528 MBBI != MBBE; ++MBBI) {
2529 DebugLoc DL = MBBI->getDebugLoc();
2530 if (!DL.isUnknown())
2531 return DL;
2532 }
2533 return DebugLoc();
2534}
2535
Devang Patelc50078e2009-11-21 02:48:08 +00002536/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002537/// emitted immediately after the function entry point.
Chris Lattner69a76972010-01-26 23:18:02 +00002538void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner2cb53302010-04-05 03:52:55 +00002539 if (!MMI->hasDebugInfo()) return;
Bill Wendlingce3c6252010-04-07 09:28:04 +00002540 if (!extractScopeInformation()) return;
Chris Lattnerb49c9f82010-03-29 20:38:20 +00002541
Devang Patelfad01682010-05-14 21:01:35 +00002542 collectVariableInfo(MF);
Devang Patel0feae422009-10-06 18:37:31 +00002543
Devang Patelce7bf012010-04-27 19:46:33 +00002544 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2545 Asm->getFunctionNumber());
Bill Wendlingf5839192009-05-20 23:19:06 +00002546 // Assumes in correct section after the entry point.
Devang Patelce7bf012010-04-27 19:46:33 +00002547 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf5839192009-05-20 23:19:06 +00002548
2549 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2550 // function.
Dan Gohmand0e9b672010-04-20 00:37:27 +00002551 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerb9692a72010-04-02 19:42:39 +00002552 if (FDL.isUnknown()) return;
2553
Devang Patel9c371c62010-05-07 20:54:48 +00002554 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Chris Lattnerb9692a72010-04-02 19:42:39 +00002555
2556 DISubprogram SP = getDISubprogram(Scope);
2557 unsigned Line, Col;
2558 if (SP.Verify()) {
2559 Line = SP.getLineNumber();
2560 Col = 0;
2561 } else {
2562 Line = FDL.getLine();
2563 Col = FDL.getCol();
Bill Wendlingf5839192009-05-20 23:19:06 +00002564 }
Chris Lattnerb9692a72010-04-02 19:42:39 +00002565
2566 recordSourceLine(Line, Col, Scope);
Bill Wendlingf5839192009-05-20 23:19:06 +00002567}
2568
Devang Patelc50078e2009-11-21 02:48:08 +00002569/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002570///
Chris Lattner69a76972010-01-26 23:18:02 +00002571void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingce3c6252010-04-07 09:28:04 +00002572 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002573
Devang Patel98e77302010-01-04 20:44:00 +00002574 if (CurrentFnDbgScope) {
2575 // Define end label for subprogram.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002576 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2577 Asm->getFunctionNumber()));
Devang Patel98e77302010-01-04 20:44:00 +00002578
2579 // Get function line info.
2580 if (!Lines.empty()) {
2581 // Get section line info.
2582 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2583 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2584 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2585 // Append the function info to section info.
2586 SectionLineInfos.insert(SectionLineInfos.end(),
2587 Lines.begin(), Lines.end());
2588 }
2589
2590 // Construct abstract scopes.
2591 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2592 AE = AbstractScopesList.end(); AI != AE; ++AI)
2593 constructScopeDIE(*AI);
2594
Devang Patelab4b3ce2010-05-04 06:15:30 +00002595 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel98e77302010-01-04 20:44:00 +00002596
Devang Patelab4b3ce2010-05-04 06:15:30 +00002597 if (!DisableFramePointerElim(*MF))
2598 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2599 dwarf::DW_FORM_flag, 1);
2600
2601
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002602 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel98e77302010-01-04 20:44:00 +00002603 MMI->getFrameMoves()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002604 }
2605
Bill Wendlingf5839192009-05-20 23:19:06 +00002606 // Clear debug info
Devang Patel387e9c12010-01-19 01:26:02 +00002607 CurrentFnDbgScope = NULL;
Devang Patelf0e70332010-05-20 16:36:41 +00002608 DbgVariableToFrameIndexMap.clear();
2609 VarToAbstractVarMap.clear();
2610 DbgVariableToDbgInstMap.clear();
2611 DbgVariableLabelsMap.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002612 DeleteContainerSeconds(DbgScopeMap);
Devang Patelf25c5832010-05-19 21:58:28 +00002613 InsnsBeginScopeSet.clear();
Devang Patelfd0ebd52010-04-09 16:04:20 +00002614 InsnsEndScopeSet.clear();
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002615 DbgValueStartMap.clear();
Devang Patel387e9c12010-01-19 01:26:02 +00002616 ConcreteScopes.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002617 DeleteContainerSeconds(AbstractScopes);
Devang Patel387e9c12010-01-19 01:26:02 +00002618 AbstractScopesList.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002619 AbstractVariables.clear();
Devang Patelce7bf012010-04-27 19:46:33 +00002620 LabelsBeforeInsn.clear();
2621 LabelsAfterInsn.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002622 Lines.clear();
Devang Patel146c6f72010-04-16 23:33:45 +00002623 PrevLabel = NULL;
Bill Wendlingf5839192009-05-20 23:19:06 +00002624}
2625
Devang Patelf0e70332010-05-20 16:36:41 +00002626/// recordVariableFrameIndex - Record a variable's index.
2627void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2628 assert (V && "Invalid DbgVariable!");
2629 DbgVariableToFrameIndexMap[V] = Index;
2630}
2631
2632/// findVariableFrameIndex - Return true if frame index for the variable
2633/// is found. Update FI to hold value of the index.
2634bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2635 assert (V && "Invalid DbgVariable!");
2636 DenseMap<const DbgVariable *, int>::iterator I =
2637 DbgVariableToFrameIndexMap.find(V);
2638 if (I == DbgVariableToFrameIndexMap.end())
2639 return false;
2640 *FI = I->second;
2641 return true;
2642}
2643
2644/// findVariableLabel - Find MCSymbol for the variable.
2645const MCSymbol *DwarfDebug::findVariableLabel(const DbgVariable *V) {
2646 DenseMap<const DbgVariable *, const MCSymbol *>::iterator I
2647 = DbgVariableLabelsMap.find(V);
2648 if (I == DbgVariableLabelsMap.end())
2649 return NULL;
2650 else return I->second;
2651}
2652
Devang Patel6da145f2010-05-20 19:57:06 +00002653/// findDbgScope - Find DbgScope for the debug loc attached with an
2654/// instruction.
2655DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2656 DbgScope *Scope = NULL;
2657 LLVMContext &Ctx =
2658 MInsn->getParent()->getParent()->getFunction()->getContext();
2659 DebugLoc DL = MInsn->getDebugLoc();
2660
2661 if (DL.isUnknown())
2662 return Scope;
2663
2664 if (const MDNode *IA = DL.getInlinedAt(Ctx))
2665 Scope = ConcreteScopes.lookup(IA);
2666 if (Scope == 0)
2667 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2668
2669 return Scope;
2670}
2671
2672
Chris Lattner597b0fc2010-03-09 04:54:43 +00002673/// recordSourceLine - Register a source line with debug info. Returns the
2674/// unique label that was emitted and which provides correspondence to
2675/// the source line list.
Devang Patel9c371c62010-05-07 20:54:48 +00002676MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S) {
Devang Patel7f75bbe2009-11-25 17:36:49 +00002677 StringRef Dir;
2678 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002679
Dan Gohman5d0dda52010-05-05 23:41:32 +00002680 unsigned Src = 1;
2681 if (S) {
2682 DIDescriptor Scope(S);
Devang Patel946d0ae2009-10-05 18:03:19 +00002683
Dan Gohman5d0dda52010-05-05 23:41:32 +00002684 if (Scope.isCompileUnit()) {
2685 DICompileUnit CU(S);
2686 Dir = CU.getDirectory();
2687 Fn = CU.getFilename();
2688 } else if (Scope.isSubprogram()) {
2689 DISubprogram SP(S);
2690 Dir = SP.getDirectory();
2691 Fn = SP.getFilename();
2692 } else if (Scope.isLexicalBlock()) {
2693 DILexicalBlock DB(S);
2694 Dir = DB.getDirectory();
2695 Fn = DB.getFilename();
2696 } else
2697 assert(0 && "Unexpected scope info");
2698
2699 Src = GetOrCreateSourceID(Dir, Fn);
2700 }
2701
Chris Lattner54e56f22010-03-14 08:36:50 +00002702 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002703 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf5839192009-05-20 23:19:06 +00002704
Chris Lattner597b0fc2010-03-09 04:54:43 +00002705 Asm->OutStreamer.EmitLabel(Label);
2706 return Label;
Bill Wendlingf5839192009-05-20 23:19:06 +00002707}
2708
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002709//===----------------------------------------------------------------------===//
2710// Emit Methods
2711//===----------------------------------------------------------------------===//
2712
Devang Patelc50078e2009-11-21 02:48:08 +00002713/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002714///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002715unsigned
2716DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002717 // Get the children.
2718 const std::vector<DIE *> &Children = Die->getChildren();
2719
2720 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002721 if (!Last && !Children.empty())
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00002722 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling55fccda2009-05-20 23:21:38 +00002723
2724 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002725 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002726
2727 // Get the abbreviation for this DIE.
2728 unsigned AbbrevNumber = Die->getAbbrevNumber();
2729 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2730
2731 // Set DIE offset
2732 Die->setOffset(Offset);
2733
2734 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002735 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002736
2737 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2738 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2739
2740 // Size the DIE attribute values.
2741 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2742 // Size attribute value.
Chris Lattner352c8e22010-04-05 00:18:22 +00002743 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling55fccda2009-05-20 23:21:38 +00002744
2745 // Size the DIE children if any.
2746 if (!Children.empty()) {
2747 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2748 "Children flag not set");
2749
2750 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002751 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002752
2753 // End of children marker.
2754 Offset += sizeof(int8_t);
2755 }
2756
2757 Die->setSize(Offset - Die->getOffset());
2758 return Offset;
2759}
2760
Devang Patelc50078e2009-11-21 02:48:08 +00002761/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002762///
Devang Patelc50078e2009-11-21 02:48:08 +00002763void DwarfDebug::computeSizeAndOffsets() {
Devang Patel7e5a86e2010-05-10 22:49:55 +00002764 unsigned PrevOffset = 0;
2765 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2766 E = CUMap.end(); I != E; ++I) {
2767 // Compute size of compile unit header.
2768 static unsigned Offset = PrevOffset +
2769 sizeof(int32_t) + // Length of Compilation Unit Info
2770 sizeof(int16_t) + // DWARF version number
2771 sizeof(int32_t) + // Offset Into Abbrev. Section
2772 sizeof(int8_t); // Pointer Size (in bytes)
2773 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
2774 PrevOffset = Offset;
2775 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002776}
2777
Chris Lattner733c69d2010-04-04 23:02:02 +00002778/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2779/// temporary label to it if SymbolStem is specified.
Chris Lattner66143d22010-04-04 22:59:04 +00002780static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner733c69d2010-04-04 23:02:02 +00002781 const char *SymbolStem = 0) {
Chris Lattner66143d22010-04-04 22:59:04 +00002782 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner733c69d2010-04-04 23:02:02 +00002783 if (!SymbolStem) return 0;
2784
Chris Lattner66143d22010-04-04 22:59:04 +00002785 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2786 Asm->OutStreamer.EmitLabel(TmpSym);
2787 return TmpSym;
2788}
2789
2790/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2791/// the start of each one.
Chris Lattnerd91fd8c2010-04-04 22:33:59 +00002792void DwarfDebug::EmitSectionLabels() {
Chris Lattner73266f92009-08-19 05:49:37 +00002793 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002794
Bill Wendling55fccda2009-05-20 23:21:38 +00002795 // Dwarf sections base addresses.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002796 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner66143d22010-04-04 22:59:04 +00002797 DwarfFrameSectionSym =
2798 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2799 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002800
Chris Lattner66143d22010-04-04 22:59:04 +00002801 DwarfInfoSectionSym =
2802 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2803 DwarfAbbrevSectionSym =
2804 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner733c69d2010-04-04 23:02:02 +00002805 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002806
2807 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner733c69d2010-04-04 23:02:02 +00002808 EmitSectionSym(Asm, MacroInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002809
Chris Lattner733c69d2010-04-04 23:02:02 +00002810 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2811 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2812 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2813 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002814 DwarfStrSectionSym =
2815 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel146c6f72010-04-16 23:33:45 +00002816 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2817 "debug_range");
Bill Wendling55fccda2009-05-20 23:21:38 +00002818
Chris Lattner66143d22010-04-04 22:59:04 +00002819 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner094932e2010-04-04 23:10:38 +00002820 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002821}
2822
Devang Patelc50078e2009-11-21 02:48:08 +00002823/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002824///
Devang Patelc50078e2009-11-21 02:48:08 +00002825void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002826 // Get the abbreviation for this DIE.
2827 unsigned AbbrevNumber = Die->getAbbrevNumber();
2828 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2829
Bill Wendling55fccda2009-05-20 23:21:38 +00002830 // Emit the code (index) for the abbreviation.
Chris Lattner97c69b72010-04-04 18:52:31 +00002831 if (Asm->isVerbose())
Chris Lattnerbcc79432010-01-22 23:18:42 +00002832 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2833 Twine::utohexstr(Die->getOffset()) + ":0x" +
2834 Twine::utohexstr(Die->getSize()) + " " +
2835 dwarf::TagString(Abbrev->getTag()));
Chris Lattner26be1c12010-04-04 19:09:29 +00002836 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002837
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002838 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling55fccda2009-05-20 23:21:38 +00002839 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2840
2841 // Emit the DIE attribute values.
2842 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2843 unsigned Attr = AbbrevData[i].getAttribute();
2844 unsigned Form = AbbrevData[i].getForm();
2845 assert(Form && "Too many attributes for DIE (check abbreviation)");
2846
Chris Lattner97c69b72010-04-04 18:52:31 +00002847 if (Asm->isVerbose())
Chris Lattner91e06b42010-01-24 18:54:17 +00002848 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2849
Bill Wendling55fccda2009-05-20 23:21:38 +00002850 switch (Attr) {
2851 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002852 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002853 break;
2854 case dwarf::DW_AT_abstract_origin: {
2855 DIEEntry *E = cast<DIEEntry>(Values[i]);
2856 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002857 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002858 Asm->EmitInt32(Addr);
2859 break;
2860 }
Devang Patel146c6f72010-04-16 23:33:45 +00002861 case dwarf::DW_AT_ranges: {
2862 // DW_AT_range Value encodes offset in debug_range section.
2863 DIEInteger *V = cast<DIEInteger>(Values[i]);
2864 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2865 V->getValue(),
2866 DwarfDebugRangeSectionSym,
2867 4);
2868 break;
2869 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002870 default:
2871 // Emit an attribute using the defined form.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002872 Values[i]->EmitValue(Asm, Form);
Bill Wendling55fccda2009-05-20 23:21:38 +00002873 break;
2874 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002875 }
2876
2877 // Emit the DIE children if any.
2878 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2879 const std::vector<DIE *> &Children = Die->getChildren();
2880
2881 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002882 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002883
Chris Lattner97c69b72010-04-04 18:52:31 +00002884 if (Asm->isVerbose())
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002885 Asm->OutStreamer.AddComment("End Of Children Mark");
2886 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002887 }
2888}
2889
Devang Patelfe0be132009-12-09 18:24:21 +00002890/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002891///
Devang Patelfe0be132009-12-09 18:24:21 +00002892void DwarfDebug::emitDebugInfo() {
2893 // Start debug info section.
2894 Asm->OutStreamer.SwitchSection(
2895 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel7e5a86e2010-05-10 22:49:55 +00002896 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2897 E = CUMap.end(); I != E; ++I) {
2898 CompileUnit *TheCU = I->second;
2899 DIE *Die = TheCU->getCUDie();
2900
2901 // Emit the compile units header.
2902 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2903 TheCU->getID()));
2904
2905 // Emit size of content not including length itself
2906 unsigned ContentSize = Die->getSize() +
2907 sizeof(int16_t) + // DWARF version number
2908 sizeof(int32_t) + // Offset Into Abbrev. Section
2909 sizeof(int8_t) + // Pointer Size (in bytes)
2910 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2911
2912 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2913 Asm->EmitInt32(ContentSize);
2914 Asm->OutStreamer.AddComment("DWARF version number");
2915 Asm->EmitInt16(dwarf::DWARF_VERSION);
2916 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2917 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2918 DwarfAbbrevSectionSym);
2919 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2920 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
2921
2922 emitDIE(Die);
2923 // FIXME - extra padding for gdb bug.
2924 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2925 Asm->EmitInt8(0);
2926 Asm->EmitInt8(0);
2927 Asm->EmitInt8(0);
2928 Asm->EmitInt8(0);
2929 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2930 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002931}
2932
Devang Patelc50078e2009-11-21 02:48:08 +00002933/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002934///
Devang Patelc50078e2009-11-21 02:48:08 +00002935void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002936 // Check to see if it is worth the effort.
2937 if (!Abbreviations.empty()) {
2938 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002939 Asm->OutStreamer.SwitchSection(
2940 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002941
Chris Lattnerb93558d2010-04-04 19:25:43 +00002942 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002943
2944 // For each abbrevation.
2945 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2946 // Get abbreviation data
2947 const DIEAbbrev *Abbrev = Abbreviations[i];
2948
2949 // Emit the abbrevations code (base 1 index.)
Chris Lattner26be1c12010-04-04 19:09:29 +00002950 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling55fccda2009-05-20 23:21:38 +00002951
2952 // Emit the abbreviations data.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002953 Abbrev->Emit(Asm);
Bill Wendling55fccda2009-05-20 23:21:38 +00002954 }
2955
2956 // Mark end of abbreviations.
Chris Lattner26be1c12010-04-04 19:09:29 +00002957 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002958
Chris Lattnerb93558d2010-04-04 19:25:43 +00002959 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002960 }
2961}
2962
Devang Patelc50078e2009-11-21 02:48:08 +00002963/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002964/// the line matrix.
2965///
Devang Patelc50078e2009-11-21 02:48:08 +00002966void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002967 // Define last address of section.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002968 Asm->OutStreamer.AddComment("Extended Op");
2969 Asm->EmitInt8(0);
2970
2971 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002972 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002973 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2974 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2975
2976 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnereb159d62010-03-10 01:17:49 +00002977
Chris Lattnerb93558d2010-04-04 19:25:43 +00002978 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002979 Asm->getTargetData().getPointerSize(),
2980 0/*AddrSpace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00002981
2982 // Mark end of matrix.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002983 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2984 Asm->EmitInt8(0);
Chris Lattnerad653482010-01-22 22:09:00 +00002985 Asm->EmitInt8(1);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002986 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002987}
2988
Devang Patelc50078e2009-11-21 02:48:08 +00002989/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002990///
Devang Patelc50078e2009-11-21 02:48:08 +00002991void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002992 // If the target is using .loc/.file, the assembler will be emitting the
2993 // .debug_line table automatically.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002994 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002995 return;
2996
2997 // Minimum line delta, thus ranging from -10..(255-10).
2998 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2999 // Maximum line delta, thus ranging from -10..(255-10).
3000 const int MaxLineDelta = 255 + MinLineDelta;
3001
3002 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00003003 Asm->OutStreamer.SwitchSection(
3004 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003005
3006 // Construct the section header.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003007 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003008 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
3009 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003010 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003011
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003012 Asm->OutStreamer.AddComment("DWARF version number");
3013 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00003014
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003015 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003016 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
3017 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003018 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003019
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003020 Asm->OutStreamer.AddComment("Minimum Instruction Length");
3021 Asm->EmitInt8(1);
3022 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
3023 Asm->EmitInt8(1);
3024 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
3025 Asm->EmitInt8(MinLineDelta);
3026 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3027 Asm->EmitInt8(MaxLineDelta);
3028 Asm->OutStreamer.AddComment("Special Opcode Base");
3029 Asm->EmitInt8(-MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00003030
3031 // Line number standard opcode encodings argument count
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003032 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3033 Asm->EmitInt8(0);
3034 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3035 Asm->EmitInt8(1);
3036 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3037 Asm->EmitInt8(1);
3038 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3039 Asm->EmitInt8(1);
3040 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3041 Asm->EmitInt8(1);
3042 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3043 Asm->EmitInt8(0);
3044 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3045 Asm->EmitInt8(0);
3046 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3047 Asm->EmitInt8(0);
3048 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3049 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003050
3051 // Emit directories.
3052 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnercc564642010-01-23 03:11:46 +00003053 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner97c69b72010-04-04 18:52:31 +00003054 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattnercc564642010-01-23 03:11:46 +00003055 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003056 }
3057
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003058 Asm->OutStreamer.AddComment("End of directories");
3059 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003060
3061 // Emit files.
3062 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3063 // Remember source id starts at 1.
3064 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnercc564642010-01-23 03:11:46 +00003065 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner97c69b72010-04-04 18:52:31 +00003066 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattnercc564642010-01-23 03:11:46 +00003067 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
3068
Chris Lattner26be1c12010-04-04 19:09:29 +00003069 Asm->EmitULEB128(Id.first, "Directory #");
3070 Asm->EmitULEB128(0, "Mod date");
3071 Asm->EmitULEB128(0, "File size");
Bill Wendling55fccda2009-05-20 23:21:38 +00003072 }
3073
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003074 Asm->OutStreamer.AddComment("End of files");
3075 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003076
Chris Lattnerb93558d2010-04-04 19:25:43 +00003077 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003078
3079 // A sequence for each text section.
3080 unsigned SecSrcLinesSize = SectionSourceLines.size();
3081
3082 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3083 // Isolate current sections line info.
3084 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3085
Bill Wendling55fccda2009-05-20 23:21:38 +00003086 // Dwarf assumes we start with first line of first source file.
3087 unsigned Source = 1;
3088 unsigned Line = 1;
3089
3090 // Construct rows of the address, source, line, column matrix.
3091 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3092 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner8d9d06a2010-03-14 08:15:55 +00003093 MCSymbol *Label = LineInfo.getLabel();
Chris Lattner31ae74d2010-03-14 02:20:58 +00003094 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling55fccda2009-05-20 23:21:38 +00003095
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003096 if (Asm->isVerbose()) {
Chris Lattneree3b40f2010-03-10 01:04:13 +00003097 std::pair<unsigned, unsigned> SrcID =
Bill Wendling55fccda2009-05-20 23:21:38 +00003098 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattneree3b40f2010-03-10 01:04:13 +00003099 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner48fb0b12010-03-10 02:29:31 +00003100 "/" +
3101 Twine(getSourceFileName(SrcID.second)) +
Chris Lattneree3b40f2010-03-10 01:04:13 +00003102 ":" + Twine(LineInfo.getLine()));
Bill Wendling55fccda2009-05-20 23:21:38 +00003103 }
3104
3105 // Define the line address.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003106 Asm->OutStreamer.AddComment("Extended Op");
3107 Asm->EmitInt8(0);
3108 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003109 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003110
3111 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3112 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3113
3114 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003115 Asm->OutStreamer.EmitSymbolValue(Label,
3116 Asm->getTargetData().getPointerSize(),
Chris Lattner31ae74d2010-03-14 02:20:58 +00003117 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00003118
Bill Wendling55fccda2009-05-20 23:21:38 +00003119 // If change of source, then switch to the new source.
3120 if (Source != LineInfo.getSourceID()) {
3121 Source = LineInfo.getSourceID();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003122 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3123 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner26be1c12010-04-04 19:09:29 +00003124 Asm->EmitULEB128(Source, "New Source");
Bill Wendling55fccda2009-05-20 23:21:38 +00003125 }
3126
3127 // If change of line.
3128 if (Line != LineInfo.getLine()) {
3129 // Determine offset.
3130 int Offset = LineInfo.getLine() - Line;
3131 int Delta = Offset - MinLineDelta;
3132
3133 // Update line.
3134 Line = LineInfo.getLine();
3135
3136 // If delta is small enough and in range...
3137 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3138 // ... then use fast opcode.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003139 Asm->OutStreamer.AddComment("Line Delta");
3140 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00003141 } else {
3142 // ... otherwise use long hand.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003143 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling55fccda2009-05-20 23:21:38 +00003144 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner26be1c12010-04-04 19:09:29 +00003145 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003146 Asm->OutStreamer.AddComment("DW_LNS_copy");
3147 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00003148 }
3149 } else {
3150 // Copy the previous row (different address or source)
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003151 Asm->OutStreamer.AddComment("DW_LNS_copy");
3152 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00003153 }
3154 }
3155
Devang Patelc50078e2009-11-21 02:48:08 +00003156 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003157 }
3158
3159 if (SecSrcLinesSize == 0)
3160 // Because we're emitting a debug_line section, we still need a line
3161 // table. The linker and friends expect it to exist. If there's nothing to
3162 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00003163 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003164
Chris Lattnerb93558d2010-04-04 19:25:43 +00003165 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003166}
3167
Devang Patelc50078e2009-11-21 02:48:08 +00003168/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003169///
Devang Patelc50078e2009-11-21 02:48:08 +00003170void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003171 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003172 return;
3173
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003174 int stackGrowth = Asm->getTargetData().getPointerSize();
3175 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3176 TargetFrameInfo::StackGrowsDown)
3177 stackGrowth *= -1;
Bill Wendling55fccda2009-05-20 23:21:38 +00003178
3179 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00003180 Asm->OutStreamer.SwitchSection(
3181 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003182
Chris Lattnerb93558d2010-04-04 19:25:43 +00003183 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003184 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003185 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3186 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003187
Chris Lattnerb93558d2010-04-04 19:25:43 +00003188 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003189 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling55fccda2009-05-20 23:21:38 +00003190 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003191 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00003192 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003193 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnercc564642010-01-23 03:11:46 +00003194 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner26be1c12010-04-04 19:09:29 +00003195 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3196 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003197 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003198 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling55fccda2009-05-20 23:21:38 +00003199 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling55fccda2009-05-20 23:21:38 +00003200
3201 std::vector<MachineMove> Moves;
3202 RI->getInitialFrameState(Moves);
3203
Chris Lattner193fec02010-04-04 23:41:46 +00003204 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00003205
Chris Lattner7101e232010-04-28 01:05:45 +00003206 Asm->EmitAlignment(2);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003207 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003208}
3209
Devang Patelc50078e2009-11-21 02:48:08 +00003210/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00003211/// section.
Chris Lattnerdd21da82010-03-13 07:26:18 +00003212void DwarfDebug::
3213emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003214 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003215 return;
3216
3217 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00003218 Asm->OutStreamer.SwitchSection(
3219 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003220
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003221 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattnerdd21da82010-03-13 07:26:18 +00003222 MCSymbol *DebugFrameBegin =
Chris Lattnerb93558d2010-04-04 19:25:43 +00003223 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattnerdd21da82010-03-13 07:26:18 +00003224 MCSymbol *DebugFrameEnd =
Chris Lattnerb93558d2010-04-04 19:25:43 +00003225 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003226 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003227
Chris Lattnerdd21da82010-03-13 07:26:18 +00003228 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling55fccda2009-05-20 23:21:38 +00003229
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003230 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003231 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3232 DwarfFrameSectionSym);
Bill Wendling55fccda2009-05-20 23:21:38 +00003233
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003234 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerb93558d2010-04-04 19:25:43 +00003235 MCSymbol *FuncBeginSym =
3236 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner314e3362010-03-13 07:40:56 +00003237 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003238 Asm->getTargetData().getPointerSize(),
3239 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00003240
3241
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003242 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003243 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003244 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00003245
Chris Lattner193fec02010-04-04 23:41:46 +00003246 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00003247
Chris Lattner7101e232010-04-28 01:05:45 +00003248 Asm->EmitAlignment(2);
Chris Lattnerdd21da82010-03-13 07:26:18 +00003249 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling55fccda2009-05-20 23:21:38 +00003250}
3251
Devang Patelfe0be132009-12-09 18:24:21 +00003252/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3253///
3254void DwarfDebug::emitDebugPubNames() {
Devang Patel7e5a86e2010-05-10 22:49:55 +00003255 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3256 E = CUMap.end(); I != E; ++I) {
3257 CompileUnit *TheCU = I->second;
3258 // Start the dwarf pubnames section.
3259 Asm->OutStreamer.SwitchSection(
3260 Asm->getObjFileLowering().getDwarfPubNamesSection());
Chris Lattnercc564642010-01-23 03:11:46 +00003261
Devang Patel7e5a86e2010-05-10 22:49:55 +00003262 Asm->OutStreamer.AddComment("Length of Public Names Info");
3263 Asm->EmitLabelDifference(
3264 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3265 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
3266
3267 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3268 TheCU->getID()));
3269
3270 Asm->OutStreamer.AddComment("DWARF Version");
3271 Asm->EmitInt16(dwarf::DWARF_VERSION);
3272
3273 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3274 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3275 DwarfInfoSectionSym);
3276
3277 Asm->OutStreamer.AddComment("Compilation Unit Length");
3278 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3279 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3280 4);
3281
3282 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3283 for (StringMap<DIE*>::const_iterator
3284 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3285 const char *Name = GI->getKeyData();
3286 DIE *Entity = GI->second;
3287
3288 Asm->OutStreamer.AddComment("DIE offset");
3289 Asm->EmitInt32(Entity->getOffset());
3290
3291 if (Asm->isVerbose())
3292 Asm->OutStreamer.AddComment("External Name");
3293 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3294 }
3295
3296 Asm->OutStreamer.AddComment("End Mark");
3297 Asm->EmitInt32(0);
3298 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3299 TheCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00003300 }
Bill Wendling55fccda2009-05-20 23:21:38 +00003301}
3302
Devang Patelec13b4f2009-11-24 01:14:22 +00003303void DwarfDebug::emitDebugPubTypes() {
Devang Patel7e5a86e2010-05-10 22:49:55 +00003304 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3305 E = CUMap.end(); I != E; ++I) {
3306 CompileUnit *TheCU = I->second;
3307 // Start the dwarf pubnames section.
3308 Asm->OutStreamer.SwitchSection(
3309 Asm->getObjFileLowering().getDwarfPubTypesSection());
3310 Asm->OutStreamer.AddComment("Length of Public Types Info");
3311 Asm->EmitLabelDifference(
3312 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3313 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Chris Lattnercc564642010-01-23 03:11:46 +00003314
Devang Patel7e5a86e2010-05-10 22:49:55 +00003315 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3316 TheCU->getID()));
3317
3318 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3319 Asm->EmitInt16(dwarf::DWARF_VERSION);
3320
3321 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3322 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3323 DwarfInfoSectionSym);
3324
3325 Asm->OutStreamer.AddComment("Compilation Unit Length");
3326 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3327 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3328 4);
3329
3330 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3331 for (StringMap<DIE*>::const_iterator
3332 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3333 const char *Name = GI->getKeyData();
3334 DIE * Entity = GI->second;
3335
3336 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3337 Asm->EmitInt32(Entity->getOffset());
3338
3339 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3340 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3341 }
3342
3343 Asm->OutStreamer.AddComment("End Mark");
3344 Asm->EmitInt32(0);
3345 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3346 TheCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00003347 }
Devang Patelec13b4f2009-11-24 01:14:22 +00003348}
3349
Devang Patelc50078e2009-11-21 02:48:08 +00003350/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003351///
Devang Patelc50078e2009-11-21 02:48:08 +00003352void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003353 // Check to see if it is worth the effort.
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003354 if (StringPool.empty()) return;
3355
3356 // Start the dwarf str section.
3357 Asm->OutStreamer.SwitchSection(
Chris Lattner73266f92009-08-19 05:49:37 +00003358 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003359
Chris Lattner0f093f82010-03-13 02:17:42 +00003360 // Get all of the string pool entries and put them in an array by their ID so
3361 // we can sort them.
3362 SmallVector<std::pair<unsigned,
3363 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3364
3365 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3366 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3367 Entries.push_back(std::make_pair(I->second.second, &*I));
3368
3369 array_pod_sort(Entries.begin(), Entries.end());
3370
3371 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003372 // Emit a label for reference from debug information entries.
Chris Lattner0f093f82010-03-13 02:17:42 +00003373 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003374
3375 // Emit the string itself.
Chris Lattner0f093f82010-03-13 02:17:42 +00003376 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00003377 }
3378}
3379
Devang Patelc50078e2009-11-21 02:48:08 +00003380/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003381///
Devang Patelc50078e2009-11-21 02:48:08 +00003382void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003383 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00003384 Asm->OutStreamer.SwitchSection(
3385 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003386}
3387
3388/// EmitDebugARanges - Emit visible names into a debug aranges section.
3389///
3390void DwarfDebug::EmitDebugARanges() {
3391 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003392 Asm->OutStreamer.SwitchSection(
3393 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003394}
3395
Devang Patelc50078e2009-11-21 02:48:08 +00003396/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003397///
Devang Patelc50078e2009-11-21 02:48:08 +00003398void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003399 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003400 Asm->OutStreamer.SwitchSection(
Devang Patel146c6f72010-04-16 23:33:45 +00003401 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patelce7bf012010-04-27 19:46:33 +00003402 unsigned char Size = Asm->getTargetData().getPointerSize();
3403 for (SmallVector<const MCSymbol *, 8>::iterator
3404 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3405 I != E; ++I) {
3406 if (*I)
3407 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel146c6f72010-04-16 23:33:45 +00003408 else
Devang Patelce7bf012010-04-27 19:46:33 +00003409 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel146c6f72010-04-16 23:33:45 +00003410 }
Bill Wendling55fccda2009-05-20 23:21:38 +00003411}
3412
Devang Patelc50078e2009-11-21 02:48:08 +00003413/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003414///
Devang Patelc50078e2009-11-21 02:48:08 +00003415void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00003416 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00003417 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00003418 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00003419 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00003420 }
3421}
3422
Devang Patelc50078e2009-11-21 02:48:08 +00003423/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00003424/// Section Header:
3425/// 1. length of section
3426/// 2. Dwarf version number
3427/// 3. address size.
3428///
3429/// Entries (one "entry" for each function that was inlined):
3430///
3431/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3432/// otherwise offset into __debug_str for regular function name.
3433/// 2. offset into __debug_str section for regular function name.
3434/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3435/// instances for the function.
3436///
3437/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3438/// inlined instance; the die_offset points to the inlined_subroutine die in the
3439/// __debug_info section, and the low_pc is the starting address for the
3440/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00003441void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003442 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003443 return;
3444
Devang Patel7e5a86e2010-05-10 22:49:55 +00003445 if (!FirstCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00003446 return;
3447
Chris Lattner73266f92009-08-19 05:49:37 +00003448 Asm->OutStreamer.SwitchSection(
3449 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerad653482010-01-22 22:09:00 +00003450
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003451 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003452 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3453 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003454
Chris Lattnerb93558d2010-04-04 19:25:43 +00003455 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003456
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003457 Asm->OutStreamer.AddComment("Dwarf Version");
3458 Asm->EmitInt16(dwarf::DWARF_VERSION);
3459 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003460 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00003461
Devang Patel9c371c62010-05-07 20:54:48 +00003462 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel90a0fe32009-11-10 23:06:00 +00003463 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00003464
Devang Patel9c371c62010-05-07 20:54:48 +00003465 const MDNode *Node = *I;
3466 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbachb23f2422009-11-22 19:20:36 +00003467 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00003468 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00003469 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00003470 StringRef LName = SP.getLinkageName();
3471 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00003472
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003473 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnercc564642010-01-23 03:11:46 +00003474 if (LName.empty()) {
3475 Asm->OutStreamer.EmitBytes(Name, 0);
3476 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3477 } else
Chris Lattnerc06b4742010-04-04 23:25:33 +00003478 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3479 DwarfStrSectionSym);
Devang Patel90a0fe32009-11-10 23:06:00 +00003480
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003481 Asm->OutStreamer.AddComment("Function name");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003482 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner26be1c12010-04-04 19:09:29 +00003483 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling55fccda2009-05-20 23:21:38 +00003484
Devang Patel90a0fe32009-11-10 23:06:00 +00003485 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00003486 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner97c69b72010-04-04 18:52:31 +00003487 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattneread58652010-03-09 00:31:02 +00003488 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00003489
Chris Lattner97c69b72010-04-04 18:52:31 +00003490 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003491 Asm->OutStreamer.EmitSymbolValue(LI->first,
3492 Asm->getTargetData().getPointerSize(),0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003493 }
3494 }
3495
Chris Lattnerb93558d2010-04-04 19:25:43 +00003496 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003497}