blob: 1cd28db11946014f3ab85bacb6a29ff915400047 [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.
115 DIEEntry *getDIEEntry(MDNode *N) { return GVToDIEEntryMap.lookup(N); }
116
117 /// insertDIEEntry - Insert debug information entry into the map.
118 void insertDIEEntry(MDNode *N, DIEEntry *E) {
119 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000120 }
121
Devang Patel2c4ceb12009-11-21 02:48:08 +0000122 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000123 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000124 void addDie(DIE *Buffer) {
125 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000126 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000127
128 // getIndexTyDie - Get an anonymous type for index type.
129 DIE *getIndexTyDie() {
130 return IndexTyDie;
131 }
132
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000133 // setIndexTyDie - Set D as anonymous type for index which can be reused
134 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000135 void setIndexTyDie(DIE *D) {
136 IndexTyDie = D;
137 }
138
Bill Wendling0310d762009-05-15 09:23:25 +0000139};
140
141//===----------------------------------------------------------------------===//
142/// DbgVariable - This class is used to track local variable information.
143///
Devang Patelf76a3d62009-11-16 21:53:40 +0000144class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000145 DIVariable Var; // Variable Descriptor.
146 unsigned FrameIndex; // Variable frame index.
Devang Patel53bb5c92009-11-10 23:06:00 +0000147 DbgVariable *AbstractVar; // Abstract variable for this variable.
148 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000149public:
Devang Patel53bb5c92009-11-10 23:06:00 +0000150 DbgVariable(DIVariable V, unsigned I)
151 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000152
153 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000154 DIVariable getVariable() const { return Var; }
155 unsigned getFrameIndex() const { return FrameIndex; }
156 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
157 DbgVariable *getAbstractVariable() const { return AbstractVar; }
158 void setDIE(DIE *D) { TheDIE = D; }
159 DIE *getDIE() const { return TheDIE; }
Bill Wendling0310d762009-05-15 09:23:25 +0000160};
161
162//===----------------------------------------------------------------------===//
163/// DbgScope - This class is used to track scope information.
164///
Devang Patelf76a3d62009-11-16 21:53:40 +0000165class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000166 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000167 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel53bb5c92009-11-10 23:06:00 +0000168 WeakVH InlinedAtLocation; // Location at which scope is inlined.
169 bool AbstractScope; // Abstract Scope
Bill Wendling0310d762009-05-15 09:23:25 +0000170 unsigned StartLabelID; // Label ID of the beginning of scope.
171 unsigned EndLabelID; // Label ID of the end of scope.
Devang Pateld38dd112009-10-01 18:25:23 +0000172 const MachineInstr *LastInsn; // Last instruction of this scope.
173 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000174 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
175 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000176
Owen Anderson04c05f72009-06-24 22:53:20 +0000177 // Private state for dump()
178 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000179public:
Devang Patelc90aefe2009-10-14 21:08:09 +0000180 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000181 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000182 StartLabelID(0), EndLabelID(0),
Devang Patelc90aefe2009-10-14 21:08:09 +0000183 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000184 virtual ~DbgScope();
185
186 // Accessors.
187 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000188 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000189 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000190 MDNode *getInlinedAt() const {
Devang Patel53bb5c92009-11-10 23:06:00 +0000191 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Patelc90aefe2009-10-14 21:08:09 +0000192 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000193 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000194 unsigned getStartLabelID() const { return StartLabelID; }
195 unsigned getEndLabelID() const { return EndLabelID; }
196 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
197 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendling0310d762009-05-15 09:23:25 +0000198 void setStartLabelID(unsigned S) { StartLabelID = S; }
199 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Pateld38dd112009-10-01 18:25:23 +0000200 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
201 const MachineInstr *getLastInsn() { return LastInsn; }
202 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000203 void setAbstractScope() { AbstractScope = true; }
204 bool isAbstractScope() const { return AbstractScope; }
Devang Pateld38dd112009-10-01 18:25:23 +0000205 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000206
Devang Patel2c4ceb12009-11-21 02:48:08 +0000207 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000208 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000209 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000210
Devang Patel2c4ceb12009-11-21 02:48:08 +0000211 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000212 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000213 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000214
Devang Patel2c4ceb12009-11-21 02:48:08 +0000215 void fixInstructionMarkers() {
Devang Patelaf9e8472009-10-01 20:31:14 +0000216 assert (getFirstInsn() && "First instruction is missing!");
217 if (getLastInsn())
218 return;
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000219
Devang Patelaf9e8472009-10-01 20:31:14 +0000220 // If a scope does not have an instruction to mark an end then use
221 // the end of last child scope.
222 SmallVector<DbgScope *, 4> &Scopes = getScopes();
223 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
224 DbgScope *L = Scopes.back();
225 if (!L->getLastInsn())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000226 L->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +0000227 setLastInsn(L->getLastInsn());
228 }
229
Bill Wendling0310d762009-05-15 09:23:25 +0000230#ifndef NDEBUG
231 void dump() const;
232#endif
233};
234
235#ifndef NDEBUG
236void DbgScope::dump() const {
Chris Lattnerc281de12009-08-23 00:51:00 +0000237 raw_ostream &err = errs();
238 err.indent(IndentLevel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000239 MDNode *N = Desc.getNode();
240 N->dump();
Chris Lattnerc281de12009-08-23 00:51:00 +0000241 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel53bb5c92009-11-10 23:06:00 +0000242 if (AbstractScope)
243 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000244
245 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000246 if (!Scopes.empty())
247 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000248 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
249 if (Scopes[i] != this)
250 Scopes[i]->dump();
251
252 IndentLevel -= 2;
253}
254#endif
255
Bill Wendling0310d762009-05-15 09:23:25 +0000256DbgScope::~DbgScope() {
257 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
258 delete Scopes[i];
259 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
260 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000261}
262
263} // end llvm namespace
264
Chris Lattneraf76e592009-08-22 20:48:53 +0000265DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000266 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000267 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patel2c4ceb12009-11-21 02:48:08 +0000268 DIEValues(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000269 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel53bb5c92009-11-10 23:06:00 +0000270 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000271 if (TimePassesIsEnabled)
272 DebugTimer = new Timer("Dwarf Debug Writer",
273 getDwarfTimerGroup());
274}
275DwarfDebug::~DwarfDebug() {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000276 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
277 delete DIEValues[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000278
Bill Wendling0310d762009-05-15 09:23:25 +0000279 delete DebugTimer;
280}
281
Devang Patel2c4ceb12009-11-21 02:48:08 +0000282/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000283///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000284void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000285 // Profile the node so that we can make it unique.
286 FoldingSetNodeID ID;
287 Abbrev.Profile(ID);
288
289 // Check the set for priors.
290 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
291
292 // If it's newly added.
293 if (InSet == &Abbrev) {
294 // Add to abbreviation list.
295 Abbreviations.push_back(&Abbrev);
296
297 // Assign the vector position + 1 as its number.
298 Abbrev.setNumber(Abbreviations.size());
299 } else {
300 // Assign existing abbreviation number.
301 Abbrev.setNumber(InSet->getNumber());
302 }
303}
304
Devang Patel2c4ceb12009-11-21 02:48:08 +0000305/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000306/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000307DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000308 DIEEntry *Value = new DIEEntry(Entry);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000309 DIEValues.push_back(Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000310 return Value;
311}
312
Devang Patel2c4ceb12009-11-21 02:48:08 +0000313/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000314///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000315void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000316 unsigned Form, uint64_t Integer) {
317 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000318 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000319 DIEValues.push_back(Value);
320 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000321}
322
Devang Patel2c4ceb12009-11-21 02:48:08 +0000323/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000324///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000325void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000326 unsigned Form, int64_t Integer) {
327 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000328 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000329 DIEValues.push_back(Value);
330 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000331}
332
Devang Patel69f57b12009-12-02 15:25:16 +0000333/// addString - Add a string attribute data and value. DIEString only
334/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000335void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patele9a05972009-11-24 19:42:17 +0000336 const StringRef String) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000337 DIEValue *Value = new DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000338 DIEValues.push_back(Value);
339 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000340}
341
Devang Patel2c4ceb12009-11-21 02:48:08 +0000342/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000343///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000344void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000345 const DWLabel &Label) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000346 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000347 DIEValues.push_back(Value);
348 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000349}
350
Devang Patel2c4ceb12009-11-21 02:48:08 +0000351/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000352///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000353void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000354 const std::string &Label) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000355 DIEValue *Value = new DIEObjectLabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000356 DIEValues.push_back(Value);
357 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000358}
359
Devang Patel2c4ceb12009-11-21 02:48:08 +0000360/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000361///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000362void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000363 const DWLabel &Label, const DWLabel &Section,
364 bool isEH, bool useSet) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000365 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000366 DIEValues.push_back(Value);
367 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000368}
369
Devang Patel2c4ceb12009-11-21 02:48:08 +0000370/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000371///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000372void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000373 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000374 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000375 DIEValues.push_back(Value);
376 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000377}
378
Devang Patel2c4ceb12009-11-21 02:48:08 +0000379/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000380///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000381void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000382 DIEBlock *Block) {
383 Block->ComputeSize(TD);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000384 DIEValues.push_back(Block);
385 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000386}
387
Devang Patel2c4ceb12009-11-21 02:48:08 +0000388/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000389/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000390void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendling0310d762009-05-15 09:23:25 +0000391 // If there is no compile unit specified, don't add a line #.
392 if (V->getCompileUnit().isNull())
393 return;
394
395 unsigned Line = V->getLineNumber();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000396 unsigned FileID = findCompileUnit(V->getCompileUnit()).getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000397 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000398 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
399 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000400}
401
Devang Patel2c4ceb12009-11-21 02:48:08 +0000402/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000403/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000404void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendling0310d762009-05-15 09:23:25 +0000405 // If there is no compile unit specified, don't add a line #.
406 if (G->getCompileUnit().isNull())
407 return;
408
409 unsigned Line = G->getLineNumber();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000410 unsigned FileID = findCompileUnit(G->getCompileUnit()).getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000411 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000412 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
413 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000414}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000415
Devang Patel2c4ceb12009-11-21 02:48:08 +0000416/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000417/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000418void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel82dfc0c2009-08-31 22:47:13 +0000419 // If there is no compile unit specified, don't add a line #.
420 if (SP->getCompileUnit().isNull())
421 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000422 // If the line number is 0, don't add it.
423 if (SP->getLineNumber() == 0)
424 return;
425
Devang Patel82dfc0c2009-08-31 22:47:13 +0000426
427 unsigned Line = SP->getLineNumber();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000428 unsigned FileID = findCompileUnit(SP->getCompileUnit()).getID();
Devang Patel82dfc0c2009-08-31 22:47:13 +0000429 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000430 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
431 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000432}
433
Devang Patel2c4ceb12009-11-21 02:48:08 +0000434/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000435/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000436void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000437 // If there is no compile unit specified, don't add a line #.
438 DICompileUnit CU = Ty->getCompileUnit();
439 if (CU.isNull())
440 return;
441
442 unsigned Line = Ty->getLineNumber();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000443 unsigned FileID = findCompileUnit(CU).getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000444 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000445 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
446 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000447}
448
Caroline Ticedc8f6042009-08-31 21:19:37 +0000449/* Byref variables, in Blocks, are declared by the programmer as
450 "SomeType VarName;", but the compiler creates a
451 __Block_byref_x_VarName struct, and gives the variable VarName
452 either the struct, or a pointer to the struct, as its type. This
453 is necessary for various behind-the-scenes things the compiler
454 needs to do with by-reference variables in blocks.
455
456 However, as far as the original *programmer* is concerned, the
457 variable should still have type 'SomeType', as originally declared.
458
459 The following function dives into the __Block_byref_x_VarName
460 struct to find the original type of the variable. This will be
461 passed back to the code generating the type for the Debug
462 Information Entry for the variable 'VarName'. 'VarName' will then
463 have the original type 'SomeType' in its debug information.
464
465 The original type 'SomeType' will be the type of the field named
466 'VarName' inside the __Block_byref_x_VarName struct.
467
468 NOTE: In order for this to not completely fail on the debugger
469 side, the Debug Information Entry for the variable VarName needs to
470 have a DW_AT_location that tells the debugger how to unwind through
471 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000472 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000473
474/// Find the type the programmer originally declared the variable to be
475/// and return that type.
476///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000477DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000478
479 DIType subType = Ty;
480 unsigned tag = Ty.getTag();
481
482 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000483 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000484 subType = DTy.getTypeDerivedFrom();
485 }
486
487 DICompositeType blockStruct = DICompositeType(subType.getNode());
488
489 DIArray Elements = blockStruct.getTypeArray();
490
491 if (Elements.isNull())
492 return Ty;
493
494 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
495 DIDescriptor Element = Elements.getElement(i);
496 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000497 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000498 return (DT.getTypeDerivedFrom());
499 }
500
501 return Ty;
502}
503
Devang Patel2c4ceb12009-11-21 02:48:08 +0000504/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000505/// and generate the DWARF information necessary to find the actual variable
506/// given the extra address information encoded in the DIVariable, starting from
507/// the starting location. Add the DWARF information to the die.
508///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000509void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000510 unsigned Attribute,
511 const MachineLocation &Location) {
512 const DIVariable &VD = DV->getVariable();
513 DIType Ty = VD.getType();
514
515 // Decode the original location, and use that as the start of the byref
516 // variable's location.
517 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
518 DIEBlock *Block = new DIEBlock();
519
520 if (Location.isReg()) {
521 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000522 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000523 } else {
524 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000525 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
526 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000527 }
528 } else {
529 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000530 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000531 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000532 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
533 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000534 }
535
Devang Patel2c4ceb12009-11-21 02:48:08 +0000536 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000537 }
538
539 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
540 uint64_t Element = VD.getAddrElement(i);
541
542 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000543 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
544 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000545 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000546 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000547 } else llvm_unreachable("unknown DIFactory Opcode");
548 }
549
550 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000551 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000552}
553
Caroline Ticedc8f6042009-08-31 21:19:37 +0000554/* Byref variables, in Blocks, are declared by the programmer as "SomeType
555 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
556 gives the variable VarName either the struct, or a pointer to the struct, as
557 its type. This is necessary for various behind-the-scenes things the
558 compiler needs to do with by-reference variables in Blocks.
559
560 However, as far as the original *programmer* is concerned, the variable
561 should still have type 'SomeType', as originally declared.
562
Devang Patel2c4ceb12009-11-21 02:48:08 +0000563 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000564 struct to find the original type of the variable, which is then assigned to
565 the variable's Debug Information Entry as its real type. So far, so good.
566 However now the debugger will expect the variable VarName to have the type
567 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000568 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000569 pointers and struct to find the actual variable of type SomeType.
570
571 The following function does just that. We start by getting
572 the "normal" location for the variable. This will be the location
573 of either the struct __Block_byref_x_VarName or the pointer to the
574 struct __Block_byref_x_VarName.
575
576 The struct will look something like:
577
578 struct __Block_byref_x_VarName {
579 ... <various fields>
580 struct __Block_byref_x_VarName *forwarding;
581 ... <various other fields>
582 SomeType VarName;
583 ... <maybe more fields>
584 };
585
586 If we are given the struct directly (as our starting point) we
587 need to tell the debugger to:
588
589 1). Add the offset of the forwarding field.
590
591 2). Follow that pointer to get the the real __Block_byref_x_VarName
592 struct to use (the real one may have been copied onto the heap).
593
594 3). Add the offset for the field VarName, to find the actual variable.
595
596 If we started with a pointer to the struct, then we need to
597 dereference that pointer first, before the other steps.
598 Translating this into DWARF ops, we will need to append the following
599 to the current location description for the variable:
600
601 DW_OP_deref -- optional, if we start with a pointer
602 DW_OP_plus_uconst <forward_fld_offset>
603 DW_OP_deref
604 DW_OP_plus_uconst <varName_fld_offset>
605
606 That is what this function does. */
607
Devang Patel2c4ceb12009-11-21 02:48:08 +0000608/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000609/// provided, and generate the DWARF information necessary to find the
610/// actual Block variable (navigating the Block struct) based on the
611/// starting location. Add the DWARF information to the die. For
612/// more information, read large comment just above here.
613///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000614void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000615 unsigned Attribute,
616 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000617 const DIVariable &VD = DV->getVariable();
618 DIType Ty = VD.getType();
619 DIType TmpTy = Ty;
620 unsigned Tag = Ty.getTag();
621 bool isPointer = false;
622
Devang Patel65dbc902009-11-25 17:36:49 +0000623 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000624
625 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000626 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000627 TmpTy = DTy.getTypeDerivedFrom();
628 isPointer = true;
629 }
630
631 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
632
Daniel Dunbar00564992009-09-19 20:40:14 +0000633 // Find the __forwarding field and the variable field in the __Block_byref
634 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000635 DIArray Fields = blockStruct.getTypeArray();
636 DIDescriptor varField = DIDescriptor();
637 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000638
639
Daniel Dunbar00564992009-09-19 20:40:14 +0000640 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
641 DIDescriptor Element = Fields.getElement(i);
642 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000643 StringRef fieldName = DT.getName();
644 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000645 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000646 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000647 varField = Element;
648 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000649
Mike Stump7e3720d2009-09-24 23:21:26 +0000650 assert(!varField.isNull() && "Can't find byref variable in Block struct");
651 assert(!forwardingField.isNull()
652 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000653
Daniel Dunbar00564992009-09-19 20:40:14 +0000654 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000655 unsigned int forwardingFieldOffset =
656 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
657 unsigned int varFieldOffset =
658 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000659
Mike Stump7e3720d2009-09-24 23:21:26 +0000660 // Decode the original location, and use that as the start of the byref
661 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000662 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
663 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000664
Daniel Dunbar00564992009-09-19 20:40:14 +0000665 if (Location.isReg()) {
666 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000667 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000668 else {
669 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000670 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
671 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000672 }
673 } else {
674 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000675 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000676 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000677 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
678 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000679 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000680
Devang Patel2c4ceb12009-11-21 02:48:08 +0000681 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000682 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000683
Mike Stump7e3720d2009-09-24 23:21:26 +0000684 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000685 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000686 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000687 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000688
Daniel Dunbar00564992009-09-19 20:40:14 +0000689 // Next add the offset for the '__forwarding' field:
690 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
691 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000692 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000693 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
694 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000695 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000696
Daniel Dunbar00564992009-09-19 20:40:14 +0000697 // Now dereference the __forwarding field to get to the real __Block_byref
698 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000699 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000700
Daniel Dunbar00564992009-09-19 20:40:14 +0000701 // Now that we've got the real __Block_byref... struct, add the offset
702 // for the variable's field to get to the location of the actual variable:
703 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000704 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000705 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
706 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000707 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000708
Daniel Dunbar00564992009-09-19 20:40:14 +0000709 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000710 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000711}
712
Devang Patel2c4ceb12009-11-21 02:48:08 +0000713/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000714/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000715void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000716 const MachineLocation &Location) {
717 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
718 DIEBlock *Block = new DIEBlock();
719
720 if (Location.isReg()) {
721 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000722 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000723 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000724 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
725 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000726 }
727 } else {
728 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000729 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000730 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000731 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
732 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000733 }
734
Devang Patel2c4ceb12009-11-21 02:48:08 +0000735 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000736 }
737
Devang Patel2c4ceb12009-11-21 02:48:08 +0000738 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000739}
740
Devang Patelc366f832009-12-10 19:14:49 +0000741/// addToContextOwner - Add Die into the list of its context owner's children.
742void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
743 if (Context.isNull())
744 ModuleCU->addDie(Die);
745 else if (Context.isType()) {
746 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
747 ContextDIE->addChild(Die);
748 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
749 ContextDIE->addChild(Die);
750 else
751 ModuleCU->addDie(Die);
752}
753
Devang Patel16ced732009-12-10 18:05:33 +0000754/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
755/// given DIType.
756DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
757 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
758 if (TyDIE)
759 return TyDIE;
760
761 // Create new type.
762 TyDIE = new DIE(dwarf::DW_TAG_base_type);
763 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
764 if (Ty.isBasicType())
765 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
766 else if (Ty.isCompositeType())
767 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
768 else {
769 assert(Ty.isDerivedType() && "Unknown kind of DIType");
770 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
771 }
772
Devang Patelc366f832009-12-10 19:14:49 +0000773 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000774 return TyDIE;
775}
776
Devang Patel2c4ceb12009-11-21 02:48:08 +0000777/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000778void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000779 if (Ty.isNull())
780 return;
781
782 // Check for pre-existence.
Devang Patel8a241142009-12-09 18:24:21 +0000783 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000784
785 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000786 if (Entry) {
787 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000788 return;
789 }
790
791 // Set up proxy.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000792 Entry = createDIEEntry();
Devang Patel8a241142009-12-09 18:24:21 +0000793 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000794
795 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000796 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000797
Devang Patel2c4ceb12009-11-21 02:48:08 +0000798 Entry->setEntry(Buffer);
799 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000800}
801
Devang Patel2c4ceb12009-11-21 02:48:08 +0000802/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000803void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000804 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000805 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000806 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000807 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000808 BTy.getEncoding());
809
810 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000811 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000812 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000813 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000814 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000815}
816
Devang Patel2c4ceb12009-11-21 02:48:08 +0000817/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000818void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000819 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000820 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000821 uint64_t Size = DTy.getSizeInBits() >> 3;
822 unsigned Tag = DTy.getTag();
823
824 // FIXME - Workaround for templates.
825 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
826
827 Buffer.setTag(Tag);
828
829 // Map to main type, void will not have a type.
830 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000831 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000832
833 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000834 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000835 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000836
837 // Add size if non-zero (derived types might be zero-sized.)
838 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000839 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000840
841 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000842 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000843 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000844}
845
Devang Patel2c4ceb12009-11-21 02:48:08 +0000846/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000847void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000848 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000849 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000850
851 uint64_t Size = CTy.getSizeInBits() >> 3;
852 unsigned Tag = CTy.getTag();
853 Buffer.setTag(Tag);
854
855 switch (Tag) {
856 case dwarf::DW_TAG_vector_type:
857 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +0000858 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000859 break;
860 case dwarf::DW_TAG_enumeration_type: {
861 DIArray Elements = CTy.getTypeArray();
862
863 // Add enumerators to enumeration type.
864 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
865 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000866 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelc5254722009-10-09 17:51:49 +0000867 if (!Enum.isNull()) {
Devang Patel8a241142009-12-09 18:24:21 +0000868 ElemDie = constructEnumTypeDIE(&Enum);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000869 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +0000870 }
Bill Wendling0310d762009-05-15 09:23:25 +0000871 }
872 }
873 break;
874 case dwarf::DW_TAG_subroutine_type: {
875 // Add return type.
876 DIArray Elements = CTy.getTypeArray();
877 DIDescriptor RTy = Elements.getElement(0);
Devang Patel8a241142009-12-09 18:24:21 +0000878 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000879
880 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000881 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000882
883 // Add arguments.
884 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
885 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
886 DIDescriptor Ty = Elements.getElement(i);
Devang Patel8a241142009-12-09 18:24:21 +0000887 addType(Arg, DIType(Ty.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000888 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000889 }
890 }
891 break;
892 case dwarf::DW_TAG_structure_type:
893 case dwarf::DW_TAG_union_type:
894 case dwarf::DW_TAG_class_type: {
895 // Add elements to structure type.
896 DIArray Elements = CTy.getTypeArray();
897
898 // A forward struct declared type may not have elements available.
899 if (Elements.isNull())
900 break;
901
902 // Add elements to structure type.
903 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
904 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000905 if (Element.isNull())
906 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000907 DIE *ElemDie = NULL;
908 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel8a241142009-12-09 18:24:21 +0000909 ElemDie = createMemberSubprogramDIE(DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000910 else
Devang Patel8a241142009-12-09 18:24:21 +0000911 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000912 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +0000913 }
914
Devang Patela1ba2692009-08-27 23:51:51 +0000915 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000916 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000917
918 unsigned RLang = CTy.getRunTimeLang();
919 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000920 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +0000921 dwarf::DW_FORM_data1, RLang);
922 break;
923 }
924 default:
925 break;
926 }
927
928 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000929 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000930 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000931
932 if (Tag == dwarf::DW_TAG_enumeration_type ||
933 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
934 // Add size if non-zero (derived types might be zero-sized.)
935 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000936 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000937 else {
938 // Add zero size if it is not a forward declaration.
939 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000940 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000941 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000942 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +0000943 }
944
945 // Add source line info if available.
946 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000947 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000948 }
949}
950
Devang Patel2c4ceb12009-11-21 02:48:08 +0000951/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
952void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +0000953 int64_t L = SR.getLo();
954 int64_t H = SR.getHi();
955 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
956
Devang Patel2c4ceb12009-11-21 02:48:08 +0000957 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +0000958 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000959 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +0000960 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000961
Devang Patel2c4ceb12009-11-21 02:48:08 +0000962 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +0000963}
964
Devang Patel2c4ceb12009-11-21 02:48:08 +0000965/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000966void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000967 DICompositeType *CTy) {
968 Buffer.setTag(dwarf::DW_TAG_array_type);
969 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000970 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000971
972 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +0000973 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +0000974 DIArray Elements = CTy->getTypeArray();
975
Devang Patel6f01d9c2009-11-21 00:31:03 +0000976 // Get an anonymous type for index type.
Devang Patel8a241142009-12-09 18:24:21 +0000977 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +0000978 if (!IdxTy) {
979 // Construct an anonymous type for index type.
980 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000981 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
982 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +0000983 dwarf::DW_ATE_signed);
Devang Patel8a241142009-12-09 18:24:21 +0000984 ModuleCU->addDie(IdxTy);
985 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000986 }
Bill Wendling0310d762009-05-15 09:23:25 +0000987
988 // Add subranges to array type.
989 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
990 DIDescriptor Element = Elements.getElement(i);
991 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000992 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000993 }
994}
995
Devang Patel2c4ceb12009-11-21 02:48:08 +0000996/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel8a241142009-12-09 18:24:21 +0000997DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator *ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000998 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel65dbc902009-11-25 17:36:49 +0000999 StringRef Name = ETy->getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001000 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001001 int64_t Value = ETy->getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001002 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001003 return Enumerator;
1004}
1005
Devang Patel2c4ceb12009-11-21 02:48:08 +00001006/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel8a241142009-12-09 18:24:21 +00001007DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001008 // If the global variable was optmized out then no need to create debug info
1009 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001010 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001011 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001012
Bill Wendling0310d762009-05-15 09:23:25 +00001013 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001014 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001015 GV.getDisplayName());
1016
Devang Patel65dbc902009-11-25 17:36:49 +00001017 StringRef LinkageName = GV.getLinkageName();
1018 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001019 // Skip special LLVM prefix that is used to inform the asm printer to not
1020 // emit usual symbol prefix before the symbol name. This happens for
1021 // Objective-C symbol names and symbol whose name is replaced using GCC's
1022 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001023 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001024 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001025 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001026 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001027 }
Devang Patel8a241142009-12-09 18:24:21 +00001028 addType(GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001029 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001030 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1031 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001032
1033 // Add address.
1034 DIEBlock *Block = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001035 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1036 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patelb71a16d2009-10-05 23:22:08 +00001037 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001038 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patelb71a16d2009-10-05 23:22:08 +00001039
Bill Wendling0310d762009-05-15 09:23:25 +00001040 return GVDie;
1041}
1042
Devang Patel2c4ceb12009-11-21 02:48:08 +00001043/// createMemberDIE - Create new member DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001044DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001045 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001046 StringRef Name = DT.getName();
1047 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001048 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001049
Devang Patel8a241142009-12-09 18:24:21 +00001050 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001051
Devang Patel2c4ceb12009-11-21 02:48:08 +00001052 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001053
Devang Patel33db5082009-11-04 22:06:12 +00001054 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001055 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001056
Bill Wendling0310d762009-05-15 09:23:25 +00001057 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001058 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001059
1060 if (Size != FieldSize) {
1061 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001062 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1063 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001064
1065 uint64_t Offset = DT.getOffsetInBits();
1066 uint64_t FieldOffset = Offset;
1067 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1068 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1069 FieldOffset = (HiMark - FieldSize);
1070 Offset -= FieldOffset;
1071
1072 // Maybe we need to work from the other end.
1073 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001074 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001075
Devang Patel33db5082009-11-04 22:06:12 +00001076 // Here WD_AT_data_member_location points to the anonymous
1077 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001078 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001079
1080 } else
1081 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001082 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001083
Devang Patel2c4ceb12009-11-21 02:48:08 +00001084 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001085
1086 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001087 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001088 dwarf::DW_ACCESS_protected);
1089 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001090 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001091 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001092 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1093 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1094 dwarf::DW_ACCESS_public);
1095 if (DT.isVirtual())
1096 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1097 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001098 return MemberDie;
1099}
1100
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001101/// createRawSubprogramDIE - Create new partially incomplete DIE. This is
1102/// a helper routine used by createMemberSubprogramDIE and
1103/// createSubprogramDIE.
Devang Patel8a241142009-12-09 18:24:21 +00001104DIE *DwarfDebug::createRawSubprogramDIE(const DISubprogram &SP) {
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001105 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel65dbc902009-11-25 17:36:49 +00001106 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001107
Devang Patel65dbc902009-11-25 17:36:49 +00001108 StringRef LinkageName = SP.getLinkageName();
1109 if (!LinkageName.empty()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001110 // Skip special LLVM prefix that is used to inform the asm printer to not
1111 // emit usual symbol prefix before the symbol name. This happens for
1112 // Objective-C symbol names and symbol whose name is replaced using GCC's
1113 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001114 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001115 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001116 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001117 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001118 }
Devang Patel2c4ceb12009-11-21 02:48:08 +00001119 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001120
Bill Wendling0310d762009-05-15 09:23:25 +00001121 // Add prototyped tag, if C or ObjC.
1122 unsigned Lang = SP.getCompileUnit().getLanguage();
1123 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1124 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001125 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001126
1127 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001128 DICompositeType SPTy = SP.getType();
1129 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001130 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001131
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001132 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001133 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001134 else
Devang Patel8a241142009-12-09 18:24:21 +00001135 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001136
Devang Patel5d11eb02009-12-03 19:11:07 +00001137 unsigned VK = SP.getVirtuality();
1138 if (VK) {
1139 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1140 DIEBlock *Block = new DIEBlock();
1141 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1142 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1143 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1144 ContainingTypeMap.insert(std::make_pair(SPDie, WeakVH(SP.getContainingType().getNode())));
1145 }
1146
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001147 return SPDie;
1148}
1149
1150/// createMemberSubprogramDIE - Create new member DIE using SP. This routine
1151/// always returns a die with DW_AT_declaration attribute.
Devang Patel8a241142009-12-09 18:24:21 +00001152DIE *DwarfDebug::createMemberSubprogramDIE(const DISubprogram &SP) {
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001153 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1154 if (!SPDie)
Devang Patel8a241142009-12-09 18:24:21 +00001155 SPDie = createSubprogramDIE(SP);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001156
1157 // If SPDie has DW_AT_declaration then reuse it.
1158 if (!SP.isDefinition())
1159 return SPDie;
1160
1161 // Otherwise create new DIE for the declaration. First push definition
1162 // DIE at the top level.
1163 if (TopLevelDIEs.insert(SPDie))
1164 TopLevelDIEsVector.push_back(SPDie);
1165
Devang Patel8a241142009-12-09 18:24:21 +00001166 SPDie = createRawSubprogramDIE(SP);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001167
1168 // Add arguments.
1169 DICompositeType SPTy = SP.getType();
1170 DIArray Args = SPTy.getTypeArray();
1171 unsigned SPTag = SPTy.getTag();
1172 if (SPTag == dwarf::DW_TAG_subroutine_type)
1173 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1174 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel8a241142009-12-09 18:24:21 +00001175 addType(Arg, DIType(Args.getElement(i).getNode()));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001176 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1177 SPDie->addChild(Arg);
1178 }
1179
1180 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1181 return SPDie;
1182}
1183
1184/// createSubprogramDIE - Create new DIE using SP.
Devang Patel8a241142009-12-09 18:24:21 +00001185DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP) {
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001186 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1187 if (SPDie)
1188 return SPDie;
1189
Devang Patel8a241142009-12-09 18:24:21 +00001190 SPDie = createRawSubprogramDIE(SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001191
1192 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001193 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001194
1195 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001196 // be handled while processing variables.
1197 DICompositeType SPTy = SP.getType();
1198 DIArray Args = SPTy.getTypeArray();
1199 unsigned SPTag = SPTy.getTag();
1200
Bill Wendling0310d762009-05-15 09:23:25 +00001201 if (SPTag == dwarf::DW_TAG_subroutine_type)
1202 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1203 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel8a241142009-12-09 18:24:21 +00001204 addType(Arg, DIType(Args.getElement(i).getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001205 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1206 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001207 }
1208 }
1209
Bill Wendling0310d762009-05-15 09:23:25 +00001210 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001211 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001212 return SPDie;
1213}
1214
Devang Patel2c4ceb12009-11-21 02:48:08 +00001215/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendling0310d762009-05-15 09:23:25 +00001216///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001217CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const {
Bill Wendling0310d762009-05-15 09:23:25 +00001218 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001219 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001220 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1221 return *I->second;
1222}
1223
Devang Patel53bb5c92009-11-10 23:06:00 +00001224/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1225/// Initialize scope and update scope hierarchy.
1226DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1227 MDNode *InlinedAt) {
1228 assert (N && "Invalid Scope encoding!");
1229 assert (MI && "Missing machine instruction!");
1230 bool GetConcreteScope = (MI && InlinedAt);
1231
1232 DbgScope *NScope = NULL;
1233
1234 if (InlinedAt)
1235 NScope = DbgScopeMap.lookup(InlinedAt);
1236 else
1237 NScope = DbgScopeMap.lookup(N);
1238 assert (NScope && "Unable to find working scope!");
1239
1240 if (NScope->getFirstInsn())
1241 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001242
1243 DbgScope *Parent = NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001244 if (GetConcreteScope) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001245 DILocation IL(InlinedAt);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001246 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel53bb5c92009-11-10 23:06:00 +00001247 IL.getOrigLocation().getNode());
1248 assert (Parent && "Unable to find Parent scope!");
1249 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001250 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001251 } else if (DIDescriptor(N).isLexicalBlock()) {
1252 DILexicalBlock DB(N);
1253 if (!DB.getContext().isNull()) {
1254 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1255 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001256 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001257 }
Devang Patelc90aefe2009-10-14 21:08:09 +00001258 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001259
Devang Patelbdf45cb2009-10-27 20:47:17 +00001260 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001261
Devang Patel53bb5c92009-11-10 23:06:00 +00001262 if (!Parent && !InlinedAt) {
Devang Patel39ae3ff2009-11-11 00:31:36 +00001263 StringRef SPName = DISubprogram(N).getLinkageName();
1264 if (SPName == MF->getFunction()->getName())
1265 CurrentFnDbgScope = NScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001266 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001267
Devang Patel53bb5c92009-11-10 23:06:00 +00001268 if (GetConcreteScope) {
1269 ConcreteScopes[InlinedAt] = NScope;
1270 getOrCreateAbstractScope(N);
1271 }
1272
Devang Patelbdf45cb2009-10-27 20:47:17 +00001273 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001274}
1275
Devang Patel53bb5c92009-11-10 23:06:00 +00001276DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1277 assert (N && "Invalid Scope encoding!");
1278
1279 DbgScope *AScope = AbstractScopes.lookup(N);
1280 if (AScope)
1281 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001282
Devang Patel53bb5c92009-11-10 23:06:00 +00001283 DbgScope *Parent = NULL;
1284
1285 DIDescriptor Scope(N);
1286 if (Scope.isLexicalBlock()) {
1287 DILexicalBlock DB(N);
1288 DIDescriptor ParentDesc = DB.getContext();
1289 if (!ParentDesc.isNull())
1290 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1291 }
1292
1293 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1294
1295 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001296 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001297 AScope->setAbstractScope();
1298 AbstractScopes[N] = AScope;
1299 if (DIDescriptor(N).isSubprogram())
1300 AbstractScopesList.push_back(AScope);
1301 return AScope;
1302}
Devang Patelaf9e8472009-10-01 20:31:14 +00001303
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001304/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001305/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1306/// If there are global variables in this scope then create and insert
1307/// DIEs for these variables.
1308DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001309
Devang Patel017d1212009-11-20 21:37:22 +00001310 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel53bb5c92009-11-10 23:06:00 +00001311 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001312 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001313 DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001314 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001315 DWLabel("func_end", SubprogramCount));
1316 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001317 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001318
Devang Patel53bb5c92009-11-10 23:06:00 +00001319 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001320 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001321
Devang Patel53bb5c92009-11-10 23:06:00 +00001322 return SPDie;
1323}
1324
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001325/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001326/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1327DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001328 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1329 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1330
1331 // Ignore empty scopes.
1332 if (StartID == EndID && StartID != 0)
1333 return NULL;
1334
1335 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1336 if (Scope->isAbstractScope())
1337 return ScopeDIE;
1338
Devang Patel2c4ceb12009-11-21 02:48:08 +00001339 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001340 StartID ?
1341 DWLabel("label", StartID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001342 : DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001343 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001344 EndID ?
1345 DWLabel("label", EndID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001346 : DWLabel("func_end", SubprogramCount));
1347
1348
1349
1350 return ScopeDIE;
1351}
1352
Devang Patel2c4ceb12009-11-21 02:48:08 +00001353/// constructInlinedScopeDIE - This scope represents inlined body of
1354/// a function. Construct DIE to represent this concrete inlined copy
1355/// of the function.
1356DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001357 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1358 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1359 assert (StartID && "Invalid starting label for an inlined scope!");
1360 assert (EndID && "Invalid end label for an inlined scope!");
1361 // Ignore empty scopes.
1362 if (StartID == EndID && StartID != 0)
1363 return NULL;
1364
1365 DIScope DS(Scope->getScopeNode());
1366 if (DS.isNull())
1367 return NULL;
1368 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1369
1370 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001371 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001372 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001373 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001374 dwarf::DW_FORM_ref4, OriginDIE);
1375
Devang Patel2c4ceb12009-11-21 02:48:08 +00001376 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001377 DWLabel("label", StartID));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001378 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001379 DWLabel("label", EndID));
1380
1381 InlinedSubprogramDIEs.insert(OriginDIE);
1382
1383 // Track the start label for this inlined function.
1384 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1385 I = InlineInfo.find(InlinedSP.getNode());
1386
1387 if (I == InlineInfo.end()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001388 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1389 ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001390 InlinedSPNodes.push_back(InlinedSP.getNode());
1391 } else
1392 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1393
1394 StringPool.insert(InlinedSP.getName());
1395 StringPool.insert(InlinedSP.getLinkageName());
1396 DILocation DL(Scope->getInlinedAt());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001397 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1398 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001399
1400 return ScopeDIE;
1401}
1402
Devang Patel2c4ceb12009-11-21 02:48:08 +00001403
1404/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001405DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001406 // Get the descriptor.
1407 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001408 StringRef Name = VD.getName();
1409 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001410 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001411
1412 // Translate tag to proper Dwarf tag. The result variable is dropped for
1413 // now.
1414 unsigned Tag;
1415 switch (VD.getTag()) {
1416 case dwarf::DW_TAG_return_variable:
1417 return NULL;
1418 case dwarf::DW_TAG_arg_variable:
1419 Tag = dwarf::DW_TAG_formal_parameter;
1420 break;
1421 case dwarf::DW_TAG_auto_variable: // fall thru
1422 default:
1423 Tag = dwarf::DW_TAG_variable;
1424 break;
1425 }
1426
1427 // Define variable debug information entry.
1428 DIE *VariableDie = new DIE(Tag);
1429
1430
1431 DIE *AbsDIE = NULL;
1432 if (DbgVariable *AV = DV->getAbstractVariable())
1433 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001434
Devang Patel53bb5c92009-11-10 23:06:00 +00001435 if (AbsDIE) {
1436 DIScope DS(Scope->getScopeNode());
1437 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001438 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc0326792009-11-11 03:09:50 +00001439 (void) OriginSPDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001440 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1441 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1442 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001443 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001444 dwarf::DW_FORM_ref4, AbsDIE);
1445 }
1446 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001447 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1448 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001449
1450 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001451 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001452 // addresses instead.
1453 if (VD.isBlockByrefVariable())
Devang Patel8a241142009-12-09 18:24:21 +00001454 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001455 else
Devang Patel8a241142009-12-09 18:24:21 +00001456 addType(VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001457 }
1458
1459 // Add variable address.
1460 if (!Scope->isAbstractScope()) {
1461 MachineLocation Location;
Jim Grosbacha2f20b22009-11-22 20:14:00 +00001462 unsigned FrameReg;
1463 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1464 Location.set(FrameReg, Offset);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001465
Devang Patel53bb5c92009-11-10 23:06:00 +00001466 if (VD.hasComplexAddress())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001467 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001468 else if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001469 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001470 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001471 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001472 }
1473 DV->setDIE(VariableDie);
1474 return VariableDie;
1475
1476}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001477
Devang Patel193f7202009-11-24 01:14:22 +00001478void DwarfDebug::addPubTypes(DISubprogram SP) {
1479 DICompositeType SPTy = SP.getType();
1480 unsigned SPTag = SPTy.getTag();
1481 if (SPTag != dwarf::DW_TAG_subroutine_type)
1482 return;
1483
1484 DIArray Args = SPTy.getTypeArray();
1485 if (Args.isNull())
1486 return;
1487
1488 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1489 DIType ATy(Args.getElement(i).getNode());
1490 if (ATy.isNull())
1491 continue;
1492 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel65dbc902009-11-25 17:36:49 +00001493 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001494 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1495 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1496 }
1497 }
1498}
1499
Devang Patel2c4ceb12009-11-21 02:48:08 +00001500/// constructScopeDIE - Construct a DIE for this scope.
1501DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001502 if (!Scope)
1503 return NULL;
1504 DIScope DS(Scope->getScopeNode());
1505 if (DS.isNull())
1506 return NULL;
1507
1508 DIE *ScopeDIE = NULL;
1509 if (Scope->getInlinedAt())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001510 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001511 else if (DS.isSubprogram()) {
1512 if (Scope->isAbstractScope())
Devang Patel017d1212009-11-20 21:37:22 +00001513 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001514 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001515 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001516 }
1517 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001518 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001519 if (!ScopeDIE) return NULL;
1520 }
1521
1522 // Add variables to scope.
1523 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1524 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001525 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001526 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001527 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001528 }
1529
1530 // Add nested scopes.
1531 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1532 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1533 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001534 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001535 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001536 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001537 }
Devang Patel193f7202009-11-24 01:14:22 +00001538
1539 if (DS.isSubprogram())
1540 addPubTypes(DISubprogram(DS.getNode()));
1541
1542 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001543}
1544
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001545/// GetOrCreateSourceID - Look up the source id with the given directory and
1546/// source file names. If none currently exists, create a new id and insert it
1547/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1548/// maps as well.
Devang Patel65dbc902009-11-25 17:36:49 +00001549unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001550 unsigned DId;
1551 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1552 if (DI != DirectoryIdMap.end()) {
1553 DId = DI->getValue();
1554 } else {
1555 DId = DirectoryNames.size() + 1;
1556 DirectoryIdMap[DirName] = DId;
1557 DirectoryNames.push_back(DirName);
1558 }
1559
1560 unsigned FId;
1561 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1562 if (FI != SourceFileIdMap.end()) {
1563 FId = FI->getValue();
1564 } else {
1565 FId = SourceFileNames.size() + 1;
1566 SourceFileIdMap[FileName] = FId;
1567 SourceFileNames.push_back(FileName);
1568 }
1569
1570 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1571 SourceIdMap.find(std::make_pair(DId, FId));
1572 if (SI != SourceIdMap.end())
1573 return SI->second;
1574
1575 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1576 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1577 SourceIds.push_back(std::make_pair(DId, FId));
1578
1579 return SrcId;
1580}
1581
Devang Patel2c4ceb12009-11-21 02:48:08 +00001582void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001583 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001584 StringRef FN = DIUnit.getFilename();
1585 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001586 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001587
1588 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001589 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001590 DWLabel("section_line", 0), DWLabel("section_line", 0),
1591 false);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001592 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001593 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001594 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001595 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001596 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001597
Devang Patel65dbc902009-11-25 17:36:49 +00001598 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001599 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001600 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001601 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001602
Devang Patel65dbc902009-11-25 17:36:49 +00001603 StringRef Flags = DIUnit.getFlags();
1604 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001605 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001606
1607 unsigned RVer = DIUnit.getRunTimeVersion();
1608 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001609 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001610 dwarf::DW_FORM_data1, RVer);
1611
1612 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001613 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001614 // Use first compile unit marked as isMain as the compile unit
1615 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001616 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001617 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001618
Devang Patele4b27562009-08-28 23:24:31 +00001619 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001620 CompileUnits.push_back(Unit);
1621}
1622
Devang Patel2c4ceb12009-11-21 02:48:08 +00001623void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001624 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001625
Devang Patel905cf5e2009-09-04 23:59:07 +00001626 // If debug information is malformed then ignore it.
1627 if (DI_GV.Verify() == false)
1628 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001629
1630 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001631 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel13e16b62009-06-26 01:49:18 +00001632 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001633
Devang Patel8a241142009-12-09 18:24:21 +00001634 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Pateledb45632009-12-10 23:25:41 +00001635 if (!VariableDie)
1636 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001637
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001638 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001639 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001640
1641 // Add to context owner.
Devang Patelc366f832009-12-10 19:14:49 +00001642 addToContextOwner(VariableDie, DI_GV.getContext());
1643
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001644 // Expose as global. FIXME - need to check external flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001645 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001646
1647 DIType GTy = DI_GV.getType();
Devang Patel65dbc902009-11-25 17:36:49 +00001648 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001649 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1650 assert (Entry && "Missing global type!");
1651 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1652 }
Devang Patel13e16b62009-06-26 01:49:18 +00001653 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001654}
1655
Devang Patel2c4ceb12009-11-21 02:48:08 +00001656void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001657 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001658
1659 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001660 if (ModuleCU->getDIE(N))
Devang Patel13e16b62009-06-26 01:49:18 +00001661 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001662
1663 if (!SP.isDefinition())
1664 // This is a method declaration which will be handled while constructing
1665 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001666 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001667
Devang Patel8a241142009-12-09 18:24:21 +00001668 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001669
1670 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001671 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001672
1673 // Add to context owner.
Devang Patel0000fad2009-12-08 23:21:45 +00001674 if (SP.getContext().getNode() == SP.getCompileUnit().getNode())
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001675 if (TopLevelDIEs.insert(SubprogramDie))
1676 TopLevelDIEsVector.push_back(SubprogramDie);
Devang Patel0000fad2009-12-08 23:21:45 +00001677
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001678 // Expose as global.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001679 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001680
Devang Patel13e16b62009-06-26 01:49:18 +00001681 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001682}
1683
Devang Patel2c4ceb12009-11-21 02:48:08 +00001684/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001685/// content. Create global DIEs and emit initial debug info sections.
1686/// This is inovked by the target AsmPrinter.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001687void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel208622d2009-06-25 22:36:02 +00001688 this->M = M;
1689
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001690 if (TimePassesIsEnabled)
1691 DebugTimer->startTimer();
1692
Devang Patel3380cc52009-11-11 19:55:08 +00001693 if (!MAI->doesSupportDebugInformation())
1694 return;
1695
Devang Patel78ab9e22009-07-30 18:56:46 +00001696 DebugInfoFinder DbgFinder;
1697 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001698
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001699 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001700 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1701 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001702 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001703
1704 if (CompileUnits.empty()) {
1705 if (TimePassesIsEnabled)
1706 DebugTimer->stopTimer();
1707
1708 return;
1709 }
1710
Devang Patel70f44262009-06-29 20:38:13 +00001711 // If main compile unit for this module is not seen than randomly
1712 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001713 if (!ModuleCU)
1714 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001715
Devang Patel53bb5c92009-11-10 23:06:00 +00001716 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001717 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1718 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001719 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001720
Devang Patelc366f832009-12-10 19:14:49 +00001721 // Create DIEs for each global variable.
1722 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1723 E = DbgFinder.global_variable_end(); I != E; ++I)
1724 constructGlobalVariableDIE(*I);
1725
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001726 MMI = mmi;
1727 shouldEmit = true;
1728 MMI->setDebugInfoAvailability(true);
1729
1730 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001731 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001732
1733 // Print out .file directives to specify files for .loc directives. These are
1734 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001735 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001736 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1737 // Remember source id starts at 1.
1738 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1739 sys::Path FullPath(getSourceDirectoryName(Id.first));
1740 bool AppendOk =
1741 FullPath.appendComponent(getSourceFileName(Id.second));
1742 assert(AppendOk && "Could not append filename to directory!");
1743 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001744 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001745 Asm->EOL();
1746 }
1747 }
1748
1749 // Emit initial sections
Devang Patel2c4ceb12009-11-21 02:48:08 +00001750 emitInitial();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001751
1752 if (TimePassesIsEnabled)
1753 DebugTimer->stopTimer();
1754}
1755
Devang Patel2c4ceb12009-11-21 02:48:08 +00001756/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001757///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001758void DwarfDebug::endModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001759 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001760 return;
1761
1762 if (TimePassesIsEnabled)
1763 DebugTimer->startTimer();
1764
Devang Patel53bb5c92009-11-10 23:06:00 +00001765 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1766 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1767 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1768 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001769 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001770 }
1771
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001772 // Insert top level DIEs.
1773 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1774 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1775 ModuleCU->getCUDie()->addChild(*TI);
1776
Devang Patel5d11eb02009-12-03 19:11:07 +00001777 for (DenseMap<DIE *, WeakVH>::iterator CI = ContainingTypeMap.begin(),
1778 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1779 DIE *SPDie = CI->first;
1780 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1781 if (!N) continue;
1782 DIE *NDie = ModuleCU->getDIE(N);
1783 if (!NDie) continue;
1784 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1785 addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1786 }
1787
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001788 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001789 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001790 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001791 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001792 EmitLabel("data_end", 0);
1793
1794 // End text sections.
1795 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001796 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001797 EmitLabel("section_end", i);
1798 }
1799
1800 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001801 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001802
1803 // Emit function debug frame information
1804 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1805 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001806 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001807
1808 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001809 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001810
1811 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001812 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001813
1814 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001815 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001816
1817 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001818 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001819
1820 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001821 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001822
Devang Patel193f7202009-11-24 01:14:22 +00001823 // Emit info into a debug pubtypes section.
1824 emitDebugPubTypes();
1825
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001826 // Emit info into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001827 emitDebugStr();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001828
1829 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001830 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001831
1832 // Emit info into a debug aranges section.
1833 EmitDebugARanges();
1834
1835 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001836 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001837
1838 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001839 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001840
1841 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001842 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001843
1844 if (TimePassesIsEnabled)
1845 DebugTimer->stopTimer();
1846}
1847
Devang Patel53bb5c92009-11-10 23:06:00 +00001848/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001849DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1850 unsigned FrameIdx,
Devang Patel53bb5c92009-11-10 23:06:00 +00001851 DILocation &ScopeLoc) {
1852
1853 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1854 if (AbsDbgVariable)
1855 return AbsDbgVariable;
1856
1857 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1858 if (!Scope)
1859 return NULL;
1860
1861 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001862 Scope->addVariable(AbsDbgVariable);
Devang Patel53bb5c92009-11-10 23:06:00 +00001863 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1864 return AbsDbgVariable;
1865}
1866
Devang Patel2c4ceb12009-11-21 02:48:08 +00001867/// collectVariableInfo - Populate DbgScope entries with variables' info.
1868void DwarfDebug::collectVariableInfo() {
Devang Patelac1ceb32009-10-09 22:42:28 +00001869 if (!MMI) return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001870
Devang Patele717faa2009-10-06 01:26:37 +00001871 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1872 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1873 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac1ceb32009-10-09 22:42:28 +00001874 MetadataBase *MB = VI->first;
1875 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel53bb5c92009-11-10 23:06:00 +00001876 if (!Var) continue;
Devang Pateleda31212009-10-08 18:48:03 +00001877 DIVariable DV (Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001878 std::pair< unsigned, MDNode *> VP = VI->second;
1879 DILocation ScopeLoc(VP.second);
1880
1881 DbgScope *Scope =
1882 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1883 if (!Scope)
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001884 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelfb0ee432009-11-10 23:20:04 +00001885 // If variable scope is not found then skip this variable.
1886 if (!Scope)
1887 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001888
1889 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001890 Scope->addVariable(RegVar);
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001891 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1892 ScopeLoc))
Devang Patel53bb5c92009-11-10 23:06:00 +00001893 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patele717faa2009-10-06 01:26:37 +00001894 }
1895}
1896
Devang Patel2c4ceb12009-11-21 02:48:08 +00001897/// beginScope - Process beginning of a scope starting at Label.
1898void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001899 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1900 if (I == DbgScopeBeginMap.end())
1901 return;
Dan Gohman277207e2009-11-23 21:30:55 +00001902 ScopeVector &SD = I->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001903 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001904 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001905 (*SDI)->setStartLabelID(Label);
1906}
1907
Devang Patel2c4ceb12009-11-21 02:48:08 +00001908/// endScope - Process end of a scope.
1909void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001910 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00001911 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00001912 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001913
1914 unsigned Label = MMI->NextLabelID();
1915 Asm->printLabel(Label);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001916 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001917
Devang Patel0d20ac82009-10-06 01:50:42 +00001918 SmallVector<DbgScope *, 2> &SD = I->second;
1919 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001920 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001921 (*SDI)->setEndLabelID(Label);
Devang Patel53bb5c92009-11-10 23:06:00 +00001922 return;
1923}
1924
1925/// createDbgScope - Create DbgScope for the scope.
1926void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1927
1928 if (!InlinedAt) {
1929 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1930 if (WScope)
1931 return;
1932 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1933 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001934 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel2f105c62009-11-11 00:18:40 +00001935 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001936 return;
1937 }
1938
1939 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1940 if (WScope)
1941 return;
1942
1943 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1944 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1945 DILocation DL(InlinedAt);
1946 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel0d20ac82009-10-06 01:50:42 +00001947}
1948
Devang Patel2c4ceb12009-11-21 02:48:08 +00001949/// extractScopeInformation - Scan machine instructions in this function
Devang Patelaf9e8472009-10-01 20:31:14 +00001950/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001951bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patelaf9e8472009-10-01 20:31:14 +00001952 // If scope information was extracted using .dbg intrinsics then there is not
1953 // any need to extract these information by scanning each instruction.
1954 if (!DbgScopeMap.empty())
1955 return false;
1956
Devang Patel53bb5c92009-11-10 23:06:00 +00001957 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelaf9e8472009-10-01 20:31:14 +00001958 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1959 I != E; ++I) {
1960 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1961 II != IE; ++II) {
1962 const MachineInstr *MInsn = II;
1963 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001964 if (DL.isUnknown()) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001965 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001966 if (!DLT.Scope) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001967 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001968 // other scopes, create one DbgScope now. This will be translated
Devang Patelaf9e8472009-10-01 20:31:14 +00001969 // into a scope DIE at the end.
Devang Patel53bb5c92009-11-10 23:06:00 +00001970 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1971 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
1972 }
1973 }
1974
1975
1976 // Build scope hierarchy using working set of scopes.
1977 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1978 I != E; ++I) {
1979 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1980 II != IE; ++II) {
1981 const MachineInstr *MInsn = II;
1982 DebugLoc DL = MInsn->getDebugLoc();
1983 if (DL.isUnknown()) continue;
1984 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
1985 if (!DLT.Scope) continue;
1986 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001987 // other scopes, create one DbgScope now. This will be translated
Devang Patel53bb5c92009-11-10 23:06:00 +00001988 // into a scope DIE at the end.
1989 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1990 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
1991 Scope->setLastInsn(MInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +00001992 }
1993 }
1994
1995 // If a scope's last instruction is not set then use its child scope's
1996 // last instruction as this scope's last instrunction.
Devang Patelbdf45cb2009-10-27 20:47:17 +00001997 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00001998 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001999 if (DI->second->isAbstractScope())
2000 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002001 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00002002 DI->second->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +00002003 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2004 }
2005
2006 // Each scope has first instruction and last instruction to mark beginning
2007 // and end of a scope respectively. Create an inverse map that list scopes
2008 // starts (and ends) with an instruction. One instruction may start (or end)
2009 // multiple scopes.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002010 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002011 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2012 DbgScope *S = DI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002013 if (S->isAbstractScope())
2014 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002015 const MachineInstr *MI = S->getFirstInsn();
2016 assert (MI && "DbgScope does not have first instruction!");
2017
2018 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2019 if (IDI != DbgScopeBeginMap.end())
2020 IDI->second.push_back(S);
2021 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002022 DbgScopeBeginMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002023
2024 MI = S->getLastInsn();
2025 assert (MI && "DbgScope does not have last instruction!");
2026 IDI = DbgScopeEndMap.find(MI);
2027 if (IDI != DbgScopeEndMap.end())
2028 IDI->second.push_back(S);
2029 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002030 DbgScopeEndMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002031 }
2032
2033 return !DbgScopeMap.empty();
2034}
2035
Devang Patel2c4ceb12009-11-21 02:48:08 +00002036/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002037/// emitted immediately after the function entry point.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002038void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002039 this->MF = MF;
2040
2041 if (!ShouldEmitDwarfDebug()) return;
2042
2043 if (TimePassesIsEnabled)
2044 DebugTimer->startTimer();
2045
Devang Patel2c4ceb12009-11-21 02:48:08 +00002046 if (!extractScopeInformation(MF))
Devang Patel60b35bd2009-10-06 18:37:31 +00002047 return;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002048
2049 collectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002050
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002051 // Begin accumulating function debug information.
2052 MMI->BeginFunction(MF);
2053
2054 // Assumes in correct section after the entry point.
2055 EmitLabel("func_begin", ++SubprogramCount);
2056
2057 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2058 // function.
Devang Patelac1ceb32009-10-09 22:42:28 +00002059 DebugLoc FDL = MF->getDefaultDebugLoc();
2060 if (!FDL.isUnknown()) {
2061 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2062 unsigned LabelID = 0;
Devang Patel1619dc32009-10-13 23:28:53 +00002063 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002064 if (!SP.isNull())
Devang Patel2c4ceb12009-11-21 02:48:08 +00002065 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002066 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00002067 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002068 Asm->printLabel(LabelID);
2069 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002070 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002071 if (TimePassesIsEnabled)
2072 DebugTimer->stopTimer();
2073}
2074
Devang Patel2c4ceb12009-11-21 02:48:08 +00002075/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002076///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002077void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002078 if (!ShouldEmitDwarfDebug()) return;
2079
2080 if (TimePassesIsEnabled)
2081 DebugTimer->startTimer();
2082
Devang Patelac1ceb32009-10-09 22:42:28 +00002083 if (DbgScopeMap.empty())
2084 return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002085
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002086 // Define end label for subprogram.
2087 EmitLabel("func_end", SubprogramCount);
2088
2089 // Get function line info.
2090 if (!Lines.empty()) {
2091 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00002092 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002093 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2094 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2095 // Append the function info to section info.
2096 SectionLineInfos.insert(SectionLineInfos.end(),
2097 Lines.begin(), Lines.end());
2098 }
2099
Devang Patel53bb5c92009-11-10 23:06:00 +00002100 // Construct abstract scopes.
2101 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002102 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002103 constructScopeDIE(*AI);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002104
Devang Patel2c4ceb12009-11-21 02:48:08 +00002105 constructScopeDIE(CurrentFnDbgScope);
Devang Patel70d75ca2009-11-12 19:02:56 +00002106
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002107 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2108 MMI->getFrameMoves()));
2109
2110 // Clear debug info
Devang Patelc09ddc12009-12-01 18:13:48 +00002111 CurrentFnDbgScope = NULL;
2112 DbgScopeMap.clear();
2113 DbgScopeBeginMap.clear();
2114 DbgScopeEndMap.clear();
2115 ConcreteScopes.clear();
2116 AbstractScopesList.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002117
2118 Lines.clear();
Devang Patelc09ddc12009-12-01 18:13:48 +00002119
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002120 if (TimePassesIsEnabled)
2121 DebugTimer->stopTimer();
2122}
2123
Devang Patel2c4ceb12009-11-21 02:48:08 +00002124/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002125/// label. Returns a unique label ID used to generate a label and provide
2126/// correspondence to the source line list.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002127unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002128 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002129 if (!MMI)
2130 return 0;
2131
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002132 if (TimePassesIsEnabled)
2133 DebugTimer->startTimer();
2134
Devang Patel65dbc902009-11-25 17:36:49 +00002135 StringRef Dir;
2136 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002137
2138 DIDescriptor Scope(S);
2139 if (Scope.isCompileUnit()) {
2140 DICompileUnit CU(S);
2141 Dir = CU.getDirectory();
2142 Fn = CU.getFilename();
2143 } else if (Scope.isSubprogram()) {
2144 DISubprogram SP(S);
2145 Dir = SP.getDirectory();
2146 Fn = SP.getFilename();
2147 } else if (Scope.isLexicalBlock()) {
2148 DILexicalBlock DB(S);
2149 Dir = DB.getDirectory();
2150 Fn = DB.getFilename();
2151 } else
2152 assert (0 && "Unexpected scope info");
2153
2154 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002155 unsigned ID = MMI->NextLabelID();
2156 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2157
2158 if (TimePassesIsEnabled)
2159 DebugTimer->stopTimer();
2160
2161 return ID;
2162}
2163
2164/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2165/// timed. Look up the source id with the given directory and source file
2166/// names. If none currently exists, create a new id and insert it in the
2167/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2168/// well.
2169unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2170 const std::string &FileName) {
2171 if (TimePassesIsEnabled)
2172 DebugTimer->startTimer();
2173
Devang Patel5ccdd102009-09-29 18:40:58 +00002174 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002175
2176 if (TimePassesIsEnabled)
2177 DebugTimer->stopTimer();
2178
2179 return SrcId;
2180}
2181
Bill Wendling829e67b2009-05-20 23:22:40 +00002182//===----------------------------------------------------------------------===//
2183// Emit Methods
2184//===----------------------------------------------------------------------===//
2185
Devang Patel2c4ceb12009-11-21 02:48:08 +00002186/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002187///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002188unsigned
2189DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002190 // Get the children.
2191 const std::vector<DIE *> &Children = Die->getChildren();
2192
2193 // If not last sibling and has children then add sibling offset attribute.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002194 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002195
2196 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002197 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002198
2199 // Get the abbreviation for this DIE.
2200 unsigned AbbrevNumber = Die->getAbbrevNumber();
2201 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2202
2203 // Set DIE offset
2204 Die->setOffset(Offset);
2205
2206 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002207 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002208
2209 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2210 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2211
2212 // Size the DIE attribute values.
2213 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2214 // Size attribute value.
2215 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2216
2217 // Size the DIE children if any.
2218 if (!Children.empty()) {
2219 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2220 "Children flag not set");
2221
2222 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002223 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002224
2225 // End of children marker.
2226 Offset += sizeof(int8_t);
2227 }
2228
2229 Die->setSize(Offset - Die->getOffset());
2230 return Offset;
2231}
2232
Devang Patel2c4ceb12009-11-21 02:48:08 +00002233/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002234///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002235void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002236 // Compute size of compile unit header.
2237 static unsigned Offset =
2238 sizeof(int32_t) + // Length of Compilation Unit Info
2239 sizeof(int16_t) + // DWARF version number
2240 sizeof(int32_t) + // Offset Into Abbrev. Section
2241 sizeof(int8_t); // Pointer Size (in bytes)
2242
Devang Patel2c4ceb12009-11-21 02:48:08 +00002243 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel1dbc7712009-06-29 20:45:18 +00002244 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002245}
2246
Devang Patel2c4ceb12009-11-21 02:48:08 +00002247/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling94d04b82009-05-20 23:21:38 +00002248/// tools to recognize the object file contains Dwarf information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002249void DwarfDebug::emitInitial() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002250 // Check to see if we already emitted intial headers.
2251 if (didInitial) return;
2252 didInitial = true;
2253
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002254 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002255
Bill Wendling94d04b82009-05-20 23:21:38 +00002256 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002257 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002258 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002259 EmitLabel("section_debug_frame", 0);
2260 }
2261
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002262 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002263 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002264 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002265 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002266 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002267 EmitLabel("section_aranges", 0);
2268
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002269 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2270 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002271 EmitLabel("section_macinfo", 0);
2272 }
2273
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002274 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002275 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002276 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002277 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002278 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002279 EmitLabel("section_pubnames", 0);
Devang Patel193f7202009-11-24 01:14:22 +00002280 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2281 EmitLabel("section_pubtypes", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002282 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002283 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002284 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002285 EmitLabel("section_ranges", 0);
2286
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002287 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002288 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002289 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002290 EmitLabel("data_begin", 0);
2291}
2292
Devang Patel2c4ceb12009-11-21 02:48:08 +00002293/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002294///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002295void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002296 // Get the abbreviation for this DIE.
2297 unsigned AbbrevNumber = Die->getAbbrevNumber();
2298 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2299
2300 Asm->EOL();
2301
2302 // Emit the code (index) for the abbreviation.
2303 Asm->EmitULEB128Bytes(AbbrevNumber);
2304
2305 if (Asm->isVerbose())
2306 Asm->EOL(std::string("Abbrev [" +
2307 utostr(AbbrevNumber) +
2308 "] 0x" + utohexstr(Die->getOffset()) +
2309 ":0x" + utohexstr(Die->getSize()) + " " +
2310 dwarf::TagString(Abbrev->getTag())));
2311 else
2312 Asm->EOL();
2313
2314 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2315 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2316
2317 // Emit the DIE attribute values.
2318 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2319 unsigned Attr = AbbrevData[i].getAttribute();
2320 unsigned Form = AbbrevData[i].getForm();
2321 assert(Form && "Too many attributes for DIE (check abbreviation)");
2322
2323 switch (Attr) {
2324 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002325 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002326 break;
2327 case dwarf::DW_AT_abstract_origin: {
2328 DIEEntry *E = cast<DIEEntry>(Values[i]);
2329 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002330 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002331 Asm->EmitInt32(Addr);
2332 break;
2333 }
2334 default:
2335 // Emit an attribute using the defined form.
2336 Values[i]->EmitValue(this, Form);
2337 break;
2338 }
2339
2340 Asm->EOL(dwarf::AttributeString(Attr));
2341 }
2342
2343 // Emit the DIE children if any.
2344 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2345 const std::vector<DIE *> &Children = Die->getChildren();
2346
2347 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002348 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002349
2350 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2351 }
2352}
2353
Devang Patel8a241142009-12-09 18:24:21 +00002354/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002355///
Devang Patel8a241142009-12-09 18:24:21 +00002356void DwarfDebug::emitDebugInfo() {
2357 // Start debug info section.
2358 Asm->OutStreamer.SwitchSection(
2359 Asm->getObjFileLowering().getDwarfInfoSection());
2360 DIE *Die = ModuleCU->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002361
2362 // Emit the compile units header.
Devang Patel8a241142009-12-09 18:24:21 +00002363 EmitLabel("info_begin", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002364
2365 // Emit size of content not including length itself
2366 unsigned ContentSize = Die->getSize() +
2367 sizeof(int16_t) + // DWARF version number
2368 sizeof(int32_t) + // Offset Into Abbrev. Section
2369 sizeof(int8_t) + // Pointer Size (in bytes)
2370 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2371
2372 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2373 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2374 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2375 Asm->EOL("Offset Into Abbrev. Section");
2376 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2377
Devang Patel2c4ceb12009-11-21 02:48:08 +00002378 emitDIE(Die);
Bill Wendling94d04b82009-05-20 23:21:38 +00002379 // FIXME - extra padding for gdb bug.
2380 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2381 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2382 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2383 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Devang Patel8a241142009-12-09 18:24:21 +00002384 EmitLabel("info_end", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002385
2386 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002387
Bill Wendling94d04b82009-05-20 23:21:38 +00002388}
2389
Devang Patel2c4ceb12009-11-21 02:48:08 +00002390/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002391///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002392void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002393 // Check to see if it is worth the effort.
2394 if (!Abbreviations.empty()) {
2395 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002396 Asm->OutStreamer.SwitchSection(
2397 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002398
2399 EmitLabel("abbrev_begin", 0);
2400
2401 // For each abbrevation.
2402 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2403 // Get abbreviation data
2404 const DIEAbbrev *Abbrev = Abbreviations[i];
2405
2406 // Emit the abbrevations code (base 1 index.)
2407 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2408 Asm->EOL("Abbreviation Code");
2409
2410 // Emit the abbreviations data.
2411 Abbrev->Emit(Asm);
2412
2413 Asm->EOL();
2414 }
2415
2416 // Mark end of abbreviations.
2417 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2418
2419 EmitLabel("abbrev_end", 0);
2420 Asm->EOL();
2421 }
2422}
2423
Devang Patel2c4ceb12009-11-21 02:48:08 +00002424/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002425/// the line matrix.
2426///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002427void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002428 // Define last address of section.
2429 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2430 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2431 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2432 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2433
2434 // Mark end of matrix.
2435 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2436 Asm->EmitULEB128Bytes(1); Asm->EOL();
2437 Asm->EmitInt8(1); Asm->EOL();
2438}
2439
Devang Patel2c4ceb12009-11-21 02:48:08 +00002440/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002441///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002442void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002443 // If the target is using .loc/.file, the assembler will be emitting the
2444 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002445 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002446 return;
2447
2448 // Minimum line delta, thus ranging from -10..(255-10).
2449 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2450 // Maximum line delta, thus ranging from -10..(255-10).
2451 const int MaxLineDelta = 255 + MinLineDelta;
2452
2453 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002454 Asm->OutStreamer.SwitchSection(
2455 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002456
2457 // Construct the section header.
2458 EmitDifference("line_end", 0, "line_begin", 0, true);
2459 Asm->EOL("Length of Source Line Info");
2460 EmitLabel("line_begin", 0);
2461
2462 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2463
2464 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2465 Asm->EOL("Prolog Length");
2466 EmitLabel("line_prolog_begin", 0);
2467
2468 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2469
2470 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2471
2472 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2473
2474 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2475
2476 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2477
2478 // Line number standard opcode encodings argument count
2479 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2480 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2481 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2482 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2483 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2484 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2485 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2486 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2487 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2488
2489 // Emit directories.
2490 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2491 Asm->EmitString(getSourceDirectoryName(DI));
2492 Asm->EOL("Directory");
2493 }
2494
2495 Asm->EmitInt8(0); Asm->EOL("End of directories");
2496
2497 // Emit files.
2498 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2499 // Remember source id starts at 1.
2500 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2501 Asm->EmitString(getSourceFileName(Id.second));
2502 Asm->EOL("Source");
2503 Asm->EmitULEB128Bytes(Id.first);
2504 Asm->EOL("Directory #");
2505 Asm->EmitULEB128Bytes(0);
2506 Asm->EOL("Mod date");
2507 Asm->EmitULEB128Bytes(0);
2508 Asm->EOL("File size");
2509 }
2510
2511 Asm->EmitInt8(0); Asm->EOL("End of files");
2512
2513 EmitLabel("line_prolog_end", 0);
2514
2515 // A sequence for each text section.
2516 unsigned SecSrcLinesSize = SectionSourceLines.size();
2517
2518 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2519 // Isolate current sections line info.
2520 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2521
Chris Lattner93b6db32009-08-08 23:39:42 +00002522 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002523 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002524 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002525 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002526 }*/
2527 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002528
2529 // Dwarf assumes we start with first line of first source file.
2530 unsigned Source = 1;
2531 unsigned Line = 1;
2532
2533 // Construct rows of the address, source, line, column matrix.
2534 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2535 const SrcLineInfo &LineInfo = LineInfos[i];
2536 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2537 if (!LabelID) continue;
2538
Caroline Ticec6f9d622009-09-11 18:25:54 +00002539 if (LineInfo.getLine() == 0) continue;
2540
Bill Wendling94d04b82009-05-20 23:21:38 +00002541 if (!Asm->isVerbose())
2542 Asm->EOL();
2543 else {
2544 std::pair<unsigned, unsigned> SourceID =
2545 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002546 O << '\t' << MAI->getCommentString() << ' '
Dan Gohmanb3b98212009-12-05 02:00:34 +00002547 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling94d04b82009-05-20 23:21:38 +00002548 << getSourceFileName(SourceID.second)
Dan Gohmanb3b98212009-12-05 02:00:34 +00002549 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002550 }
2551
2552 // Define the line address.
2553 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2554 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2555 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2556 EmitReference("label", LabelID); Asm->EOL("Location label");
2557
2558 // If change of source, then switch to the new source.
2559 if (Source != LineInfo.getSourceID()) {
2560 Source = LineInfo.getSourceID();
2561 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2562 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2563 }
2564
2565 // If change of line.
2566 if (Line != LineInfo.getLine()) {
2567 // Determine offset.
2568 int Offset = LineInfo.getLine() - Line;
2569 int Delta = Offset - MinLineDelta;
2570
2571 // Update line.
2572 Line = LineInfo.getLine();
2573
2574 // If delta is small enough and in range...
2575 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2576 // ... then use fast opcode.
2577 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2578 } else {
2579 // ... otherwise use long hand.
2580 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2581 Asm->EOL("DW_LNS_advance_line");
2582 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2583 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2584 }
2585 } else {
2586 // Copy the previous row (different address or source)
2587 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2588 }
2589 }
2590
Devang Patel2c4ceb12009-11-21 02:48:08 +00002591 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002592 }
2593
2594 if (SecSrcLinesSize == 0)
2595 // Because we're emitting a debug_line section, we still need a line
2596 // table. The linker and friends expect it to exist. If there's nothing to
2597 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002598 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002599
2600 EmitLabel("line_end", 0);
2601 Asm->EOL();
2602}
2603
Devang Patel2c4ceb12009-11-21 02:48:08 +00002604/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002605///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002606void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002607 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002608 return;
2609
2610 int stackGrowth =
2611 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2612 TargetFrameInfo::StackGrowsUp ?
2613 TD->getPointerSize() : -TD->getPointerSize();
2614
2615 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002616 Asm->OutStreamer.SwitchSection(
2617 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002618
2619 EmitLabel("debug_frame_common", 0);
2620 EmitDifference("debug_frame_common_end", 0,
2621 "debug_frame_common_begin", 0, true);
2622 Asm->EOL("Length of Common Information Entry");
2623
2624 EmitLabel("debug_frame_common_begin", 0);
2625 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2626 Asm->EOL("CIE Identifier Tag");
2627 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2628 Asm->EOL("CIE Version");
2629 Asm->EmitString("");
2630 Asm->EOL("CIE Augmentation");
2631 Asm->EmitULEB128Bytes(1);
2632 Asm->EOL("CIE Code Alignment Factor");
2633 Asm->EmitSLEB128Bytes(stackGrowth);
2634 Asm->EOL("CIE Data Alignment Factor");
2635 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2636 Asm->EOL("CIE RA Column");
2637
2638 std::vector<MachineMove> Moves;
2639 RI->getInitialFrameState(Moves);
2640
2641 EmitFrameMoves(NULL, 0, Moves, false);
2642
2643 Asm->EmitAlignment(2, 0, 0, false);
2644 EmitLabel("debug_frame_common_end", 0);
2645
2646 Asm->EOL();
2647}
2648
Devang Patel2c4ceb12009-11-21 02:48:08 +00002649/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00002650/// section.
2651void
Devang Patel2c4ceb12009-11-21 02:48:08 +00002652DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002653 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002654 return;
2655
2656 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002657 Asm->OutStreamer.SwitchSection(
2658 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002659
2660 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2661 "debug_frame_begin", DebugFrameInfo.Number, true);
2662 Asm->EOL("Length of Frame Information Entry");
2663
2664 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2665
2666 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2667 0, 0, true, false);
2668 Asm->EOL("FDE CIE offset");
2669
2670 EmitReference("func_begin", DebugFrameInfo.Number);
2671 Asm->EOL("FDE initial location");
2672 EmitDifference("func_end", DebugFrameInfo.Number,
2673 "func_begin", DebugFrameInfo.Number);
2674 Asm->EOL("FDE address range");
2675
2676 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2677 false);
2678
2679 Asm->EmitAlignment(2, 0, 0, false);
2680 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2681
2682 Asm->EOL();
2683}
2684
Devang Patel8a241142009-12-09 18:24:21 +00002685/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2686///
2687void DwarfDebug::emitDebugPubNames() {
2688 // Start the dwarf pubnames section.
2689 Asm->OutStreamer.SwitchSection(
2690 Asm->getObjFileLowering().getDwarfPubNamesSection());
2691
2692 EmitDifference("pubnames_end", ModuleCU->getID(),
2693 "pubnames_begin", ModuleCU->getID(), true);
Bill Wendling94d04b82009-05-20 23:21:38 +00002694 Asm->EOL("Length of Public Names Info");
2695
Devang Patel8a241142009-12-09 18:24:21 +00002696 EmitLabel("pubnames_begin", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002697
2698 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2699
2700 EmitSectionOffset("info_begin", "section_info",
Devang Patel8a241142009-12-09 18:24:21 +00002701 ModuleCU->getID(), 0, true, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00002702 Asm->EOL("Offset of Compilation Unit Info");
2703
Devang Patel8a241142009-12-09 18:24:21 +00002704 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002705 true);
2706 Asm->EOL("Compilation Unit Length");
2707
Devang Patel8a241142009-12-09 18:24:21 +00002708 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling94d04b82009-05-20 23:21:38 +00002709 for (StringMap<DIE*>::const_iterator
2710 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2711 const char *Name = GI->getKeyData();
2712 DIE * Entity = GI->second;
2713
2714 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2715 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2716 }
2717
2718 Asm->EmitInt32(0); Asm->EOL("End Mark");
Devang Patel8a241142009-12-09 18:24:21 +00002719 EmitLabel("pubnames_end", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002720
2721 Asm->EOL();
2722}
2723
Devang Patel193f7202009-11-24 01:14:22 +00002724void DwarfDebug::emitDebugPubTypes() {
Devang Patelf3a03762009-11-24 19:18:41 +00002725 // Start the dwarf pubnames section.
2726 Asm->OutStreamer.SwitchSection(
2727 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patel193f7202009-11-24 01:14:22 +00002728 EmitDifference("pubtypes_end", ModuleCU->getID(),
2729 "pubtypes_begin", ModuleCU->getID(), true);
2730 Asm->EOL("Length of Public Types Info");
2731
2732 EmitLabel("pubtypes_begin", ModuleCU->getID());
2733
2734 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2735
2736 EmitSectionOffset("info_begin", "section_info",
2737 ModuleCU->getID(), 0, true, false);
2738 Asm->EOL("Offset of Compilation ModuleCU Info");
2739
2740 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2741 true);
2742 Asm->EOL("Compilation ModuleCU Length");
2743
2744 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2745 for (StringMap<DIE*>::const_iterator
2746 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2747 const char *Name = GI->getKeyData();
2748 DIE * Entity = GI->second;
2749
2750 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2751 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2752 }
2753
2754 Asm->EmitInt32(0); Asm->EOL("End Mark");
2755 EmitLabel("pubtypes_end", ModuleCU->getID());
2756
2757 Asm->EOL();
2758}
2759
Devang Patel2c4ceb12009-11-21 02:48:08 +00002760/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002761///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002762void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002763 // Check to see if it is worth the effort.
2764 if (!StringPool.empty()) {
2765 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002766 Asm->OutStreamer.SwitchSection(
2767 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002768
2769 // For each of strings in the string pool.
2770 for (unsigned StringID = 1, N = StringPool.size();
2771 StringID <= N; ++StringID) {
2772 // Emit a label for reference from debug information entries.
2773 EmitLabel("string", StringID);
2774
2775 // Emit the string itself.
2776 const std::string &String = StringPool[StringID];
2777 Asm->EmitString(String); Asm->EOL();
2778 }
2779
2780 Asm->EOL();
2781 }
2782}
2783
Devang Patel2c4ceb12009-11-21 02:48:08 +00002784/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002785///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002786void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002787 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002788 Asm->OutStreamer.SwitchSection(
2789 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002790 Asm->EOL();
2791}
2792
2793/// EmitDebugARanges - Emit visible names into a debug aranges section.
2794///
2795void DwarfDebug::EmitDebugARanges() {
2796 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002797 Asm->OutStreamer.SwitchSection(
2798 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002799
2800 // FIXME - Mock up
2801#if 0
2802 CompileUnit *Unit = GetBaseCompileUnit();
2803
2804 // Don't include size of length
2805 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2806
2807 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2808
2809 EmitReference("info_begin", Unit->getID());
2810 Asm->EOL("Offset of Compilation Unit Info");
2811
2812 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2813
2814 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2815
2816 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2817 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2818
2819 // Range 1
2820 EmitReference("text_begin", 0); Asm->EOL("Address");
2821 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2822
2823 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2824 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2825#endif
2826
2827 Asm->EOL();
2828}
2829
Devang Patel2c4ceb12009-11-21 02:48:08 +00002830/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002831///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002832void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002833 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002834 Asm->OutStreamer.SwitchSection(
2835 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002836 Asm->EOL();
2837}
2838
Devang Patel2c4ceb12009-11-21 02:48:08 +00002839/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002840///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002841void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002842 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002843 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002844 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002845 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002846 Asm->EOL();
2847 }
2848}
2849
Devang Patel2c4ceb12009-11-21 02:48:08 +00002850/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002851/// Section Header:
2852/// 1. length of section
2853/// 2. Dwarf version number
2854/// 3. address size.
2855///
2856/// Entries (one "entry" for each function that was inlined):
2857///
2858/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2859/// otherwise offset into __debug_str for regular function name.
2860/// 2. offset into __debug_str section for regular function name.
2861/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2862/// instances for the function.
2863///
2864/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2865/// inlined instance; the die_offset points to the inlined_subroutine die in the
2866/// __debug_info section, and the low_pc is the starting address for the
2867/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002868void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002869 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002870 return;
2871
Devang Patel1dbc7712009-06-29 20:45:18 +00002872 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002873 return;
2874
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002875 Asm->OutStreamer.SwitchSection(
2876 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002877 Asm->EOL();
2878 EmitDifference("debug_inlined_end", 1,
2879 "debug_inlined_begin", 1, true);
2880 Asm->EOL("Length of Debug Inlined Information Entry");
2881
2882 EmitLabel("debug_inlined_begin", 1);
2883
2884 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2885 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2886
Devang Patel53bb5c92009-11-10 23:06:00 +00002887 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2888 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002889
Devang Patel53bb5c92009-11-10 23:06:00 +00002890// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2891 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2892 MDNode *Node = *I;
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002893 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2894 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002895 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002896 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002897 StringRef LName = SP.getLinkageName();
2898 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002899
Devang Patel65dbc902009-11-25 17:36:49 +00002900 if (LName.empty())
Devang Patel53cb17d2009-07-16 01:01:22 +00002901 Asm->EmitString(Name);
2902 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002903 // Skip special LLVM prefix that is used to inform the asm printer to not
2904 // emit usual symbol prefix before the symbol name. This happens for
2905 // Objective-C symbol names and symbol whose name is replaced using GCC's
2906 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002907 if (LName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00002908 LName = LName.substr(1);
Devang Patel53bb5c92009-11-10 23:06:00 +00002909// Asm->EmitString(LName);
2910 EmitSectionOffset("string", "section_str",
2911 StringPool.idFor(LName), false, true);
2912
Devang Patel53cb17d2009-07-16 01:01:22 +00002913 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002914 Asm->EOL("MIPS linkage name");
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002915// Asm->EmitString(Name);
Devang Patel53bb5c92009-11-10 23:06:00 +00002916 EmitSectionOffset("string", "section_str",
2917 StringPool.idFor(Name), false, true);
2918 Asm->EOL("Function name");
Bill Wendling94d04b82009-05-20 23:21:38 +00002919 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2920
Devang Patel53bb5c92009-11-10 23:06:00 +00002921 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002922 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002923 DIE *SP = LI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00002924 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2925
2926 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002927 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002928 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002929 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002930
Devang Patel53bb5c92009-11-10 23:06:00 +00002931 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling94d04b82009-05-20 23:21:38 +00002932 }
2933 }
2934
2935 EmitLabel("debug_inlined_end", 1);
2936 Asm->EOL();
2937}