blob: 2a1ff9ae8c80b184d2d6ab12750e9e67bcf16927 [file] [log] [blame]
Bill Wendling0310d762009-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//===----------------------------------------------------------------------===//
Devang Patele4b27562009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Chris Lattner45111d12010-01-16 21:57:06 +000021#include "llvm/Target/Mangler.h"
Bill Wendling0310d762009-05-15 09:23:25 +000022#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000024#include "llvm/Target/TargetLoweringObjectFile.h"
25#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000026#include "llvm/ADT/StringExtras.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000029#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000030#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000031#include "llvm/Support/Timer.h"
32#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000033using namespace llvm;
34
Bill Wendling0310d762009-05-15 09:23:25 +000035//===----------------------------------------------------------------------===//
36
37/// Configuration values for initial hash set sizes (log2).
38///
Bill Wendling0310d762009-05-15 09:23:25 +000039static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000040
41namespace llvm {
42
43//===----------------------------------------------------------------------===//
44/// CompileUnit - This dwarf writer support class manages information associate
45/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000046class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000047 /// ID - File identifier for source.
48 ///
49 unsigned ID;
50
51 /// Die - Compile unit debug information entry.
52 ///
Douglas Gregor1ddcf352010-03-08 02:58:37 +000053 DIE *CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000054
Douglas Gregor1ddcf352010-03-08 02:58:37 +000055 /// IndexTyDie - An anonymous type for index type.
Devang Patel6f01d9c2009-11-21 00:31:03 +000056 DIE *IndexTyDie;
57
Bill Wendling0310d762009-05-15 09:23:25 +000058 /// GVToDieMap - Tracks the mapping of unit level debug informaton
59 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000060 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel622b0262010-01-19 06:19:05 +000061 DenseMap<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000062
63 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
64 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000065 /// FIXME : Rename
Devang Patel622b0262010-01-19 06:19:05 +000066 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000067
68 /// Globals - A map of globally visible named entities for this unit.
69 ///
70 StringMap<DIE*> Globals;
71
Devang Patel193f7202009-11-24 01:14:22 +000072 /// GlobalTypes - A map of globally visible types for this unit.
73 ///
74 StringMap<DIE*> GlobalTypes;
75
Bill Wendling0310d762009-05-15 09:23:25 +000076public:
77 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +000078 : ID(I), CUDie(D), IndexTyDie(0) {}
Douglas Gregor1ddcf352010-03-08 02:58:37 +000079 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendling0310d762009-05-15 09:23:25 +000080
81 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +000082 unsigned getID() const { return ID; }
Douglas Gregor1ddcf352010-03-08 02:58:37 +000083 DIE* getCUDie() const { return CUDie; }
Devang Patel193f7202009-11-24 01:14:22 +000084 const StringMap<DIE*> &getGlobals() const { return Globals; }
85 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +000086
87 /// hasContent - Return true if this compile unit has something to write out.
88 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000089 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +000090
Devang Patel2c4ceb12009-11-21 02:48:08 +000091 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +000092 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000093 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000094
Devang Patel193f7202009-11-24 01:14:22 +000095 /// addGlobalType - Add a new global type to the compile unit.
96 ///
97 void addGlobalType(const std::string &Name, DIE *Die) {
98 GlobalTypes[Name] = Die;
99 }
100
Devang Patel017d1212009-11-20 21:37:22 +0000101 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000102 /// specified debug variable.
Devang Patel017d1212009-11-20 21:37:22 +0000103 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000104
Devang Patel017d1212009-11-20 21:37:22 +0000105 /// insertDIE - Insert DIE into the map.
106 void insertDIE(MDNode *N, DIE *D) {
107 GVToDieMap.insert(std::make_pair(N, D));
108 }
Bill Wendling0310d762009-05-15 09:23:25 +0000109
Devang Patel017d1212009-11-20 21:37:22 +0000110 /// getDIEEntry - Returns the debug information entry for the speciefied
111 /// debug variable.
Devang Patel6404e4e2009-12-15 19:16:48 +0000112 DIEEntry *getDIEEntry(MDNode *N) {
Devang Patel622b0262010-01-19 06:19:05 +0000113 DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel6404e4e2009-12-15 19:16:48 +0000114 if (I == GVToDIEEntryMap.end())
115 return NULL;
116 return I->second;
117 }
Devang Patel017d1212009-11-20 21:37:22 +0000118
119 /// insertDIEEntry - Insert debug information entry into the map.
120 void insertDIEEntry(MDNode *N, DIEEntry *E) {
121 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000122 }
123
Devang Patel2c4ceb12009-11-21 02:48:08 +0000124 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000125 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000126 void addDie(DIE *Buffer) {
127 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000128 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000129
130 // getIndexTyDie - Get an anonymous type for index type.
131 DIE *getIndexTyDie() {
132 return IndexTyDie;
133 }
134
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000135 // setIndexTyDie - Set D as anonymous type for index which can be reused
136 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000137 void setIndexTyDie(DIE *D) {
138 IndexTyDie = D;
139 }
140
Bill Wendling0310d762009-05-15 09:23:25 +0000141};
142
143//===----------------------------------------------------------------------===//
144/// DbgVariable - This class is used to track local variable information.
145///
Devang Patelf76a3d62009-11-16 21:53:40 +0000146class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000147 DIVariable Var; // Variable Descriptor.
148 unsigned FrameIndex; // Variable frame index.
Devang Patel53bb5c92009-11-10 23:06:00 +0000149 DbgVariable *AbstractVar; // Abstract variable for this variable.
150 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000151public:
Devang Patel53bb5c92009-11-10 23:06:00 +0000152 DbgVariable(DIVariable V, unsigned I)
153 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000154
155 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000156 DIVariable getVariable() const { return Var; }
157 unsigned getFrameIndex() const { return FrameIndex; }
158 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
159 DbgVariable *getAbstractVariable() const { return AbstractVar; }
160 void setDIE(DIE *D) { TheDIE = D; }
161 DIE *getDIE() const { return TheDIE; }
Bill Wendling0310d762009-05-15 09:23:25 +0000162};
163
164//===----------------------------------------------------------------------===//
165/// DbgScope - This class is used to track scope information.
166///
Devang Patelf76a3d62009-11-16 21:53:40 +0000167class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000168 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000169 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000170 // Location at which this scope is inlined.
171 AssertingVH<MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000172 bool AbstractScope; // Abstract Scope
Bill Wendling0310d762009-05-15 09:23:25 +0000173 unsigned StartLabelID; // Label ID of the beginning of scope.
174 unsigned EndLabelID; // Label ID of the end of scope.
Devang Pateld38dd112009-10-01 18:25:23 +0000175 const MachineInstr *LastInsn; // Last instruction of this scope.
176 const MachineInstr *FirstInsn; // First instruction of this scope.
Douglas Gregor1ddcf352010-03-08 02:58:37 +0000177 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
178 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000179
Owen Anderson04c05f72009-06-24 22:53:20 +0000180 // Private state for dump()
181 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000182public:
Devang Patelc90aefe2009-10-14 21:08:09 +0000183 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000184 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000185 StartLabelID(0), EndLabelID(0),
Devang Patelc90aefe2009-10-14 21:08:09 +0000186 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000187 virtual ~DbgScope();
188
189 // Accessors.
190 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000191 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000192 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000193 MDNode *getInlinedAt() const {
Devang Patel3139fcf2010-01-26 21:39:14 +0000194 return InlinedAtLocation;
Devang Patelc90aefe2009-10-14 21:08:09 +0000195 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000196 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000197 unsigned getStartLabelID() const { return StartLabelID; }
198 unsigned getEndLabelID() const { return EndLabelID; }
Douglas Gregor1ddcf352010-03-08 02:58:37 +0000199 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
200 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendling0310d762009-05-15 09:23:25 +0000201 void setStartLabelID(unsigned S) { StartLabelID = S; }
202 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Pateld38dd112009-10-01 18:25:23 +0000203 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
204 const MachineInstr *getLastInsn() { return LastInsn; }
205 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000206 void setAbstractScope() { AbstractScope = true; }
207 bool isAbstractScope() const { return AbstractScope; }
Devang Pateld38dd112009-10-01 18:25:23 +0000208 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000209
Devang Patel2c4ceb12009-11-21 02:48:08 +0000210 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000211 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000212 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000213
Devang Patel2c4ceb12009-11-21 02:48:08 +0000214 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000215 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000216 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000217
Devang Patel344130e2010-01-04 20:44:00 +0000218 void fixInstructionMarkers(DenseMap<const MachineInstr *,
219 unsigned> &MIIndexMap) {
Devang Patelaf9e8472009-10-01 20:31:14 +0000220 assert (getFirstInsn() && "First instruction is missing!");
Devang Patel344130e2010-01-04 20:44:00 +0000221
222 // Use the end of last child scope as end of this scope.
Douglas Gregor1ddcf352010-03-08 02:58:37 +0000223 SmallVector<DbgScope *, 4> &Scopes = getScopes();
Devang Patelee890ed2010-01-05 16:59:17 +0000224 const MachineInstr *LastInsn = getFirstInsn();
Devang Patel344130e2010-01-04 20:44:00 +0000225 unsigned LIndex = 0;
226 if (Scopes.empty()) {
227 assert (getLastInsn() && "Inner most scope does not have last insn!");
228 return;
229 }
Douglas Gregor1ddcf352010-03-08 02:58:37 +0000230 for (SmallVector<DbgScope *, 4>::iterator SI = Scopes.begin(),
Devang Patel344130e2010-01-04 20:44:00 +0000231 SE = Scopes.end(); SI != SE; ++SI) {
232 DbgScope *DS = *SI;
233 DS->fixInstructionMarkers(MIIndexMap);
234 const MachineInstr *DSLastInsn = DS->getLastInsn();
235 unsigned DSI = MIIndexMap[DSLastInsn];
236 if (DSI > LIndex) {
237 LastInsn = DSLastInsn;
238 LIndex = DSI;
239 }
240 }
Devang Patel22fb4b22010-02-17 02:20:34 +0000241
242 unsigned CurrentLastInsnIndex = 0;
243 if (const MachineInstr *CL = getLastInsn())
244 CurrentLastInsnIndex = MIIndexMap[CL];
245 unsigned FIndex = MIIndexMap[getFirstInsn()];
246
247 // Set LastInsn as the last instruction for this scope only if
248 // it follows
249 // 1) this scope's first instruction and
250 // 2) current last instruction for this scope, if any.
251 if (LIndex >= CurrentLastInsnIndex && LIndex >= FIndex)
252 setLastInsn(LastInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +0000253 }
254
Bill Wendling0310d762009-05-15 09:23:25 +0000255#ifndef NDEBUG
256 void dump() const;
257#endif
258};
259
260#ifndef NDEBUG
261void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000262 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000263 err.indent(IndentLevel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000264 MDNode *N = Desc.getNode();
265 N->dump();
Chris Lattnerc281de12009-08-23 00:51:00 +0000266 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel53bb5c92009-11-10 23:06:00 +0000267 if (AbstractScope)
268 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000269
270 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000271 if (!Scopes.empty())
272 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000273 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
274 if (Scopes[i] != this)
275 Scopes[i]->dump();
276
277 IndentLevel -= 2;
278}
279#endif
280
Bill Wendling0310d762009-05-15 09:23:25 +0000281DbgScope::~DbgScope() {
Douglas Gregor1ddcf352010-03-08 02:58:37 +0000282 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
283 delete Scopes[i];
Bill Wendling0310d762009-05-15 09:23:25 +0000284 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
285 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000286}
287
288} // end llvm namespace
289
Chris Lattneraf76e592009-08-22 20:48:53 +0000290DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Chris Lattnerc3421bb2010-03-09 00:00:15 +0000291 : DwarfPrinter(OS, A, T), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000292 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patel2c4ceb12009-11-21 02:48:08 +0000293 DIEValues(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000294 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel53bb5c92009-11-10 23:06:00 +0000295 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000296 if (TimePassesIsEnabled)
Chris Lattner0b86a6f2009-12-28 07:41:18 +0000297 DebugTimer = new Timer("Dwarf Debug Writer");
Bill Wendling0310d762009-05-15 09:23:25 +0000298}
299DwarfDebug::~DwarfDebug() {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000300 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
301 delete DIEValues[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000302
Bill Wendling0310d762009-05-15 09:23:25 +0000303 delete DebugTimer;
304}
305
Devang Patel2c4ceb12009-11-21 02:48:08 +0000306/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000307///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000308void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000309 // Profile the node so that we can make it unique.
310 FoldingSetNodeID ID;
311 Abbrev.Profile(ID);
312
313 // Check the set for priors.
314 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
315
316 // If it's newly added.
317 if (InSet == &Abbrev) {
318 // Add to abbreviation list.
319 Abbreviations.push_back(&Abbrev);
320
321 // Assign the vector position + 1 as its number.
322 Abbrev.setNumber(Abbreviations.size());
323 } else {
324 // Assign existing abbreviation number.
325 Abbrev.setNumber(InSet->getNumber());
326 }
327}
328
Devang Patel2c4ceb12009-11-21 02:48:08 +0000329/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000330/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000331DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000332 DIEEntry *Value = new DIEEntry(Entry);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000333 DIEValues.push_back(Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000334 return Value;
335}
336
Devang Patel2c4ceb12009-11-21 02:48:08 +0000337/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000338///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000339void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000340 unsigned Form, uint64_t Integer) {
341 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000342 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000343 DIEValues.push_back(Value);
344 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000345}
346
Devang Patel2c4ceb12009-11-21 02:48:08 +0000347/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000348///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000349void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000350 unsigned Form, int64_t Integer) {
351 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000352 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000353 DIEValues.push_back(Value);
354 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000355}
356
Devang Patel69f57b12009-12-02 15:25:16 +0000357/// addString - Add a string attribute data and value. DIEString only
358/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000359void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner4cf202b2010-01-23 03:11:46 +0000360 StringRef String) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000361 DIEValue *Value = new DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000362 DIEValues.push_back(Value);
363 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000364}
365
Devang Patel2c4ceb12009-11-21 02:48:08 +0000366/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000367///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000368void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000369 const MCSymbol *Label) {
Chris Lattner4faf59a2010-03-08 22:31:46 +0000370 DIEValue *Value = new DIELabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000371 DIEValues.push_back(Value);
372 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000373}
374
Devang Patel2c4ceb12009-11-21 02:48:08 +0000375/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000376///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000377void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000378 const MCSymbol *Label,const MCSymbol *Section,
Chris Lattner57578762010-03-08 23:23:25 +0000379 bool isEH) {
380 DIEValue *Value = new DIESectionOffset(Label, Section, isEH);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000381 DIEValues.push_back(Value);
382 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000383}
384
Devang Patel2c4ceb12009-11-21 02:48:08 +0000385/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000386///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000387void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000388 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000389 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000390 DIEValues.push_back(Value);
391 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000392}
393
Devang Patel2c4ceb12009-11-21 02:48:08 +0000394/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000395///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000396void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000397 DIEBlock *Block) {
398 Block->ComputeSize(TD);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000399 DIEValues.push_back(Block);
400 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000401}
402
Devang Patel2c4ceb12009-11-21 02:48:08 +0000403/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000404/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000405void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendling0310d762009-05-15 09:23:25 +0000406 // If there is no compile unit specified, don't add a line #.
Devang Patel3c91b052010-03-08 20:52:55 +0000407 if (!V->getCompileUnit().Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000408 return;
409
410 unsigned Line = V->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000411 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
412 V->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000413 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000414 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
415 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000416}
417
Devang Patel2c4ceb12009-11-21 02:48:08 +0000418/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000419/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000420void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendling0310d762009-05-15 09:23:25 +0000421 // If there is no compile unit specified, don't add a line #.
Devang Patel3c91b052010-03-08 20:52:55 +0000422 if (!G->getCompileUnit().Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000423 return;
424
425 unsigned Line = G->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000426 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
427 G->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000428 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000429 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
430 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000431}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000432
Devang Patel2c4ceb12009-11-21 02:48:08 +0000433/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000434/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000435void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel82dfc0c2009-08-31 22:47:13 +0000436 // If there is no compile unit specified, don't add a line #.
Devang Patel3c91b052010-03-08 20:52:55 +0000437 if (!SP->getCompileUnit().Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000438 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000439 // If the line number is 0, don't add it.
440 if (SP->getLineNumber() == 0)
441 return;
442
Devang Patel82dfc0c2009-08-31 22:47:13 +0000443 unsigned Line = SP->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000444 if (!SP->getContext().Verify())
445 return;
446 unsigned FileID = GetOrCreateSourceID(SP->getContext().getDirectory(),
447 SP->getContext().getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000448 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000449 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
450 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000451}
452
Devang Patel2c4ceb12009-11-21 02:48:08 +0000453/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000454/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000455void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000456 // If there is no compile unit specified, don't add a line #.
457 DICompileUnit CU = Ty->getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000458 if (!CU.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000459 return;
460
461 unsigned Line = Ty->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000462 if (!Ty->getContext().Verify())
463 return;
464 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
465 Ty->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000466 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000467 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
468 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000469}
470
Devang Patel6404e4e2009-12-15 19:16:48 +0000471/// addSourceLine - Add location information to specified debug information
472/// entry.
473void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
474 // If there is no compile unit specified, don't add a line #.
Devang Patel3c91b052010-03-08 20:52:55 +0000475 if (!NS->getCompileUnit().Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000476 return;
477
478 unsigned Line = NS->getLineNumber();
479 StringRef FN = NS->getFilename();
480 StringRef Dir = NS->getDirectory();
481
482 unsigned FileID = GetOrCreateSourceID(Dir, FN);
483 assert(FileID && "Invalid file id");
484 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
485 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
486}
487
Caroline Ticedc8f6042009-08-31 21:19:37 +0000488/* Byref variables, in Blocks, are declared by the programmer as
489 "SomeType VarName;", but the compiler creates a
490 __Block_byref_x_VarName struct, and gives the variable VarName
491 either the struct, or a pointer to the struct, as its type. This
492 is necessary for various behind-the-scenes things the compiler
493 needs to do with by-reference variables in blocks.
494
495 However, as far as the original *programmer* is concerned, the
496 variable should still have type 'SomeType', as originally declared.
497
498 The following function dives into the __Block_byref_x_VarName
499 struct to find the original type of the variable. This will be
500 passed back to the code generating the type for the Debug
501 Information Entry for the variable 'VarName'. 'VarName' will then
502 have the original type 'SomeType' in its debug information.
503
504 The original type 'SomeType' will be the type of the field named
505 'VarName' inside the __Block_byref_x_VarName struct.
506
507 NOTE: In order for this to not completely fail on the debugger
508 side, the Debug Information Entry for the variable VarName needs to
509 have a DW_AT_location that tells the debugger how to unwind through
510 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000511 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000512
513/// Find the type the programmer originally declared the variable to be
514/// and return that type.
515///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000516DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000517
518 DIType subType = Ty;
519 unsigned tag = Ty.getTag();
520
521 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000522 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000523 subType = DTy.getTypeDerivedFrom();
524 }
525
526 DICompositeType blockStruct = DICompositeType(subType.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000527 DIArray Elements = blockStruct.getTypeArray();
528
Caroline Ticedc8f6042009-08-31 21:19:37 +0000529 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
530 DIDescriptor Element = Elements.getElement(i);
531 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000532 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000533 return (DT.getTypeDerivedFrom());
534 }
535
536 return Ty;
537}
538
Devang Patel2c4ceb12009-11-21 02:48:08 +0000539/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000540/// and generate the DWARF information necessary to find the actual variable
541/// given the extra address information encoded in the DIVariable, starting from
542/// the starting location. Add the DWARF information to the die.
543///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000544void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000545 unsigned Attribute,
546 const MachineLocation &Location) {
547 const DIVariable &VD = DV->getVariable();
548 DIType Ty = VD.getType();
549
550 // Decode the original location, and use that as the start of the byref
551 // variable's location.
552 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
553 DIEBlock *Block = new DIEBlock();
554
555 if (Location.isReg()) {
556 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000557 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000558 } else {
559 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000560 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
561 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000562 }
563 } else {
564 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000565 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000566 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000567 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
568 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000569 }
570
Devang Patel2c4ceb12009-11-21 02:48:08 +0000571 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000572 }
573
574 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
575 uint64_t Element = VD.getAddrElement(i);
576
577 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000578 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
579 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000580 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000581 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000582 } else llvm_unreachable("unknown DIFactory Opcode");
583 }
584
585 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000586 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000587}
588
Caroline Ticedc8f6042009-08-31 21:19:37 +0000589/* Byref variables, in Blocks, are declared by the programmer as "SomeType
590 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
591 gives the variable VarName either the struct, or a pointer to the struct, as
592 its type. This is necessary for various behind-the-scenes things the
593 compiler needs to do with by-reference variables in Blocks.
594
595 However, as far as the original *programmer* is concerned, the variable
596 should still have type 'SomeType', as originally declared.
597
Devang Patel2c4ceb12009-11-21 02:48:08 +0000598 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000599 struct to find the original type of the variable, which is then assigned to
600 the variable's Debug Information Entry as its real type. So far, so good.
601 However now the debugger will expect the variable VarName to have the type
602 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000603 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000604 pointers and struct to find the actual variable of type SomeType.
605
606 The following function does just that. We start by getting
607 the "normal" location for the variable. This will be the location
608 of either the struct __Block_byref_x_VarName or the pointer to the
609 struct __Block_byref_x_VarName.
610
611 The struct will look something like:
612
613 struct __Block_byref_x_VarName {
614 ... <various fields>
615 struct __Block_byref_x_VarName *forwarding;
616 ... <various other fields>
617 SomeType VarName;
618 ... <maybe more fields>
619 };
620
621 If we are given the struct directly (as our starting point) we
622 need to tell the debugger to:
623
624 1). Add the offset of the forwarding field.
625
Dan Gohmanf451cb82010-02-10 16:03:48 +0000626 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000627 struct to use (the real one may have been copied onto the heap).
628
629 3). Add the offset for the field VarName, to find the actual variable.
630
631 If we started with a pointer to the struct, then we need to
632 dereference that pointer first, before the other steps.
633 Translating this into DWARF ops, we will need to append the following
634 to the current location description for the variable:
635
636 DW_OP_deref -- optional, if we start with a pointer
637 DW_OP_plus_uconst <forward_fld_offset>
638 DW_OP_deref
639 DW_OP_plus_uconst <varName_fld_offset>
640
641 That is what this function does. */
642
Devang Patel2c4ceb12009-11-21 02:48:08 +0000643/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000644/// provided, and generate the DWARF information necessary to find the
645/// actual Block variable (navigating the Block struct) based on the
646/// starting location. Add the DWARF information to the die. For
647/// more information, read large comment just above here.
648///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000649void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000650 unsigned Attribute,
651 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000652 const DIVariable &VD = DV->getVariable();
653 DIType Ty = VD.getType();
654 DIType TmpTy = Ty;
655 unsigned Tag = Ty.getTag();
656 bool isPointer = false;
657
Devang Patel65dbc902009-11-25 17:36:49 +0000658 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000659
660 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000661 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000662 TmpTy = DTy.getTypeDerivedFrom();
663 isPointer = true;
664 }
665
666 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
667
Daniel Dunbar00564992009-09-19 20:40:14 +0000668 // Find the __forwarding field and the variable field in the __Block_byref
669 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000670 DIArray Fields = blockStruct.getTypeArray();
671 DIDescriptor varField = DIDescriptor();
672 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000673
Daniel Dunbar00564992009-09-19 20:40:14 +0000674 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
675 DIDescriptor Element = Fields.getElement(i);
676 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000677 StringRef fieldName = DT.getName();
678 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000679 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000680 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000681 varField = Element;
682 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000683
Daniel Dunbar00564992009-09-19 20:40:14 +0000684 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000685 unsigned int forwardingFieldOffset =
686 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
687 unsigned int varFieldOffset =
688 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000689
Mike Stump7e3720d2009-09-24 23:21:26 +0000690 // Decode the original location, and use that as the start of the byref
691 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000692 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
693 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000694
Daniel Dunbar00564992009-09-19 20:40:14 +0000695 if (Location.isReg()) {
696 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000697 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000698 else {
699 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000700 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
701 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000702 }
703 } else {
704 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000705 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000706 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000707 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
708 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000709 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000710
Devang Patel2c4ceb12009-11-21 02:48:08 +0000711 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000712 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000713
Mike Stump7e3720d2009-09-24 23:21:26 +0000714 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000715 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000716 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000717 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000718
Daniel Dunbar00564992009-09-19 20:40:14 +0000719 // Next add the offset for the '__forwarding' field:
720 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
721 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000722 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000723 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
724 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000725 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000726
Daniel Dunbar00564992009-09-19 20:40:14 +0000727 // Now dereference the __forwarding field to get to the real __Block_byref
728 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000729 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000730
Daniel Dunbar00564992009-09-19 20:40:14 +0000731 // Now that we've got the real __Block_byref... struct, add the offset
732 // for the variable's field to get to the location of the actual variable:
733 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000734 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000735 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
736 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000737 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000738
Daniel Dunbar00564992009-09-19 20:40:14 +0000739 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000740 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000741}
742
Devang Patel2c4ceb12009-11-21 02:48:08 +0000743/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000744/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000745void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000746 const MachineLocation &Location) {
747 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
748 DIEBlock *Block = new DIEBlock();
749
750 if (Location.isReg()) {
751 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000752 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000753 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000754 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
755 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000756 }
757 } else {
758 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000759 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000760 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000761 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
762 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000763 }
764
Devang Patel2c4ceb12009-11-21 02:48:08 +0000765 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000766 }
767
Devang Patel2c4ceb12009-11-21 02:48:08 +0000768 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000769}
770
Devang Patelc366f832009-12-10 19:14:49 +0000771/// addToContextOwner - Add Die into the list of its context owner's children.
772void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000773 if (Context.isType()) {
Devang Patelc366f832009-12-10 19:14:49 +0000774 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
775 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000776 } else if (Context.isNameSpace()) {
777 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
778 ContextDIE->addChild(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000779 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
780 ContextDIE->addChild(Die);
781 else
782 ModuleCU->addDie(Die);
783}
784
Devang Patel16ced732009-12-10 18:05:33 +0000785/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
786/// given DIType.
787DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
788 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
789 if (TyDIE)
790 return TyDIE;
791
792 // Create new type.
793 TyDIE = new DIE(dwarf::DW_TAG_base_type);
794 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
795 if (Ty.isBasicType())
796 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
797 else if (Ty.isCompositeType())
798 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
799 else {
800 assert(Ty.isDerivedType() && "Unknown kind of DIType");
801 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
802 }
803
Devang Patelc366f832009-12-10 19:14:49 +0000804 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000805 return TyDIE;
806}
807
Devang Patel2c4ceb12009-11-21 02:48:08 +0000808/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000809void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel3c91b052010-03-08 20:52:55 +0000810 if (!Ty.isValid())
Bill Wendling0310d762009-05-15 09:23:25 +0000811 return;
812
813 // Check for pre-existence.
Devang Patel8a241142009-12-09 18:24:21 +0000814 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000815 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000816 if (Entry) {
817 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000818 return;
819 }
820
821 // Set up proxy.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000822 Entry = createDIEEntry();
Devang Patel8a241142009-12-09 18:24:21 +0000823 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000824
825 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000826 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000827
Devang Patel2c4ceb12009-11-21 02:48:08 +0000828 Entry->setEntry(Buffer);
829 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000830}
831
Devang Patel2c4ceb12009-11-21 02:48:08 +0000832/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000833void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000834 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000835 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000836 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000837 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000838 BTy.getEncoding());
839
840 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000841 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000842 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000843 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000844 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000845}
846
Devang Patel2c4ceb12009-11-21 02:48:08 +0000847/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000848void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000849 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000850 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000851 uint64_t Size = DTy.getSizeInBits() >> 3;
852 unsigned Tag = DTy.getTag();
853
854 // FIXME - Workaround for templates.
855 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
856
857 Buffer.setTag(Tag);
858
859 // Map to main type, void will not have a type.
860 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000861 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000862
863 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000864 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000865 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000866
867 // Add size if non-zero (derived types might be zero-sized.)
868 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000869 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000870
871 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000872 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000873 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000874}
875
Devang Patel2c4ceb12009-11-21 02:48:08 +0000876/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000877void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000878 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000879 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000880
881 uint64_t Size = CTy.getSizeInBits() >> 3;
882 unsigned Tag = CTy.getTag();
883 Buffer.setTag(Tag);
884
885 switch (Tag) {
886 case dwarf::DW_TAG_vector_type:
887 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +0000888 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000889 break;
890 case dwarf::DW_TAG_enumeration_type: {
891 DIArray Elements = CTy.getTypeArray();
892
893 // Add enumerators to enumeration type.
894 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
895 DIE *ElemDie = NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000896 DIDescriptor Enum(Elements.getElement(i).getNode());
897 if (Enum.isEnumerator()) {
898 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000899 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +0000900 }
Bill Wendling0310d762009-05-15 09:23:25 +0000901 }
902 }
903 break;
904 case dwarf::DW_TAG_subroutine_type: {
905 // Add return type.
906 DIArray Elements = CTy.getTypeArray();
907 DIDescriptor RTy = Elements.getElement(0);
Devang Patel8a241142009-12-09 18:24:21 +0000908 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000909
910 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000911 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000912
913 // Add arguments.
914 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
915 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
916 DIDescriptor Ty = Elements.getElement(i);
Devang Patel8a241142009-12-09 18:24:21 +0000917 addType(Arg, DIType(Ty.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000918 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000919 }
920 }
921 break;
922 case dwarf::DW_TAG_structure_type:
923 case dwarf::DW_TAG_union_type:
924 case dwarf::DW_TAG_class_type: {
925 // Add elements to structure type.
926 DIArray Elements = CTy.getTypeArray();
927
928 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +0000929 unsigned N = Elements.getNumElements();
930 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +0000931 break;
932
933 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +0000934 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +0000935 DIDescriptor Element = Elements.getElement(i);
936 DIE *ElemDie = NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000937 if (Element.isSubprogram())
Devang Patelffe966c2009-12-14 16:18:45 +0000938 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Devang Patel3c91b052010-03-08 20:52:55 +0000939 else if (Element.isVariable()) {
Devang Patel1ee0cb92010-01-30 01:08:30 +0000940 DIVariable DV(Element.getNode());
941 ElemDie = new DIE(dwarf::DW_TAG_variable);
942 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
943 DV.getName());
944 addType(ElemDie, DV.getType());
945 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
946 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
947 addSourceLine(ElemDie, &DV);
Devang Patel3c91b052010-03-08 20:52:55 +0000948 } else if (Element.isDerivedType())
Devang Patel8a241142009-12-09 18:24:21 +0000949 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel3c91b052010-03-08 20:52:55 +0000950 else
951 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000952 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +0000953 }
954
Devang Patela1ba2692009-08-27 23:51:51 +0000955 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000956 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000957
958 unsigned RLang = CTy.getRunTimeLang();
959 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000960 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +0000961 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +0000962
963 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel3c91b052010-03-08 20:52:55 +0000964 if (DIDescriptor(ContainingType.getNode()).isCompositeType())
Devang Patelb5544992010-01-26 21:16:06 +0000965 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
966 getOrCreateTypeDIE(DIType(ContainingType.getNode())));
Bill Wendling0310d762009-05-15 09:23:25 +0000967 break;
968 }
969 default:
970 break;
971 }
972
973 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000974 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000975 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000976
Devang Patel8cbb6122010-01-29 18:34:58 +0000977 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendling0310d762009-05-15 09:23:25 +0000978 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
979 // Add size if non-zero (derived types might be zero-sized.)
980 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000981 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000982 else {
983 // Add zero size if it is not a forward declaration.
984 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000985 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000986 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000987 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +0000988 }
989
990 // Add source line info if available.
991 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000992 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000993 }
994}
995
Devang Patel2c4ceb12009-11-21 02:48:08 +0000996/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
997void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +0000998 int64_t L = SR.getLo();
999 int64_t H = SR.getHi();
1000 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1001
Devang Patel2c4ceb12009-11-21 02:48:08 +00001002 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001003 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001004 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001005 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001006
Devang Patel2c4ceb12009-11-21 02:48:08 +00001007 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001008}
1009
Devang Patel2c4ceb12009-11-21 02:48:08 +00001010/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001011void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001012 DICompositeType *CTy) {
1013 Buffer.setTag(dwarf::DW_TAG_array_type);
1014 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001015 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001016
1017 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001018 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001019 DIArray Elements = CTy->getTypeArray();
1020
Devang Patel6f01d9c2009-11-21 00:31:03 +00001021 // Get an anonymous type for index type.
Devang Patel8a241142009-12-09 18:24:21 +00001022 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001023 if (!IdxTy) {
1024 // Construct an anonymous type for index type.
1025 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001026 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1027 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001028 dwarf::DW_ATE_signed);
Devang Patel8a241142009-12-09 18:24:21 +00001029 ModuleCU->addDie(IdxTy);
1030 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001031 }
Bill Wendling0310d762009-05-15 09:23:25 +00001032
1033 // Add subranges to array type.
1034 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1035 DIDescriptor Element = Elements.getElement(i);
1036 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001037 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001038 }
1039}
1040
Devang Patel2c4ceb12009-11-21 02:48:08 +00001041/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001042DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001043 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001044 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001045 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001046 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001047 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001048 return Enumerator;
1049}
1050
Devang Patel351ca332010-01-05 01:46:14 +00001051/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1052/// printer to not emit usual symbol prefix before the symbol name is used then
1053/// return linkage name after skipping this special LLVM prefix.
1054static StringRef getRealLinkageName(StringRef LinkageName) {
1055 char One = '\1';
1056 if (LinkageName.startswith(StringRef(&One, 1)))
1057 return LinkageName.substr(1);
1058 return LinkageName;
1059}
1060
Devang Patel2c4ceb12009-11-21 02:48:08 +00001061/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel8a241142009-12-09 18:24:21 +00001062DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001063 // If the global variable was optmized out then no need to create debug info
1064 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001065 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001066 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001067
Bill Wendling0310d762009-05-15 09:23:25 +00001068 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001069 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001070 GV.getDisplayName());
1071
Devang Patel65dbc902009-11-25 17:36:49 +00001072 StringRef LinkageName = GV.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001073 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001074 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001075 getRealLinkageName(LinkageName));
1076
Devang Patel8a241142009-12-09 18:24:21 +00001077 addType(GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001078 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001079 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1080 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001081
Bill Wendling0310d762009-05-15 09:23:25 +00001082 return GVDie;
1083}
1084
Devang Patel2c4ceb12009-11-21 02:48:08 +00001085/// createMemberDIE - Create new member DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001086DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001087 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001088 StringRef Name = DT.getName();
1089 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001090 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001091
Devang Patel8a241142009-12-09 18:24:21 +00001092 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001093
Devang Patel2c4ceb12009-11-21 02:48:08 +00001094 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001095
Devang Patel33db5082009-11-04 22:06:12 +00001096 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001097 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001098
Bill Wendling0310d762009-05-15 09:23:25 +00001099 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001100 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001101
1102 if (Size != FieldSize) {
1103 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001104 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1105 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001106
1107 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001108 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1109 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001110 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001111 Offset -= FieldOffset;
1112
1113 // Maybe we need to work from the other end.
1114 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001115 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001116
Devang Patel33db5082009-11-04 22:06:12 +00001117 // Here WD_AT_data_member_location points to the anonymous
1118 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001119 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001120
1121 } else
1122 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001123 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001124
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001125 if (DT.getTag() == dwarf::DW_TAG_inheritance
1126 && DT.isVirtual()) {
1127
1128 // For C++, virtual base classes are not at fixed offset. Use following
1129 // expression to extract appropriate offset from vtable.
1130 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1131
1132 DIEBlock *VBaseLocationDie = new DIEBlock();
1133 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1134 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1135 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1136 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1137 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1138 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1139 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1140
1141 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1142 VBaseLocationDie);
1143 } else
1144 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001145
1146 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001147 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001148 dwarf::DW_ACCESS_protected);
1149 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001150 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001151 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001152 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1153 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1154 dwarf::DW_ACCESS_public);
1155 if (DT.isVirtual())
1156 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1157 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001158 return MemberDie;
1159}
1160
Devang Patelffe966c2009-12-14 16:18:45 +00001161/// createSubprogramDIE - Create new DIE using SP.
1162DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1163 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1164 if (SPDie)
1165 return SPDie;
1166
1167 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001168 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001169 if (!SP.getName().empty())
1170 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001171
Devang Patel65dbc902009-11-25 17:36:49 +00001172 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001173 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001174 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001175 getRealLinkageName(LinkageName));
1176
Devang Patel2c4ceb12009-11-21 02:48:08 +00001177 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001178
Bill Wendling0310d762009-05-15 09:23:25 +00001179 // Add prototyped tag, if C or ObjC.
1180 unsigned Lang = SP.getCompileUnit().getLanguage();
1181 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1182 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001183 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001184
1185 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001186 DICompositeType SPTy = SP.getType();
1187 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001188 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001189
Devang Patel3c91b052010-03-08 20:52:55 +00001190 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001191 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001192 else
Devang Patel8a241142009-12-09 18:24:21 +00001193 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001194
Devang Patel5d11eb02009-12-03 19:11:07 +00001195 unsigned VK = SP.getVirtuality();
1196 if (VK) {
1197 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1198 DIEBlock *Block = new DIEBlock();
1199 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1200 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1201 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel622b0262010-01-19 06:19:05 +00001202 ContainingTypeMap.insert(std::make_pair(SPDie,
1203 SP.getContainingType().getNode()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001204 }
1205
Devang Patelffe966c2009-12-14 16:18:45 +00001206 if (MakeDecl || !SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001207 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001208
1209 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001210 // be handled while processing variables.
1211 DICompositeType SPTy = SP.getType();
1212 DIArray Args = SPTy.getTypeArray();
1213 unsigned SPTag = SPTy.getTag();
1214
Bill Wendling0310d762009-05-15 09:23:25 +00001215 if (SPTag == dwarf::DW_TAG_subroutine_type)
1216 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1217 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelb4645642010-02-06 01:02:37 +00001218 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1219 addType(Arg, ATy);
1220 if (ATy.isArtificial())
1221 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001222 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001223 }
1224 }
1225
Devang Patel4e0d19d2010-02-03 19:57:19 +00001226 if (SP.isArtificial())
1227 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1228
Bill Wendling0310d762009-05-15 09:23:25 +00001229 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001230 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001231 return SPDie;
1232}
1233
Devang Patel53bb5c92009-11-10 23:06:00 +00001234/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1235/// Initialize scope and update scope hierarchy.
1236DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1237 MDNode *InlinedAt) {
1238 assert (N && "Invalid Scope encoding!");
1239 assert (MI && "Missing machine instruction!");
1240 bool GetConcreteScope = (MI && InlinedAt);
1241
1242 DbgScope *NScope = NULL;
1243
1244 if (InlinedAt)
1245 NScope = DbgScopeMap.lookup(InlinedAt);
1246 else
1247 NScope = DbgScopeMap.lookup(N);
1248 assert (NScope && "Unable to find working scope!");
1249
1250 if (NScope->getFirstInsn())
1251 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001252
1253 DbgScope *Parent = NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001254 if (GetConcreteScope) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001255 DILocation IL(InlinedAt);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001256 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel53bb5c92009-11-10 23:06:00 +00001257 IL.getOrigLocation().getNode());
1258 assert (Parent && "Unable to find Parent scope!");
1259 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001260 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001261 } else if (DIDescriptor(N).isLexicalBlock()) {
1262 DILexicalBlock DB(N);
Devang Patel3c91b052010-03-08 20:52:55 +00001263 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1264 NScope->setParent(Parent);
1265 Parent->addScope(NScope);
Devang Patelc90aefe2009-10-14 21:08:09 +00001266 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001267
Devang Patelbdf45cb2009-10-27 20:47:17 +00001268 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001269
Devang Patel53bb5c92009-11-10 23:06:00 +00001270 if (!Parent && !InlinedAt) {
Devang Patel39ae3ff2009-11-11 00:31:36 +00001271 StringRef SPName = DISubprogram(N).getLinkageName();
1272 if (SPName == MF->getFunction()->getName())
1273 CurrentFnDbgScope = NScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001274 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001275
Devang Patel53bb5c92009-11-10 23:06:00 +00001276 if (GetConcreteScope) {
1277 ConcreteScopes[InlinedAt] = NScope;
1278 getOrCreateAbstractScope(N);
1279 }
1280
Devang Patelbdf45cb2009-10-27 20:47:17 +00001281 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001282}
1283
Devang Patel53bb5c92009-11-10 23:06:00 +00001284DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1285 assert (N && "Invalid Scope encoding!");
1286
1287 DbgScope *AScope = AbstractScopes.lookup(N);
1288 if (AScope)
1289 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001290
Devang Patel53bb5c92009-11-10 23:06:00 +00001291 DbgScope *Parent = NULL;
1292
1293 DIDescriptor Scope(N);
1294 if (Scope.isLexicalBlock()) {
1295 DILexicalBlock DB(N);
1296 DIDescriptor ParentDesc = DB.getContext();
Devang Patel3c91b052010-03-08 20:52:55 +00001297 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001298 }
1299
1300 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1301
1302 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001303 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001304 AScope->setAbstractScope();
1305 AbstractScopes[N] = AScope;
1306 if (DIDescriptor(N).isSubprogram())
1307 AbstractScopesList.push_back(AScope);
1308 return AScope;
1309}
Devang Patelaf9e8472009-10-01 20:31:14 +00001310
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001311/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001312/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1313/// If there are global variables in this scope then create and insert
1314/// DIEs for these variables.
1315DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001316
Devang Patel017d1212009-11-20 21:37:22 +00001317 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel53bb5c92009-11-10 23:06:00 +00001318 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patelffe966c2009-12-14 16:18:45 +00001319 DISubprogram SP(SPNode);
Devang Patel6cda22e2010-02-05 23:09:20 +00001320 // There is not any need to generate specification DIE for a function
1321 // defined at compile unit level. If a function is defined inside another
1322 // function then gdb prefers the definition at top level and but does not
1323 // expect specification DIE in parent function. So avoid creating
1324 // specification DIE for a function defined inside a function.
1325 if (SP.isDefinition() && !SP.getContext().isCompileUnit()
1326 && !SP.getContext().isSubprogram()) {
Devang Patelffe966c2009-12-14 16:18:45 +00001327 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1328 // Add arguments.
1329 DICompositeType SPTy = SP.getType();
1330 DIArray Args = SPTy.getTypeArray();
1331 unsigned SPTag = SPTy.getTag();
1332 if (SPTag == dwarf::DW_TAG_subroutine_type)
1333 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1334 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelb4645642010-02-06 01:02:37 +00001335 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1336 addType(Arg, ATy);
1337 if (ATy.isArtificial())
1338 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelffe966c2009-12-14 16:18:45 +00001339 SPDie->addChild(Arg);
1340 }
1341 DIE *SPDeclDie = SPDie;
1342 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1343 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1344 SPDeclDie);
Devang Patelffe966c2009-12-14 16:18:45 +00001345 ModuleCU->addDie(SPDie);
1346 }
Devang Patelb4645642010-02-06 01:02:37 +00001347
Devang Patel2c4ceb12009-11-21 02:48:08 +00001348 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00001349 getDWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001350 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00001351 getDWLabel("func_end", SubprogramCount));
Devang Patel53bb5c92009-11-10 23:06:00 +00001352 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001353 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001354
Devang Patel53bb5c92009-11-10 23:06:00 +00001355 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001356 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001357
Devang Patel53bb5c92009-11-10 23:06:00 +00001358 return SPDie;
1359}
1360
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001361/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001362/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1363DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001364 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1365 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1366
1367 // Ignore empty scopes.
1368 if (StartID == EndID && StartID != 0)
1369 return NULL;
1370
1371 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1372 if (Scope->isAbstractScope())
1373 return ScopeDIE;
1374
Devang Patel2c4ceb12009-11-21 02:48:08 +00001375 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00001376 StartID ? getDWLabel("label", StartID)
1377 : getDWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001378 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00001379 EndID ? getDWLabel("label", EndID)
1380 : getDWLabel("func_end", SubprogramCount));
Devang Patel53bb5c92009-11-10 23:06:00 +00001381
1382 return ScopeDIE;
1383}
1384
Devang Patel2c4ceb12009-11-21 02:48:08 +00001385/// constructInlinedScopeDIE - This scope represents inlined body of
1386/// a function. Construct DIE to represent this concrete inlined copy
1387/// of the function.
1388DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001389 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1390 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1391 assert (StartID && "Invalid starting label for an inlined scope!");
1392 assert (EndID && "Invalid end label for an inlined scope!");
1393 // Ignore empty scopes.
1394 if (StartID == EndID && StartID != 0)
1395 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001396 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001397 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001398 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001399 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1400
1401 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001402 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001403 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001404 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001405 dwarf::DW_FORM_ref4, OriginDIE);
1406
Devang Patel2c4ceb12009-11-21 02:48:08 +00001407 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00001408 getDWLabel("label", StartID));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001409 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00001410 getDWLabel("label", EndID));
Devang Patel53bb5c92009-11-10 23:06:00 +00001411
1412 InlinedSubprogramDIEs.insert(OriginDIE);
1413
1414 // Track the start label for this inlined function.
Devang Patel622b0262010-01-19 06:19:05 +00001415 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel53bb5c92009-11-10 23:06:00 +00001416 I = InlineInfo.find(InlinedSP.getNode());
1417
1418 if (I == InlineInfo.end()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001419 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1420 ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001421 InlinedSPNodes.push_back(InlinedSP.getNode());
1422 } else
1423 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1424
1425 StringPool.insert(InlinedSP.getName());
Devang Patel351ca332010-01-05 01:46:14 +00001426 StringPool.insert(getRealLinkageName(InlinedSP.getLinkageName()));
1427
Devang Patel53bb5c92009-11-10 23:06:00 +00001428 DILocation DL(Scope->getInlinedAt());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001429 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1430 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001431
1432 return ScopeDIE;
1433}
1434
Devang Patel2c4ceb12009-11-21 02:48:08 +00001435
1436/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001437DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001438 // Get the descriptor.
1439 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001440 StringRef Name = VD.getName();
1441 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001442 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001443
1444 // Translate tag to proper Dwarf tag. The result variable is dropped for
1445 // now.
1446 unsigned Tag;
1447 switch (VD.getTag()) {
1448 case dwarf::DW_TAG_return_variable:
1449 return NULL;
1450 case dwarf::DW_TAG_arg_variable:
1451 Tag = dwarf::DW_TAG_formal_parameter;
1452 break;
1453 case dwarf::DW_TAG_auto_variable: // fall thru
1454 default:
1455 Tag = dwarf::DW_TAG_variable;
1456 break;
1457 }
1458
1459 // Define variable debug information entry.
1460 DIE *VariableDie = new DIE(Tag);
1461
1462
1463 DIE *AbsDIE = NULL;
1464 if (DbgVariable *AV = DV->getAbstractVariable())
1465 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001466
Devang Patel53bb5c92009-11-10 23:06:00 +00001467 if (AbsDIE) {
1468 DIScope DS(Scope->getScopeNode());
1469 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001470 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc0326792009-11-11 03:09:50 +00001471 (void) OriginSPDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001472 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1473 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1474 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001475 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001476 dwarf::DW_FORM_ref4, AbsDIE);
1477 }
1478 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001479 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1480 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001481
1482 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001483 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001484 // addresses instead.
1485 if (VD.isBlockByrefVariable())
Devang Patel8a241142009-12-09 18:24:21 +00001486 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001487 else
Devang Patel8a241142009-12-09 18:24:21 +00001488 addType(VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001489 }
1490
1491 // Add variable address.
1492 if (!Scope->isAbstractScope()) {
1493 MachineLocation Location;
Jim Grosbacha2f20b22009-11-22 20:14:00 +00001494 unsigned FrameReg;
1495 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1496 Location.set(FrameReg, Offset);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001497
Devang Patel53bb5c92009-11-10 23:06:00 +00001498 if (VD.hasComplexAddress())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001499 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001500 else if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001501 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001502 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001503 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001504 }
Devang Patelb4645642010-02-06 01:02:37 +00001505
1506 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1507 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001508 DV->setDIE(VariableDie);
1509 return VariableDie;
1510
1511}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001512
Devang Patel193f7202009-11-24 01:14:22 +00001513void DwarfDebug::addPubTypes(DISubprogram SP) {
1514 DICompositeType SPTy = SP.getType();
1515 unsigned SPTag = SPTy.getTag();
1516 if (SPTag != dwarf::DW_TAG_subroutine_type)
1517 return;
1518
1519 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001520 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1521 DIType ATy(Args.getElement(i).getNode());
Devang Patel3c91b052010-03-08 20:52:55 +00001522 if (!ATy.isValid())
Devang Patel193f7202009-11-24 01:14:22 +00001523 continue;
1524 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel3c91b052010-03-08 20:52:55 +00001525 if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001526 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1527 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1528 }
1529 }
1530}
1531
Devang Patel2c4ceb12009-11-21 02:48:08 +00001532/// constructScopeDIE - Construct a DIE for this scope.
1533DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001534 if (!Scope || !Scope->getScopeNode())
1535 return NULL;
1536
1537 DIScope DS(Scope->getScopeNode());
1538 DIE *ScopeDIE = NULL;
1539 if (Scope->getInlinedAt())
1540 ScopeDIE = constructInlinedScopeDIE(Scope);
1541 else if (DS.isSubprogram()) {
1542 if (Scope->isAbstractScope())
1543 ScopeDIE = ModuleCU->getDIE(DS.getNode());
1544 else
1545 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
1546 }
1547 else {
1548 ScopeDIE = constructLexicalScopeDIE(Scope);
1549 if (!ScopeDIE) return NULL;
1550 }
1551
Devang Patel53bb5c92009-11-10 23:06:00 +00001552 // Add variables to scope.
Douglas Gregor1ddcf352010-03-08 02:58:37 +00001553 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001554 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001555 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001556 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001557 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001558 }
1559
1560 // Add nested scopes.
Douglas Gregor1ddcf352010-03-08 02:58:37 +00001561 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001562 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1563 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001564 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001565 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001566 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001567 }
Devang Patel193f7202009-11-24 01:14:22 +00001568
1569 if (DS.isSubprogram())
1570 addPubTypes(DISubprogram(DS.getNode()));
1571
1572 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001573}
1574
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001575/// GetOrCreateSourceID - Look up the source id with the given directory and
1576/// source file names. If none currently exists, create a new id and insert it
1577/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1578/// maps as well.
Devang Patel65dbc902009-11-25 17:36:49 +00001579unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001580 unsigned DId;
1581 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1582 if (DI != DirectoryIdMap.end()) {
1583 DId = DI->getValue();
1584 } else {
1585 DId = DirectoryNames.size() + 1;
1586 DirectoryIdMap[DirName] = DId;
1587 DirectoryNames.push_back(DirName);
1588 }
1589
1590 unsigned FId;
1591 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1592 if (FI != SourceFileIdMap.end()) {
1593 FId = FI->getValue();
1594 } else {
1595 FId = SourceFileNames.size() + 1;
1596 SourceFileIdMap[FileName] = FId;
1597 SourceFileNames.push_back(FileName);
1598 }
1599
1600 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1601 SourceIdMap.find(std::make_pair(DId, FId));
1602 if (SI != SourceIdMap.end())
1603 return SI->second;
1604
1605 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1606 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1607 SourceIds.push_back(std::make_pair(DId, FId));
1608
1609 return SrcId;
1610}
1611
Devang Patel6404e4e2009-12-15 19:16:48 +00001612/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1613DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1614 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1615 if (NDie)
1616 return NDie;
1617 NDie = new DIE(dwarf::DW_TAG_namespace);
1618 ModuleCU->insertDIE(NS.getNode(), NDie);
1619 if (!NS.getName().empty())
1620 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1621 addSourceLine(NDie, &NS);
1622 addToContextOwner(NDie, NS.getContext());
1623 return NDie;
1624}
1625
Devang Pateld037d7a2009-12-11 21:37:07 +00001626CompileUnit *DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001627 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001628 StringRef FN = DIUnit.getFilename();
1629 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001630 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001631
1632 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00001633 // FIXME: Why getting the delta between two identical labels??
Devang Patel2c4ceb12009-11-21 02:48:08 +00001634 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00001635 getTempLabel("section_line"), getTempLabel("section_line"),
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001636 false);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001637 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001638 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001639 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001640 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001641 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001642
Devang Patel65dbc902009-11-25 17:36:49 +00001643 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001644 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001645 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001646 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001647
Devang Patel65dbc902009-11-25 17:36:49 +00001648 StringRef Flags = DIUnit.getFlags();
1649 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001650 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001651
1652 unsigned RVer = DIUnit.getRunTimeVersion();
1653 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001654 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001655 dwarf::DW_FORM_data1, RVer);
1656
1657 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001658 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001659 // Use first compile unit marked as isMain as the compile unit
1660 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001661 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001662 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001663
Devang Pateld037d7a2009-12-11 21:37:07 +00001664 return Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001665}
1666
Devang Patel2c4ceb12009-11-21 02:48:08 +00001667void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001668 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001669
Devang Patel905cf5e2009-09-04 23:59:07 +00001670 // If debug information is malformed then ignore it.
1671 if (DI_GV.Verify() == false)
1672 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001673
1674 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001675 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel13e16b62009-06-26 01:49:18 +00001676 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001677
Devang Patel8a241142009-12-09 18:24:21 +00001678 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Pateledb45632009-12-10 23:25:41 +00001679 if (!VariableDie)
1680 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001681
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001682 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001683 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001684
1685 // Add to context owner.
Devang Patelc9b16cc2010-01-15 01:12:22 +00001686 DIDescriptor GVContext = DI_GV.getContext();
1687 // Do not create specification DIE if context is either compile unit
1688 // or a subprogram.
1689 if (DI_GV.isDefinition() && !GVContext.isCompileUnit()
1690 && !GVContext.isSubprogram()) {
Devang Patel6404e4e2009-12-15 19:16:48 +00001691 // Create specification DIE.
1692 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1693 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1694 dwarf::DW_FORM_ref4, VariableDie);
1695 DIEBlock *Block = new DIEBlock();
1696 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001697 addLabel(Block, 0, dwarf::DW_FORM_udata,
1698 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001699 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel8581e012010-02-09 01:58:33 +00001700 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel6404e4e2009-12-15 19:16:48 +00001701 ModuleCU->addDie(VariableSpecDIE);
1702 } else {
1703 DIEBlock *Block = new DIEBlock();
1704 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001705 addLabel(Block, 0, dwarf::DW_FORM_udata,
1706 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001707 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1708 }
Devang Patelc9b16cc2010-01-15 01:12:22 +00001709 addToContextOwner(VariableDie, GVContext);
Devang Patelc366f832009-12-10 19:14:49 +00001710
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001711 // Expose as global. FIXME - need to check external flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001712 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001713
1714 DIType GTy = DI_GV.getType();
Devang Patel65dbc902009-11-25 17:36:49 +00001715 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001716 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1717 assert (Entry && "Missing global type!");
1718 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1719 }
Devang Patel13e16b62009-06-26 01:49:18 +00001720 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001721}
1722
Devang Patel2c4ceb12009-11-21 02:48:08 +00001723void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001724 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001725
1726 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001727 if (ModuleCU->getDIE(N))
Devang Patel13e16b62009-06-26 01:49:18 +00001728 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001729
1730 if (!SP.isDefinition())
1731 // This is a method declaration which will be handled while constructing
1732 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001733 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001734
Devang Patel8a241142009-12-09 18:24:21 +00001735 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001736
1737 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001738 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001739
1740 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001741 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001742
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001743 // Expose as global.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001744 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001745
Devang Patel13e16b62009-06-26 01:49:18 +00001746 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001747}
1748
Devang Patel2c4ceb12009-11-21 02:48:08 +00001749/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001750/// content. Create global DIEs and emit initial debug info sections.
1751/// This is inovked by the target AsmPrinter.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001752void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel208622d2009-06-25 22:36:02 +00001753 this->M = M;
1754
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001755 if (TimePassesIsEnabled)
1756 DebugTimer->startTimer();
1757
Devang Patel3380cc52009-11-11 19:55:08 +00001758 if (!MAI->doesSupportDebugInformation())
1759 return;
1760
Devang Patel78ab9e22009-07-30 18:56:46 +00001761 DebugInfoFinder DbgFinder;
1762 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001763
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001764 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001765 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1766 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001767 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001768
Devang Patel1dbc7712009-06-29 20:45:18 +00001769 if (!ModuleCU)
Devang Patel77bf2952010-03-08 22:02:50 +00001770 return;
Devang Patel70f44262009-06-29 20:38:13 +00001771
Devang Patel53bb5c92009-11-10 23:06:00 +00001772 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001773 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1774 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001775 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001776
Devang Patelc366f832009-12-10 19:14:49 +00001777 // Create DIEs for each global variable.
1778 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1779 E = DbgFinder.global_variable_end(); I != E; ++I)
1780 constructGlobalVariableDIE(*I);
1781
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001782 MMI = mmi;
1783 shouldEmit = true;
1784 MMI->setDebugInfoAvailability(true);
1785
1786 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001787 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001788
1789 // Print out .file directives to specify files for .loc directives. These are
1790 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001791 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001792 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1793 // Remember source id starts at 1.
1794 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001795 // FIXME: don't use sys::path for this! This should not depend on the
1796 // host.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001797 sys::Path FullPath(getSourceDirectoryName(Id.first));
1798 bool AppendOk =
1799 FullPath.appendComponent(getSourceFileName(Id.second));
1800 assert(AppendOk && "Could not append filename to directory!");
1801 AppendOk = false;
Chris Lattnera6594fc2010-01-25 18:58:59 +00001802 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001803 }
1804 }
1805
1806 // Emit initial sections
Devang Patel2c4ceb12009-11-21 02:48:08 +00001807 emitInitial();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001808
1809 if (TimePassesIsEnabled)
1810 DebugTimer->stopTimer();
1811}
1812
Devang Patel2c4ceb12009-11-21 02:48:08 +00001813/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001814///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001815void DwarfDebug::endModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001816 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001817 return;
1818
1819 if (TimePassesIsEnabled)
1820 DebugTimer->startTimer();
1821
Devang Patel53bb5c92009-11-10 23:06:00 +00001822 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1823 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1824 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1825 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001826 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001827 }
1828
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001829 // Insert top level DIEs.
1830 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1831 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1832 ModuleCU->getCUDie()->addChild(*TI);
1833
Devang Patel622b0262010-01-19 06:19:05 +00001834 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00001835 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1836 DIE *SPDie = CI->first;
1837 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1838 if (!N) continue;
1839 DIE *NDie = ModuleCU->getDIE(N);
1840 if (!NDie) continue;
1841 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patelf8b72ca2010-01-15 00:26:31 +00001842 // FIXME - This is not the correct approach.
1843 // addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00001844 }
1845
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001846 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001847 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00001848 Asm->OutStreamer.EmitLabel(getTempLabel("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001849 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00001850 Asm->OutStreamer.EmitLabel(getTempLabel("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001851
1852 // End text sections.
1853 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001854 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerf829eef2010-03-08 22:44:40 +00001855 Asm->OutStreamer.EmitLabel(getDWLabel("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001856 }
1857
1858 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001859 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001860
1861 // Emit function debug frame information
1862 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1863 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001864 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001865
1866 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001867 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001868
1869 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001870 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001871
1872 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001873 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001874
1875 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001876 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001877
1878 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001879 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001880
Devang Patel193f7202009-11-24 01:14:22 +00001881 // Emit info into a debug pubtypes section.
1882 emitDebugPubTypes();
1883
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001884 // Emit info into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001885 emitDebugStr();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001886
1887 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001888 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001889
1890 // Emit info into a debug aranges section.
1891 EmitDebugARanges();
1892
1893 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001894 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001895
1896 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001897 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001898
1899 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001900 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001901
1902 if (TimePassesIsEnabled)
1903 DebugTimer->stopTimer();
1904}
1905
Devang Patel53bb5c92009-11-10 23:06:00 +00001906/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001907DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1908 unsigned FrameIdx,
Devang Patel53bb5c92009-11-10 23:06:00 +00001909 DILocation &ScopeLoc) {
1910
1911 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1912 if (AbsDbgVariable)
1913 return AbsDbgVariable;
1914
1915 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1916 if (!Scope)
1917 return NULL;
1918
1919 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001920 Scope->addVariable(AbsDbgVariable);
Devang Patel53bb5c92009-11-10 23:06:00 +00001921 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1922 return AbsDbgVariable;
1923}
1924
Devang Patel2c4ceb12009-11-21 02:48:08 +00001925/// collectVariableInfo - Populate DbgScope entries with variables' info.
1926void DwarfDebug::collectVariableInfo() {
Devang Patelac1ceb32009-10-09 22:42:28 +00001927 if (!MMI) return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001928
Devang Patele717faa2009-10-06 01:26:37 +00001929 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1930 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1931 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelbc5201f2010-01-22 22:52:10 +00001932 MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001933 if (!Var) continue;
Devang Pateleda31212009-10-08 18:48:03 +00001934 DIVariable DV (Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001935 std::pair< unsigned, MDNode *> VP = VI->second;
1936 DILocation ScopeLoc(VP.second);
1937
1938 DbgScope *Scope =
1939 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1940 if (!Scope)
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001941 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelfb0ee432009-11-10 23:20:04 +00001942 // If variable scope is not found then skip this variable.
1943 if (!Scope)
1944 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001945
1946 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001947 Scope->addVariable(RegVar);
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001948 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1949 ScopeLoc))
Devang Patel53bb5c92009-11-10 23:06:00 +00001950 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patele717faa2009-10-06 01:26:37 +00001951 }
1952}
1953
Devang Patel2c4ceb12009-11-21 02:48:08 +00001954/// beginScope - Process beginning of a scope starting at Label.
1955void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001956 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1957 if (I == DbgScopeBeginMap.end())
1958 return;
Dan Gohman277207e2009-11-23 21:30:55 +00001959 ScopeVector &SD = I->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001960 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001961 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001962 (*SDI)->setStartLabelID(Label);
1963}
1964
Devang Patel2c4ceb12009-11-21 02:48:08 +00001965/// endScope - Process end of a scope.
1966void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001967 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00001968 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00001969 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001970
1971 unsigned Label = MMI->NextLabelID();
1972 Asm->printLabel(Label);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001973 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001974
Devang Patel0d20ac82009-10-06 01:50:42 +00001975 SmallVector<DbgScope *, 2> &SD = I->second;
1976 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001977 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001978 (*SDI)->setEndLabelID(Label);
Devang Patel53bb5c92009-11-10 23:06:00 +00001979 return;
1980}
1981
1982/// createDbgScope - Create DbgScope for the scope.
1983void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1984
1985 if (!InlinedAt) {
1986 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1987 if (WScope)
1988 return;
1989 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1990 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001991 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel2f105c62009-11-11 00:18:40 +00001992 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001993 return;
1994 }
1995
1996 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1997 if (WScope)
1998 return;
1999
2000 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2001 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2002 DILocation DL(InlinedAt);
2003 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel0d20ac82009-10-06 01:50:42 +00002004}
2005
Devang Patel2c4ceb12009-11-21 02:48:08 +00002006/// extractScopeInformation - Scan machine instructions in this function
Devang Patelaf9e8472009-10-01 20:31:14 +00002007/// and collect DbgScopes. Return true, if atleast one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002008bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002009 // If scope information was extracted using .dbg intrinsics then there is not
2010 // any need to extract these information by scanning each instruction.
2011 if (!DbgScopeMap.empty())
2012 return false;
2013
Devang Patel344130e2010-01-04 20:44:00 +00002014 DenseMap<const MachineInstr *, unsigned> MIIndexMap;
2015 unsigned MIIndex = 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00002016 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelaf9e8472009-10-01 20:31:14 +00002017 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2018 I != E; ++I) {
2019 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2020 II != IE; ++II) {
2021 const MachineInstr *MInsn = II;
Devang Patel344130e2010-01-04 20:44:00 +00002022 MIIndexMap[MInsn] = MIIndex++;
Devang Patelaf9e8472009-10-01 20:31:14 +00002023 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00002024 if (DL.isUnknown()) continue;
Devang Patel6b61f582010-01-16 06:09:35 +00002025 DILocation DLT = MF->getDILocation(DL);
2026 DIScope DLTScope = DLT.getScope();
Devang Patelaf9e8472009-10-01 20:31:14 +00002027 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002028 // other scopes, create one DbgScope now. This will be translated
Devang Patelaf9e8472009-10-01 20:31:14 +00002029 // into a scope DIE at the end.
Devang Patel6b61f582010-01-16 06:09:35 +00002030 if (DLTScope.isCompileUnit()) continue;
2031 createDbgScope(DLTScope.getNode(), DLT.getOrigLocation().getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00002032 }
2033 }
2034
2035
2036 // Build scope hierarchy using working set of scopes.
2037 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2038 I != E; ++I) {
2039 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2040 II != IE; ++II) {
2041 const MachineInstr *MInsn = II;
2042 DebugLoc DL = MInsn->getDebugLoc();
2043 if (DL.isUnknown()) continue;
Devang Patel6b61f582010-01-16 06:09:35 +00002044 DILocation DLT = MF->getDILocation(DL);
2045 DIScope DLTScope = DLT.getScope();
Devang Patel53bb5c92009-11-10 23:06:00 +00002046 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002047 // other scopes, create one DbgScope now. This will be translated
Devang Patel53bb5c92009-11-10 23:06:00 +00002048 // into a scope DIE at the end.
Devang Patel6b61f582010-01-16 06:09:35 +00002049 if (DLTScope.isCompileUnit()) continue;
2050 DbgScope *Scope = getUpdatedDbgScope(DLTScope.getNode(), MInsn,
2051 DLT.getOrigLocation().getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00002052 Scope->setLastInsn(MInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +00002053 }
2054 }
2055
Devang Patel344130e2010-01-04 20:44:00 +00002056 if (!CurrentFnDbgScope)
2057 return false;
2058
2059 CurrentFnDbgScope->fixInstructionMarkers(MIIndexMap);
Devang Patelaf9e8472009-10-01 20:31:14 +00002060
2061 // Each scope has first instruction and last instruction to mark beginning
2062 // and end of a scope respectively. Create an inverse map that list scopes
2063 // starts (and ends) with an instruction. One instruction may start (or end)
Devang Patel42aafd72010-01-20 02:05:23 +00002064 // multiple scopes. Ignore scopes that are not reachable.
2065 SmallVector<DbgScope *, 4> WorkList;
2066 WorkList.push_back(CurrentFnDbgScope);
2067 while (!WorkList.empty()) {
2068 DbgScope *S = WorkList.back(); WorkList.pop_back();
2069
Douglas Gregor1ddcf352010-03-08 02:58:37 +00002070 SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patel42aafd72010-01-20 02:05:23 +00002071 if (!Children.empty())
Douglas Gregor1ddcf352010-03-08 02:58:37 +00002072 for (SmallVector<DbgScope *, 4>::iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002073 SE = Children.end(); SI != SE; ++SI)
2074 WorkList.push_back(*SI);
2075
Devang Patel53bb5c92009-11-10 23:06:00 +00002076 if (S->isAbstractScope())
2077 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002078 const MachineInstr *MI = S->getFirstInsn();
2079 assert (MI && "DbgScope does not have first instruction!");
2080
2081 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2082 if (IDI != DbgScopeBeginMap.end())
2083 IDI->second.push_back(S);
2084 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002085 DbgScopeBeginMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002086
2087 MI = S->getLastInsn();
2088 assert (MI && "DbgScope does not have last instruction!");
2089 IDI = DbgScopeEndMap.find(MI);
2090 if (IDI != DbgScopeEndMap.end())
2091 IDI->second.push_back(S);
2092 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002093 DbgScopeEndMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002094 }
2095
2096 return !DbgScopeMap.empty();
2097}
2098
Devang Patel2c4ceb12009-11-21 02:48:08 +00002099/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002100/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002101void DwarfDebug::beginFunction(const MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002102 this->MF = MF;
2103
2104 if (!ShouldEmitDwarfDebug()) return;
2105
2106 if (TimePassesIsEnabled)
2107 DebugTimer->startTimer();
2108
Chris Lattnereec791a2010-01-26 23:18:02 +00002109 if (!extractScopeInformation())
Devang Patel60b35bd2009-10-06 18:37:31 +00002110 return;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002111
2112 collectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002113
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002114 // Assumes in correct section after the entry point.
Chris Lattnerf829eef2010-03-08 22:44:40 +00002115 Asm->OutStreamer.EmitLabel(getDWLabel("func_begin", ++SubprogramCount));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002116
2117 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2118 // function.
Devang Patelac1ceb32009-10-09 22:42:28 +00002119 DebugLoc FDL = MF->getDefaultDebugLoc();
2120 if (!FDL.isUnknown()) {
Devang Patel6b61f582010-01-16 06:09:35 +00002121 DILocation DLT = MF->getDILocation(FDL);
Devang Patelac1ceb32009-10-09 22:42:28 +00002122 unsigned LabelID = 0;
Devang Patel6b61f582010-01-16 06:09:35 +00002123 DISubprogram SP = getDISubprogram(DLT.getScope().getNode());
Devang Patel3c91b052010-03-08 20:52:55 +00002124 if (SP.Verify())
Devang Patel6b61f582010-01-16 06:09:35 +00002125 LabelID = recordSourceLine(SP.getLineNumber(), 0,
2126 DLT.getScope().getNode());
Devang Patelac1ceb32009-10-09 22:42:28 +00002127 else
Devang Patel6b61f582010-01-16 06:09:35 +00002128 LabelID = recordSourceLine(DLT.getLineNumber(),
2129 DLT.getColumnNumber(),
2130 DLT.getScope().getNode());
Devang Patelac1ceb32009-10-09 22:42:28 +00002131 Asm->printLabel(LabelID);
2132 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002133 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002134 if (TimePassesIsEnabled)
2135 DebugTimer->stopTimer();
2136}
2137
Devang Patel2c4ceb12009-11-21 02:48:08 +00002138/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002139///
Chris Lattnereec791a2010-01-26 23:18:02 +00002140void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002141 if (!ShouldEmitDwarfDebug()) return;
2142
2143 if (TimePassesIsEnabled)
2144 DebugTimer->startTimer();
2145
Devang Patelac1ceb32009-10-09 22:42:28 +00002146 if (DbgScopeMap.empty())
2147 return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002148
Devang Patel344130e2010-01-04 20:44:00 +00002149 if (CurrentFnDbgScope) {
2150 // Define end label for subprogram.
Chris Lattnerf829eef2010-03-08 22:44:40 +00002151 Asm->OutStreamer.EmitLabel(getDWLabel("func_end", SubprogramCount));
Devang Patel344130e2010-01-04 20:44:00 +00002152
2153 // Get function line info.
2154 if (!Lines.empty()) {
2155 // Get section line info.
2156 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2157 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2158 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2159 // Append the function info to section info.
2160 SectionLineInfos.insert(SectionLineInfos.end(),
2161 Lines.begin(), Lines.end());
2162 }
2163
2164 // Construct abstract scopes.
2165 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2166 AE = AbstractScopesList.end(); AI != AE; ++AI)
2167 constructScopeDIE(*AI);
2168
2169 constructScopeDIE(CurrentFnDbgScope);
2170
2171 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2172 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002173 }
2174
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002175 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002176 CurrentFnDbgScope = NULL;
Douglas Gregor1ddcf352010-03-08 02:58:37 +00002177 DbgScopeMap.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002178 DbgScopeBeginMap.clear();
2179 DbgScopeEndMap.clear();
2180 ConcreteScopes.clear();
2181 AbstractScopesList.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002182 Lines.clear();
Devang Patelc09ddc12009-12-01 18:13:48 +00002183
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002184 if (TimePassesIsEnabled)
2185 DebugTimer->stopTimer();
2186}
2187
Devang Patel2c4ceb12009-11-21 02:48:08 +00002188/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002189/// label. Returns a unique label ID used to generate a label and provide
2190/// correspondence to the source line list.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002191unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002192 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002193 if (!MMI)
2194 return 0;
2195
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002196 if (TimePassesIsEnabled)
2197 DebugTimer->startTimer();
2198
Devang Patel65dbc902009-11-25 17:36:49 +00002199 StringRef Dir;
2200 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002201
2202 DIDescriptor Scope(S);
2203 if (Scope.isCompileUnit()) {
2204 DICompileUnit CU(S);
2205 Dir = CU.getDirectory();
2206 Fn = CU.getFilename();
2207 } else if (Scope.isSubprogram()) {
2208 DISubprogram SP(S);
2209 Dir = SP.getDirectory();
2210 Fn = SP.getFilename();
2211 } else if (Scope.isLexicalBlock()) {
2212 DILexicalBlock DB(S);
2213 Dir = DB.getDirectory();
2214 Fn = DB.getFilename();
2215 } else
2216 assert (0 && "Unexpected scope info");
2217
2218 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002219 unsigned ID = MMI->NextLabelID();
2220 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2221
2222 if (TimePassesIsEnabled)
2223 DebugTimer->stopTimer();
2224
2225 return ID;
2226}
2227
2228/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2229/// timed. Look up the source id with the given directory and source file
2230/// names. If none currently exists, create a new id and insert it in the
2231/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2232/// well.
2233unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2234 const std::string &FileName) {
2235 if (TimePassesIsEnabled)
2236 DebugTimer->startTimer();
2237
Devang Patel5ccdd102009-09-29 18:40:58 +00002238 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002239
2240 if (TimePassesIsEnabled)
2241 DebugTimer->stopTimer();
2242
2243 return SrcId;
2244}
2245
Bill Wendling829e67b2009-05-20 23:22:40 +00002246//===----------------------------------------------------------------------===//
2247// Emit Methods
2248//===----------------------------------------------------------------------===//
2249
Devang Patel2c4ceb12009-11-21 02:48:08 +00002250/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002251///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002252unsigned
2253DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002254 // Get the children.
2255 const std::vector<DIE *> &Children = Die->getChildren();
2256
2257 // If not last sibling and has children then add sibling offset attribute.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002258 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002259
2260 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002261 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002262
2263 // Get the abbreviation for this DIE.
2264 unsigned AbbrevNumber = Die->getAbbrevNumber();
2265 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2266
2267 // Set DIE offset
2268 Die->setOffset(Offset);
2269
2270 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002271 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002272
2273 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2274 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2275
2276 // Size the DIE attribute values.
2277 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2278 // Size attribute value.
2279 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2280
2281 // Size the DIE children if any.
2282 if (!Children.empty()) {
2283 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2284 "Children flag not set");
2285
2286 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002287 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002288
2289 // End of children marker.
2290 Offset += sizeof(int8_t);
2291 }
2292
2293 Die->setSize(Offset - Die->getOffset());
2294 return Offset;
2295}
2296
Devang Patel2c4ceb12009-11-21 02:48:08 +00002297/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002298///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002299void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002300 // Compute size of compile unit header.
2301 static unsigned Offset =
2302 sizeof(int32_t) + // Length of Compilation Unit Info
2303 sizeof(int16_t) + // DWARF version number
2304 sizeof(int32_t) + // Offset Into Abbrev. Section
2305 sizeof(int8_t); // Pointer Size (in bytes)
2306
Devang Patel2c4ceb12009-11-21 02:48:08 +00002307 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel1dbc7712009-06-29 20:45:18 +00002308 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002309}
2310
Devang Patel2c4ceb12009-11-21 02:48:08 +00002311/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling94d04b82009-05-20 23:21:38 +00002312/// tools to recognize the object file contains Dwarf information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002313void DwarfDebug::emitInitial() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002314 // Check to see if we already emitted intial headers.
2315 if (didInitial) return;
2316 didInitial = true;
2317
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002318 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002319
Bill Wendling94d04b82009-05-20 23:21:38 +00002320 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002321 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002322 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002323 Asm->OutStreamer.EmitLabel(getTempLabel("section_debug_frame"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002324 }
2325
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002326 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002327 Asm->OutStreamer.EmitLabel(getTempLabel("section_info"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002328 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002329 Asm->OutStreamer.EmitLabel(getTempLabel("section_abbrev"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002330 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002331 Asm->OutStreamer.EmitLabel(getTempLabel("section_aranges"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002332
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002333 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2334 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Chris Lattnerf829eef2010-03-08 22:44:40 +00002335 Asm->OutStreamer.EmitLabel(getTempLabel("section_macinfo"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002336 }
2337
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002338 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002339 Asm->OutStreamer.EmitLabel(getTempLabel("section_line"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002340 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002341 Asm->OutStreamer.EmitLabel(getTempLabel("section_loc"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002342 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002343 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubnames"));
Devang Patel193f7202009-11-24 01:14:22 +00002344 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002345 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubtypes"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002346 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002347 Asm->OutStreamer.EmitLabel(getTempLabel("section_str"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002348 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002349 Asm->OutStreamer.EmitLabel(getTempLabel("section_ranges"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002350
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002351 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002352 Asm->OutStreamer.EmitLabel(getTempLabel("text_begin"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002353 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Chris Lattnerf829eef2010-03-08 22:44:40 +00002354 Asm->OutStreamer.EmitLabel(getTempLabel("data_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002355}
2356
Devang Patel2c4ceb12009-11-21 02:48:08 +00002357/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002358///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002359void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002360 // Get the abbreviation for this DIE.
2361 unsigned AbbrevNumber = Die->getAbbrevNumber();
2362 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2363
Chris Lattner0ad9c912010-01-22 22:09:00 +00002364 Asm->O << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002365
2366 // Emit the code (index) for the abbreviation.
Chris Lattner894d75a2010-01-22 23:18:42 +00002367 if (Asm->VerboseAsm)
2368 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2369 Twine::utohexstr(Die->getOffset()) + ":0x" +
2370 Twine::utohexstr(Die->getSize()) + " " +
2371 dwarf::TagString(Abbrev->getTag()));
2372 EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002373
2374 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2375 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2376
2377 // Emit the DIE attribute values.
2378 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2379 unsigned Attr = AbbrevData[i].getAttribute();
2380 unsigned Form = AbbrevData[i].getForm();
2381 assert(Form && "Too many attributes for DIE (check abbreviation)");
2382
Chris Lattnera8013622010-01-24 18:54:17 +00002383 if (Asm->VerboseAsm)
2384 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2385
Bill Wendling94d04b82009-05-20 23:21:38 +00002386 switch (Attr) {
2387 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002388 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002389 break;
2390 case dwarf::DW_AT_abstract_origin: {
2391 DIEEntry *E = cast<DIEEntry>(Values[i]);
2392 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002393 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002394 Asm->EmitInt32(Addr);
2395 break;
2396 }
2397 default:
2398 // Emit an attribute using the defined form.
2399 Values[i]->EmitValue(this, Form);
Chris Lattner3586e5e2010-01-24 19:01:06 +00002400 O << "\n"; // REMOVE This once all EmitValue impls emit their own newline.
Bill Wendling94d04b82009-05-20 23:21:38 +00002401 break;
2402 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002403 }
2404
2405 // Emit the DIE children if any.
2406 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2407 const std::vector<DIE *> &Children = Die->getChildren();
2408
2409 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002410 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002411
Chris Lattnerfaca5492010-01-22 23:47:11 +00002412 Asm->EmitInt8(0); EOL("End Of Children Mark");
Bill Wendling94d04b82009-05-20 23:21:38 +00002413 }
2414}
2415
Devang Patel8a241142009-12-09 18:24:21 +00002416/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002417///
Devang Patel8a241142009-12-09 18:24:21 +00002418void DwarfDebug::emitDebugInfo() {
2419 // Start debug info section.
2420 Asm->OutStreamer.SwitchSection(
2421 Asm->getObjFileLowering().getDwarfInfoSection());
2422 DIE *Die = ModuleCU->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002423
2424 // Emit the compile units header.
Chris Lattnerf829eef2010-03-08 22:44:40 +00002425 Asm->OutStreamer.EmitLabel(getDWLabel("info_begin", ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002426
2427 // Emit size of content not including length itself
2428 unsigned ContentSize = Die->getSize() +
2429 sizeof(int16_t) + // DWARF version number
2430 sizeof(int32_t) + // Offset Into Abbrev. Section
2431 sizeof(int8_t) + // Pointer Size (in bytes)
2432 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2433
Chris Lattnerfaca5492010-01-22 23:47:11 +00002434 Asm->EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2435 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF version number");
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00002436 EmitSectionOffset(getTempLabel("abbrev_begin"),getTempLabel("section_abbrev"),
2437 true, false);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002438 EOL("Offset Into Abbrev. Section");
2439 Asm->EmitInt8(TD->getPointerSize()); EOL("Address Size (in bytes)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002440
Devang Patel2c4ceb12009-11-21 02:48:08 +00002441 emitDIE(Die);
Bill Wendling94d04b82009-05-20 23:21:38 +00002442 // FIXME - extra padding for gdb bug.
Chris Lattnerfaca5492010-01-22 23:47:11 +00002443 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2444 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2445 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2446 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
Chris Lattnerf829eef2010-03-08 22:44:40 +00002447 Asm->OutStreamer.EmitLabel(getDWLabel("info_end", ModuleCU->getID()));
Chris Lattner0ad9c912010-01-22 22:09:00 +00002448 Asm->O << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002449}
2450
Devang Patel2c4ceb12009-11-21 02:48:08 +00002451/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002452///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002453void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002454 // Check to see if it is worth the effort.
2455 if (!Abbreviations.empty()) {
2456 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002457 Asm->OutStreamer.SwitchSection(
2458 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002459
Chris Lattnerf829eef2010-03-08 22:44:40 +00002460 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002461
2462 // For each abbrevation.
2463 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2464 // Get abbreviation data
2465 const DIEAbbrev *Abbrev = Abbreviations[i];
2466
2467 // Emit the abbrevations code (base 1 index.)
Chris Lattner894d75a2010-01-22 23:18:42 +00002468 EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002469
2470 // Emit the abbreviations data.
Chris Lattner894d75a2010-01-22 23:18:42 +00002471 Abbrev->Emit(this);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002472 Asm->O << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002473 }
2474
2475 // Mark end of abbreviations.
Chris Lattner894d75a2010-01-22 23:18:42 +00002476 EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002477
Chris Lattnerf829eef2010-03-08 22:44:40 +00002478 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002479 }
2480}
2481
Devang Patel2c4ceb12009-11-21 02:48:08 +00002482/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002483/// the line matrix.
2484///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002485void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002486 // Define last address of section.
Chris Lattnerfaca5492010-01-22 23:47:11 +00002487 Asm->EmitInt8(0); EOL("Extended Op");
2488 Asm->EmitInt8(TD->getPointerSize() + 1); EOL("Op size");
2489 Asm->EmitInt8(dwarf::DW_LNE_set_address); EOL("DW_LNE_set_address");
Chris Lattnerc7249da2010-03-08 22:47:57 +00002490 EmitReference(getDWLabel("section_end", SectionEnd));
2491 EOL("Section end label");
Bill Wendling94d04b82009-05-20 23:21:38 +00002492
2493 // Mark end of matrix.
Chris Lattnerfaca5492010-01-22 23:47:11 +00002494 Asm->EmitInt8(0); EOL("DW_LNE_end_sequence");
Chris Lattner0ad9c912010-01-22 22:09:00 +00002495 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002496 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002497}
2498
Devang Patel2c4ceb12009-11-21 02:48:08 +00002499/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002500///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002501void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002502 // If the target is using .loc/.file, the assembler will be emitting the
2503 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002504 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002505 return;
2506
2507 // Minimum line delta, thus ranging from -10..(255-10).
2508 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2509 // Maximum line delta, thus ranging from -10..(255-10).
2510 const int MaxLineDelta = 255 + MinLineDelta;
2511
2512 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002513 Asm->OutStreamer.SwitchSection(
2514 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002515
2516 // Construct the section header.
Chris Lattnerf829eef2010-03-08 22:44:40 +00002517 EmitDifference(getTempLabel("line_end"), getTempLabel("line_begin"), true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002518 EOL("Length of Source Line Info");
Chris Lattnerf829eef2010-03-08 22:44:40 +00002519 Asm->OutStreamer.EmitLabel(getTempLabel("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002520
Chris Lattnerfaca5492010-01-22 23:47:11 +00002521 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF version number");
Bill Wendling94d04b82009-05-20 23:21:38 +00002522
Chris Lattnerf829eef2010-03-08 22:44:40 +00002523 EmitDifference(getTempLabel("line_prolog_end"),
2524 getTempLabel("line_prolog_begin"), true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002525 EOL("Prolog Length");
Chris Lattnerf829eef2010-03-08 22:44:40 +00002526 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002527
Chris Lattnerfaca5492010-01-22 23:47:11 +00002528 Asm->EmitInt8(1); EOL("Minimum Instruction Length");
2529 Asm->EmitInt8(1); EOL("Default is_stmt_start flag");
2530 Asm->EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2531 Asm->EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2532 Asm->EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Bill Wendling94d04b82009-05-20 23:21:38 +00002533
2534 // Line number standard opcode encodings argument count
Chris Lattnerfaca5492010-01-22 23:47:11 +00002535 Asm->EmitInt8(0); EOL("DW_LNS_copy arg count");
2536 Asm->EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2537 Asm->EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2538 Asm->EmitInt8(1); EOL("DW_LNS_set_file arg count");
2539 Asm->EmitInt8(1); EOL("DW_LNS_set_column arg count");
2540 Asm->EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2541 Asm->EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2542 Asm->EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2543 Asm->EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002544
2545 // Emit directories.
2546 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00002547 const std::string &Dir = getSourceDirectoryName(DI);
2548 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Directory");
2549 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002550 }
2551
Chris Lattnerfaca5492010-01-22 23:47:11 +00002552 Asm->EmitInt8(0); EOL("End of directories");
Bill Wendling94d04b82009-05-20 23:21:38 +00002553
2554 // Emit files.
2555 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2556 // Remember source id starts at 1.
2557 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00002558 const std::string &FN = getSourceFileName(Id.second);
2559 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Source");
2560 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2561
Chris Lattner894d75a2010-01-22 23:18:42 +00002562 EmitULEB128(Id.first, "Directory #");
2563 EmitULEB128(0, "Mod date");
2564 EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00002565 }
2566
Chris Lattnerfaca5492010-01-22 23:47:11 +00002567 Asm->EmitInt8(0); EOL("End of files");
Bill Wendling94d04b82009-05-20 23:21:38 +00002568
Chris Lattnerf829eef2010-03-08 22:44:40 +00002569 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002570
2571 // A sequence for each text section.
2572 unsigned SecSrcLinesSize = SectionSourceLines.size();
2573
2574 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2575 // Isolate current sections line info.
2576 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2577
Chris Lattner93b6db32009-08-08 23:39:42 +00002578 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002579 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002580 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002581 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002582 }*/
Chris Lattner0ad9c912010-01-22 22:09:00 +00002583 Asm->O << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002584
2585 // Dwarf assumes we start with first line of first source file.
2586 unsigned Source = 1;
2587 unsigned Line = 1;
2588
2589 // Construct rows of the address, source, line, column matrix.
2590 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2591 const SrcLineInfo &LineInfo = LineInfos[i];
2592 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2593 if (!LabelID) continue;
2594
Caroline Ticec6f9d622009-09-11 18:25:54 +00002595 if (LineInfo.getLine() == 0) continue;
2596
Bill Wendling94d04b82009-05-20 23:21:38 +00002597 if (!Asm->isVerbose())
Chris Lattner0ad9c912010-01-22 22:09:00 +00002598 Asm->O << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002599 else {
2600 std::pair<unsigned, unsigned> SourceID =
2601 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002602 O << '\t' << MAI->getCommentString() << ' '
Dan Gohmanb3b98212009-12-05 02:00:34 +00002603 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling94d04b82009-05-20 23:21:38 +00002604 << getSourceFileName(SourceID.second)
Dan Gohmanb3b98212009-12-05 02:00:34 +00002605 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002606 }
2607
2608 // Define the line address.
Chris Lattnerfaca5492010-01-22 23:47:11 +00002609 Asm->EmitInt8(0); EOL("Extended Op");
2610 Asm->EmitInt8(TD->getPointerSize() + 1); EOL("Op size");
2611 Asm->EmitInt8(dwarf::DW_LNE_set_address); EOL("DW_LNE_set_address");
Chris Lattnerc7249da2010-03-08 22:47:57 +00002612 EmitReference(getDWLabel("label", LabelID)); EOL("Location label");
Bill Wendling94d04b82009-05-20 23:21:38 +00002613
2614 // If change of source, then switch to the new source.
2615 if (Source != LineInfo.getSourceID()) {
2616 Source = LineInfo.getSourceID();
Chris Lattnerfaca5492010-01-22 23:47:11 +00002617 Asm->EmitInt8(dwarf::DW_LNS_set_file); EOL("DW_LNS_set_file");
Chris Lattner894d75a2010-01-22 23:18:42 +00002618 EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00002619 }
2620
2621 // If change of line.
2622 if (Line != LineInfo.getLine()) {
2623 // Determine offset.
2624 int Offset = LineInfo.getLine() - Line;
2625 int Delta = Offset - MinLineDelta;
2626
2627 // Update line.
2628 Line = LineInfo.getLine();
2629
2630 // If delta is small enough and in range...
2631 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2632 // ... then use fast opcode.
Chris Lattnerfaca5492010-01-22 23:47:11 +00002633 Asm->EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Bill Wendling94d04b82009-05-20 23:21:38 +00002634 } else {
2635 // ... otherwise use long hand.
2636 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002637 EOL("DW_LNS_advance_line");
Chris Lattnerbb9078a2010-01-22 22:56:55 +00002638 EmitSLEB128(Offset, "Line Offset");
Chris Lattnerfaca5492010-01-22 23:47:11 +00002639 Asm->EmitInt8(dwarf::DW_LNS_copy); EOL("DW_LNS_copy");
Bill Wendling94d04b82009-05-20 23:21:38 +00002640 }
2641 } else {
2642 // Copy the previous row (different address or source)
Chris Lattnerfaca5492010-01-22 23:47:11 +00002643 Asm->EmitInt8(dwarf::DW_LNS_copy); EOL("DW_LNS_copy");
Bill Wendling94d04b82009-05-20 23:21:38 +00002644 }
2645 }
2646
Devang Patel2c4ceb12009-11-21 02:48:08 +00002647 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002648 }
2649
2650 if (SecSrcLinesSize == 0)
2651 // Because we're emitting a debug_line section, we still need a line
2652 // table. The linker and friends expect it to exist. If there's nothing to
2653 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002654 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002655
Chris Lattnerf829eef2010-03-08 22:44:40 +00002656 Asm->OutStreamer.EmitLabel(getTempLabel("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002657}
2658
Devang Patel2c4ceb12009-11-21 02:48:08 +00002659/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002660///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002661void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002662 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002663 return;
2664
2665 int stackGrowth =
2666 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2667 TargetFrameInfo::StackGrowsUp ?
2668 TD->getPointerSize() : -TD->getPointerSize();
2669
2670 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002671 Asm->OutStreamer.SwitchSection(
2672 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002673
Chris Lattnerf829eef2010-03-08 22:44:40 +00002674 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common"));
Chris Lattnereffa8682010-03-08 23:02:59 +00002675 EmitDifference(getTempLabel("debug_frame_common_end"),
2676 getTempLabel("debug_frame_common_begin"), true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002677 EOL("Length of Common Information Entry");
Bill Wendling94d04b82009-05-20 23:21:38 +00002678
Chris Lattnerf829eef2010-03-08 22:44:40 +00002679 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002680 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002681 EOL("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00002682 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002683 EOL("CIE Version");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002684 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattnerfaca5492010-01-22 23:47:11 +00002685 EOL("CIE Augmentation");
Chris Lattner894d75a2010-01-22 23:18:42 +00002686 EmitULEB128(1, "CIE Code Alignment Factor");
Chris Lattnerbb9078a2010-01-22 22:56:55 +00002687 EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Bill Wendling94d04b82009-05-20 23:21:38 +00002688 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Chris Lattnerfaca5492010-01-22 23:47:11 +00002689 EOL("CIE RA Column");
Bill Wendling94d04b82009-05-20 23:21:38 +00002690
2691 std::vector<MachineMove> Moves;
2692 RI->getInitialFrameState(Moves);
2693
2694 EmitFrameMoves(NULL, 0, Moves, false);
2695
2696 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattnerf829eef2010-03-08 22:44:40 +00002697 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002698}
2699
Devang Patel2c4ceb12009-11-21 02:48:08 +00002700/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00002701/// section.
2702void
Devang Patel2c4ceb12009-11-21 02:48:08 +00002703DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002704 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002705 return;
2706
2707 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002708 Asm->OutStreamer.SwitchSection(
2709 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002710
Chris Lattnereffa8682010-03-08 23:02:59 +00002711 EmitDifference(getDWLabel("debug_frame_end", DebugFrameInfo.Number),
2712 getDWLabel("debug_frame_begin", DebugFrameInfo.Number), true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002713 EOL("Length of Frame Information Entry");
Bill Wendling94d04b82009-05-20 23:21:38 +00002714
Chris Lattnerf829eef2010-03-08 22:44:40 +00002715 Asm->OutStreamer.EmitLabel(getDWLabel("debug_frame_begin",
2716 DebugFrameInfo.Number));
Bill Wendling94d04b82009-05-20 23:21:38 +00002717
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00002718 EmitSectionOffset(getTempLabel("debug_frame_common"),
2719 getTempLabel("section_debug_frame"), true, false);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002720 EOL("FDE CIE offset");
Bill Wendling94d04b82009-05-20 23:21:38 +00002721
Chris Lattnerc7249da2010-03-08 22:47:57 +00002722 EmitReference(getDWLabel("func_begin", DebugFrameInfo.Number));
Chris Lattnerfaca5492010-01-22 23:47:11 +00002723 EOL("FDE initial location");
Chris Lattnerc7249da2010-03-08 22:47:57 +00002724 EmitDifference(getDWLabel("func_end", DebugFrameInfo.Number),
2725 getDWLabel("func_begin", DebugFrameInfo.Number));
Chris Lattnerfaca5492010-01-22 23:47:11 +00002726 EOL("FDE address range");
Bill Wendling94d04b82009-05-20 23:21:38 +00002727
2728 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2729 false);
2730
2731 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattnerf829eef2010-03-08 22:44:40 +00002732 Asm->OutStreamer.EmitLabel(getDWLabel("debug_frame_end",
2733 DebugFrameInfo.Number));
Bill Wendling94d04b82009-05-20 23:21:38 +00002734}
2735
Devang Patel8a241142009-12-09 18:24:21 +00002736/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2737///
2738void DwarfDebug::emitDebugPubNames() {
2739 // Start the dwarf pubnames section.
2740 Asm->OutStreamer.SwitchSection(
2741 Asm->getObjFileLowering().getDwarfPubNamesSection());
2742
Chris Lattnereffa8682010-03-08 23:02:59 +00002743 EmitDifference(getDWLabel("pubnames_end", ModuleCU->getID()),
2744 getDWLabel("pubnames_begin", ModuleCU->getID()), true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002745 EOL("Length of Public Names Info");
Bill Wendling94d04b82009-05-20 23:21:38 +00002746
Chris Lattnerf829eef2010-03-08 22:44:40 +00002747 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_begin", ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002748
Chris Lattnerfaca5492010-01-22 23:47:11 +00002749 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00002750
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00002751 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2752 getTempLabel("section_info"),
2753 true, false);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002754 EOL("Offset of Compilation Unit Info");
Bill Wendling94d04b82009-05-20 23:21:38 +00002755
Chris Lattnereffa8682010-03-08 23:02:59 +00002756 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2757 getDWLabel("info_begin", ModuleCU->getID()),
Bill Wendling94d04b82009-05-20 23:21:38 +00002758 true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002759 EOL("Compilation Unit Length");
Bill Wendling94d04b82009-05-20 23:21:38 +00002760
Devang Patel8a241142009-12-09 18:24:21 +00002761 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling94d04b82009-05-20 23:21:38 +00002762 for (StringMap<DIE*>::const_iterator
2763 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2764 const char *Name = GI->getKeyData();
2765 DIE * Entity = GI->second;
2766
Chris Lattnerfaca5492010-01-22 23:47:11 +00002767 Asm->EmitInt32(Entity->getOffset()); EOL("DIE offset");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002768
2769 if (Asm->VerboseAsm)
2770 Asm->OutStreamer.AddComment("External Name");
2771 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002772 }
2773
Chris Lattnerfaca5492010-01-22 23:47:11 +00002774 Asm->EmitInt32(0); EOL("End Mark");
Chris Lattnerf829eef2010-03-08 22:44:40 +00002775 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_end", ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002776}
2777
Devang Patel193f7202009-11-24 01:14:22 +00002778void DwarfDebug::emitDebugPubTypes() {
Devang Patelf3a03762009-11-24 19:18:41 +00002779 // Start the dwarf pubnames section.
2780 Asm->OutStreamer.SwitchSection(
2781 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattnereffa8682010-03-08 23:02:59 +00002782 EmitDifference(getDWLabel("pubtypes_end", ModuleCU->getID()),
2783 getDWLabel("pubtypes_begin", ModuleCU->getID()), true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002784 EOL("Length of Public Types Info");
Devang Patel193f7202009-11-24 01:14:22 +00002785
Chris Lattnerf829eef2010-03-08 22:44:40 +00002786 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_begin", ModuleCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00002787
Devang Patele3d6d222010-02-02 03:47:27 +00002788 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DWARF Version");
2789 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patel193f7202009-11-24 01:14:22 +00002790
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00002791 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2792 getTempLabel("section_info"), true, false);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002793 EOL("Offset of Compilation ModuleCU Info");
Devang Patel193f7202009-11-24 01:14:22 +00002794
Chris Lattnereffa8682010-03-08 23:02:59 +00002795 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2796 getDWLabel("info_begin", ModuleCU->getID()),
Devang Patel193f7202009-11-24 01:14:22 +00002797 true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002798 EOL("Compilation ModuleCU Length");
Devang Patel193f7202009-11-24 01:14:22 +00002799
2800 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2801 for (StringMap<DIE*>::const_iterator
2802 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2803 const char *Name = GI->getKeyData();
2804 DIE * Entity = GI->second;
2805
Devang Patele3d6d222010-02-02 03:47:27 +00002806 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2807 Asm->EmitInt32(Entity->getOffset());
Chris Lattner4cf202b2010-01-23 03:11:46 +00002808
2809 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("External Name");
Devang Patel31acb892010-02-02 03:37:03 +00002810 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patel193f7202009-11-24 01:14:22 +00002811 }
2812
Chris Lattnerfaca5492010-01-22 23:47:11 +00002813 Asm->EmitInt32(0); EOL("End Mark");
Chris Lattnerf829eef2010-03-08 22:44:40 +00002814 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_end", ModuleCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00002815}
2816
Devang Patel2c4ceb12009-11-21 02:48:08 +00002817/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002818///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002819void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002820 // Check to see if it is worth the effort.
2821 if (!StringPool.empty()) {
2822 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002823 Asm->OutStreamer.SwitchSection(
2824 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002825
2826 // For each of strings in the string pool.
2827 for (unsigned StringID = 1, N = StringPool.size();
2828 StringID <= N; ++StringID) {
2829 // Emit a label for reference from debug information entries.
Chris Lattnerf829eef2010-03-08 22:44:40 +00002830 Asm->OutStreamer.EmitLabel(getDWLabel("string", StringID));
Bill Wendling94d04b82009-05-20 23:21:38 +00002831
2832 // Emit the string itself.
2833 const std::string &String = StringPool[StringID];
Chris Lattner4cf202b2010-01-23 03:11:46 +00002834 Asm->OutStreamer.EmitBytes(StringRef(String.c_str(), String.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002835 }
2836
Chris Lattner0ad9c912010-01-22 22:09:00 +00002837 Asm->O << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002838 }
2839}
2840
Devang Patel2c4ceb12009-11-21 02:48:08 +00002841/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002842///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002843void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002844 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002845 Asm->OutStreamer.SwitchSection(
2846 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002847}
2848
2849/// EmitDebugARanges - Emit visible names into a debug aranges section.
2850///
2851void DwarfDebug::EmitDebugARanges() {
2852 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002853 Asm->OutStreamer.SwitchSection(
2854 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002855
2856 // FIXME - Mock up
2857#if 0
2858 CompileUnit *Unit = GetBaseCompileUnit();
2859
2860 // Don't include size of length
Chris Lattnerfaca5492010-01-22 23:47:11 +00002861 Asm->EmitInt32(0x1c); EOL("Length of Address Ranges Info");
Bill Wendling94d04b82009-05-20 23:21:38 +00002862
Chris Lattnerfaca5492010-01-22 23:47:11 +00002863 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("Dwarf Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00002864
2865 EmitReference("info_begin", Unit->getID());
Chris Lattnerfaca5492010-01-22 23:47:11 +00002866 EOL("Offset of Compilation Unit Info");
Bill Wendling94d04b82009-05-20 23:21:38 +00002867
Chris Lattnerfaca5492010-01-22 23:47:11 +00002868 Asm->EmitInt8(TD->getPointerSize()); EOL("Size of Address");
Bill Wendling94d04b82009-05-20 23:21:38 +00002869
Chris Lattnerfaca5492010-01-22 23:47:11 +00002870 Asm->EmitInt8(0); EOL("Size of Segment Descriptor");
Bill Wendling94d04b82009-05-20 23:21:38 +00002871
Chris Lattnerfaca5492010-01-22 23:47:11 +00002872 Asm->EmitInt16(0); EOL("Pad (1)");
2873 Asm->EmitInt16(0); EOL("Pad (2)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002874
2875 // Range 1
Chris Lattnerfaca5492010-01-22 23:47:11 +00002876 EmitReference("text_begin", 0); EOL("Address");
Chris Lattnereffa8682010-03-08 23:02:59 +00002877 EmitDifference(getTempLabel("text_end"), getTempLabel("text_begin"),
2878 true); EOL("Length");
Bill Wendling94d04b82009-05-20 23:21:38 +00002879
Chris Lattnerfaca5492010-01-22 23:47:11 +00002880 Asm->EmitInt32(0); EOL("EOM (1)");
2881 Asm->EmitInt32(0); EOL("EOM (2)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002882#endif
Bill Wendling94d04b82009-05-20 23:21:38 +00002883}
2884
Devang Patel2c4ceb12009-11-21 02:48:08 +00002885/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002886///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002887void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002888 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002889 Asm->OutStreamer.SwitchSection(
2890 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002891}
2892
Devang Patel2c4ceb12009-11-21 02:48:08 +00002893/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002894///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002895void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002896 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002897 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002898 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002899 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002900 }
2901}
2902
Devang Patel2c4ceb12009-11-21 02:48:08 +00002903/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002904/// Section Header:
2905/// 1. length of section
2906/// 2. Dwarf version number
2907/// 3. address size.
2908///
2909/// Entries (one "entry" for each function that was inlined):
2910///
2911/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2912/// otherwise offset into __debug_str for regular function name.
2913/// 2. offset into __debug_str section for regular function name.
2914/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2915/// instances for the function.
2916///
2917/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2918/// inlined instance; the die_offset points to the inlined_subroutine die in the
2919/// __debug_info section, and the low_pc is the starting address for the
2920/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002921void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002922 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002923 return;
2924
Devang Patel1dbc7712009-06-29 20:45:18 +00002925 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002926 return;
2927
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002928 Asm->OutStreamer.SwitchSection(
2929 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002930
Chris Lattnereffa8682010-03-08 23:02:59 +00002931 EmitDifference(getDWLabel("debug_inlined_end", 1),
2932 getDWLabel("debug_inlined_begin", 1), true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002933 EOL("Length of Debug Inlined Information Entry");
Bill Wendling94d04b82009-05-20 23:21:38 +00002934
Chris Lattnerf829eef2010-03-08 22:44:40 +00002935 Asm->OutStreamer.EmitLabel(getDWLabel("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002936
Chris Lattnerfaca5492010-01-22 23:47:11 +00002937 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("Dwarf Version");
2938 Asm->EmitInt8(TD->getPointerSize()); EOL("Address Size (in bytes)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002939
Devang Patel53bb5c92009-11-10 23:06:00 +00002940 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2941 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002942
Devang Patel53bb5c92009-11-10 23:06:00 +00002943 MDNode *Node = *I;
Devang Patel622b0262010-01-19 06:19:05 +00002944 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002945 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002946 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002947 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002948 StringRef LName = SP.getLinkageName();
2949 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002950
Chris Lattner4cf202b2010-01-23 03:11:46 +00002951 if (LName.empty()) {
2952 Asm->OutStreamer.EmitBytes(Name, 0);
2953 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
2954 } else
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00002955 EmitSectionOffset(getDWLabel("string",
2956 StringPool.idFor(getRealLinkageName(LName))),
2957 getTempLabel("section_str"), true);
Devang Patel53bb5c92009-11-10 23:06:00 +00002958
Chris Lattnerfaca5492010-01-22 23:47:11 +00002959 EOL("MIPS linkage name");
Chris Lattnerb98b1bf2010-03-08 22:23:36 +00002960 EmitSectionOffset(getDWLabel("string", StringPool.idFor(Name)),
2961 getTempLabel("section_str"), false, true);
Chris Lattnerfaca5492010-01-22 23:47:11 +00002962 EOL("Function name");
Chris Lattner894d75a2010-01-22 23:18:42 +00002963 EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002964
Devang Patel53bb5c92009-11-10 23:06:00 +00002965 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002966 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002967 DIE *SP = LI->second;
Chris Lattnerfaca5492010-01-22 23:47:11 +00002968 Asm->EmitInt32(SP->getOffset()); EOL("DIE offset");
Bill Wendling94d04b82009-05-20 23:21:38 +00002969
2970 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002971 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002972 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002973 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002974
Chris Lattnerdd8187a2010-03-08 22:52:49 +00002975 PrintLabelName(getDWLabel("label", LI->first)); EOL("low_pc");
Bill Wendling94d04b82009-05-20 23:21:38 +00002976 }
2977 }
2978
Chris Lattnerf829eef2010-03-08 22:44:40 +00002979 Asm->OutStreamer.EmitLabel(getDWLabel("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002980}