blob: 4da549866bf5bd5b7407fdbde10a19917ccbd823 [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
Bill Wendlingce3c6252010-04-07 09:28:04 +000050namespace {
51 const char *DWARFGroupName = "DWARF Emission";
52 const char *DbgTimerName = "DWARF Debug Writer";
53} // end anonymous namespace
54
Bill Wendlingb12b3d72009-05-15 09:23:25 +000055//===----------------------------------------------------------------------===//
56
57/// Configuration values for initial hash set sizes (log2).
58///
Bill Wendlingb12b3d72009-05-15 09:23:25 +000059static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-05-15 09:23:25 +000060
61namespace llvm {
62
63//===----------------------------------------------------------------------===//
64/// CompileUnit - This dwarf writer support class manages information associate
65/// with a source file.
Nick Lewyckyee68f452009-11-17 08:11:44 +000066class CompileUnit {
Bill Wendlingb12b3d72009-05-15 09:23:25 +000067 /// ID - File identifier for source.
68 ///
69 unsigned ID;
70
71 /// Die - Compile unit debug information entry.
72 ///
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +000073 const OwningPtr<DIE> CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000074
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000075 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel1233f912009-11-21 00:31:03 +000076 DIE *IndexTyDie;
77
Bill Wendlingb12b3d72009-05-15 09:23:25 +000078 /// GVToDieMap - Tracks the mapping of unit level debug informaton
79 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000080 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel7d707f92010-01-19 06:19:05 +000081 DenseMap<MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000082
83 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
84 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel15e723d2009-08-28 23:24:31 +000085 /// FIXME : Rename
Devang Patel7d707f92010-01-19 06:19:05 +000086 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000087
88 /// Globals - A map of globally visible named entities for this unit.
89 ///
90 StringMap<DIE*> Globals;
91
Devang Patelec13b4f2009-11-24 01:14:22 +000092 /// GlobalTypes - A map of globally visible types for this unit.
93 ///
94 StringMap<DIE*> GlobalTypes;
95
Bill Wendlingb12b3d72009-05-15 09:23:25 +000096public:
97 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +000098 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +000099
100 // Accessors.
Devang Patelec13b4f2009-11-24 01:14:22 +0000101 unsigned getID() const { return ID; }
Jeffrey Yasskinc3545712010-03-11 18:29:55 +0000102 DIE* getCUDie() const { return CUDie.get(); }
Devang Patelec13b4f2009-11-24 01:14:22 +0000103 const StringMap<DIE*> &getGlobals() const { return Globals; }
104 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000105
106 /// hasContent - Return true if this compile unit has something to write out.
107 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000108 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000109
Devang Patelc50078e2009-11-21 02:48:08 +0000110 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000111 ///
Benjamin Kramer648940b2010-03-31 20:15:45 +0000112 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000113
Devang Patelec13b4f2009-11-24 01:14:22 +0000114 /// addGlobalType - Add a new global type to the compile unit.
115 ///
Benjamin Kramer648940b2010-03-31 20:15:45 +0000116 void addGlobalType(StringRef Name, DIE *Die) {
Devang Patelec13b4f2009-11-24 01:14:22 +0000117 GlobalTypes[Name] = Die;
118 }
119
Devang Pateld90672c2009-11-20 21:37:22 +0000120 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000121 /// specified debug variable.
Devang Pateld90672c2009-11-20 21:37:22 +0000122 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +0000123
Devang Pateld90672c2009-11-20 21:37:22 +0000124 /// insertDIE - Insert DIE into the map.
125 void insertDIE(MDNode *N, DIE *D) {
126 GVToDieMap.insert(std::make_pair(N, D));
127 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000128
Devang Pateld90672c2009-11-20 21:37:22 +0000129 /// getDIEEntry - Returns the debug information entry for the speciefied
130 /// debug variable.
Devang Patel8287d662009-12-15 19:16:48 +0000131 DIEEntry *getDIEEntry(MDNode *N) {
Devang Patel7d707f92010-01-19 06:19:05 +0000132 DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel8287d662009-12-15 19:16:48 +0000133 if (I == GVToDIEEntryMap.end())
134 return NULL;
135 return I->second;
136 }
Devang Pateld90672c2009-11-20 21:37:22 +0000137
138 /// insertDIEEntry - Insert debug information entry into the map.
139 void insertDIEEntry(MDNode *N, DIEEntry *E) {
140 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000141 }
142
Devang Patelc50078e2009-11-21 02:48:08 +0000143 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000144 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000145 void addDie(DIE *Buffer) {
146 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000147 }
Devang Patel1233f912009-11-21 00:31:03 +0000148
149 // getIndexTyDie - Get an anonymous type for index type.
150 DIE *getIndexTyDie() {
151 return IndexTyDie;
152 }
153
Jim Grosbachb23f2422009-11-22 19:20:36 +0000154 // setIndexTyDie - Set D as anonymous type for index which can be reused
155 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000156 void setIndexTyDie(DIE *D) {
157 IndexTyDie = D;
158 }
159
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000160};
161
162//===----------------------------------------------------------------------===//
163/// DbgVariable - This class is used to track local variable information.
164///
Devang Patel4cb32c32009-11-16 21:53:40 +0000165class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000166 DIVariable Var; // Variable Descriptor.
167 unsigned FrameIndex; // Variable frame index.
Devang Patel14da02f2010-03-15 18:33:46 +0000168 const MachineInstr *DbgValueMInsn; // DBG_VALUE
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000169 // DbgValueLabel - DBG_VALUE is effective from this label.
170 MCSymbol *DbgValueLabel;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000171 DbgVariable *const AbstractVar; // Abstract variable for this variable.
Devang Patel90a0fe32009-11-10 23:06:00 +0000172 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000173public:
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000174 // AbsVar may be NULL.
175 DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar)
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000176 : Var(V), FrameIndex(I), DbgValueMInsn(0),
177 DbgValueLabel(0), AbstractVar(AbsVar), TheDIE(0) {}
Devang Patel14da02f2010-03-15 18:33:46 +0000178 DbgVariable(DIVariable V, const MachineInstr *MI, DbgVariable *AbsVar)
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000179 : Var(V), FrameIndex(0), DbgValueMInsn(MI), DbgValueLabel(0),
180 AbstractVar(AbsVar), TheDIE(0)
Devang Patel14da02f2010-03-15 18:33:46 +0000181 {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000182
183 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000184 DIVariable getVariable() const { return Var; }
185 unsigned getFrameIndex() const { return FrameIndex; }
Devang Patel14da02f2010-03-15 18:33:46 +0000186 const MachineInstr *getDbgValue() const { return DbgValueMInsn; }
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000187 MCSymbol *getDbgValueLabel() const { return DbgValueLabel; }
188 void setDbgValueLabel(MCSymbol *L) { DbgValueLabel = L; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000189 DbgVariable *getAbstractVariable() const { return AbstractVar; }
190 void setDIE(DIE *D) { TheDIE = D; }
191 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000192};
193
194//===----------------------------------------------------------------------===//
Devang Patelce7bf012010-04-27 19:46:33 +0000195/// DbgRange - This is used to track range of instructions with identical
196/// debug info scope.
197///
198typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
199
200//===----------------------------------------------------------------------===//
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000201/// DbgScope - This class is used to track scope information.
202///
Devang Patel4cb32c32009-11-16 21:53:40 +0000203class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000204 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000205 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelae89d3b2010-01-26 21:39:14 +0000206 // Location at which this scope is inlined.
207 AssertingVH<MDNode> InlinedAtLocation;
Devang Patel90a0fe32009-11-10 23:06:00 +0000208 bool AbstractScope; // Abstract Scope
Devang Patel90ecd192009-10-01 18:25:23 +0000209 const MachineInstr *LastInsn; // Last instruction of this scope.
210 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patelce7bf012010-04-27 19:46:33 +0000211 unsigned DFSIn, DFSOut;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000212 // Scopes defined in scope. Contents not owned.
213 SmallVector<DbgScope *, 4> Scopes;
214 // Variables declared in scope. Contents owned.
215 SmallVector<DbgVariable *, 8> Variables;
Devang Patelce7bf012010-04-27 19:46:33 +0000216 SmallVector<DbgRange, 4> Ranges;
Owen Anderson696d4862009-06-24 22:53:20 +0000217 // Private state for dump()
218 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000219public:
Devang Pateldd7bb432009-10-14 21:08:09 +0000220 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000221 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patelce7bf012010-04-27 19:46:33 +0000222 LastInsn(0), FirstInsn(0),
223 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000224 virtual ~DbgScope();
225
226 // Accessors.
227 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000228 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000229 DIDescriptor getDesc() const { return Desc; }
Chris Lattner0f093f82010-03-13 02:17:42 +0000230 MDNode *getInlinedAt() const { return InlinedAtLocation; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000231 MDNode *getScopeNode() const { return Desc.getNode(); }
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000232 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
233 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patelce7bf012010-04-27 19:46:33 +0000234 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
235
236 /// openInsnRange - This scope covers instruction range starting from MI.
237 void openInsnRange(const MachineInstr *MI) {
238 if (!FirstInsn)
239 FirstInsn = MI;
240
241 if (Parent)
242 Parent->openInsnRange(MI);
243 }
244
245 /// extendInsnRange - Extend the current instruction range covered by
246 /// this scope.
247 void extendInsnRange(const MachineInstr *MI) {
248 assert (FirstInsn && "MI Range is not open!");
249 LastInsn = MI;
250 if (Parent)
251 Parent->extendInsnRange(MI);
252 }
253
254 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
255 /// until now. This is used when a new scope is encountered while walking
256 /// machine instructions.
257 void closeInsnRange(DbgScope *NewScope = NULL) {
258 assert (LastInsn && "Last insn missing!");
259 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
260 FirstInsn = NULL;
261 LastInsn = NULL;
262 // If Parent dominates NewScope then do not close Parent's instruction
263 // range.
264 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
265 Parent->closeInsnRange(NewScope);
266 }
267
Devang Patel90a0fe32009-11-10 23:06:00 +0000268 void setAbstractScope() { AbstractScope = true; }
269 bool isAbstractScope() const { return AbstractScope; }
Devang Patelce7bf012010-04-27 19:46:33 +0000270
271 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
272 unsigned getDFSOut() const { return DFSOut; }
273 void setDFSOut(unsigned O) { DFSOut = O; }
274 unsigned getDFSIn() const { return DFSIn; }
275 void setDFSIn(unsigned I) { DFSIn = I; }
276 bool dominates(const DbgScope *S) {
277 if (S == this)
278 return true;
279 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
280 return true;
281 return false;
282 }
Devang Patel90a0fe32009-11-10 23:06:00 +0000283
Devang Patelc50078e2009-11-21 02:48:08 +0000284 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000285 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000286 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000287
Devang Patelc50078e2009-11-21 02:48:08 +0000288 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000289 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000290 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000291
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000292#ifndef NDEBUG
293 void dump() const;
294#endif
295};
Devang Patelce7bf012010-04-27 19:46:33 +0000296
Chris Lattner8b9430c2010-04-05 04:09:20 +0000297} // end llvm namespace
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000298
299#ifndef NDEBUG
300void DbgScope::dump() const {
David Greenedbc06132009-12-24 00:31:35 +0000301 raw_ostream &err = dbgs();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000302 err.indent(IndentLevel);
Devang Patel90a0fe32009-11-10 23:06:00 +0000303 MDNode *N = Desc.getNode();
304 N->dump();
Devang Patel90a0fe32009-11-10 23:06:00 +0000305 if (AbstractScope)
306 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000307
308 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000309 if (!Scopes.empty())
310 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000311 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
312 if (Scopes[i] != this)
313 Scopes[i]->dump();
314
315 IndentLevel -= 2;
316}
317#endif
318
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000319DbgScope::~DbgScope() {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000320 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
321 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000322}
323
Chris Lattnerec9a1f42010-04-05 05:11:15 +0000324DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000325 : Asm(A), MMI(Asm->MMI), ModuleCU(0),
Chris Lattner2cb53302010-04-05 03:52:55 +0000326 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel146c6f72010-04-16 23:33:45 +0000327 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattner0f093f82010-03-13 02:17:42 +0000328 NextStringPoolNumber = 0;
Chris Lattner66143d22010-04-04 22:59:04 +0000329
Chris Lattner094932e2010-04-04 23:10:38 +0000330 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
331 DwarfStrSectionSym = TextSectionSym = 0;
Devang Patelce7bf012010-04-27 19:46:33 +0000332 DwarfDebugRangeSectionSym = 0;
333 FunctionBeginSym = 0;
Edwin Törökc669cb62010-04-07 10:44:46 +0000334 if (TimePassesIsEnabled) {
335 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
336 beginModule(M);
337 } else {
338 beginModule(M);
339 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000340}
341DwarfDebug::~DwarfDebug() {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000342 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
343 DIEBlocks[j]->~DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000344}
345
Chris Lattner0f093f82010-03-13 02:17:42 +0000346MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
347 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
348 if (Entry.first) return Entry.first;
349
350 Entry.second = NextStringPoolNumber++;
Chris Lattnerb93558d2010-04-04 19:25:43 +0000351 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattner0f093f82010-03-13 02:17:42 +0000352}
353
354
Devang Patelc50078e2009-11-21 02:48:08 +0000355/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000356///
Devang Patelc50078e2009-11-21 02:48:08 +0000357void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000358 // Profile the node so that we can make it unique.
359 FoldingSetNodeID ID;
360 Abbrev.Profile(ID);
361
362 // Check the set for priors.
363 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
364
365 // If it's newly added.
366 if (InSet == &Abbrev) {
367 // Add to abbreviation list.
368 Abbreviations.push_back(&Abbrev);
369
370 // Assign the vector position + 1 as its number.
371 Abbrev.setNumber(Abbreviations.size());
372 } else {
373 // Assign existing abbreviation number.
374 Abbrev.setNumber(InSet->getNumber());
375 }
376}
377
Devang Patelc50078e2009-11-21 02:48:08 +0000378/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000379/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000380DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000381 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000382 return Value;
383}
384
Devang Patelc50078e2009-11-21 02:48:08 +0000385/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000386///
Devang Patelc50078e2009-11-21 02:48:08 +0000387void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000388 unsigned Form, uint64_t Integer) {
389 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000390 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000391 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000392}
393
Devang Patelc50078e2009-11-21 02:48:08 +0000394/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000395///
Devang Patelc50078e2009-11-21 02:48:08 +0000396void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000397 unsigned Form, int64_t Integer) {
398 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000399 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000400 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000401}
402
Devang Pateldf0f2152009-12-02 15:25:16 +0000403/// addString - Add a string attribute data and value. DIEString only
404/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000405void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnercc564642010-01-23 03:11:46 +0000406 StringRef String) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000407 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000408 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000409}
410
Devang Patelc50078e2009-11-21 02:48:08 +0000411/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000412///
Devang Patelc50078e2009-11-21 02:48:08 +0000413void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000414 const MCSymbol *Label) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000415 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000416 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000417}
418
Devang Patelc50078e2009-11-21 02:48:08 +0000419/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000420///
Devang Patelc50078e2009-11-21 02:48:08 +0000421void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000422 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000423 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000424 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000425}
426
Chris Lattner855b6bb2010-04-05 05:24:55 +0000427/// addDIEEntry - Add a DIE attribute data and value.
428///
429void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
430 DIE *Entry) {
431 Die->addValue(Attribute, Form, createDIEEntry(Entry));
432}
433
434
Devang Patelc50078e2009-11-21 02:48:08 +0000435/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000436///
Devang Patelc50078e2009-11-21 02:48:08 +0000437void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000438 DIEBlock *Block) {
Chris Lattner352c8e22010-04-05 00:18:22 +0000439 Block->ComputeSize(Asm);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000440 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patelc50078e2009-11-21 02:48:08 +0000441 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000442}
443
Devang Patelc50078e2009-11-21 02:48:08 +0000444/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000445/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000446void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000447 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000448 if (!V->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000449 return;
450
451 unsigned Line = V->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000452 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
453 V->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000454 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000455 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
456 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000457}
458
Devang Patelc50078e2009-11-21 02:48:08 +0000459/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000460/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000461void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000462 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000463 if (!G->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000464 return;
465
466 unsigned Line = G->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000467 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
468 G->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000469 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000470 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
471 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000472}
Devang Patel318d70d2009-08-31 22:47:13 +0000473
Devang Patelc50078e2009-11-21 02:48:08 +0000474/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000475/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000476void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-08-31 22:47:13 +0000477 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000478 if (!SP->getCompileUnit().Verify())
Devang Patel318d70d2009-08-31 22:47:13 +0000479 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000480 // If the line number is 0, don't add it.
481 if (SP->getLineNumber() == 0)
482 return;
483
Devang Patel318d70d2009-08-31 22:47:13 +0000484 unsigned Line = SP->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000485 if (!SP->getContext().Verify())
486 return;
Devang Patele485a5d2010-03-24 21:30:35 +0000487 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
488 SP->getFilename());
Devang Patel318d70d2009-08-31 22:47:13 +0000489 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000490 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
491 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000492}
493
Devang Patelc50078e2009-11-21 02:48:08 +0000494/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000495/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000496void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000497 // If there is no compile unit specified, don't add a line #.
498 DICompileUnit CU = Ty->getCompileUnit();
Devang Patel9e592492010-03-08 20:52:55 +0000499 if (!CU.Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000500 return;
501
502 unsigned Line = Ty->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000503 if (!Ty->getContext().Verify())
504 return;
505 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
506 Ty->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000507 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000508 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
509 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000510}
511
Devang Patel8287d662009-12-15 19:16:48 +0000512/// addSourceLine - Add location information to specified debug information
513/// entry.
514void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
515 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000516 if (!NS->getCompileUnit().Verify())
Devang Patel8287d662009-12-15 19:16:48 +0000517 return;
518
519 unsigned Line = NS->getLineNumber();
520 StringRef FN = NS->getFilename();
521 StringRef Dir = NS->getDirectory();
522
523 unsigned FileID = GetOrCreateSourceID(Dir, FN);
524 assert(FileID && "Invalid file id");
525 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
526 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
527}
528
Caroline Tice248d5572009-08-31 21:19:37 +0000529/* Byref variables, in Blocks, are declared by the programmer as
530 "SomeType VarName;", but the compiler creates a
531 __Block_byref_x_VarName struct, and gives the variable VarName
532 either the struct, or a pointer to the struct, as its type. This
533 is necessary for various behind-the-scenes things the compiler
534 needs to do with by-reference variables in blocks.
535
536 However, as far as the original *programmer* is concerned, the
537 variable should still have type 'SomeType', as originally declared.
538
539 The following function dives into the __Block_byref_x_VarName
540 struct to find the original type of the variable. This will be
541 passed back to the code generating the type for the Debug
542 Information Entry for the variable 'VarName'. 'VarName' will then
543 have the original type 'SomeType' in its debug information.
544
545 The original type 'SomeType' will be the type of the field named
546 'VarName' inside the __Block_byref_x_VarName struct.
547
548 NOTE: In order for this to not completely fail on the debugger
549 side, the Debug Information Entry for the variable VarName needs to
550 have a DW_AT_location that tells the debugger how to unwind through
551 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000552 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000553
554/// Find the type the programmer originally declared the variable to be
555/// and return that type.
556///
Devang Patelc50078e2009-11-21 02:48:08 +0000557DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000558
559 DIType subType = Ty;
560 unsigned tag = Ty.getTag();
561
562 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000563 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000564 subType = DTy.getTypeDerivedFrom();
565 }
566
567 DICompositeType blockStruct = DICompositeType(subType.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000568 DIArray Elements = blockStruct.getTypeArray();
569
Caroline Tice248d5572009-08-31 21:19:37 +0000570 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
571 DIDescriptor Element = Elements.getElement(i);
572 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000573 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000574 return (DT.getTypeDerivedFrom());
575 }
576
577 return Ty;
578}
579
Devang Patelc50078e2009-11-21 02:48:08 +0000580/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000581/// and generate the DWARF information necessary to find the actual variable
582/// given the extra address information encoded in the DIVariable, starting from
583/// the starting location. Add the DWARF information to the die.
584///
Devang Patelc50078e2009-11-21 02:48:08 +0000585void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000586 unsigned Attribute,
587 const MachineLocation &Location) {
588 const DIVariable &VD = DV->getVariable();
589 DIType Ty = VD.getType();
590
591 // Decode the original location, and use that as the start of the byref
592 // variable's location.
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000593 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000594 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000595 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000596
597 if (Location.isReg()) {
598 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000599 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000600 } else {
601 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000602 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
603 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000604 }
605 } else {
606 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000607 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000608 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000609 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
610 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000611 }
612
Devang Patelc50078e2009-11-21 02:48:08 +0000613 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000614 }
615
616 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
617 uint64_t Element = VD.getAddrElement(i);
618
619 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000620 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
621 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000622 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000623 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000624 } else llvm_unreachable("unknown DIFactory Opcode");
625 }
626
627 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000628 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000629}
630
Caroline Tice248d5572009-08-31 21:19:37 +0000631/* Byref variables, in Blocks, are declared by the programmer as "SomeType
632 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
633 gives the variable VarName either the struct, or a pointer to the struct, as
634 its type. This is necessary for various behind-the-scenes things the
635 compiler needs to do with by-reference variables in Blocks.
636
637 However, as far as the original *programmer* is concerned, the variable
638 should still have type 'SomeType', as originally declared.
639
Devang Patelc50078e2009-11-21 02:48:08 +0000640 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000641 struct to find the original type of the variable, which is then assigned to
642 the variable's Debug Information Entry as its real type. So far, so good.
643 However now the debugger will expect the variable VarName to have the type
644 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000645 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000646 pointers and struct to find the actual variable of type SomeType.
647
648 The following function does just that. We start by getting
649 the "normal" location for the variable. This will be the location
650 of either the struct __Block_byref_x_VarName or the pointer to the
651 struct __Block_byref_x_VarName.
652
653 The struct will look something like:
654
655 struct __Block_byref_x_VarName {
656 ... <various fields>
657 struct __Block_byref_x_VarName *forwarding;
658 ... <various other fields>
659 SomeType VarName;
660 ... <maybe more fields>
661 };
662
663 If we are given the struct directly (as our starting point) we
664 need to tell the debugger to:
665
666 1). Add the offset of the forwarding field.
667
Dan Gohmandf1a7ff2010-02-10 16:03:48 +0000668 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000669 struct to use (the real one may have been copied onto the heap).
670
671 3). Add the offset for the field VarName, to find the actual variable.
672
673 If we started with a pointer to the struct, then we need to
674 dereference that pointer first, before the other steps.
675 Translating this into DWARF ops, we will need to append the following
676 to the current location description for the variable:
677
678 DW_OP_deref -- optional, if we start with a pointer
679 DW_OP_plus_uconst <forward_fld_offset>
680 DW_OP_deref
681 DW_OP_plus_uconst <varName_fld_offset>
682
683 That is what this function does. */
684
Devang Patelc50078e2009-11-21 02:48:08 +0000685/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000686/// provided, and generate the DWARF information necessary to find the
687/// actual Block variable (navigating the Block struct) based on the
688/// starting location. Add the DWARF information to the die. For
689/// more information, read large comment just above here.
690///
Devang Patelc50078e2009-11-21 02:48:08 +0000691void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000692 unsigned Attribute,
693 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000694 const DIVariable &VD = DV->getVariable();
695 DIType Ty = VD.getType();
696 DIType TmpTy = Ty;
697 unsigned Tag = Ty.getTag();
698 bool isPointer = false;
699
Devang Patel7f75bbe2009-11-25 17:36:49 +0000700 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000701
702 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000703 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000704 TmpTy = DTy.getTypeDerivedFrom();
705 isPointer = true;
706 }
707
708 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
709
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000710 // Find the __forwarding field and the variable field in the __Block_byref
711 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000712 DIArray Fields = blockStruct.getTypeArray();
713 DIDescriptor varField = DIDescriptor();
714 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000715
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000716 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
717 DIDescriptor Element = Fields.getElement(i);
718 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000719 StringRef fieldName = DT.getName();
720 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000721 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000722 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000723 varField = Element;
724 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000725
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000726 // Get the offsets for the forwarding field and the variable field.
Chris Lattner5df82552010-03-31 06:06:37 +0000727 unsigned forwardingFieldOffset =
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000728 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
Chris Lattner5df82552010-03-31 06:06:37 +0000729 unsigned varFieldOffset =
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000730 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000731
Mike Stump2fd84e22009-09-24 23:21:26 +0000732 // Decode the original location, and use that as the start of the byref
733 // variable's location.
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000734 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000735 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000736 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000737
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000738 if (Location.isReg()) {
739 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000740 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000741 else {
742 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000743 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
744 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000745 }
746 } else {
747 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000748 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000749 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000750 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
751 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000752 }
Caroline Tice248d5572009-08-31 21:19:37 +0000753
Devang Patelc50078e2009-11-21 02:48:08 +0000754 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000755 }
Caroline Tice248d5572009-08-31 21:19:37 +0000756
Mike Stump2fd84e22009-09-24 23:21:26 +0000757 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000758 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000759 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000760 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000761
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000762 // Next add the offset for the '__forwarding' field:
763 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
764 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000765 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000766 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
767 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000768 }
Caroline Tice248d5572009-08-31 21:19:37 +0000769
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000770 // Now dereference the __forwarding field to get to the real __Block_byref
771 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000772 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000773
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000774 // Now that we've got the real __Block_byref... struct, add the offset
775 // for the variable's field to get to the location of the actual variable:
776 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000777 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000778 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
779 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000780 }
Caroline Tice248d5572009-08-31 21:19:37 +0000781
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000782 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000783 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000784}
785
Devang Patelc50078e2009-11-21 02:48:08 +0000786/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000787/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000788void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000789 const MachineLocation &Location) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000790 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000791 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000792 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000793
794 if (Location.isReg()) {
795 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000796 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000797 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000798 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
799 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000800 }
801 } else {
802 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000803 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000804 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000805 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
806 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000807 }
808
Devang Patelc50078e2009-11-21 02:48:08 +0000809 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000810 }
811
Devang Patelc50078e2009-11-21 02:48:08 +0000812 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000813}
814
Devang Patel7e625392010-04-28 01:03:09 +0000815/// addRegisterAddress - Add register location entry in variable DIE.
816bool DwarfDebug::addRegisterAddress(DIE *Die, DbgVariable *DV,
817 const MachineOperand &MO) {
818 assert (MO.isReg() && "Invalid machine operand!");
819 if (!MO.getReg())
820 return false;
821 MachineLocation Location;
822 Location.set(MO.getReg());
823 addAddress(Die, dwarf::DW_AT_location, Location);
824 if (MCSymbol *VS = DV->getDbgValueLabel())
825 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
826 return true;
827}
828
829/// addConstantValue - Add constant value entry in variable DIE.
830bool DwarfDebug::addConstantValue(DIE *Die, DbgVariable *DV,
831 const MachineOperand &MO) {
832 assert (MO.isImm() && "Invalid machine operand!");
833 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
834 unsigned Imm = MO.getImm();
835 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
836 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
837 if (MCSymbol *VS = DV->getDbgValueLabel())
838 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
839 return true;
840}
841
842/// addConstantFPValue - Add constant value entry in variable DIE.
843bool DwarfDebug::addConstantFPValue(DIE *Die, DbgVariable *DV,
844 const MachineOperand &MO) {
845 assert (MO.isFPImm() && "Invalid machine operand!");
846 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
847 APFloat FPImm = MO.getFPImm()->getValueAPF();
848
849 // Get the raw data form of the floating point.
850 const APInt FltVal = FPImm.bitcastToAPInt();
851 const char *FltPtr = (const char*)FltVal.getRawData();
852
853 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
854 bool LittleEndian = Asm->getTargetData().isLittleEndian();
855 int Incr = (LittleEndian ? 1 : -1);
856 int Start = (LittleEndian ? 0 : NumBytes - 1);
857 int Stop = (LittleEndian ? NumBytes : -1);
858
859 // Output the constant to DWARF one byte at a time.
860 for (; Start != Stop; Start += Incr)
861 addUInt(Block, 0, dwarf::DW_FORM_data1,
862 (unsigned char)0xFF & FltPtr[Start]);
863
864 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
865
866 if (MCSymbol *VS = DV->getDbgValueLabel())
867 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
868 VS);
869 return true;
870}
871
872
Devang Patel1a8f9a82009-12-10 19:14:49 +0000873/// addToContextOwner - Add Die into the list of its context owner's children.
874void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel9e592492010-03-08 20:52:55 +0000875 if (Context.isType()) {
Devang Patel1a8f9a82009-12-10 19:14:49 +0000876 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
877 ContextDIE->addChild(Die);
Devang Patel8287d662009-12-15 19:16:48 +0000878 } else if (Context.isNameSpace()) {
879 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
880 ContextDIE->addChild(Die);
Devang Patel1a8f9a82009-12-10 19:14:49 +0000881 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
882 ContextDIE->addChild(Die);
883 else
884 ModuleCU->addDie(Die);
885}
886
Devang Patel7f139c12009-12-10 18:05:33 +0000887/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
888/// given DIType.
889DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
890 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
891 if (TyDIE)
892 return TyDIE;
893
894 // Create new type.
895 TyDIE = new DIE(dwarf::DW_TAG_base_type);
896 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
897 if (Ty.isBasicType())
898 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
899 else if (Ty.isCompositeType())
900 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
901 else {
902 assert(Ty.isDerivedType() && "Unknown kind of DIType");
903 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
904 }
905
Devang Patel1a8f9a82009-12-10 19:14:49 +0000906 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel7f139c12009-12-10 18:05:33 +0000907 return TyDIE;
908}
909
Devang Patelc50078e2009-11-21 02:48:08 +0000910/// addType - Add a new type attribute to the specified entity.
Devang Patelfe0be132009-12-09 18:24:21 +0000911void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9e592492010-03-08 20:52:55 +0000912 if (!Ty.isValid())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000913 return;
914
915 // Check for pre-existence.
Devang Patelfe0be132009-12-09 18:24:21 +0000916 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000917 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000918 if (Entry) {
919 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000920 return;
921 }
922
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000923 // Construct type.
Devang Patel7f139c12009-12-10 18:05:33 +0000924 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000925
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000926 // Set up proxy.
927 Entry = createDIEEntry(Buffer);
928 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
929
Devang Patelc50078e2009-11-21 02:48:08 +0000930 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000931}
932
Devang Patelc50078e2009-11-21 02:48:08 +0000933/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patelfe0be132009-12-09 18:24:21 +0000934void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000935 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000936 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000937 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000938 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000939 BTy.getEncoding());
940
941 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000942 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000943 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000944 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000945 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000946}
947
Devang Patelc50078e2009-11-21 02:48:08 +0000948/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patelfe0be132009-12-09 18:24:21 +0000949void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000950 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000951 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000952 uint64_t Size = DTy.getSizeInBits() >> 3;
953 unsigned Tag = DTy.getTag();
954
955 // FIXME - Workaround for templates.
956 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
957
958 Buffer.setTag(Tag);
959
960 // Map to main type, void will not have a type.
961 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelfe0be132009-12-09 18:24:21 +0000962 addType(&Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000963
964 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000965 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000966 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000967
968 // Add size if non-zero (derived types might be zero-sized.)
969 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000970 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000971
972 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000973 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000974 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000975}
976
Devang Patelc50078e2009-11-21 02:48:08 +0000977/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000978void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000979 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000980 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000981
982 uint64_t Size = CTy.getSizeInBits() >> 3;
983 unsigned Tag = CTy.getTag();
984 Buffer.setTag(Tag);
985
986 switch (Tag) {
987 case dwarf::DW_TAG_vector_type:
988 case dwarf::DW_TAG_array_type:
Devang Patelfe0be132009-12-09 18:24:21 +0000989 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000990 break;
991 case dwarf::DW_TAG_enumeration_type: {
992 DIArray Elements = CTy.getTypeArray();
993
994 // Add enumerators to enumeration type.
995 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
996 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +0000997 DIDescriptor Enum(Elements.getElement(i).getNode());
998 if (Enum.isEnumerator()) {
999 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +00001000 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +00001001 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001002 }
1003 }
1004 break;
1005 case dwarf::DW_TAG_subroutine_type: {
1006 // Add return type.
1007 DIArray Elements = CTy.getTypeArray();
1008 DIDescriptor RTy = Elements.getElement(0);
Devang Patelfe0be132009-12-09 18:24:21 +00001009 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001010
1011 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001012 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001013
1014 // Add arguments.
1015 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1016 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1017 DIDescriptor Ty = Elements.getElement(i);
Devang Patelfe0be132009-12-09 18:24:21 +00001018 addType(Arg, DIType(Ty.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +00001019 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001020 }
1021 }
1022 break;
1023 case dwarf::DW_TAG_structure_type:
1024 case dwarf::DW_TAG_union_type:
1025 case dwarf::DW_TAG_class_type: {
1026 // Add elements to structure type.
1027 DIArray Elements = CTy.getTypeArray();
1028
1029 // A forward struct declared type may not have elements available.
Devang Patel9e592492010-03-08 20:52:55 +00001030 unsigned N = Elements.getNumElements();
1031 if (N == 0)
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001032 break;
1033
1034 // Add elements to structure type.
Devang Patel9e592492010-03-08 20:52:55 +00001035 for (unsigned i = 0; i < N; ++i) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001036 DIDescriptor Element = Elements.getElement(i);
1037 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001038 if (Element.isSubprogram())
Devang Patel814a12c2009-12-14 16:18:45 +00001039 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Devang Patel9e592492010-03-08 20:52:55 +00001040 else if (Element.isVariable()) {
Devang Patel423dfa02010-01-30 01:08:30 +00001041 DIVariable DV(Element.getNode());
1042 ElemDie = new DIE(dwarf::DW_TAG_variable);
1043 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1044 DV.getName());
1045 addType(ElemDie, DV.getType());
1046 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1047 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1048 addSourceLine(ElemDie, &DV);
Devang Patel9e592492010-03-08 20:52:55 +00001049 } else if (Element.isDerivedType())
Devang Patelfe0be132009-12-09 18:24:21 +00001050 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel9e592492010-03-08 20:52:55 +00001051 else
1052 continue;
Devang Patelc50078e2009-11-21 02:48:08 +00001053 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001054 }
1055
Devang Patel20b32102009-08-27 23:51:51 +00001056 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +00001057 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001058
1059 unsigned RLang = CTy.getRunTimeLang();
1060 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +00001061 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001062 dwarf::DW_FORM_data1, RLang);
Devang Patel5ae52402010-01-26 21:16:06 +00001063
1064 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel9e592492010-03-08 20:52:55 +00001065 if (DIDescriptor(ContainingType.getNode()).isCompositeType())
Devang Patel5ae52402010-01-26 21:16:06 +00001066 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1067 getOrCreateTypeDIE(DIType(ContainingType.getNode())));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001068 break;
1069 }
1070 default:
1071 break;
1072 }
1073
1074 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001075 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001076 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001077
Devang Pateldc73c372010-01-29 18:34:58 +00001078 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001079 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1080 // Add size if non-zero (derived types might be zero-sized.)
1081 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +00001082 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001083 else {
1084 // Add zero size if it is not a forward declaration.
1085 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001086 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001087 else
Devang Patelc50078e2009-11-21 02:48:08 +00001088 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001089 }
1090
1091 // Add source line info if available.
1092 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001093 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001094 }
1095}
1096
Devang Patelc50078e2009-11-21 02:48:08 +00001097/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1098void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001099 int64_t L = SR.getLo();
1100 int64_t H = SR.getHi();
1101 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1102
Devang Patelc50078e2009-11-21 02:48:08 +00001103 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +00001104 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +00001105 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +00001106 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001107
Devang Patelc50078e2009-11-21 02:48:08 +00001108 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001109}
1110
Devang Patelc50078e2009-11-21 02:48:08 +00001111/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +00001112void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001113 DICompositeType *CTy) {
1114 Buffer.setTag(dwarf::DW_TAG_array_type);
1115 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001116 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001117
1118 // Emit derived type.
Devang Patelfe0be132009-12-09 18:24:21 +00001119 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001120 DIArray Elements = CTy->getTypeArray();
1121
Devang Patel1233f912009-11-21 00:31:03 +00001122 // Get an anonymous type for index type.
Devang Patelfe0be132009-12-09 18:24:21 +00001123 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel1233f912009-11-21 00:31:03 +00001124 if (!IdxTy) {
1125 // Construct an anonymous type for index type.
1126 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +00001127 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1128 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +00001129 dwarf::DW_ATE_signed);
Devang Patelfe0be132009-12-09 18:24:21 +00001130 ModuleCU->addDie(IdxTy);
1131 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +00001132 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001133
1134 // Add subranges to array type.
1135 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1136 DIDescriptor Element = Elements.getElement(i);
1137 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001138 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001139 }
1140}
1141
Devang Patelc50078e2009-11-21 02:48:08 +00001142/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel9e592492010-03-08 20:52:55 +00001143DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001144 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel9e592492010-03-08 20:52:55 +00001145 StringRef Name = ETy.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001146 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel9e592492010-03-08 20:52:55 +00001147 int64_t Value = ETy.getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001148 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001149 return Enumerator;
1150}
1151
Devang Patel141e1c42010-01-05 01:46:14 +00001152/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1153/// printer to not emit usual symbol prefix before the symbol name is used then
1154/// return linkage name after skipping this special LLVM prefix.
1155static StringRef getRealLinkageName(StringRef LinkageName) {
1156 char One = '\1';
1157 if (LinkageName.startswith(StringRef(&One, 1)))
1158 return LinkageName.substr(1);
1159 return LinkageName;
1160}
1161
Devang Patelc50078e2009-11-21 02:48:08 +00001162/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patelfe0be132009-12-09 18:24:21 +00001163DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001164 // If the global variable was optmized out then no need to create debug info
1165 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001166 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001167 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001168
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001169 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001170 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001171 GV.getDisplayName());
1172
Devang Patel7f75bbe2009-11-25 17:36:49 +00001173 StringRef LinkageName = GV.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001174 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001175 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001176 getRealLinkageName(LinkageName));
1177
Devang Patelfe0be132009-12-09 18:24:21 +00001178 addType(GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001179 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001180 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1181 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001182
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001183 return GVDie;
1184}
1185
Devang Patelc50078e2009-11-21 02:48:08 +00001186/// createMemberDIE - Create new member DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001187DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001188 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001189 StringRef Name = DT.getName();
1190 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001191 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001192
Devang Patelfe0be132009-12-09 18:24:21 +00001193 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001194
Devang Patelc50078e2009-11-21 02:48:08 +00001195 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001196
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001197 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001198 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001199
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001200 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001201 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001202
1203 if (Size != FieldSize) {
1204 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001205 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1206 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001207
1208 uint64_t Offset = DT.getOffsetInBits();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001209 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1210 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer631e3e32010-01-07 17:50:57 +00001211 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001212 Offset -= FieldOffset;
1213
1214 // Maybe we need to work from the other end.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001215 if (Asm->getTargetData().isLittleEndian())
1216 Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001217 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001218
Devang Patel7d9fe582009-11-04 22:06:12 +00001219 // Here WD_AT_data_member_location points to the anonymous
1220 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001221 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001222
1223 } else
1224 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001225 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001226
Devang Patel84aba942010-02-03 20:08:48 +00001227 if (DT.getTag() == dwarf::DW_TAG_inheritance
1228 && DT.isVirtual()) {
1229
1230 // For C++, virtual base classes are not at fixed offset. Use following
1231 // expression to extract appropriate offset from vtable.
1232 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1233
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001234 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel84aba942010-02-03 20:08:48 +00001235 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1236 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1237 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1238 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1239 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1240 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1241 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1242
1243 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1244 VBaseLocationDie);
1245 } else
1246 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001247
1248 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001249 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001250 dwarf::DW_ACCESS_protected);
1251 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001252 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001253 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-12-03 19:11:07 +00001254 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1255 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1256 dwarf::DW_ACCESS_public);
1257 if (DT.isVirtual())
1258 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1259 dwarf::DW_VIRTUALITY_virtual);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001260 return MemberDie;
1261}
1262
Devang Patel814a12c2009-12-14 16:18:45 +00001263/// createSubprogramDIE - Create new DIE using SP.
1264DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1265 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1266 if (SPDie)
1267 return SPDie;
1268
1269 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel36cd9f82010-03-02 17:58:15 +00001270 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel0ab194f2010-03-02 01:26:20 +00001271 if (!SP.getName().empty())
1272 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001273
Devang Patel7f75bbe2009-11-25 17:36:49 +00001274 StringRef LinkageName = SP.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001275 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001276 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001277 getRealLinkageName(LinkageName));
1278
Devang Patelc50078e2009-11-21 02:48:08 +00001279 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001280
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001281 // Add prototyped tag, if C or ObjC.
1282 unsigned Lang = SP.getCompileUnit().getLanguage();
1283 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1284 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001285 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001286
1287 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001288 DICompositeType SPTy = SP.getType();
1289 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001290 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001291
Devang Patel9e592492010-03-08 20:52:55 +00001292 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelfe0be132009-12-09 18:24:21 +00001293 addType(SPDie, SPTy);
Devang Patelc1df8792009-12-03 01:25:38 +00001294 else
Devang Patelfe0be132009-12-09 18:24:21 +00001295 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patelc1df8792009-12-03 01:25:38 +00001296
Devang Patel188c85d2009-12-03 19:11:07 +00001297 unsigned VK = SP.getVirtuality();
1298 if (VK) {
1299 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001300 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel188c85d2009-12-03 19:11:07 +00001301 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1302 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1303 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel7d707f92010-01-19 06:19:05 +00001304 ContainingTypeMap.insert(std::make_pair(SPDie,
1305 SP.getContainingType().getNode()));
Devang Patel188c85d2009-12-03 19:11:07 +00001306 }
1307
Devang Patel814a12c2009-12-14 16:18:45 +00001308 if (MakeDecl || !SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001309 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001310
1311 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-12-03 01:25:38 +00001312 // be handled while processing variables.
1313 DICompositeType SPTy = SP.getType();
1314 DIArray Args = SPTy.getTypeArray();
1315 unsigned SPTag = SPTy.getTag();
1316
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001317 if (SPTag == dwarf::DW_TAG_subroutine_type)
1318 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1319 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc5dc7252010-02-06 01:02:37 +00001320 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1321 addType(Arg, ATy);
1322 if (ATy.isArtificial())
1323 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc50078e2009-11-21 02:48:08 +00001324 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001325 }
1326 }
1327
Devang Patelbf7d00a2010-02-03 19:57:19 +00001328 if (SP.isArtificial())
1329 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1330
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001331 if (!SP.isLocalToUnit())
1332 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel91474172010-04-19 19:14:02 +00001333
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001334 if (SP.isOptimized())
1335 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1336
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001337 // DW_TAG_inlined_subroutine may refer to this DIE.
1338 ModuleCU->insertDIE(SP.getNode(), SPDie);
1339
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001340 return SPDie;
1341}
1342
Devang Patel90a0fe32009-11-10 23:06:00 +00001343DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
Chris Lattner8143ce82010-03-31 05:36:29 +00001344 assert(N && "Invalid Scope encoding!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001345
1346 DbgScope *AScope = AbstractScopes.lookup(N);
1347 if (AScope)
1348 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001349
Devang Patel90a0fe32009-11-10 23:06:00 +00001350 DbgScope *Parent = NULL;
1351
1352 DIDescriptor Scope(N);
1353 if (Scope.isLexicalBlock()) {
1354 DILexicalBlock DB(N);
1355 DIDescriptor ParentDesc = DB.getContext();
Devang Patel9e592492010-03-08 20:52:55 +00001356 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001357 }
1358
1359 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1360
1361 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001362 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001363 AScope->setAbstractScope();
1364 AbstractScopes[N] = AScope;
1365 if (DIDescriptor(N).isSubprogram())
1366 AbstractScopesList.push_back(AScope);
1367 return AScope;
1368}
Devang Patel6a260102009-10-01 20:31:14 +00001369
Devang Patel75c10622010-04-06 23:53:48 +00001370/// isSubprogramContext - Return true if Context is either a subprogram
1371/// or another context nested inside a subprogram.
Dan Gohman738422f2010-04-15 17:08:50 +00001372static bool isSubprogramContext(MDNode *Context) {
Devang Patel75c10622010-04-06 23:53:48 +00001373 if (!Context)
1374 return false;
1375 DIDescriptor D(Context);
1376 if (D.isSubprogram())
1377 return true;
1378 if (D.isType())
1379 return isSubprogramContext(DIType(Context).getContext().getNode());
1380 return false;
1381}
1382
Jim Grosbach652b7432009-11-21 23:12:12 +00001383/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001384/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1385/// If there are global variables in this scope then create and insert
1386/// DIEs for these variables.
1387DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001388 DIE *SPDie = ModuleCU->getDIE(SPNode);
1389 assert(SPDie && "Unable to find subprogram DIE!");
1390 DISubprogram SP(SPNode);
Chris Lattnerdd21da82010-03-13 07:26:18 +00001391
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001392 // There is not any need to generate specification DIE for a function
1393 // defined at compile unit level. If a function is defined inside another
1394 // function then gdb prefers the definition at top level and but does not
1395 // expect specification DIE in parent function. So avoid creating
1396 // specification DIE for a function defined inside a function.
1397 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel75c10622010-04-06 23:53:48 +00001398 !SP.getContext().isFile() &&
1399 !isSubprogramContext(SP.getContext().getNode())) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001400 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1401
1402 // Add arguments.
1403 DICompositeType SPTy = SP.getType();
1404 DIArray Args = SPTy.getTypeArray();
1405 unsigned SPTag = SPTy.getTag();
1406 if (SPTag == dwarf::DW_TAG_subroutine_type)
1407 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1408 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1409 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1410 addType(Arg, ATy);
1411 if (ATy.isArtificial())
1412 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1413 SPDie->addChild(Arg);
1414 }
1415 DIE *SPDeclDie = SPDie;
1416 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1417 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1418 SPDeclDie);
1419 ModuleCU->addDie(SPDie);
1420 }
1421
1422 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1423 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1424 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1425 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1426 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1427 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1428 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelc5dc7252010-02-06 01:02:37 +00001429
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001430 return SPDie;
Devang Patel90a0fe32009-11-10 23:06:00 +00001431}
1432
Jim Grosbach652b7432009-11-21 23:12:12 +00001433/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001434/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1435DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patelce7bf012010-04-27 19:46:33 +00001436
1437 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1438 if (Scope->isAbstractScope())
1439 return ScopeDIE;
1440
1441 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1442 if (Ranges.empty())
1443 return 0;
1444
1445 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1446 if (Ranges.size() > 1) {
1447 // .debug_range section has not been laid out yet. Emit offset in
1448 // .debug_range as a uint, size 4, for now. emitDIE will handle
1449 // DW_AT_ranges appropriately.
1450 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1451 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1452 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1453 RE = Ranges.end(); RI != RE; ++RI) {
1454 DebugRangeSymbols.push_back(LabelsBeforeInsn.lookup(RI->first));
1455 DebugRangeSymbols.push_back(LabelsAfterInsn.lookup(RI->second));
1456 }
1457 DebugRangeSymbols.push_back(NULL);
1458 DebugRangeSymbols.push_back(NULL);
1459 return ScopeDIE;
1460 }
1461
1462 MCSymbol *Start = LabelsBeforeInsn.lookup(RI->first);
1463 MCSymbol *End = LabelsAfterInsn.lookup(RI->second);
1464
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001465 if (Start == 0 || End == 0) return 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00001466
Chris Lattner855e8f52010-03-09 01:58:53 +00001467 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1468 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattner5e423432010-03-09 01:51:43 +00001469
Devang Patelce7bf012010-04-27 19:46:33 +00001470 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1471 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel90a0fe32009-11-10 23:06:00 +00001472
1473 return ScopeDIE;
1474}
1475
Devang Patelc50078e2009-11-21 02:48:08 +00001476/// constructInlinedScopeDIE - This scope represents inlined body of
1477/// a function. Construct DIE to represent this concrete inlined copy
1478/// of the function.
1479DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patelce7bf012010-04-27 19:46:33 +00001480
1481 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1482 assert (Ranges.empty() == false
1483 && "DbgScope does not have instruction markers!");
1484
1485 // FIXME : .debug_inlined section specification does not clearly state how
1486 // to emit inlined scope that is split into multiple instruction ranges.
1487 // For now, use first instruction range and emit low_pc/high_pc pair and
1488 // corresponding .debug_inlined section entry for this pair.
1489 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1490 MCSymbol *StartLabel = LabelsBeforeInsn.lookup(RI->first);
1491 MCSymbol *EndLabel = LabelsAfterInsn.lookup(RI->second);
1492
1493 if (StartLabel == 0 || EndLabel == 0) {
1494 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1495 return 0;
1496 }
Chris Lattner855e8f52010-03-09 01:58:53 +00001497 assert(StartLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001498 "Invalid starting label for an inlined scope!");
Chris Lattner855e8f52010-03-09 01:58:53 +00001499 assert(EndLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001500 "Invalid end label for an inlined scope!");
Devang Patelce7bf012010-04-27 19:46:33 +00001501
Devang Patel9e592492010-03-08 20:52:55 +00001502 if (!Scope->getScopeNode())
Devang Patelbe149872010-03-08 19:20:38 +00001503 return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001504 DIScope DS(Scope->getScopeNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001505 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1506
1507 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001508 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Chris Lattner8143ce82010-03-31 05:36:29 +00001509 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001510 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001511 dwarf::DW_FORM_ref4, OriginDIE);
1512
Chris Lattneread58652010-03-09 00:31:02 +00001513 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattner855e8f52010-03-09 01:58:53 +00001514 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel90a0fe32009-11-10 23:06:00 +00001515
1516 InlinedSubprogramDIEs.insert(OriginDIE);
1517
1518 // Track the start label for this inlined function.
Devang Patel7d707f92010-01-19 06:19:05 +00001519 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel90a0fe32009-11-10 23:06:00 +00001520 I = InlineInfo.find(InlinedSP.getNode());
1521
1522 if (I == InlineInfo.end()) {
Chris Lattneread58652010-03-09 00:31:02 +00001523 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartLabel,
Jim Grosbachb23f2422009-11-22 19:20:36 +00001524 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001525 InlinedSPNodes.push_back(InlinedSP.getNode());
1526 } else
Chris Lattneread58652010-03-09 00:31:02 +00001527 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001528
Devang Patel90a0fe32009-11-10 23:06:00 +00001529 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001530 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1531 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001532
1533 return ScopeDIE;
1534}
1535
Devang Patelc50078e2009-11-21 02:48:08 +00001536
1537/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001538DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001539 // Get the descriptor.
1540 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001541 StringRef Name = VD.getName();
1542 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001543 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001544
1545 // Translate tag to proper Dwarf tag. The result variable is dropped for
1546 // now.
1547 unsigned Tag;
1548 switch (VD.getTag()) {
1549 case dwarf::DW_TAG_return_variable:
1550 return NULL;
1551 case dwarf::DW_TAG_arg_variable:
1552 Tag = dwarf::DW_TAG_formal_parameter;
1553 break;
1554 case dwarf::DW_TAG_auto_variable: // fall thru
1555 default:
1556 Tag = dwarf::DW_TAG_variable;
1557 break;
1558 }
1559
1560 // Define variable debug information entry.
1561 DIE *VariableDie = new DIE(Tag);
1562
1563
1564 DIE *AbsDIE = NULL;
1565 if (DbgVariable *AV = DV->getAbstractVariable())
1566 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001567
Devang Patel90a0fe32009-11-10 23:06:00 +00001568 if (AbsDIE) {
1569 DIScope DS(Scope->getScopeNode());
1570 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001571 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001572 (void) OriginSPDIE;
Chris Lattner8143ce82010-03-31 05:36:29 +00001573 assert(OriginSPDIE && "Unable to find Origin DIE for the SP!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001574 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
Chris Lattner8143ce82010-03-31 05:36:29 +00001575 assert(AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001576 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001577 dwarf::DW_FORM_ref4, AbsDIE);
1578 }
1579 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001580 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1581 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001582
1583 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001584 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001585 // addresses instead.
1586 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001587 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001588 else
Devang Patelfe0be132009-12-09 18:24:21 +00001589 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001590 }
1591
1592 // Add variable address.
1593 if (!Scope->isAbstractScope()) {
Devang Patel14da02f2010-03-15 18:33:46 +00001594 // Check if variable is described by DBG_VALUE instruction.
Devang Patel7e625392010-04-28 01:03:09 +00001595 if (const MachineInstr *DVInsn = DV->getDbgValue()) {
1596 bool updated = false;
1597 // FIXME : Handle getNumOperands != 3
1598 if (DVInsn->getNumOperands() == 3) {
1599 if (DVInsn->getOperand(0).isReg())
1600 updated = addRegisterAddress(VariableDie, DV, DVInsn->getOperand(0));
1601 else if (DVInsn->getOperand(0).isImm())
1602 updated = addConstantValue(VariableDie, DV, DVInsn->getOperand(0));
1603 else if (DVInsn->getOperand(0).isFPImm())
1604 updated = addConstantFPValue(VariableDie, DV, DVInsn->getOperand(0));
Devang Patel7c3efb12010-04-28 01:39:28 +00001605 } else {
1606 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1607 if (Location.getReg()) {
1608 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1609 if (MCSymbol *VS = DV->getDbgValueLabel())
1610 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1611 VS);
1612 updated = true;
1613 }
Devang Patel7e625392010-04-28 01:03:09 +00001614 }
1615 if (!updated) {
1616 // If variableDie is not updated then DBG_VALUE instruction does not
1617 // have valid variable info.
1618 delete VariableDie;
1619 return NULL;
1620 }
1621 }
1622 else {
Devang Patel14da02f2010-03-15 18:33:46 +00001623 MachineLocation Location;
1624 unsigned FrameReg;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001625 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1626 int Offset = RI->getFrameIndexReference(*Asm->MF, DV->getFrameIndex(),
Chris Lattner5df82552010-03-31 06:06:37 +00001627 FrameReg);
Devang Patel14da02f2010-03-15 18:33:46 +00001628 Location.set(FrameReg, Offset);
1629
1630 if (VD.hasComplexAddress())
1631 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1632 else if (VD.isBlockByrefVariable())
1633 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1634 else
1635 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1636 }
Devang Patel90a0fe32009-11-10 23:06:00 +00001637 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001638
1639 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1640 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001641 DV->setDIE(VariableDie);
1642 return VariableDie;
1643
1644}
Devang Patelc50078e2009-11-21 02:48:08 +00001645
Devang Patelec13b4f2009-11-24 01:14:22 +00001646void DwarfDebug::addPubTypes(DISubprogram SP) {
1647 DICompositeType SPTy = SP.getType();
1648 unsigned SPTag = SPTy.getTag();
1649 if (SPTag != dwarf::DW_TAG_subroutine_type)
1650 return;
1651
1652 DIArray Args = SPTy.getTypeArray();
Devang Patelec13b4f2009-11-24 01:14:22 +00001653 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1654 DIType ATy(Args.getElement(i).getNode());
Devang Patel9e592492010-03-08 20:52:55 +00001655 if (!ATy.isValid())
Devang Patelec13b4f2009-11-24 01:14:22 +00001656 continue;
1657 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel21171c22010-04-13 20:35:04 +00001658 if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty()
1659 && !CATy.isForwardDecl()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001660 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1661 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1662 }
1663 }
1664}
1665
Devang Patelc50078e2009-11-21 02:48:08 +00001666/// constructScopeDIE - Construct a DIE for this scope.
1667DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel9e592492010-03-08 20:52:55 +00001668 if (!Scope || !Scope->getScopeNode())
1669 return NULL;
1670
1671 DIScope DS(Scope->getScopeNode());
1672 DIE *ScopeDIE = NULL;
1673 if (Scope->getInlinedAt())
1674 ScopeDIE = constructInlinedScopeDIE(Scope);
1675 else if (DS.isSubprogram()) {
1676 if (Scope->isAbstractScope())
1677 ScopeDIE = ModuleCU->getDIE(DS.getNode());
1678 else
1679 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
1680 }
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001681 else
Devang Patel9e592492010-03-08 20:52:55 +00001682 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001683 if (!ScopeDIE) return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001684
Devang Patel90a0fe32009-11-10 23:06:00 +00001685 // Add variables to scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001686 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel90a0fe32009-11-10 23:06:00 +00001687 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelfe0be132009-12-09 18:24:21 +00001688 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +00001689 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001690 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001691 }
1692
1693 // Add nested scopes.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001694 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel90a0fe32009-11-10 23:06:00 +00001695 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1696 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001697 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001698 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001699 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001700 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001701
1702 if (DS.isSubprogram())
1703 addPubTypes(DISubprogram(DS.getNode()));
1704
1705 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001706}
1707
Bill Wendlingf5839192009-05-20 23:19:06 +00001708/// GetOrCreateSourceID - Look up the source id with the given directory and
1709/// source file names. If none currently exists, create a new id and insert it
1710/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1711/// maps as well.
Chris Lattner5df82552010-03-31 06:06:37 +00001712unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf5839192009-05-20 23:19:06 +00001713 unsigned DId;
1714 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1715 if (DI != DirectoryIdMap.end()) {
1716 DId = DI->getValue();
1717 } else {
1718 DId = DirectoryNames.size() + 1;
1719 DirectoryIdMap[DirName] = DId;
1720 DirectoryNames.push_back(DirName);
1721 }
1722
1723 unsigned FId;
1724 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1725 if (FI != SourceFileIdMap.end()) {
1726 FId = FI->getValue();
1727 } else {
1728 FId = SourceFileNames.size() + 1;
1729 SourceFileIdMap[FileName] = FId;
1730 SourceFileNames.push_back(FileName);
1731 }
1732
1733 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1734 SourceIdMap.find(std::make_pair(DId, FId));
1735 if (SI != SourceIdMap.end())
1736 return SI->second;
1737
1738 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1739 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1740 SourceIds.push_back(std::make_pair(DId, FId));
1741
1742 return SrcId;
1743}
1744
Devang Patel8287d662009-12-15 19:16:48 +00001745/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1746DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1747 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1748 if (NDie)
1749 return NDie;
1750 NDie = new DIE(dwarf::DW_TAG_namespace);
1751 ModuleCU->insertDIE(NS.getNode(), NDie);
1752 if (!NS.getName().empty())
1753 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1754 addSourceLine(NDie, &NS);
1755 addToContextOwner(NDie, NS.getContext());
1756 return NDie;
1757}
1758
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001759void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001760 DICompileUnit DIUnit(N);
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001761 // Use first compile unit marked as isMain as the compile unit for this
1762 // module.
1763 if (ModuleCU || !DIUnit.isMain())
1764 return;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001765 StringRef FN = DIUnit.getFilename();
1766 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001767 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001768
1769 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001770 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001771 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001772 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001773 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001774 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel325691a2010-04-26 22:54:28 +00001775 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1776 // simplifies debug range entries.
1777 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_data4, 0);
Devang Patel7ac39832010-03-22 23:11:36 +00001778 // DW_AT_stmt_list is a offset of line number information for this
1779 // compile unit in debug_line section. It is always zero when only one
1780 // compile unit is emitted in one object file.
1781 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf5839192009-05-20 23:19:06 +00001782
Devang Patel7f75bbe2009-11-25 17:36:49 +00001783 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001784 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001785 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001786 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001787
Devang Patel7f75bbe2009-11-25 17:36:49 +00001788 StringRef Flags = DIUnit.getFlags();
1789 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001790 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001791
1792 unsigned RVer = DIUnit.getRunTimeVersion();
1793 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001794 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001795 dwarf::DW_FORM_data1, RVer);
1796
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001797 assert(!ModuleCU &&
1798 "ModuleCU assigned since the top of constructCompileUnit");
1799 ModuleCU = new CompileUnit(ID, Die);
Bill Wendlingf5839192009-05-20 23:19:06 +00001800}
1801
Devang Patelc50078e2009-11-21 02:48:08 +00001802void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001803 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001804
Devang Patel0c03f062009-09-04 23:59:07 +00001805 // If debug information is malformed then ignore it.
1806 if (DI_GV.Verify() == false)
1807 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001808
1809 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001810 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001811 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001812
Devang Patelfe0be132009-12-09 18:24:21 +00001813 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001814 if (!VariableDie)
1815 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001816
Bill Wendlingf5839192009-05-20 23:19:06 +00001817 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001818 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001819
1820 // Add to context owner.
Devang Pateldcc0dce2010-01-15 01:12:22 +00001821 DIDescriptor GVContext = DI_GV.getContext();
1822 // Do not create specification DIE if context is either compile unit
1823 // or a subprogram.
Chris Lattner5df82552010-03-31 06:06:37 +00001824 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel75c10622010-04-06 23:53:48 +00001825 !GVContext.isFile() &&
1826 !isSubprogramContext(GVContext.getNode())) {
Devang Patel8287d662009-12-15 19:16:48 +00001827 // Create specification DIE.
1828 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1829 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1830 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001831 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001832 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001833 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001834 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001835 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patela0a98582010-02-09 01:58:33 +00001836 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel8287d662009-12-15 19:16:48 +00001837 ModuleCU->addDie(VariableSpecDIE);
1838 } else {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001839 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001840 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001841 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001842 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001843 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1844 }
Devang Pateldcc0dce2010-01-15 01:12:22 +00001845 addToContextOwner(VariableDie, GVContext);
Devang Patel1a8f9a82009-12-10 19:14:49 +00001846
Bill Wendlingf5839192009-05-20 23:19:06 +00001847 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001848 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001849
1850 DIType GTy = DI_GV.getType();
Devang Patel21171c22010-04-13 20:35:04 +00001851 if (GTy.isCompositeType() && !GTy.getName().empty()
1852 && !GTy.isForwardDecl()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001853 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
Chris Lattner8143ce82010-03-31 05:36:29 +00001854 assert(Entry && "Missing global type!");
Devang Patelec13b4f2009-11-24 01:14:22 +00001855 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1856 }
Devang Patel166f8432009-06-26 01:49:18 +00001857 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001858}
1859
Devang Patelc50078e2009-11-21 02:48:08 +00001860void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001861 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001862
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001863 // Check for pre-existence.
1864 if (ModuleCU->getDIE(N))
1865 return;
1866
Bill Wendlingf5839192009-05-20 23:19:06 +00001867 if (!SP.isDefinition())
1868 // This is a method declaration which will be handled while constructing
1869 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001870 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001871
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001872 DIE *SubprogramDie = createSubprogramDIE(SP);
1873
1874 // Add to map.
1875 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001876
1877 // Add to context owner.
Devang Patel8287d662009-12-15 19:16:48 +00001878 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patelde2d3682009-12-08 23:21:45 +00001879
Bill Wendlingf5839192009-05-20 23:19:06 +00001880 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001881 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001882
Devang Patel166f8432009-06-26 01:49:18 +00001883 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001884}
1885
Devang Patelc50078e2009-11-21 02:48:08 +00001886/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001887/// content. Create global DIEs and emit initial debug info sections.
1888/// This is inovked by the target AsmPrinter.
Chris Lattner75e113c2010-04-04 07:48:20 +00001889void DwarfDebug::beginModule(Module *M) {
Devang Patelce7bf012010-04-27 19:46:33 +00001890 if (DisableDebugInfoPrinting)
1891 return;
1892
Devang Patelfda766d2009-07-30 18:56:46 +00001893 DebugInfoFinder DbgFinder;
1894 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001895
Chris Lattnera52b6172010-04-05 02:19:28 +00001896 bool HasDebugInfo = false;
1897
1898 // Scan all the compile-units to see if there are any marked as the main unit.
1899 // if not, we do not generate debug info.
1900 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1901 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1902 if (DICompileUnit(*I).isMain()) {
1903 HasDebugInfo = true;
1904 break;
1905 }
1906 }
1907
1908 if (!HasDebugInfo) return;
1909
1910 // Tell MMI that we have debug info.
1911 MMI->setDebugInfoAvailability(true);
1912
Chris Lattner706afc02010-04-04 23:17:54 +00001913 // Emit initial sections.
Chris Lattnera52b6172010-04-05 02:19:28 +00001914 EmitSectionLabels();
Chris Lattner706afc02010-04-04 23:17:54 +00001915
Bill Wendlingf5839192009-05-20 23:19:06 +00001916 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001917 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1918 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001919 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001920
Devang Patel90a0fe32009-11-10 23:06:00 +00001921 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001922 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1923 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001924 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001925
Devang Patel1a8f9a82009-12-10 19:14:49 +00001926 // Create DIEs for each global variable.
1927 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1928 E = DbgFinder.global_variable_end(); I != E; ++I)
1929 constructGlobalVariableDIE(*I);
1930
Bill Wendlingf5839192009-05-20 23:19:06 +00001931 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001932 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001933
1934 // Print out .file directives to specify files for .loc directives. These are
1935 // printed out early so that they precede any .loc directives.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001936 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001937 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1938 // Remember source id starts at 1.
1939 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerad653482010-01-22 22:09:00 +00001940 // FIXME: don't use sys::path for this! This should not depend on the
1941 // host.
Bill Wendlingf5839192009-05-20 23:19:06 +00001942 sys::Path FullPath(getSourceDirectoryName(Id.first));
1943 bool AppendOk =
1944 FullPath.appendComponent(getSourceFileName(Id.second));
1945 assert(AppendOk && "Could not append filename to directory!");
1946 AppendOk = false;
Chris Lattner59be4ea2010-01-25 18:58:59 +00001947 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001948 }
1949 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001950}
1951
Devang Patelc50078e2009-11-21 02:48:08 +00001952/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001953///
Devang Patelc50078e2009-11-21 02:48:08 +00001954void DwarfDebug::endModule() {
Bill Wendlingce3c6252010-04-07 09:28:04 +00001955 if (!ModuleCU) return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001956
Devang Patel90a0fe32009-11-10 23:06:00 +00001957 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1958 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1959 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1960 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001961 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001962 }
1963
Devang Patel7d707f92010-01-19 06:19:05 +00001964 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel188c85d2009-12-03 19:11:07 +00001965 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1966 DIE *SPDie = CI->first;
1967 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1968 if (!N) continue;
1969 DIE *NDie = ModuleCU->getDIE(N);
1970 if (!NDie) continue;
1971 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel188c85d2009-12-03 19:11:07 +00001972 }
1973
Bill Wendlingf5839192009-05-20 23:19:06 +00001974 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001975 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00001976 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner73266f92009-08-19 05:49:37 +00001977 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00001978 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf5839192009-05-20 23:19:06 +00001979
1980 // End text sections.
1981 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001982 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerb93558d2010-04-04 19:25:43 +00001983 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf5839192009-05-20 23:19:06 +00001984 }
1985
1986 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001987 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001988
1989 // Emit function debug frame information
1990 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1991 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001992 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001993
1994 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001995 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001996
1997 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001998 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001999
2000 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00002001 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00002002
2003 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00002004 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00002005
2006 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00002007 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00002008
Devang Patelec13b4f2009-11-24 01:14:22 +00002009 // Emit info into a debug pubtypes section.
2010 emitDebugPubTypes();
2011
Bill Wendlingf5839192009-05-20 23:19:06 +00002012 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00002013 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00002014
2015 // Emit info into a debug aranges section.
2016 EmitDebugARanges();
2017
2018 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00002019 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00002020
2021 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00002022 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002023
2024 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00002025 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002026
Chris Lattner0f093f82010-03-13 02:17:42 +00002027 // Emit info into a debug str section.
2028 emitDebugStr();
2029
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00002030 delete ModuleCU;
2031 ModuleCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf5839192009-05-20 23:19:06 +00002032}
2033
Devang Patel90a0fe32009-11-10 23:06:00 +00002034/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00002035DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2036 unsigned FrameIdx,
Chris Lattnerb9692a72010-04-02 19:42:39 +00002037 DebugLoc ScopeLoc) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002038
2039 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
2040 if (AbsDbgVariable)
2041 return AbsDbgVariable;
2042
Chris Lattnerb9692a72010-04-02 19:42:39 +00002043 LLVMContext &Ctx = Var.getNode()->getContext();
2044 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel90a0fe32009-11-10 23:06:00 +00002045 if (!Scope)
2046 return NULL;
2047
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002048 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
2049 NULL /* No more-abstract variable*/);
Devang Patelc50078e2009-11-21 02:48:08 +00002050 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00002051 AbstractVariables[Var.getNode()] = AbsDbgVariable;
2052 return AbsDbgVariable;
2053}
2054
Devang Patel14da02f2010-03-15 18:33:46 +00002055/// findAbstractVariable - Find abstract variable, if any, associated with Var.
2056/// FIXME : Refactor findAbstractVariable.
2057DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2058 const MachineInstr *MI,
Chris Lattnerb9692a72010-04-02 19:42:39 +00002059 DebugLoc ScopeLoc) {
Devang Patel14da02f2010-03-15 18:33:46 +00002060
2061 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
2062 if (AbsDbgVariable)
2063 return AbsDbgVariable;
2064
Chris Lattnerb9692a72010-04-02 19:42:39 +00002065 LLVMContext &Ctx = Var.getNode()->getContext();
2066 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel14da02f2010-03-15 18:33:46 +00002067 if (!Scope)
2068 return NULL;
2069
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002070 AbsDbgVariable = new DbgVariable(Var, MI,
Devang Patel14da02f2010-03-15 18:33:46 +00002071 NULL /* No more-abstract variable*/);
2072 Scope->addVariable(AbsDbgVariable);
2073 AbstractVariables[Var.getNode()] = AbsDbgVariable;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002074 DbgValueStartMap[MI] = AbsDbgVariable;
Devang Patel14da02f2010-03-15 18:33:46 +00002075 return AbsDbgVariable;
2076}
2077
Devang Patelc50078e2009-11-21 02:48:08 +00002078/// collectVariableInfo - Populate DbgScope entries with variables' info.
2079void DwarfDebug::collectVariableInfo() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002080 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002081
Devang Patel84139992009-10-06 01:26:37 +00002082 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2083 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2084 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel0a0ea052010-01-22 22:52:10 +00002085 MDNode *Var = VI->first;
Devang Patel90a0fe32009-11-10 23:06:00 +00002086 if (!Var) continue;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002087 DIVariable DV(Var);
2088 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002089
Chris Lattnerb9692a72010-04-02 19:42:39 +00002090 DbgScope *Scope = 0;
2091 if (MDNode *IA = VP.second.getInlinedAt(Ctx))
2092 Scope = ConcreteScopes.lookup(IA);
2093 if (Scope == 0)
2094 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2095
Devang Patelbad42262009-11-10 23:20:04 +00002096 // If variable scope is not found then skip this variable.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002097 if (Scope == 0)
Devang Patelbad42262009-11-10 23:20:04 +00002098 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00002099
Chris Lattnerb9692a72010-04-02 19:42:39 +00002100 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second);
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002101 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Devang Patelc50078e2009-11-21 02:48:08 +00002102 Scope->addVariable(RegVar);
Devang Patel84139992009-10-06 01:26:37 +00002103 }
Devang Patel14da02f2010-03-15 18:33:46 +00002104
2105 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002106 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel14da02f2010-03-15 18:33:46 +00002107 I != E; ++I) {
2108 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2109 II != IE; ++II) {
2110 const MachineInstr *MInsn = II;
Chris Lattner202f10b2010-03-31 05:39:57 +00002111 if (!MInsn->isDebugValue())
Devang Patel14da02f2010-03-15 18:33:46 +00002112 continue;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002113
Devang Patel7f78e212010-04-27 20:54:45 +00002114 // Ignore Undef values.
Devang Patelbecba812010-04-27 22:04:41 +00002115 if (MInsn->getOperand(0).isReg() && !MInsn->getOperand(0).getReg())
Devang Patel7f78e212010-04-27 20:54:45 +00002116 continue;
2117
Dan Gohman8db62492010-04-17 16:43:55 +00002118 DIVariable DV(
2119 const_cast<MDNode *>(MInsn->getOperand(MInsn->getNumOperands() - 1)
2120 .getMetadata()));
Devang Patel14da02f2010-03-15 18:33:46 +00002121 if (DV.getTag() == dwarf::DW_TAG_arg_variable) {
2122 // FIXME Handle inlined subroutine arguments.
2123 DbgVariable *ArgVar = new DbgVariable(DV, MInsn, NULL);
2124 CurrentFnDbgScope->addVariable(ArgVar);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002125 DbgValueStartMap[MInsn] = ArgVar;
Devang Patel14da02f2010-03-15 18:33:46 +00002126 continue;
2127 }
2128
2129 DebugLoc DL = MInsn->getDebugLoc();
2130 if (DL.isUnknown()) continue;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002131 DbgScope *Scope = 0;
2132 if (MDNode *IA = DL.getInlinedAt(Ctx))
2133 Scope = ConcreteScopes.lookup(IA);
2134 if (Scope == 0)
2135 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2136
Devang Patel14da02f2010-03-15 18:33:46 +00002137 // If variable scope is not found then skip this variable.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002138 if (Scope == 0)
Devang Patel14da02f2010-03-15 18:33:46 +00002139 continue;
2140
Chris Lattnerb9692a72010-04-02 19:42:39 +00002141 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
Devang Patel14da02f2010-03-15 18:33:46 +00002142 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002143 DbgValueStartMap[MInsn] = RegVar;
Devang Patel14da02f2010-03-15 18:33:46 +00002144 Scope->addVariable(RegVar);
2145 }
2146 }
Devang Patel84139992009-10-06 01:26:37 +00002147}
2148
Devang Patelabc2b352010-03-29 17:20:31 +00002149/// beginScope - Process beginning of a scope.
2150void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patelabc2b352010-03-29 17:20:31 +00002151 // Check location.
2152 DebugLoc DL = MI->getDebugLoc();
Dan Gohman5d0dda52010-05-05 23:41:32 +00002153 if (DL.isUnknown()) {
2154 // This instruction has no debug location. If the preceding instruction
2155 // did, emit debug location information to indicate that the debug
2156 // location is now unknown.
2157 MCSymbol *Label = NULL;
2158 if (DL == PrevInstLoc)
2159 Label = PrevLabel;
2160 else {
2161 Label = recordSourceLine(DL.getLine(), DL.getCol(), 0);
2162 PrevInstLoc = DL;
2163 PrevLabel = Label;
2164 }
Devang Patelabc2b352010-03-29 17:20:31 +00002165 return;
Dan Gohman5d0dda52010-05-05 23:41:32 +00002166 }
Devang Patelabc2b352010-03-29 17:20:31 +00002167
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002168 MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerb9692a72010-04-02 19:42:39 +00002169
2170 // FIXME: Should only verify each scope once!
2171 if (!DIScope(Scope).Verify())
Devang Patelabc2b352010-03-29 17:20:31 +00002172 return;
Devang Patelabc2b352010-03-29 17:20:31 +00002173
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002174 // DBG_VALUE instruction establishes new value.
Chris Lattner202f10b2010-03-31 05:39:57 +00002175 if (MI->isDebugValue()) {
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002176 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2177 = DbgValueStartMap.find(MI);
2178 if (DI != DbgValueStartMap.end()) {
Devang Patelce7bf012010-04-27 19:46:33 +00002179 MCSymbol *Label = NULL;
2180 if (DL == PrevInstLoc)
2181 Label = PrevLabel;
2182 else {
2183 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2184 PrevInstLoc = DL;
2185 PrevLabel = Label;
2186 }
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002187 DI->second->setDbgValueLabel(Label);
2188 }
Devang Patel98ca2372010-03-30 18:07:00 +00002189 return;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002190 }
2191
Devang Patelabc2b352010-03-29 17:20:31 +00002192 // Emit a label to indicate location change. This is used for line
Devang Patelce7bf012010-04-27 19:46:33 +00002193 // table even if this instruction does not start a new scope.
2194 MCSymbol *Label = NULL;
2195 if (DL == PrevInstLoc)
2196 Label = PrevLabel;
2197 else {
2198 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2199 PrevInstLoc = DL;
2200 PrevLabel = Label;
2201 }
Devang Patelabc2b352010-03-29 17:20:31 +00002202
Devang Patel6f878382010-04-08 16:50:29 +00002203 // If this instruction begins a scope then note down corresponding label.
2204 if (InsnsBeginScopeSet.count(MI) != 0)
Devang Patelce7bf012010-04-27 19:46:33 +00002205 LabelsBeforeInsn[MI] = Label;
Devang Patel393a46d2009-10-06 01:50:42 +00002206}
2207
Devang Patelc50078e2009-11-21 02:48:08 +00002208/// endScope - Process end of a scope.
2209void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel6f878382010-04-08 16:50:29 +00002210 if (InsnsEndScopeSet.count(MI) != 0) {
2211 // Emit a label if this instruction ends a scope.
2212 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2213 Asm->OutStreamer.EmitLabel(Label);
Devang Patelce7bf012010-04-27 19:46:33 +00002214 LabelsAfterInsn[MI] = Label;
Devang Patel6f878382010-04-08 16:50:29 +00002215 }
Devang Patel90a0fe32009-11-10 23:06:00 +00002216}
2217
Devang Patelce7bf012010-04-27 19:46:33 +00002218/// getOrCreateDbgScope - Create DbgScope for the scope.
2219DbgScope *DwarfDebug::getOrCreateDbgScope(MDNode *Scope, MDNode *InlinedAt) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002220 if (!InlinedAt) {
2221 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2222 if (WScope)
Devang Patelce7bf012010-04-27 19:46:33 +00002223 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002224 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2225 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patelce7bf012010-04-27 19:46:33 +00002226 if (DIDescriptor(Scope).isLexicalBlock()) {
2227 DbgScope *Parent =
2228 getOrCreateDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
2229 WScope->setParent(Parent);
2230 Parent->addScope(WScope);
2231 }
2232
2233 if (!WScope->getParent()) {
2234 StringRef SPName = DISubprogram(Scope).getLinkageName();
2235 if (SPName == Asm->MF->getFunction()->getName())
2236 CurrentFnDbgScope = WScope;
2237 }
2238
2239 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002240 }
2241
2242 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2243 if (WScope)
Devang Patelce7bf012010-04-27 19:46:33 +00002244 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002245
2246 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2247 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2248 DILocation DL(InlinedAt);
Devang Patelce7bf012010-04-27 19:46:33 +00002249 DbgScope *Parent =
2250 getOrCreateDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
2251 WScope->setParent(Parent);
2252 Parent->addScope(WScope);
2253
2254 ConcreteScopes[InlinedAt] = WScope;
2255 getOrCreateAbstractScope(Scope);
2256
2257 return WScope;
Devang Patel393a46d2009-10-06 01:50:42 +00002258}
2259
Devang Patelce7bf012010-04-27 19:46:33 +00002260/// hasValidLocation - Return true if debug location entry attached with
2261/// machine instruction encodes valid location info.
2262static bool hasValidLocation(LLVMContext &Ctx,
2263 const MachineInstr *MInsn,
2264 MDNode *&Scope, MDNode *&InlinedAt) {
2265 if (MInsn->isDebugValue())
2266 return false;
2267 DebugLoc DL = MInsn->getDebugLoc();
2268 if (DL.isUnknown()) return false;
2269
2270 MDNode *S = DL.getScope(Ctx);
2271
2272 // There is no need to create another DIE for compile unit. For all
2273 // other scopes, create one DbgScope now. This will be translated
2274 // into a scope DIE at the end.
2275 if (DIScope(S).isCompileUnit()) return false;
2276
2277 Scope = S;
2278 InlinedAt = DL.getInlinedAt(Ctx);
2279 return true;
2280}
2281
2282/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2283/// hierarchy.
2284static void calculateDominanceGraph(DbgScope *Scope) {
2285 assert (Scope && "Unable to calculate scop edominance graph!");
2286 SmallVector<DbgScope *, 4> WorkStack;
2287 WorkStack.push_back(Scope);
2288 unsigned Counter = 0;
2289 while (!WorkStack.empty()) {
2290 DbgScope *WS = WorkStack.back();
2291 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2292 bool visitedChildren = false;
2293 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2294 SE = Children.end(); SI != SE; ++SI) {
2295 DbgScope *ChildScope = *SI;
2296 if (!ChildScope->getDFSOut()) {
2297 WorkStack.push_back(ChildScope);
2298 visitedChildren = true;
2299 ChildScope->setDFSIn(++Counter);
2300 break;
2301 }
2302 }
2303 if (!visitedChildren) {
2304 WorkStack.pop_back();
2305 WS->setDFSOut(++Counter);
2306 }
2307 }
2308}
2309
2310/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2311static
2312void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2313 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2314{
2315#ifndef NDEBUG
2316 unsigned PrevDFSIn = 0;
2317 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2318 I != E; ++I) {
2319 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2320 II != IE; ++II) {
2321 const MachineInstr *MInsn = II;
2322 MDNode *Scope = NULL;
2323 MDNode *InlinedAt = NULL;
2324
2325 // Check if instruction has valid location information.
2326 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2327 dbgs() << " [ ";
2328 if (InlinedAt)
2329 dbgs() << "*";
2330 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2331 MI2ScopeMap.find(MInsn);
2332 if (DI != MI2ScopeMap.end()) {
2333 DbgScope *S = DI->second;
2334 dbgs() << S->getDFSIn();
2335 PrevDFSIn = S->getDFSIn();
2336 } else
2337 dbgs() << PrevDFSIn;
2338 } else
2339 dbgs() << " [ x" << PrevDFSIn;
2340 dbgs() << " ]";
2341 MInsn->dump();
2342 }
2343 dbgs() << "\n";
2344 }
2345#endif
2346}
Devang Patelc50078e2009-11-21 02:48:08 +00002347/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner202f10b2010-03-31 05:39:57 +00002348/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner69a76972010-01-26 23:18:02 +00002349bool DwarfDebug::extractScopeInformation() {
Devang Patel6a260102009-10-01 20:31:14 +00002350 // If scope information was extracted using .dbg intrinsics then there is not
2351 // any need to extract these information by scanning each instruction.
2352 if (!DbgScopeMap.empty())
2353 return false;
2354
Dan Gohman30573dd2010-04-23 01:18:53 +00002355 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelce7bf012010-04-27 19:46:33 +00002356 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2357 SmallVector<DbgRange, 4> MIRanges;
2358 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
2359 MDNode *PrevScope = NULL;
2360 MDNode *PrevInlinedAt = NULL;
2361 const MachineInstr *RangeBeginMI = NULL;
2362 const MachineInstr *PrevMI = NULL;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002363 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel6a260102009-10-01 20:31:14 +00002364 I != E; ++I) {
2365 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2366 II != IE; ++II) {
2367 const MachineInstr *MInsn = II;
Devang Patelce7bf012010-04-27 19:46:33 +00002368 MDNode *Scope = NULL;
2369 MDNode *InlinedAt = NULL;
2370
2371 // Check if instruction has valid location information.
2372 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2373 PrevMI = MInsn;
2374 continue;
2375 }
Chris Lattnerb9692a72010-04-02 19:42:39 +00002376
Devang Patelce7bf012010-04-27 19:46:33 +00002377 // If scope has not changed then skip this instruction.
2378 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2379 PrevMI = MInsn;
2380 continue;
2381 }
2382
2383 if (RangeBeginMI) {
2384 // If we have alread seen a beginning of a instruction range and
2385 // current instruction scope does not match scope of first instruction
2386 // in this range then create a new instruction range.
2387 DbgRange R(RangeBeginMI, PrevMI);
2388 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
2389 MIRanges.push_back(R);
2390 }
2391
2392 // This is a beginning of a new instruction range.
2393 RangeBeginMI = MInsn;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002394
Devang Patelce7bf012010-04-27 19:46:33 +00002395 // Reset previous markers.
2396 PrevMI = MInsn;
2397 PrevScope = Scope;
2398 PrevInlinedAt = InlinedAt;
Devang Patel90a0fe32009-11-10 23:06:00 +00002399 }
2400 }
2401
Devang Patelce7bf012010-04-27 19:46:33 +00002402 // Create last instruction range.
2403 if (RangeBeginMI && PrevMI && PrevScope) {
2404 DbgRange R(RangeBeginMI, PrevMI);
2405 MIRanges.push_back(R);
2406 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel6a260102009-10-01 20:31:14 +00002407 }
Devang Patelce7bf012010-04-27 19:46:33 +00002408
Devang Patel98e77302010-01-04 20:44:00 +00002409 if (!CurrentFnDbgScope)
2410 return false;
2411
Devang Patelce7bf012010-04-27 19:46:33 +00002412 calculateDominanceGraph(CurrentFnDbgScope);
2413 if (PrintDbgScope)
2414 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2415
2416 // Find ranges of instructions covered by each DbgScope;
2417 DbgScope *PrevDbgScope = NULL;
2418 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2419 RE = MIRanges.end(); RI != RE; ++RI) {
2420 const DbgRange &R = *RI;
2421 DbgScope *S = MI2ScopeMap.lookup(R.first);
2422 assert (S && "Lost DbgScope for a machine instruction!");
2423 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2424 PrevDbgScope->closeInsnRange(S);
2425 S->openInsnRange(R.first);
2426 S->extendInsnRange(R.second);
2427 PrevDbgScope = S;
2428 }
2429
2430 if (PrevDbgScope)
2431 PrevDbgScope->closeInsnRange();
Devang Patel6a260102009-10-01 20:31:14 +00002432
Devang Patelf63b2262010-04-08 18:43:56 +00002433 identifyScopeMarkers();
Devang Patel0d8f3b72010-04-08 15:37:09 +00002434
2435 return !DbgScopeMap.empty();
2436}
2437
Devang Patelce7bf012010-04-27 19:46:33 +00002438/// identifyScopeMarkers() -
2439/// Each DbgScope has first instruction and last instruction to mark beginning
2440/// and end of a scope respectively. Create an inverse map that list scopes
2441/// starts (and ends) with an instruction. One instruction may start (or end)
2442/// multiple scopes. Ignore scopes that are not reachable.
Devang Patelf63b2262010-04-08 18:43:56 +00002443void DwarfDebug::identifyScopeMarkers() {
Devang Patelfd311df2010-01-20 02:05:23 +00002444 SmallVector<DbgScope *, 4> WorkList;
2445 WorkList.push_back(CurrentFnDbgScope);
2446 while (!WorkList.empty()) {
Chris Lattner202f10b2010-03-31 05:39:57 +00002447 DbgScope *S = WorkList.pop_back_val();
Devang Patelce7bf012010-04-27 19:46:33 +00002448
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002449 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patelfd311df2010-01-20 02:05:23 +00002450 if (!Children.empty())
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002451 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patelfd311df2010-01-20 02:05:23 +00002452 SE = Children.end(); SI != SE; ++SI)
2453 WorkList.push_back(*SI);
2454
Devang Patel90a0fe32009-11-10 23:06:00 +00002455 if (S->isAbstractScope())
2456 continue;
Devang Patelce7bf012010-04-27 19:46:33 +00002457
2458 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2459 if (Ranges.empty())
2460 continue;
2461 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2462 RE = Ranges.end(); RI != RE; ++RI) {
2463 assert(RI->first && "DbgRange does not have first instruction!");
2464 assert(RI->second && "DbgRange does not have second instruction!");
2465 InsnsBeginScopeSet.insert(RI->first);
2466 InsnsEndScopeSet.insert(RI->second);
2467 }
Devang Patel6a260102009-10-01 20:31:14 +00002468 }
Devang Patel6a260102009-10-01 20:31:14 +00002469}
2470
Dan Gohmand0e9b672010-04-20 00:37:27 +00002471/// FindFirstDebugLoc - Find the first debug location in the function. This
2472/// is intended to be an approximation for the source position of the
2473/// beginning of the function.
2474static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2475 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2476 I != E; ++I)
2477 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2478 MBBI != MBBE; ++MBBI) {
2479 DebugLoc DL = MBBI->getDebugLoc();
2480 if (!DL.isUnknown())
2481 return DL;
2482 }
2483 return DebugLoc();
2484}
2485
Devang Patelc50078e2009-11-21 02:48:08 +00002486/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002487/// emitted immediately after the function entry point.
Chris Lattner69a76972010-01-26 23:18:02 +00002488void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner2cb53302010-04-05 03:52:55 +00002489 if (!MMI->hasDebugInfo()) return;
Bill Wendlingce3c6252010-04-07 09:28:04 +00002490 if (!extractScopeInformation()) return;
Chris Lattnerb49c9f82010-03-29 20:38:20 +00002491
Devang Patelc50078e2009-11-21 02:48:08 +00002492 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002493
Devang Patelce7bf012010-04-27 19:46:33 +00002494 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2495 Asm->getFunctionNumber());
Bill Wendlingf5839192009-05-20 23:19:06 +00002496 // Assumes in correct section after the entry point.
Devang Patelce7bf012010-04-27 19:46:33 +00002497 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf5839192009-05-20 23:19:06 +00002498
2499 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2500 // function.
Dan Gohmand0e9b672010-04-20 00:37:27 +00002501 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerb9692a72010-04-02 19:42:39 +00002502 if (FDL.isUnknown()) return;
2503
2504 MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
2505
2506 DISubprogram SP = getDISubprogram(Scope);
2507 unsigned Line, Col;
2508 if (SP.Verify()) {
2509 Line = SP.getLineNumber();
2510 Col = 0;
2511 } else {
2512 Line = FDL.getLine();
2513 Col = FDL.getCol();
Bill Wendlingf5839192009-05-20 23:19:06 +00002514 }
Chris Lattnerb9692a72010-04-02 19:42:39 +00002515
2516 recordSourceLine(Line, Col, Scope);
Bill Wendlingf5839192009-05-20 23:19:06 +00002517}
2518
Devang Patelc50078e2009-11-21 02:48:08 +00002519/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002520///
Chris Lattner69a76972010-01-26 23:18:02 +00002521void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingce3c6252010-04-07 09:28:04 +00002522 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002523
Devang Patel98e77302010-01-04 20:44:00 +00002524 if (CurrentFnDbgScope) {
2525 // Define end label for subprogram.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002526 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2527 Asm->getFunctionNumber()));
Devang Patel98e77302010-01-04 20:44:00 +00002528
2529 // Get function line info.
2530 if (!Lines.empty()) {
2531 // Get section line info.
2532 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2533 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2534 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2535 // Append the function info to section info.
2536 SectionLineInfos.insert(SectionLineInfos.end(),
2537 Lines.begin(), Lines.end());
2538 }
2539
2540 // Construct abstract scopes.
2541 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2542 AE = AbstractScopesList.end(); AI != AE; ++AI)
2543 constructScopeDIE(*AI);
2544
Devang Patelab4b3ce2010-05-04 06:15:30 +00002545 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel98e77302010-01-04 20:44:00 +00002546
Devang Patelab4b3ce2010-05-04 06:15:30 +00002547 if (!DisableFramePointerElim(*MF))
2548 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2549 dwarf::DW_FORM_flag, 1);
2550
2551
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002552 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel98e77302010-01-04 20:44:00 +00002553 MMI->getFrameMoves()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002554 }
2555
Bill Wendlingf5839192009-05-20 23:19:06 +00002556 // Clear debug info
Devang Patel387e9c12010-01-19 01:26:02 +00002557 CurrentFnDbgScope = NULL;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002558 DeleteContainerSeconds(DbgScopeMap);
Devang Patelfd0ebd52010-04-09 16:04:20 +00002559 InsnsBeginScopeSet.clear();
2560 InsnsEndScopeSet.clear();
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002561 DbgValueStartMap.clear();
Devang Patel387e9c12010-01-19 01:26:02 +00002562 ConcreteScopes.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002563 DeleteContainerSeconds(AbstractScopes);
Devang Patel387e9c12010-01-19 01:26:02 +00002564 AbstractScopesList.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002565 AbstractVariables.clear();
Devang Patelce7bf012010-04-27 19:46:33 +00002566 LabelsBeforeInsn.clear();
2567 LabelsAfterInsn.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002568 Lines.clear();
Devang Patel146c6f72010-04-16 23:33:45 +00002569 PrevLabel = NULL;
Bill Wendlingf5839192009-05-20 23:19:06 +00002570}
2571
Chris Lattner597b0fc2010-03-09 04:54:43 +00002572/// recordSourceLine - Register a source line with debug info. Returns the
2573/// unique label that was emitted and which provides correspondence to
2574/// the source line list.
2575MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, MDNode *S) {
Devang Patel7f75bbe2009-11-25 17:36:49 +00002576 StringRef Dir;
2577 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002578
Dan Gohman5d0dda52010-05-05 23:41:32 +00002579 unsigned Src = 1;
2580 if (S) {
2581 DIDescriptor Scope(S);
Devang Patel946d0ae2009-10-05 18:03:19 +00002582
Dan Gohman5d0dda52010-05-05 23:41:32 +00002583 if (Scope.isCompileUnit()) {
2584 DICompileUnit CU(S);
2585 Dir = CU.getDirectory();
2586 Fn = CU.getFilename();
2587 } else if (Scope.isSubprogram()) {
2588 DISubprogram SP(S);
2589 Dir = SP.getDirectory();
2590 Fn = SP.getFilename();
2591 } else if (Scope.isLexicalBlock()) {
2592 DILexicalBlock DB(S);
2593 Dir = DB.getDirectory();
2594 Fn = DB.getFilename();
2595 } else
2596 assert(0 && "Unexpected scope info");
2597
2598 Src = GetOrCreateSourceID(Dir, Fn);
2599 }
2600
Chris Lattner54e56f22010-03-14 08:36:50 +00002601 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002602 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf5839192009-05-20 23:19:06 +00002603
Chris Lattner597b0fc2010-03-09 04:54:43 +00002604 Asm->OutStreamer.EmitLabel(Label);
2605 return Label;
Bill Wendlingf5839192009-05-20 23:19:06 +00002606}
2607
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002608//===----------------------------------------------------------------------===//
2609// Emit Methods
2610//===----------------------------------------------------------------------===//
2611
Devang Patelc50078e2009-11-21 02:48:08 +00002612/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002613///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002614unsigned
2615DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002616 // Get the children.
2617 const std::vector<DIE *> &Children = Die->getChildren();
2618
2619 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002620 if (!Last && !Children.empty())
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00002621 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling55fccda2009-05-20 23:21:38 +00002622
2623 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002624 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002625
2626 // Get the abbreviation for this DIE.
2627 unsigned AbbrevNumber = Die->getAbbrevNumber();
2628 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2629
2630 // Set DIE offset
2631 Die->setOffset(Offset);
2632
2633 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002634 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002635
2636 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2637 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2638
2639 // Size the DIE attribute values.
2640 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2641 // Size attribute value.
Chris Lattner352c8e22010-04-05 00:18:22 +00002642 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling55fccda2009-05-20 23:21:38 +00002643
2644 // Size the DIE children if any.
2645 if (!Children.empty()) {
2646 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2647 "Children flag not set");
2648
2649 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002650 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002651
2652 // End of children marker.
2653 Offset += sizeof(int8_t);
2654 }
2655
2656 Die->setSize(Offset - Die->getOffset());
2657 return Offset;
2658}
2659
Devang Patelc50078e2009-11-21 02:48:08 +00002660/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002661///
Devang Patelc50078e2009-11-21 02:48:08 +00002662void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002663 // Compute size of compile unit header.
2664 static unsigned Offset =
2665 sizeof(int32_t) + // Length of Compilation Unit Info
2666 sizeof(int16_t) + // DWARF version number
2667 sizeof(int32_t) + // Offset Into Abbrev. Section
2668 sizeof(int8_t); // Pointer Size (in bytes)
2669
Devang Patelc50078e2009-11-21 02:48:08 +00002670 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002671}
2672
Chris Lattner733c69d2010-04-04 23:02:02 +00002673/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2674/// temporary label to it if SymbolStem is specified.
Chris Lattner66143d22010-04-04 22:59:04 +00002675static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner733c69d2010-04-04 23:02:02 +00002676 const char *SymbolStem = 0) {
Chris Lattner66143d22010-04-04 22:59:04 +00002677 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner733c69d2010-04-04 23:02:02 +00002678 if (!SymbolStem) return 0;
2679
Chris Lattner66143d22010-04-04 22:59:04 +00002680 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2681 Asm->OutStreamer.EmitLabel(TmpSym);
2682 return TmpSym;
2683}
2684
2685/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2686/// the start of each one.
Chris Lattnerd91fd8c2010-04-04 22:33:59 +00002687void DwarfDebug::EmitSectionLabels() {
Chris Lattner73266f92009-08-19 05:49:37 +00002688 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002689
Bill Wendling55fccda2009-05-20 23:21:38 +00002690 // Dwarf sections base addresses.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002691 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner66143d22010-04-04 22:59:04 +00002692 DwarfFrameSectionSym =
2693 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2694 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002695
Chris Lattner66143d22010-04-04 22:59:04 +00002696 DwarfInfoSectionSym =
2697 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2698 DwarfAbbrevSectionSym =
2699 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner733c69d2010-04-04 23:02:02 +00002700 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002701
2702 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner733c69d2010-04-04 23:02:02 +00002703 EmitSectionSym(Asm, MacroInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002704
Chris Lattner733c69d2010-04-04 23:02:02 +00002705 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2706 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2707 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2708 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002709 DwarfStrSectionSym =
2710 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel146c6f72010-04-16 23:33:45 +00002711 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2712 "debug_range");
Bill Wendling55fccda2009-05-20 23:21:38 +00002713
Chris Lattner66143d22010-04-04 22:59:04 +00002714 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner094932e2010-04-04 23:10:38 +00002715 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002716}
2717
Devang Patelc50078e2009-11-21 02:48:08 +00002718/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002719///
Devang Patelc50078e2009-11-21 02:48:08 +00002720void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002721 // Get the abbreviation for this DIE.
2722 unsigned AbbrevNumber = Die->getAbbrevNumber();
2723 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2724
Bill Wendling55fccda2009-05-20 23:21:38 +00002725 // Emit the code (index) for the abbreviation.
Chris Lattner97c69b72010-04-04 18:52:31 +00002726 if (Asm->isVerbose())
Chris Lattnerbcc79432010-01-22 23:18:42 +00002727 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2728 Twine::utohexstr(Die->getOffset()) + ":0x" +
2729 Twine::utohexstr(Die->getSize()) + " " +
2730 dwarf::TagString(Abbrev->getTag()));
Chris Lattner26be1c12010-04-04 19:09:29 +00002731 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002732
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002733 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling55fccda2009-05-20 23:21:38 +00002734 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2735
2736 // Emit the DIE attribute values.
2737 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2738 unsigned Attr = AbbrevData[i].getAttribute();
2739 unsigned Form = AbbrevData[i].getForm();
2740 assert(Form && "Too many attributes for DIE (check abbreviation)");
2741
Chris Lattner97c69b72010-04-04 18:52:31 +00002742 if (Asm->isVerbose())
Chris Lattner91e06b42010-01-24 18:54:17 +00002743 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2744
Bill Wendling55fccda2009-05-20 23:21:38 +00002745 switch (Attr) {
2746 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002747 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002748 break;
2749 case dwarf::DW_AT_abstract_origin: {
2750 DIEEntry *E = cast<DIEEntry>(Values[i]);
2751 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002752 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002753 Asm->EmitInt32(Addr);
2754 break;
2755 }
Devang Patel146c6f72010-04-16 23:33:45 +00002756 case dwarf::DW_AT_ranges: {
2757 // DW_AT_range Value encodes offset in debug_range section.
2758 DIEInteger *V = cast<DIEInteger>(Values[i]);
2759 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2760 V->getValue(),
2761 DwarfDebugRangeSectionSym,
2762 4);
2763 break;
2764 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002765 default:
2766 // Emit an attribute using the defined form.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002767 Values[i]->EmitValue(Asm, Form);
Bill Wendling55fccda2009-05-20 23:21:38 +00002768 break;
2769 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002770 }
2771
2772 // Emit the DIE children if any.
2773 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2774 const std::vector<DIE *> &Children = Die->getChildren();
2775
2776 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002777 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002778
Chris Lattner97c69b72010-04-04 18:52:31 +00002779 if (Asm->isVerbose())
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002780 Asm->OutStreamer.AddComment("End Of Children Mark");
2781 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002782 }
2783}
2784
Devang Patelfe0be132009-12-09 18:24:21 +00002785/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002786///
Devang Patelfe0be132009-12-09 18:24:21 +00002787void DwarfDebug::emitDebugInfo() {
2788 // Start debug info section.
2789 Asm->OutStreamer.SwitchSection(
2790 Asm->getObjFileLowering().getDwarfInfoSection());
2791 DIE *Die = ModuleCU->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002792
2793 // Emit the compile units header.
Chris Lattnerb93558d2010-04-04 19:25:43 +00002794 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2795 ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002796
2797 // Emit size of content not including length itself
2798 unsigned ContentSize = Die->getSize() +
2799 sizeof(int16_t) + // DWARF version number
2800 sizeof(int32_t) + // Offset Into Abbrev. Section
2801 sizeof(int8_t) + // Pointer Size (in bytes)
2802 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2803
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002804 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2805 Asm->EmitInt32(ContentSize);
2806 Asm->OutStreamer.AddComment("DWARF version number");
2807 Asm->EmitInt16(dwarf::DWARF_VERSION);
2808 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Chris Lattnerc06b4742010-04-04 23:25:33 +00002809 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2810 DwarfAbbrevSectionSym);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002811 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002812 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00002813
Devang Patelc50078e2009-11-21 02:48:08 +00002814 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002815 // FIXME - extra padding for gdb bug.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002816 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2817 Asm->EmitInt8(0);
2818 Asm->EmitInt8(0);
2819 Asm->EmitInt8(0);
2820 Asm->EmitInt8(0);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002821 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002822}
2823
Devang Patelc50078e2009-11-21 02:48:08 +00002824/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002825///
Devang Patelc50078e2009-11-21 02:48:08 +00002826void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002827 // Check to see if it is worth the effort.
2828 if (!Abbreviations.empty()) {
2829 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002830 Asm->OutStreamer.SwitchSection(
2831 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002832
Chris Lattnerb93558d2010-04-04 19:25:43 +00002833 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002834
2835 // For each abbrevation.
2836 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2837 // Get abbreviation data
2838 const DIEAbbrev *Abbrev = Abbreviations[i];
2839
2840 // Emit the abbrevations code (base 1 index.)
Chris Lattner26be1c12010-04-04 19:09:29 +00002841 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling55fccda2009-05-20 23:21:38 +00002842
2843 // Emit the abbreviations data.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002844 Abbrev->Emit(Asm);
Bill Wendling55fccda2009-05-20 23:21:38 +00002845 }
2846
2847 // Mark end of abbreviations.
Chris Lattner26be1c12010-04-04 19:09:29 +00002848 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002849
Chris Lattnerb93558d2010-04-04 19:25:43 +00002850 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002851 }
2852}
2853
Devang Patelc50078e2009-11-21 02:48:08 +00002854/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002855/// the line matrix.
2856///
Devang Patelc50078e2009-11-21 02:48:08 +00002857void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002858 // Define last address of section.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002859 Asm->OutStreamer.AddComment("Extended Op");
2860 Asm->EmitInt8(0);
2861
2862 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002863 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002864 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2865 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2866
2867 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnereb159d62010-03-10 01:17:49 +00002868
Chris Lattnerb93558d2010-04-04 19:25:43 +00002869 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002870 Asm->getTargetData().getPointerSize(),
2871 0/*AddrSpace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00002872
2873 // Mark end of matrix.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002874 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2875 Asm->EmitInt8(0);
Chris Lattnerad653482010-01-22 22:09:00 +00002876 Asm->EmitInt8(1);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002877 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002878}
2879
Devang Patelc50078e2009-11-21 02:48:08 +00002880/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002881///
Devang Patelc50078e2009-11-21 02:48:08 +00002882void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002883 // If the target is using .loc/.file, the assembler will be emitting the
2884 // .debug_line table automatically.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002885 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002886 return;
2887
2888 // Minimum line delta, thus ranging from -10..(255-10).
2889 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2890 // Maximum line delta, thus ranging from -10..(255-10).
2891 const int MaxLineDelta = 255 + MinLineDelta;
2892
2893 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002894 Asm->OutStreamer.SwitchSection(
2895 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002896
2897 // Construct the section header.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002898 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002899 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2900 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002901 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002902
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002903 Asm->OutStreamer.AddComment("DWARF version number");
2904 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00002905
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002906 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002907 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2908 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002909 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002910
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002911 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2912 Asm->EmitInt8(1);
2913 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2914 Asm->EmitInt8(1);
2915 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2916 Asm->EmitInt8(MinLineDelta);
2917 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
2918 Asm->EmitInt8(MaxLineDelta);
2919 Asm->OutStreamer.AddComment("Special Opcode Base");
2920 Asm->EmitInt8(-MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00002921
2922 // Line number standard opcode encodings argument count
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002923 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
2924 Asm->EmitInt8(0);
2925 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
2926 Asm->EmitInt8(1);
2927 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
2928 Asm->EmitInt8(1);
2929 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
2930 Asm->EmitInt8(1);
2931 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
2932 Asm->EmitInt8(1);
2933 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
2934 Asm->EmitInt8(0);
2935 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
2936 Asm->EmitInt8(0);
2937 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
2938 Asm->EmitInt8(0);
2939 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
2940 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002941
2942 // Emit directories.
2943 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnercc564642010-01-23 03:11:46 +00002944 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner97c69b72010-04-04 18:52:31 +00002945 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattnercc564642010-01-23 03:11:46 +00002946 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002947 }
2948
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002949 Asm->OutStreamer.AddComment("End of directories");
2950 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002951
2952 // Emit files.
2953 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2954 // Remember source id starts at 1.
2955 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnercc564642010-01-23 03:11:46 +00002956 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner97c69b72010-04-04 18:52:31 +00002957 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattnercc564642010-01-23 03:11:46 +00002958 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2959
Chris Lattner26be1c12010-04-04 19:09:29 +00002960 Asm->EmitULEB128(Id.first, "Directory #");
2961 Asm->EmitULEB128(0, "Mod date");
2962 Asm->EmitULEB128(0, "File size");
Bill Wendling55fccda2009-05-20 23:21:38 +00002963 }
2964
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002965 Asm->OutStreamer.AddComment("End of files");
2966 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002967
Chris Lattnerb93558d2010-04-04 19:25:43 +00002968 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002969
2970 // A sequence for each text section.
2971 unsigned SecSrcLinesSize = SectionSourceLines.size();
2972
2973 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2974 // Isolate current sections line info.
2975 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2976
Bill Wendling55fccda2009-05-20 23:21:38 +00002977 // Dwarf assumes we start with first line of first source file.
2978 unsigned Source = 1;
2979 unsigned Line = 1;
2980
2981 // Construct rows of the address, source, line, column matrix.
2982 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2983 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002984 MCSymbol *Label = LineInfo.getLabel();
Chris Lattner31ae74d2010-03-14 02:20:58 +00002985 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling55fccda2009-05-20 23:21:38 +00002986
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002987 if (Asm->isVerbose()) {
Chris Lattneree3b40f2010-03-10 01:04:13 +00002988 std::pair<unsigned, unsigned> SrcID =
Bill Wendling55fccda2009-05-20 23:21:38 +00002989 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattneree3b40f2010-03-10 01:04:13 +00002990 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner48fb0b12010-03-10 02:29:31 +00002991 "/" +
2992 Twine(getSourceFileName(SrcID.second)) +
Chris Lattneree3b40f2010-03-10 01:04:13 +00002993 ":" + Twine(LineInfo.getLine()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002994 }
2995
2996 // Define the line address.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002997 Asm->OutStreamer.AddComment("Extended Op");
2998 Asm->EmitInt8(0);
2999 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003000 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003001
3002 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3003 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3004
3005 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003006 Asm->OutStreamer.EmitSymbolValue(Label,
3007 Asm->getTargetData().getPointerSize(),
Chris Lattner31ae74d2010-03-14 02:20:58 +00003008 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00003009
Bill Wendling55fccda2009-05-20 23:21:38 +00003010 // If change of source, then switch to the new source.
3011 if (Source != LineInfo.getSourceID()) {
3012 Source = LineInfo.getSourceID();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003013 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3014 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner26be1c12010-04-04 19:09:29 +00003015 Asm->EmitULEB128(Source, "New Source");
Bill Wendling55fccda2009-05-20 23:21:38 +00003016 }
3017
3018 // If change of line.
3019 if (Line != LineInfo.getLine()) {
3020 // Determine offset.
3021 int Offset = LineInfo.getLine() - Line;
3022 int Delta = Offset - MinLineDelta;
3023
3024 // Update line.
3025 Line = LineInfo.getLine();
3026
3027 // If delta is small enough and in range...
3028 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3029 // ... then use fast opcode.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003030 Asm->OutStreamer.AddComment("Line Delta");
3031 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00003032 } else {
3033 // ... otherwise use long hand.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003034 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling55fccda2009-05-20 23:21:38 +00003035 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner26be1c12010-04-04 19:09:29 +00003036 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003037 Asm->OutStreamer.AddComment("DW_LNS_copy");
3038 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00003039 }
3040 } else {
3041 // Copy the previous row (different address or source)
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003042 Asm->OutStreamer.AddComment("DW_LNS_copy");
3043 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00003044 }
3045 }
3046
Devang Patelc50078e2009-11-21 02:48:08 +00003047 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003048 }
3049
3050 if (SecSrcLinesSize == 0)
3051 // Because we're emitting a debug_line section, we still need a line
3052 // table. The linker and friends expect it to exist. If there's nothing to
3053 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00003054 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003055
Chris Lattnerb93558d2010-04-04 19:25:43 +00003056 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003057}
3058
Devang Patelc50078e2009-11-21 02:48:08 +00003059/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003060///
Devang Patelc50078e2009-11-21 02:48:08 +00003061void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003062 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003063 return;
3064
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003065 int stackGrowth = Asm->getTargetData().getPointerSize();
3066 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3067 TargetFrameInfo::StackGrowsDown)
3068 stackGrowth *= -1;
Bill Wendling55fccda2009-05-20 23:21:38 +00003069
3070 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00003071 Asm->OutStreamer.SwitchSection(
3072 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003073
Chris Lattnerb93558d2010-04-04 19:25:43 +00003074 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003075 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003076 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3077 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003078
Chris Lattnerb93558d2010-04-04 19:25:43 +00003079 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003080 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling55fccda2009-05-20 23:21:38 +00003081 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003082 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00003083 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003084 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnercc564642010-01-23 03:11:46 +00003085 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner26be1c12010-04-04 19:09:29 +00003086 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3087 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003088 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003089 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling55fccda2009-05-20 23:21:38 +00003090 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling55fccda2009-05-20 23:21:38 +00003091
3092 std::vector<MachineMove> Moves;
3093 RI->getInitialFrameState(Moves);
3094
Chris Lattner193fec02010-04-04 23:41:46 +00003095 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00003096
Chris Lattner7101e232010-04-28 01:05:45 +00003097 Asm->EmitAlignment(2);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003098 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003099}
3100
Devang Patelc50078e2009-11-21 02:48:08 +00003101/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00003102/// section.
Chris Lattnerdd21da82010-03-13 07:26:18 +00003103void DwarfDebug::
3104emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003105 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003106 return;
3107
3108 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00003109 Asm->OutStreamer.SwitchSection(
3110 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003111
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003112 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattnerdd21da82010-03-13 07:26:18 +00003113 MCSymbol *DebugFrameBegin =
Chris Lattnerb93558d2010-04-04 19:25:43 +00003114 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattnerdd21da82010-03-13 07:26:18 +00003115 MCSymbol *DebugFrameEnd =
Chris Lattnerb93558d2010-04-04 19:25:43 +00003116 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003117 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003118
Chris Lattnerdd21da82010-03-13 07:26:18 +00003119 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling55fccda2009-05-20 23:21:38 +00003120
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003121 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003122 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3123 DwarfFrameSectionSym);
Bill Wendling55fccda2009-05-20 23:21:38 +00003124
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003125 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerb93558d2010-04-04 19:25:43 +00003126 MCSymbol *FuncBeginSym =
3127 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner314e3362010-03-13 07:40:56 +00003128 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003129 Asm->getTargetData().getPointerSize(),
3130 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00003131
3132
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003133 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003134 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003135 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00003136
Chris Lattner193fec02010-04-04 23:41:46 +00003137 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00003138
Chris Lattner7101e232010-04-28 01:05:45 +00003139 Asm->EmitAlignment(2);
Chris Lattnerdd21da82010-03-13 07:26:18 +00003140 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling55fccda2009-05-20 23:21:38 +00003141}
3142
Devang Patelfe0be132009-12-09 18:24:21 +00003143/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3144///
3145void DwarfDebug::emitDebugPubNames() {
3146 // Start the dwarf pubnames section.
3147 Asm->OutStreamer.SwitchSection(
3148 Asm->getObjFileLowering().getDwarfPubNamesSection());
3149
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003150 Asm->OutStreamer.AddComment("Length of Public Names Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003151 Asm->EmitLabelDifference(
3152 Asm->GetTempSymbol("pubnames_end", ModuleCU->getID()),
3153 Asm->GetTempSymbol("pubnames_begin", ModuleCU->getID()), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003154
Chris Lattnerb93558d2010-04-04 19:25:43 +00003155 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3156 ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00003157
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003158 Asm->OutStreamer.AddComment("DWARF Version");
3159 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00003160
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003161 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003162 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3163 DwarfInfoSectionSym);
Bill Wendling55fccda2009-05-20 23:21:38 +00003164
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003165 Asm->OutStreamer.AddComment("Compilation Unit Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003166 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3167 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3168 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003169
Devang Patelfe0be132009-12-09 18:24:21 +00003170 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00003171 for (StringMap<DIE*>::const_iterator
3172 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3173 const char *Name = GI->getKeyData();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003174 DIE *Entity = GI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00003175
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003176 Asm->OutStreamer.AddComment("DIE offset");
3177 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00003178
Chris Lattner97c69b72010-04-04 18:52:31 +00003179 if (Asm->isVerbose())
Chris Lattnercc564642010-01-23 03:11:46 +00003180 Asm->OutStreamer.AddComment("External Name");
3181 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003182 }
3183
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003184 Asm->OutStreamer.AddComment("End Mark");
3185 Asm->EmitInt32(0);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003186 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3187 ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00003188}
3189
Devang Patelec13b4f2009-11-24 01:14:22 +00003190void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00003191 // Start the dwarf pubnames section.
3192 Asm->OutStreamer.SwitchSection(
3193 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003194 Asm->OutStreamer.AddComment("Length of Public Types Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003195 Asm->EmitLabelDifference(
3196 Asm->GetTempSymbol("pubtypes_end", ModuleCU->getID()),
3197 Asm->GetTempSymbol("pubtypes_begin", ModuleCU->getID()), 4);
Devang Patelec13b4f2009-11-24 01:14:22 +00003198
Chris Lattnerb93558d2010-04-04 19:25:43 +00003199 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3200 ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00003201
Chris Lattner97c69b72010-04-04 18:52:31 +00003202 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
Devang Patel40b6cda2010-02-02 03:47:27 +00003203 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patelec13b4f2009-11-24 01:14:22 +00003204
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003205 Asm->OutStreamer.AddComment("Offset of Compilation ModuleCU Info");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003206 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3207 DwarfInfoSectionSym);
Devang Patelec13b4f2009-11-24 01:14:22 +00003208
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003209 Asm->OutStreamer.AddComment("Compilation ModuleCU Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003210 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3211 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3212 4);
Devang Patelec13b4f2009-11-24 01:14:22 +00003213
3214 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
3215 for (StringMap<DIE*>::const_iterator
3216 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3217 const char *Name = GI->getKeyData();
3218 DIE * Entity = GI->second;
3219
Chris Lattner97c69b72010-04-04 18:52:31 +00003220 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Devang Patel40b6cda2010-02-02 03:47:27 +00003221 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00003222
Chris Lattner97c69b72010-04-04 18:52:31 +00003223 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Devang Patel0ceb4892010-02-02 03:37:03 +00003224 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00003225 }
3226
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003227 Asm->OutStreamer.AddComment("End Mark");
3228 Asm->EmitInt32(0);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003229 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3230 ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00003231}
3232
Devang Patelc50078e2009-11-21 02:48:08 +00003233/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003234///
Devang Patelc50078e2009-11-21 02:48:08 +00003235void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003236 // Check to see if it is worth the effort.
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003237 if (StringPool.empty()) return;
3238
3239 // Start the dwarf str section.
3240 Asm->OutStreamer.SwitchSection(
Chris Lattner73266f92009-08-19 05:49:37 +00003241 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003242
Chris Lattner0f093f82010-03-13 02:17:42 +00003243 // Get all of the string pool entries and put them in an array by their ID so
3244 // we can sort them.
3245 SmallVector<std::pair<unsigned,
3246 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3247
3248 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3249 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3250 Entries.push_back(std::make_pair(I->second.second, &*I));
3251
3252 array_pod_sort(Entries.begin(), Entries.end());
3253
3254 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003255 // Emit a label for reference from debug information entries.
Chris Lattner0f093f82010-03-13 02:17:42 +00003256 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003257
3258 // Emit the string itself.
Chris Lattner0f093f82010-03-13 02:17:42 +00003259 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00003260 }
3261}
3262
Devang Patelc50078e2009-11-21 02:48:08 +00003263/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003264///
Devang Patelc50078e2009-11-21 02:48:08 +00003265void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003266 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00003267 Asm->OutStreamer.SwitchSection(
3268 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003269}
3270
3271/// EmitDebugARanges - Emit visible names into a debug aranges section.
3272///
3273void DwarfDebug::EmitDebugARanges() {
3274 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003275 Asm->OutStreamer.SwitchSection(
3276 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003277}
3278
Devang Patelc50078e2009-11-21 02:48:08 +00003279/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003280///
Devang Patelc50078e2009-11-21 02:48:08 +00003281void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003282 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003283 Asm->OutStreamer.SwitchSection(
Devang Patel146c6f72010-04-16 23:33:45 +00003284 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patelce7bf012010-04-27 19:46:33 +00003285 unsigned char Size = Asm->getTargetData().getPointerSize();
3286 for (SmallVector<const MCSymbol *, 8>::iterator
3287 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3288 I != E; ++I) {
3289 if (*I)
3290 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel146c6f72010-04-16 23:33:45 +00003291 else
Devang Patelce7bf012010-04-27 19:46:33 +00003292 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel146c6f72010-04-16 23:33:45 +00003293 }
Bill Wendling55fccda2009-05-20 23:21:38 +00003294}
3295
Devang Patelc50078e2009-11-21 02:48:08 +00003296/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003297///
Devang Patelc50078e2009-11-21 02:48:08 +00003298void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00003299 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00003300 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00003301 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00003302 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00003303 }
3304}
3305
Devang Patelc50078e2009-11-21 02:48:08 +00003306/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00003307/// Section Header:
3308/// 1. length of section
3309/// 2. Dwarf version number
3310/// 3. address size.
3311///
3312/// Entries (one "entry" for each function that was inlined):
3313///
3314/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3315/// otherwise offset into __debug_str for regular function name.
3316/// 2. offset into __debug_str section for regular function name.
3317/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3318/// instances for the function.
3319///
3320/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3321/// inlined instance; the die_offset points to the inlined_subroutine die in the
3322/// __debug_info section, and the low_pc is the starting address for the
3323/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00003324void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003325 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003326 return;
3327
Devang Patel5a3d37f2009-06-29 20:45:18 +00003328 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00003329 return;
3330
Chris Lattner73266f92009-08-19 05:49:37 +00003331 Asm->OutStreamer.SwitchSection(
3332 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerad653482010-01-22 22:09:00 +00003333
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003334 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003335 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3336 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003337
Chris Lattnerb93558d2010-04-04 19:25:43 +00003338 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003339
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003340 Asm->OutStreamer.AddComment("Dwarf Version");
3341 Asm->EmitInt16(dwarf::DWARF_VERSION);
3342 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003343 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00003344
Devang Patel90a0fe32009-11-10 23:06:00 +00003345 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
3346 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00003347
Devang Patel90a0fe32009-11-10 23:06:00 +00003348 MDNode *Node = *I;
Devang Patel7d707f92010-01-19 06:19:05 +00003349 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbachb23f2422009-11-22 19:20:36 +00003350 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00003351 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00003352 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00003353 StringRef LName = SP.getLinkageName();
3354 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00003355
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003356 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnercc564642010-01-23 03:11:46 +00003357 if (LName.empty()) {
3358 Asm->OutStreamer.EmitBytes(Name, 0);
3359 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3360 } else
Chris Lattnerc06b4742010-04-04 23:25:33 +00003361 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3362 DwarfStrSectionSym);
Devang Patel90a0fe32009-11-10 23:06:00 +00003363
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003364 Asm->OutStreamer.AddComment("Function name");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003365 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner26be1c12010-04-04 19:09:29 +00003366 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling55fccda2009-05-20 23:21:38 +00003367
Devang Patel90a0fe32009-11-10 23:06:00 +00003368 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00003369 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner97c69b72010-04-04 18:52:31 +00003370 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattneread58652010-03-09 00:31:02 +00003371 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00003372
Chris Lattner97c69b72010-04-04 18:52:31 +00003373 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003374 Asm->OutStreamer.EmitSymbolValue(LI->first,
3375 Asm->getTargetData().getPointerSize(),0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003376 }
3377 }
3378
Chris Lattnerb93558d2010-04-04 19:25:43 +00003379 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003380}