blob: dcb5900fe90729897528bdb850d1de39b289c330 [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 Patel2c4ceb12009-11-21 02:48:08 +0000333/// addString - Add a string attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000334///
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);
Bill Wendling0310d762009-05-15 09:23:25 +0000782}
783
Devang Patel2c4ceb12009-11-21 02:48:08 +0000784/// constructTypeDIE - Construct basic type die from DIBasicType.
785void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000786 DIBasicType BTy) {
787 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000788 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000789 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000790 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000791 BTy.getEncoding());
792
793 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000794 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000795 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000796 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000797 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000798}
799
Devang Patel2c4ceb12009-11-21 02:48:08 +0000800/// constructTypeDIE - Construct derived type die from DIDerivedType.
801void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000802 DIDerivedType DTy) {
803 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000804 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000805 uint64_t Size = DTy.getSizeInBits() >> 3;
806 unsigned Tag = DTy.getTag();
807
808 // FIXME - Workaround for templates.
809 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
810
811 Buffer.setTag(Tag);
812
813 // Map to main type, void will not have a type.
814 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000815 addType(DW_Unit, &Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000816
817 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000818 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000819 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000820
821 // Add size if non-zero (derived types might be zero-sized.)
822 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000823 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000824
825 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000826 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000827 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000828}
829
Devang Patel2c4ceb12009-11-21 02:48:08 +0000830/// constructTypeDIE - Construct type DIE from DICompositeType.
831void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000832 DICompositeType CTy) {
833 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000834 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000835
836 uint64_t Size = CTy.getSizeInBits() >> 3;
837 unsigned Tag = CTy.getTag();
838 Buffer.setTag(Tag);
839
840 switch (Tag) {
841 case dwarf::DW_TAG_vector_type:
842 case dwarf::DW_TAG_array_type:
Devang Patel2c4ceb12009-11-21 02:48:08 +0000843 constructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000844 break;
845 case dwarf::DW_TAG_enumeration_type: {
846 DIArray Elements = CTy.getTypeArray();
847
848 // Add enumerators to enumeration type.
849 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
850 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000851 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelc5254722009-10-09 17:51:49 +0000852 if (!Enum.isNull()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000853 ElemDie = constructEnumTypeDIE(DW_Unit, &Enum);
854 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +0000855 }
Bill Wendling0310d762009-05-15 09:23:25 +0000856 }
857 }
858 break;
859 case dwarf::DW_TAG_subroutine_type: {
860 // Add return type.
861 DIArray Elements = CTy.getTypeArray();
862 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000863 addType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000864
865 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000866 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000867
868 // Add arguments.
869 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
870 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
871 DIDescriptor Ty = Elements.getElement(i);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000872 addType(DW_Unit, Arg, DIType(Ty.getNode()));
873 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000874 }
875 }
876 break;
877 case dwarf::DW_TAG_structure_type:
878 case dwarf::DW_TAG_union_type:
879 case dwarf::DW_TAG_class_type: {
880 // Add elements to structure type.
881 DIArray Elements = CTy.getTypeArray();
882
883 // A forward struct declared type may not have elements available.
884 if (Elements.isNull())
885 break;
886
887 // Add elements to structure type.
888 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
889 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000890 if (Element.isNull())
891 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000892 DIE *ElemDie = NULL;
893 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000894 ElemDie = createSubprogramDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000895 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000896 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000897 ElemDie = createMemberDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000898 DIDerivedType(Element.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000899 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +0000900 }
901
Devang Patela1ba2692009-08-27 23:51:51 +0000902 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000903 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000904
905 unsigned RLang = CTy.getRunTimeLang();
906 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000907 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +0000908 dwarf::DW_FORM_data1, RLang);
909 break;
910 }
911 default:
912 break;
913 }
914
915 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000916 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000917 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000918
919 if (Tag == dwarf::DW_TAG_enumeration_type ||
920 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
921 // Add size if non-zero (derived types might be zero-sized.)
922 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000923 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000924 else {
925 // Add zero size if it is not a forward declaration.
926 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000927 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000928 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000929 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +0000930 }
931
932 // Add source line info if available.
933 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000934 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000935 }
936}
937
Devang Patel2c4ceb12009-11-21 02:48:08 +0000938/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
939void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +0000940 int64_t L = SR.getLo();
941 int64_t H = SR.getHi();
942 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
943
Devang Patel2c4ceb12009-11-21 02:48:08 +0000944 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +0000945 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000946 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patel6325a532009-08-14 20:59:16 +0000947 if (H)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000948 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000949
Devang Patel2c4ceb12009-11-21 02:48:08 +0000950 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +0000951}
952
Devang Patel2c4ceb12009-11-21 02:48:08 +0000953/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
954void DwarfDebug::constructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000955 DICompositeType *CTy) {
956 Buffer.setTag(dwarf::DW_TAG_array_type);
957 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000958 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000959
960 // Emit derived type.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000961 addType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +0000962 DIArray Elements = CTy->getTypeArray();
963
Devang Patel6f01d9c2009-11-21 00:31:03 +0000964 // Get an anonymous type for index type.
965 DIE *IdxTy = DW_Unit->getIndexTyDie();
966 if (!IdxTy) {
967 // Construct an anonymous type for index type.
968 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000969 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
970 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +0000971 dwarf::DW_ATE_signed);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000972 DW_Unit->addDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000973 DW_Unit->setIndexTyDie(IdxTy);
974 }
Bill Wendling0310d762009-05-15 09:23:25 +0000975
976 // Add subranges to array type.
977 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
978 DIDescriptor Element = Elements.getElement(i);
979 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000980 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000981 }
982}
983
Devang Patel2c4ceb12009-11-21 02:48:08 +0000984/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
985DIE *DwarfDebug::constructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000986 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel65dbc902009-11-25 17:36:49 +0000987 StringRef Name = ETy->getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000988 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000989 int64_t Value = ETy->getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +0000990 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000991 return Enumerator;
992}
993
Devang Patel2c4ceb12009-11-21 02:48:08 +0000994/// createGlobalVariableDIE - Create new DIE using GV.
995DIE *DwarfDebug::createGlobalVariableDIE(CompileUnit *DW_Unit,
Bill Wendling0310d762009-05-15 09:23:25 +0000996 const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000997 // If the global variable was optmized out then no need to create debug info
998 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +0000999 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001000 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001001
Bill Wendling0310d762009-05-15 09:23:25 +00001002 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001003 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001004 GV.getDisplayName());
1005
Devang Patel65dbc902009-11-25 17:36:49 +00001006 StringRef LinkageName = GV.getLinkageName();
1007 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001008 // Skip special LLVM prefix that is used to inform the asm printer to not
1009 // emit usual symbol prefix before the symbol name. This happens for
1010 // Objective-C symbol names and symbol whose name is replaced using GCC's
1011 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001012 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001013 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001014 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001015 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001016 }
Devang Patel2c4ceb12009-11-21 02:48:08 +00001017 addType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001018 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001019 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1020 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001021
1022 // Add address.
1023 DIEBlock *Block = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001024 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1025 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patelb71a16d2009-10-05 23:22:08 +00001026 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001027 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patelb71a16d2009-10-05 23:22:08 +00001028
Bill Wendling0310d762009-05-15 09:23:25 +00001029 return GVDie;
1030}
1031
Devang Patel2c4ceb12009-11-21 02:48:08 +00001032/// createMemberDIE - Create new member DIE.
1033DIE *DwarfDebug::createMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
Bill Wendling0310d762009-05-15 09:23:25 +00001034 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001035 StringRef Name = DT.getName();
1036 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001037 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001038
Devang Patel2c4ceb12009-11-21 02:48:08 +00001039 addType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001040
Devang Patel2c4ceb12009-11-21 02:48:08 +00001041 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001042
Devang Patel33db5082009-11-04 22:06:12 +00001043 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001044 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001045
Bill Wendling0310d762009-05-15 09:23:25 +00001046 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001047 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001048
1049 if (Size != FieldSize) {
1050 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001051 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1052 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001053
1054 uint64_t Offset = DT.getOffsetInBits();
1055 uint64_t FieldOffset = Offset;
1056 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1057 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1058 FieldOffset = (HiMark - FieldSize);
1059 Offset -= FieldOffset;
1060
1061 // Maybe we need to work from the other end.
1062 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001063 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001064
Devang Patel33db5082009-11-04 22:06:12 +00001065 // Here WD_AT_data_member_location points to the anonymous
1066 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001067 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001068
1069 } else
1070 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001071 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001072
Devang Patel2c4ceb12009-11-21 02:48:08 +00001073 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001074
1075 if (DT.isProtected())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001076 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
Bill Wendling0310d762009-05-15 09:23:25 +00001077 dwarf::DW_ACCESS_protected);
1078 else if (DT.isPrivate())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001079 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
Bill Wendling0310d762009-05-15 09:23:25 +00001080 dwarf::DW_ACCESS_private);
1081
1082 return MemberDie;
1083}
1084
Devang Patel2c4ceb12009-11-21 02:48:08 +00001085/// createSubprogramDIE - Create new DIE using SP.
1086DIE *DwarfDebug::createSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling0310d762009-05-15 09:23:25 +00001087 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +00001088 bool IsConstructor,
1089 bool IsInlined) {
Devang Patel7d0750a2009-12-01 23:07:59 +00001090 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1091 if (SPDie)
1092 return SPDie;
1093
1094 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel65dbc902009-11-25 17:36:49 +00001095 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001096
Devang Patel65dbc902009-11-25 17:36:49 +00001097 StringRef LinkageName = SP.getLinkageName();
1098 if (!LinkageName.empty()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001099 // Skip special LLVM prefix that is used to inform the asm printer to not
1100 // emit usual symbol prefix before the symbol name. This happens for
1101 // Objective-C symbol names and symbol whose name is replaced using GCC's
1102 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001103 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001104 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001105 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001106 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001107 }
Devang Patel2c4ceb12009-11-21 02:48:08 +00001108 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001109
1110 DICompositeType SPTy = SP.getType();
1111 DIArray Args = SPTy.getTypeArray();
1112
1113 // Add prototyped tag, if C or ObjC.
1114 unsigned Lang = SP.getCompileUnit().getLanguage();
1115 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1116 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001117 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001118
1119 // Add Return Type.
1120 unsigned SPTag = SPTy.getTag();
1121 if (!IsConstructor) {
1122 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001123 addType(DW_Unit, SPDie, SPTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001124 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001125 addType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001126 }
1127
1128 if (!SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001129 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001130
1131 // Add arguments. Do not add arguments for subprogram definition. They will
1132 // be handled through RecordVariable.
1133 if (SPTag == dwarf::DW_TAG_subroutine_type)
1134 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1135 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001136 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1137 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1138 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001139 }
1140 }
1141
Bill Wendling0310d762009-05-15 09:23:25 +00001142 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel017d1212009-11-20 21:37:22 +00001143 DW_Unit->insertDIE(SP.getNode(), SPDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001144 return SPDie;
1145}
1146
Devang Patel2c4ceb12009-11-21 02:48:08 +00001147/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendling0310d762009-05-15 09:23:25 +00001148///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001149CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const {
Bill Wendling0310d762009-05-15 09:23:25 +00001150 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001151 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001152 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1153 return *I->second;
1154}
1155
Devang Patel2c4ceb12009-11-21 02:48:08 +00001156/// createDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +00001157///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001158DIE *DwarfDebug::createDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001159 // Get the descriptor.
1160 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001161 StringRef Name = VD.getName();
1162 if (Name.empty())
Devang Patel1b845972009-11-03 18:30:27 +00001163 return NULL;
Bill Wendling0310d762009-05-15 09:23:25 +00001164
1165 // Translate tag to proper Dwarf tag. The result variable is dropped for
1166 // now.
1167 unsigned Tag;
1168 switch (VD.getTag()) {
1169 case dwarf::DW_TAG_return_variable:
1170 return NULL;
1171 case dwarf::DW_TAG_arg_variable:
1172 Tag = dwarf::DW_TAG_formal_parameter;
1173 break;
1174 case dwarf::DW_TAG_auto_variable: // fall thru
1175 default:
1176 Tag = dwarf::DW_TAG_variable;
1177 break;
1178 }
1179
1180 // Define variable debug information entry.
1181 DIE *VariableDie = new DIE(Tag);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001182 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001183
1184 // Add source line info if available.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001185 addSourceLine(VariableDie, &VD);
Bill Wendling0310d762009-05-15 09:23:25 +00001186
1187 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001188 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001189 // addresses instead.
Caroline Ticedc8f6042009-08-31 21:19:37 +00001190 if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001191 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Caroline Ticedc8f6042009-08-31 21:19:37 +00001192 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001193 addType(Unit, VariableDie, VD.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001194
1195 // Add variable address.
Devang Patel53bb5c92009-11-10 23:06:00 +00001196 // Variables for abstract instances of inlined functions don't get a
1197 // location.
1198 MachineLocation Location;
Jim Grosbacha2f20b22009-11-22 20:14:00 +00001199 unsigned FrameReg;
1200 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1201 Location.set(FrameReg, Offset);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001202
1203
Devang Patel53bb5c92009-11-10 23:06:00 +00001204 if (VD.hasComplexAddress())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001205 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001206 else if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001207 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001208 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001209 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001210
1211 return VariableDie;
1212}
1213
Devang Patel53bb5c92009-11-10 23:06:00 +00001214/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1215/// Initialize scope and update scope hierarchy.
1216DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1217 MDNode *InlinedAt) {
1218 assert (N && "Invalid Scope encoding!");
1219 assert (MI && "Missing machine instruction!");
1220 bool GetConcreteScope = (MI && InlinedAt);
1221
1222 DbgScope *NScope = NULL;
1223
1224 if (InlinedAt)
1225 NScope = DbgScopeMap.lookup(InlinedAt);
1226 else
1227 NScope = DbgScopeMap.lookup(N);
1228 assert (NScope && "Unable to find working scope!");
1229
1230 if (NScope->getFirstInsn())
1231 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001232
1233 DbgScope *Parent = NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001234 if (GetConcreteScope) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001235 DILocation IL(InlinedAt);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001236 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel53bb5c92009-11-10 23:06:00 +00001237 IL.getOrigLocation().getNode());
1238 assert (Parent && "Unable to find Parent scope!");
1239 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001240 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001241 } else if (DIDescriptor(N).isLexicalBlock()) {
1242 DILexicalBlock DB(N);
1243 if (!DB.getContext().isNull()) {
1244 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1245 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001246 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001247 }
Devang Patelc90aefe2009-10-14 21:08:09 +00001248 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001249
Devang Patelbdf45cb2009-10-27 20:47:17 +00001250 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001251
Devang Patel53bb5c92009-11-10 23:06:00 +00001252 if (!Parent && !InlinedAt) {
Devang Patel39ae3ff2009-11-11 00:31:36 +00001253 StringRef SPName = DISubprogram(N).getLinkageName();
1254 if (SPName == MF->getFunction()->getName())
1255 CurrentFnDbgScope = NScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001256 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001257
Devang Patel53bb5c92009-11-10 23:06:00 +00001258 if (GetConcreteScope) {
1259 ConcreteScopes[InlinedAt] = NScope;
1260 getOrCreateAbstractScope(N);
1261 }
1262
Devang Patelbdf45cb2009-10-27 20:47:17 +00001263 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001264}
1265
Devang Patel53bb5c92009-11-10 23:06:00 +00001266DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1267 assert (N && "Invalid Scope encoding!");
1268
1269 DbgScope *AScope = AbstractScopes.lookup(N);
1270 if (AScope)
1271 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001272
Devang Patel53bb5c92009-11-10 23:06:00 +00001273 DbgScope *Parent = NULL;
1274
1275 DIDescriptor Scope(N);
1276 if (Scope.isLexicalBlock()) {
1277 DILexicalBlock DB(N);
1278 DIDescriptor ParentDesc = DB.getContext();
1279 if (!ParentDesc.isNull())
1280 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1281 }
1282
1283 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1284
1285 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001286 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001287 AScope->setAbstractScope();
1288 AbstractScopes[N] = AScope;
1289 if (DIDescriptor(N).isSubprogram())
1290 AbstractScopesList.push_back(AScope);
1291 return AScope;
1292}
Devang Patelaf9e8472009-10-01 20:31:14 +00001293
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001294/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001295/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1296/// If there are global variables in this scope then create and insert
1297/// DIEs for these variables.
1298DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001299
Devang Patel017d1212009-11-20 21:37:22 +00001300 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel53bb5c92009-11-10 23:06:00 +00001301 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001302 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001303 DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001304 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001305 DWLabel("func_end", SubprogramCount));
1306 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001307 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001308
Devang Patel53bb5c92009-11-10 23:06:00 +00001309 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001310 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001311
1312 // If there are global variables at this scope then add their dies.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001313 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00001314 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1315 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1316 if (!N) continue;
1317 DIGlobalVariable GV(N);
1318 if (GV.getContext().getNode() == SPNode) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001319 DIE *ScopedGVDie = createGlobalVariableDIE(ModuleCU, GV);
Devang Patelfb0ee432009-11-10 23:20:04 +00001320 if (ScopedGVDie)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001321 SPDie->addChild(ScopedGVDie);
Devang Patel53bb5c92009-11-10 23:06:00 +00001322 }
1323 }
Devang Patel193f7202009-11-24 01:14:22 +00001324
Devang Patel53bb5c92009-11-10 23:06:00 +00001325 return SPDie;
1326}
1327
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001328/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001329/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1330DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001331 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1332 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1333
1334 // Ignore empty scopes.
1335 if (StartID == EndID && StartID != 0)
1336 return NULL;
1337
1338 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1339 if (Scope->isAbstractScope())
1340 return ScopeDIE;
1341
Devang Patel2c4ceb12009-11-21 02:48:08 +00001342 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001343 StartID ?
1344 DWLabel("label", StartID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001345 : DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001346 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001347 EndID ?
1348 DWLabel("label", EndID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001349 : DWLabel("func_end", SubprogramCount));
1350
1351
1352
1353 return ScopeDIE;
1354}
1355
Devang Patel2c4ceb12009-11-21 02:48:08 +00001356/// constructInlinedScopeDIE - This scope represents inlined body of
1357/// a function. Construct DIE to represent this concrete inlined copy
1358/// of the function.
1359DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001360 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1361 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1362 assert (StartID && "Invalid starting label for an inlined scope!");
1363 assert (EndID && "Invalid end label for an inlined scope!");
1364 // Ignore empty scopes.
1365 if (StartID == EndID && StartID != 0)
1366 return NULL;
1367
1368 DIScope DS(Scope->getScopeNode());
1369 if (DS.isNull())
1370 return NULL;
1371 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1372
1373 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001374 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001375 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001376 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001377 dwarf::DW_FORM_ref4, OriginDIE);
1378
Devang Patel2c4ceb12009-11-21 02:48:08 +00001379 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001380 DWLabel("label", StartID));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001381 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001382 DWLabel("label", EndID));
1383
1384 InlinedSubprogramDIEs.insert(OriginDIE);
1385
1386 // Track the start label for this inlined function.
1387 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1388 I = InlineInfo.find(InlinedSP.getNode());
1389
1390 if (I == InlineInfo.end()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001391 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1392 ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001393 InlinedSPNodes.push_back(InlinedSP.getNode());
1394 } else
1395 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1396
1397 StringPool.insert(InlinedSP.getName());
1398 StringPool.insert(InlinedSP.getLinkageName());
1399 DILocation DL(Scope->getInlinedAt());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001400 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1401 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001402
1403 return ScopeDIE;
1404}
1405
Devang Patel2c4ceb12009-11-21 02:48:08 +00001406
1407/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001408DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV,
Devang Patel53bb5c92009-11-10 23:06:00 +00001409 DbgScope *Scope, CompileUnit *Unit) {
1410 // Get the descriptor.
1411 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001412 StringRef Name = VD.getName();
1413 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001414 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001415
1416 // Translate tag to proper Dwarf tag. The result variable is dropped for
1417 // now.
1418 unsigned Tag;
1419 switch (VD.getTag()) {
1420 case dwarf::DW_TAG_return_variable:
1421 return NULL;
1422 case dwarf::DW_TAG_arg_variable:
1423 Tag = dwarf::DW_TAG_formal_parameter;
1424 break;
1425 case dwarf::DW_TAG_auto_variable: // fall thru
1426 default:
1427 Tag = dwarf::DW_TAG_variable;
1428 break;
1429 }
1430
1431 // Define variable debug information entry.
1432 DIE *VariableDie = new DIE(Tag);
1433
1434
1435 DIE *AbsDIE = NULL;
1436 if (DbgVariable *AV = DV->getAbstractVariable())
1437 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001438
Devang Patel53bb5c92009-11-10 23:06:00 +00001439 if (AbsDIE) {
1440 DIScope DS(Scope->getScopeNode());
1441 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001442 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc0326792009-11-11 03:09:50 +00001443 (void) OriginSPDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001444 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1445 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1446 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001447 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001448 dwarf::DW_FORM_ref4, AbsDIE);
1449 }
1450 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001451 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1452 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001453
1454 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001455 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001456 // addresses instead.
1457 if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001458 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001459 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001460 addType(Unit, VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001461 }
1462
1463 // Add variable address.
1464 if (!Scope->isAbstractScope()) {
1465 MachineLocation Location;
Jim Grosbacha2f20b22009-11-22 20:14:00 +00001466 unsigned FrameReg;
1467 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1468 Location.set(FrameReg, Offset);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001469
Devang Patel53bb5c92009-11-10 23:06:00 +00001470 if (VD.hasComplexAddress())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001471 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001472 else if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001473 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001474 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001475 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001476 }
1477 DV->setDIE(VariableDie);
1478 return VariableDie;
1479
1480}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001481
Devang Patel193f7202009-11-24 01:14:22 +00001482void DwarfDebug::addPubTypes(DISubprogram SP) {
1483 DICompositeType SPTy = SP.getType();
1484 unsigned SPTag = SPTy.getTag();
1485 if (SPTag != dwarf::DW_TAG_subroutine_type)
1486 return;
1487
1488 DIArray Args = SPTy.getTypeArray();
1489 if (Args.isNull())
1490 return;
1491
1492 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1493 DIType ATy(Args.getElement(i).getNode());
1494 if (ATy.isNull())
1495 continue;
1496 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel65dbc902009-11-25 17:36:49 +00001497 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001498 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1499 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1500 }
1501 }
1502}
1503
Devang Patel2c4ceb12009-11-21 02:48:08 +00001504/// constructScopeDIE - Construct a DIE for this scope.
1505DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001506 if (!Scope)
1507 return NULL;
1508 DIScope DS(Scope->getScopeNode());
1509 if (DS.isNull())
1510 return NULL;
1511
1512 DIE *ScopeDIE = NULL;
1513 if (Scope->getInlinedAt())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001514 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001515 else if (DS.isSubprogram()) {
1516 if (Scope->isAbstractScope())
Devang Patel017d1212009-11-20 21:37:22 +00001517 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001518 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001519 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001520 }
1521 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001522 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001523 if (!ScopeDIE) return NULL;
1524 }
1525
1526 // Add variables to scope.
1527 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1528 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001529 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope, ModuleCU);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001530 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001531 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001532 }
1533
1534 // Add nested scopes.
1535 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1536 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1537 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001538 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001539 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001540 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001541 }
Devang Patel193f7202009-11-24 01:14:22 +00001542
1543 if (DS.isSubprogram())
1544 addPubTypes(DISubprogram(DS.getNode()));
1545
1546 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001547}
1548
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001549/// GetOrCreateSourceID - Look up the source id with the given directory and
1550/// source file names. If none currently exists, create a new id and insert it
1551/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1552/// maps as well.
Devang Patel65dbc902009-11-25 17:36:49 +00001553unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001554 unsigned DId;
1555 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1556 if (DI != DirectoryIdMap.end()) {
1557 DId = DI->getValue();
1558 } else {
1559 DId = DirectoryNames.size() + 1;
1560 DirectoryIdMap[DirName] = DId;
1561 DirectoryNames.push_back(DirName);
1562 }
1563
1564 unsigned FId;
1565 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1566 if (FI != SourceFileIdMap.end()) {
1567 FId = FI->getValue();
1568 } else {
1569 FId = SourceFileNames.size() + 1;
1570 SourceFileIdMap[FileName] = FId;
1571 SourceFileNames.push_back(FileName);
1572 }
1573
1574 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1575 SourceIdMap.find(std::make_pair(DId, FId));
1576 if (SI != SourceIdMap.end())
1577 return SI->second;
1578
1579 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1580 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1581 SourceIds.push_back(std::make_pair(DId, FId));
1582
1583 return SrcId;
1584}
1585
Devang Patel2c4ceb12009-11-21 02:48:08 +00001586void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001587 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001588 StringRef FN = DIUnit.getFilename();
1589 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001590 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001591
1592 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001593 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001594 DWLabel("section_line", 0), DWLabel("section_line", 0),
1595 false);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001596 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001597 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001598 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001599 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001600 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001601
Devang Patel65dbc902009-11-25 17:36:49 +00001602 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001603 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001604 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001605 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001606
Devang Patel65dbc902009-11-25 17:36:49 +00001607 StringRef Flags = DIUnit.getFlags();
1608 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001609 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001610
1611 unsigned RVer = DIUnit.getRunTimeVersion();
1612 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001613 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001614 dwarf::DW_FORM_data1, RVer);
1615
1616 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001617 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001618 // Use first compile unit marked as isMain as the compile unit
1619 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001620 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001621 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001622
Devang Patele4b27562009-08-28 23:24:31 +00001623 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001624 CompileUnits.push_back(Unit);
1625}
1626
Devang Patel2c4ceb12009-11-21 02:48:08 +00001627void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001628 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001629
Devang Patel905cf5e2009-09-04 23:59:07 +00001630 // If debug information is malformed then ignore it.
1631 if (DI_GV.Verify() == false)
1632 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001633
1634 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001635 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel13e16b62009-06-26 01:49:18 +00001636 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001637
Devang Patel2c4ceb12009-11-21 02:48:08 +00001638 DIE *VariableDie = createGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001639
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001640 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001641 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001642
1643 // Add to context owner.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001644 ModuleCU->getCUDie()->addChild(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001645
1646 // Expose as global. FIXME - need to check external flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001647 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001648
1649 DIType GTy = DI_GV.getType();
Devang Patel65dbc902009-11-25 17:36:49 +00001650 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001651 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1652 assert (Entry && "Missing global type!");
1653 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1654 }
Devang Patel13e16b62009-06-26 01:49:18 +00001655 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001656}
1657
Devang Patel2c4ceb12009-11-21 02:48:08 +00001658void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001659 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001660
1661 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001662 if (ModuleCU->getDIE(N))
Devang Patel13e16b62009-06-26 01:49:18 +00001663 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001664
1665 if (!SP.isDefinition())
1666 // This is a method declaration which will be handled while constructing
1667 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001668 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001669
Devang Patel2c4ceb12009-11-21 02:48:08 +00001670 DIE *SubprogramDie = createSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001671
1672 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001673 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001674
1675 // Add to context owner.
Devang Patel7d0750a2009-12-01 23:07:59 +00001676 if (SP.getContext().getNode() == SP.getCompileUnit().getNode())
1677 ModuleCU->getCUDie()->addChild(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001678
1679 // Expose as global.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001680 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001681
Devang Patel13e16b62009-06-26 01:49:18 +00001682 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001683}
1684
Devang Patel2c4ceb12009-11-21 02:48:08 +00001685/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001686/// content. Create global DIEs and emit initial debug info sections.
1687/// This is inovked by the target AsmPrinter.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001688void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel208622d2009-06-25 22:36:02 +00001689 this->M = M;
1690
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001691 if (TimePassesIsEnabled)
1692 DebugTimer->startTimer();
1693
Devang Patel3380cc52009-11-11 19:55:08 +00001694 if (!MAI->doesSupportDebugInformation())
1695 return;
1696
Devang Patel78ab9e22009-07-30 18:56:46 +00001697 DebugInfoFinder DbgFinder;
1698 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001699
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001700 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001701 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1702 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001703 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001704
1705 if (CompileUnits.empty()) {
1706 if (TimePassesIsEnabled)
1707 DebugTimer->stopTimer();
1708
1709 return;
1710 }
1711
Devang Patel70f44262009-06-29 20:38:13 +00001712 // If main compile unit for this module is not seen than randomly
1713 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001714 if (!ModuleCU)
1715 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001716
Devang Patel13e16b62009-06-26 01:49:18 +00001717 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001718 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patelfd07cf52009-10-05 23:40:42 +00001719 E = DbgFinder.global_variable_end(); I != E; ++I) {
1720 DIGlobalVariable GV(*I);
1721 if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
1722 ScopedGVs.push_back(*I);
1723 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001724 constructGlobalVariableDIE(*I);
Devang Patelfd07cf52009-10-05 23:40:42 +00001725 }
Devang Patel13e16b62009-06-26 01:49:18 +00001726
Devang Patel53bb5c92009-11-10 23:06:00 +00001727 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001728 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1729 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001730 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001731
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001732 MMI = mmi;
1733 shouldEmit = true;
1734 MMI->setDebugInfoAvailability(true);
1735
1736 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001737 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001738
1739 // Print out .file directives to specify files for .loc directives. These are
1740 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001741 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001742 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1743 // Remember source id starts at 1.
1744 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1745 sys::Path FullPath(getSourceDirectoryName(Id.first));
1746 bool AppendOk =
1747 FullPath.appendComponent(getSourceFileName(Id.second));
1748 assert(AppendOk && "Could not append filename to directory!");
1749 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001750 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001751 Asm->EOL();
1752 }
1753 }
1754
1755 // Emit initial sections
Devang Patel2c4ceb12009-11-21 02:48:08 +00001756 emitInitial();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001757
1758 if (TimePassesIsEnabled)
1759 DebugTimer->stopTimer();
1760}
1761
Devang Patel2c4ceb12009-11-21 02:48:08 +00001762/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001763///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001764void DwarfDebug::endModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001765 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001766 return;
1767
1768 if (TimePassesIsEnabled)
1769 DebugTimer->startTimer();
1770
Devang Patel53bb5c92009-11-10 23:06:00 +00001771 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1772 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1773 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1774 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001775 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001776 }
1777
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001778 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001779 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001780 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001781 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001782 EmitLabel("data_end", 0);
1783
1784 // End text sections.
1785 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001786 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001787 EmitLabel("section_end", i);
1788 }
1789
1790 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001791 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001792
1793 // Emit function debug frame information
1794 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1795 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001796 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001797
1798 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001799 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001800
1801 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001802 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001803
1804 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001805 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001806
1807 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001808 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001809
1810 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001811 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001812
Devang Patel193f7202009-11-24 01:14:22 +00001813 // Emit info into a debug pubtypes section.
1814 emitDebugPubTypes();
1815
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001816 // Emit info into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001817 emitDebugStr();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001818
1819 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001820 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001821
1822 // Emit info into a debug aranges section.
1823 EmitDebugARanges();
1824
1825 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001826 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001827
1828 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001829 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001830
1831 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001832 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001833
1834 if (TimePassesIsEnabled)
1835 DebugTimer->stopTimer();
1836}
1837
Devang Patel53bb5c92009-11-10 23:06:00 +00001838/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001839DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1840 unsigned FrameIdx,
Devang Patel53bb5c92009-11-10 23:06:00 +00001841 DILocation &ScopeLoc) {
1842
1843 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1844 if (AbsDbgVariable)
1845 return AbsDbgVariable;
1846
1847 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1848 if (!Scope)
1849 return NULL;
1850
1851 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001852 Scope->addVariable(AbsDbgVariable);
Devang Patel53bb5c92009-11-10 23:06:00 +00001853 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1854 return AbsDbgVariable;
1855}
1856
Devang Patel2c4ceb12009-11-21 02:48:08 +00001857/// collectVariableInfo - Populate DbgScope entries with variables' info.
1858void DwarfDebug::collectVariableInfo() {
Devang Patelac1ceb32009-10-09 22:42:28 +00001859 if (!MMI) return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001860
Devang Patele717faa2009-10-06 01:26:37 +00001861 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1862 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1863 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac1ceb32009-10-09 22:42:28 +00001864 MetadataBase *MB = VI->first;
1865 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel53bb5c92009-11-10 23:06:00 +00001866 if (!Var) continue;
Devang Pateleda31212009-10-08 18:48:03 +00001867 DIVariable DV (Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001868 std::pair< unsigned, MDNode *> VP = VI->second;
1869 DILocation ScopeLoc(VP.second);
1870
1871 DbgScope *Scope =
1872 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1873 if (!Scope)
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001874 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelfb0ee432009-11-10 23:20:04 +00001875 // If variable scope is not found then skip this variable.
1876 if (!Scope)
1877 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001878
1879 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001880 Scope->addVariable(RegVar);
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001881 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1882 ScopeLoc))
Devang Patel53bb5c92009-11-10 23:06:00 +00001883 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patele717faa2009-10-06 01:26:37 +00001884 }
1885}
1886
Devang Patel2c4ceb12009-11-21 02:48:08 +00001887/// beginScope - Process beginning of a scope starting at Label.
1888void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001889 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1890 if (I == DbgScopeBeginMap.end())
1891 return;
Dan Gohman277207e2009-11-23 21:30:55 +00001892 ScopeVector &SD = I->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001893 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001894 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001895 (*SDI)->setStartLabelID(Label);
1896}
1897
Devang Patel2c4ceb12009-11-21 02:48:08 +00001898/// endScope - Process end of a scope.
1899void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001900 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00001901 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00001902 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001903
1904 unsigned Label = MMI->NextLabelID();
1905 Asm->printLabel(Label);
1906
Devang Patel0d20ac82009-10-06 01:50:42 +00001907 SmallVector<DbgScope *, 2> &SD = I->second;
1908 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001909 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001910 (*SDI)->setEndLabelID(Label);
Devang Patel53bb5c92009-11-10 23:06:00 +00001911 return;
1912}
1913
1914/// createDbgScope - Create DbgScope for the scope.
1915void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1916
1917 if (!InlinedAt) {
1918 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1919 if (WScope)
1920 return;
1921 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1922 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001923 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel2f105c62009-11-11 00:18:40 +00001924 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001925 return;
1926 }
1927
1928 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1929 if (WScope)
1930 return;
1931
1932 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1933 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1934 DILocation DL(InlinedAt);
1935 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel0d20ac82009-10-06 01:50:42 +00001936}
1937
Devang Patel2c4ceb12009-11-21 02:48:08 +00001938/// extractScopeInformation - Scan machine instructions in this function
Devang Patelaf9e8472009-10-01 20:31:14 +00001939/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001940bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patelaf9e8472009-10-01 20:31:14 +00001941 // If scope information was extracted using .dbg intrinsics then there is not
1942 // any need to extract these information by scanning each instruction.
1943 if (!DbgScopeMap.empty())
1944 return false;
1945
Devang Patel53bb5c92009-11-10 23:06:00 +00001946 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelaf9e8472009-10-01 20:31:14 +00001947 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1948 I != E; ++I) {
1949 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1950 II != IE; ++II) {
1951 const MachineInstr *MInsn = II;
1952 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001953 if (DL.isUnknown()) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001954 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001955 if (!DLT.Scope) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001956 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001957 // other scopes, create one DbgScope now. This will be translated
Devang Patelaf9e8472009-10-01 20:31:14 +00001958 // into a scope DIE at the end.
Devang Patel53bb5c92009-11-10 23:06:00 +00001959 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1960 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
1961 }
1962 }
1963
1964
1965 // Build scope hierarchy using working set of scopes.
1966 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1967 I != E; ++I) {
1968 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1969 II != IE; ++II) {
1970 const MachineInstr *MInsn = II;
1971 DebugLoc DL = MInsn->getDebugLoc();
1972 if (DL.isUnknown()) continue;
1973 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
1974 if (!DLT.Scope) continue;
1975 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001976 // other scopes, create one DbgScope now. This will be translated
Devang Patel53bb5c92009-11-10 23:06:00 +00001977 // into a scope DIE at the end.
1978 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1979 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
1980 Scope->setLastInsn(MInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +00001981 }
1982 }
1983
1984 // If a scope's last instruction is not set then use its child scope's
1985 // last instruction as this scope's last instrunction.
Devang Patelbdf45cb2009-10-27 20:47:17 +00001986 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00001987 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001988 if (DI->second->isAbstractScope())
1989 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001990 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001991 DI->second->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +00001992 assert (DI->second->getLastInsn() && "Invalid last instruction!");
1993 }
1994
1995 // Each scope has first instruction and last instruction to mark beginning
1996 // and end of a scope respectively. Create an inverse map that list scopes
1997 // starts (and ends) with an instruction. One instruction may start (or end)
1998 // multiple scopes.
Devang Patelbdf45cb2009-10-27 20:47:17 +00001999 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002000 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2001 DbgScope *S = DI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002002 if (S->isAbstractScope())
2003 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002004 const MachineInstr *MI = S->getFirstInsn();
2005 assert (MI && "DbgScope does not have first instruction!");
2006
2007 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2008 if (IDI != DbgScopeBeginMap.end())
2009 IDI->second.push_back(S);
2010 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002011 DbgScopeBeginMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002012
2013 MI = S->getLastInsn();
2014 assert (MI && "DbgScope does not have last instruction!");
2015 IDI = DbgScopeEndMap.find(MI);
2016 if (IDI != DbgScopeEndMap.end())
2017 IDI->second.push_back(S);
2018 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002019 DbgScopeEndMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002020 }
2021
2022 return !DbgScopeMap.empty();
2023}
2024
Devang Patel2c4ceb12009-11-21 02:48:08 +00002025/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002026/// emitted immediately after the function entry point.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002027void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002028 this->MF = MF;
2029
2030 if (!ShouldEmitDwarfDebug()) return;
2031
2032 if (TimePassesIsEnabled)
2033 DebugTimer->startTimer();
2034
Devang Patel2c4ceb12009-11-21 02:48:08 +00002035 if (!extractScopeInformation(MF))
Devang Patel60b35bd2009-10-06 18:37:31 +00002036 return;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002037
2038 collectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002039
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002040 // Begin accumulating function debug information.
2041 MMI->BeginFunction(MF);
2042
2043 // Assumes in correct section after the entry point.
2044 EmitLabel("func_begin", ++SubprogramCount);
2045
2046 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2047 // function.
Devang Patelac1ceb32009-10-09 22:42:28 +00002048 DebugLoc FDL = MF->getDefaultDebugLoc();
2049 if (!FDL.isUnknown()) {
2050 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2051 unsigned LabelID = 0;
Devang Patel1619dc32009-10-13 23:28:53 +00002052 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002053 if (!SP.isNull())
Devang Patel2c4ceb12009-11-21 02:48:08 +00002054 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002055 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00002056 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002057 Asm->printLabel(LabelID);
2058 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002059 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002060 if (TimePassesIsEnabled)
2061 DebugTimer->stopTimer();
2062}
2063
Devang Patel2c4ceb12009-11-21 02:48:08 +00002064/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002065///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002066void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002067 if (!ShouldEmitDwarfDebug()) return;
2068
2069 if (TimePassesIsEnabled)
2070 DebugTimer->startTimer();
2071
Devang Patelac1ceb32009-10-09 22:42:28 +00002072 if (DbgScopeMap.empty())
2073 return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002074
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002075 // Define end label for subprogram.
2076 EmitLabel("func_end", SubprogramCount);
2077
2078 // Get function line info.
2079 if (!Lines.empty()) {
2080 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00002081 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002082 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2083 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2084 // Append the function info to section info.
2085 SectionLineInfos.insert(SectionLineInfos.end(),
2086 Lines.begin(), Lines.end());
2087 }
2088
Devang Patel53bb5c92009-11-10 23:06:00 +00002089 // Construct abstract scopes.
2090 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002091 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002092 constructScopeDIE(*AI);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002093
Devang Patel2c4ceb12009-11-21 02:48:08 +00002094 constructScopeDIE(CurrentFnDbgScope);
Devang Patel70d75ca2009-11-12 19:02:56 +00002095
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002096 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2097 MMI->getFrameMoves()));
2098
2099 // Clear debug info
Devang Patelc09ddc12009-12-01 18:13:48 +00002100 CurrentFnDbgScope = NULL;
2101 DbgScopeMap.clear();
2102 DbgScopeBeginMap.clear();
2103 DbgScopeEndMap.clear();
2104 ConcreteScopes.clear();
2105 AbstractScopesList.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002106
2107 Lines.clear();
Devang Patelc09ddc12009-12-01 18:13:48 +00002108
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002109 if (TimePassesIsEnabled)
2110 DebugTimer->stopTimer();
2111}
2112
Devang Patel2c4ceb12009-11-21 02:48:08 +00002113/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002114/// label. Returns a unique label ID used to generate a label and provide
2115/// correspondence to the source line list.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002116unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002117 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002118 if (!MMI)
2119 return 0;
2120
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002121 if (TimePassesIsEnabled)
2122 DebugTimer->startTimer();
2123
Devang Patel65dbc902009-11-25 17:36:49 +00002124 StringRef Dir;
2125 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002126
2127 DIDescriptor Scope(S);
2128 if (Scope.isCompileUnit()) {
2129 DICompileUnit CU(S);
2130 Dir = CU.getDirectory();
2131 Fn = CU.getFilename();
2132 } else if (Scope.isSubprogram()) {
2133 DISubprogram SP(S);
2134 Dir = SP.getDirectory();
2135 Fn = SP.getFilename();
2136 } else if (Scope.isLexicalBlock()) {
2137 DILexicalBlock DB(S);
2138 Dir = DB.getDirectory();
2139 Fn = DB.getFilename();
2140 } else
2141 assert (0 && "Unexpected scope info");
2142
2143 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002144 unsigned ID = MMI->NextLabelID();
2145 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2146
2147 if (TimePassesIsEnabled)
2148 DebugTimer->stopTimer();
2149
2150 return ID;
2151}
2152
2153/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2154/// timed. Look up the source id with the given directory and source file
2155/// names. If none currently exists, create a new id and insert it in the
2156/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2157/// well.
2158unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2159 const std::string &FileName) {
2160 if (TimePassesIsEnabled)
2161 DebugTimer->startTimer();
2162
Devang Patel5ccdd102009-09-29 18:40:58 +00002163 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002164
2165 if (TimePassesIsEnabled)
2166 DebugTimer->stopTimer();
2167
2168 return SrcId;
2169}
2170
Bill Wendling829e67b2009-05-20 23:22:40 +00002171//===----------------------------------------------------------------------===//
2172// Emit Methods
2173//===----------------------------------------------------------------------===//
2174
Devang Patel2c4ceb12009-11-21 02:48:08 +00002175/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002176///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002177unsigned
2178DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002179 // Get the children.
2180 const std::vector<DIE *> &Children = Die->getChildren();
2181
2182 // If not last sibling and has children then add sibling offset attribute.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002183 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002184
2185 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002186 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002187
2188 // Get the abbreviation for this DIE.
2189 unsigned AbbrevNumber = Die->getAbbrevNumber();
2190 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2191
2192 // Set DIE offset
2193 Die->setOffset(Offset);
2194
2195 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002196 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002197
2198 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2199 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2200
2201 // Size the DIE attribute values.
2202 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2203 // Size attribute value.
2204 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2205
2206 // Size the DIE children if any.
2207 if (!Children.empty()) {
2208 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2209 "Children flag not set");
2210
2211 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002212 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002213
2214 // End of children marker.
2215 Offset += sizeof(int8_t);
2216 }
2217
2218 Die->setSize(Offset - Die->getOffset());
2219 return Offset;
2220}
2221
Devang Patel2c4ceb12009-11-21 02:48:08 +00002222/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002223///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002224void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002225 // Compute size of compile unit header.
2226 static unsigned Offset =
2227 sizeof(int32_t) + // Length of Compilation Unit Info
2228 sizeof(int16_t) + // DWARF version number
2229 sizeof(int32_t) + // Offset Into Abbrev. Section
2230 sizeof(int8_t); // Pointer Size (in bytes)
2231
Devang Patel2c4ceb12009-11-21 02:48:08 +00002232 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel1dbc7712009-06-29 20:45:18 +00002233 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002234}
2235
Devang Patel2c4ceb12009-11-21 02:48:08 +00002236/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling94d04b82009-05-20 23:21:38 +00002237/// tools to recognize the object file contains Dwarf information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002238void DwarfDebug::emitInitial() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002239 // Check to see if we already emitted intial headers.
2240 if (didInitial) return;
2241 didInitial = true;
2242
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002243 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002244
Bill Wendling94d04b82009-05-20 23:21:38 +00002245 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002246 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002247 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002248 EmitLabel("section_debug_frame", 0);
2249 }
2250
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002251 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002252 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002253 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002254 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002255 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002256 EmitLabel("section_aranges", 0);
2257
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002258 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2259 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002260 EmitLabel("section_macinfo", 0);
2261 }
2262
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002263 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002264 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002265 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002266 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002267 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002268 EmitLabel("section_pubnames", 0);
Devang Patel193f7202009-11-24 01:14:22 +00002269 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2270 EmitLabel("section_pubtypes", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002271 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002272 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002273 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002274 EmitLabel("section_ranges", 0);
2275
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002276 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002277 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002278 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002279 EmitLabel("data_begin", 0);
2280}
2281
Devang Patel2c4ceb12009-11-21 02:48:08 +00002282/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002283///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002284void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002285 // Get the abbreviation for this DIE.
2286 unsigned AbbrevNumber = Die->getAbbrevNumber();
2287 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2288
2289 Asm->EOL();
2290
2291 // Emit the code (index) for the abbreviation.
2292 Asm->EmitULEB128Bytes(AbbrevNumber);
2293
2294 if (Asm->isVerbose())
2295 Asm->EOL(std::string("Abbrev [" +
2296 utostr(AbbrevNumber) +
2297 "] 0x" + utohexstr(Die->getOffset()) +
2298 ":0x" + utohexstr(Die->getSize()) + " " +
2299 dwarf::TagString(Abbrev->getTag())));
2300 else
2301 Asm->EOL();
2302
2303 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2304 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2305
2306 // Emit the DIE attribute values.
2307 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2308 unsigned Attr = AbbrevData[i].getAttribute();
2309 unsigned Form = AbbrevData[i].getForm();
2310 assert(Form && "Too many attributes for DIE (check abbreviation)");
2311
2312 switch (Attr) {
2313 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002314 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002315 break;
2316 case dwarf::DW_AT_abstract_origin: {
2317 DIEEntry *E = cast<DIEEntry>(Values[i]);
2318 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002319 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002320 Asm->EmitInt32(Addr);
2321 break;
2322 }
2323 default:
2324 // Emit an attribute using the defined form.
2325 Values[i]->EmitValue(this, Form);
2326 break;
2327 }
2328
2329 Asm->EOL(dwarf::AttributeString(Attr));
2330 }
2331
2332 // Emit the DIE children if any.
2333 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2334 const std::vector<DIE *> &Children = Die->getChildren();
2335
2336 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002337 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002338
2339 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2340 }
2341}
2342
Devang Patel2c4ceb12009-11-21 02:48:08 +00002343/// emitDebugInfo / emitDebugInfoPerCU - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002344///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002345void DwarfDebug::emitDebugInfoPerCU(CompileUnit *Unit) {
Devang Patel017d1212009-11-20 21:37:22 +00002346 DIE *Die = Unit->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002347
2348 // Emit the compile units header.
2349 EmitLabel("info_begin", Unit->getID());
2350
2351 // Emit size of content not including length itself
2352 unsigned ContentSize = Die->getSize() +
2353 sizeof(int16_t) + // DWARF version number
2354 sizeof(int32_t) + // Offset Into Abbrev. Section
2355 sizeof(int8_t) + // Pointer Size (in bytes)
2356 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2357
2358 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2359 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2360 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2361 Asm->EOL("Offset Into Abbrev. Section");
2362 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2363
Devang Patel2c4ceb12009-11-21 02:48:08 +00002364 emitDIE(Die);
Bill Wendling94d04b82009-05-20 23:21:38 +00002365 // FIXME - extra padding for gdb bug.
2366 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2367 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2368 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2369 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2370 EmitLabel("info_end", Unit->getID());
2371
2372 Asm->EOL();
2373}
2374
Devang Patel2c4ceb12009-11-21 02:48:08 +00002375void DwarfDebug::emitDebugInfo() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002376 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002377 Asm->OutStreamer.SwitchSection(
2378 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002379
Devang Patel2c4ceb12009-11-21 02:48:08 +00002380 emitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002381}
2382
Devang Patel2c4ceb12009-11-21 02:48:08 +00002383/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002384///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002385void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002386 // Check to see if it is worth the effort.
2387 if (!Abbreviations.empty()) {
2388 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002389 Asm->OutStreamer.SwitchSection(
2390 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002391
2392 EmitLabel("abbrev_begin", 0);
2393
2394 // For each abbrevation.
2395 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2396 // Get abbreviation data
2397 const DIEAbbrev *Abbrev = Abbreviations[i];
2398
2399 // Emit the abbrevations code (base 1 index.)
2400 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2401 Asm->EOL("Abbreviation Code");
2402
2403 // Emit the abbreviations data.
2404 Abbrev->Emit(Asm);
2405
2406 Asm->EOL();
2407 }
2408
2409 // Mark end of abbreviations.
2410 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2411
2412 EmitLabel("abbrev_end", 0);
2413 Asm->EOL();
2414 }
2415}
2416
Devang Patel2c4ceb12009-11-21 02:48:08 +00002417/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002418/// the line matrix.
2419///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002420void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002421 // Define last address of section.
2422 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2423 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2424 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2425 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2426
2427 // Mark end of matrix.
2428 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2429 Asm->EmitULEB128Bytes(1); Asm->EOL();
2430 Asm->EmitInt8(1); Asm->EOL();
2431}
2432
Devang Patel2c4ceb12009-11-21 02:48:08 +00002433/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002434///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002435void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002436 // If the target is using .loc/.file, the assembler will be emitting the
2437 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002438 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002439 return;
2440
2441 // Minimum line delta, thus ranging from -10..(255-10).
2442 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2443 // Maximum line delta, thus ranging from -10..(255-10).
2444 const int MaxLineDelta = 255 + MinLineDelta;
2445
2446 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002447 Asm->OutStreamer.SwitchSection(
2448 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002449
2450 // Construct the section header.
2451 EmitDifference("line_end", 0, "line_begin", 0, true);
2452 Asm->EOL("Length of Source Line Info");
2453 EmitLabel("line_begin", 0);
2454
2455 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2456
2457 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2458 Asm->EOL("Prolog Length");
2459 EmitLabel("line_prolog_begin", 0);
2460
2461 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2462
2463 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2464
2465 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2466
2467 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2468
2469 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2470
2471 // Line number standard opcode encodings argument count
2472 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2473 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2474 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2475 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2476 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2477 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2478 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2479 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2480 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2481
2482 // Emit directories.
2483 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2484 Asm->EmitString(getSourceDirectoryName(DI));
2485 Asm->EOL("Directory");
2486 }
2487
2488 Asm->EmitInt8(0); Asm->EOL("End of directories");
2489
2490 // Emit files.
2491 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2492 // Remember source id starts at 1.
2493 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2494 Asm->EmitString(getSourceFileName(Id.second));
2495 Asm->EOL("Source");
2496 Asm->EmitULEB128Bytes(Id.first);
2497 Asm->EOL("Directory #");
2498 Asm->EmitULEB128Bytes(0);
2499 Asm->EOL("Mod date");
2500 Asm->EmitULEB128Bytes(0);
2501 Asm->EOL("File size");
2502 }
2503
2504 Asm->EmitInt8(0); Asm->EOL("End of files");
2505
2506 EmitLabel("line_prolog_end", 0);
2507
2508 // A sequence for each text section.
2509 unsigned SecSrcLinesSize = SectionSourceLines.size();
2510
2511 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2512 // Isolate current sections line info.
2513 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2514
Chris Lattner93b6db32009-08-08 23:39:42 +00002515 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002516 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002517 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002518 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002519 }*/
2520 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002521
2522 // Dwarf assumes we start with first line of first source file.
2523 unsigned Source = 1;
2524 unsigned Line = 1;
2525
2526 // Construct rows of the address, source, line, column matrix.
2527 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2528 const SrcLineInfo &LineInfo = LineInfos[i];
2529 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2530 if (!LabelID) continue;
2531
Caroline Ticec6f9d622009-09-11 18:25:54 +00002532 if (LineInfo.getLine() == 0) continue;
2533
Bill Wendling94d04b82009-05-20 23:21:38 +00002534 if (!Asm->isVerbose())
2535 Asm->EOL();
2536 else {
2537 std::pair<unsigned, unsigned> SourceID =
2538 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002539 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002540 << getSourceDirectoryName(SourceID.first) << ' '
2541 << getSourceFileName(SourceID.second)
2542 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2543 }
2544
2545 // Define the line address.
2546 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2547 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2548 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2549 EmitReference("label", LabelID); Asm->EOL("Location label");
2550
2551 // If change of source, then switch to the new source.
2552 if (Source != LineInfo.getSourceID()) {
2553 Source = LineInfo.getSourceID();
2554 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2555 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2556 }
2557
2558 // If change of line.
2559 if (Line != LineInfo.getLine()) {
2560 // Determine offset.
2561 int Offset = LineInfo.getLine() - Line;
2562 int Delta = Offset - MinLineDelta;
2563
2564 // Update line.
2565 Line = LineInfo.getLine();
2566
2567 // If delta is small enough and in range...
2568 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2569 // ... then use fast opcode.
2570 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2571 } else {
2572 // ... otherwise use long hand.
2573 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2574 Asm->EOL("DW_LNS_advance_line");
2575 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2576 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2577 }
2578 } else {
2579 // Copy the previous row (different address or source)
2580 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2581 }
2582 }
2583
Devang Patel2c4ceb12009-11-21 02:48:08 +00002584 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002585 }
2586
2587 if (SecSrcLinesSize == 0)
2588 // Because we're emitting a debug_line section, we still need a line
2589 // table. The linker and friends expect it to exist. If there's nothing to
2590 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002591 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002592
2593 EmitLabel("line_end", 0);
2594 Asm->EOL();
2595}
2596
Devang Patel2c4ceb12009-11-21 02:48:08 +00002597/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002598///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002599void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002600 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002601 return;
2602
2603 int stackGrowth =
2604 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2605 TargetFrameInfo::StackGrowsUp ?
2606 TD->getPointerSize() : -TD->getPointerSize();
2607
2608 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002609 Asm->OutStreamer.SwitchSection(
2610 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002611
2612 EmitLabel("debug_frame_common", 0);
2613 EmitDifference("debug_frame_common_end", 0,
2614 "debug_frame_common_begin", 0, true);
2615 Asm->EOL("Length of Common Information Entry");
2616
2617 EmitLabel("debug_frame_common_begin", 0);
2618 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2619 Asm->EOL("CIE Identifier Tag");
2620 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2621 Asm->EOL("CIE Version");
2622 Asm->EmitString("");
2623 Asm->EOL("CIE Augmentation");
2624 Asm->EmitULEB128Bytes(1);
2625 Asm->EOL("CIE Code Alignment Factor");
2626 Asm->EmitSLEB128Bytes(stackGrowth);
2627 Asm->EOL("CIE Data Alignment Factor");
2628 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2629 Asm->EOL("CIE RA Column");
2630
2631 std::vector<MachineMove> Moves;
2632 RI->getInitialFrameState(Moves);
2633
2634 EmitFrameMoves(NULL, 0, Moves, false);
2635
2636 Asm->EmitAlignment(2, 0, 0, false);
2637 EmitLabel("debug_frame_common_end", 0);
2638
2639 Asm->EOL();
2640}
2641
Devang Patel2c4ceb12009-11-21 02:48:08 +00002642/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00002643/// section.
2644void
Devang Patel2c4ceb12009-11-21 02:48:08 +00002645DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002646 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002647 return;
2648
2649 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002650 Asm->OutStreamer.SwitchSection(
2651 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002652
2653 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2654 "debug_frame_begin", DebugFrameInfo.Number, true);
2655 Asm->EOL("Length of Frame Information Entry");
2656
2657 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2658
2659 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2660 0, 0, true, false);
2661 Asm->EOL("FDE CIE offset");
2662
2663 EmitReference("func_begin", DebugFrameInfo.Number);
2664 Asm->EOL("FDE initial location");
2665 EmitDifference("func_end", DebugFrameInfo.Number,
2666 "func_begin", DebugFrameInfo.Number);
2667 Asm->EOL("FDE address range");
2668
2669 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2670 false);
2671
2672 Asm->EmitAlignment(2, 0, 0, false);
2673 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2674
2675 Asm->EOL();
2676}
2677
Devang Patel2c4ceb12009-11-21 02:48:08 +00002678void DwarfDebug::emitDebugPubNamesPerCU(CompileUnit *Unit) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002679 EmitDifference("pubnames_end", Unit->getID(),
2680 "pubnames_begin", Unit->getID(), true);
2681 Asm->EOL("Length of Public Names Info");
2682
2683 EmitLabel("pubnames_begin", Unit->getID());
2684
2685 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2686
2687 EmitSectionOffset("info_begin", "section_info",
2688 Unit->getID(), 0, true, false);
2689 Asm->EOL("Offset of Compilation Unit Info");
2690
2691 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2692 true);
2693 Asm->EOL("Compilation Unit Length");
2694
Devang Patel193f7202009-11-24 01:14:22 +00002695 const StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling94d04b82009-05-20 23:21:38 +00002696 for (StringMap<DIE*>::const_iterator
2697 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2698 const char *Name = GI->getKeyData();
2699 DIE * Entity = GI->second;
2700
2701 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2702 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2703 }
2704
2705 Asm->EmitInt32(0); Asm->EOL("End Mark");
2706 EmitLabel("pubnames_end", Unit->getID());
2707
2708 Asm->EOL();
2709}
2710
Devang Patel2c4ceb12009-11-21 02:48:08 +00002711/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002712///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002713void DwarfDebug::emitDebugPubNames() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002714 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002715 Asm->OutStreamer.SwitchSection(
2716 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002717
Devang Patel2c4ceb12009-11-21 02:48:08 +00002718 emitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002719}
2720
Devang Patel193f7202009-11-24 01:14:22 +00002721void DwarfDebug::emitDebugPubTypes() {
Devang Patelf3a03762009-11-24 19:18:41 +00002722 // Start the dwarf pubnames section.
2723 Asm->OutStreamer.SwitchSection(
2724 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patel193f7202009-11-24 01:14:22 +00002725 EmitDifference("pubtypes_end", ModuleCU->getID(),
2726 "pubtypes_begin", ModuleCU->getID(), true);
2727 Asm->EOL("Length of Public Types Info");
2728
2729 EmitLabel("pubtypes_begin", ModuleCU->getID());
2730
2731 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2732
2733 EmitSectionOffset("info_begin", "section_info",
2734 ModuleCU->getID(), 0, true, false);
2735 Asm->EOL("Offset of Compilation ModuleCU Info");
2736
2737 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2738 true);
2739 Asm->EOL("Compilation ModuleCU Length");
2740
2741 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2742 for (StringMap<DIE*>::const_iterator
2743 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2744 const char *Name = GI->getKeyData();
2745 DIE * Entity = GI->second;
2746
2747 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2748 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2749 }
2750
2751 Asm->EmitInt32(0); Asm->EOL("End Mark");
2752 EmitLabel("pubtypes_end", ModuleCU->getID());
2753
2754 Asm->EOL();
2755}
2756
Devang Patel2c4ceb12009-11-21 02:48:08 +00002757/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002758///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002759void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002760 // Check to see if it is worth the effort.
2761 if (!StringPool.empty()) {
2762 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002763 Asm->OutStreamer.SwitchSection(
2764 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002765
2766 // For each of strings in the string pool.
2767 for (unsigned StringID = 1, N = StringPool.size();
2768 StringID <= N; ++StringID) {
2769 // Emit a label for reference from debug information entries.
2770 EmitLabel("string", StringID);
2771
2772 // Emit the string itself.
2773 const std::string &String = StringPool[StringID];
2774 Asm->EmitString(String); Asm->EOL();
2775 }
2776
2777 Asm->EOL();
2778 }
2779}
2780
Devang Patel2c4ceb12009-11-21 02:48:08 +00002781/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002782///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002783void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002784 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002785 Asm->OutStreamer.SwitchSection(
2786 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002787 Asm->EOL();
2788}
2789
2790/// EmitDebugARanges - Emit visible names into a debug aranges section.
2791///
2792void DwarfDebug::EmitDebugARanges() {
2793 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002794 Asm->OutStreamer.SwitchSection(
2795 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002796
2797 // FIXME - Mock up
2798#if 0
2799 CompileUnit *Unit = GetBaseCompileUnit();
2800
2801 // Don't include size of length
2802 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2803
2804 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2805
2806 EmitReference("info_begin", Unit->getID());
2807 Asm->EOL("Offset of Compilation Unit Info");
2808
2809 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2810
2811 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2812
2813 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2814 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2815
2816 // Range 1
2817 EmitReference("text_begin", 0); Asm->EOL("Address");
2818 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2819
2820 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2821 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2822#endif
2823
2824 Asm->EOL();
2825}
2826
Devang Patel2c4ceb12009-11-21 02:48:08 +00002827/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002828///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002829void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002830 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002831 Asm->OutStreamer.SwitchSection(
2832 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002833 Asm->EOL();
2834}
2835
Devang Patel2c4ceb12009-11-21 02:48:08 +00002836/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002837///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002838void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002839 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002840 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002841 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002842 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002843 Asm->EOL();
2844 }
2845}
2846
Devang Patel2c4ceb12009-11-21 02:48:08 +00002847/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002848/// Section Header:
2849/// 1. length of section
2850/// 2. Dwarf version number
2851/// 3. address size.
2852///
2853/// Entries (one "entry" for each function that was inlined):
2854///
2855/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2856/// otherwise offset into __debug_str for regular function name.
2857/// 2. offset into __debug_str section for regular function name.
2858/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2859/// instances for the function.
2860///
2861/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2862/// inlined instance; the die_offset points to the inlined_subroutine die in the
2863/// __debug_info section, and the low_pc is the starting address for the
2864/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002865void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002866 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002867 return;
2868
Devang Patel1dbc7712009-06-29 20:45:18 +00002869 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002870 return;
2871
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002872 Asm->OutStreamer.SwitchSection(
2873 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002874 Asm->EOL();
2875 EmitDifference("debug_inlined_end", 1,
2876 "debug_inlined_begin", 1, true);
2877 Asm->EOL("Length of Debug Inlined Information Entry");
2878
2879 EmitLabel("debug_inlined_begin", 1);
2880
2881 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2882 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2883
Devang Patel53bb5c92009-11-10 23:06:00 +00002884 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2885 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002886
Devang Patel53bb5c92009-11-10 23:06:00 +00002887// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2888 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2889 MDNode *Node = *I;
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002890 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2891 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002892 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002893 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002894 StringRef LName = SP.getLinkageName();
2895 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002896
Devang Patel65dbc902009-11-25 17:36:49 +00002897 if (LName.empty())
Devang Patel53cb17d2009-07-16 01:01:22 +00002898 Asm->EmitString(Name);
2899 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002900 // Skip special LLVM prefix that is used to inform the asm printer to not
2901 // emit usual symbol prefix before the symbol name. This happens for
2902 // Objective-C symbol names and symbol whose name is replaced using GCC's
2903 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002904 if (LName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00002905 LName = LName.substr(1);
Devang Patel53bb5c92009-11-10 23:06:00 +00002906// Asm->EmitString(LName);
2907 EmitSectionOffset("string", "section_str",
2908 StringPool.idFor(LName), false, true);
2909
Devang Patel53cb17d2009-07-16 01:01:22 +00002910 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002911 Asm->EOL("MIPS linkage name");
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002912// Asm->EmitString(Name);
Devang Patel53bb5c92009-11-10 23:06:00 +00002913 EmitSectionOffset("string", "section_str",
2914 StringPool.idFor(Name), false, true);
2915 Asm->EOL("Function name");
Bill Wendling94d04b82009-05-20 23:21:38 +00002916 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2917
Devang Patel53bb5c92009-11-10 23:06:00 +00002918 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002919 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002920 DIE *SP = LI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00002921 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2922
2923 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002924 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002925 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002926 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002927
Devang Patel53bb5c92009-11-10 23:06:00 +00002928 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling94d04b82009-05-20 23:21:38 +00002929 }
2930 }
2931
2932 EmitLabel("debug_inlined_end", 1);
2933 Asm->EOL();
2934}