blob: b462fe84e05c767926918aa50abfc51e67acbb31 [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
Devang Patel4063f6b2009-12-07 21:41:32 +0000449/// addSourceLine - Add location information to specified debug information
450/// entry.
451void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
452 // If there is no compile unit specified, don't add a line #.
453 if (NS->getCompileUnit().isNull())
454 return;
455
456 unsigned Line = NS->getLineNumber();
457 StringRef FN = NS->getFilename();
458 StringRef Dir = NS->getDirectory();
459
460 unsigned FileID = GetOrCreateSourceID(Dir, FN);
461 assert(FileID && "Invalid file id");
462 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
463 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
464}
465
Caroline Ticedc8f6042009-08-31 21:19:37 +0000466/* Byref variables, in Blocks, are declared by the programmer as
467 "SomeType VarName;", but the compiler creates a
468 __Block_byref_x_VarName struct, and gives the variable VarName
469 either the struct, or a pointer to the struct, as its type. This
470 is necessary for various behind-the-scenes things the compiler
471 needs to do with by-reference variables in blocks.
472
473 However, as far as the original *programmer* is concerned, the
474 variable should still have type 'SomeType', as originally declared.
475
476 The following function dives into the __Block_byref_x_VarName
477 struct to find the original type of the variable. This will be
478 passed back to the code generating the type for the Debug
479 Information Entry for the variable 'VarName'. 'VarName' will then
480 have the original type 'SomeType' in its debug information.
481
482 The original type 'SomeType' will be the type of the field named
483 'VarName' inside the __Block_byref_x_VarName struct.
484
485 NOTE: In order for this to not completely fail on the debugger
486 side, the Debug Information Entry for the variable VarName needs to
487 have a DW_AT_location that tells the debugger how to unwind through
488 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000489 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000490
491/// Find the type the programmer originally declared the variable to be
492/// and return that type.
493///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000494DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000495
496 DIType subType = Ty;
497 unsigned tag = Ty.getTag();
498
499 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000500 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000501 subType = DTy.getTypeDerivedFrom();
502 }
503
504 DICompositeType blockStruct = DICompositeType(subType.getNode());
505
506 DIArray Elements = blockStruct.getTypeArray();
507
508 if (Elements.isNull())
509 return Ty;
510
511 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
512 DIDescriptor Element = Elements.getElement(i);
513 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000514 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000515 return (DT.getTypeDerivedFrom());
516 }
517
518 return Ty;
519}
520
Devang Patel2c4ceb12009-11-21 02:48:08 +0000521/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000522/// and generate the DWARF information necessary to find the actual variable
523/// given the extra address information encoded in the DIVariable, starting from
524/// the starting location. Add the DWARF information to the die.
525///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000526void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000527 unsigned Attribute,
528 const MachineLocation &Location) {
529 const DIVariable &VD = DV->getVariable();
530 DIType Ty = VD.getType();
531
532 // Decode the original location, and use that as the start of the byref
533 // variable's location.
534 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
535 DIEBlock *Block = new DIEBlock();
536
537 if (Location.isReg()) {
538 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000539 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000540 } else {
541 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000542 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
543 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000544 }
545 } else {
546 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000547 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000548 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000549 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
550 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000551 }
552
Devang Patel2c4ceb12009-11-21 02:48:08 +0000553 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000554 }
555
556 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
557 uint64_t Element = VD.getAddrElement(i);
558
559 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000560 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
561 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000562 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000563 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000564 } else llvm_unreachable("unknown DIFactory Opcode");
565 }
566
567 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000568 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000569}
570
Caroline Ticedc8f6042009-08-31 21:19:37 +0000571/* Byref variables, in Blocks, are declared by the programmer as "SomeType
572 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
573 gives the variable VarName either the struct, or a pointer to the struct, as
574 its type. This is necessary for various behind-the-scenes things the
575 compiler needs to do with by-reference variables in Blocks.
576
577 However, as far as the original *programmer* is concerned, the variable
578 should still have type 'SomeType', as originally declared.
579
Devang Patel2c4ceb12009-11-21 02:48:08 +0000580 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000581 struct to find the original type of the variable, which is then assigned to
582 the variable's Debug Information Entry as its real type. So far, so good.
583 However now the debugger will expect the variable VarName to have the type
584 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000585 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000586 pointers and struct to find the actual variable of type SomeType.
587
588 The following function does just that. We start by getting
589 the "normal" location for the variable. This will be the location
590 of either the struct __Block_byref_x_VarName or the pointer to the
591 struct __Block_byref_x_VarName.
592
593 The struct will look something like:
594
595 struct __Block_byref_x_VarName {
596 ... <various fields>
597 struct __Block_byref_x_VarName *forwarding;
598 ... <various other fields>
599 SomeType VarName;
600 ... <maybe more fields>
601 };
602
603 If we are given the struct directly (as our starting point) we
604 need to tell the debugger to:
605
606 1). Add the offset of the forwarding field.
607
608 2). Follow that pointer to get the the real __Block_byref_x_VarName
609 struct to use (the real one may have been copied onto the heap).
610
611 3). Add the offset for the field VarName, to find the actual variable.
612
613 If we started with a pointer to the struct, then we need to
614 dereference that pointer first, before the other steps.
615 Translating this into DWARF ops, we will need to append the following
616 to the current location description for the variable:
617
618 DW_OP_deref -- optional, if we start with a pointer
619 DW_OP_plus_uconst <forward_fld_offset>
620 DW_OP_deref
621 DW_OP_plus_uconst <varName_fld_offset>
622
623 That is what this function does. */
624
Devang Patel2c4ceb12009-11-21 02:48:08 +0000625/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000626/// provided, and generate the DWARF information necessary to find the
627/// actual Block variable (navigating the Block struct) based on the
628/// starting location. Add the DWARF information to the die. For
629/// more information, read large comment just above here.
630///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000631void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000632 unsigned Attribute,
633 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000634 const DIVariable &VD = DV->getVariable();
635 DIType Ty = VD.getType();
636 DIType TmpTy = Ty;
637 unsigned Tag = Ty.getTag();
638 bool isPointer = false;
639
Devang Patel65dbc902009-11-25 17:36:49 +0000640 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000641
642 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000643 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000644 TmpTy = DTy.getTypeDerivedFrom();
645 isPointer = true;
646 }
647
648 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
649
Daniel Dunbar00564992009-09-19 20:40:14 +0000650 // Find the __forwarding field and the variable field in the __Block_byref
651 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000652 DIArray Fields = blockStruct.getTypeArray();
653 DIDescriptor varField = DIDescriptor();
654 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000655
656
Daniel Dunbar00564992009-09-19 20:40:14 +0000657 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
658 DIDescriptor Element = Fields.getElement(i);
659 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000660 StringRef fieldName = DT.getName();
661 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000662 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000663 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000664 varField = Element;
665 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000666
Mike Stump7e3720d2009-09-24 23:21:26 +0000667 assert(!varField.isNull() && "Can't find byref variable in Block struct");
668 assert(!forwardingField.isNull()
669 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000670
Daniel Dunbar00564992009-09-19 20:40:14 +0000671 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000672 unsigned int forwardingFieldOffset =
673 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
674 unsigned int varFieldOffset =
675 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000676
Mike Stump7e3720d2009-09-24 23:21:26 +0000677 // Decode the original location, and use that as the start of the byref
678 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000679 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
680 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000681
Daniel Dunbar00564992009-09-19 20:40:14 +0000682 if (Location.isReg()) {
683 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000684 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000685 else {
686 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000687 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
688 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000689 }
690 } else {
691 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000692 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000693 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000694 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
695 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000696 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000697
Devang Patel2c4ceb12009-11-21 02:48:08 +0000698 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000699 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000700
Mike Stump7e3720d2009-09-24 23:21:26 +0000701 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000702 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000703 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000704 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000705
Daniel Dunbar00564992009-09-19 20:40:14 +0000706 // Next add the offset for the '__forwarding' field:
707 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
708 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000709 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000710 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
711 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000712 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000713
Daniel Dunbar00564992009-09-19 20:40:14 +0000714 // Now dereference the __forwarding field to get to the real __Block_byref
715 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000716 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000717
Daniel Dunbar00564992009-09-19 20:40:14 +0000718 // Now that we've got the real __Block_byref... struct, add the offset
719 // for the variable's field to get to the location of the actual variable:
720 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000721 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000722 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
723 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000724 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000725
Daniel Dunbar00564992009-09-19 20:40:14 +0000726 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000727 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000728}
729
Devang Patel2c4ceb12009-11-21 02:48:08 +0000730/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000731/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000732void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000733 const MachineLocation &Location) {
734 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
735 DIEBlock *Block = new DIEBlock();
736
737 if (Location.isReg()) {
738 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000739 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000740 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000741 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
742 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000743 }
744 } else {
745 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000746 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000747 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000748 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
749 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000750 }
751
Devang Patel2c4ceb12009-11-21 02:48:08 +0000752 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000753 }
754
Devang Patel2c4ceb12009-11-21 02:48:08 +0000755 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000756}
757
Devang Patel2c4ceb12009-11-21 02:48:08 +0000758/// addType - Add a new type attribute to the specified entity.
Devang Patela3a60c62009-12-08 15:31:31 +0000759void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000760 if (Ty.isNull())
761 return;
762
763 // Check for pre-existence.
Devang Patela3a60c62009-12-08 15:31:31 +0000764 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000765
766 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000767 if (Entry) {
768 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000769 return;
770 }
771
772 // Set up proxy.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000773 Entry = createDIEEntry();
Devang Patela3a60c62009-12-08 15:31:31 +0000774 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000775
776 // Construct type.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000777 DIE *Buffer = new DIE(dwarf::DW_TAG_base_type);
Devang Patelb3235d22009-12-03 23:46:57 +0000778 ModuleCU->insertDIE(Ty.getNode(), Buffer);
Devang Patel6ceea332009-08-31 18:49:10 +0000779 if (Ty.isBasicType())
Devang Patela3a60c62009-12-08 15:31:31 +0000780 constructTypeDIE(*Buffer, DIBasicType(Ty.getNode()));
Devang Patel6ceea332009-08-31 18:49:10 +0000781 else if (Ty.isCompositeType())
Devang Patela3a60c62009-12-08 15:31:31 +0000782 constructTypeDIE(*Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000783 else {
Devang Patel6ceea332009-08-31 18:49:10 +0000784 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patela3a60c62009-12-08 15:31:31 +0000785 constructTypeDIE(*Buffer, DIDerivedType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000786 }
787
788 // Add debug information entry to entity and appropriate context.
789 DIE *Die = NULL;
790 DIDescriptor Context = Ty.getContext();
Devang Patel4063f6b2009-12-07 21:41:32 +0000791 if (!Context.isNull()) {
792 if (Context.isNameSpace()) {
793 DINameSpace NS(Context.getNode());
794 Die = getOrCreateNameSpace(NS);
795 } else
Devang Patela3a60c62009-12-08 15:31:31 +0000796 Die = ModuleCU->getDIE(Context.getNode());
Devang Patel4063f6b2009-12-07 21:41:32 +0000797 }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000798 if (Die)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000799 Die->addChild(Buffer);
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000800 else
Devang Patela3a60c62009-12-08 15:31:31 +0000801 ModuleCU->addDie(Buffer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000802 Entry->setEntry(Buffer);
803 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000804}
805
Devang Patel2c4ceb12009-11-21 02:48:08 +0000806/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patela3a60c62009-12-08 15:31:31 +0000807void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000808 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000809 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000810 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000811 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000812 BTy.getEncoding());
813
814 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000815 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000816 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000817 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000818 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000819}
820
Devang Patel2c4ceb12009-11-21 02:48:08 +0000821/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patela3a60c62009-12-08 15:31:31 +0000822void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000823 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000824 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000825 uint64_t Size = DTy.getSizeInBits() >> 3;
826 unsigned Tag = DTy.getTag();
827
828 // FIXME - Workaround for templates.
829 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
830
831 Buffer.setTag(Tag);
832
833 // Map to main type, void will not have a type.
834 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patela3a60c62009-12-08 15:31:31 +0000835 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000836
837 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000838 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000839 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000840
841 // Add size if non-zero (derived types might be zero-sized.)
842 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000843 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000844
845 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000846 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000847 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000848}
849
Devang Patel2c4ceb12009-11-21 02:48:08 +0000850/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patela3a60c62009-12-08 15:31:31 +0000851void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000852 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000853 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000854
855 uint64_t Size = CTy.getSizeInBits() >> 3;
856 unsigned Tag = CTy.getTag();
857 Buffer.setTag(Tag);
858
859 switch (Tag) {
860 case dwarf::DW_TAG_vector_type:
861 case dwarf::DW_TAG_array_type:
Devang Patela3a60c62009-12-08 15:31:31 +0000862 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000863 break;
864 case dwarf::DW_TAG_enumeration_type: {
865 DIArray Elements = CTy.getTypeArray();
866
867 // Add enumerators to enumeration type.
868 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
869 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000870 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelc5254722009-10-09 17:51:49 +0000871 if (!Enum.isNull()) {
Devang Patela3a60c62009-12-08 15:31:31 +0000872 ElemDie = constructEnumTypeDIE(&Enum);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000873 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +0000874 }
Bill Wendling0310d762009-05-15 09:23:25 +0000875 }
876 }
877 break;
878 case dwarf::DW_TAG_subroutine_type: {
879 // Add return type.
880 DIArray Elements = CTy.getTypeArray();
881 DIDescriptor RTy = Elements.getElement(0);
Devang Patela3a60c62009-12-08 15:31:31 +0000882 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000883
884 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000885 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000886
887 // Add arguments.
888 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
889 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
890 DIDescriptor Ty = Elements.getElement(i);
Devang Patela3a60c62009-12-08 15:31:31 +0000891 addType(Arg, DIType(Ty.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000892 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000893 }
894 }
895 break;
896 case dwarf::DW_TAG_structure_type:
897 case dwarf::DW_TAG_union_type:
898 case dwarf::DW_TAG_class_type: {
899 // Add elements to structure type.
900 DIArray Elements = CTy.getTypeArray();
901
902 // A forward struct declared type may not have elements available.
903 if (Elements.isNull())
904 break;
905
906 // Add elements to structure type.
907 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
908 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000909 if (Element.isNull())
910 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000911 DIE *ElemDie = NULL;
912 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patela3a60c62009-12-08 15:31:31 +0000913 ElemDie = createMemberSubprogramDIE(DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000914 else
Devang Patela3a60c62009-12-08 15:31:31 +0000915 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000916 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +0000917 }
918
Devang Patela1ba2692009-08-27 23:51:51 +0000919 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000920 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000921
922 unsigned RLang = CTy.getRunTimeLang();
923 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000924 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +0000925 dwarf::DW_FORM_data1, RLang);
926 break;
927 }
928 default:
929 break;
930 }
931
932 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000933 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000934 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000935
936 if (Tag == dwarf::DW_TAG_enumeration_type ||
937 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
938 // Add size if non-zero (derived types might be zero-sized.)
939 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000940 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000941 else {
942 // Add zero size if it is not a forward declaration.
943 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000944 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000945 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000946 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +0000947 }
948
949 // Add source line info if available.
950 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000951 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000952 }
953}
954
Devang Patel2c4ceb12009-11-21 02:48:08 +0000955/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
956void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +0000957 int64_t L = SR.getLo();
958 int64_t H = SR.getHi();
959 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
960
Devang Patel2c4ceb12009-11-21 02:48:08 +0000961 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +0000962 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000963 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +0000964 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000965
Devang Patel2c4ceb12009-11-21 02:48:08 +0000966 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +0000967}
968
Devang Patel2c4ceb12009-11-21 02:48:08 +0000969/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patela3a60c62009-12-08 15:31:31 +0000970void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000971 DICompositeType *CTy) {
972 Buffer.setTag(dwarf::DW_TAG_array_type);
973 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000974 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000975
976 // Emit derived type.
Devang Patela3a60c62009-12-08 15:31:31 +0000977 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +0000978 DIArray Elements = CTy->getTypeArray();
979
Devang Patel6f01d9c2009-11-21 00:31:03 +0000980 // Get an anonymous type for index type.
Devang Patela3a60c62009-12-08 15:31:31 +0000981 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +0000982 if (!IdxTy) {
983 // Construct an anonymous type for index type.
984 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000985 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
986 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +0000987 dwarf::DW_ATE_signed);
Devang Patela3a60c62009-12-08 15:31:31 +0000988 ModuleCU->addDie(IdxTy);
989 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000990 }
Bill Wendling0310d762009-05-15 09:23:25 +0000991
992 // Add subranges to array type.
993 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
994 DIDescriptor Element = Elements.getElement(i);
995 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000996 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000997 }
998}
999
Devang Patel2c4ceb12009-11-21 02:48:08 +00001000/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patela3a60c62009-12-08 15:31:31 +00001001DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator *ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001002 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel65dbc902009-11-25 17:36:49 +00001003 StringRef Name = ETy->getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001004 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001005 int64_t Value = ETy->getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001006 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001007 return Enumerator;
1008}
1009
Devang Patel2c4ceb12009-11-21 02:48:08 +00001010/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patela3a60c62009-12-08 15:31:31 +00001011DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001012 // If the global variable was optmized out then no need to create debug info
1013 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001014 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001015 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001016
Bill Wendling0310d762009-05-15 09:23:25 +00001017 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001018 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001019 GV.getDisplayName());
1020
Devang Patel65dbc902009-11-25 17:36:49 +00001021 StringRef LinkageName = GV.getLinkageName();
1022 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001023 // Skip special LLVM prefix that is used to inform the asm printer to not
1024 // emit usual symbol prefix before the symbol name. This happens for
1025 // Objective-C symbol names and symbol whose name is replaced using GCC's
1026 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001027 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001028 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001029 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001030 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001031 }
Devang Patela3a60c62009-12-08 15:31:31 +00001032 addType(GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001033 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001034 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1035 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001036
1037 // Add address.
1038 DIEBlock *Block = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001039 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1040 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patelb71a16d2009-10-05 23:22:08 +00001041 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001042 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patelb71a16d2009-10-05 23:22:08 +00001043
Bill Wendling0310d762009-05-15 09:23:25 +00001044 return GVDie;
1045}
1046
Devang Patel2c4ceb12009-11-21 02:48:08 +00001047/// createMemberDIE - Create new member DIE.
Devang Patela3a60c62009-12-08 15:31:31 +00001048DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001049 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001050 StringRef Name = DT.getName();
1051 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001052 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001053
Devang Patela3a60c62009-12-08 15:31:31 +00001054 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001055
Devang Patel2c4ceb12009-11-21 02:48:08 +00001056 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001057
Devang Patel33db5082009-11-04 22:06:12 +00001058 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001059 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001060
Bill Wendling0310d762009-05-15 09:23:25 +00001061 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001062 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001063
1064 if (Size != FieldSize) {
1065 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001066 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1067 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001068
1069 uint64_t Offset = DT.getOffsetInBits();
1070 uint64_t FieldOffset = Offset;
1071 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1072 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1073 FieldOffset = (HiMark - FieldSize);
1074 Offset -= FieldOffset;
1075
1076 // Maybe we need to work from the other end.
1077 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001078 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001079
Devang Patel33db5082009-11-04 22:06:12 +00001080 // Here WD_AT_data_member_location points to the anonymous
1081 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001082 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001083
1084 } else
1085 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001086 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001087
Devang Patel2c4ceb12009-11-21 02:48:08 +00001088 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001089
1090 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001091 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001092 dwarf::DW_ACCESS_protected);
1093 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001094 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001095 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001096 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1097 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1098 dwarf::DW_ACCESS_public);
1099 if (DT.isVirtual())
1100 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1101 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001102 return MemberDie;
1103}
1104
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001105/// createRawSubprogramDIE - Create new partially incomplete DIE. This is
1106/// a helper routine used by createMemberSubprogramDIE and
1107/// createSubprogramDIE.
Devang Patela3a60c62009-12-08 15:31:31 +00001108DIE *DwarfDebug::createRawSubprogramDIE(const DISubprogram &SP) {
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001109 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel65dbc902009-11-25 17:36:49 +00001110 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001111
Devang Patel65dbc902009-11-25 17:36:49 +00001112 StringRef LinkageName = SP.getLinkageName();
1113 if (!LinkageName.empty()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001114 // Skip special LLVM prefix that is used to inform the asm printer to not
1115 // emit usual symbol prefix before the symbol name. This happens for
1116 // Objective-C symbol names and symbol whose name is replaced using GCC's
1117 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001118 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001119 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001120 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001121 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001122 }
Devang Patel2c4ceb12009-11-21 02:48:08 +00001123 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001124
Bill Wendling0310d762009-05-15 09:23:25 +00001125 // Add prototyped tag, if C or ObjC.
1126 unsigned Lang = SP.getCompileUnit().getLanguage();
1127 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1128 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001129 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001130
1131 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001132 DICompositeType SPTy = SP.getType();
1133 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001134 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001135
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001136 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patela3a60c62009-12-08 15:31:31 +00001137 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001138 else
Devang Patela3a60c62009-12-08 15:31:31 +00001139 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001140
Devang Patel5d11eb02009-12-03 19:11:07 +00001141 unsigned VK = SP.getVirtuality();
1142 if (VK) {
1143 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1144 DIEBlock *Block = new DIEBlock();
1145 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1146 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1147 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1148 ContainingTypeMap.insert(std::make_pair(SPDie, WeakVH(SP.getContainingType().getNode())));
1149 }
1150
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001151 return SPDie;
1152}
1153
1154/// createMemberSubprogramDIE - Create new member DIE using SP. This routine
1155/// always returns a die with DW_AT_declaration attribute.
Devang Patela3a60c62009-12-08 15:31:31 +00001156DIE *DwarfDebug::createMemberSubprogramDIE(const DISubprogram &SP) {
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001157 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1158 if (!SPDie)
Devang Patela3a60c62009-12-08 15:31:31 +00001159 SPDie = createSubprogramDIE(SP);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001160
1161 // If SPDie has DW_AT_declaration then reuse it.
1162 if (!SP.isDefinition())
1163 return SPDie;
1164
1165 // Otherwise create new DIE for the declaration. First push definition
1166 // DIE at the top level.
1167 if (TopLevelDIEs.insert(SPDie))
1168 TopLevelDIEsVector.push_back(SPDie);
1169
Devang Patela3a60c62009-12-08 15:31:31 +00001170 SPDie = createRawSubprogramDIE(SP);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001171
1172 // Add arguments.
1173 DICompositeType SPTy = SP.getType();
1174 DIArray Args = SPTy.getTypeArray();
1175 unsigned SPTag = SPTy.getTag();
1176 if (SPTag == dwarf::DW_TAG_subroutine_type)
1177 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1178 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patela3a60c62009-12-08 15:31:31 +00001179 addType(Arg, DIType(Args.getElement(i).getNode()));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001180 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1181 SPDie->addChild(Arg);
1182 }
1183
1184 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1185 return SPDie;
1186}
1187
1188/// createSubprogramDIE - Create new DIE using SP.
Devang Patela3a60c62009-12-08 15:31:31 +00001189DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP) {
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001190 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1191 if (SPDie)
1192 return SPDie;
1193
Devang Patela3a60c62009-12-08 15:31:31 +00001194 SPDie = createRawSubprogramDIE(SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001195
1196 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001197 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001198
1199 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001200 // be handled while processing variables.
1201 DICompositeType SPTy = SP.getType();
1202 DIArray Args = SPTy.getTypeArray();
1203 unsigned SPTag = SPTy.getTag();
1204
Bill Wendling0310d762009-05-15 09:23:25 +00001205 if (SPTag == dwarf::DW_TAG_subroutine_type)
1206 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1207 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patela3a60c62009-12-08 15:31:31 +00001208 addType(Arg, DIType(Args.getElement(i).getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001209 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1210 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001211 }
1212 }
1213
Bill Wendling0310d762009-05-15 09:23:25 +00001214 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patela3a60c62009-12-08 15:31:31 +00001215 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001216 return SPDie;
1217}
1218
Devang Patel2c4ceb12009-11-21 02:48:08 +00001219/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendling0310d762009-05-15 09:23:25 +00001220///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001221CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const {
Bill Wendling0310d762009-05-15 09:23:25 +00001222 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001223 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001224 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1225 return *I->second;
1226}
1227
Devang Patel53bb5c92009-11-10 23:06:00 +00001228/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1229/// Initialize scope and update scope hierarchy.
1230DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1231 MDNode *InlinedAt) {
1232 assert (N && "Invalid Scope encoding!");
1233 assert (MI && "Missing machine instruction!");
1234 bool GetConcreteScope = (MI && InlinedAt);
1235
1236 DbgScope *NScope = NULL;
1237
1238 if (InlinedAt)
1239 NScope = DbgScopeMap.lookup(InlinedAt);
1240 else
1241 NScope = DbgScopeMap.lookup(N);
1242 assert (NScope && "Unable to find working scope!");
1243
1244 if (NScope->getFirstInsn())
1245 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001246
1247 DbgScope *Parent = NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001248 if (GetConcreteScope) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001249 DILocation IL(InlinedAt);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001250 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel53bb5c92009-11-10 23:06:00 +00001251 IL.getOrigLocation().getNode());
1252 assert (Parent && "Unable to find Parent scope!");
1253 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001254 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001255 } else if (DIDescriptor(N).isLexicalBlock()) {
1256 DILexicalBlock DB(N);
1257 if (!DB.getContext().isNull()) {
1258 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1259 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001260 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001261 }
Devang Patelc90aefe2009-10-14 21:08:09 +00001262 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001263
Devang Patelbdf45cb2009-10-27 20:47:17 +00001264 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001265
Devang Patel53bb5c92009-11-10 23:06:00 +00001266 if (!Parent && !InlinedAt) {
Devang Patel39ae3ff2009-11-11 00:31:36 +00001267 StringRef SPName = DISubprogram(N).getLinkageName();
1268 if (SPName == MF->getFunction()->getName())
1269 CurrentFnDbgScope = NScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001270 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001271
Devang Patel53bb5c92009-11-10 23:06:00 +00001272 if (GetConcreteScope) {
1273 ConcreteScopes[InlinedAt] = NScope;
1274 getOrCreateAbstractScope(N);
1275 }
1276
Devang Patelbdf45cb2009-10-27 20:47:17 +00001277 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001278}
1279
Devang Patel53bb5c92009-11-10 23:06:00 +00001280DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1281 assert (N && "Invalid Scope encoding!");
1282
1283 DbgScope *AScope = AbstractScopes.lookup(N);
1284 if (AScope)
1285 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001286
Devang Patel53bb5c92009-11-10 23:06:00 +00001287 DbgScope *Parent = NULL;
1288
1289 DIDescriptor Scope(N);
1290 if (Scope.isLexicalBlock()) {
1291 DILexicalBlock DB(N);
1292 DIDescriptor ParentDesc = DB.getContext();
1293 if (!ParentDesc.isNull())
1294 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1295 }
1296
1297 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1298
1299 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001300 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001301 AScope->setAbstractScope();
1302 AbstractScopes[N] = AScope;
1303 if (DIDescriptor(N).isSubprogram())
1304 AbstractScopesList.push_back(AScope);
1305 return AScope;
1306}
Devang Patelaf9e8472009-10-01 20:31:14 +00001307
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001308/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001309/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1310/// If there are global variables in this scope then create and insert
1311/// DIEs for these variables.
1312DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001313
Devang Patel017d1212009-11-20 21:37:22 +00001314 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel53bb5c92009-11-10 23:06:00 +00001315 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001316 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001317 DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001318 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001319 DWLabel("func_end", SubprogramCount));
1320 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001321 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001322
Devang Patel53bb5c92009-11-10 23:06:00 +00001323 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001324 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001325
1326 // If there are global variables at this scope then add their dies.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001327 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00001328 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1329 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1330 if (!N) continue;
1331 DIGlobalVariable GV(N);
1332 if (GV.getContext().getNode() == SPNode) {
Devang Patela3a60c62009-12-08 15:31:31 +00001333 DIE *ScopedGVDie = createGlobalVariableDIE(GV);
Devang Patelfb0ee432009-11-10 23:20:04 +00001334 if (ScopedGVDie)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001335 SPDie->addChild(ScopedGVDie);
Devang Patel53bb5c92009-11-10 23:06:00 +00001336 }
1337 }
Devang Patel193f7202009-11-24 01:14:22 +00001338
Devang Patel53bb5c92009-11-10 23:06:00 +00001339 return SPDie;
1340}
1341
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001342/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001343/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1344DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001345 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1346 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1347
1348 // Ignore empty scopes.
1349 if (StartID == EndID && StartID != 0)
1350 return NULL;
1351
1352 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1353 if (Scope->isAbstractScope())
1354 return ScopeDIE;
1355
Devang Patel2c4ceb12009-11-21 02:48:08 +00001356 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001357 StartID ?
1358 DWLabel("label", StartID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001359 : DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001360 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001361 EndID ?
1362 DWLabel("label", EndID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001363 : DWLabel("func_end", SubprogramCount));
1364
1365
1366
1367 return ScopeDIE;
1368}
1369
Devang Patel2c4ceb12009-11-21 02:48:08 +00001370/// constructInlinedScopeDIE - This scope represents inlined body of
1371/// a function. Construct DIE to represent this concrete inlined copy
1372/// of the function.
1373DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001374 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1375 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1376 assert (StartID && "Invalid starting label for an inlined scope!");
1377 assert (EndID && "Invalid end label for an inlined scope!");
1378 // Ignore empty scopes.
1379 if (StartID == EndID && StartID != 0)
1380 return NULL;
1381
1382 DIScope DS(Scope->getScopeNode());
1383 if (DS.isNull())
1384 return NULL;
1385 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1386
1387 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001388 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001389 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001390 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001391 dwarf::DW_FORM_ref4, OriginDIE);
1392
Devang Patel2c4ceb12009-11-21 02:48:08 +00001393 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001394 DWLabel("label", StartID));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001395 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001396 DWLabel("label", EndID));
1397
1398 InlinedSubprogramDIEs.insert(OriginDIE);
1399
1400 // Track the start label for this inlined function.
1401 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1402 I = InlineInfo.find(InlinedSP.getNode());
1403
1404 if (I == InlineInfo.end()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001405 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1406 ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001407 InlinedSPNodes.push_back(InlinedSP.getNode());
1408 } else
1409 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1410
1411 StringPool.insert(InlinedSP.getName());
1412 StringPool.insert(InlinedSP.getLinkageName());
1413 DILocation DL(Scope->getInlinedAt());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001414 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1415 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001416
1417 return ScopeDIE;
1418}
1419
Devang Patel2c4ceb12009-11-21 02:48:08 +00001420
1421/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patela3a60c62009-12-08 15:31:31 +00001422DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001423 // Get the descriptor.
1424 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001425 StringRef Name = VD.getName();
1426 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001427 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001428
1429 // Translate tag to proper Dwarf tag. The result variable is dropped for
1430 // now.
1431 unsigned Tag;
1432 switch (VD.getTag()) {
1433 case dwarf::DW_TAG_return_variable:
1434 return NULL;
1435 case dwarf::DW_TAG_arg_variable:
1436 Tag = dwarf::DW_TAG_formal_parameter;
1437 break;
1438 case dwarf::DW_TAG_auto_variable: // fall thru
1439 default:
1440 Tag = dwarf::DW_TAG_variable;
1441 break;
1442 }
1443
1444 // Define variable debug information entry.
1445 DIE *VariableDie = new DIE(Tag);
1446
1447
1448 DIE *AbsDIE = NULL;
1449 if (DbgVariable *AV = DV->getAbstractVariable())
1450 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001451
Devang Patel53bb5c92009-11-10 23:06:00 +00001452 if (AbsDIE) {
1453 DIScope DS(Scope->getScopeNode());
1454 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001455 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc0326792009-11-11 03:09:50 +00001456 (void) OriginSPDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001457 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1458 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1459 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001460 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001461 dwarf::DW_FORM_ref4, AbsDIE);
1462 }
1463 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001464 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1465 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001466
1467 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001468 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001469 // addresses instead.
1470 if (VD.isBlockByrefVariable())
Devang Patela3a60c62009-12-08 15:31:31 +00001471 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001472 else
Devang Patela3a60c62009-12-08 15:31:31 +00001473 addType(VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001474 }
1475
1476 // Add variable address.
1477 if (!Scope->isAbstractScope()) {
1478 MachineLocation Location;
Jim Grosbacha2f20b22009-11-22 20:14:00 +00001479 unsigned FrameReg;
1480 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1481 Location.set(FrameReg, Offset);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001482
Devang Patel53bb5c92009-11-10 23:06:00 +00001483 if (VD.hasComplexAddress())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001484 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001485 else if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001486 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001487 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001488 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001489 }
1490 DV->setDIE(VariableDie);
1491 return VariableDie;
1492
1493}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001494
Devang Patel193f7202009-11-24 01:14:22 +00001495void DwarfDebug::addPubTypes(DISubprogram SP) {
1496 DICompositeType SPTy = SP.getType();
1497 unsigned SPTag = SPTy.getTag();
1498 if (SPTag != dwarf::DW_TAG_subroutine_type)
1499 return;
1500
1501 DIArray Args = SPTy.getTypeArray();
1502 if (Args.isNull())
1503 return;
1504
1505 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1506 DIType ATy(Args.getElement(i).getNode());
1507 if (ATy.isNull())
1508 continue;
1509 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel65dbc902009-11-25 17:36:49 +00001510 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001511 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1512 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1513 }
1514 }
1515}
1516
Devang Patel2c4ceb12009-11-21 02:48:08 +00001517/// constructScopeDIE - Construct a DIE for this scope.
1518DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001519 if (!Scope)
1520 return NULL;
1521 DIScope DS(Scope->getScopeNode());
1522 if (DS.isNull())
1523 return NULL;
1524
1525 DIE *ScopeDIE = NULL;
1526 if (Scope->getInlinedAt())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001527 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001528 else if (DS.isSubprogram()) {
1529 if (Scope->isAbstractScope())
Devang Patel017d1212009-11-20 21:37:22 +00001530 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001531 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001532 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001533 }
1534 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001535 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001536 if (!ScopeDIE) return NULL;
1537 }
1538
1539 // Add variables to scope.
1540 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1541 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patela3a60c62009-12-08 15:31:31 +00001542 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001543 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001544 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001545 }
1546
1547 // Add nested scopes.
1548 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1549 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1550 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001551 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001552 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001553 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001554 }
Devang Patel193f7202009-11-24 01:14:22 +00001555
1556 if (DS.isSubprogram())
1557 addPubTypes(DISubprogram(DS.getNode()));
1558
1559 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001560}
1561
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001562/// GetOrCreateSourceID - Look up the source id with the given directory and
1563/// source file names. If none currently exists, create a new id and insert it
1564/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1565/// maps as well.
Devang Patel65dbc902009-11-25 17:36:49 +00001566unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001567 unsigned DId;
1568 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1569 if (DI != DirectoryIdMap.end()) {
1570 DId = DI->getValue();
1571 } else {
1572 DId = DirectoryNames.size() + 1;
1573 DirectoryIdMap[DirName] = DId;
1574 DirectoryNames.push_back(DirName);
1575 }
1576
1577 unsigned FId;
1578 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1579 if (FI != SourceFileIdMap.end()) {
1580 FId = FI->getValue();
1581 } else {
1582 FId = SourceFileNames.size() + 1;
1583 SourceFileIdMap[FileName] = FId;
1584 SourceFileNames.push_back(FileName);
1585 }
1586
1587 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1588 SourceIdMap.find(std::make_pair(DId, FId));
1589 if (SI != SourceIdMap.end())
1590 return SI->second;
1591
1592 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1593 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1594 SourceIds.push_back(std::make_pair(DId, FId));
1595
1596 return SrcId;
1597}
1598
Devang Patel4063f6b2009-12-07 21:41:32 +00001599/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1600DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace &NS) {
1601 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1602 if (NDie)
1603 return NDie;
1604
1605 NDie = new DIE(dwarf::DW_TAG_namespace);
1606 ModuleCU->insertDIE(NS.getNode(), NDie);
1607 if (!NS.getName().empty())
1608 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1609 addSourceLine(NDie, &NS);
1610 DIDescriptor NSContext = NS.getContext();
1611 DIE *Context = NULL;
1612 if (NSContext.isNameSpace()) {
1613 DINameSpace NS2(NSContext.getNode());
1614 Context = getOrCreateNameSpace(NS2);
1615 }
1616 else
1617 Context = ModuleCU->getCUDie();
1618 Context->addChild(NDie);
1619 return NDie;
1620}
1621
Devang Patel2c4ceb12009-11-21 02:48:08 +00001622void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001623 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001624 StringRef FN = DIUnit.getFilename();
1625 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001626 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001627
1628 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001629 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001630 DWLabel("section_line", 0), DWLabel("section_line", 0),
1631 false);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001632 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001633 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001634 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001635 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001636 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001637
Devang Patel65dbc902009-11-25 17:36:49 +00001638 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001639 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001640 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001641 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001642
Devang Patel65dbc902009-11-25 17:36:49 +00001643 StringRef Flags = DIUnit.getFlags();
1644 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001645 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001646
1647 unsigned RVer = DIUnit.getRunTimeVersion();
1648 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001649 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001650 dwarf::DW_FORM_data1, RVer);
1651
1652 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001653 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001654 // Use first compile unit marked as isMain as the compile unit
1655 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001656 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001657 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001658
Devang Patele4b27562009-08-28 23:24:31 +00001659 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001660 CompileUnits.push_back(Unit);
1661}
1662
Devang Patel2c4ceb12009-11-21 02:48:08 +00001663void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001664 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001665
Devang Patel905cf5e2009-09-04 23:59:07 +00001666 // If debug information is malformed then ignore it.
1667 if (DI_GV.Verify() == false)
1668 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001669
1670 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001671 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel13e16b62009-06-26 01:49:18 +00001672 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001673
Devang Patela3a60c62009-12-08 15:31:31 +00001674 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001675
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001676 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001677 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001678
1679 // Add to context owner.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001680 if (TopLevelDIEs.insert(VariableDie))
1681 TopLevelDIEsVector.push_back(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001682
1683 // Expose as global. FIXME - need to check external flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001684 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001685
1686 DIType GTy = DI_GV.getType();
Devang Patel65dbc902009-11-25 17:36:49 +00001687 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001688 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1689 assert (Entry && "Missing global type!");
1690 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1691 }
Devang Patel13e16b62009-06-26 01:49:18 +00001692 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001693}
1694
Devang Patel2c4ceb12009-11-21 02:48:08 +00001695void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001696 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001697
1698 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001699 if (ModuleCU->getDIE(N))
Devang Patel13e16b62009-06-26 01:49:18 +00001700 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001701
1702 if (!SP.isDefinition())
1703 // This is a method declaration which will be handled while constructing
1704 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001705 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001706
Devang Patela3a60c62009-12-08 15:31:31 +00001707 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001708
1709 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001710 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001711
1712 // Add to context owner.
Devang Patel4063f6b2009-12-07 21:41:32 +00001713 DIDescriptor SPContext = SP.getContext();
1714 if (SPContext.isCompileUnit()
1715 && SPContext.getNode() == SP.getCompileUnit().getNode()) {
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001716 if (TopLevelDIEs.insert(SubprogramDie))
1717 TopLevelDIEsVector.push_back(SubprogramDie);
Devang Patel4063f6b2009-12-07 21:41:32 +00001718 } else if (SPContext.isNameSpace()) {
1719 DINameSpace NS(SPContext.getNode());
1720 DIE *NDie = getOrCreateNameSpace(NS);
1721 NDie->addChild(SubprogramDie);
1722 }
1723
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001724 // Expose as global.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001725 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001726
Devang Patel13e16b62009-06-26 01:49:18 +00001727 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001728}
1729
Devang Patel2c4ceb12009-11-21 02:48:08 +00001730/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001731/// content. Create global DIEs and emit initial debug info sections.
1732/// This is inovked by the target AsmPrinter.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001733void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel208622d2009-06-25 22:36:02 +00001734 this->M = M;
1735
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001736 if (TimePassesIsEnabled)
1737 DebugTimer->startTimer();
1738
Devang Patel3380cc52009-11-11 19:55:08 +00001739 if (!MAI->doesSupportDebugInformation())
1740 return;
1741
Devang Patel78ab9e22009-07-30 18:56:46 +00001742 DebugInfoFinder DbgFinder;
1743 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001744
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001745 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001746 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1747 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001748 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001749
1750 if (CompileUnits.empty()) {
1751 if (TimePassesIsEnabled)
1752 DebugTimer->stopTimer();
1753
1754 return;
1755 }
1756
Devang Patel70f44262009-06-29 20:38:13 +00001757 // If main compile unit for this module is not seen than randomly
1758 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001759 if (!ModuleCU)
1760 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001761
Devang Patel13e16b62009-06-26 01:49:18 +00001762 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001763 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patelfd07cf52009-10-05 23:40:42 +00001764 E = DbgFinder.global_variable_end(); I != E; ++I) {
1765 DIGlobalVariable GV(*I);
Devang Patel4063f6b2009-12-07 21:41:32 +00001766 DIDescriptor GVContext = GV.getContext();
1767 if (GVContext.isCompileUnit()
1768 && GVContext.getNode() == GV.getCompileUnit().getNode())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001769 constructGlobalVariableDIE(*I);
Devang Patel4063f6b2009-12-07 21:41:32 +00001770 else if (GVContext.isNameSpace()) {
Devang Patela3a60c62009-12-08 15:31:31 +00001771 DIE *GVDie = createGlobalVariableDIE(GV);
Devang Patel018b6602009-12-08 15:01:35 +00001772 if (GVDie) {
1773 DINameSpace NS(GVContext.getNode());
1774 DIE *NDie = getOrCreateNameSpace(NS);
1775 NDie->addChild(GVDie);
1776 }
Devang Patel4063f6b2009-12-07 21:41:32 +00001777 }
1778 else
1779 ScopedGVs.push_back(*I);
Devang Patelfd07cf52009-10-05 23:40:42 +00001780 }
Devang Patel13e16b62009-06-26 01:49:18 +00001781
Devang Patel53bb5c92009-11-10 23:06:00 +00001782 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001783 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1784 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001785 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001786
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001787 MMI = mmi;
1788 shouldEmit = true;
1789 MMI->setDebugInfoAvailability(true);
1790
1791 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001792 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001793
1794 // Print out .file directives to specify files for .loc directives. These are
1795 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001796 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001797 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1798 // Remember source id starts at 1.
1799 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1800 sys::Path FullPath(getSourceDirectoryName(Id.first));
1801 bool AppendOk =
1802 FullPath.appendComponent(getSourceFileName(Id.second));
1803 assert(AppendOk && "Could not append filename to directory!");
1804 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001805 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001806 Asm->EOL();
1807 }
1808 }
1809
1810 // Emit initial sections
Devang Patel2c4ceb12009-11-21 02:48:08 +00001811 emitInitial();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001812
1813 if (TimePassesIsEnabled)
1814 DebugTimer->stopTimer();
1815}
1816
Devang Patel2c4ceb12009-11-21 02:48:08 +00001817/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001818///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001819void DwarfDebug::endModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001820 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001821 return;
1822
1823 if (TimePassesIsEnabled)
1824 DebugTimer->startTimer();
1825
Devang Patel53bb5c92009-11-10 23:06:00 +00001826 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1827 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1828 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1829 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001830 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001831 }
1832
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001833 // Insert top level DIEs.
1834 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1835 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1836 ModuleCU->getCUDie()->addChild(*TI);
1837
Devang Patel5d11eb02009-12-03 19:11:07 +00001838 for (DenseMap<DIE *, WeakVH>::iterator CI = ContainingTypeMap.begin(),
1839 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1840 DIE *SPDie = CI->first;
1841 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1842 if (!N) continue;
1843 DIE *NDie = ModuleCU->getDIE(N);
1844 if (!NDie) continue;
1845 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1846 addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1847 }
1848
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001849 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001850 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001851 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001852 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001853 EmitLabel("data_end", 0);
1854
1855 // End text sections.
1856 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001857 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001858 EmitLabel("section_end", i);
1859 }
1860
1861 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001862 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001863
1864 // Emit function debug frame information
1865 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1866 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001867 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001868
1869 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001870 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001871
1872 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001873 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001874
1875 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001876 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001877
1878 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001879 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001880
1881 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001882 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001883
Devang Patel193f7202009-11-24 01:14:22 +00001884 // Emit info into a debug pubtypes section.
1885 emitDebugPubTypes();
1886
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001887 // Emit info into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001888 emitDebugStr();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001889
1890 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001891 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001892
1893 // Emit info into a debug aranges section.
1894 EmitDebugARanges();
1895
1896 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001897 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001898
1899 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001900 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001901
1902 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001903 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001904
1905 if (TimePassesIsEnabled)
1906 DebugTimer->stopTimer();
1907}
1908
Devang Patel53bb5c92009-11-10 23:06:00 +00001909/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001910DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1911 unsigned FrameIdx,
Devang Patel53bb5c92009-11-10 23:06:00 +00001912 DILocation &ScopeLoc) {
1913
1914 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1915 if (AbsDbgVariable)
1916 return AbsDbgVariable;
1917
1918 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1919 if (!Scope)
1920 return NULL;
1921
1922 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001923 Scope->addVariable(AbsDbgVariable);
Devang Patel53bb5c92009-11-10 23:06:00 +00001924 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1925 return AbsDbgVariable;
1926}
1927
Devang Patel2c4ceb12009-11-21 02:48:08 +00001928/// collectVariableInfo - Populate DbgScope entries with variables' info.
1929void DwarfDebug::collectVariableInfo() {
Devang Patelac1ceb32009-10-09 22:42:28 +00001930 if (!MMI) return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001931
Devang Patele717faa2009-10-06 01:26:37 +00001932 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1933 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1934 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac1ceb32009-10-09 22:42:28 +00001935 MetadataBase *MB = VI->first;
1936 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel53bb5c92009-11-10 23:06:00 +00001937 if (!Var) continue;
Devang Pateleda31212009-10-08 18:48:03 +00001938 DIVariable DV (Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001939 std::pair< unsigned, MDNode *> VP = VI->second;
1940 DILocation ScopeLoc(VP.second);
1941
1942 DbgScope *Scope =
1943 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1944 if (!Scope)
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001945 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelfb0ee432009-11-10 23:20:04 +00001946 // If variable scope is not found then skip this variable.
1947 if (!Scope)
1948 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001949
1950 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001951 Scope->addVariable(RegVar);
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001952 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1953 ScopeLoc))
Devang Patel53bb5c92009-11-10 23:06:00 +00001954 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patele717faa2009-10-06 01:26:37 +00001955 }
1956}
1957
Devang Patel2c4ceb12009-11-21 02:48:08 +00001958/// beginScope - Process beginning of a scope starting at Label.
1959void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001960 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1961 if (I == DbgScopeBeginMap.end())
1962 return;
Dan Gohman277207e2009-11-23 21:30:55 +00001963 ScopeVector &SD = I->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001964 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001965 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001966 (*SDI)->setStartLabelID(Label);
1967}
1968
Devang Patel2c4ceb12009-11-21 02:48:08 +00001969/// endScope - Process end of a scope.
1970void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001971 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00001972 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00001973 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001974
1975 unsigned Label = MMI->NextLabelID();
1976 Asm->printLabel(Label);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001977 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001978
Devang Patel0d20ac82009-10-06 01:50:42 +00001979 SmallVector<DbgScope *, 2> &SD = I->second;
1980 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001981 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001982 (*SDI)->setEndLabelID(Label);
Devang Patel53bb5c92009-11-10 23:06:00 +00001983 return;
1984}
1985
1986/// createDbgScope - Create DbgScope for the scope.
1987void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1988
1989 if (!InlinedAt) {
1990 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1991 if (WScope)
1992 return;
1993 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1994 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001995 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel2f105c62009-11-11 00:18:40 +00001996 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001997 return;
1998 }
1999
2000 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2001 if (WScope)
2002 return;
2003
2004 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2005 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2006 DILocation DL(InlinedAt);
2007 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel0d20ac82009-10-06 01:50:42 +00002008}
2009
Devang Patel2c4ceb12009-11-21 02:48:08 +00002010/// extractScopeInformation - Scan machine instructions in this function
Devang Patelaf9e8472009-10-01 20:31:14 +00002011/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002012bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patelaf9e8472009-10-01 20:31:14 +00002013 // If scope information was extracted using .dbg intrinsics then there is not
2014 // any need to extract these information by scanning each instruction.
2015 if (!DbgScopeMap.empty())
2016 return false;
2017
Devang Patel53bb5c92009-11-10 23:06:00 +00002018 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelaf9e8472009-10-01 20:31:14 +00002019 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2020 I != E; ++I) {
2021 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2022 II != IE; ++II) {
2023 const MachineInstr *MInsn = II;
2024 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00002025 if (DL.isUnknown()) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002026 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel53bb5c92009-11-10 23:06:00 +00002027 if (!DLT.Scope) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002028 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002029 // other scopes, create one DbgScope now. This will be translated
Devang Patelaf9e8472009-10-01 20:31:14 +00002030 // into a scope DIE at the end.
Devang Patel53bb5c92009-11-10 23:06:00 +00002031 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2032 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
2033 }
2034 }
2035
2036
2037 // Build scope hierarchy using working set of scopes.
2038 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2039 I != E; ++I) {
2040 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2041 II != IE; ++II) {
2042 const MachineInstr *MInsn = II;
2043 DebugLoc DL = MInsn->getDebugLoc();
2044 if (DL.isUnknown()) continue;
2045 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
2046 if (!DLT.Scope) continue;
2047 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002048 // other scopes, create one DbgScope now. This will be translated
Devang Patel53bb5c92009-11-10 23:06:00 +00002049 // into a scope DIE at the end.
2050 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2051 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
2052 Scope->setLastInsn(MInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +00002053 }
2054 }
2055
2056 // If a scope's last instruction is not set then use its child scope's
2057 // last instruction as this scope's last instrunction.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002058 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002059 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002060 if (DI->second->isAbstractScope())
2061 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002062 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00002063 DI->second->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +00002064 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2065 }
2066
2067 // Each scope has first instruction and last instruction to mark beginning
2068 // and end of a scope respectively. Create an inverse map that list scopes
2069 // starts (and ends) with an instruction. One instruction may start (or end)
2070 // multiple scopes.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002071 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002072 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2073 DbgScope *S = DI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002074 if (S->isAbstractScope())
2075 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002076 const MachineInstr *MI = S->getFirstInsn();
2077 assert (MI && "DbgScope does not have first instruction!");
2078
2079 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2080 if (IDI != DbgScopeBeginMap.end())
2081 IDI->second.push_back(S);
2082 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002083 DbgScopeBeginMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002084
2085 MI = S->getLastInsn();
2086 assert (MI && "DbgScope does not have last instruction!");
2087 IDI = DbgScopeEndMap.find(MI);
2088 if (IDI != DbgScopeEndMap.end())
2089 IDI->second.push_back(S);
2090 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002091 DbgScopeEndMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002092 }
2093
2094 return !DbgScopeMap.empty();
2095}
2096
Devang Patel2c4ceb12009-11-21 02:48:08 +00002097/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002098/// emitted immediately after the function entry point.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002099void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002100 this->MF = MF;
2101
2102 if (!ShouldEmitDwarfDebug()) return;
2103
2104 if (TimePassesIsEnabled)
2105 DebugTimer->startTimer();
2106
Devang Patel2c4ceb12009-11-21 02:48:08 +00002107 if (!extractScopeInformation(MF))
Devang Patel60b35bd2009-10-06 18:37:31 +00002108 return;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002109
2110 collectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002111
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002112 // Begin accumulating function debug information.
2113 MMI->BeginFunction(MF);
2114
2115 // Assumes in correct section after the entry point.
2116 EmitLabel("func_begin", ++SubprogramCount);
2117
2118 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2119 // function.
Devang Patelac1ceb32009-10-09 22:42:28 +00002120 DebugLoc FDL = MF->getDefaultDebugLoc();
2121 if (!FDL.isUnknown()) {
2122 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2123 unsigned LabelID = 0;
Devang Patel1619dc32009-10-13 23:28:53 +00002124 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002125 if (!SP.isNull())
Devang Patel2c4ceb12009-11-21 02:48:08 +00002126 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002127 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00002128 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002129 Asm->printLabel(LabelID);
2130 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002131 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002132 if (TimePassesIsEnabled)
2133 DebugTimer->stopTimer();
2134}
2135
Devang Patel2c4ceb12009-11-21 02:48:08 +00002136/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002137///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002138void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002139 if (!ShouldEmitDwarfDebug()) return;
2140
2141 if (TimePassesIsEnabled)
2142 DebugTimer->startTimer();
2143
Devang Patelac1ceb32009-10-09 22:42:28 +00002144 if (DbgScopeMap.empty())
2145 return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002146
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002147 // Define end label for subprogram.
2148 EmitLabel("func_end", SubprogramCount);
2149
2150 // Get function line info.
2151 if (!Lines.empty()) {
2152 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00002153 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002154 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2155 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2156 // Append the function info to section info.
2157 SectionLineInfos.insert(SectionLineInfos.end(),
2158 Lines.begin(), Lines.end());
2159 }
2160
Devang Patel53bb5c92009-11-10 23:06:00 +00002161 // Construct abstract scopes.
2162 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002163 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002164 constructScopeDIE(*AI);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002165
Devang Patel2c4ceb12009-11-21 02:48:08 +00002166 constructScopeDIE(CurrentFnDbgScope);
Devang Patel70d75ca2009-11-12 19:02:56 +00002167
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002168 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2169 MMI->getFrameMoves()));
2170
2171 // Clear debug info
Devang Patelc09ddc12009-12-01 18:13:48 +00002172 CurrentFnDbgScope = NULL;
2173 DbgScopeMap.clear();
2174 DbgScopeBeginMap.clear();
2175 DbgScopeEndMap.clear();
2176 ConcreteScopes.clear();
2177 AbstractScopesList.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002178
2179 Lines.clear();
Devang Patelc09ddc12009-12-01 18:13:48 +00002180
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002181 if (TimePassesIsEnabled)
2182 DebugTimer->stopTimer();
2183}
2184
Devang Patel2c4ceb12009-11-21 02:48:08 +00002185/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002186/// label. Returns a unique label ID used to generate a label and provide
2187/// correspondence to the source line list.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002188unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002189 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002190 if (!MMI)
2191 return 0;
2192
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002193 if (TimePassesIsEnabled)
2194 DebugTimer->startTimer();
2195
Devang Patel65dbc902009-11-25 17:36:49 +00002196 StringRef Dir;
2197 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002198
2199 DIDescriptor Scope(S);
2200 if (Scope.isCompileUnit()) {
2201 DICompileUnit CU(S);
2202 Dir = CU.getDirectory();
2203 Fn = CU.getFilename();
2204 } else if (Scope.isSubprogram()) {
2205 DISubprogram SP(S);
2206 Dir = SP.getDirectory();
2207 Fn = SP.getFilename();
2208 } else if (Scope.isLexicalBlock()) {
2209 DILexicalBlock DB(S);
2210 Dir = DB.getDirectory();
2211 Fn = DB.getFilename();
2212 } else
2213 assert (0 && "Unexpected scope info");
2214
2215 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002216 unsigned ID = MMI->NextLabelID();
2217 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2218
2219 if (TimePassesIsEnabled)
2220 DebugTimer->stopTimer();
2221
2222 return ID;
2223}
2224
2225/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2226/// timed. Look up the source id with the given directory and source file
2227/// names. If none currently exists, create a new id and insert it in the
2228/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2229/// well.
2230unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2231 const std::string &FileName) {
2232 if (TimePassesIsEnabled)
2233 DebugTimer->startTimer();
2234
Devang Patel5ccdd102009-09-29 18:40:58 +00002235 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002236
2237 if (TimePassesIsEnabled)
2238 DebugTimer->stopTimer();
2239
2240 return SrcId;
2241}
2242
Bill Wendling829e67b2009-05-20 23:22:40 +00002243//===----------------------------------------------------------------------===//
2244// Emit Methods
2245//===----------------------------------------------------------------------===//
2246
Devang Patel2c4ceb12009-11-21 02:48:08 +00002247/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002248///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002249unsigned
2250DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002251 // Get the children.
2252 const std::vector<DIE *> &Children = Die->getChildren();
2253
2254 // If not last sibling and has children then add sibling offset attribute.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002255 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002256
2257 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002258 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002259
2260 // Get the abbreviation for this DIE.
2261 unsigned AbbrevNumber = Die->getAbbrevNumber();
2262 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2263
2264 // Set DIE offset
2265 Die->setOffset(Offset);
2266
2267 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002268 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002269
2270 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2271 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2272
2273 // Size the DIE attribute values.
2274 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2275 // Size attribute value.
2276 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2277
2278 // Size the DIE children if any.
2279 if (!Children.empty()) {
2280 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2281 "Children flag not set");
2282
2283 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002284 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002285
2286 // End of children marker.
2287 Offset += sizeof(int8_t);
2288 }
2289
2290 Die->setSize(Offset - Die->getOffset());
2291 return Offset;
2292}
2293
Devang Patel2c4ceb12009-11-21 02:48:08 +00002294/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002295///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002296void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002297 // Compute size of compile unit header.
2298 static unsigned Offset =
2299 sizeof(int32_t) + // Length of Compilation Unit Info
2300 sizeof(int16_t) + // DWARF version number
2301 sizeof(int32_t) + // Offset Into Abbrev. Section
2302 sizeof(int8_t); // Pointer Size (in bytes)
2303
Devang Patel2c4ceb12009-11-21 02:48:08 +00002304 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel1dbc7712009-06-29 20:45:18 +00002305 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002306}
2307
Devang Patel2c4ceb12009-11-21 02:48:08 +00002308/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling94d04b82009-05-20 23:21:38 +00002309/// tools to recognize the object file contains Dwarf information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002310void DwarfDebug::emitInitial() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002311 // Check to see if we already emitted intial headers.
2312 if (didInitial) return;
2313 didInitial = true;
2314
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002315 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002316
Bill Wendling94d04b82009-05-20 23:21:38 +00002317 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002318 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002319 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002320 EmitLabel("section_debug_frame", 0);
2321 }
2322
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002323 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002324 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002325 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002326 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002327 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002328 EmitLabel("section_aranges", 0);
2329
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002330 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2331 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002332 EmitLabel("section_macinfo", 0);
2333 }
2334
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002335 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002336 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002337 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002338 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002339 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002340 EmitLabel("section_pubnames", 0);
Devang Patel193f7202009-11-24 01:14:22 +00002341 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2342 EmitLabel("section_pubtypes", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002343 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002344 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002345 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002346 EmitLabel("section_ranges", 0);
2347
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002348 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002349 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002350 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002351 EmitLabel("data_begin", 0);
2352}
2353
Devang Patel2c4ceb12009-11-21 02:48:08 +00002354/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002355///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002356void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002357 // Get the abbreviation for this DIE.
2358 unsigned AbbrevNumber = Die->getAbbrevNumber();
2359 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2360
2361 Asm->EOL();
2362
2363 // Emit the code (index) for the abbreviation.
2364 Asm->EmitULEB128Bytes(AbbrevNumber);
2365
2366 if (Asm->isVerbose())
2367 Asm->EOL(std::string("Abbrev [" +
2368 utostr(AbbrevNumber) +
2369 "] 0x" + utohexstr(Die->getOffset()) +
2370 ":0x" + utohexstr(Die->getSize()) + " " +
2371 dwarf::TagString(Abbrev->getTag())));
2372 else
2373 Asm->EOL();
2374
2375 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2376 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2377
2378 // Emit the DIE attribute values.
2379 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2380 unsigned Attr = AbbrevData[i].getAttribute();
2381 unsigned Form = AbbrevData[i].getForm();
2382 assert(Form && "Too many attributes for DIE (check abbreviation)");
2383
2384 switch (Attr) {
2385 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002386 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002387 break;
2388 case dwarf::DW_AT_abstract_origin: {
2389 DIEEntry *E = cast<DIEEntry>(Values[i]);
2390 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002391 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002392 Asm->EmitInt32(Addr);
2393 break;
2394 }
2395 default:
2396 // Emit an attribute using the defined form.
2397 Values[i]->EmitValue(this, Form);
2398 break;
2399 }
2400
2401 Asm->EOL(dwarf::AttributeString(Attr));
2402 }
2403
2404 // Emit the DIE children if any.
2405 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2406 const std::vector<DIE *> &Children = Die->getChildren();
2407
2408 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002409 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002410
2411 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2412 }
2413}
2414
Devang Patela3a60c62009-12-08 15:31:31 +00002415/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002416///
Devang Patela3a60c62009-12-08 15:31:31 +00002417void DwarfDebug::emitDebugInfo() {
2418 // Start debug info section.
2419 Asm->OutStreamer.SwitchSection(
2420 Asm->getObjFileLowering().getDwarfInfoSection());
2421 DIE *Die = ModuleCU->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002422
2423 // Emit the compile units header.
Devang Patela3a60c62009-12-08 15:31:31 +00002424 EmitLabel("info_begin", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002425
2426 // Emit size of content not including length itself
2427 unsigned ContentSize = Die->getSize() +
2428 sizeof(int16_t) + // DWARF version number
2429 sizeof(int32_t) + // Offset Into Abbrev. Section
2430 sizeof(int8_t) + // Pointer Size (in bytes)
2431 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2432
2433 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2434 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2435 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2436 Asm->EOL("Offset Into Abbrev. Section");
2437 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2438
Devang Patel2c4ceb12009-11-21 02:48:08 +00002439 emitDIE(Die);
Bill Wendling94d04b82009-05-20 23:21:38 +00002440 // FIXME - extra padding for gdb bug.
2441 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2442 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2443 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2444 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Devang Patela3a60c62009-12-08 15:31:31 +00002445 EmitLabel("info_end", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002446
2447 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002448
Bill Wendling94d04b82009-05-20 23:21:38 +00002449}
2450
Devang Patel2c4ceb12009-11-21 02:48:08 +00002451/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002452///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002453void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002454 // Check to see if it is worth the effort.
2455 if (!Abbreviations.empty()) {
2456 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002457 Asm->OutStreamer.SwitchSection(
2458 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002459
2460 EmitLabel("abbrev_begin", 0);
2461
2462 // For each abbrevation.
2463 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2464 // Get abbreviation data
2465 const DIEAbbrev *Abbrev = Abbreviations[i];
2466
2467 // Emit the abbrevations code (base 1 index.)
2468 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2469 Asm->EOL("Abbreviation Code");
2470
2471 // Emit the abbreviations data.
2472 Abbrev->Emit(Asm);
2473
2474 Asm->EOL();
2475 }
2476
2477 // Mark end of abbreviations.
2478 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2479
2480 EmitLabel("abbrev_end", 0);
2481 Asm->EOL();
2482 }
2483}
2484
Devang Patel2c4ceb12009-11-21 02:48:08 +00002485/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002486/// the line matrix.
2487///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002488void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002489 // Define last address of section.
2490 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2491 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2492 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2493 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2494
2495 // Mark end of matrix.
2496 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2497 Asm->EmitULEB128Bytes(1); Asm->EOL();
2498 Asm->EmitInt8(1); Asm->EOL();
2499}
2500
Devang Patel2c4ceb12009-11-21 02:48:08 +00002501/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002502///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002503void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002504 // If the target is using .loc/.file, the assembler will be emitting the
2505 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002506 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002507 return;
2508
2509 // Minimum line delta, thus ranging from -10..(255-10).
2510 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2511 // Maximum line delta, thus ranging from -10..(255-10).
2512 const int MaxLineDelta = 255 + MinLineDelta;
2513
2514 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002515 Asm->OutStreamer.SwitchSection(
2516 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002517
2518 // Construct the section header.
2519 EmitDifference("line_end", 0, "line_begin", 0, true);
2520 Asm->EOL("Length of Source Line Info");
2521 EmitLabel("line_begin", 0);
2522
2523 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2524
2525 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2526 Asm->EOL("Prolog Length");
2527 EmitLabel("line_prolog_begin", 0);
2528
2529 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2530
2531 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2532
2533 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2534
2535 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2536
2537 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2538
2539 // Line number standard opcode encodings argument count
2540 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2541 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2542 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2543 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2544 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2545 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2546 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2547 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2548 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2549
2550 // Emit directories.
2551 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2552 Asm->EmitString(getSourceDirectoryName(DI));
2553 Asm->EOL("Directory");
2554 }
2555
2556 Asm->EmitInt8(0); Asm->EOL("End of directories");
2557
2558 // Emit files.
2559 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2560 // Remember source id starts at 1.
2561 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2562 Asm->EmitString(getSourceFileName(Id.second));
2563 Asm->EOL("Source");
2564 Asm->EmitULEB128Bytes(Id.first);
2565 Asm->EOL("Directory #");
2566 Asm->EmitULEB128Bytes(0);
2567 Asm->EOL("Mod date");
2568 Asm->EmitULEB128Bytes(0);
2569 Asm->EOL("File size");
2570 }
2571
2572 Asm->EmitInt8(0); Asm->EOL("End of files");
2573
2574 EmitLabel("line_prolog_end", 0);
2575
2576 // A sequence for each text section.
2577 unsigned SecSrcLinesSize = SectionSourceLines.size();
2578
2579 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2580 // Isolate current sections line info.
2581 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2582
Chris Lattner93b6db32009-08-08 23:39:42 +00002583 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002584 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002585 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002586 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002587 }*/
2588 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002589
2590 // Dwarf assumes we start with first line of first source file.
2591 unsigned Source = 1;
2592 unsigned Line = 1;
2593
2594 // Construct rows of the address, source, line, column matrix.
2595 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2596 const SrcLineInfo &LineInfo = LineInfos[i];
2597 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2598 if (!LabelID) continue;
2599
Caroline Ticec6f9d622009-09-11 18:25:54 +00002600 if (LineInfo.getLine() == 0) continue;
2601
Bill Wendling94d04b82009-05-20 23:21:38 +00002602 if (!Asm->isVerbose())
2603 Asm->EOL();
2604 else {
2605 std::pair<unsigned, unsigned> SourceID =
2606 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002607 O << '\t' << MAI->getCommentString() << ' '
Dan Gohmanb3b98212009-12-05 02:00:34 +00002608 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling94d04b82009-05-20 23:21:38 +00002609 << getSourceFileName(SourceID.second)
Dan Gohmanb3b98212009-12-05 02:00:34 +00002610 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002611 }
2612
2613 // Define the line address.
2614 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2615 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2616 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2617 EmitReference("label", LabelID); Asm->EOL("Location label");
2618
2619 // If change of source, then switch to the new source.
2620 if (Source != LineInfo.getSourceID()) {
2621 Source = LineInfo.getSourceID();
2622 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2623 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2624 }
2625
2626 // If change of line.
2627 if (Line != LineInfo.getLine()) {
2628 // Determine offset.
2629 int Offset = LineInfo.getLine() - Line;
2630 int Delta = Offset - MinLineDelta;
2631
2632 // Update line.
2633 Line = LineInfo.getLine();
2634
2635 // If delta is small enough and in range...
2636 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2637 // ... then use fast opcode.
2638 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2639 } else {
2640 // ... otherwise use long hand.
2641 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2642 Asm->EOL("DW_LNS_advance_line");
2643 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2644 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2645 }
2646 } else {
2647 // Copy the previous row (different address or source)
2648 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2649 }
2650 }
2651
Devang Patel2c4ceb12009-11-21 02:48:08 +00002652 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002653 }
2654
2655 if (SecSrcLinesSize == 0)
2656 // Because we're emitting a debug_line section, we still need a line
2657 // table. The linker and friends expect it to exist. If there's nothing to
2658 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002659 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002660
2661 EmitLabel("line_end", 0);
2662 Asm->EOL();
2663}
2664
Devang Patel2c4ceb12009-11-21 02:48:08 +00002665/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002666///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002667void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002668 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002669 return;
2670
2671 int stackGrowth =
2672 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2673 TargetFrameInfo::StackGrowsUp ?
2674 TD->getPointerSize() : -TD->getPointerSize();
2675
2676 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002677 Asm->OutStreamer.SwitchSection(
2678 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002679
2680 EmitLabel("debug_frame_common", 0);
2681 EmitDifference("debug_frame_common_end", 0,
2682 "debug_frame_common_begin", 0, true);
2683 Asm->EOL("Length of Common Information Entry");
2684
2685 EmitLabel("debug_frame_common_begin", 0);
2686 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2687 Asm->EOL("CIE Identifier Tag");
2688 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2689 Asm->EOL("CIE Version");
2690 Asm->EmitString("");
2691 Asm->EOL("CIE Augmentation");
2692 Asm->EmitULEB128Bytes(1);
2693 Asm->EOL("CIE Code Alignment Factor");
2694 Asm->EmitSLEB128Bytes(stackGrowth);
2695 Asm->EOL("CIE Data Alignment Factor");
2696 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2697 Asm->EOL("CIE RA Column");
2698
2699 std::vector<MachineMove> Moves;
2700 RI->getInitialFrameState(Moves);
2701
2702 EmitFrameMoves(NULL, 0, Moves, false);
2703
2704 Asm->EmitAlignment(2, 0, 0, false);
2705 EmitLabel("debug_frame_common_end", 0);
2706
2707 Asm->EOL();
2708}
2709
Devang Patel2c4ceb12009-11-21 02:48:08 +00002710/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00002711/// section.
2712void
Devang Patel2c4ceb12009-11-21 02:48:08 +00002713DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002714 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002715 return;
2716
2717 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002718 Asm->OutStreamer.SwitchSection(
2719 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002720
2721 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2722 "debug_frame_begin", DebugFrameInfo.Number, true);
2723 Asm->EOL("Length of Frame Information Entry");
2724
2725 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2726
2727 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2728 0, 0, true, false);
2729 Asm->EOL("FDE CIE offset");
2730
2731 EmitReference("func_begin", DebugFrameInfo.Number);
2732 Asm->EOL("FDE initial location");
2733 EmitDifference("func_end", DebugFrameInfo.Number,
2734 "func_begin", DebugFrameInfo.Number);
2735 Asm->EOL("FDE address range");
2736
2737 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2738 false);
2739
2740 Asm->EmitAlignment(2, 0, 0, false);
2741 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2742
2743 Asm->EOL();
2744}
2745
Devang Patela3a60c62009-12-08 15:31:31 +00002746/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2747///
2748void DwarfDebug::emitDebugPubNames() {
2749 // Start the dwarf pubnames section.
2750 Asm->OutStreamer.SwitchSection(
2751 Asm->getObjFileLowering().getDwarfPubNamesSection());
2752
2753 EmitDifference("pubnames_end", ModuleCU->getID(),
2754 "pubnames_begin", ModuleCU->getID(), true);
Bill Wendling94d04b82009-05-20 23:21:38 +00002755 Asm->EOL("Length of Public Names Info");
2756
Devang Patela3a60c62009-12-08 15:31:31 +00002757 EmitLabel("pubnames_begin", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002758
2759 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2760
2761 EmitSectionOffset("info_begin", "section_info",
Devang Patela3a60c62009-12-08 15:31:31 +00002762 ModuleCU->getID(), 0, true, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00002763 Asm->EOL("Offset of Compilation Unit Info");
2764
Devang Patela3a60c62009-12-08 15:31:31 +00002765 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002766 true);
2767 Asm->EOL("Compilation Unit Length");
2768
Devang Patela3a60c62009-12-08 15:31:31 +00002769 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling94d04b82009-05-20 23:21:38 +00002770 for (StringMap<DIE*>::const_iterator
2771 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2772 const char *Name = GI->getKeyData();
2773 DIE * Entity = GI->second;
2774
2775 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2776 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2777 }
2778
2779 Asm->EmitInt32(0); Asm->EOL("End Mark");
Devang Patela3a60c62009-12-08 15:31:31 +00002780 EmitLabel("pubnames_end", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002781
2782 Asm->EOL();
2783}
2784
Devang Patel193f7202009-11-24 01:14:22 +00002785void DwarfDebug::emitDebugPubTypes() {
Devang Patelf3a03762009-11-24 19:18:41 +00002786 // Start the dwarf pubnames section.
2787 Asm->OutStreamer.SwitchSection(
2788 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patel193f7202009-11-24 01:14:22 +00002789 EmitDifference("pubtypes_end", ModuleCU->getID(),
2790 "pubtypes_begin", ModuleCU->getID(), true);
2791 Asm->EOL("Length of Public Types Info");
2792
2793 EmitLabel("pubtypes_begin", ModuleCU->getID());
2794
2795 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2796
2797 EmitSectionOffset("info_begin", "section_info",
2798 ModuleCU->getID(), 0, true, false);
2799 Asm->EOL("Offset of Compilation ModuleCU Info");
2800
2801 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2802 true);
2803 Asm->EOL("Compilation ModuleCU Length");
2804
2805 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2806 for (StringMap<DIE*>::const_iterator
2807 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2808 const char *Name = GI->getKeyData();
2809 DIE * Entity = GI->second;
2810
2811 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2812 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2813 }
2814
2815 Asm->EmitInt32(0); Asm->EOL("End Mark");
2816 EmitLabel("pubtypes_end", ModuleCU->getID());
2817
2818 Asm->EOL();
2819}
2820
Devang Patel2c4ceb12009-11-21 02:48:08 +00002821/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002822///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002823void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002824 // Check to see if it is worth the effort.
2825 if (!StringPool.empty()) {
2826 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002827 Asm->OutStreamer.SwitchSection(
2828 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002829
2830 // For each of strings in the string pool.
2831 for (unsigned StringID = 1, N = StringPool.size();
2832 StringID <= N; ++StringID) {
2833 // Emit a label for reference from debug information entries.
2834 EmitLabel("string", StringID);
2835
2836 // Emit the string itself.
2837 const std::string &String = StringPool[StringID];
2838 Asm->EmitString(String); Asm->EOL();
2839 }
2840
2841 Asm->EOL();
2842 }
2843}
2844
Devang Patel2c4ceb12009-11-21 02:48:08 +00002845/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002846///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002847void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002848 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002849 Asm->OutStreamer.SwitchSection(
2850 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002851 Asm->EOL();
2852}
2853
2854/// EmitDebugARanges - Emit visible names into a debug aranges section.
2855///
2856void DwarfDebug::EmitDebugARanges() {
2857 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002858 Asm->OutStreamer.SwitchSection(
2859 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002860
2861 // FIXME - Mock up
2862#if 0
2863 CompileUnit *Unit = GetBaseCompileUnit();
2864
2865 // Don't include size of length
2866 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2867
2868 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2869
2870 EmitReference("info_begin", Unit->getID());
2871 Asm->EOL("Offset of Compilation Unit Info");
2872
2873 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2874
2875 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2876
2877 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2878 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2879
2880 // Range 1
2881 EmitReference("text_begin", 0); Asm->EOL("Address");
2882 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2883
2884 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2885 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2886#endif
2887
2888 Asm->EOL();
2889}
2890
Devang Patel2c4ceb12009-11-21 02:48:08 +00002891/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002892///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002893void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002894 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002895 Asm->OutStreamer.SwitchSection(
2896 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002897 Asm->EOL();
2898}
2899
Devang Patel2c4ceb12009-11-21 02:48:08 +00002900/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002901///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002902void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002903 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002904 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002905 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002906 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002907 Asm->EOL();
2908 }
2909}
2910
Devang Patel2c4ceb12009-11-21 02:48:08 +00002911/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002912/// Section Header:
2913/// 1. length of section
2914/// 2. Dwarf version number
2915/// 3. address size.
2916///
2917/// Entries (one "entry" for each function that was inlined):
2918///
2919/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2920/// otherwise offset into __debug_str for regular function name.
2921/// 2. offset into __debug_str section for regular function name.
2922/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2923/// instances for the function.
2924///
2925/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2926/// inlined instance; the die_offset points to the inlined_subroutine die in the
2927/// __debug_info section, and the low_pc is the starting address for the
2928/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002929void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002930 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002931 return;
2932
Devang Patel1dbc7712009-06-29 20:45:18 +00002933 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002934 return;
2935
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002936 Asm->OutStreamer.SwitchSection(
2937 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002938 Asm->EOL();
2939 EmitDifference("debug_inlined_end", 1,
2940 "debug_inlined_begin", 1, true);
2941 Asm->EOL("Length of Debug Inlined Information Entry");
2942
2943 EmitLabel("debug_inlined_begin", 1);
2944
2945 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2946 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2947
Devang Patel53bb5c92009-11-10 23:06:00 +00002948 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2949 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002950
Devang Patel53bb5c92009-11-10 23:06:00 +00002951// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2952 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2953 MDNode *Node = *I;
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002954 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2955 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002956 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002957 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002958 StringRef LName = SP.getLinkageName();
2959 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002960
Devang Patel65dbc902009-11-25 17:36:49 +00002961 if (LName.empty())
Devang Patel53cb17d2009-07-16 01:01:22 +00002962 Asm->EmitString(Name);
2963 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002964 // Skip special LLVM prefix that is used to inform the asm printer to not
2965 // emit usual symbol prefix before the symbol name. This happens for
2966 // Objective-C symbol names and symbol whose name is replaced using GCC's
2967 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002968 if (LName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00002969 LName = LName.substr(1);
Devang Patel53bb5c92009-11-10 23:06:00 +00002970// Asm->EmitString(LName);
2971 EmitSectionOffset("string", "section_str",
2972 StringPool.idFor(LName), false, true);
2973
Devang Patel53cb17d2009-07-16 01:01:22 +00002974 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002975 Asm->EOL("MIPS linkage name");
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002976// Asm->EmitString(Name);
Devang Patel53bb5c92009-11-10 23:06:00 +00002977 EmitSectionOffset("string", "section_str",
2978 StringPool.idFor(Name), false, true);
2979 Asm->EOL("Function name");
Bill Wendling94d04b82009-05-20 23:21:38 +00002980 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2981
Devang Patel53bb5c92009-11-10 23:06:00 +00002982 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002983 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002984 DIE *SP = LI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00002985 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2986
2987 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002988 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002989 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002990 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002991
Devang Patel53bb5c92009-11-10 23:06:00 +00002992 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling94d04b82009-05-20 23:21:38 +00002993 }
2994 }
2995
2996 EmitLabel("debug_inlined_end", 1);
2997 Asm->EOL();
2998}