blob: f3b7f340357fb600a4a726b0c616521f9f25540d [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"
Bill Wendling0310d762009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
Chris Lattner334fd1f2009-09-16 00:08:41 +000028#include "llvm/Support/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/Support/Timer.h"
30#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000031using namespace llvm;
32
33static TimerGroup &getDwarfTimerGroup() {
34 static TimerGroup DwarfTimerGroup("Dwarf Debugging");
35 return DwarfTimerGroup;
36}
37
38//===----------------------------------------------------------------------===//
39
40/// Configuration values for initial hash set sizes (log2).
41///
Bill Wendling0310d762009-05-15 09:23:25 +000042static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000043
44namespace llvm {
45
46//===----------------------------------------------------------------------===//
47/// CompileUnit - This dwarf writer support class manages information associate
48/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000049class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000050 /// ID - File identifier for source.
51 ///
52 unsigned ID;
53
54 /// Die - Compile unit debug information entry.
55 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000056 DIE *CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000057
Devang Patel6f01d9c2009-11-21 00:31:03 +000058 /// IndexTyDie - An anonymous type for index type.
59 DIE *IndexTyDie;
60
Bill Wendling0310d762009-05-15 09:23:25 +000061 /// GVToDieMap - Tracks the mapping of unit level debug informaton
62 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000063 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel017d1212009-11-20 21:37:22 +000064 ValueMap<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000065
66 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
67 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000068 /// FIXME : Rename
Devang Patel017d1212009-11-20 21:37:22 +000069 ValueMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000070
71 /// Globals - A map of globally visible named entities for this unit.
72 ///
73 StringMap<DIE*> Globals;
74
Devang Patel193f7202009-11-24 01:14:22 +000075 /// GlobalTypes - A map of globally visible types for this unit.
76 ///
77 StringMap<DIE*> GlobalTypes;
78
Bill Wendling0310d762009-05-15 09:23:25 +000079public:
80 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +000081 : ID(I), CUDie(D), IndexTyDie(0) {}
82 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendling0310d762009-05-15 09:23:25 +000083
84 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +000085 unsigned getID() const { return ID; }
86 DIE* getCUDie() const { return CUDie; }
87 const StringMap<DIE*> &getGlobals() const { return Globals; }
88 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +000089
90 /// hasContent - Return true if this compile unit has something to write out.
91 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000092 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +000093
Devang Patel2c4ceb12009-11-21 02:48:08 +000094 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +000095 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000096 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000097
Devang Patel193f7202009-11-24 01:14:22 +000098 /// addGlobalType - Add a new global type to the compile unit.
99 ///
100 void addGlobalType(const std::string &Name, DIE *Die) {
101 GlobalTypes[Name] = Die;
102 }
103
Devang Patel017d1212009-11-20 21:37:22 +0000104 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000105 /// specified debug variable.
Devang Patel017d1212009-11-20 21:37:22 +0000106 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000107
Devang Patel017d1212009-11-20 21:37:22 +0000108 /// insertDIE - Insert DIE into the map.
109 void insertDIE(MDNode *N, DIE *D) {
110 GVToDieMap.insert(std::make_pair(N, D));
111 }
Bill Wendling0310d762009-05-15 09:23:25 +0000112
Devang Patel017d1212009-11-20 21:37:22 +0000113 /// getDIEEntry - Returns the debug information entry for the speciefied
114 /// debug variable.
Devang Patel6404e4e2009-12-15 19:16:48 +0000115 DIEEntry *getDIEEntry(MDNode *N) {
116 ValueMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
117 if (I == GVToDIEEntryMap.end())
118 return NULL;
119 return I->second;
120 }
Devang Patel017d1212009-11-20 21:37:22 +0000121
122 /// insertDIEEntry - Insert debug information entry into the map.
123 void insertDIEEntry(MDNode *N, DIEEntry *E) {
124 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000125 }
126
Devang Patel2c4ceb12009-11-21 02:48:08 +0000127 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000128 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000129 void addDie(DIE *Buffer) {
130 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000131 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000132
133 // getIndexTyDie - Get an anonymous type for index type.
134 DIE *getIndexTyDie() {
135 return IndexTyDie;
136 }
137
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000138 // setIndexTyDie - Set D as anonymous type for index which can be reused
139 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000140 void setIndexTyDie(DIE *D) {
141 IndexTyDie = D;
142 }
143
Bill Wendling0310d762009-05-15 09:23:25 +0000144};
145
146//===----------------------------------------------------------------------===//
147/// DbgVariable - This class is used to track local variable information.
148///
Devang Patelf76a3d62009-11-16 21:53:40 +0000149class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000150 DIVariable Var; // Variable Descriptor.
151 unsigned FrameIndex; // Variable frame index.
Devang Patel53bb5c92009-11-10 23:06:00 +0000152 DbgVariable *AbstractVar; // Abstract variable for this variable.
153 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000154public:
Devang Patel53bb5c92009-11-10 23:06:00 +0000155 DbgVariable(DIVariable V, unsigned I)
156 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000157
158 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000159 DIVariable getVariable() const { return Var; }
160 unsigned getFrameIndex() const { return FrameIndex; }
161 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
162 DbgVariable *getAbstractVariable() const { return AbstractVar; }
163 void setDIE(DIE *D) { TheDIE = D; }
164 DIE *getDIE() const { return TheDIE; }
Bill Wendling0310d762009-05-15 09:23:25 +0000165};
166
167//===----------------------------------------------------------------------===//
168/// DbgScope - This class is used to track scope information.
169///
Devang Patelf76a3d62009-11-16 21:53:40 +0000170class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000171 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000172 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel53bb5c92009-11-10 23:06:00 +0000173 WeakVH InlinedAtLocation; // Location at which scope is inlined.
174 bool AbstractScope; // Abstract Scope
Bill Wendling0310d762009-05-15 09:23:25 +0000175 unsigned StartLabelID; // Label ID of the beginning of scope.
176 unsigned EndLabelID; // Label ID of the end of scope.
Devang Pateld38dd112009-10-01 18:25:23 +0000177 const MachineInstr *LastInsn; // Last instruction of this scope.
178 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000179 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
180 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000181
Owen Anderson04c05f72009-06-24 22:53:20 +0000182 // Private state for dump()
183 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000184public:
Devang Patelc90aefe2009-10-14 21:08:09 +0000185 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000186 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000187 StartLabelID(0), EndLabelID(0),
Devang Patelc90aefe2009-10-14 21:08:09 +0000188 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000189 virtual ~DbgScope();
190
191 // Accessors.
192 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000193 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000194 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000195 MDNode *getInlinedAt() const {
Devang Patel53bb5c92009-11-10 23:06:00 +0000196 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Patelc90aefe2009-10-14 21:08:09 +0000197 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000198 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000199 unsigned getStartLabelID() const { return StartLabelID; }
200 unsigned getEndLabelID() const { return EndLabelID; }
201 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
202 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendling0310d762009-05-15 09:23:25 +0000203 void setStartLabelID(unsigned S) { StartLabelID = S; }
204 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Pateld38dd112009-10-01 18:25:23 +0000205 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
206 const MachineInstr *getLastInsn() { return LastInsn; }
207 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000208 void setAbstractScope() { AbstractScope = true; }
209 bool isAbstractScope() const { return AbstractScope; }
Devang Pateld38dd112009-10-01 18:25:23 +0000210 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000211
Devang Patel2c4ceb12009-11-21 02:48:08 +0000212 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000213 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000214 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000215
Devang Patel2c4ceb12009-11-21 02:48:08 +0000216 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000217 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000218 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000219
Devang Patel2c4ceb12009-11-21 02:48:08 +0000220 void fixInstructionMarkers() {
Devang Patelaf9e8472009-10-01 20:31:14 +0000221 assert (getFirstInsn() && "First instruction is missing!");
222 if (getLastInsn())
223 return;
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000224
Devang Patelaf9e8472009-10-01 20:31:14 +0000225 // If a scope does not have an instruction to mark an end then use
226 // the end of last child scope.
227 SmallVector<DbgScope *, 4> &Scopes = getScopes();
228 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
229 DbgScope *L = Scopes.back();
230 if (!L->getLastInsn())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000231 L->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +0000232 setLastInsn(L->getLastInsn());
233 }
234
Bill Wendling0310d762009-05-15 09:23:25 +0000235#ifndef NDEBUG
236 void dump() const;
237#endif
238};
239
240#ifndef NDEBUG
241void DbgScope::dump() const {
Chris Lattnerc281de12009-08-23 00:51:00 +0000242 raw_ostream &err = errs();
243 err.indent(IndentLevel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000244 MDNode *N = Desc.getNode();
245 N->dump();
Chris Lattnerc281de12009-08-23 00:51:00 +0000246 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel53bb5c92009-11-10 23:06:00 +0000247 if (AbstractScope)
248 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000249
250 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000251 if (!Scopes.empty())
252 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000253 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
254 if (Scopes[i] != this)
255 Scopes[i]->dump();
256
257 IndentLevel -= 2;
258}
259#endif
260
Bill Wendling0310d762009-05-15 09:23:25 +0000261DbgScope::~DbgScope() {
262 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
263 delete Scopes[i];
264 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
265 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000266}
267
268} // end llvm namespace
269
Chris Lattneraf76e592009-08-22 20:48:53 +0000270DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000271 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000272 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patel2c4ceb12009-11-21 02:48:08 +0000273 DIEValues(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000274 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel53bb5c92009-11-10 23:06:00 +0000275 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000276 if (TimePassesIsEnabled)
277 DebugTimer = new Timer("Dwarf Debug Writer",
278 getDwarfTimerGroup());
279}
280DwarfDebug::~DwarfDebug() {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000281 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
282 delete DIEValues[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000283
Bill Wendling0310d762009-05-15 09:23:25 +0000284 delete DebugTimer;
285}
286
Devang Patel2c4ceb12009-11-21 02:48:08 +0000287/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000288///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000289void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000290 // Profile the node so that we can make it unique.
291 FoldingSetNodeID ID;
292 Abbrev.Profile(ID);
293
294 // Check the set for priors.
295 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
296
297 // If it's newly added.
298 if (InSet == &Abbrev) {
299 // Add to abbreviation list.
300 Abbreviations.push_back(&Abbrev);
301
302 // Assign the vector position + 1 as its number.
303 Abbrev.setNumber(Abbreviations.size());
304 } else {
305 // Assign existing abbreviation number.
306 Abbrev.setNumber(InSet->getNumber());
307 }
308}
309
Devang Patel2c4ceb12009-11-21 02:48:08 +0000310/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000311/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000312DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000313 DIEEntry *Value = new DIEEntry(Entry);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000314 DIEValues.push_back(Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000315 return Value;
316}
317
Devang Patel2c4ceb12009-11-21 02:48:08 +0000318/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000319///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000320void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000321 unsigned Form, uint64_t Integer) {
322 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000323 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000324 DIEValues.push_back(Value);
325 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000326}
327
Devang Patel2c4ceb12009-11-21 02:48:08 +0000328/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000329///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000330void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000331 unsigned Form, int64_t Integer) {
332 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000333 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000334 DIEValues.push_back(Value);
335 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000336}
337
Devang Patel69f57b12009-12-02 15:25:16 +0000338/// addString - Add a string attribute data and value. DIEString only
339/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000340void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patele9a05972009-11-24 19:42:17 +0000341 const StringRef String) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000342 DIEValue *Value = new DIEString(String);
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/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000348///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000349void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000350 const DWLabel &Label) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000351 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000352 DIEValues.push_back(Value);
353 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000354}
355
Devang Patel2c4ceb12009-11-21 02:48:08 +0000356/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000357///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000358void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000359 const std::string &Label) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000360 DIEValue *Value = new DIEObjectLabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000361 DIEValues.push_back(Value);
362 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000363}
364
Devang Patel2c4ceb12009-11-21 02:48:08 +0000365/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000366///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000367void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000368 const DWLabel &Label, const DWLabel &Section,
369 bool isEH, bool useSet) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000370 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
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/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000376///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000377void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000378 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000379 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000380 DIEValues.push_back(Value);
381 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000382}
383
Devang Patel2c4ceb12009-11-21 02:48:08 +0000384/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000385///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000386void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000387 DIEBlock *Block) {
388 Block->ComputeSize(TD);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000389 DIEValues.push_back(Block);
390 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000391}
392
Devang Patel2c4ceb12009-11-21 02:48:08 +0000393/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000394/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000395void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendling0310d762009-05-15 09:23:25 +0000396 // If there is no compile unit specified, don't add a line #.
397 if (V->getCompileUnit().isNull())
398 return;
399
400 unsigned Line = V->getLineNumber();
Devang Pateld037d7a2009-12-11 21:37:07 +0000401 unsigned FileID = findCompileUnit(V->getCompileUnit())->getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000402 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000403 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
404 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000405}
406
Devang Patel2c4ceb12009-11-21 02:48:08 +0000407/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000408/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000409void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendling0310d762009-05-15 09:23:25 +0000410 // If there is no compile unit specified, don't add a line #.
411 if (G->getCompileUnit().isNull())
412 return;
413
414 unsigned Line = G->getLineNumber();
Devang Pateld037d7a2009-12-11 21:37:07 +0000415 unsigned FileID = findCompileUnit(G->getCompileUnit())->getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000416 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000417 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
418 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000419}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000420
Devang Patel2c4ceb12009-11-21 02:48:08 +0000421/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000422/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000423void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel82dfc0c2009-08-31 22:47:13 +0000424 // If there is no compile unit specified, don't add a line #.
425 if (SP->getCompileUnit().isNull())
426 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000427 // If the line number is 0, don't add it.
428 if (SP->getLineNumber() == 0)
429 return;
430
Devang Patel82dfc0c2009-08-31 22:47:13 +0000431
432 unsigned Line = SP->getLineNumber();
Devang Pateld037d7a2009-12-11 21:37:07 +0000433 unsigned FileID = findCompileUnit(SP->getCompileUnit())->getID();
Devang Patel82dfc0c2009-08-31 22:47:13 +0000434 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000435 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
436 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000437}
438
Devang Patel2c4ceb12009-11-21 02:48:08 +0000439/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000440/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000441void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000442 // If there is no compile unit specified, don't add a line #.
443 DICompileUnit CU = Ty->getCompileUnit();
444 if (CU.isNull())
445 return;
446
447 unsigned Line = Ty->getLineNumber();
Devang Pateld037d7a2009-12-11 21:37:07 +0000448 unsigned FileID = findCompileUnit(CU)->getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000449 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000450 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
451 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000452}
453
Devang Patel6404e4e2009-12-15 19:16:48 +0000454/// addSourceLine - Add location information to specified debug information
455/// entry.
456void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
457 // If there is no compile unit specified, don't add a line #.
458 if (NS->getCompileUnit().isNull())
459 return;
460
461 unsigned Line = NS->getLineNumber();
462 StringRef FN = NS->getFilename();
463 StringRef Dir = NS->getDirectory();
464
465 unsigned FileID = GetOrCreateSourceID(Dir, FN);
466 assert(FileID && "Invalid file id");
467 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
468 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
469}
470
Caroline Ticedc8f6042009-08-31 21:19:37 +0000471/* Byref variables, in Blocks, are declared by the programmer as
472 "SomeType VarName;", but the compiler creates a
473 __Block_byref_x_VarName struct, and gives the variable VarName
474 either the struct, or a pointer to the struct, as its type. This
475 is necessary for various behind-the-scenes things the compiler
476 needs to do with by-reference variables in blocks.
477
478 However, as far as the original *programmer* is concerned, the
479 variable should still have type 'SomeType', as originally declared.
480
481 The following function dives into the __Block_byref_x_VarName
482 struct to find the original type of the variable. This will be
483 passed back to the code generating the type for the Debug
484 Information Entry for the variable 'VarName'. 'VarName' will then
485 have the original type 'SomeType' in its debug information.
486
487 The original type 'SomeType' will be the type of the field named
488 'VarName' inside the __Block_byref_x_VarName struct.
489
490 NOTE: In order for this to not completely fail on the debugger
491 side, the Debug Information Entry for the variable VarName needs to
492 have a DW_AT_location that tells the debugger how to unwind through
493 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000494 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000495
496/// Find the type the programmer originally declared the variable to be
497/// and return that type.
498///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000499DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000500
501 DIType subType = Ty;
502 unsigned tag = Ty.getTag();
503
504 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000505 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000506 subType = DTy.getTypeDerivedFrom();
507 }
508
509 DICompositeType blockStruct = DICompositeType(subType.getNode());
510
511 DIArray Elements = blockStruct.getTypeArray();
512
513 if (Elements.isNull())
514 return Ty;
515
516 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
517 DIDescriptor Element = Elements.getElement(i);
518 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000519 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000520 return (DT.getTypeDerivedFrom());
521 }
522
523 return Ty;
524}
525
Devang Patel2c4ceb12009-11-21 02:48:08 +0000526/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000527/// and generate the DWARF information necessary to find the actual variable
528/// given the extra address information encoded in the DIVariable, starting from
529/// the starting location. Add the DWARF information to the die.
530///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000531void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000532 unsigned Attribute,
533 const MachineLocation &Location) {
534 const DIVariable &VD = DV->getVariable();
535 DIType Ty = VD.getType();
536
537 // Decode the original location, and use that as the start of the byref
538 // variable's location.
539 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
540 DIEBlock *Block = new DIEBlock();
541
542 if (Location.isReg()) {
543 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000544 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000545 } else {
546 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000547 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
548 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000549 }
550 } else {
551 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000552 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000553 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000554 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
555 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000556 }
557
Devang Patel2c4ceb12009-11-21 02:48:08 +0000558 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000559 }
560
561 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
562 uint64_t Element = VD.getAddrElement(i);
563
564 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000565 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
566 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000567 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000568 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000569 } else llvm_unreachable("unknown DIFactory Opcode");
570 }
571
572 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000573 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000574}
575
Caroline Ticedc8f6042009-08-31 21:19:37 +0000576/* Byref variables, in Blocks, are declared by the programmer as "SomeType
577 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
578 gives the variable VarName either the struct, or a pointer to the struct, as
579 its type. This is necessary for various behind-the-scenes things the
580 compiler needs to do with by-reference variables in Blocks.
581
582 However, as far as the original *programmer* is concerned, the variable
583 should still have type 'SomeType', as originally declared.
584
Devang Patel2c4ceb12009-11-21 02:48:08 +0000585 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000586 struct to find the original type of the variable, which is then assigned to
587 the variable's Debug Information Entry as its real type. So far, so good.
588 However now the debugger will expect the variable VarName to have the type
589 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000590 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000591 pointers and struct to find the actual variable of type SomeType.
592
593 The following function does just that. We start by getting
594 the "normal" location for the variable. This will be the location
595 of either the struct __Block_byref_x_VarName or the pointer to the
596 struct __Block_byref_x_VarName.
597
598 The struct will look something like:
599
600 struct __Block_byref_x_VarName {
601 ... <various fields>
602 struct __Block_byref_x_VarName *forwarding;
603 ... <various other fields>
604 SomeType VarName;
605 ... <maybe more fields>
606 };
607
608 If we are given the struct directly (as our starting point) we
609 need to tell the debugger to:
610
611 1). Add the offset of the forwarding field.
612
613 2). Follow that pointer to get the the real __Block_byref_x_VarName
614 struct to use (the real one may have been copied onto the heap).
615
616 3). Add the offset for the field VarName, to find the actual variable.
617
618 If we started with a pointer to the struct, then we need to
619 dereference that pointer first, before the other steps.
620 Translating this into DWARF ops, we will need to append the following
621 to the current location description for the variable:
622
623 DW_OP_deref -- optional, if we start with a pointer
624 DW_OP_plus_uconst <forward_fld_offset>
625 DW_OP_deref
626 DW_OP_plus_uconst <varName_fld_offset>
627
628 That is what this function does. */
629
Devang Patel2c4ceb12009-11-21 02:48:08 +0000630/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000631/// provided, and generate the DWARF information necessary to find the
632/// actual Block variable (navigating the Block struct) based on the
633/// starting location. Add the DWARF information to the die. For
634/// more information, read large comment just above here.
635///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000636void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000637 unsigned Attribute,
638 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000639 const DIVariable &VD = DV->getVariable();
640 DIType Ty = VD.getType();
641 DIType TmpTy = Ty;
642 unsigned Tag = Ty.getTag();
643 bool isPointer = false;
644
Devang Patel65dbc902009-11-25 17:36:49 +0000645 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000646
647 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000648 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000649 TmpTy = DTy.getTypeDerivedFrom();
650 isPointer = true;
651 }
652
653 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
654
Daniel Dunbar00564992009-09-19 20:40:14 +0000655 // Find the __forwarding field and the variable field in the __Block_byref
656 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000657 DIArray Fields = blockStruct.getTypeArray();
658 DIDescriptor varField = DIDescriptor();
659 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000660
661
Daniel Dunbar00564992009-09-19 20:40:14 +0000662 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
663 DIDescriptor Element = Fields.getElement(i);
664 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000665 StringRef fieldName = DT.getName();
666 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000667 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000668 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000669 varField = Element;
670 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000671
Mike Stump7e3720d2009-09-24 23:21:26 +0000672 assert(!varField.isNull() && "Can't find byref variable in Block struct");
673 assert(!forwardingField.isNull()
674 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000675
Daniel Dunbar00564992009-09-19 20:40:14 +0000676 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000677 unsigned int forwardingFieldOffset =
678 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
679 unsigned int varFieldOffset =
680 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000681
Mike Stump7e3720d2009-09-24 23:21:26 +0000682 // Decode the original location, and use that as the start of the byref
683 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000684 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
685 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000686
Daniel Dunbar00564992009-09-19 20:40:14 +0000687 if (Location.isReg()) {
688 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000689 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000690 else {
691 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000692 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
693 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000694 }
695 } else {
696 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000697 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000698 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000699 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
700 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000701 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000702
Devang Patel2c4ceb12009-11-21 02:48:08 +0000703 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000704 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000705
Mike Stump7e3720d2009-09-24 23:21:26 +0000706 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000707 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000708 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000709 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000710
Daniel Dunbar00564992009-09-19 20:40:14 +0000711 // Next add the offset for the '__forwarding' field:
712 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
713 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000714 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000715 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
716 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000717 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000718
Daniel Dunbar00564992009-09-19 20:40:14 +0000719 // Now dereference the __forwarding field to get to the real __Block_byref
720 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000721 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000722
Daniel Dunbar00564992009-09-19 20:40:14 +0000723 // Now that we've got the real __Block_byref... struct, add the offset
724 // for the variable's field to get to the location of the actual variable:
725 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000726 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000727 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
728 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000729 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000730
Daniel Dunbar00564992009-09-19 20:40:14 +0000731 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000732 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000733}
734
Devang Patel2c4ceb12009-11-21 02:48:08 +0000735/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000736/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000737void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000738 const MachineLocation &Location) {
739 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
740 DIEBlock *Block = new DIEBlock();
741
742 if (Location.isReg()) {
743 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000744 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000745 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000746 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
747 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000748 }
749 } else {
750 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000751 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000752 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000753 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
754 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000755 }
756
Devang Patel2c4ceb12009-11-21 02:48:08 +0000757 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000758 }
759
Devang Patel2c4ceb12009-11-21 02:48:08 +0000760 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000761}
762
Devang Patelc366f832009-12-10 19:14:49 +0000763/// addToContextOwner - Add Die into the list of its context owner's children.
764void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
765 if (Context.isNull())
766 ModuleCU->addDie(Die);
767 else if (Context.isType()) {
768 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
769 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000770 } else if (Context.isNameSpace()) {
771 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
772 ContextDIE->addChild(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000773 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
774 ContextDIE->addChild(Die);
775 else
776 ModuleCU->addDie(Die);
777}
778
Devang Patel16ced732009-12-10 18:05:33 +0000779/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
780/// given DIType.
781DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
782 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
783 if (TyDIE)
784 return TyDIE;
785
786 // Create new type.
787 TyDIE = new DIE(dwarf::DW_TAG_base_type);
788 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
789 if (Ty.isBasicType())
790 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
791 else if (Ty.isCompositeType())
792 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
793 else {
794 assert(Ty.isDerivedType() && "Unknown kind of DIType");
795 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
796 }
797
Devang Patelc366f832009-12-10 19:14:49 +0000798 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000799 return TyDIE;
800}
801
Devang Patel2c4ceb12009-11-21 02:48:08 +0000802/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000803void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000804 if (Ty.isNull())
805 return;
806
807 // Check for pre-existence.
Devang Patel8a241142009-12-09 18:24:21 +0000808 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000809 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000810 if (Entry) {
811 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000812 return;
813 }
814
815 // Set up proxy.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000816 Entry = createDIEEntry();
Devang Patel8a241142009-12-09 18:24:21 +0000817 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000818
819 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000820 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000821
Devang Patel2c4ceb12009-11-21 02:48:08 +0000822 Entry->setEntry(Buffer);
823 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000824}
825
Devang Patel2c4ceb12009-11-21 02:48:08 +0000826/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000827void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000828 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000829 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000830 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000831 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000832 BTy.getEncoding());
833
834 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000835 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000836 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000837 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000838 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000839}
840
Devang Patel2c4ceb12009-11-21 02:48:08 +0000841/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000842void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000843 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000844 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000845 uint64_t Size = DTy.getSizeInBits() >> 3;
846 unsigned Tag = DTy.getTag();
847
848 // FIXME - Workaround for templates.
849 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
850
851 Buffer.setTag(Tag);
852
853 // Map to main type, void will not have a type.
854 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000855 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000856
857 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000858 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000859 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000860
861 // Add size if non-zero (derived types might be zero-sized.)
862 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000863 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000864
865 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000866 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000867 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000868}
869
Devang Patel2c4ceb12009-11-21 02:48:08 +0000870/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000871void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000872 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000873 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000874
875 uint64_t Size = CTy.getSizeInBits() >> 3;
876 unsigned Tag = CTy.getTag();
877 Buffer.setTag(Tag);
878
879 switch (Tag) {
880 case dwarf::DW_TAG_vector_type:
881 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +0000882 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000883 break;
884 case dwarf::DW_TAG_enumeration_type: {
885 DIArray Elements = CTy.getTypeArray();
886
887 // Add enumerators to enumeration type.
888 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
889 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000890 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelc5254722009-10-09 17:51:49 +0000891 if (!Enum.isNull()) {
Devang Patel8a241142009-12-09 18:24:21 +0000892 ElemDie = constructEnumTypeDIE(&Enum);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000893 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +0000894 }
Bill Wendling0310d762009-05-15 09:23:25 +0000895 }
896 }
897 break;
898 case dwarf::DW_TAG_subroutine_type: {
899 // Add return type.
900 DIArray Elements = CTy.getTypeArray();
901 DIDescriptor RTy = Elements.getElement(0);
Devang Patel8a241142009-12-09 18:24:21 +0000902 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000903
904 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000905 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000906
907 // Add arguments.
908 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
909 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
910 DIDescriptor Ty = Elements.getElement(i);
Devang Patel8a241142009-12-09 18:24:21 +0000911 addType(Arg, DIType(Ty.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000912 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000913 }
914 }
915 break;
916 case dwarf::DW_TAG_structure_type:
917 case dwarf::DW_TAG_union_type:
918 case dwarf::DW_TAG_class_type: {
919 // Add elements to structure type.
920 DIArray Elements = CTy.getTypeArray();
921
922 // A forward struct declared type may not have elements available.
923 if (Elements.isNull())
924 break;
925
926 // Add elements to structure type.
927 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
928 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000929 if (Element.isNull())
930 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000931 DIE *ElemDie = NULL;
932 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patelffe966c2009-12-14 16:18:45 +0000933 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000934 else
Devang Patel8a241142009-12-09 18:24:21 +0000935 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000936 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +0000937 }
938
Devang Patela1ba2692009-08-27 23:51:51 +0000939 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000940 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000941
942 unsigned RLang = CTy.getRunTimeLang();
943 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000944 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +0000945 dwarf::DW_FORM_data1, RLang);
946 break;
947 }
948 default:
949 break;
950 }
951
952 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000953 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000954 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000955
956 if (Tag == dwarf::DW_TAG_enumeration_type ||
957 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
958 // Add size if non-zero (derived types might be zero-sized.)
959 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000960 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000961 else {
962 // Add zero size if it is not a forward declaration.
963 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000964 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000965 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000966 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +0000967 }
968
969 // Add source line info if available.
970 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000971 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000972 }
973}
974
Devang Patel2c4ceb12009-11-21 02:48:08 +0000975/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
976void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +0000977 int64_t L = SR.getLo();
978 int64_t H = SR.getHi();
979 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
980
Devang Patel2c4ceb12009-11-21 02:48:08 +0000981 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +0000982 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000983 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +0000984 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000985
Devang Patel2c4ceb12009-11-21 02:48:08 +0000986 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +0000987}
988
Devang Patel2c4ceb12009-11-21 02:48:08 +0000989/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000990void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000991 DICompositeType *CTy) {
992 Buffer.setTag(dwarf::DW_TAG_array_type);
993 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000994 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000995
996 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +0000997 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +0000998 DIArray Elements = CTy->getTypeArray();
999
Devang Patel6f01d9c2009-11-21 00:31:03 +00001000 // Get an anonymous type for index type.
Devang Patel8a241142009-12-09 18:24:21 +00001001 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001002 if (!IdxTy) {
1003 // Construct an anonymous type for index type.
1004 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001005 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1006 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001007 dwarf::DW_ATE_signed);
Devang Patel8a241142009-12-09 18:24:21 +00001008 ModuleCU->addDie(IdxTy);
1009 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001010 }
Bill Wendling0310d762009-05-15 09:23:25 +00001011
1012 // Add subranges to array type.
1013 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1014 DIDescriptor Element = Elements.getElement(i);
1015 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001016 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001017 }
1018}
1019
Devang Patel2c4ceb12009-11-21 02:48:08 +00001020/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel8a241142009-12-09 18:24:21 +00001021DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator *ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001022 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel65dbc902009-11-25 17:36:49 +00001023 StringRef Name = ETy->getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001024 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001025 int64_t Value = ETy->getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001026 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001027 return Enumerator;
1028}
1029
Devang Patel2c4ceb12009-11-21 02:48:08 +00001030/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel8a241142009-12-09 18:24:21 +00001031DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001032 // If the global variable was optmized out then no need to create debug info
1033 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001034 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001035 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001036
Bill Wendling0310d762009-05-15 09:23:25 +00001037 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001038 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001039 GV.getDisplayName());
1040
Devang Patel65dbc902009-11-25 17:36:49 +00001041 StringRef LinkageName = GV.getLinkageName();
1042 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001043 // Skip special LLVM prefix that is used to inform the asm printer to not
1044 // emit usual symbol prefix before the symbol name. This happens for
1045 // Objective-C symbol names and symbol whose name is replaced using GCC's
1046 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001047 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001048 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001049 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001050 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001051 }
Devang Patel8a241142009-12-09 18:24:21 +00001052 addType(GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001053 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001054 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1055 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001056
Bill Wendling0310d762009-05-15 09:23:25 +00001057 return GVDie;
1058}
1059
Devang Patel2c4ceb12009-11-21 02:48:08 +00001060/// createMemberDIE - Create new member DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001061DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001062 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001063 StringRef Name = DT.getName();
1064 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001065 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001066
Devang Patel8a241142009-12-09 18:24:21 +00001067 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001068
Devang Patel2c4ceb12009-11-21 02:48:08 +00001069 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001070
Devang Patel33db5082009-11-04 22:06:12 +00001071 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001072 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001073
Bill Wendling0310d762009-05-15 09:23:25 +00001074 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001075 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001076
1077 if (Size != FieldSize) {
1078 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001079 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1080 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001081
1082 uint64_t Offset = DT.getOffsetInBits();
1083 uint64_t FieldOffset = Offset;
1084 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1085 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1086 FieldOffset = (HiMark - FieldSize);
1087 Offset -= FieldOffset;
1088
1089 // Maybe we need to work from the other end.
1090 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001091 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001092
Devang Patel33db5082009-11-04 22:06:12 +00001093 // Here WD_AT_data_member_location points to the anonymous
1094 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001095 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001096
1097 } else
1098 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001099 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001100
Devang Patel2c4ceb12009-11-21 02:48:08 +00001101 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001102
1103 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001104 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001105 dwarf::DW_ACCESS_protected);
1106 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001107 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001108 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001109 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1110 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1111 dwarf::DW_ACCESS_public);
1112 if (DT.isVirtual())
1113 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1114 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001115 return MemberDie;
1116}
1117
Devang Patelffe966c2009-12-14 16:18:45 +00001118/// createSubprogramDIE - Create new DIE using SP.
1119DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1120 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1121 if (SPDie)
1122 return SPDie;
1123
1124 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel65dbc902009-11-25 17:36:49 +00001125 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001126
Devang Patel65dbc902009-11-25 17:36:49 +00001127 StringRef LinkageName = SP.getLinkageName();
1128 if (!LinkageName.empty()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001129 // Skip special LLVM prefix that is used to inform the asm printer to not
1130 // emit usual symbol prefix before the symbol name. This happens for
1131 // Objective-C symbol names and symbol whose name is replaced using GCC's
1132 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001133 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001134 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001135 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001136 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001137 }
Devang Patel2c4ceb12009-11-21 02:48:08 +00001138 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001139
Bill Wendling0310d762009-05-15 09:23:25 +00001140 // Add prototyped tag, if C or ObjC.
1141 unsigned Lang = SP.getCompileUnit().getLanguage();
1142 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1143 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001144 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001145
1146 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001147 DICompositeType SPTy = SP.getType();
1148 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001149 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001150
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001151 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001152 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001153 else
Devang Patel8a241142009-12-09 18:24:21 +00001154 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001155
Devang Patel5d11eb02009-12-03 19:11:07 +00001156 unsigned VK = SP.getVirtuality();
1157 if (VK) {
1158 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1159 DIEBlock *Block = new DIEBlock();
1160 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1161 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1162 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1163 ContainingTypeMap.insert(std::make_pair(SPDie, WeakVH(SP.getContainingType().getNode())));
1164 }
1165
Devang Patelffe966c2009-12-14 16:18:45 +00001166 if (MakeDecl || !SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001167 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001168
1169 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001170 // be handled while processing variables.
1171 DICompositeType SPTy = SP.getType();
1172 DIArray Args = SPTy.getTypeArray();
1173 unsigned SPTag = SPTy.getTag();
1174
Bill Wendling0310d762009-05-15 09:23:25 +00001175 if (SPTag == dwarf::DW_TAG_subroutine_type)
1176 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1177 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel8a241142009-12-09 18:24:21 +00001178 addType(Arg, DIType(Args.getElement(i).getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001179 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1180 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001181 }
1182 }
1183
Bill Wendling0310d762009-05-15 09:23:25 +00001184 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001185 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001186 return SPDie;
1187}
1188
Devang Patel2c4ceb12009-11-21 02:48:08 +00001189/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendling0310d762009-05-15 09:23:25 +00001190///
Devang Pateld037d7a2009-12-11 21:37:07 +00001191CompileUnit *DwarfDebug::findCompileUnit(DICompileUnit Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001192 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001193 CompileUnitMap.find(Unit.getNode());
Devang Pateld037d7a2009-12-11 21:37:07 +00001194 if (I == CompileUnitMap.end())
1195 return constructCompileUnit(Unit.getNode());
1196 return I->second;
Bill Wendling0310d762009-05-15 09:23:25 +00001197}
1198
Devang Patel53bb5c92009-11-10 23:06:00 +00001199/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1200/// Initialize scope and update scope hierarchy.
1201DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1202 MDNode *InlinedAt) {
1203 assert (N && "Invalid Scope encoding!");
1204 assert (MI && "Missing machine instruction!");
1205 bool GetConcreteScope = (MI && InlinedAt);
1206
1207 DbgScope *NScope = NULL;
1208
1209 if (InlinedAt)
1210 NScope = DbgScopeMap.lookup(InlinedAt);
1211 else
1212 NScope = DbgScopeMap.lookup(N);
1213 assert (NScope && "Unable to find working scope!");
1214
1215 if (NScope->getFirstInsn())
1216 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001217
1218 DbgScope *Parent = NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001219 if (GetConcreteScope) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001220 DILocation IL(InlinedAt);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001221 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel53bb5c92009-11-10 23:06:00 +00001222 IL.getOrigLocation().getNode());
1223 assert (Parent && "Unable to find Parent scope!");
1224 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001225 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001226 } else if (DIDescriptor(N).isLexicalBlock()) {
1227 DILexicalBlock DB(N);
1228 if (!DB.getContext().isNull()) {
1229 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1230 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001231 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001232 }
Devang Patelc90aefe2009-10-14 21:08:09 +00001233 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001234
Devang Patelbdf45cb2009-10-27 20:47:17 +00001235 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001236
Devang Patel53bb5c92009-11-10 23:06:00 +00001237 if (!Parent && !InlinedAt) {
Devang Patel39ae3ff2009-11-11 00:31:36 +00001238 StringRef SPName = DISubprogram(N).getLinkageName();
1239 if (SPName == MF->getFunction()->getName())
1240 CurrentFnDbgScope = NScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001241 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001242
Devang Patel53bb5c92009-11-10 23:06:00 +00001243 if (GetConcreteScope) {
1244 ConcreteScopes[InlinedAt] = NScope;
1245 getOrCreateAbstractScope(N);
1246 }
1247
Devang Patelbdf45cb2009-10-27 20:47:17 +00001248 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001249}
1250
Devang Patel53bb5c92009-11-10 23:06:00 +00001251DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1252 assert (N && "Invalid Scope encoding!");
1253
1254 DbgScope *AScope = AbstractScopes.lookup(N);
1255 if (AScope)
1256 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001257
Devang Patel53bb5c92009-11-10 23:06:00 +00001258 DbgScope *Parent = NULL;
1259
1260 DIDescriptor Scope(N);
1261 if (Scope.isLexicalBlock()) {
1262 DILexicalBlock DB(N);
1263 DIDescriptor ParentDesc = DB.getContext();
1264 if (!ParentDesc.isNull())
1265 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1266 }
1267
1268 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1269
1270 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001271 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001272 AScope->setAbstractScope();
1273 AbstractScopes[N] = AScope;
1274 if (DIDescriptor(N).isSubprogram())
1275 AbstractScopesList.push_back(AScope);
1276 return AScope;
1277}
Devang Patelaf9e8472009-10-01 20:31:14 +00001278
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001279/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001280/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1281/// If there are global variables in this scope then create and insert
1282/// DIEs for these variables.
1283DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001284
Devang Patel017d1212009-11-20 21:37:22 +00001285 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel53bb5c92009-11-10 23:06:00 +00001286 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patelffe966c2009-12-14 16:18:45 +00001287 DISubprogram SP(SPNode);
1288 if (SP.isDefinition() && !SP.getContext().isCompileUnit()) {
1289 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1290 // Add arguments.
1291 DICompositeType SPTy = SP.getType();
1292 DIArray Args = SPTy.getTypeArray();
1293 unsigned SPTag = SPTy.getTag();
1294 if (SPTag == dwarf::DW_TAG_subroutine_type)
1295 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1296 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1297 addType(Arg, DIType(Args.getElement(i).getNode()));
1298 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1299 SPDie->addChild(Arg);
1300 }
1301 DIE *SPDeclDie = SPDie;
1302 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1303 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1304 SPDeclDie);
Devang Patelffe966c2009-12-14 16:18:45 +00001305 ModuleCU->addDie(SPDie);
1306 }
1307
Devang Patel2c4ceb12009-11-21 02:48:08 +00001308 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001309 DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001310 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001311 DWLabel("func_end", SubprogramCount));
1312 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001313 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001314
Devang Patel53bb5c92009-11-10 23:06:00 +00001315 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001316 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001317
Devang Patel53bb5c92009-11-10 23:06:00 +00001318 return SPDie;
1319}
1320
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001321/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001322/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1323DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001324 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1325 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1326
1327 // Ignore empty scopes.
1328 if (StartID == EndID && StartID != 0)
1329 return NULL;
1330
1331 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1332 if (Scope->isAbstractScope())
1333 return ScopeDIE;
1334
Devang Patel2c4ceb12009-11-21 02:48:08 +00001335 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001336 StartID ?
1337 DWLabel("label", StartID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001338 : DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001339 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001340 EndID ?
1341 DWLabel("label", EndID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001342 : DWLabel("func_end", SubprogramCount));
1343
1344
1345
1346 return ScopeDIE;
1347}
1348
Devang Patel2c4ceb12009-11-21 02:48:08 +00001349/// constructInlinedScopeDIE - This scope represents inlined body of
1350/// a function. Construct DIE to represent this concrete inlined copy
1351/// of the function.
1352DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001353 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1354 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1355 assert (StartID && "Invalid starting label for an inlined scope!");
1356 assert (EndID && "Invalid end label for an inlined scope!");
1357 // Ignore empty scopes.
1358 if (StartID == EndID && StartID != 0)
1359 return NULL;
1360
1361 DIScope DS(Scope->getScopeNode());
1362 if (DS.isNull())
1363 return NULL;
1364 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1365
1366 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001367 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001368 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001369 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001370 dwarf::DW_FORM_ref4, OriginDIE);
1371
Devang Patel2c4ceb12009-11-21 02:48:08 +00001372 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001373 DWLabel("label", StartID));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001374 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001375 DWLabel("label", EndID));
1376
1377 InlinedSubprogramDIEs.insert(OriginDIE);
1378
1379 // Track the start label for this inlined function.
1380 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1381 I = InlineInfo.find(InlinedSP.getNode());
1382
1383 if (I == InlineInfo.end()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001384 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1385 ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001386 InlinedSPNodes.push_back(InlinedSP.getNode());
1387 } else
1388 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1389
1390 StringPool.insert(InlinedSP.getName());
1391 StringPool.insert(InlinedSP.getLinkageName());
1392 DILocation DL(Scope->getInlinedAt());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001393 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1394 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001395
1396 return ScopeDIE;
1397}
1398
Devang Patel2c4ceb12009-11-21 02:48:08 +00001399
1400/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001401DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001402 // Get the descriptor.
1403 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001404 StringRef Name = VD.getName();
1405 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001406 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001407
1408 // Translate tag to proper Dwarf tag. The result variable is dropped for
1409 // now.
1410 unsigned Tag;
1411 switch (VD.getTag()) {
1412 case dwarf::DW_TAG_return_variable:
1413 return NULL;
1414 case dwarf::DW_TAG_arg_variable:
1415 Tag = dwarf::DW_TAG_formal_parameter;
1416 break;
1417 case dwarf::DW_TAG_auto_variable: // fall thru
1418 default:
1419 Tag = dwarf::DW_TAG_variable;
1420 break;
1421 }
1422
1423 // Define variable debug information entry.
1424 DIE *VariableDie = new DIE(Tag);
1425
1426
1427 DIE *AbsDIE = NULL;
1428 if (DbgVariable *AV = DV->getAbstractVariable())
1429 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001430
Devang Patel53bb5c92009-11-10 23:06:00 +00001431 if (AbsDIE) {
1432 DIScope DS(Scope->getScopeNode());
1433 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001434 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc0326792009-11-11 03:09:50 +00001435 (void) OriginSPDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001436 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1437 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1438 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001439 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001440 dwarf::DW_FORM_ref4, AbsDIE);
1441 }
1442 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001443 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1444 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001445
1446 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001447 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001448 // addresses instead.
1449 if (VD.isBlockByrefVariable())
Devang Patel8a241142009-12-09 18:24:21 +00001450 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001451 else
Devang Patel8a241142009-12-09 18:24:21 +00001452 addType(VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001453 }
1454
1455 // Add variable address.
1456 if (!Scope->isAbstractScope()) {
1457 MachineLocation Location;
Jim Grosbacha2f20b22009-11-22 20:14:00 +00001458 unsigned FrameReg;
1459 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1460 Location.set(FrameReg, Offset);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001461
Devang Patel53bb5c92009-11-10 23:06:00 +00001462 if (VD.hasComplexAddress())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001463 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001464 else if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001465 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001466 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001467 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001468 }
1469 DV->setDIE(VariableDie);
1470 return VariableDie;
1471
1472}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001473
Devang Patel193f7202009-11-24 01:14:22 +00001474void DwarfDebug::addPubTypes(DISubprogram SP) {
1475 DICompositeType SPTy = SP.getType();
1476 unsigned SPTag = SPTy.getTag();
1477 if (SPTag != dwarf::DW_TAG_subroutine_type)
1478 return;
1479
1480 DIArray Args = SPTy.getTypeArray();
1481 if (Args.isNull())
1482 return;
1483
1484 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1485 DIType ATy(Args.getElement(i).getNode());
1486 if (ATy.isNull())
1487 continue;
1488 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel65dbc902009-11-25 17:36:49 +00001489 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001490 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1491 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1492 }
1493 }
1494}
1495
Devang Patel2c4ceb12009-11-21 02:48:08 +00001496/// constructScopeDIE - Construct a DIE for this scope.
1497DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001498 if (!Scope)
1499 return NULL;
1500 DIScope DS(Scope->getScopeNode());
1501 if (DS.isNull())
1502 return NULL;
1503
1504 DIE *ScopeDIE = NULL;
1505 if (Scope->getInlinedAt())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001506 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001507 else if (DS.isSubprogram()) {
1508 if (Scope->isAbstractScope())
Devang Patel017d1212009-11-20 21:37:22 +00001509 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001510 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001511 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001512 }
1513 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001514 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001515 if (!ScopeDIE) return NULL;
1516 }
1517
1518 // Add variables to scope.
1519 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1520 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001521 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001522 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001523 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001524 }
1525
1526 // Add nested scopes.
1527 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1528 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1529 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001530 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001531 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001532 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001533 }
Devang Patel193f7202009-11-24 01:14:22 +00001534
1535 if (DS.isSubprogram())
1536 addPubTypes(DISubprogram(DS.getNode()));
1537
1538 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001539}
1540
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001541/// GetOrCreateSourceID - Look up the source id with the given directory and
1542/// source file names. If none currently exists, create a new id and insert it
1543/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1544/// maps as well.
Devang Patel65dbc902009-11-25 17:36:49 +00001545unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001546 unsigned DId;
1547 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1548 if (DI != DirectoryIdMap.end()) {
1549 DId = DI->getValue();
1550 } else {
1551 DId = DirectoryNames.size() + 1;
1552 DirectoryIdMap[DirName] = DId;
1553 DirectoryNames.push_back(DirName);
1554 }
1555
1556 unsigned FId;
1557 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1558 if (FI != SourceFileIdMap.end()) {
1559 FId = FI->getValue();
1560 } else {
1561 FId = SourceFileNames.size() + 1;
1562 SourceFileIdMap[FileName] = FId;
1563 SourceFileNames.push_back(FileName);
1564 }
1565
1566 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1567 SourceIdMap.find(std::make_pair(DId, FId));
1568 if (SI != SourceIdMap.end())
1569 return SI->second;
1570
1571 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1572 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1573 SourceIds.push_back(std::make_pair(DId, FId));
1574
1575 return SrcId;
1576}
1577
Devang Patel6404e4e2009-12-15 19:16:48 +00001578/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1579DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1580 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1581 if (NDie)
1582 return NDie;
1583 NDie = new DIE(dwarf::DW_TAG_namespace);
1584 ModuleCU->insertDIE(NS.getNode(), NDie);
1585 if (!NS.getName().empty())
1586 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1587 addSourceLine(NDie, &NS);
1588 addToContextOwner(NDie, NS.getContext());
1589 return NDie;
1590}
1591
Devang Pateld037d7a2009-12-11 21:37:07 +00001592CompileUnit *DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001593 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001594 StringRef FN = DIUnit.getFilename();
1595 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001596 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001597
1598 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001599 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001600 DWLabel("section_line", 0), DWLabel("section_line", 0),
1601 false);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001602 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001603 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001604 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001605 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001606 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001607
Devang Patel65dbc902009-11-25 17:36:49 +00001608 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001609 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001610 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001611 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001612
Devang Patel65dbc902009-11-25 17:36:49 +00001613 StringRef Flags = DIUnit.getFlags();
1614 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001615 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001616
1617 unsigned RVer = DIUnit.getRunTimeVersion();
1618 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001619 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001620 dwarf::DW_FORM_data1, RVer);
1621
1622 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001623 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001624 // Use first compile unit marked as isMain as the compile unit
1625 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001626 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001627 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001628
Devang Patele4b27562009-08-28 23:24:31 +00001629 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001630 CompileUnits.push_back(Unit);
Devang Pateld037d7a2009-12-11 21:37:07 +00001631 return Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001632}
1633
Devang Patel2c4ceb12009-11-21 02:48:08 +00001634void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001635 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001636
Devang Patel905cf5e2009-09-04 23:59:07 +00001637 // If debug information is malformed then ignore it.
1638 if (DI_GV.Verify() == false)
1639 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001640
1641 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001642 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel13e16b62009-06-26 01:49:18 +00001643 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001644
Devang Patel8a241142009-12-09 18:24:21 +00001645 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Pateledb45632009-12-10 23:25:41 +00001646 if (!VariableDie)
1647 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001648
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001649 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001650 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001651
1652 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001653 if (DI_GV.isDefinition()
1654 && !DI_GV.getContext().isCompileUnit()) {
1655 // Create specification DIE.
1656 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1657 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1658 dwarf::DW_FORM_ref4, VariableDie);
1659 DIEBlock *Block = new DIEBlock();
1660 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1661 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
1662 Asm->Mang->getMangledName(DI_GV.getGlobal()));
1663 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1664 ModuleCU->addDie(VariableSpecDIE);
1665 } else {
1666 DIEBlock *Block = new DIEBlock();
1667 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1668 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
1669 Asm->Mang->getMangledName(DI_GV.getGlobal()));
1670 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1671 }
Devang Patelc366f832009-12-10 19:14:49 +00001672 addToContextOwner(VariableDie, DI_GV.getContext());
1673
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001674 // Expose as global. FIXME - need to check external flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001675 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001676
1677 DIType GTy = DI_GV.getType();
Devang Patel65dbc902009-11-25 17:36:49 +00001678 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001679 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1680 assert (Entry && "Missing global type!");
1681 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1682 }
Devang Patel13e16b62009-06-26 01:49:18 +00001683 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001684}
1685
Devang Patel2c4ceb12009-11-21 02:48:08 +00001686void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001687 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001688
1689 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001690 if (ModuleCU->getDIE(N))
Devang Patel13e16b62009-06-26 01:49:18 +00001691 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001692
1693 if (!SP.isDefinition())
1694 // This is a method declaration which will be handled while constructing
1695 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001696 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001697
Devang Patel8a241142009-12-09 18:24:21 +00001698 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001699
1700 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001701 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001702
1703 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001704 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001705
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001706 // Expose as global.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001707 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001708
Devang Patel13e16b62009-06-26 01:49:18 +00001709 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001710}
1711
Devang Patel2c4ceb12009-11-21 02:48:08 +00001712/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001713/// content. Create global DIEs and emit initial debug info sections.
1714/// This is inovked by the target AsmPrinter.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001715void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel208622d2009-06-25 22:36:02 +00001716 this->M = M;
1717
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001718 if (TimePassesIsEnabled)
1719 DebugTimer->startTimer();
1720
Devang Patel3380cc52009-11-11 19:55:08 +00001721 if (!MAI->doesSupportDebugInformation())
1722 return;
1723
Devang Patel78ab9e22009-07-30 18:56:46 +00001724 DebugInfoFinder DbgFinder;
1725 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001726
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001727 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001728 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1729 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001730 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001731
1732 if (CompileUnits.empty()) {
1733 if (TimePassesIsEnabled)
1734 DebugTimer->stopTimer();
1735
1736 return;
1737 }
1738
Devang Patel70f44262009-06-29 20:38:13 +00001739 // If main compile unit for this module is not seen than randomly
1740 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001741 if (!ModuleCU)
1742 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001743
Devang Patel53bb5c92009-11-10 23:06:00 +00001744 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001745 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1746 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001747 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001748
Devang Patelc366f832009-12-10 19:14:49 +00001749 // Create DIEs for each global variable.
1750 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1751 E = DbgFinder.global_variable_end(); I != E; ++I)
1752 constructGlobalVariableDIE(*I);
1753
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001754 MMI = mmi;
1755 shouldEmit = true;
1756 MMI->setDebugInfoAvailability(true);
1757
1758 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001759 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001760
1761 // Print out .file directives to specify files for .loc directives. These are
1762 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001763 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001764 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1765 // Remember source id starts at 1.
1766 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1767 sys::Path FullPath(getSourceDirectoryName(Id.first));
1768 bool AppendOk =
1769 FullPath.appendComponent(getSourceFileName(Id.second));
1770 assert(AppendOk && "Could not append filename to directory!");
1771 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001772 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001773 Asm->EOL();
1774 }
1775 }
1776
1777 // Emit initial sections
Devang Patel2c4ceb12009-11-21 02:48:08 +00001778 emitInitial();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001779
1780 if (TimePassesIsEnabled)
1781 DebugTimer->stopTimer();
1782}
1783
Devang Patel2c4ceb12009-11-21 02:48:08 +00001784/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001785///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001786void DwarfDebug::endModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001787 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001788 return;
1789
1790 if (TimePassesIsEnabled)
1791 DebugTimer->startTimer();
1792
Devang Patel53bb5c92009-11-10 23:06:00 +00001793 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1794 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1795 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1796 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001797 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001798 }
1799
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001800 // Insert top level DIEs.
1801 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1802 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1803 ModuleCU->getCUDie()->addChild(*TI);
1804
Devang Patel5d11eb02009-12-03 19:11:07 +00001805 for (DenseMap<DIE *, WeakVH>::iterator CI = ContainingTypeMap.begin(),
1806 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1807 DIE *SPDie = CI->first;
1808 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1809 if (!N) continue;
1810 DIE *NDie = ModuleCU->getDIE(N);
1811 if (!NDie) continue;
1812 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1813 addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1814 }
1815
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001816 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001817 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001818 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001819 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001820 EmitLabel("data_end", 0);
1821
1822 // End text sections.
1823 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001824 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001825 EmitLabel("section_end", i);
1826 }
1827
1828 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001829 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001830
1831 // Emit function debug frame information
1832 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1833 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001834 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001835
1836 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001837 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001838
1839 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001840 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001841
1842 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001843 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001844
1845 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001846 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001847
1848 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001849 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001850
Devang Patel193f7202009-11-24 01:14:22 +00001851 // Emit info into a debug pubtypes section.
1852 emitDebugPubTypes();
1853
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001854 // Emit info into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001855 emitDebugStr();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001856
1857 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001858 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001859
1860 // Emit info into a debug aranges section.
1861 EmitDebugARanges();
1862
1863 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001864 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001865
1866 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001867 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001868
1869 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001870 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001871
1872 if (TimePassesIsEnabled)
1873 DebugTimer->stopTimer();
1874}
1875
Devang Patel53bb5c92009-11-10 23:06:00 +00001876/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001877DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1878 unsigned FrameIdx,
Devang Patel53bb5c92009-11-10 23:06:00 +00001879 DILocation &ScopeLoc) {
1880
1881 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1882 if (AbsDbgVariable)
1883 return AbsDbgVariable;
1884
1885 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1886 if (!Scope)
1887 return NULL;
1888
1889 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001890 Scope->addVariable(AbsDbgVariable);
Devang Patel53bb5c92009-11-10 23:06:00 +00001891 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1892 return AbsDbgVariable;
1893}
1894
Devang Patel2c4ceb12009-11-21 02:48:08 +00001895/// collectVariableInfo - Populate DbgScope entries with variables' info.
1896void DwarfDebug::collectVariableInfo() {
Devang Patelac1ceb32009-10-09 22:42:28 +00001897 if (!MMI) return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001898
Devang Patele717faa2009-10-06 01:26:37 +00001899 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1900 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1901 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac1ceb32009-10-09 22:42:28 +00001902 MetadataBase *MB = VI->first;
1903 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel53bb5c92009-11-10 23:06:00 +00001904 if (!Var) continue;
Devang Pateleda31212009-10-08 18:48:03 +00001905 DIVariable DV (Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001906 std::pair< unsigned, MDNode *> VP = VI->second;
1907 DILocation ScopeLoc(VP.second);
1908
1909 DbgScope *Scope =
1910 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1911 if (!Scope)
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001912 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelfb0ee432009-11-10 23:20:04 +00001913 // If variable scope is not found then skip this variable.
1914 if (!Scope)
1915 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001916
1917 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001918 Scope->addVariable(RegVar);
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001919 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1920 ScopeLoc))
Devang Patel53bb5c92009-11-10 23:06:00 +00001921 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patele717faa2009-10-06 01:26:37 +00001922 }
1923}
1924
Devang Patel2c4ceb12009-11-21 02:48:08 +00001925/// beginScope - Process beginning of a scope starting at Label.
1926void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001927 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1928 if (I == DbgScopeBeginMap.end())
1929 return;
Dan Gohman277207e2009-11-23 21:30:55 +00001930 ScopeVector &SD = I->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001931 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001932 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001933 (*SDI)->setStartLabelID(Label);
1934}
1935
Devang Patel2c4ceb12009-11-21 02:48:08 +00001936/// endScope - Process end of a scope.
1937void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001938 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00001939 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00001940 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001941
1942 unsigned Label = MMI->NextLabelID();
1943 Asm->printLabel(Label);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001944 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001945
Devang Patel0d20ac82009-10-06 01:50:42 +00001946 SmallVector<DbgScope *, 2> &SD = I->second;
1947 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001948 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001949 (*SDI)->setEndLabelID(Label);
Devang Patel53bb5c92009-11-10 23:06:00 +00001950 return;
1951}
1952
1953/// createDbgScope - Create DbgScope for the scope.
1954void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1955
1956 if (!InlinedAt) {
1957 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1958 if (WScope)
1959 return;
1960 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1961 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001962 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel2f105c62009-11-11 00:18:40 +00001963 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001964 return;
1965 }
1966
1967 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1968 if (WScope)
1969 return;
1970
1971 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1972 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1973 DILocation DL(InlinedAt);
1974 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel0d20ac82009-10-06 01:50:42 +00001975}
1976
Devang Patel2c4ceb12009-11-21 02:48:08 +00001977/// extractScopeInformation - Scan machine instructions in this function
Devang Patelaf9e8472009-10-01 20:31:14 +00001978/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001979bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patelaf9e8472009-10-01 20:31:14 +00001980 // If scope information was extracted using .dbg intrinsics then there is not
1981 // any need to extract these information by scanning each instruction.
1982 if (!DbgScopeMap.empty())
1983 return false;
1984
Devang Patel53bb5c92009-11-10 23:06:00 +00001985 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelaf9e8472009-10-01 20:31:14 +00001986 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1987 I != E; ++I) {
1988 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1989 II != IE; ++II) {
1990 const MachineInstr *MInsn = II;
1991 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001992 if (DL.isUnknown()) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001993 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001994 if (!DLT.Scope) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001995 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001996 // other scopes, create one DbgScope now. This will be translated
Devang Patelaf9e8472009-10-01 20:31:14 +00001997 // into a scope DIE at the end.
Devang Patel53bb5c92009-11-10 23:06:00 +00001998 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1999 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
2000 }
2001 }
2002
2003
2004 // Build scope hierarchy using working set of scopes.
2005 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2006 I != E; ++I) {
2007 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2008 II != IE; ++II) {
2009 const MachineInstr *MInsn = II;
2010 DebugLoc DL = MInsn->getDebugLoc();
2011 if (DL.isUnknown()) continue;
2012 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
2013 if (!DLT.Scope) continue;
2014 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002015 // other scopes, create one DbgScope now. This will be translated
Devang Patel53bb5c92009-11-10 23:06:00 +00002016 // into a scope DIE at the end.
2017 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2018 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
2019 Scope->setLastInsn(MInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +00002020 }
2021 }
2022
2023 // If a scope's last instruction is not set then use its child scope's
2024 // last instruction as this scope's last instrunction.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002025 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002026 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002027 if (DI->second->isAbstractScope())
2028 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002029 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00002030 DI->second->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +00002031 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2032 }
2033
2034 // Each scope has first instruction and last instruction to mark beginning
2035 // and end of a scope respectively. Create an inverse map that list scopes
2036 // starts (and ends) with an instruction. One instruction may start (or end)
2037 // multiple scopes.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002038 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002039 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2040 DbgScope *S = DI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002041 if (S->isAbstractScope())
2042 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002043 const MachineInstr *MI = S->getFirstInsn();
2044 assert (MI && "DbgScope does not have first instruction!");
2045
2046 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2047 if (IDI != DbgScopeBeginMap.end())
2048 IDI->second.push_back(S);
2049 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002050 DbgScopeBeginMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002051
2052 MI = S->getLastInsn();
2053 assert (MI && "DbgScope does not have last instruction!");
2054 IDI = DbgScopeEndMap.find(MI);
2055 if (IDI != DbgScopeEndMap.end())
2056 IDI->second.push_back(S);
2057 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002058 DbgScopeEndMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002059 }
2060
2061 return !DbgScopeMap.empty();
2062}
2063
Devang Patel2c4ceb12009-11-21 02:48:08 +00002064/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002065/// emitted immediately after the function entry point.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002066void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002067 this->MF = MF;
2068
2069 if (!ShouldEmitDwarfDebug()) return;
2070
2071 if (TimePassesIsEnabled)
2072 DebugTimer->startTimer();
2073
Devang Patel2c4ceb12009-11-21 02:48:08 +00002074 if (!extractScopeInformation(MF))
Devang Patel60b35bd2009-10-06 18:37:31 +00002075 return;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002076
2077 collectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002078
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002079 // Begin accumulating function debug information.
2080 MMI->BeginFunction(MF);
2081
2082 // Assumes in correct section after the entry point.
2083 EmitLabel("func_begin", ++SubprogramCount);
2084
2085 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2086 // function.
Devang Patelac1ceb32009-10-09 22:42:28 +00002087 DebugLoc FDL = MF->getDefaultDebugLoc();
2088 if (!FDL.isUnknown()) {
2089 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2090 unsigned LabelID = 0;
Devang Patel1619dc32009-10-13 23:28:53 +00002091 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002092 if (!SP.isNull())
Devang Patel2c4ceb12009-11-21 02:48:08 +00002093 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002094 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00002095 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002096 Asm->printLabel(LabelID);
2097 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002098 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002099 if (TimePassesIsEnabled)
2100 DebugTimer->stopTimer();
2101}
2102
Devang Patel2c4ceb12009-11-21 02:48:08 +00002103/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002104///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002105void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002106 if (!ShouldEmitDwarfDebug()) return;
2107
2108 if (TimePassesIsEnabled)
2109 DebugTimer->startTimer();
2110
Devang Patelac1ceb32009-10-09 22:42:28 +00002111 if (DbgScopeMap.empty())
2112 return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002113
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002114 // Define end label for subprogram.
2115 EmitLabel("func_end", SubprogramCount);
2116
2117 // Get function line info.
2118 if (!Lines.empty()) {
2119 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00002120 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002121 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2122 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2123 // Append the function info to section info.
2124 SectionLineInfos.insert(SectionLineInfos.end(),
2125 Lines.begin(), Lines.end());
2126 }
2127
Devang Patel53bb5c92009-11-10 23:06:00 +00002128 // Construct abstract scopes.
2129 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002130 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002131 constructScopeDIE(*AI);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002132
Devang Patel2c4ceb12009-11-21 02:48:08 +00002133 constructScopeDIE(CurrentFnDbgScope);
Devang Patel70d75ca2009-11-12 19:02:56 +00002134
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002135 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2136 MMI->getFrameMoves()));
2137
2138 // Clear debug info
Devang Patelc09ddc12009-12-01 18:13:48 +00002139 CurrentFnDbgScope = NULL;
2140 DbgScopeMap.clear();
2141 DbgScopeBeginMap.clear();
2142 DbgScopeEndMap.clear();
2143 ConcreteScopes.clear();
2144 AbstractScopesList.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002145
2146 Lines.clear();
Devang Patelc09ddc12009-12-01 18:13:48 +00002147
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002148 if (TimePassesIsEnabled)
2149 DebugTimer->stopTimer();
2150}
2151
Devang Patel2c4ceb12009-11-21 02:48:08 +00002152/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002153/// label. Returns a unique label ID used to generate a label and provide
2154/// correspondence to the source line list.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002155unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002156 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002157 if (!MMI)
2158 return 0;
2159
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002160 if (TimePassesIsEnabled)
2161 DebugTimer->startTimer();
2162
Devang Patel65dbc902009-11-25 17:36:49 +00002163 StringRef Dir;
2164 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002165
2166 DIDescriptor Scope(S);
2167 if (Scope.isCompileUnit()) {
2168 DICompileUnit CU(S);
2169 Dir = CU.getDirectory();
2170 Fn = CU.getFilename();
2171 } else if (Scope.isSubprogram()) {
2172 DISubprogram SP(S);
2173 Dir = SP.getDirectory();
2174 Fn = SP.getFilename();
2175 } else if (Scope.isLexicalBlock()) {
2176 DILexicalBlock DB(S);
2177 Dir = DB.getDirectory();
2178 Fn = DB.getFilename();
2179 } else
2180 assert (0 && "Unexpected scope info");
2181
2182 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002183 unsigned ID = MMI->NextLabelID();
2184 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2185
2186 if (TimePassesIsEnabled)
2187 DebugTimer->stopTimer();
2188
2189 return ID;
2190}
2191
2192/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2193/// timed. Look up the source id with the given directory and source file
2194/// names. If none currently exists, create a new id and insert it in the
2195/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2196/// well.
2197unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2198 const std::string &FileName) {
2199 if (TimePassesIsEnabled)
2200 DebugTimer->startTimer();
2201
Devang Patel5ccdd102009-09-29 18:40:58 +00002202 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002203
2204 if (TimePassesIsEnabled)
2205 DebugTimer->stopTimer();
2206
2207 return SrcId;
2208}
2209
Bill Wendling829e67b2009-05-20 23:22:40 +00002210//===----------------------------------------------------------------------===//
2211// Emit Methods
2212//===----------------------------------------------------------------------===//
2213
Devang Patel2c4ceb12009-11-21 02:48:08 +00002214/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002215///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002216unsigned
2217DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002218 // Get the children.
2219 const std::vector<DIE *> &Children = Die->getChildren();
2220
2221 // If not last sibling and has children then add sibling offset attribute.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002222 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002223
2224 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002225 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002226
2227 // Get the abbreviation for this DIE.
2228 unsigned AbbrevNumber = Die->getAbbrevNumber();
2229 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2230
2231 // Set DIE offset
2232 Die->setOffset(Offset);
2233
2234 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002235 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002236
2237 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2238 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2239
2240 // Size the DIE attribute values.
2241 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2242 // Size attribute value.
2243 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2244
2245 // Size the DIE children if any.
2246 if (!Children.empty()) {
2247 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2248 "Children flag not set");
2249
2250 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002251 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002252
2253 // End of children marker.
2254 Offset += sizeof(int8_t);
2255 }
2256
2257 Die->setSize(Offset - Die->getOffset());
2258 return Offset;
2259}
2260
Devang Patel2c4ceb12009-11-21 02:48:08 +00002261/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002262///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002263void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002264 // Compute size of compile unit header.
2265 static unsigned Offset =
2266 sizeof(int32_t) + // Length of Compilation Unit Info
2267 sizeof(int16_t) + // DWARF version number
2268 sizeof(int32_t) + // Offset Into Abbrev. Section
2269 sizeof(int8_t); // Pointer Size (in bytes)
2270
Devang Patel2c4ceb12009-11-21 02:48:08 +00002271 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel1dbc7712009-06-29 20:45:18 +00002272 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002273}
2274
Devang Patel2c4ceb12009-11-21 02:48:08 +00002275/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling94d04b82009-05-20 23:21:38 +00002276/// tools to recognize the object file contains Dwarf information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002277void DwarfDebug::emitInitial() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002278 // Check to see if we already emitted intial headers.
2279 if (didInitial) return;
2280 didInitial = true;
2281
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002282 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002283
Bill Wendling94d04b82009-05-20 23:21:38 +00002284 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002285 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002286 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002287 EmitLabel("section_debug_frame", 0);
2288 }
2289
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002290 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002291 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002292 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002293 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002294 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002295 EmitLabel("section_aranges", 0);
2296
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002297 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2298 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002299 EmitLabel("section_macinfo", 0);
2300 }
2301
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002302 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002303 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002304 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002305 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002306 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002307 EmitLabel("section_pubnames", 0);
Devang Patel193f7202009-11-24 01:14:22 +00002308 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2309 EmitLabel("section_pubtypes", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002310 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002311 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002312 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002313 EmitLabel("section_ranges", 0);
2314
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002315 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002316 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002317 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002318 EmitLabel("data_begin", 0);
2319}
2320
Devang Patel2c4ceb12009-11-21 02:48:08 +00002321/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002322///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002323void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002324 // Get the abbreviation for this DIE.
2325 unsigned AbbrevNumber = Die->getAbbrevNumber();
2326 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2327
2328 Asm->EOL();
2329
2330 // Emit the code (index) for the abbreviation.
2331 Asm->EmitULEB128Bytes(AbbrevNumber);
2332
2333 if (Asm->isVerbose())
2334 Asm->EOL(std::string("Abbrev [" +
2335 utostr(AbbrevNumber) +
2336 "] 0x" + utohexstr(Die->getOffset()) +
2337 ":0x" + utohexstr(Die->getSize()) + " " +
2338 dwarf::TagString(Abbrev->getTag())));
2339 else
2340 Asm->EOL();
2341
2342 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2343 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2344
2345 // Emit the DIE attribute values.
2346 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2347 unsigned Attr = AbbrevData[i].getAttribute();
2348 unsigned Form = AbbrevData[i].getForm();
2349 assert(Form && "Too many attributes for DIE (check abbreviation)");
2350
2351 switch (Attr) {
2352 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002353 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002354 break;
2355 case dwarf::DW_AT_abstract_origin: {
2356 DIEEntry *E = cast<DIEEntry>(Values[i]);
2357 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002358 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002359 Asm->EmitInt32(Addr);
2360 break;
2361 }
2362 default:
2363 // Emit an attribute using the defined form.
2364 Values[i]->EmitValue(this, Form);
2365 break;
2366 }
2367
2368 Asm->EOL(dwarf::AttributeString(Attr));
2369 }
2370
2371 // Emit the DIE children if any.
2372 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2373 const std::vector<DIE *> &Children = Die->getChildren();
2374
2375 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002376 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002377
2378 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2379 }
2380}
2381
Devang Patel8a241142009-12-09 18:24:21 +00002382/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002383///
Devang Patel8a241142009-12-09 18:24:21 +00002384void DwarfDebug::emitDebugInfo() {
2385 // Start debug info section.
2386 Asm->OutStreamer.SwitchSection(
2387 Asm->getObjFileLowering().getDwarfInfoSection());
2388 DIE *Die = ModuleCU->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002389
2390 // Emit the compile units header.
Devang Patel8a241142009-12-09 18:24:21 +00002391 EmitLabel("info_begin", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002392
2393 // Emit size of content not including length itself
2394 unsigned ContentSize = Die->getSize() +
2395 sizeof(int16_t) + // DWARF version number
2396 sizeof(int32_t) + // Offset Into Abbrev. Section
2397 sizeof(int8_t) + // Pointer Size (in bytes)
2398 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2399
2400 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2401 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2402 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2403 Asm->EOL("Offset Into Abbrev. Section");
2404 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2405
Devang Patel2c4ceb12009-11-21 02:48:08 +00002406 emitDIE(Die);
Bill Wendling94d04b82009-05-20 23:21:38 +00002407 // FIXME - extra padding for gdb bug.
2408 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2409 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2410 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2411 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Devang Patel8a241142009-12-09 18:24:21 +00002412 EmitLabel("info_end", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002413
2414 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002415}
2416
Devang Patel2c4ceb12009-11-21 02:48:08 +00002417/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002418///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002419void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002420 // Check to see if it is worth the effort.
2421 if (!Abbreviations.empty()) {
2422 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002423 Asm->OutStreamer.SwitchSection(
2424 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002425
2426 EmitLabel("abbrev_begin", 0);
2427
2428 // For each abbrevation.
2429 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2430 // Get abbreviation data
2431 const DIEAbbrev *Abbrev = Abbreviations[i];
2432
2433 // Emit the abbrevations code (base 1 index.)
2434 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2435 Asm->EOL("Abbreviation Code");
2436
2437 // Emit the abbreviations data.
2438 Abbrev->Emit(Asm);
2439
2440 Asm->EOL();
2441 }
2442
2443 // Mark end of abbreviations.
2444 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2445
2446 EmitLabel("abbrev_end", 0);
2447 Asm->EOL();
2448 }
2449}
2450
Devang Patel2c4ceb12009-11-21 02:48:08 +00002451/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002452/// the line matrix.
2453///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002454void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002455 // Define last address of section.
2456 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2457 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2458 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2459 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2460
2461 // Mark end of matrix.
2462 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2463 Asm->EmitULEB128Bytes(1); Asm->EOL();
2464 Asm->EmitInt8(1); Asm->EOL();
2465}
2466
Devang Patel2c4ceb12009-11-21 02:48:08 +00002467/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002468///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002469void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002470 // If the target is using .loc/.file, the assembler will be emitting the
2471 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002472 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002473 return;
2474
2475 // Minimum line delta, thus ranging from -10..(255-10).
2476 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2477 // Maximum line delta, thus ranging from -10..(255-10).
2478 const int MaxLineDelta = 255 + MinLineDelta;
2479
2480 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002481 Asm->OutStreamer.SwitchSection(
2482 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002483
2484 // Construct the section header.
2485 EmitDifference("line_end", 0, "line_begin", 0, true);
2486 Asm->EOL("Length of Source Line Info");
2487 EmitLabel("line_begin", 0);
2488
2489 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2490
2491 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2492 Asm->EOL("Prolog Length");
2493 EmitLabel("line_prolog_begin", 0);
2494
2495 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2496
2497 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2498
2499 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2500
2501 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2502
2503 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2504
2505 // Line number standard opcode encodings argument count
2506 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2507 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2508 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2509 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2510 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2511 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2512 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2513 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2514 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2515
2516 // Emit directories.
2517 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2518 Asm->EmitString(getSourceDirectoryName(DI));
2519 Asm->EOL("Directory");
2520 }
2521
2522 Asm->EmitInt8(0); Asm->EOL("End of directories");
2523
2524 // Emit files.
2525 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2526 // Remember source id starts at 1.
2527 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2528 Asm->EmitString(getSourceFileName(Id.second));
2529 Asm->EOL("Source");
2530 Asm->EmitULEB128Bytes(Id.first);
2531 Asm->EOL("Directory #");
2532 Asm->EmitULEB128Bytes(0);
2533 Asm->EOL("Mod date");
2534 Asm->EmitULEB128Bytes(0);
2535 Asm->EOL("File size");
2536 }
2537
2538 Asm->EmitInt8(0); Asm->EOL("End of files");
2539
2540 EmitLabel("line_prolog_end", 0);
2541
2542 // A sequence for each text section.
2543 unsigned SecSrcLinesSize = SectionSourceLines.size();
2544
2545 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2546 // Isolate current sections line info.
2547 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2548
Chris Lattner93b6db32009-08-08 23:39:42 +00002549 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002550 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002551 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002552 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002553 }*/
2554 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002555
2556 // Dwarf assumes we start with first line of first source file.
2557 unsigned Source = 1;
2558 unsigned Line = 1;
2559
2560 // Construct rows of the address, source, line, column matrix.
2561 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2562 const SrcLineInfo &LineInfo = LineInfos[i];
2563 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2564 if (!LabelID) continue;
2565
Caroline Ticec6f9d622009-09-11 18:25:54 +00002566 if (LineInfo.getLine() == 0) continue;
2567
Bill Wendling94d04b82009-05-20 23:21:38 +00002568 if (!Asm->isVerbose())
2569 Asm->EOL();
2570 else {
2571 std::pair<unsigned, unsigned> SourceID =
2572 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002573 O << '\t' << MAI->getCommentString() << ' '
Dan Gohmanb3b98212009-12-05 02:00:34 +00002574 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling94d04b82009-05-20 23:21:38 +00002575 << getSourceFileName(SourceID.second)
Dan Gohmanb3b98212009-12-05 02:00:34 +00002576 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002577 }
2578
2579 // Define the line address.
2580 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2581 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2582 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2583 EmitReference("label", LabelID); Asm->EOL("Location label");
2584
2585 // If change of source, then switch to the new source.
2586 if (Source != LineInfo.getSourceID()) {
2587 Source = LineInfo.getSourceID();
2588 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2589 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2590 }
2591
2592 // If change of line.
2593 if (Line != LineInfo.getLine()) {
2594 // Determine offset.
2595 int Offset = LineInfo.getLine() - Line;
2596 int Delta = Offset - MinLineDelta;
2597
2598 // Update line.
2599 Line = LineInfo.getLine();
2600
2601 // If delta is small enough and in range...
2602 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2603 // ... then use fast opcode.
2604 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2605 } else {
2606 // ... otherwise use long hand.
2607 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2608 Asm->EOL("DW_LNS_advance_line");
2609 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2610 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2611 }
2612 } else {
2613 // Copy the previous row (different address or source)
2614 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2615 }
2616 }
2617
Devang Patel2c4ceb12009-11-21 02:48:08 +00002618 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002619 }
2620
2621 if (SecSrcLinesSize == 0)
2622 // Because we're emitting a debug_line section, we still need a line
2623 // table. The linker and friends expect it to exist. If there's nothing to
2624 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002625 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002626
2627 EmitLabel("line_end", 0);
2628 Asm->EOL();
2629}
2630
Devang Patel2c4ceb12009-11-21 02:48:08 +00002631/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002632///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002633void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002634 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002635 return;
2636
2637 int stackGrowth =
2638 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2639 TargetFrameInfo::StackGrowsUp ?
2640 TD->getPointerSize() : -TD->getPointerSize();
2641
2642 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002643 Asm->OutStreamer.SwitchSection(
2644 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002645
2646 EmitLabel("debug_frame_common", 0);
2647 EmitDifference("debug_frame_common_end", 0,
2648 "debug_frame_common_begin", 0, true);
2649 Asm->EOL("Length of Common Information Entry");
2650
2651 EmitLabel("debug_frame_common_begin", 0);
2652 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2653 Asm->EOL("CIE Identifier Tag");
2654 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2655 Asm->EOL("CIE Version");
2656 Asm->EmitString("");
2657 Asm->EOL("CIE Augmentation");
2658 Asm->EmitULEB128Bytes(1);
2659 Asm->EOL("CIE Code Alignment Factor");
2660 Asm->EmitSLEB128Bytes(stackGrowth);
2661 Asm->EOL("CIE Data Alignment Factor");
2662 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2663 Asm->EOL("CIE RA Column");
2664
2665 std::vector<MachineMove> Moves;
2666 RI->getInitialFrameState(Moves);
2667
2668 EmitFrameMoves(NULL, 0, Moves, false);
2669
2670 Asm->EmitAlignment(2, 0, 0, false);
2671 EmitLabel("debug_frame_common_end", 0);
2672
2673 Asm->EOL();
2674}
2675
Devang Patel2c4ceb12009-11-21 02:48:08 +00002676/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00002677/// section.
2678void
Devang Patel2c4ceb12009-11-21 02:48:08 +00002679DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002680 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002681 return;
2682
2683 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002684 Asm->OutStreamer.SwitchSection(
2685 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002686
2687 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2688 "debug_frame_begin", DebugFrameInfo.Number, true);
2689 Asm->EOL("Length of Frame Information Entry");
2690
2691 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2692
2693 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2694 0, 0, true, false);
2695 Asm->EOL("FDE CIE offset");
2696
2697 EmitReference("func_begin", DebugFrameInfo.Number);
2698 Asm->EOL("FDE initial location");
2699 EmitDifference("func_end", DebugFrameInfo.Number,
2700 "func_begin", DebugFrameInfo.Number);
2701 Asm->EOL("FDE address range");
2702
2703 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2704 false);
2705
2706 Asm->EmitAlignment(2, 0, 0, false);
2707 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2708
2709 Asm->EOL();
2710}
2711
Devang Patel8a241142009-12-09 18:24:21 +00002712/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2713///
2714void DwarfDebug::emitDebugPubNames() {
2715 // Start the dwarf pubnames section.
2716 Asm->OutStreamer.SwitchSection(
2717 Asm->getObjFileLowering().getDwarfPubNamesSection());
2718
2719 EmitDifference("pubnames_end", ModuleCU->getID(),
2720 "pubnames_begin", ModuleCU->getID(), true);
Bill Wendling94d04b82009-05-20 23:21:38 +00002721 Asm->EOL("Length of Public Names Info");
2722
Devang Patel8a241142009-12-09 18:24:21 +00002723 EmitLabel("pubnames_begin", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002724
2725 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2726
2727 EmitSectionOffset("info_begin", "section_info",
Devang Patel8a241142009-12-09 18:24:21 +00002728 ModuleCU->getID(), 0, true, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00002729 Asm->EOL("Offset of Compilation Unit Info");
2730
Devang Patel8a241142009-12-09 18:24:21 +00002731 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002732 true);
2733 Asm->EOL("Compilation Unit Length");
2734
Devang Patel8a241142009-12-09 18:24:21 +00002735 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling94d04b82009-05-20 23:21:38 +00002736 for (StringMap<DIE*>::const_iterator
2737 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2738 const char *Name = GI->getKeyData();
2739 DIE * Entity = GI->second;
2740
2741 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2742 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2743 }
2744
2745 Asm->EmitInt32(0); Asm->EOL("End Mark");
Devang Patel8a241142009-12-09 18:24:21 +00002746 EmitLabel("pubnames_end", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002747
2748 Asm->EOL();
2749}
2750
Devang Patel193f7202009-11-24 01:14:22 +00002751void DwarfDebug::emitDebugPubTypes() {
Devang Patelf3a03762009-11-24 19:18:41 +00002752 // Start the dwarf pubnames section.
2753 Asm->OutStreamer.SwitchSection(
2754 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patel193f7202009-11-24 01:14:22 +00002755 EmitDifference("pubtypes_end", ModuleCU->getID(),
2756 "pubtypes_begin", ModuleCU->getID(), true);
2757 Asm->EOL("Length of Public Types Info");
2758
2759 EmitLabel("pubtypes_begin", ModuleCU->getID());
2760
2761 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2762
2763 EmitSectionOffset("info_begin", "section_info",
2764 ModuleCU->getID(), 0, true, false);
2765 Asm->EOL("Offset of Compilation ModuleCU Info");
2766
2767 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2768 true);
2769 Asm->EOL("Compilation ModuleCU Length");
2770
2771 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2772 for (StringMap<DIE*>::const_iterator
2773 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2774 const char *Name = GI->getKeyData();
2775 DIE * Entity = GI->second;
2776
2777 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2778 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2779 }
2780
2781 Asm->EmitInt32(0); Asm->EOL("End Mark");
2782 EmitLabel("pubtypes_end", ModuleCU->getID());
2783
2784 Asm->EOL();
2785}
2786
Devang Patel2c4ceb12009-11-21 02:48:08 +00002787/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002788///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002789void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002790 // Check to see if it is worth the effort.
2791 if (!StringPool.empty()) {
2792 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002793 Asm->OutStreamer.SwitchSection(
2794 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002795
2796 // For each of strings in the string pool.
2797 for (unsigned StringID = 1, N = StringPool.size();
2798 StringID <= N; ++StringID) {
2799 // Emit a label for reference from debug information entries.
2800 EmitLabel("string", StringID);
2801
2802 // Emit the string itself.
2803 const std::string &String = StringPool[StringID];
2804 Asm->EmitString(String); Asm->EOL();
2805 }
2806
2807 Asm->EOL();
2808 }
2809}
2810
Devang Patel2c4ceb12009-11-21 02:48:08 +00002811/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002812///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002813void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002814 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002815 Asm->OutStreamer.SwitchSection(
2816 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002817 Asm->EOL();
2818}
2819
2820/// EmitDebugARanges - Emit visible names into a debug aranges section.
2821///
2822void DwarfDebug::EmitDebugARanges() {
2823 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002824 Asm->OutStreamer.SwitchSection(
2825 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002826
2827 // FIXME - Mock up
2828#if 0
2829 CompileUnit *Unit = GetBaseCompileUnit();
2830
2831 // Don't include size of length
2832 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2833
2834 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2835
2836 EmitReference("info_begin", Unit->getID());
2837 Asm->EOL("Offset of Compilation Unit Info");
2838
2839 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2840
2841 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2842
2843 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2844 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2845
2846 // Range 1
2847 EmitReference("text_begin", 0); Asm->EOL("Address");
2848 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2849
2850 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2851 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2852#endif
2853
2854 Asm->EOL();
2855}
2856
Devang Patel2c4ceb12009-11-21 02:48:08 +00002857/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002858///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002859void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002860 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002861 Asm->OutStreamer.SwitchSection(
2862 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002863 Asm->EOL();
2864}
2865
Devang Patel2c4ceb12009-11-21 02:48:08 +00002866/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002867///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002868void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002869 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002870 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002871 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002872 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002873 Asm->EOL();
2874 }
2875}
2876
Devang Patel2c4ceb12009-11-21 02:48:08 +00002877/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002878/// Section Header:
2879/// 1. length of section
2880/// 2. Dwarf version number
2881/// 3. address size.
2882///
2883/// Entries (one "entry" for each function that was inlined):
2884///
2885/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2886/// otherwise offset into __debug_str for regular function name.
2887/// 2. offset into __debug_str section for regular function name.
2888/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2889/// instances for the function.
2890///
2891/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2892/// inlined instance; the die_offset points to the inlined_subroutine die in the
2893/// __debug_info section, and the low_pc is the starting address for the
2894/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002895void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002896 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002897 return;
2898
Devang Patel1dbc7712009-06-29 20:45:18 +00002899 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002900 return;
2901
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002902 Asm->OutStreamer.SwitchSection(
2903 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002904 Asm->EOL();
2905 EmitDifference("debug_inlined_end", 1,
2906 "debug_inlined_begin", 1, true);
2907 Asm->EOL("Length of Debug Inlined Information Entry");
2908
2909 EmitLabel("debug_inlined_begin", 1);
2910
2911 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2912 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2913
Devang Patel53bb5c92009-11-10 23:06:00 +00002914 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2915 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002916
Devang Patel53bb5c92009-11-10 23:06:00 +00002917// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2918 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2919 MDNode *Node = *I;
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002920 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2921 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002922 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002923 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002924 StringRef LName = SP.getLinkageName();
2925 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002926
Devang Patel65dbc902009-11-25 17:36:49 +00002927 if (LName.empty())
Devang Patel53cb17d2009-07-16 01:01:22 +00002928 Asm->EmitString(Name);
2929 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002930 // Skip special LLVM prefix that is used to inform the asm printer to not
2931 // emit usual symbol prefix before the symbol name. This happens for
2932 // Objective-C symbol names and symbol whose name is replaced using GCC's
2933 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002934 if (LName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00002935 LName = LName.substr(1);
Devang Patel53bb5c92009-11-10 23:06:00 +00002936// Asm->EmitString(LName);
2937 EmitSectionOffset("string", "section_str",
2938 StringPool.idFor(LName), false, true);
2939
Devang Patel53cb17d2009-07-16 01:01:22 +00002940 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002941 Asm->EOL("MIPS linkage name");
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002942// Asm->EmitString(Name);
Devang Patel53bb5c92009-11-10 23:06:00 +00002943 EmitSectionOffset("string", "section_str",
2944 StringPool.idFor(Name), false, true);
2945 Asm->EOL("Function name");
Bill Wendling94d04b82009-05-20 23:21:38 +00002946 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2947
Devang Patel53bb5c92009-11-10 23:06:00 +00002948 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002949 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002950 DIE *SP = LI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00002951 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2952
2953 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002954 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002955 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002956 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002957
Devang Patel53bb5c92009-11-10 23:06:00 +00002958 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling94d04b82009-05-20 23:21:38 +00002959 }
2960 }
2961
2962 EmitLabel("debug_inlined_end", 1);
2963 Asm->EOL();
2964}