blob: e16bc2adf2ca4df703eb24f5d07dc3cf50abbb99 [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Devang Patele4b27562009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
Chris Lattner334fd1f2009-09-16 00:08:41 +000028#include "llvm/Support/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/Support/Timer.h"
30#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000031using namespace llvm;
32
33static TimerGroup &getDwarfTimerGroup() {
34 static TimerGroup DwarfTimerGroup("Dwarf Debugging");
35 return DwarfTimerGroup;
36}
37
38//===----------------------------------------------------------------------===//
39
40/// Configuration values for initial hash set sizes (log2).
41///
Bill Wendling0310d762009-05-15 09:23:25 +000042static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000043
44namespace llvm {
45
46//===----------------------------------------------------------------------===//
47/// CompileUnit - This dwarf writer support class manages information associate
48/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000049class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000050 /// ID - File identifier for source.
51 ///
52 unsigned ID;
53
54 /// Die - Compile unit debug information entry.
55 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000056 DIE *CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000057
Devang Patel6f01d9c2009-11-21 00:31:03 +000058 /// IndexTyDie - An anonymous type for index type.
59 DIE *IndexTyDie;
60
Bill Wendling0310d762009-05-15 09:23:25 +000061 /// GVToDieMap - Tracks the mapping of unit level debug informaton
62 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000063 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel017d1212009-11-20 21:37:22 +000064 ValueMap<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000065
66 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
67 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000068 /// FIXME : Rename
Devang Patel017d1212009-11-20 21:37:22 +000069 ValueMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000070
71 /// Globals - A map of globally visible named entities for this unit.
72 ///
73 StringMap<DIE*> Globals;
74
Devang Patel193f7202009-11-24 01:14:22 +000075 /// GlobalTypes - A map of globally visible types for this unit.
76 ///
77 StringMap<DIE*> GlobalTypes;
78
Bill Wendling0310d762009-05-15 09:23:25 +000079public:
80 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +000081 : ID(I), CUDie(D), IndexTyDie(0) {}
82 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendling0310d762009-05-15 09:23:25 +000083
84 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +000085 unsigned getID() const { return ID; }
86 DIE* getCUDie() const { return CUDie; }
87 const StringMap<DIE*> &getGlobals() const { return Globals; }
88 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +000089
90 /// hasContent - Return true if this compile unit has something to write out.
91 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000092 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +000093
Devang Patel2c4ceb12009-11-21 02:48:08 +000094 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +000095 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000096 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000097
Devang Patel193f7202009-11-24 01:14:22 +000098 /// addGlobalType - Add a new global type to the compile unit.
99 ///
100 void addGlobalType(const std::string &Name, DIE *Die) {
101 GlobalTypes[Name] = Die;
102 }
103
Devang Patel017d1212009-11-20 21:37:22 +0000104 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000105 /// specified debug variable.
Devang Patel017d1212009-11-20 21:37:22 +0000106 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000107
Devang Patel017d1212009-11-20 21:37:22 +0000108 /// insertDIE - Insert DIE into the map.
109 void insertDIE(MDNode *N, DIE *D) {
110 GVToDieMap.insert(std::make_pair(N, D));
111 }
Bill Wendling0310d762009-05-15 09:23:25 +0000112
Devang Patel017d1212009-11-20 21:37:22 +0000113 /// getDIEEntry - Returns the debug information entry for the speciefied
114 /// debug variable.
115 DIEEntry *getDIEEntry(MDNode *N) { return GVToDIEEntryMap.lookup(N); }
116
117 /// insertDIEEntry - Insert debug information entry into the map.
118 void insertDIEEntry(MDNode *N, DIEEntry *E) {
119 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000120 }
121
Devang Patel2c4ceb12009-11-21 02:48:08 +0000122 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000123 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000124 void addDie(DIE *Buffer) {
125 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000126 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000127
128 // getIndexTyDie - Get an anonymous type for index type.
129 DIE *getIndexTyDie() {
130 return IndexTyDie;
131 }
132
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000133 // setIndexTyDie - Set D as anonymous type for index which can be reused
134 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000135 void setIndexTyDie(DIE *D) {
136 IndexTyDie = D;
137 }
138
Bill Wendling0310d762009-05-15 09:23:25 +0000139};
140
141//===----------------------------------------------------------------------===//
142/// DbgVariable - This class is used to track local variable information.
143///
Devang Patelf76a3d62009-11-16 21:53:40 +0000144class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000145 DIVariable Var; // Variable Descriptor.
146 unsigned FrameIndex; // Variable frame index.
Devang Patel53bb5c92009-11-10 23:06:00 +0000147 DbgVariable *AbstractVar; // Abstract variable for this variable.
148 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000149public:
Devang Patel53bb5c92009-11-10 23:06:00 +0000150 DbgVariable(DIVariable V, unsigned I)
151 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000152
153 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000154 DIVariable getVariable() const { return Var; }
155 unsigned getFrameIndex() const { return FrameIndex; }
156 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
157 DbgVariable *getAbstractVariable() const { return AbstractVar; }
158 void setDIE(DIE *D) { TheDIE = D; }
159 DIE *getDIE() const { return TheDIE; }
Bill Wendling0310d762009-05-15 09:23:25 +0000160};
161
162//===----------------------------------------------------------------------===//
163/// DbgScope - This class is used to track scope information.
164///
Devang Patelf76a3d62009-11-16 21:53:40 +0000165class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000166 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000167 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel53bb5c92009-11-10 23:06:00 +0000168 WeakVH InlinedAtLocation; // Location at which scope is inlined.
169 bool AbstractScope; // Abstract Scope
Bill Wendling0310d762009-05-15 09:23:25 +0000170 unsigned StartLabelID; // Label ID of the beginning of scope.
171 unsigned EndLabelID; // Label ID of the end of scope.
Devang Pateld38dd112009-10-01 18:25:23 +0000172 const MachineInstr *LastInsn; // Last instruction of this scope.
173 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000174 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
175 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000176
Owen Anderson04c05f72009-06-24 22:53:20 +0000177 // Private state for dump()
178 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000179public:
Devang Patelc90aefe2009-10-14 21:08:09 +0000180 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000181 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000182 StartLabelID(0), EndLabelID(0),
Devang Patelc90aefe2009-10-14 21:08:09 +0000183 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000184 virtual ~DbgScope();
185
186 // Accessors.
187 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000188 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000189 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000190 MDNode *getInlinedAt() const {
Devang Patel53bb5c92009-11-10 23:06:00 +0000191 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Patelc90aefe2009-10-14 21:08:09 +0000192 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000193 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000194 unsigned getStartLabelID() const { return StartLabelID; }
195 unsigned getEndLabelID() const { return EndLabelID; }
196 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
197 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendling0310d762009-05-15 09:23:25 +0000198 void setStartLabelID(unsigned S) { StartLabelID = S; }
199 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Pateld38dd112009-10-01 18:25:23 +0000200 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
201 const MachineInstr *getLastInsn() { return LastInsn; }
202 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000203 void setAbstractScope() { AbstractScope = true; }
204 bool isAbstractScope() const { return AbstractScope; }
Devang Pateld38dd112009-10-01 18:25:23 +0000205 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000206
Devang Patel2c4ceb12009-11-21 02:48:08 +0000207 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000208 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000209 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000210
Devang Patel2c4ceb12009-11-21 02:48:08 +0000211 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000212 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000213 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000214
Devang Patel2c4ceb12009-11-21 02:48:08 +0000215 void fixInstructionMarkers() {
Devang Patelaf9e8472009-10-01 20:31:14 +0000216 assert (getFirstInsn() && "First instruction is missing!");
217 if (getLastInsn())
218 return;
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000219
Devang Patelaf9e8472009-10-01 20:31:14 +0000220 // If a scope does not have an instruction to mark an end then use
221 // the end of last child scope.
222 SmallVector<DbgScope *, 4> &Scopes = getScopes();
223 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
224 DbgScope *L = Scopes.back();
225 if (!L->getLastInsn())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000226 L->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +0000227 setLastInsn(L->getLastInsn());
228 }
229
Bill Wendling0310d762009-05-15 09:23:25 +0000230#ifndef NDEBUG
231 void dump() const;
232#endif
233};
234
235#ifndef NDEBUG
236void DbgScope::dump() const {
Chris Lattnerc281de12009-08-23 00:51:00 +0000237 raw_ostream &err = errs();
238 err.indent(IndentLevel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000239 MDNode *N = Desc.getNode();
240 N->dump();
Chris Lattnerc281de12009-08-23 00:51:00 +0000241 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel53bb5c92009-11-10 23:06:00 +0000242 if (AbstractScope)
243 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000244
245 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000246 if (!Scopes.empty())
247 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000248 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
249 if (Scopes[i] != this)
250 Scopes[i]->dump();
251
252 IndentLevel -= 2;
253}
254#endif
255
Bill Wendling0310d762009-05-15 09:23:25 +0000256DbgScope::~DbgScope() {
257 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
258 delete Scopes[i];
259 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
260 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000261}
262
263} // end llvm namespace
264
Chris Lattneraf76e592009-08-22 20:48:53 +0000265DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000266 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000267 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patel2c4ceb12009-11-21 02:48:08 +0000268 DIEValues(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000269 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel53bb5c92009-11-10 23:06:00 +0000270 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000271 if (TimePassesIsEnabled)
272 DebugTimer = new Timer("Dwarf Debug Writer",
273 getDwarfTimerGroup());
274}
275DwarfDebug::~DwarfDebug() {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000276 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
277 delete DIEValues[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000278
Bill Wendling0310d762009-05-15 09:23:25 +0000279 delete DebugTimer;
280}
281
Devang Patel2c4ceb12009-11-21 02:48:08 +0000282/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000283///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000284void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000285 // Profile the node so that we can make it unique.
286 FoldingSetNodeID ID;
287 Abbrev.Profile(ID);
288
289 // Check the set for priors.
290 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
291
292 // If it's newly added.
293 if (InSet == &Abbrev) {
294 // Add to abbreviation list.
295 Abbreviations.push_back(&Abbrev);
296
297 // Assign the vector position + 1 as its number.
298 Abbrev.setNumber(Abbreviations.size());
299 } else {
300 // Assign existing abbreviation number.
301 Abbrev.setNumber(InSet->getNumber());
302 }
303}
304
Devang Patel2c4ceb12009-11-21 02:48:08 +0000305/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000306/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000307DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000308 DIEEntry *Value = new DIEEntry(Entry);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000309 DIEValues.push_back(Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000310 return Value;
311}
312
Devang Patel2c4ceb12009-11-21 02:48:08 +0000313/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000314///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000315void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000316 unsigned Form, uint64_t Integer) {
317 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000318 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000319 DIEValues.push_back(Value);
320 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000321}
322
Devang Patel2c4ceb12009-11-21 02:48:08 +0000323/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000324///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000325void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000326 unsigned Form, int64_t Integer) {
327 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000328 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000329 DIEValues.push_back(Value);
330 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000331}
332
Devang Patel69f57b12009-12-02 15:25:16 +0000333/// addString - Add a string attribute data and value. DIEString only
334/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000335void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patele9a05972009-11-24 19:42:17 +0000336 const StringRef String) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000337 DIEValue *Value = new DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000338 DIEValues.push_back(Value);
339 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000340}
341
Devang Patel2c4ceb12009-11-21 02:48:08 +0000342/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000343///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000344void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000345 const DWLabel &Label) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000346 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000347 DIEValues.push_back(Value);
348 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000349}
350
Devang Patel2c4ceb12009-11-21 02:48:08 +0000351/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000352///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000353void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000354 const std::string &Label) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000355 DIEValue *Value = new DIEObjectLabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000356 DIEValues.push_back(Value);
357 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000358}
359
Devang Patel2c4ceb12009-11-21 02:48:08 +0000360/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000361///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000362void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000363 const DWLabel &Label, const DWLabel &Section,
364 bool isEH, bool useSet) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000365 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000366 DIEValues.push_back(Value);
367 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000368}
369
Devang Patel2c4ceb12009-11-21 02:48:08 +0000370/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000371///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000372void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000373 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000374 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000375 DIEValues.push_back(Value);
376 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000377}
378
Devang Patel2c4ceb12009-11-21 02:48:08 +0000379/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000380///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000381void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000382 DIEBlock *Block) {
383 Block->ComputeSize(TD);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000384 DIEValues.push_back(Block);
385 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000386}
387
Devang Patel2c4ceb12009-11-21 02:48:08 +0000388/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000389/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000390void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendling0310d762009-05-15 09:23:25 +0000391 // If there is no compile unit specified, don't add a line #.
392 if (V->getCompileUnit().isNull())
393 return;
394
395 unsigned Line = V->getLineNumber();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000396 unsigned FileID = findCompileUnit(V->getCompileUnit()).getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000397 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000398 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
399 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000400}
401
Devang Patel2c4ceb12009-11-21 02:48:08 +0000402/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000403/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000404void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendling0310d762009-05-15 09:23:25 +0000405 // If there is no compile unit specified, don't add a line #.
406 if (G->getCompileUnit().isNull())
407 return;
408
409 unsigned Line = G->getLineNumber();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000410 unsigned FileID = findCompileUnit(G->getCompileUnit()).getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000411 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000412 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
413 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000414}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000415
Devang Patel2c4ceb12009-11-21 02:48:08 +0000416/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000417/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000418void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel82dfc0c2009-08-31 22:47:13 +0000419 // If there is no compile unit specified, don't add a line #.
420 if (SP->getCompileUnit().isNull())
421 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000422 // If the line number is 0, don't add it.
423 if (SP->getLineNumber() == 0)
424 return;
425
Devang Patel82dfc0c2009-08-31 22:47:13 +0000426
427 unsigned Line = SP->getLineNumber();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000428 unsigned FileID = findCompileUnit(SP->getCompileUnit()).getID();
Devang Patel82dfc0c2009-08-31 22:47:13 +0000429 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000430 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
431 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000432}
433
Devang Patel2c4ceb12009-11-21 02:48:08 +0000434/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000435/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000436void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000437 // If there is no compile unit specified, don't add a line #.
438 DICompileUnit CU = Ty->getCompileUnit();
439 if (CU.isNull())
440 return;
441
442 unsigned Line = Ty->getLineNumber();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000443 unsigned FileID = findCompileUnit(CU).getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000444 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000445 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
446 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000447}
448
Caroline Ticedc8f6042009-08-31 21:19:37 +0000449/* Byref variables, in Blocks, are declared by the programmer as
450 "SomeType VarName;", but the compiler creates a
451 __Block_byref_x_VarName struct, and gives the variable VarName
452 either the struct, or a pointer to the struct, as its type. This
453 is necessary for various behind-the-scenes things the compiler
454 needs to do with by-reference variables in blocks.
455
456 However, as far as the original *programmer* is concerned, the
457 variable should still have type 'SomeType', as originally declared.
458
459 The following function dives into the __Block_byref_x_VarName
460 struct to find the original type of the variable. This will be
461 passed back to the code generating the type for the Debug
462 Information Entry for the variable 'VarName'. 'VarName' will then
463 have the original type 'SomeType' in its debug information.
464
465 The original type 'SomeType' will be the type of the field named
466 'VarName' inside the __Block_byref_x_VarName struct.
467
468 NOTE: In order for this to not completely fail on the debugger
469 side, the Debug Information Entry for the variable VarName needs to
470 have a DW_AT_location that tells the debugger how to unwind through
471 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000472 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000473
474/// Find the type the programmer originally declared the variable to be
475/// and return that type.
476///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000477DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000478
479 DIType subType = Ty;
480 unsigned tag = Ty.getTag();
481
482 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000483 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000484 subType = DTy.getTypeDerivedFrom();
485 }
486
487 DICompositeType blockStruct = DICompositeType(subType.getNode());
488
489 DIArray Elements = blockStruct.getTypeArray();
490
491 if (Elements.isNull())
492 return Ty;
493
494 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
495 DIDescriptor Element = Elements.getElement(i);
496 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000497 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000498 return (DT.getTypeDerivedFrom());
499 }
500
501 return Ty;
502}
503
Devang Patel2c4ceb12009-11-21 02:48:08 +0000504/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000505/// and generate the DWARF information necessary to find the actual variable
506/// given the extra address information encoded in the DIVariable, starting from
507/// the starting location. Add the DWARF information to the die.
508///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000509void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000510 unsigned Attribute,
511 const MachineLocation &Location) {
512 const DIVariable &VD = DV->getVariable();
513 DIType Ty = VD.getType();
514
515 // Decode the original location, and use that as the start of the byref
516 // variable's location.
517 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
518 DIEBlock *Block = new DIEBlock();
519
520 if (Location.isReg()) {
521 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000522 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000523 } else {
524 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000525 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
526 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000527 }
528 } else {
529 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000530 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000531 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000532 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
533 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000534 }
535
Devang Patel2c4ceb12009-11-21 02:48:08 +0000536 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000537 }
538
539 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
540 uint64_t Element = VD.getAddrElement(i);
541
542 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000543 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
544 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000545 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000546 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000547 } else llvm_unreachable("unknown DIFactory Opcode");
548 }
549
550 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000551 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000552}
553
Caroline Ticedc8f6042009-08-31 21:19:37 +0000554/* Byref variables, in Blocks, are declared by the programmer as "SomeType
555 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
556 gives the variable VarName either the struct, or a pointer to the struct, as
557 its type. This is necessary for various behind-the-scenes things the
558 compiler needs to do with by-reference variables in Blocks.
559
560 However, as far as the original *programmer* is concerned, the variable
561 should still have type 'SomeType', as originally declared.
562
Devang Patel2c4ceb12009-11-21 02:48:08 +0000563 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000564 struct to find the original type of the variable, which is then assigned to
565 the variable's Debug Information Entry as its real type. So far, so good.
566 However now the debugger will expect the variable VarName to have the type
567 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000568 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000569 pointers and struct to find the actual variable of type SomeType.
570
571 The following function does just that. We start by getting
572 the "normal" location for the variable. This will be the location
573 of either the struct __Block_byref_x_VarName or the pointer to the
574 struct __Block_byref_x_VarName.
575
576 The struct will look something like:
577
578 struct __Block_byref_x_VarName {
579 ... <various fields>
580 struct __Block_byref_x_VarName *forwarding;
581 ... <various other fields>
582 SomeType VarName;
583 ... <maybe more fields>
584 };
585
586 If we are given the struct directly (as our starting point) we
587 need to tell the debugger to:
588
589 1). Add the offset of the forwarding field.
590
591 2). Follow that pointer to get the the real __Block_byref_x_VarName
592 struct to use (the real one may have been copied onto the heap).
593
594 3). Add the offset for the field VarName, to find the actual variable.
595
596 If we started with a pointer to the struct, then we need to
597 dereference that pointer first, before the other steps.
598 Translating this into DWARF ops, we will need to append the following
599 to the current location description for the variable:
600
601 DW_OP_deref -- optional, if we start with a pointer
602 DW_OP_plus_uconst <forward_fld_offset>
603 DW_OP_deref
604 DW_OP_plus_uconst <varName_fld_offset>
605
606 That is what this function does. */
607
Devang Patel2c4ceb12009-11-21 02:48:08 +0000608/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000609/// provided, and generate the DWARF information necessary to find the
610/// actual Block variable (navigating the Block struct) based on the
611/// starting location. Add the DWARF information to the die. For
612/// more information, read large comment just above here.
613///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000614void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000615 unsigned Attribute,
616 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000617 const DIVariable &VD = DV->getVariable();
618 DIType Ty = VD.getType();
619 DIType TmpTy = Ty;
620 unsigned Tag = Ty.getTag();
621 bool isPointer = false;
622
Devang Patel65dbc902009-11-25 17:36:49 +0000623 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000624
625 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000626 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000627 TmpTy = DTy.getTypeDerivedFrom();
628 isPointer = true;
629 }
630
631 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
632
Daniel Dunbar00564992009-09-19 20:40:14 +0000633 // Find the __forwarding field and the variable field in the __Block_byref
634 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000635 DIArray Fields = blockStruct.getTypeArray();
636 DIDescriptor varField = DIDescriptor();
637 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000638
639
Daniel Dunbar00564992009-09-19 20:40:14 +0000640 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
641 DIDescriptor Element = Fields.getElement(i);
642 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000643 StringRef fieldName = DT.getName();
644 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000645 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000646 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000647 varField = Element;
648 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000649
Mike Stump7e3720d2009-09-24 23:21:26 +0000650 assert(!varField.isNull() && "Can't find byref variable in Block struct");
651 assert(!forwardingField.isNull()
652 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000653
Daniel Dunbar00564992009-09-19 20:40:14 +0000654 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000655 unsigned int forwardingFieldOffset =
656 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
657 unsigned int varFieldOffset =
658 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000659
Mike Stump7e3720d2009-09-24 23:21:26 +0000660 // Decode the original location, and use that as the start of the byref
661 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000662 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
663 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000664
Daniel Dunbar00564992009-09-19 20:40:14 +0000665 if (Location.isReg()) {
666 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000667 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000668 else {
669 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000670 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
671 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000672 }
673 } else {
674 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000675 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000676 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000677 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
678 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000679 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000680
Devang Patel2c4ceb12009-11-21 02:48:08 +0000681 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000682 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000683
Mike Stump7e3720d2009-09-24 23:21:26 +0000684 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000685 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000686 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000687 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000688
Daniel Dunbar00564992009-09-19 20:40:14 +0000689 // Next add the offset for the '__forwarding' field:
690 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
691 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000692 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000693 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
694 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000695 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000696
Daniel Dunbar00564992009-09-19 20:40:14 +0000697 // Now dereference the __forwarding field to get to the real __Block_byref
698 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000699 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000700
Daniel Dunbar00564992009-09-19 20:40:14 +0000701 // Now that we've got the real __Block_byref... struct, add the offset
702 // for the variable's field to get to the location of the actual variable:
703 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000704 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000705 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
706 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000707 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000708
Daniel Dunbar00564992009-09-19 20:40:14 +0000709 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000710 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000711}
712
Devang Patel2c4ceb12009-11-21 02:48:08 +0000713/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000714/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000715void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000716 const MachineLocation &Location) {
717 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
718 DIEBlock *Block = new DIEBlock();
719
720 if (Location.isReg()) {
721 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000722 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000723 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000724 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
725 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000726 }
727 } else {
728 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000729 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000730 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000731 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
732 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000733 }
734
Devang Patel2c4ceb12009-11-21 02:48:08 +0000735 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000736 }
737
Devang Patel2c4ceb12009-11-21 02:48:08 +0000738 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000739}
740
Devang Patel2c4ceb12009-11-21 02:48:08 +0000741/// addType - Add a new type attribute to the specified entity.
742void DwarfDebug::addType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000743 if (Ty.isNull())
744 return;
745
746 // Check for pre-existence.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000747 DIEEntry *Entry = DW_Unit->getDIEEntry(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000748
749 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000750 if (Entry) {
751 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000752 return;
753 }
754
755 // Set up proxy.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000756 Entry = createDIEEntry();
757 DW_Unit->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000758
759 // Construct type.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000760 DIE *Buffer = new DIE(dwarf::DW_TAG_base_type);
Devang Patel6ceea332009-08-31 18:49:10 +0000761 if (Ty.isBasicType())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000762 constructTypeDIE(DW_Unit, *Buffer, DIBasicType(Ty.getNode()));
Devang Patel6ceea332009-08-31 18:49:10 +0000763 else if (Ty.isCompositeType())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000764 constructTypeDIE(DW_Unit, *Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000765 else {
Devang Patel6ceea332009-08-31 18:49:10 +0000766 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000767 constructTypeDIE(DW_Unit, *Buffer, DIDerivedType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000768 }
769
770 // Add debug information entry to entity and appropriate context.
771 DIE *Die = NULL;
772 DIDescriptor Context = Ty.getContext();
773 if (!Context.isNull())
Devang Patel017d1212009-11-20 21:37:22 +0000774 Die = DW_Unit->getDIE(Context.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000775
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000776 if (Die)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000777 Die->addChild(Buffer);
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000778 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000779 DW_Unit->addDie(Buffer);
780 Entry->setEntry(Buffer);
781 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Devang Patel5d11eb02009-12-03 19:11:07 +0000782 ModuleCU->insertDIE(Ty.getNode(), Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000783}
784
Devang Patel2c4ceb12009-11-21 02:48:08 +0000785/// constructTypeDIE - Construct basic type die from DIBasicType.
786void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000787 DIBasicType BTy) {
788 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000789 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000790 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000791 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000792 BTy.getEncoding());
793
794 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000795 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000796 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000797 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000798 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000799}
800
Devang Patel2c4ceb12009-11-21 02:48:08 +0000801/// constructTypeDIE - Construct derived type die from DIDerivedType.
802void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000803 DIDerivedType DTy) {
804 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000805 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000806 uint64_t Size = DTy.getSizeInBits() >> 3;
807 unsigned Tag = DTy.getTag();
808
809 // FIXME - Workaround for templates.
810 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
811
812 Buffer.setTag(Tag);
813
814 // Map to main type, void will not have a type.
815 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000816 addType(DW_Unit, &Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000817
818 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000819 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000820 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000821
822 // Add size if non-zero (derived types might be zero-sized.)
823 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000824 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000825
826 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000827 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000828 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000829}
830
Devang Patel2c4ceb12009-11-21 02:48:08 +0000831/// constructTypeDIE - Construct type DIE from DICompositeType.
832void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000833 DICompositeType CTy) {
834 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000835 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000836
837 uint64_t Size = CTy.getSizeInBits() >> 3;
838 unsigned Tag = CTy.getTag();
839 Buffer.setTag(Tag);
840
841 switch (Tag) {
842 case dwarf::DW_TAG_vector_type:
843 case dwarf::DW_TAG_array_type:
Devang Patel2c4ceb12009-11-21 02:48:08 +0000844 constructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000845 break;
846 case dwarf::DW_TAG_enumeration_type: {
847 DIArray Elements = CTy.getTypeArray();
848
849 // Add enumerators to enumeration type.
850 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
851 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000852 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelc5254722009-10-09 17:51:49 +0000853 if (!Enum.isNull()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000854 ElemDie = constructEnumTypeDIE(DW_Unit, &Enum);
855 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +0000856 }
Bill Wendling0310d762009-05-15 09:23:25 +0000857 }
858 }
859 break;
860 case dwarf::DW_TAG_subroutine_type: {
861 // Add return type.
862 DIArray Elements = CTy.getTypeArray();
863 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000864 addType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000865
866 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000867 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000868
869 // Add arguments.
870 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
871 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
872 DIDescriptor Ty = Elements.getElement(i);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000873 addType(DW_Unit, Arg, DIType(Ty.getNode()));
874 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000875 }
876 }
877 break;
878 case dwarf::DW_TAG_structure_type:
879 case dwarf::DW_TAG_union_type:
880 case dwarf::DW_TAG_class_type: {
881 // Add elements to structure type.
882 DIArray Elements = CTy.getTypeArray();
883
884 // A forward struct declared type may not have elements available.
885 if (Elements.isNull())
886 break;
887
888 // Add elements to structure type.
889 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
890 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000891 if (Element.isNull())
892 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000893 DIE *ElemDie = NULL;
894 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000895 ElemDie = createMemberSubprogramDIE(DW_Unit,
896 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000897 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000898 ElemDie = createMemberDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000899 DIDerivedType(Element.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000900 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +0000901 }
902
Devang Patela1ba2692009-08-27 23:51:51 +0000903 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000904 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000905
906 unsigned RLang = CTy.getRunTimeLang();
907 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000908 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +0000909 dwarf::DW_FORM_data1, RLang);
910 break;
911 }
912 default:
913 break;
914 }
915
916 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000917 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000918 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000919
920 if (Tag == dwarf::DW_TAG_enumeration_type ||
921 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
922 // Add size if non-zero (derived types might be zero-sized.)
923 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000924 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000925 else {
926 // Add zero size if it is not a forward declaration.
927 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000928 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000929 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000930 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +0000931 }
932
933 // Add source line info if available.
934 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000935 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000936 }
937}
938
Devang Patel2c4ceb12009-11-21 02:48:08 +0000939/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
940void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +0000941 int64_t L = SR.getLo();
942 int64_t H = SR.getHi();
943 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
944
Devang Patel2c4ceb12009-11-21 02:48:08 +0000945 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +0000946 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000947 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patel6325a532009-08-14 20:59:16 +0000948 if (H)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000949 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000950
Devang Patel2c4ceb12009-11-21 02:48:08 +0000951 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +0000952}
953
Devang Patel2c4ceb12009-11-21 02:48:08 +0000954/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
955void DwarfDebug::constructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000956 DICompositeType *CTy) {
957 Buffer.setTag(dwarf::DW_TAG_array_type);
958 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000959 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000960
961 // Emit derived type.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000962 addType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +0000963 DIArray Elements = CTy->getTypeArray();
964
Devang Patel6f01d9c2009-11-21 00:31:03 +0000965 // Get an anonymous type for index type.
966 DIE *IdxTy = DW_Unit->getIndexTyDie();
967 if (!IdxTy) {
968 // Construct an anonymous type for index type.
969 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000970 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
971 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +0000972 dwarf::DW_ATE_signed);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000973 DW_Unit->addDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000974 DW_Unit->setIndexTyDie(IdxTy);
975 }
Bill Wendling0310d762009-05-15 09:23:25 +0000976
977 // Add subranges to array type.
978 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
979 DIDescriptor Element = Elements.getElement(i);
980 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000981 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000982 }
983}
984
Devang Patel2c4ceb12009-11-21 02:48:08 +0000985/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
986DIE *DwarfDebug::constructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000987 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel65dbc902009-11-25 17:36:49 +0000988 StringRef Name = ETy->getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000989 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000990 int64_t Value = ETy->getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000991 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000992 return Enumerator;
993}
994
Devang Patel2c4ceb12009-11-21 02:48:08 +0000995/// createGlobalVariableDIE - Create new DIE using GV.
996DIE *DwarfDebug::createGlobalVariableDIE(CompileUnit *DW_Unit,
Bill Wendling0310d762009-05-15 09:23:25 +0000997 const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000998 // If the global variable was optmized out then no need to create debug info
999 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001000 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001001 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001002
Bill Wendling0310d762009-05-15 09:23:25 +00001003 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001004 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001005 GV.getDisplayName());
1006
Devang Patel65dbc902009-11-25 17:36:49 +00001007 StringRef LinkageName = GV.getLinkageName();
1008 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001009 // Skip special LLVM prefix that is used to inform the asm printer to not
1010 // emit usual symbol prefix before the symbol name. This happens for
1011 // Objective-C symbol names and symbol whose name is replaced using GCC's
1012 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001013 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001014 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001015 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001016 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001017 }
Devang Patel2c4ceb12009-11-21 02:48:08 +00001018 addType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001019 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001020 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1021 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001022
1023 // Add address.
1024 DIEBlock *Block = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001025 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1026 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patelb71a16d2009-10-05 23:22:08 +00001027 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001028 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patelb71a16d2009-10-05 23:22:08 +00001029
Bill Wendling0310d762009-05-15 09:23:25 +00001030 return GVDie;
1031}
1032
Devang Patel2c4ceb12009-11-21 02:48:08 +00001033/// createMemberDIE - Create new member DIE.
1034DIE *DwarfDebug::createMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
Bill Wendling0310d762009-05-15 09:23:25 +00001035 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001036 StringRef Name = DT.getName();
1037 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001038 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001039
Devang Patel2c4ceb12009-11-21 02:48:08 +00001040 addType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001041
Devang Patel2c4ceb12009-11-21 02:48:08 +00001042 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001043
Devang Patel33db5082009-11-04 22:06:12 +00001044 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001045 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001046
Bill Wendling0310d762009-05-15 09:23:25 +00001047 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001048 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001049
1050 if (Size != FieldSize) {
1051 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001052 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1053 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001054
1055 uint64_t Offset = DT.getOffsetInBits();
1056 uint64_t FieldOffset = Offset;
1057 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1058 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1059 FieldOffset = (HiMark - FieldSize);
1060 Offset -= FieldOffset;
1061
1062 // Maybe we need to work from the other end.
1063 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001064 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001065
Devang Patel33db5082009-11-04 22:06:12 +00001066 // Here WD_AT_data_member_location points to the anonymous
1067 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001068 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001069
1070 } else
1071 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001072 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001073
Devang Patel2c4ceb12009-11-21 02:48:08 +00001074 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001075
1076 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001077 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001078 dwarf::DW_ACCESS_protected);
1079 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001080 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001081 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001082 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1083 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1084 dwarf::DW_ACCESS_public);
1085 if (DT.isVirtual())
1086 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1087 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001088 return MemberDie;
1089}
1090
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001091/// createRawSubprogramDIE - Create new partially incomplete DIE. This is
1092/// a helper routine used by createMemberSubprogramDIE and
1093/// createSubprogramDIE.
1094DIE *DwarfDebug::createRawSubprogramDIE(CompileUnit *DW_Unit,
1095 const DISubprogram &SP) {
1096 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel65dbc902009-11-25 17:36:49 +00001097 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001098
Devang Patel65dbc902009-11-25 17:36:49 +00001099 StringRef LinkageName = SP.getLinkageName();
1100 if (!LinkageName.empty()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001101 // Skip special LLVM prefix that is used to inform the asm printer to not
1102 // emit usual symbol prefix before the symbol name. This happens for
1103 // Objective-C symbol names and symbol whose name is replaced using GCC's
1104 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001105 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001106 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001107 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001108 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001109 }
Devang Patel2c4ceb12009-11-21 02:48:08 +00001110 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001111
Bill Wendling0310d762009-05-15 09:23:25 +00001112 // Add prototyped tag, if C or ObjC.
1113 unsigned Lang = SP.getCompileUnit().getLanguage();
1114 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1115 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001116 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001117
1118 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001119 DICompositeType SPTy = SP.getType();
1120 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001121 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001122
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001123 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1124 addType(DW_Unit, SPDie, SPTy);
1125 else
1126 addType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
1127
Devang Patel5d11eb02009-12-03 19:11:07 +00001128 unsigned VK = SP.getVirtuality();
1129 if (VK) {
1130 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1131 DIEBlock *Block = new DIEBlock();
1132 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1133 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1134 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1135 ContainingTypeMap.insert(std::make_pair(SPDie, WeakVH(SP.getContainingType().getNode())));
1136 }
1137
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001138 return SPDie;
1139}
1140
1141/// createMemberSubprogramDIE - Create new member DIE using SP. This routine
1142/// always returns a die with DW_AT_declaration attribute.
1143DIE *DwarfDebug::createMemberSubprogramDIE(CompileUnit *DW_Unit,
1144 const DISubprogram &SP) {
1145 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1146 if (!SPDie)
1147 SPDie = createSubprogramDIE(DW_Unit, SP);
1148
1149 // If SPDie has DW_AT_declaration then reuse it.
1150 if (!SP.isDefinition())
1151 return SPDie;
1152
1153 // Otherwise create new DIE for the declaration. First push definition
1154 // DIE at the top level.
1155 if (TopLevelDIEs.insert(SPDie))
1156 TopLevelDIEsVector.push_back(SPDie);
1157
1158 SPDie = createRawSubprogramDIE(DW_Unit, SP);
1159
1160 // Add arguments.
1161 DICompositeType SPTy = SP.getType();
1162 DIArray Args = SPTy.getTypeArray();
1163 unsigned SPTag = SPTy.getTag();
1164 if (SPTag == dwarf::DW_TAG_subroutine_type)
1165 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1166 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1167 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1168 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1169 SPDie->addChild(Arg);
1170 }
1171
1172 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1173 return SPDie;
1174}
1175
1176/// createSubprogramDIE - Create new DIE using SP.
1177DIE *DwarfDebug::createSubprogramDIE(CompileUnit *DW_Unit,
1178 const DISubprogram &SP) {
1179 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1180 if (SPDie)
1181 return SPDie;
1182
1183 SPDie = createRawSubprogramDIE(DW_Unit, SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001184
1185 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001186 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001187
1188 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001189 // be handled while processing variables.
1190 DICompositeType SPTy = SP.getType();
1191 DIArray Args = SPTy.getTypeArray();
1192 unsigned SPTag = SPTy.getTag();
1193
Bill Wendling0310d762009-05-15 09:23:25 +00001194 if (SPTag == dwarf::DW_TAG_subroutine_type)
1195 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1196 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001197 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1198 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1199 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001200 }
1201 }
1202
Bill Wendling0310d762009-05-15 09:23:25 +00001203 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel017d1212009-11-20 21:37:22 +00001204 DW_Unit->insertDIE(SP.getNode(), SPDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001205 return SPDie;
1206}
1207
Devang Patel2c4ceb12009-11-21 02:48:08 +00001208/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendling0310d762009-05-15 09:23:25 +00001209///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001210CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const {
Bill Wendling0310d762009-05-15 09:23:25 +00001211 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001212 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001213 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1214 return *I->second;
1215}
1216
Devang Patel2c4ceb12009-11-21 02:48:08 +00001217/// createDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +00001218///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001219DIE *DwarfDebug::createDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001220 // Get the descriptor.
1221 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001222 StringRef Name = VD.getName();
1223 if (Name.empty())
Devang Patel1b845972009-11-03 18:30:27 +00001224 return NULL;
Bill Wendling0310d762009-05-15 09:23:25 +00001225
1226 // Translate tag to proper Dwarf tag. The result variable is dropped for
1227 // now.
1228 unsigned Tag;
1229 switch (VD.getTag()) {
1230 case dwarf::DW_TAG_return_variable:
1231 return NULL;
1232 case dwarf::DW_TAG_arg_variable:
1233 Tag = dwarf::DW_TAG_formal_parameter;
1234 break;
1235 case dwarf::DW_TAG_auto_variable: // fall thru
1236 default:
1237 Tag = dwarf::DW_TAG_variable;
1238 break;
1239 }
1240
1241 // Define variable debug information entry.
1242 DIE *VariableDie = new DIE(Tag);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001243 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001244
1245 // Add source line info if available.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001246 addSourceLine(VariableDie, &VD);
Bill Wendling0310d762009-05-15 09:23:25 +00001247
1248 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001249 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001250 // addresses instead.
Caroline Ticedc8f6042009-08-31 21:19:37 +00001251 if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001252 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Caroline Ticedc8f6042009-08-31 21:19:37 +00001253 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001254 addType(Unit, VariableDie, VD.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001255
1256 // Add variable address.
Devang Patel53bb5c92009-11-10 23:06:00 +00001257 // Variables for abstract instances of inlined functions don't get a
1258 // location.
1259 MachineLocation Location;
Jim Grosbacha2f20b22009-11-22 20:14:00 +00001260 unsigned FrameReg;
1261 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1262 Location.set(FrameReg, Offset);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001263
1264
Devang Patel53bb5c92009-11-10 23:06:00 +00001265 if (VD.hasComplexAddress())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001266 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001267 else if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001268 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001269 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001270 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001271
1272 return VariableDie;
1273}
1274
Devang Patel53bb5c92009-11-10 23:06:00 +00001275/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1276/// Initialize scope and update scope hierarchy.
1277DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1278 MDNode *InlinedAt) {
1279 assert (N && "Invalid Scope encoding!");
1280 assert (MI && "Missing machine instruction!");
1281 bool GetConcreteScope = (MI && InlinedAt);
1282
1283 DbgScope *NScope = NULL;
1284
1285 if (InlinedAt)
1286 NScope = DbgScopeMap.lookup(InlinedAt);
1287 else
1288 NScope = DbgScopeMap.lookup(N);
1289 assert (NScope && "Unable to find working scope!");
1290
1291 if (NScope->getFirstInsn())
1292 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001293
1294 DbgScope *Parent = NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001295 if (GetConcreteScope) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001296 DILocation IL(InlinedAt);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001297 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel53bb5c92009-11-10 23:06:00 +00001298 IL.getOrigLocation().getNode());
1299 assert (Parent && "Unable to find Parent scope!");
1300 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001301 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001302 } else if (DIDescriptor(N).isLexicalBlock()) {
1303 DILexicalBlock DB(N);
1304 if (!DB.getContext().isNull()) {
1305 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1306 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001307 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001308 }
Devang Patelc90aefe2009-10-14 21:08:09 +00001309 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001310
Devang Patelbdf45cb2009-10-27 20:47:17 +00001311 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001312
Devang Patel53bb5c92009-11-10 23:06:00 +00001313 if (!Parent && !InlinedAt) {
Devang Patel39ae3ff2009-11-11 00:31:36 +00001314 StringRef SPName = DISubprogram(N).getLinkageName();
1315 if (SPName == MF->getFunction()->getName())
1316 CurrentFnDbgScope = NScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001317 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001318
Devang Patel53bb5c92009-11-10 23:06:00 +00001319 if (GetConcreteScope) {
1320 ConcreteScopes[InlinedAt] = NScope;
1321 getOrCreateAbstractScope(N);
1322 }
1323
Devang Patelbdf45cb2009-10-27 20:47:17 +00001324 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001325}
1326
Devang Patel53bb5c92009-11-10 23:06:00 +00001327DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1328 assert (N && "Invalid Scope encoding!");
1329
1330 DbgScope *AScope = AbstractScopes.lookup(N);
1331 if (AScope)
1332 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001333
Devang Patel53bb5c92009-11-10 23:06:00 +00001334 DbgScope *Parent = NULL;
1335
1336 DIDescriptor Scope(N);
1337 if (Scope.isLexicalBlock()) {
1338 DILexicalBlock DB(N);
1339 DIDescriptor ParentDesc = DB.getContext();
1340 if (!ParentDesc.isNull())
1341 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1342 }
1343
1344 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1345
1346 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001347 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001348 AScope->setAbstractScope();
1349 AbstractScopes[N] = AScope;
1350 if (DIDescriptor(N).isSubprogram())
1351 AbstractScopesList.push_back(AScope);
1352 return AScope;
1353}
Devang Patelaf9e8472009-10-01 20:31:14 +00001354
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001355/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001356/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1357/// If there are global variables in this scope then create and insert
1358/// DIEs for these variables.
1359DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001360
Devang Patel017d1212009-11-20 21:37:22 +00001361 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel53bb5c92009-11-10 23:06:00 +00001362 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001363 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001364 DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001365 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001366 DWLabel("func_end", SubprogramCount));
1367 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001368 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001369
Devang Patel53bb5c92009-11-10 23:06:00 +00001370 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001371 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001372
1373 // If there are global variables at this scope then add their dies.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001374 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00001375 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1376 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1377 if (!N) continue;
1378 DIGlobalVariable GV(N);
1379 if (GV.getContext().getNode() == SPNode) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001380 DIE *ScopedGVDie = createGlobalVariableDIE(ModuleCU, GV);
Devang Patelfb0ee432009-11-10 23:20:04 +00001381 if (ScopedGVDie)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001382 SPDie->addChild(ScopedGVDie);
Devang Patel53bb5c92009-11-10 23:06:00 +00001383 }
1384 }
Devang Patel193f7202009-11-24 01:14:22 +00001385
Devang Patel53bb5c92009-11-10 23:06:00 +00001386 return SPDie;
1387}
1388
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001389/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001390/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1391DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001392 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1393 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1394
1395 // Ignore empty scopes.
1396 if (StartID == EndID && StartID != 0)
1397 return NULL;
1398
1399 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1400 if (Scope->isAbstractScope())
1401 return ScopeDIE;
1402
Devang Patel2c4ceb12009-11-21 02:48:08 +00001403 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001404 StartID ?
1405 DWLabel("label", StartID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001406 : DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001407 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001408 EndID ?
1409 DWLabel("label", EndID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001410 : DWLabel("func_end", SubprogramCount));
1411
1412
1413
1414 return ScopeDIE;
1415}
1416
Devang Patel2c4ceb12009-11-21 02:48:08 +00001417/// constructInlinedScopeDIE - This scope represents inlined body of
1418/// a function. Construct DIE to represent this concrete inlined copy
1419/// of the function.
1420DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001421 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1422 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1423 assert (StartID && "Invalid starting label for an inlined scope!");
1424 assert (EndID && "Invalid end label for an inlined scope!");
1425 // Ignore empty scopes.
1426 if (StartID == EndID && StartID != 0)
1427 return NULL;
1428
1429 DIScope DS(Scope->getScopeNode());
1430 if (DS.isNull())
1431 return NULL;
1432 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1433
1434 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001435 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001436 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001437 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001438 dwarf::DW_FORM_ref4, OriginDIE);
1439
Devang Patel2c4ceb12009-11-21 02:48:08 +00001440 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001441 DWLabel("label", StartID));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001442 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001443 DWLabel("label", EndID));
1444
1445 InlinedSubprogramDIEs.insert(OriginDIE);
1446
1447 // Track the start label for this inlined function.
1448 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1449 I = InlineInfo.find(InlinedSP.getNode());
1450
1451 if (I == InlineInfo.end()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001452 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1453 ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001454 InlinedSPNodes.push_back(InlinedSP.getNode());
1455 } else
1456 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1457
1458 StringPool.insert(InlinedSP.getName());
1459 StringPool.insert(InlinedSP.getLinkageName());
1460 DILocation DL(Scope->getInlinedAt());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001461 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1462 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001463
1464 return ScopeDIE;
1465}
1466
Devang Patel2c4ceb12009-11-21 02:48:08 +00001467
1468/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001469DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV,
Devang Patel53bb5c92009-11-10 23:06:00 +00001470 DbgScope *Scope, CompileUnit *Unit) {
1471 // Get the descriptor.
1472 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001473 StringRef Name = VD.getName();
1474 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001475 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001476
1477 // Translate tag to proper Dwarf tag. The result variable is dropped for
1478 // now.
1479 unsigned Tag;
1480 switch (VD.getTag()) {
1481 case dwarf::DW_TAG_return_variable:
1482 return NULL;
1483 case dwarf::DW_TAG_arg_variable:
1484 Tag = dwarf::DW_TAG_formal_parameter;
1485 break;
1486 case dwarf::DW_TAG_auto_variable: // fall thru
1487 default:
1488 Tag = dwarf::DW_TAG_variable;
1489 break;
1490 }
1491
1492 // Define variable debug information entry.
1493 DIE *VariableDie = new DIE(Tag);
1494
1495
1496 DIE *AbsDIE = NULL;
1497 if (DbgVariable *AV = DV->getAbstractVariable())
1498 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001499
Devang Patel53bb5c92009-11-10 23:06:00 +00001500 if (AbsDIE) {
1501 DIScope DS(Scope->getScopeNode());
1502 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001503 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc0326792009-11-11 03:09:50 +00001504 (void) OriginSPDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001505 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1506 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1507 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001508 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001509 dwarf::DW_FORM_ref4, AbsDIE);
1510 }
1511 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001512 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1513 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001514
1515 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001516 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001517 // addresses instead.
1518 if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001519 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001520 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001521 addType(Unit, VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001522 }
1523
1524 // Add variable address.
1525 if (!Scope->isAbstractScope()) {
1526 MachineLocation Location;
Jim Grosbacha2f20b22009-11-22 20:14:00 +00001527 unsigned FrameReg;
1528 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1529 Location.set(FrameReg, Offset);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001530
Devang Patel53bb5c92009-11-10 23:06:00 +00001531 if (VD.hasComplexAddress())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001532 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001533 else if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001534 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001535 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001536 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001537 }
1538 DV->setDIE(VariableDie);
1539 return VariableDie;
1540
1541}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001542
Devang Patel193f7202009-11-24 01:14:22 +00001543void DwarfDebug::addPubTypes(DISubprogram SP) {
1544 DICompositeType SPTy = SP.getType();
1545 unsigned SPTag = SPTy.getTag();
1546 if (SPTag != dwarf::DW_TAG_subroutine_type)
1547 return;
1548
1549 DIArray Args = SPTy.getTypeArray();
1550 if (Args.isNull())
1551 return;
1552
1553 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1554 DIType ATy(Args.getElement(i).getNode());
1555 if (ATy.isNull())
1556 continue;
1557 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel65dbc902009-11-25 17:36:49 +00001558 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001559 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1560 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1561 }
1562 }
1563}
1564
Devang Patel2c4ceb12009-11-21 02:48:08 +00001565/// constructScopeDIE - Construct a DIE for this scope.
1566DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001567 if (!Scope)
1568 return NULL;
1569 DIScope DS(Scope->getScopeNode());
1570 if (DS.isNull())
1571 return NULL;
1572
1573 DIE *ScopeDIE = NULL;
1574 if (Scope->getInlinedAt())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001575 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001576 else if (DS.isSubprogram()) {
1577 if (Scope->isAbstractScope())
Devang Patel017d1212009-11-20 21:37:22 +00001578 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001579 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001580 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001581 }
1582 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001583 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001584 if (!ScopeDIE) return NULL;
1585 }
1586
1587 // Add variables to scope.
1588 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1589 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001590 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope, ModuleCU);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001591 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001592 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001593 }
1594
1595 // Add nested scopes.
1596 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1597 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1598 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001599 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001600 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001601 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001602 }
Devang Patel193f7202009-11-24 01:14:22 +00001603
1604 if (DS.isSubprogram())
1605 addPubTypes(DISubprogram(DS.getNode()));
1606
1607 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001608}
1609
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001610/// GetOrCreateSourceID - Look up the source id with the given directory and
1611/// source file names. If none currently exists, create a new id and insert it
1612/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1613/// maps as well.
Devang Patel65dbc902009-11-25 17:36:49 +00001614unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001615 unsigned DId;
1616 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1617 if (DI != DirectoryIdMap.end()) {
1618 DId = DI->getValue();
1619 } else {
1620 DId = DirectoryNames.size() + 1;
1621 DirectoryIdMap[DirName] = DId;
1622 DirectoryNames.push_back(DirName);
1623 }
1624
1625 unsigned FId;
1626 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1627 if (FI != SourceFileIdMap.end()) {
1628 FId = FI->getValue();
1629 } else {
1630 FId = SourceFileNames.size() + 1;
1631 SourceFileIdMap[FileName] = FId;
1632 SourceFileNames.push_back(FileName);
1633 }
1634
1635 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1636 SourceIdMap.find(std::make_pair(DId, FId));
1637 if (SI != SourceIdMap.end())
1638 return SI->second;
1639
1640 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1641 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1642 SourceIds.push_back(std::make_pair(DId, FId));
1643
1644 return SrcId;
1645}
1646
Devang Patel2c4ceb12009-11-21 02:48:08 +00001647void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001648 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001649 StringRef FN = DIUnit.getFilename();
1650 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001651 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001652
1653 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001654 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001655 DWLabel("section_line", 0), DWLabel("section_line", 0),
1656 false);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001657 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001658 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001659 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001660 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001661 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001662
Devang Patel65dbc902009-11-25 17:36:49 +00001663 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001664 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001665 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001666 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001667
Devang Patel65dbc902009-11-25 17:36:49 +00001668 StringRef Flags = DIUnit.getFlags();
1669 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001670 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001671
1672 unsigned RVer = DIUnit.getRunTimeVersion();
1673 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001674 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001675 dwarf::DW_FORM_data1, RVer);
1676
1677 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001678 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001679 // Use first compile unit marked as isMain as the compile unit
1680 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001681 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001682 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001683
Devang Patele4b27562009-08-28 23:24:31 +00001684 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001685 CompileUnits.push_back(Unit);
1686}
1687
Devang Patel2c4ceb12009-11-21 02:48:08 +00001688void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001689 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001690
Devang Patel905cf5e2009-09-04 23:59:07 +00001691 // If debug information is malformed then ignore it.
1692 if (DI_GV.Verify() == false)
1693 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001694
1695 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001696 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel13e16b62009-06-26 01:49:18 +00001697 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001698
Devang Patel2c4ceb12009-11-21 02:48:08 +00001699 DIE *VariableDie = createGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001700
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001701 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001702 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001703
1704 // Add to context owner.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001705 if (TopLevelDIEs.insert(VariableDie))
1706 TopLevelDIEsVector.push_back(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001707
1708 // Expose as global. FIXME - need to check external flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001709 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001710
1711 DIType GTy = DI_GV.getType();
Devang Patel65dbc902009-11-25 17:36:49 +00001712 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001713 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1714 assert (Entry && "Missing global type!");
1715 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1716 }
Devang Patel13e16b62009-06-26 01:49:18 +00001717 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001718}
1719
Devang Patel2c4ceb12009-11-21 02:48:08 +00001720void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001721 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001722
1723 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001724 if (ModuleCU->getDIE(N))
Devang Patel13e16b62009-06-26 01:49:18 +00001725 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001726
1727 if (!SP.isDefinition())
1728 // This is a method declaration which will be handled while constructing
1729 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001730 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001731
Devang Patel2c4ceb12009-11-21 02:48:08 +00001732 DIE *SubprogramDie = createSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001733
1734 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001735 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001736
1737 // Add to context owner.
Devang Patel7d0750a2009-12-01 23:07:59 +00001738 if (SP.getContext().getNode() == SP.getCompileUnit().getNode())
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001739 if (TopLevelDIEs.insert(SubprogramDie))
1740 TopLevelDIEsVector.push_back(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001741
1742 // Expose as global.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001743 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001744
Devang Patel13e16b62009-06-26 01:49:18 +00001745 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001746}
1747
Devang Patel2c4ceb12009-11-21 02:48:08 +00001748/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001749/// content. Create global DIEs and emit initial debug info sections.
1750/// This is inovked by the target AsmPrinter.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001751void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel208622d2009-06-25 22:36:02 +00001752 this->M = M;
1753
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001754 if (TimePassesIsEnabled)
1755 DebugTimer->startTimer();
1756
Devang Patel3380cc52009-11-11 19:55:08 +00001757 if (!MAI->doesSupportDebugInformation())
1758 return;
1759
Devang Patel78ab9e22009-07-30 18:56:46 +00001760 DebugInfoFinder DbgFinder;
1761 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001762
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001763 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001764 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1765 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001766 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001767
1768 if (CompileUnits.empty()) {
1769 if (TimePassesIsEnabled)
1770 DebugTimer->stopTimer();
1771
1772 return;
1773 }
1774
Devang Patel70f44262009-06-29 20:38:13 +00001775 // If main compile unit for this module is not seen than randomly
1776 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001777 if (!ModuleCU)
1778 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001779
Devang Patel13e16b62009-06-26 01:49:18 +00001780 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001781 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patelfd07cf52009-10-05 23:40:42 +00001782 E = DbgFinder.global_variable_end(); I != E; ++I) {
1783 DIGlobalVariable GV(*I);
1784 if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
1785 ScopedGVs.push_back(*I);
1786 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001787 constructGlobalVariableDIE(*I);
Devang Patelfd07cf52009-10-05 23:40:42 +00001788 }
Devang Patel13e16b62009-06-26 01:49:18 +00001789
Devang Patel53bb5c92009-11-10 23:06:00 +00001790 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001791 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1792 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001793 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001794
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001795 MMI = mmi;
1796 shouldEmit = true;
1797 MMI->setDebugInfoAvailability(true);
1798
1799 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001800 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001801
1802 // Print out .file directives to specify files for .loc directives. These are
1803 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001804 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001805 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1806 // Remember source id starts at 1.
1807 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1808 sys::Path FullPath(getSourceDirectoryName(Id.first));
1809 bool AppendOk =
1810 FullPath.appendComponent(getSourceFileName(Id.second));
1811 assert(AppendOk && "Could not append filename to directory!");
1812 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001813 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001814 Asm->EOL();
1815 }
1816 }
1817
1818 // Emit initial sections
Devang Patel2c4ceb12009-11-21 02:48:08 +00001819 emitInitial();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001820
1821 if (TimePassesIsEnabled)
1822 DebugTimer->stopTimer();
1823}
1824
Devang Patel2c4ceb12009-11-21 02:48:08 +00001825/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001826///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001827void DwarfDebug::endModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001828 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001829 return;
1830
1831 if (TimePassesIsEnabled)
1832 DebugTimer->startTimer();
1833
Devang Patel53bb5c92009-11-10 23:06:00 +00001834 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1835 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1836 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1837 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001838 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001839 }
1840
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001841 // Insert top level DIEs.
1842 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1843 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1844 ModuleCU->getCUDie()->addChild(*TI);
1845
Devang Patel5d11eb02009-12-03 19:11:07 +00001846 for (DenseMap<DIE *, WeakVH>::iterator CI = ContainingTypeMap.begin(),
1847 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1848 DIE *SPDie = CI->first;
1849 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1850 if (!N) continue;
1851 DIE *NDie = ModuleCU->getDIE(N);
1852 if (!NDie) continue;
1853 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1854 addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1855 }
1856
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001857 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001858 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001859 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001860 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001861 EmitLabel("data_end", 0);
1862
1863 // End text sections.
1864 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001865 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001866 EmitLabel("section_end", i);
1867 }
1868
1869 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001870 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001871
1872 // Emit function debug frame information
1873 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1874 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001875 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001876
1877 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001878 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001879
1880 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001881 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001882
1883 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001884 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001885
1886 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001887 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001888
1889 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001890 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001891
Devang Patel193f7202009-11-24 01:14:22 +00001892 // Emit info into a debug pubtypes section.
1893 emitDebugPubTypes();
1894
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001895 // Emit info into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001896 emitDebugStr();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001897
1898 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001899 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001900
1901 // Emit info into a debug aranges section.
1902 EmitDebugARanges();
1903
1904 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001905 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001906
1907 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001908 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001909
1910 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001911 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001912
1913 if (TimePassesIsEnabled)
1914 DebugTimer->stopTimer();
1915}
1916
Devang Patel53bb5c92009-11-10 23:06:00 +00001917/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001918DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1919 unsigned FrameIdx,
Devang Patel53bb5c92009-11-10 23:06:00 +00001920 DILocation &ScopeLoc) {
1921
1922 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1923 if (AbsDbgVariable)
1924 return AbsDbgVariable;
1925
1926 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1927 if (!Scope)
1928 return NULL;
1929
1930 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001931 Scope->addVariable(AbsDbgVariable);
Devang Patel53bb5c92009-11-10 23:06:00 +00001932 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1933 return AbsDbgVariable;
1934}
1935
Devang Patel2c4ceb12009-11-21 02:48:08 +00001936/// collectVariableInfo - Populate DbgScope entries with variables' info.
1937void DwarfDebug::collectVariableInfo() {
Devang Patelac1ceb32009-10-09 22:42:28 +00001938 if (!MMI) return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001939
Devang Patele717faa2009-10-06 01:26:37 +00001940 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1941 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1942 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac1ceb32009-10-09 22:42:28 +00001943 MetadataBase *MB = VI->first;
1944 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel53bb5c92009-11-10 23:06:00 +00001945 if (!Var) continue;
Devang Pateleda31212009-10-08 18:48:03 +00001946 DIVariable DV (Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001947 std::pair< unsigned, MDNode *> VP = VI->second;
1948 DILocation ScopeLoc(VP.second);
1949
1950 DbgScope *Scope =
1951 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1952 if (!Scope)
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001953 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelfb0ee432009-11-10 23:20:04 +00001954 // If variable scope is not found then skip this variable.
1955 if (!Scope)
1956 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001957
1958 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001959 Scope->addVariable(RegVar);
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001960 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1961 ScopeLoc))
Devang Patel53bb5c92009-11-10 23:06:00 +00001962 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patele717faa2009-10-06 01:26:37 +00001963 }
1964}
1965
Devang Patel2c4ceb12009-11-21 02:48:08 +00001966/// beginScope - Process beginning of a scope starting at Label.
1967void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001968 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1969 if (I == DbgScopeBeginMap.end())
1970 return;
Dan Gohman277207e2009-11-23 21:30:55 +00001971 ScopeVector &SD = I->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001972 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001973 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001974 (*SDI)->setStartLabelID(Label);
1975}
1976
Devang Patel2c4ceb12009-11-21 02:48:08 +00001977/// endScope - Process end of a scope.
1978void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001979 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00001980 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00001981 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001982
1983 unsigned Label = MMI->NextLabelID();
1984 Asm->printLabel(Label);
1985
Devang Patel0d20ac82009-10-06 01:50:42 +00001986 SmallVector<DbgScope *, 2> &SD = I->second;
1987 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001988 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001989 (*SDI)->setEndLabelID(Label);
Devang Patel53bb5c92009-11-10 23:06:00 +00001990 return;
1991}
1992
1993/// createDbgScope - Create DbgScope for the scope.
1994void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1995
1996 if (!InlinedAt) {
1997 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1998 if (WScope)
1999 return;
2000 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2001 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002002 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel2f105c62009-11-11 00:18:40 +00002003 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel53bb5c92009-11-10 23:06:00 +00002004 return;
2005 }
2006
2007 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2008 if (WScope)
2009 return;
2010
2011 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2012 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2013 DILocation DL(InlinedAt);
2014 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel0d20ac82009-10-06 01:50:42 +00002015}
2016
Devang Patel2c4ceb12009-11-21 02:48:08 +00002017/// extractScopeInformation - Scan machine instructions in this function
Devang Patelaf9e8472009-10-01 20:31:14 +00002018/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002019bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patelaf9e8472009-10-01 20:31:14 +00002020 // If scope information was extracted using .dbg intrinsics then there is not
2021 // any need to extract these information by scanning each instruction.
2022 if (!DbgScopeMap.empty())
2023 return false;
2024
Devang Patel53bb5c92009-11-10 23:06:00 +00002025 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelaf9e8472009-10-01 20:31:14 +00002026 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2027 I != E; ++I) {
2028 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2029 II != IE; ++II) {
2030 const MachineInstr *MInsn = II;
2031 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00002032 if (DL.isUnknown()) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002033 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel53bb5c92009-11-10 23:06:00 +00002034 if (!DLT.Scope) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002035 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002036 // other scopes, create one DbgScope now. This will be translated
Devang Patelaf9e8472009-10-01 20:31:14 +00002037 // into a scope DIE at the end.
Devang Patel53bb5c92009-11-10 23:06:00 +00002038 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2039 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
2040 }
2041 }
2042
2043
2044 // Build scope hierarchy using working set of scopes.
2045 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2046 I != E; ++I) {
2047 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2048 II != IE; ++II) {
2049 const MachineInstr *MInsn = II;
2050 DebugLoc DL = MInsn->getDebugLoc();
2051 if (DL.isUnknown()) continue;
2052 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
2053 if (!DLT.Scope) continue;
2054 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002055 // other scopes, create one DbgScope now. This will be translated
Devang Patel53bb5c92009-11-10 23:06:00 +00002056 // into a scope DIE at the end.
2057 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2058 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
2059 Scope->setLastInsn(MInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +00002060 }
2061 }
2062
2063 // If a scope's last instruction is not set then use its child scope's
2064 // last instruction as this scope's last instrunction.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002065 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002066 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002067 if (DI->second->isAbstractScope())
2068 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002069 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00002070 DI->second->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +00002071 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2072 }
2073
2074 // Each scope has first instruction and last instruction to mark beginning
2075 // and end of a scope respectively. Create an inverse map that list scopes
2076 // starts (and ends) with an instruction. One instruction may start (or end)
2077 // multiple scopes.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002078 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002079 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2080 DbgScope *S = DI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002081 if (S->isAbstractScope())
2082 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002083 const MachineInstr *MI = S->getFirstInsn();
2084 assert (MI && "DbgScope does not have first instruction!");
2085
2086 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2087 if (IDI != DbgScopeBeginMap.end())
2088 IDI->second.push_back(S);
2089 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002090 DbgScopeBeginMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002091
2092 MI = S->getLastInsn();
2093 assert (MI && "DbgScope does not have last instruction!");
2094 IDI = DbgScopeEndMap.find(MI);
2095 if (IDI != DbgScopeEndMap.end())
2096 IDI->second.push_back(S);
2097 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002098 DbgScopeEndMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002099 }
2100
2101 return !DbgScopeMap.empty();
2102}
2103
Devang Patel2c4ceb12009-11-21 02:48:08 +00002104/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002105/// emitted immediately after the function entry point.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002106void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002107 this->MF = MF;
2108
2109 if (!ShouldEmitDwarfDebug()) return;
2110
2111 if (TimePassesIsEnabled)
2112 DebugTimer->startTimer();
2113
Devang Patel2c4ceb12009-11-21 02:48:08 +00002114 if (!extractScopeInformation(MF))
Devang Patel60b35bd2009-10-06 18:37:31 +00002115 return;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002116
2117 collectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002118
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002119 // Begin accumulating function debug information.
2120 MMI->BeginFunction(MF);
2121
2122 // Assumes in correct section after the entry point.
2123 EmitLabel("func_begin", ++SubprogramCount);
2124
2125 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2126 // function.
Devang Patelac1ceb32009-10-09 22:42:28 +00002127 DebugLoc FDL = MF->getDefaultDebugLoc();
2128 if (!FDL.isUnknown()) {
2129 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2130 unsigned LabelID = 0;
Devang Patel1619dc32009-10-13 23:28:53 +00002131 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002132 if (!SP.isNull())
Devang Patel2c4ceb12009-11-21 02:48:08 +00002133 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002134 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00002135 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002136 Asm->printLabel(LabelID);
2137 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002138 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002139 if (TimePassesIsEnabled)
2140 DebugTimer->stopTimer();
2141}
2142
Devang Patel2c4ceb12009-11-21 02:48:08 +00002143/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002144///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002145void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002146 if (!ShouldEmitDwarfDebug()) return;
2147
2148 if (TimePassesIsEnabled)
2149 DebugTimer->startTimer();
2150
Devang Patelac1ceb32009-10-09 22:42:28 +00002151 if (DbgScopeMap.empty())
2152 return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002153
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002154 // Define end label for subprogram.
2155 EmitLabel("func_end", SubprogramCount);
2156
2157 // Get function line info.
2158 if (!Lines.empty()) {
2159 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00002160 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002161 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2162 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2163 // Append the function info to section info.
2164 SectionLineInfos.insert(SectionLineInfos.end(),
2165 Lines.begin(), Lines.end());
2166 }
2167
Devang Patel53bb5c92009-11-10 23:06:00 +00002168 // Construct abstract scopes.
2169 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002170 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002171 constructScopeDIE(*AI);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002172
Devang Patel2c4ceb12009-11-21 02:48:08 +00002173 constructScopeDIE(CurrentFnDbgScope);
Devang Patel70d75ca2009-11-12 19:02:56 +00002174
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002175 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2176 MMI->getFrameMoves()));
2177
2178 // Clear debug info
Devang Patelc09ddc12009-12-01 18:13:48 +00002179 CurrentFnDbgScope = NULL;
2180 DbgScopeMap.clear();
2181 DbgScopeBeginMap.clear();
2182 DbgScopeEndMap.clear();
2183 ConcreteScopes.clear();
2184 AbstractScopesList.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002185
2186 Lines.clear();
Devang Patelc09ddc12009-12-01 18:13:48 +00002187
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002188 if (TimePassesIsEnabled)
2189 DebugTimer->stopTimer();
2190}
2191
Devang Patel2c4ceb12009-11-21 02:48:08 +00002192/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002193/// label. Returns a unique label ID used to generate a label and provide
2194/// correspondence to the source line list.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002195unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002196 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002197 if (!MMI)
2198 return 0;
2199
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002200 if (TimePassesIsEnabled)
2201 DebugTimer->startTimer();
2202
Devang Patel65dbc902009-11-25 17:36:49 +00002203 StringRef Dir;
2204 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002205
2206 DIDescriptor Scope(S);
2207 if (Scope.isCompileUnit()) {
2208 DICompileUnit CU(S);
2209 Dir = CU.getDirectory();
2210 Fn = CU.getFilename();
2211 } else if (Scope.isSubprogram()) {
2212 DISubprogram SP(S);
2213 Dir = SP.getDirectory();
2214 Fn = SP.getFilename();
2215 } else if (Scope.isLexicalBlock()) {
2216 DILexicalBlock DB(S);
2217 Dir = DB.getDirectory();
2218 Fn = DB.getFilename();
2219 } else
2220 assert (0 && "Unexpected scope info");
2221
2222 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002223 unsigned ID = MMI->NextLabelID();
2224 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2225
2226 if (TimePassesIsEnabled)
2227 DebugTimer->stopTimer();
2228
2229 return ID;
2230}
2231
2232/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2233/// timed. Look up the source id with the given directory and source file
2234/// names. If none currently exists, create a new id and insert it in the
2235/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2236/// well.
2237unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2238 const std::string &FileName) {
2239 if (TimePassesIsEnabled)
2240 DebugTimer->startTimer();
2241
Devang Patel5ccdd102009-09-29 18:40:58 +00002242 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002243
2244 if (TimePassesIsEnabled)
2245 DebugTimer->stopTimer();
2246
2247 return SrcId;
2248}
2249
Bill Wendling829e67b2009-05-20 23:22:40 +00002250//===----------------------------------------------------------------------===//
2251// Emit Methods
2252//===----------------------------------------------------------------------===//
2253
Devang Patel2c4ceb12009-11-21 02:48:08 +00002254/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002255///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002256unsigned
2257DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002258 // Get the children.
2259 const std::vector<DIE *> &Children = Die->getChildren();
2260
2261 // If not last sibling and has children then add sibling offset attribute.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002262 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002263
2264 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002265 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002266
2267 // Get the abbreviation for this DIE.
2268 unsigned AbbrevNumber = Die->getAbbrevNumber();
2269 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2270
2271 // Set DIE offset
2272 Die->setOffset(Offset);
2273
2274 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002275 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002276
2277 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2278 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2279
2280 // Size the DIE attribute values.
2281 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2282 // Size attribute value.
2283 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2284
2285 // Size the DIE children if any.
2286 if (!Children.empty()) {
2287 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2288 "Children flag not set");
2289
2290 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002291 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002292
2293 // End of children marker.
2294 Offset += sizeof(int8_t);
2295 }
2296
2297 Die->setSize(Offset - Die->getOffset());
2298 return Offset;
2299}
2300
Devang Patel2c4ceb12009-11-21 02:48:08 +00002301/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002302///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002303void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002304 // Compute size of compile unit header.
2305 static unsigned Offset =
2306 sizeof(int32_t) + // Length of Compilation Unit Info
2307 sizeof(int16_t) + // DWARF version number
2308 sizeof(int32_t) + // Offset Into Abbrev. Section
2309 sizeof(int8_t); // Pointer Size (in bytes)
2310
Devang Patel2c4ceb12009-11-21 02:48:08 +00002311 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel1dbc7712009-06-29 20:45:18 +00002312 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002313}
2314
Devang Patel2c4ceb12009-11-21 02:48:08 +00002315/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling94d04b82009-05-20 23:21:38 +00002316/// tools to recognize the object file contains Dwarf information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002317void DwarfDebug::emitInitial() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002318 // Check to see if we already emitted intial headers.
2319 if (didInitial) return;
2320 didInitial = true;
2321
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002322 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002323
Bill Wendling94d04b82009-05-20 23:21:38 +00002324 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002325 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002326 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002327 EmitLabel("section_debug_frame", 0);
2328 }
2329
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002330 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002331 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002332 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002333 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002334 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002335 EmitLabel("section_aranges", 0);
2336
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002337 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2338 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002339 EmitLabel("section_macinfo", 0);
2340 }
2341
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002342 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002343 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002344 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002345 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002346 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002347 EmitLabel("section_pubnames", 0);
Devang Patel193f7202009-11-24 01:14:22 +00002348 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2349 EmitLabel("section_pubtypes", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002350 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002351 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002352 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002353 EmitLabel("section_ranges", 0);
2354
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002355 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002356 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002357 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002358 EmitLabel("data_begin", 0);
2359}
2360
Devang Patel2c4ceb12009-11-21 02:48:08 +00002361/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002362///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002363void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002364 // Get the abbreviation for this DIE.
2365 unsigned AbbrevNumber = Die->getAbbrevNumber();
2366 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2367
2368 Asm->EOL();
2369
2370 // Emit the code (index) for the abbreviation.
2371 Asm->EmitULEB128Bytes(AbbrevNumber);
2372
2373 if (Asm->isVerbose())
2374 Asm->EOL(std::string("Abbrev [" +
2375 utostr(AbbrevNumber) +
2376 "] 0x" + utohexstr(Die->getOffset()) +
2377 ":0x" + utohexstr(Die->getSize()) + " " +
2378 dwarf::TagString(Abbrev->getTag())));
2379 else
2380 Asm->EOL();
2381
2382 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2383 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2384
2385 // Emit the DIE attribute values.
2386 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2387 unsigned Attr = AbbrevData[i].getAttribute();
2388 unsigned Form = AbbrevData[i].getForm();
2389 assert(Form && "Too many attributes for DIE (check abbreviation)");
2390
2391 switch (Attr) {
2392 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002393 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002394 break;
2395 case dwarf::DW_AT_abstract_origin: {
2396 DIEEntry *E = cast<DIEEntry>(Values[i]);
2397 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002398 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002399 Asm->EmitInt32(Addr);
2400 break;
2401 }
2402 default:
2403 // Emit an attribute using the defined form.
2404 Values[i]->EmitValue(this, Form);
2405 break;
2406 }
2407
2408 Asm->EOL(dwarf::AttributeString(Attr));
2409 }
2410
2411 // Emit the DIE children if any.
2412 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2413 const std::vector<DIE *> &Children = Die->getChildren();
2414
2415 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002416 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002417
2418 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2419 }
2420}
2421
Devang Patel2c4ceb12009-11-21 02:48:08 +00002422/// emitDebugInfo / emitDebugInfoPerCU - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002423///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002424void DwarfDebug::emitDebugInfoPerCU(CompileUnit *Unit) {
Devang Patel017d1212009-11-20 21:37:22 +00002425 DIE *Die = Unit->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002426
2427 // Emit the compile units header.
2428 EmitLabel("info_begin", Unit->getID());
2429
2430 // Emit size of content not including length itself
2431 unsigned ContentSize = Die->getSize() +
2432 sizeof(int16_t) + // DWARF version number
2433 sizeof(int32_t) + // Offset Into Abbrev. Section
2434 sizeof(int8_t) + // Pointer Size (in bytes)
2435 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2436
2437 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2438 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2439 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2440 Asm->EOL("Offset Into Abbrev. Section");
2441 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2442
Devang Patel2c4ceb12009-11-21 02:48:08 +00002443 emitDIE(Die);
Bill Wendling94d04b82009-05-20 23:21:38 +00002444 // FIXME - extra padding for gdb bug.
2445 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2446 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2447 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2448 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2449 EmitLabel("info_end", Unit->getID());
2450
2451 Asm->EOL();
2452}
2453
Devang Patel2c4ceb12009-11-21 02:48:08 +00002454void DwarfDebug::emitDebugInfo() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002455 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002456 Asm->OutStreamer.SwitchSection(
2457 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002458
Devang Patel2c4ceb12009-11-21 02:48:08 +00002459 emitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002460}
2461
Devang Patel2c4ceb12009-11-21 02:48:08 +00002462/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002463///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002464void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002465 // Check to see if it is worth the effort.
2466 if (!Abbreviations.empty()) {
2467 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002468 Asm->OutStreamer.SwitchSection(
2469 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002470
2471 EmitLabel("abbrev_begin", 0);
2472
2473 // For each abbrevation.
2474 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2475 // Get abbreviation data
2476 const DIEAbbrev *Abbrev = Abbreviations[i];
2477
2478 // Emit the abbrevations code (base 1 index.)
2479 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2480 Asm->EOL("Abbreviation Code");
2481
2482 // Emit the abbreviations data.
2483 Abbrev->Emit(Asm);
2484
2485 Asm->EOL();
2486 }
2487
2488 // Mark end of abbreviations.
2489 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2490
2491 EmitLabel("abbrev_end", 0);
2492 Asm->EOL();
2493 }
2494}
2495
Devang Patel2c4ceb12009-11-21 02:48:08 +00002496/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002497/// the line matrix.
2498///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002499void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002500 // Define last address of section.
2501 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2502 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2503 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2504 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2505
2506 // Mark end of matrix.
2507 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2508 Asm->EmitULEB128Bytes(1); Asm->EOL();
2509 Asm->EmitInt8(1); Asm->EOL();
2510}
2511
Devang Patel2c4ceb12009-11-21 02:48:08 +00002512/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002513///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002514void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002515 // If the target is using .loc/.file, the assembler will be emitting the
2516 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002517 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002518 return;
2519
2520 // Minimum line delta, thus ranging from -10..(255-10).
2521 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2522 // Maximum line delta, thus ranging from -10..(255-10).
2523 const int MaxLineDelta = 255 + MinLineDelta;
2524
2525 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002526 Asm->OutStreamer.SwitchSection(
2527 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002528
2529 // Construct the section header.
2530 EmitDifference("line_end", 0, "line_begin", 0, true);
2531 Asm->EOL("Length of Source Line Info");
2532 EmitLabel("line_begin", 0);
2533
2534 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2535
2536 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2537 Asm->EOL("Prolog Length");
2538 EmitLabel("line_prolog_begin", 0);
2539
2540 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2541
2542 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2543
2544 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2545
2546 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2547
2548 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2549
2550 // Line number standard opcode encodings argument count
2551 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2552 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2553 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2554 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2555 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2556 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2557 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2558 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2559 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2560
2561 // Emit directories.
2562 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2563 Asm->EmitString(getSourceDirectoryName(DI));
2564 Asm->EOL("Directory");
2565 }
2566
2567 Asm->EmitInt8(0); Asm->EOL("End of directories");
2568
2569 // Emit files.
2570 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2571 // Remember source id starts at 1.
2572 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2573 Asm->EmitString(getSourceFileName(Id.second));
2574 Asm->EOL("Source");
2575 Asm->EmitULEB128Bytes(Id.first);
2576 Asm->EOL("Directory #");
2577 Asm->EmitULEB128Bytes(0);
2578 Asm->EOL("Mod date");
2579 Asm->EmitULEB128Bytes(0);
2580 Asm->EOL("File size");
2581 }
2582
2583 Asm->EmitInt8(0); Asm->EOL("End of files");
2584
2585 EmitLabel("line_prolog_end", 0);
2586
2587 // A sequence for each text section.
2588 unsigned SecSrcLinesSize = SectionSourceLines.size();
2589
2590 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2591 // Isolate current sections line info.
2592 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2593
Chris Lattner93b6db32009-08-08 23:39:42 +00002594 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002595 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002596 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002597 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002598 }*/
2599 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002600
2601 // Dwarf assumes we start with first line of first source file.
2602 unsigned Source = 1;
2603 unsigned Line = 1;
2604
2605 // Construct rows of the address, source, line, column matrix.
2606 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2607 const SrcLineInfo &LineInfo = LineInfos[i];
2608 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2609 if (!LabelID) continue;
2610
Caroline Ticec6f9d622009-09-11 18:25:54 +00002611 if (LineInfo.getLine() == 0) continue;
2612
Bill Wendling94d04b82009-05-20 23:21:38 +00002613 if (!Asm->isVerbose())
2614 Asm->EOL();
2615 else {
2616 std::pair<unsigned, unsigned> SourceID =
2617 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002618 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002619 << getSourceDirectoryName(SourceID.first) << ' '
2620 << getSourceFileName(SourceID.second)
2621 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2622 }
2623
2624 // Define the line address.
2625 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2626 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2627 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2628 EmitReference("label", LabelID); Asm->EOL("Location label");
2629
2630 // If change of source, then switch to the new source.
2631 if (Source != LineInfo.getSourceID()) {
2632 Source = LineInfo.getSourceID();
2633 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2634 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2635 }
2636
2637 // If change of line.
2638 if (Line != LineInfo.getLine()) {
2639 // Determine offset.
2640 int Offset = LineInfo.getLine() - Line;
2641 int Delta = Offset - MinLineDelta;
2642
2643 // Update line.
2644 Line = LineInfo.getLine();
2645
2646 // If delta is small enough and in range...
2647 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2648 // ... then use fast opcode.
2649 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2650 } else {
2651 // ... otherwise use long hand.
2652 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2653 Asm->EOL("DW_LNS_advance_line");
2654 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2655 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2656 }
2657 } else {
2658 // Copy the previous row (different address or source)
2659 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2660 }
2661 }
2662
Devang Patel2c4ceb12009-11-21 02:48:08 +00002663 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002664 }
2665
2666 if (SecSrcLinesSize == 0)
2667 // Because we're emitting a debug_line section, we still need a line
2668 // table. The linker and friends expect it to exist. If there's nothing to
2669 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002670 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002671
2672 EmitLabel("line_end", 0);
2673 Asm->EOL();
2674}
2675
Devang Patel2c4ceb12009-11-21 02:48:08 +00002676/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002677///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002678void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002679 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002680 return;
2681
2682 int stackGrowth =
2683 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2684 TargetFrameInfo::StackGrowsUp ?
2685 TD->getPointerSize() : -TD->getPointerSize();
2686
2687 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002688 Asm->OutStreamer.SwitchSection(
2689 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002690
2691 EmitLabel("debug_frame_common", 0);
2692 EmitDifference("debug_frame_common_end", 0,
2693 "debug_frame_common_begin", 0, true);
2694 Asm->EOL("Length of Common Information Entry");
2695
2696 EmitLabel("debug_frame_common_begin", 0);
2697 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2698 Asm->EOL("CIE Identifier Tag");
2699 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2700 Asm->EOL("CIE Version");
2701 Asm->EmitString("");
2702 Asm->EOL("CIE Augmentation");
2703 Asm->EmitULEB128Bytes(1);
2704 Asm->EOL("CIE Code Alignment Factor");
2705 Asm->EmitSLEB128Bytes(stackGrowth);
2706 Asm->EOL("CIE Data Alignment Factor");
2707 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2708 Asm->EOL("CIE RA Column");
2709
2710 std::vector<MachineMove> Moves;
2711 RI->getInitialFrameState(Moves);
2712
2713 EmitFrameMoves(NULL, 0, Moves, false);
2714
2715 Asm->EmitAlignment(2, 0, 0, false);
2716 EmitLabel("debug_frame_common_end", 0);
2717
2718 Asm->EOL();
2719}
2720
Devang Patel2c4ceb12009-11-21 02:48:08 +00002721/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00002722/// section.
2723void
Devang Patel2c4ceb12009-11-21 02:48:08 +00002724DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002725 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002726 return;
2727
2728 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002729 Asm->OutStreamer.SwitchSection(
2730 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002731
2732 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2733 "debug_frame_begin", DebugFrameInfo.Number, true);
2734 Asm->EOL("Length of Frame Information Entry");
2735
2736 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2737
2738 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2739 0, 0, true, false);
2740 Asm->EOL("FDE CIE offset");
2741
2742 EmitReference("func_begin", DebugFrameInfo.Number);
2743 Asm->EOL("FDE initial location");
2744 EmitDifference("func_end", DebugFrameInfo.Number,
2745 "func_begin", DebugFrameInfo.Number);
2746 Asm->EOL("FDE address range");
2747
2748 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2749 false);
2750
2751 Asm->EmitAlignment(2, 0, 0, false);
2752 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2753
2754 Asm->EOL();
2755}
2756
Devang Patel2c4ceb12009-11-21 02:48:08 +00002757void DwarfDebug::emitDebugPubNamesPerCU(CompileUnit *Unit) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002758 EmitDifference("pubnames_end", Unit->getID(),
2759 "pubnames_begin", Unit->getID(), true);
2760 Asm->EOL("Length of Public Names Info");
2761
2762 EmitLabel("pubnames_begin", Unit->getID());
2763
2764 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2765
2766 EmitSectionOffset("info_begin", "section_info",
2767 Unit->getID(), 0, true, false);
2768 Asm->EOL("Offset of Compilation Unit Info");
2769
2770 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2771 true);
2772 Asm->EOL("Compilation Unit Length");
2773
Devang Patel193f7202009-11-24 01:14:22 +00002774 const StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling94d04b82009-05-20 23:21:38 +00002775 for (StringMap<DIE*>::const_iterator
2776 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2777 const char *Name = GI->getKeyData();
2778 DIE * Entity = GI->second;
2779
2780 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2781 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2782 }
2783
2784 Asm->EmitInt32(0); Asm->EOL("End Mark");
2785 EmitLabel("pubnames_end", Unit->getID());
2786
2787 Asm->EOL();
2788}
2789
Devang Patel2c4ceb12009-11-21 02:48:08 +00002790/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002791///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002792void DwarfDebug::emitDebugPubNames() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002793 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002794 Asm->OutStreamer.SwitchSection(
2795 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002796
Devang Patel2c4ceb12009-11-21 02:48:08 +00002797 emitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002798}
2799
Devang Patel193f7202009-11-24 01:14:22 +00002800void DwarfDebug::emitDebugPubTypes() {
Devang Patelf3a03762009-11-24 19:18:41 +00002801 // Start the dwarf pubnames section.
2802 Asm->OutStreamer.SwitchSection(
2803 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patel193f7202009-11-24 01:14:22 +00002804 EmitDifference("pubtypes_end", ModuleCU->getID(),
2805 "pubtypes_begin", ModuleCU->getID(), true);
2806 Asm->EOL("Length of Public Types Info");
2807
2808 EmitLabel("pubtypes_begin", ModuleCU->getID());
2809
2810 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2811
2812 EmitSectionOffset("info_begin", "section_info",
2813 ModuleCU->getID(), 0, true, false);
2814 Asm->EOL("Offset of Compilation ModuleCU Info");
2815
2816 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2817 true);
2818 Asm->EOL("Compilation ModuleCU Length");
2819
2820 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2821 for (StringMap<DIE*>::const_iterator
2822 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2823 const char *Name = GI->getKeyData();
2824 DIE * Entity = GI->second;
2825
2826 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2827 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2828 }
2829
2830 Asm->EmitInt32(0); Asm->EOL("End Mark");
2831 EmitLabel("pubtypes_end", ModuleCU->getID());
2832
2833 Asm->EOL();
2834}
2835
Devang Patel2c4ceb12009-11-21 02:48:08 +00002836/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002837///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002838void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002839 // Check to see if it is worth the effort.
2840 if (!StringPool.empty()) {
2841 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002842 Asm->OutStreamer.SwitchSection(
2843 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002844
2845 // For each of strings in the string pool.
2846 for (unsigned StringID = 1, N = StringPool.size();
2847 StringID <= N; ++StringID) {
2848 // Emit a label for reference from debug information entries.
2849 EmitLabel("string", StringID);
2850
2851 // Emit the string itself.
2852 const std::string &String = StringPool[StringID];
2853 Asm->EmitString(String); Asm->EOL();
2854 }
2855
2856 Asm->EOL();
2857 }
2858}
2859
Devang Patel2c4ceb12009-11-21 02:48:08 +00002860/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002861///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002862void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002863 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002864 Asm->OutStreamer.SwitchSection(
2865 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002866 Asm->EOL();
2867}
2868
2869/// EmitDebugARanges - Emit visible names into a debug aranges section.
2870///
2871void DwarfDebug::EmitDebugARanges() {
2872 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002873 Asm->OutStreamer.SwitchSection(
2874 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002875
2876 // FIXME - Mock up
2877#if 0
2878 CompileUnit *Unit = GetBaseCompileUnit();
2879
2880 // Don't include size of length
2881 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2882
2883 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2884
2885 EmitReference("info_begin", Unit->getID());
2886 Asm->EOL("Offset of Compilation Unit Info");
2887
2888 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2889
2890 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2891
2892 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2893 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2894
2895 // Range 1
2896 EmitReference("text_begin", 0); Asm->EOL("Address");
2897 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2898
2899 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2900 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2901#endif
2902
2903 Asm->EOL();
2904}
2905
Devang Patel2c4ceb12009-11-21 02:48:08 +00002906/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002907///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002908void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002909 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002910 Asm->OutStreamer.SwitchSection(
2911 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002912 Asm->EOL();
2913}
2914
Devang Patel2c4ceb12009-11-21 02:48:08 +00002915/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002916///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002917void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002918 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002919 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002920 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002921 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002922 Asm->EOL();
2923 }
2924}
2925
Devang Patel2c4ceb12009-11-21 02:48:08 +00002926/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002927/// Section Header:
2928/// 1. length of section
2929/// 2. Dwarf version number
2930/// 3. address size.
2931///
2932/// Entries (one "entry" for each function that was inlined):
2933///
2934/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2935/// otherwise offset into __debug_str for regular function name.
2936/// 2. offset into __debug_str section for regular function name.
2937/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2938/// instances for the function.
2939///
2940/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2941/// inlined instance; the die_offset points to the inlined_subroutine die in the
2942/// __debug_info section, and the low_pc is the starting address for the
2943/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002944void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002945 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002946 return;
2947
Devang Patel1dbc7712009-06-29 20:45:18 +00002948 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002949 return;
2950
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002951 Asm->OutStreamer.SwitchSection(
2952 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002953 Asm->EOL();
2954 EmitDifference("debug_inlined_end", 1,
2955 "debug_inlined_begin", 1, true);
2956 Asm->EOL("Length of Debug Inlined Information Entry");
2957
2958 EmitLabel("debug_inlined_begin", 1);
2959
2960 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2961 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2962
Devang Patel53bb5c92009-11-10 23:06:00 +00002963 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2964 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002965
Devang Patel53bb5c92009-11-10 23:06:00 +00002966// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2967 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2968 MDNode *Node = *I;
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002969 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2970 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002971 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002972 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002973 StringRef LName = SP.getLinkageName();
2974 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002975
Devang Patel65dbc902009-11-25 17:36:49 +00002976 if (LName.empty())
Devang Patel53cb17d2009-07-16 01:01:22 +00002977 Asm->EmitString(Name);
2978 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002979 // Skip special LLVM prefix that is used to inform the asm printer to not
2980 // emit usual symbol prefix before the symbol name. This happens for
2981 // Objective-C symbol names and symbol whose name is replaced using GCC's
2982 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002983 if (LName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00002984 LName = LName.substr(1);
Devang Patel53bb5c92009-11-10 23:06:00 +00002985// Asm->EmitString(LName);
2986 EmitSectionOffset("string", "section_str",
2987 StringPool.idFor(LName), false, true);
2988
Devang Patel53cb17d2009-07-16 01:01:22 +00002989 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002990 Asm->EOL("MIPS linkage name");
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002991// Asm->EmitString(Name);
Devang Patel53bb5c92009-11-10 23:06:00 +00002992 EmitSectionOffset("string", "section_str",
2993 StringPool.idFor(Name), false, true);
2994 Asm->EOL("Function name");
Bill Wendling94d04b82009-05-20 23:21:38 +00002995 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2996
Devang Patel53bb5c92009-11-10 23:06:00 +00002997 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002998 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002999 DIE *SP = LI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00003000 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
3001
3002 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00003003 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00003004 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00003005 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00003006
Devang Patel53bb5c92009-11-10 23:06:00 +00003007 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling94d04b82009-05-20 23:21:38 +00003008 }
3009 }
3010
3011 EmitLabel("debug_inlined_end", 1);
3012 Asm->EOL();
3013}