blob: f32dea9ebf2fa468cb2002fc157414f73312594d [file] [log] [blame]
Bill Wendlingb12b3d72009-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 Patel15e723d2009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greened87baff2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner73266f92009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Daniel Dunbarc74255d2009-10-13 06:47:08 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
Chris Lattner5632fab2009-09-16 00:08:41 +000028#include "llvm/Support/Mangler.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000029#include "llvm/Support/Timer.h"
30#include "llvm/System/Path.h"
Bill Wendlingb12b3d72009-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 Wendlingb12b3d72009-05-15 09:23:25 +000042static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-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 Lewyckyee68f452009-11-17 08:11:44 +000049class CompileUnit {
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +000056 DIE *CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000057
Devang Patel1233f912009-11-21 00:31:03 +000058 /// IndexTyDie - An anonymous type for index type.
59 DIE *IndexTyDie;
60
Bill Wendlingb12b3d72009-05-15 09:23:25 +000061 /// GVToDieMap - Tracks the mapping of unit level debug informaton
62 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000063 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Pateld90672c2009-11-20 21:37:22 +000064 ValueMap<MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-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 Patel15e723d2009-08-28 23:24:31 +000068 /// FIXME : Rename
Devang Pateld90672c2009-11-20 21:37:22 +000069 ValueMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-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 Patelec13b4f2009-11-24 01:14:22 +000075 /// GlobalTypes - A map of globally visible types for this unit.
76 ///
77 StringMap<DIE*> GlobalTypes;
78
Bill Wendlingb12b3d72009-05-15 09:23:25 +000079public:
80 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +000081 : ID(I), CUDie(D), IndexTyDie(0) {}
82 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000083
84 // Accessors.
Devang Patelec13b4f2009-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 Wendlingb12b3d72009-05-15 09:23:25 +000089
90 /// hasContent - Return true if this compile unit has something to write out.
91 ///
Devang Patelc50078e2009-11-21 02:48:08 +000092 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000093
Devang Patelc50078e2009-11-21 02:48:08 +000094 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +000095 ///
Devang Patelc50078e2009-11-21 02:48:08 +000096 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000097
Devang Patelec13b4f2009-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 Pateld90672c2009-11-20 21:37:22 +0000104 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000105 /// specified debug variable.
Devang Pateld90672c2009-11-20 21:37:22 +0000106 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +0000107
Devang Pateld90672c2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000112
Devang Pateld90672c2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000120 }
121
Devang Patelc50078e2009-11-21 02:48:08 +0000122 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000123 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000124 void addDie(DIE *Buffer) {
125 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000126 }
Devang Patel1233f912009-11-21 00:31:03 +0000127
128 // getIndexTyDie - Get an anonymous type for index type.
129 DIE *getIndexTyDie() {
130 return IndexTyDie;
131 }
132
Jim Grosbachb23f2422009-11-22 19:20:36 +0000133 // setIndexTyDie - Set D as anonymous type for index which can be reused
134 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000135 void setIndexTyDie(DIE *D) {
136 IndexTyDie = D;
137 }
138
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000139};
140
141//===----------------------------------------------------------------------===//
142/// DbgVariable - This class is used to track local variable information.
143///
Devang Patel4cb32c32009-11-16 21:53:40 +0000144class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000145 DIVariable Var; // Variable Descriptor.
146 unsigned FrameIndex; // Variable frame index.
Devang Patel90a0fe32009-11-10 23:06:00 +0000147 DbgVariable *AbstractVar; // Abstract variable for this variable.
148 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000149public:
Devang Patel90a0fe32009-11-10 23:06:00 +0000150 DbgVariable(DIVariable V, unsigned I)
151 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000152
153 // Accessors.
Devang Patel90a0fe32009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000160};
161
162//===----------------------------------------------------------------------===//
163/// DbgScope - This class is used to track scope information.
164///
Devang Patel4cb32c32009-11-16 21:53:40 +0000165class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000166 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000167 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel90a0fe32009-11-10 23:06:00 +0000168 WeakVH InlinedAtLocation; // Location at which scope is inlined.
169 bool AbstractScope; // Abstract Scope
Bill Wendlingb12b3d72009-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 Patel90ecd192009-10-01 18:25:23 +0000172 const MachineInstr *LastInsn; // Last instruction of this scope.
173 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000174 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
175 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbar41716322009-09-19 20:40:05 +0000176
Owen Anderson696d4862009-06-24 22:53:20 +0000177 // Private state for dump()
178 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000179public:
Devang Pateldd7bb432009-10-14 21:08:09 +0000180 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000181 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach652b7432009-11-21 23:12:12 +0000182 StartLabelID(0), EndLabelID(0),
Devang Pateldd7bb432009-10-14 21:08:09 +0000183 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000184 virtual ~DbgScope();
185
186 // Accessors.
187 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000188 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000189 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach652b7432009-11-21 23:12:12 +0000190 MDNode *getInlinedAt() const {
Devang Patel90a0fe32009-11-10 23:06:00 +0000191 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Pateldd7bb432009-10-14 21:08:09 +0000192 }
Devang Patel90a0fe32009-11-10 23:06:00 +0000193 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendlingb12b3d72009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000198 void setStartLabelID(unsigned S) { StartLabelID = S; }
199 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Patel90ecd192009-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 Patel90a0fe32009-11-10 23:06:00 +0000203 void setAbstractScope() { AbstractScope = true; }
204 bool isAbstractScope() const { return AbstractScope; }
Devang Patel90ecd192009-10-01 18:25:23 +0000205 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000206
Devang Patelc50078e2009-11-21 02:48:08 +0000207 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000208 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000209 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000210
Devang Patelc50078e2009-11-21 02:48:08 +0000211 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000212 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000213 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000214
Devang Patelc50078e2009-11-21 02:48:08 +0000215 void fixInstructionMarkers() {
Devang Patel6a260102009-10-01 20:31:14 +0000216 assert (getFirstInsn() && "First instruction is missing!");
217 if (getLastInsn())
218 return;
Jim Grosbach652b7432009-11-21 23:12:12 +0000219
Devang Patel6a260102009-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 Patelc50078e2009-11-21 02:48:08 +0000226 L->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +0000227 setLastInsn(L->getLastInsn());
228 }
229
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000230#ifndef NDEBUG
231 void dump() const;
232#endif
233};
234
235#ifndef NDEBUG
236void DbgScope::dump() const {
Chris Lattnerebb8c082009-08-23 00:51:00 +0000237 raw_ostream &err = errs();
238 err.indent(IndentLevel);
Devang Patel90a0fe32009-11-10 23:06:00 +0000239 MDNode *N = Desc.getNode();
240 N->dump();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000241 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel90a0fe32009-11-10 23:06:00 +0000242 if (AbstractScope)
243 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000244
245 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000246 if (!Scopes.empty())
247 err << "Children ...\n";
Bill Wendlingb12b3d72009-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 Wendlingb12b3d72009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000261}
262
263} // end llvm namespace
264
Chris Lattner621c44d2009-08-22 20:48:53 +0000265DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel5a3d37f2009-06-29 20:45:18 +0000266 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000267 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patelc50078e2009-11-21 02:48:08 +0000268 DIEValues(), StringPool(),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000269 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel90a0fe32009-11-10 23:06:00 +0000270 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000271 if (TimePassesIsEnabled)
272 DebugTimer = new Timer("Dwarf Debug Writer",
273 getDwarfTimerGroup());
274}
275DwarfDebug::~DwarfDebug() {
Devang Patelc50078e2009-11-21 02:48:08 +0000276 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
277 delete DIEValues[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000278
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000279 delete DebugTimer;
280}
281
Devang Patelc50078e2009-11-21 02:48:08 +0000282/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000283///
Devang Patelc50078e2009-11-21 02:48:08 +0000284void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +0000305/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000306/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000307DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel1233f912009-11-21 00:31:03 +0000308 DIEEntry *Value = new DIEEntry(Entry);
Devang Patelc50078e2009-11-21 02:48:08 +0000309 DIEValues.push_back(Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000310 return Value;
311}
312
Devang Patelc50078e2009-11-21 02:48:08 +0000313/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000314///
Devang Patelc50078e2009-11-21 02:48:08 +0000315void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000316 unsigned Form, uint64_t Integer) {
317 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000318 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000319 DIEValues.push_back(Value);
320 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000321}
322
Devang Patelc50078e2009-11-21 02:48:08 +0000323/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000324///
Devang Patelc50078e2009-11-21 02:48:08 +0000325void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000326 unsigned Form, int64_t Integer) {
327 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000328 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000329 DIEValues.push_back(Value);
330 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000331}
332
Devang Pateldf0f2152009-12-02 15:25:16 +0000333/// addString - Add a string attribute data and value. DIEString only
334/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000335void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patelc10337d2009-11-24 19:42:17 +0000336 const StringRef String) {
Devang Patel1233f912009-11-21 00:31:03 +0000337 DIEValue *Value = new DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000338 DIEValues.push_back(Value);
339 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000340}
341
Devang Patelc50078e2009-11-21 02:48:08 +0000342/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000343///
Devang Patelc50078e2009-11-21 02:48:08 +0000344void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000345 const DWLabel &Label) {
Devang Patel1233f912009-11-21 00:31:03 +0000346 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000347 DIEValues.push_back(Value);
348 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000349}
350
Devang Patelc50078e2009-11-21 02:48:08 +0000351/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000352///
Devang Patelc50078e2009-11-21 02:48:08 +0000353void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000354 const std::string &Label) {
Devang Patel1233f912009-11-21 00:31:03 +0000355 DIEValue *Value = new DIEObjectLabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000356 DIEValues.push_back(Value);
357 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000358}
359
Devang Patelc50078e2009-11-21 02:48:08 +0000360/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000361///
Devang Patelc50078e2009-11-21 02:48:08 +0000362void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000363 const DWLabel &Label, const DWLabel &Section,
364 bool isEH, bool useSet) {
Devang Patel1233f912009-11-21 00:31:03 +0000365 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
Devang Patelc50078e2009-11-21 02:48:08 +0000366 DIEValues.push_back(Value);
367 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000368}
369
Devang Patelc50078e2009-11-21 02:48:08 +0000370/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000371///
Devang Patelc50078e2009-11-21 02:48:08 +0000372void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000373 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel1233f912009-11-21 00:31:03 +0000374 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000375 DIEValues.push_back(Value);
376 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000377}
378
Devang Patelc50078e2009-11-21 02:48:08 +0000379/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000380///
Devang Patelc50078e2009-11-21 02:48:08 +0000381void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000382 DIEBlock *Block) {
383 Block->ComputeSize(TD);
Devang Patelc50078e2009-11-21 02:48:08 +0000384 DIEValues.push_back(Block);
385 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000386}
387
Devang Patelc50078e2009-11-21 02:48:08 +0000388/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000389/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000390void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +0000396 unsigned FileID = findCompileUnit(V->getCompileUnit()).getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000397 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000400}
401
Devang Patelc50078e2009-11-21 02:48:08 +0000402/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000403/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000404void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +0000410 unsigned FileID = findCompileUnit(G->getCompileUnit()).getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000411 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000414}
Devang Patel318d70d2009-08-31 22:47:13 +0000415
Devang Patelc50078e2009-11-21 02:48:08 +0000416/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000417/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000418void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-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 Tice9da96d82009-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 Patel318d70d2009-08-31 22:47:13 +0000426
427 unsigned Line = SP->getLineNumber();
Devang Patelc50078e2009-11-21 02:48:08 +0000428 unsigned FileID = findCompileUnit(SP->getCompileUnit()).getID();
Devang Patel318d70d2009-08-31 22:47:13 +0000429 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-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 Patel318d70d2009-08-31 22:47:13 +0000432}
433
Devang Patelc50078e2009-11-21 02:48:08 +0000434/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000435/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000436void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +0000443 unsigned FileID = findCompileUnit(CU).getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000444 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000447}
448
Devang Patel641f8202009-12-07 21:41:32 +0000449/// addSourceLine - Add location information to specified debug information
450/// entry.
451void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
452 // If there is no compile unit specified, don't add a line #.
453 if (NS->getCompileUnit().isNull())
454 return;
455
456 unsigned Line = NS->getLineNumber();
457 StringRef FN = NS->getFilename();
458 StringRef Dir = NS->getDirectory();
459
460 unsigned FileID = GetOrCreateSourceID(Dir, FN);
461 assert(FileID && "Invalid file id");
462 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
463 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
464}
465
Caroline Tice248d5572009-08-31 21:19:37 +0000466/* Byref variables, in Blocks, are declared by the programmer as
467 "SomeType VarName;", but the compiler creates a
468 __Block_byref_x_VarName struct, and gives the variable VarName
469 either the struct, or a pointer to the struct, as its type. This
470 is necessary for various behind-the-scenes things the compiler
471 needs to do with by-reference variables in blocks.
472
473 However, as far as the original *programmer* is concerned, the
474 variable should still have type 'SomeType', as originally declared.
475
476 The following function dives into the __Block_byref_x_VarName
477 struct to find the original type of the variable. This will be
478 passed back to the code generating the type for the Debug
479 Information Entry for the variable 'VarName'. 'VarName' will then
480 have the original type 'SomeType' in its debug information.
481
482 The original type 'SomeType' will be the type of the field named
483 'VarName' inside the __Block_byref_x_VarName struct.
484
485 NOTE: In order for this to not completely fail on the debugger
486 side, the Debug Information Entry for the variable VarName needs to
487 have a DW_AT_location that tells the debugger how to unwind through
488 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000489 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000490
491/// Find the type the programmer originally declared the variable to be
492/// and return that type.
493///
Devang Patelc50078e2009-11-21 02:48:08 +0000494DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000495
496 DIType subType = Ty;
497 unsigned tag = Ty.getTag();
498
499 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000500 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000501 subType = DTy.getTypeDerivedFrom();
502 }
503
504 DICompositeType blockStruct = DICompositeType(subType.getNode());
505
506 DIArray Elements = blockStruct.getTypeArray();
507
508 if (Elements.isNull())
509 return Ty;
510
511 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
512 DIDescriptor Element = Elements.getElement(i);
513 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000514 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000515 return (DT.getTypeDerivedFrom());
516 }
517
518 return Ty;
519}
520
Devang Patelc50078e2009-11-21 02:48:08 +0000521/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000522/// and generate the DWARF information necessary to find the actual variable
523/// given the extra address information encoded in the DIVariable, starting from
524/// the starting location. Add the DWARF information to the die.
525///
Devang Patelc50078e2009-11-21 02:48:08 +0000526void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000527 unsigned Attribute,
528 const MachineLocation &Location) {
529 const DIVariable &VD = DV->getVariable();
530 DIType Ty = VD.getType();
531
532 // Decode the original location, and use that as the start of the byref
533 // variable's location.
534 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
535 DIEBlock *Block = new DIEBlock();
536
537 if (Location.isReg()) {
538 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000539 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000540 } else {
541 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000542 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
543 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000544 }
545 } else {
546 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000547 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000548 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000549 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
550 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000551 }
552
Devang Patelc50078e2009-11-21 02:48:08 +0000553 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000554 }
555
556 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
557 uint64_t Element = VD.getAddrElement(i);
558
559 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000560 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
561 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000562 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000563 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000564 } else llvm_unreachable("unknown DIFactory Opcode");
565 }
566
567 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000568 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000569}
570
Caroline Tice248d5572009-08-31 21:19:37 +0000571/* Byref variables, in Blocks, are declared by the programmer as "SomeType
572 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
573 gives the variable VarName either the struct, or a pointer to the struct, as
574 its type. This is necessary for various behind-the-scenes things the
575 compiler needs to do with by-reference variables in Blocks.
576
577 However, as far as the original *programmer* is concerned, the variable
578 should still have type 'SomeType', as originally declared.
579
Devang Patelc50078e2009-11-21 02:48:08 +0000580 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000581 struct to find the original type of the variable, which is then assigned to
582 the variable's Debug Information Entry as its real type. So far, so good.
583 However now the debugger will expect the variable VarName to have the type
584 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000585 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000586 pointers and struct to find the actual variable of type SomeType.
587
588 The following function does just that. We start by getting
589 the "normal" location for the variable. This will be the location
590 of either the struct __Block_byref_x_VarName or the pointer to the
591 struct __Block_byref_x_VarName.
592
593 The struct will look something like:
594
595 struct __Block_byref_x_VarName {
596 ... <various fields>
597 struct __Block_byref_x_VarName *forwarding;
598 ... <various other fields>
599 SomeType VarName;
600 ... <maybe more fields>
601 };
602
603 If we are given the struct directly (as our starting point) we
604 need to tell the debugger to:
605
606 1). Add the offset of the forwarding field.
607
608 2). Follow that pointer to get the the real __Block_byref_x_VarName
609 struct to use (the real one may have been copied onto the heap).
610
611 3). Add the offset for the field VarName, to find the actual variable.
612
613 If we started with a pointer to the struct, then we need to
614 dereference that pointer first, before the other steps.
615 Translating this into DWARF ops, we will need to append the following
616 to the current location description for the variable:
617
618 DW_OP_deref -- optional, if we start with a pointer
619 DW_OP_plus_uconst <forward_fld_offset>
620 DW_OP_deref
621 DW_OP_plus_uconst <varName_fld_offset>
622
623 That is what this function does. */
624
Devang Patelc50078e2009-11-21 02:48:08 +0000625/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000626/// provided, and generate the DWARF information necessary to find the
627/// actual Block variable (navigating the Block struct) based on the
628/// starting location. Add the DWARF information to the die. For
629/// more information, read large comment just above here.
630///
Devang Patelc50078e2009-11-21 02:48:08 +0000631void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000632 unsigned Attribute,
633 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000634 const DIVariable &VD = DV->getVariable();
635 DIType Ty = VD.getType();
636 DIType TmpTy = Ty;
637 unsigned Tag = Ty.getTag();
638 bool isPointer = false;
639
Devang Patel7f75bbe2009-11-25 17:36:49 +0000640 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000641
642 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000643 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000644 TmpTy = DTy.getTypeDerivedFrom();
645 isPointer = true;
646 }
647
648 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
649
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000650 // Find the __forwarding field and the variable field in the __Block_byref
651 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000652 DIArray Fields = blockStruct.getTypeArray();
653 DIDescriptor varField = DIDescriptor();
654 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000655
656
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000657 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
658 DIDescriptor Element = Fields.getElement(i);
659 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000660 StringRef fieldName = DT.getName();
661 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000662 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000663 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000664 varField = Element;
665 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000666
Mike Stump2fd84e22009-09-24 23:21:26 +0000667 assert(!varField.isNull() && "Can't find byref variable in Block struct");
668 assert(!forwardingField.isNull()
669 && "Can't find forwarding field in Block struct");
Caroline Tice248d5572009-08-31 21:19:37 +0000670
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000671 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000672 unsigned int forwardingFieldOffset =
673 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
674 unsigned int varFieldOffset =
675 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000676
Mike Stump2fd84e22009-09-24 23:21:26 +0000677 // Decode the original location, and use that as the start of the byref
678 // variable's location.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000679 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
680 DIEBlock *Block = new DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000681
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000682 if (Location.isReg()) {
683 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000684 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000685 else {
686 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000687 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
688 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000689 }
690 } else {
691 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000692 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000693 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000694 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
695 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000696 }
Caroline Tice248d5572009-08-31 21:19:37 +0000697
Devang Patelc50078e2009-11-21 02:48:08 +0000698 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000699 }
Caroline Tice248d5572009-08-31 21:19:37 +0000700
Mike Stump2fd84e22009-09-24 23:21:26 +0000701 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000702 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000703 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000704 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000705
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000706 // Next add the offset for the '__forwarding' field:
707 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
708 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000709 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000710 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
711 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000712 }
Caroline Tice248d5572009-08-31 21:19:37 +0000713
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000714 // Now dereference the __forwarding field to get to the real __Block_byref
715 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000716 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000717
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000718 // Now that we've got the real __Block_byref... struct, add the offset
719 // for the variable's field to get to the location of the actual variable:
720 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000721 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000722 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
723 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000724 }
Caroline Tice248d5572009-08-31 21:19:37 +0000725
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000726 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000727 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000728}
729
Devang Patelc50078e2009-11-21 02:48:08 +0000730/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000731/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000732void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000733 const MachineLocation &Location) {
734 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
735 DIEBlock *Block = new DIEBlock();
736
737 if (Location.isReg()) {
738 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000739 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000740 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000741 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
742 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000743 }
744 } else {
745 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000746 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000747 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000748 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
749 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000750 }
751
Devang Patelc50078e2009-11-21 02:48:08 +0000752 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000753 }
754
Devang Patelc50078e2009-11-21 02:48:08 +0000755 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000756}
757
Devang Patelc50078e2009-11-21 02:48:08 +0000758/// addType - Add a new type attribute to the specified entity.
759void DwarfDebug::addType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000760 if (Ty.isNull())
761 return;
762
763 // Check for pre-existence.
Devang Patelc50078e2009-11-21 02:48:08 +0000764 DIEEntry *Entry = DW_Unit->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000765
766 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000767 if (Entry) {
768 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000769 return;
770 }
771
772 // Set up proxy.
Devang Patelc50078e2009-11-21 02:48:08 +0000773 Entry = createDIEEntry();
774 DW_Unit->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000775
776 // Construct type.
Devang Patel1233f912009-11-21 00:31:03 +0000777 DIE *Buffer = new DIE(dwarf::DW_TAG_base_type);
Devang Patel4285ddb2009-12-03 23:46:57 +0000778 ModuleCU->insertDIE(Ty.getNode(), Buffer);
Devang Patel56843af2009-08-31 18:49:10 +0000779 if (Ty.isBasicType())
Devang Patelc50078e2009-11-21 02:48:08 +0000780 constructTypeDIE(DW_Unit, *Buffer, DIBasicType(Ty.getNode()));
Devang Patel56843af2009-08-31 18:49:10 +0000781 else if (Ty.isCompositeType())
Devang Patelc50078e2009-11-21 02:48:08 +0000782 constructTypeDIE(DW_Unit, *Buffer, DICompositeType(Ty.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000783 else {
Devang Patel56843af2009-08-31 18:49:10 +0000784 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patelc50078e2009-11-21 02:48:08 +0000785 constructTypeDIE(DW_Unit, *Buffer, DIDerivedType(Ty.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000786 }
787
788 // Add debug information entry to entity and appropriate context.
789 DIE *Die = NULL;
790 DIDescriptor Context = Ty.getContext();
Devang Patel641f8202009-12-07 21:41:32 +0000791 if (!Context.isNull()) {
792 if (Context.isNameSpace()) {
793 DINameSpace NS(Context.getNode());
794 Die = getOrCreateNameSpace(NS);
795 } else
796 Die = DW_Unit->getDIE(Context.getNode());
797 }
Jim Grosbach652b7432009-11-21 23:12:12 +0000798 if (Die)
Devang Patelc50078e2009-11-21 02:48:08 +0000799 Die->addChild(Buffer);
Jim Grosbach652b7432009-11-21 23:12:12 +0000800 else
Devang Patelc50078e2009-11-21 02:48:08 +0000801 DW_Unit->addDie(Buffer);
802 Entry->setEntry(Buffer);
803 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000804}
805
Devang Patelc50078e2009-11-21 02:48:08 +0000806/// constructTypeDIE - Construct basic type die from DIBasicType.
807void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000808 DIBasicType BTy) {
809 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000810 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000811 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000812 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000813 BTy.getEncoding());
814
815 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000816 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000817 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000818 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000819 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000820}
821
Devang Patelc50078e2009-11-21 02:48:08 +0000822/// constructTypeDIE - Construct derived type die from DIDerivedType.
823void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000824 DIDerivedType DTy) {
825 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000826 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000827 uint64_t Size = DTy.getSizeInBits() >> 3;
828 unsigned Tag = DTy.getTag();
829
830 // FIXME - Workaround for templates.
831 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
832
833 Buffer.setTag(Tag);
834
835 // Map to main type, void will not have a type.
836 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelc50078e2009-11-21 02:48:08 +0000837 addType(DW_Unit, &Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000838
839 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000840 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000841 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000842
843 // Add size if non-zero (derived types might be zero-sized.)
844 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000845 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000846
847 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000848 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000849 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000850}
851
Devang Patelc50078e2009-11-21 02:48:08 +0000852/// constructTypeDIE - Construct type DIE from DICompositeType.
853void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000854 DICompositeType CTy) {
855 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000856 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000857
858 uint64_t Size = CTy.getSizeInBits() >> 3;
859 unsigned Tag = CTy.getTag();
860 Buffer.setTag(Tag);
861
862 switch (Tag) {
863 case dwarf::DW_TAG_vector_type:
864 case dwarf::DW_TAG_array_type:
Devang Patelc50078e2009-11-21 02:48:08 +0000865 constructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000866 break;
867 case dwarf::DW_TAG_enumeration_type: {
868 DIArray Elements = CTy.getTypeArray();
869
870 // Add enumerators to enumeration type.
871 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
872 DIE *ElemDie = NULL;
Devang Patel15e723d2009-08-28 23:24:31 +0000873 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelfb812752009-10-09 17:51:49 +0000874 if (!Enum.isNull()) {
Devang Patelc50078e2009-11-21 02:48:08 +0000875 ElemDie = constructEnumTypeDIE(DW_Unit, &Enum);
876 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000877 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000878 }
879 }
880 break;
881 case dwarf::DW_TAG_subroutine_type: {
882 // Add return type.
883 DIArray Elements = CTy.getTypeArray();
884 DIDescriptor RTy = Elements.getElement(0);
Devang Patelc50078e2009-11-21 02:48:08 +0000885 addType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000886
887 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000888 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000889
890 // Add arguments.
891 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
892 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
893 DIDescriptor Ty = Elements.getElement(i);
Devang Patelc50078e2009-11-21 02:48:08 +0000894 addType(DW_Unit, Arg, DIType(Ty.getNode()));
895 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000896 }
897 }
898 break;
899 case dwarf::DW_TAG_structure_type:
900 case dwarf::DW_TAG_union_type:
901 case dwarf::DW_TAG_class_type: {
902 // Add elements to structure type.
903 DIArray Elements = CTy.getTypeArray();
904
905 // A forward struct declared type may not have elements available.
906 if (Elements.isNull())
907 break;
908
909 // Add elements to structure type.
910 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
911 DIDescriptor Element = Elements.getElement(i);
Devang Patel15e723d2009-08-28 23:24:31 +0000912 if (Element.isNull())
913 continue;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000914 DIE *ElemDie = NULL;
915 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patelc1df8792009-12-03 01:25:38 +0000916 ElemDie = createMemberSubprogramDIE(DW_Unit,
917 DISubprogram(Element.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000918 else
Devang Patelc50078e2009-11-21 02:48:08 +0000919 ElemDie = createMemberDIE(DW_Unit,
Devang Patel15e723d2009-08-28 23:24:31 +0000920 DIDerivedType(Element.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000921 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000922 }
923
Devang Patel20b32102009-08-27 23:51:51 +0000924 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +0000925 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000926
927 unsigned RLang = CTy.getRunTimeLang();
928 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +0000929 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000930 dwarf::DW_FORM_data1, RLang);
931 break;
932 }
933 default:
934 break;
935 }
936
937 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000938 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000939 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000940
941 if (Tag == dwarf::DW_TAG_enumeration_type ||
942 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
943 // Add size if non-zero (derived types might be zero-sized.)
944 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000945 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000946 else {
947 // Add zero size if it is not a forward declaration.
948 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000949 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000950 else
Devang Patelc50078e2009-11-21 02:48:08 +0000951 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000952 }
953
954 // Add source line info if available.
955 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000956 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000957 }
958}
959
Devang Patelc50078e2009-11-21 02:48:08 +0000960/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
961void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000962 int64_t L = SR.getLo();
963 int64_t H = SR.getHi();
964 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
965
Devang Patelc50078e2009-11-21 02:48:08 +0000966 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +0000967 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +0000968 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +0000969 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000970
Devang Patelc50078e2009-11-21 02:48:08 +0000971 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000972}
973
Devang Patelc50078e2009-11-21 02:48:08 +0000974/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
975void DwarfDebug::constructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000976 DICompositeType *CTy) {
977 Buffer.setTag(dwarf::DW_TAG_array_type);
978 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000979 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000980
981 // Emit derived type.
Devang Patelc50078e2009-11-21 02:48:08 +0000982 addType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000983 DIArray Elements = CTy->getTypeArray();
984
Devang Patel1233f912009-11-21 00:31:03 +0000985 // Get an anonymous type for index type.
986 DIE *IdxTy = DW_Unit->getIndexTyDie();
987 if (!IdxTy) {
988 // Construct an anonymous type for index type.
989 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000990 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
991 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +0000992 dwarf::DW_ATE_signed);
Devang Patelc50078e2009-11-21 02:48:08 +0000993 DW_Unit->addDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +0000994 DW_Unit->setIndexTyDie(IdxTy);
995 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000996
997 // Add subranges to array type.
998 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
999 DIDescriptor Element = Elements.getElement(i);
1000 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001001 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001002 }
1003}
1004
Devang Patelc50078e2009-11-21 02:48:08 +00001005/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1006DIE *DwarfDebug::constructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001007 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001008 StringRef Name = ETy->getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001009 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001010 int64_t Value = ETy->getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001011 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001012 return Enumerator;
1013}
1014
Devang Patelc50078e2009-11-21 02:48:08 +00001015/// createGlobalVariableDIE - Create new DIE using GV.
1016DIE *DwarfDebug::createGlobalVariableDIE(CompileUnit *DW_Unit,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001017 const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001018 // If the global variable was optmized out then no need to create debug info
1019 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001020 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001021 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001022
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001023 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001024 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001025 GV.getDisplayName());
1026
Devang Patel7f75bbe2009-11-25 17:36:49 +00001027 StringRef LinkageName = GV.getLinkageName();
1028 if (!LinkageName.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001029 // Skip special LLVM prefix that is used to inform the asm printer to not
1030 // emit usual symbol prefix before the symbol name. This happens for
1031 // Objective-C symbol names and symbol whose name is replaced using GCC's
1032 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00001033 if (LinkageName[0] == 1)
Benjamin Kramer62b81882009-11-25 18:26:09 +00001034 LinkageName = LinkageName.substr(1);
Devang Patelc50078e2009-11-21 02:48:08 +00001035 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001036 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001037 }
Devang Patelc50078e2009-11-21 02:48:08 +00001038 addType(DW_Unit, GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001039 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001040 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1041 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001042
1043 // Add address.
1044 DIEBlock *Block = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001045 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1046 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patel6bd5cc82009-10-05 23:22:08 +00001047 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patelc50078e2009-11-21 02:48:08 +00001048 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001049
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001050 return GVDie;
1051}
1052
Devang Patelc50078e2009-11-21 02:48:08 +00001053/// createMemberDIE - Create new member DIE.
1054DIE *DwarfDebug::createMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001055 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001056 StringRef Name = DT.getName();
1057 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001058 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001059
Devang Patelc50078e2009-11-21 02:48:08 +00001060 addType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001061
Devang Patelc50078e2009-11-21 02:48:08 +00001062 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001063
Devang Patel7d9fe582009-11-04 22:06:12 +00001064 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001065 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001066
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001067 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001068 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001069
1070 if (Size != FieldSize) {
1071 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001072 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1073 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001074
1075 uint64_t Offset = DT.getOffsetInBits();
1076 uint64_t FieldOffset = Offset;
1077 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1078 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1079 FieldOffset = (HiMark - FieldSize);
1080 Offset -= FieldOffset;
1081
1082 // Maybe we need to work from the other end.
1083 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001084 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001085
Devang Patel7d9fe582009-11-04 22:06:12 +00001086 // Here WD_AT_data_member_location points to the anonymous
1087 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001088 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001089
1090 } else
1091 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001092 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001093
Devang Patelc50078e2009-11-21 02:48:08 +00001094 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001095
1096 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001097 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001098 dwarf::DW_ACCESS_protected);
1099 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001100 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001101 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-12-03 19:11:07 +00001102 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1103 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1104 dwarf::DW_ACCESS_public);
1105 if (DT.isVirtual())
1106 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1107 dwarf::DW_VIRTUALITY_virtual);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001108 return MemberDie;
1109}
1110
Devang Patelc1df8792009-12-03 01:25:38 +00001111/// createRawSubprogramDIE - Create new partially incomplete DIE. This is
1112/// a helper routine used by createMemberSubprogramDIE and
1113/// createSubprogramDIE.
1114DIE *DwarfDebug::createRawSubprogramDIE(CompileUnit *DW_Unit,
1115 const DISubprogram &SP) {
1116 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001117 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001118
Devang Patel7f75bbe2009-11-25 17:36:49 +00001119 StringRef LinkageName = SP.getLinkageName();
1120 if (!LinkageName.empty()) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001121 // Skip special LLVM prefix that is used to inform the asm printer to not
1122 // emit usual symbol prefix before the symbol name. This happens for
1123 // Objective-C symbol names and symbol whose name is replaced using GCC's
1124 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00001125 if (LinkageName[0] == 1)
Benjamin Kramer62b81882009-11-25 18:26:09 +00001126 LinkageName = LinkageName.substr(1);
Devang Patelc50078e2009-11-21 02:48:08 +00001127 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001128 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001129 }
Devang Patelc50078e2009-11-21 02:48:08 +00001130 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001131
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001132 // Add prototyped tag, if C or ObjC.
1133 unsigned Lang = SP.getCompileUnit().getLanguage();
1134 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1135 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001136 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001137
1138 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001139 DICompositeType SPTy = SP.getType();
1140 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001141 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001142
Devang Patelc1df8792009-12-03 01:25:38 +00001143 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1144 addType(DW_Unit, SPDie, SPTy);
1145 else
1146 addType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
1147
Devang Patel188c85d2009-12-03 19:11:07 +00001148 unsigned VK = SP.getVirtuality();
1149 if (VK) {
1150 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1151 DIEBlock *Block = new DIEBlock();
1152 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1153 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1154 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1155 ContainingTypeMap.insert(std::make_pair(SPDie, WeakVH(SP.getContainingType().getNode())));
1156 }
1157
Devang Patelc1df8792009-12-03 01:25:38 +00001158 return SPDie;
1159}
1160
1161/// createMemberSubprogramDIE - Create new member DIE using SP. This routine
1162/// always returns a die with DW_AT_declaration attribute.
1163DIE *DwarfDebug::createMemberSubprogramDIE(CompileUnit *DW_Unit,
1164 const DISubprogram &SP) {
1165 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1166 if (!SPDie)
1167 SPDie = createSubprogramDIE(DW_Unit, SP);
1168
1169 // If SPDie has DW_AT_declaration then reuse it.
1170 if (!SP.isDefinition())
1171 return SPDie;
1172
1173 // Otherwise create new DIE for the declaration. First push definition
1174 // DIE at the top level.
1175 if (TopLevelDIEs.insert(SPDie))
1176 TopLevelDIEsVector.push_back(SPDie);
1177
1178 SPDie = createRawSubprogramDIE(DW_Unit, SP);
1179
1180 // Add arguments.
1181 DICompositeType SPTy = SP.getType();
1182 DIArray Args = SPTy.getTypeArray();
1183 unsigned SPTag = SPTy.getTag();
1184 if (SPTag == dwarf::DW_TAG_subroutine_type)
1185 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1186 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1187 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1188 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1189 SPDie->addChild(Arg);
1190 }
1191
1192 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1193 return SPDie;
1194}
1195
1196/// createSubprogramDIE - Create new DIE using SP.
1197DIE *DwarfDebug::createSubprogramDIE(CompileUnit *DW_Unit,
1198 const DISubprogram &SP) {
1199 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1200 if (SPDie)
1201 return SPDie;
1202
1203 SPDie = createRawSubprogramDIE(DW_Unit, SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001204
1205 if (!SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001206 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001207
1208 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-12-03 01:25:38 +00001209 // be handled while processing variables.
1210 DICompositeType SPTy = SP.getType();
1211 DIArray Args = SPTy.getTypeArray();
1212 unsigned SPTag = SPTy.getTag();
1213
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001214 if (SPTag == dwarf::DW_TAG_subroutine_type)
1215 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1216 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc50078e2009-11-21 02:48:08 +00001217 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1218 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1219 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001220 }
1221 }
1222
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001223 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Pateld90672c2009-11-20 21:37:22 +00001224 DW_Unit->insertDIE(SP.getNode(), SPDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001225 return SPDie;
1226}
1227
Devang Patelc50078e2009-11-21 02:48:08 +00001228/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001229///
Devang Patelc50078e2009-11-21 02:48:08 +00001230CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001231 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patel15e723d2009-08-28 23:24:31 +00001232 CompileUnitMap.find(Unit.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001233 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1234 return *I->second;
1235}
1236
Devang Patelc50078e2009-11-21 02:48:08 +00001237/// createDbgScopeVariable - Create a new scope variable.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001238///
Devang Patelc50078e2009-11-21 02:48:08 +00001239DIE *DwarfDebug::createDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001240 // Get the descriptor.
1241 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001242 StringRef Name = VD.getName();
1243 if (Name.empty())
Devang Patel11ac7e72009-11-03 18:30:27 +00001244 return NULL;
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001245
1246 // Translate tag to proper Dwarf tag. The result variable is dropped for
1247 // now.
1248 unsigned Tag;
1249 switch (VD.getTag()) {
1250 case dwarf::DW_TAG_return_variable:
1251 return NULL;
1252 case dwarf::DW_TAG_arg_variable:
1253 Tag = dwarf::DW_TAG_formal_parameter;
1254 break;
1255 case dwarf::DW_TAG_auto_variable: // fall thru
1256 default:
1257 Tag = dwarf::DW_TAG_variable;
1258 break;
1259 }
1260
1261 // Define variable debug information entry.
1262 DIE *VariableDie = new DIE(Tag);
Devang Patelc50078e2009-11-21 02:48:08 +00001263 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001264
1265 // Add source line info if available.
Devang Patelc50078e2009-11-21 02:48:08 +00001266 addSourceLine(VariableDie, &VD);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001267
1268 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001269 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001270 // addresses instead.
Caroline Tice248d5572009-08-31 21:19:37 +00001271 if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001272 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Caroline Tice248d5572009-08-31 21:19:37 +00001273 else
Devang Patelc50078e2009-11-21 02:48:08 +00001274 addType(Unit, VariableDie, VD.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001275
1276 // Add variable address.
Devang Patel90a0fe32009-11-10 23:06:00 +00001277 // Variables for abstract instances of inlined functions don't get a
1278 // location.
1279 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001280 unsigned FrameReg;
1281 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1282 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001283
1284
Devang Patel90a0fe32009-11-10 23:06:00 +00001285 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001286 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001287 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001288 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001289 else
Devang Patelc50078e2009-11-21 02:48:08 +00001290 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001291
1292 return VariableDie;
1293}
1294
Devang Patel90a0fe32009-11-10 23:06:00 +00001295/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1296/// Initialize scope and update scope hierarchy.
1297DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1298 MDNode *InlinedAt) {
1299 assert (N && "Invalid Scope encoding!");
1300 assert (MI && "Missing machine instruction!");
1301 bool GetConcreteScope = (MI && InlinedAt);
1302
1303 DbgScope *NScope = NULL;
1304
1305 if (InlinedAt)
1306 NScope = DbgScopeMap.lookup(InlinedAt);
1307 else
1308 NScope = DbgScopeMap.lookup(N);
1309 assert (NScope && "Unable to find working scope!");
1310
1311 if (NScope->getFirstInsn())
1312 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001313
1314 DbgScope *Parent = NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001315 if (GetConcreteScope) {
Devang Pateldd7bb432009-10-14 21:08:09 +00001316 DILocation IL(InlinedAt);
Jim Grosbach652b7432009-11-21 23:12:12 +00001317 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel90a0fe32009-11-10 23:06:00 +00001318 IL.getOrigLocation().getNode());
1319 assert (Parent && "Unable to find Parent scope!");
1320 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001321 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001322 } else if (DIDescriptor(N).isLexicalBlock()) {
1323 DILexicalBlock DB(N);
1324 if (!DB.getContext().isNull()) {
1325 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1326 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001327 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001328 }
Devang Pateldd7bb432009-10-14 21:08:09 +00001329 }
Devang Patel6a260102009-10-01 20:31:14 +00001330
Devang Patelf5278f22009-10-27 20:47:17 +00001331 NScope->setFirstInsn(MI);
Devang Patel6a260102009-10-01 20:31:14 +00001332
Devang Patel90a0fe32009-11-10 23:06:00 +00001333 if (!Parent && !InlinedAt) {
Devang Patelce8986f2009-11-11 00:31:36 +00001334 StringRef SPName = DISubprogram(N).getLinkageName();
1335 if (SPName == MF->getFunction()->getName())
1336 CurrentFnDbgScope = NScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00001337 }
Devang Patel6a260102009-10-01 20:31:14 +00001338
Devang Patel90a0fe32009-11-10 23:06:00 +00001339 if (GetConcreteScope) {
1340 ConcreteScopes[InlinedAt] = NScope;
1341 getOrCreateAbstractScope(N);
1342 }
1343
Devang Patelf5278f22009-10-27 20:47:17 +00001344 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001345}
1346
Devang Patel90a0fe32009-11-10 23:06:00 +00001347DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1348 assert (N && "Invalid Scope encoding!");
1349
1350 DbgScope *AScope = AbstractScopes.lookup(N);
1351 if (AScope)
1352 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001353
Devang Patel90a0fe32009-11-10 23:06:00 +00001354 DbgScope *Parent = NULL;
1355
1356 DIDescriptor Scope(N);
1357 if (Scope.isLexicalBlock()) {
1358 DILexicalBlock DB(N);
1359 DIDescriptor ParentDesc = DB.getContext();
1360 if (!ParentDesc.isNull())
1361 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1362 }
1363
1364 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1365
1366 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001367 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001368 AScope->setAbstractScope();
1369 AbstractScopes[N] = AScope;
1370 if (DIDescriptor(N).isSubprogram())
1371 AbstractScopesList.push_back(AScope);
1372 return AScope;
1373}
Devang Patel6a260102009-10-01 20:31:14 +00001374
Jim Grosbach652b7432009-11-21 23:12:12 +00001375/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001376/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1377/// If there are global variables in this scope then create and insert
1378/// DIEs for these variables.
1379DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001380
Devang Pateld90672c2009-11-20 21:37:22 +00001381 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel90a0fe32009-11-10 23:06:00 +00001382 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001383 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001384 DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001385 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001386 DWLabel("func_end", SubprogramCount));
1387 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patelc50078e2009-11-21 02:48:08 +00001388 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach652b7432009-11-21 23:12:12 +00001389
Devang Patel90a0fe32009-11-10 23:06:00 +00001390 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001391 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001392
1393 // If there are global variables at this scope then add their dies.
Jim Grosbach652b7432009-11-21 23:12:12 +00001394 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
Devang Patel90a0fe32009-11-10 23:06:00 +00001395 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1396 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1397 if (!N) continue;
1398 DIGlobalVariable GV(N);
1399 if (GV.getContext().getNode() == SPNode) {
Devang Patelc50078e2009-11-21 02:48:08 +00001400 DIE *ScopedGVDie = createGlobalVariableDIE(ModuleCU, GV);
Devang Patelbad42262009-11-10 23:20:04 +00001401 if (ScopedGVDie)
Devang Patelc50078e2009-11-21 02:48:08 +00001402 SPDie->addChild(ScopedGVDie);
Devang Patel90a0fe32009-11-10 23:06:00 +00001403 }
1404 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001405
Devang Patel90a0fe32009-11-10 23:06:00 +00001406 return SPDie;
1407}
1408
Jim Grosbach652b7432009-11-21 23:12:12 +00001409/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001410/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1411DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001412 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1413 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1414
1415 // Ignore empty scopes.
1416 if (StartID == EndID && StartID != 0)
1417 return NULL;
1418
1419 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1420 if (Scope->isAbstractScope())
1421 return ScopeDIE;
1422
Devang Patelc50078e2009-11-21 02:48:08 +00001423 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001424 StartID ?
1425 DWLabel("label", StartID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001426 : DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001427 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001428 EndID ?
1429 DWLabel("label", EndID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001430 : DWLabel("func_end", SubprogramCount));
1431
1432
1433
1434 return ScopeDIE;
1435}
1436
Devang Patelc50078e2009-11-21 02:48:08 +00001437/// constructInlinedScopeDIE - This scope represents inlined body of
1438/// a function. Construct DIE to represent this concrete inlined copy
1439/// of the function.
1440DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001441 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1442 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1443 assert (StartID && "Invalid starting label for an inlined scope!");
1444 assert (EndID && "Invalid end label for an inlined scope!");
1445 // Ignore empty scopes.
1446 if (StartID == EndID && StartID != 0)
1447 return NULL;
1448
1449 DIScope DS(Scope->getScopeNode());
1450 if (DS.isNull())
1451 return NULL;
1452 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1453
1454 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001455 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001456 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001457 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001458 dwarf::DW_FORM_ref4, OriginDIE);
1459
Devang Patelc50078e2009-11-21 02:48:08 +00001460 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001461 DWLabel("label", StartID));
Devang Patelc50078e2009-11-21 02:48:08 +00001462 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001463 DWLabel("label", EndID));
1464
1465 InlinedSubprogramDIEs.insert(OriginDIE);
1466
1467 // Track the start label for this inlined function.
1468 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1469 I = InlineInfo.find(InlinedSP.getNode());
1470
1471 if (I == InlineInfo.end()) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001472 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1473 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001474 InlinedSPNodes.push_back(InlinedSP.getNode());
1475 } else
1476 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1477
1478 StringPool.insert(InlinedSP.getName());
1479 StringPool.insert(InlinedSP.getLinkageName());
1480 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001481 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1482 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001483
1484 return ScopeDIE;
1485}
1486
Devang Patelc50078e2009-11-21 02:48:08 +00001487
1488/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Jim Grosbach652b7432009-11-21 23:12:12 +00001489DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV,
Devang Patel90a0fe32009-11-10 23:06:00 +00001490 DbgScope *Scope, CompileUnit *Unit) {
1491 // Get the descriptor.
1492 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001493 StringRef Name = VD.getName();
1494 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001495 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001496
1497 // Translate tag to proper Dwarf tag. The result variable is dropped for
1498 // now.
1499 unsigned Tag;
1500 switch (VD.getTag()) {
1501 case dwarf::DW_TAG_return_variable:
1502 return NULL;
1503 case dwarf::DW_TAG_arg_variable:
1504 Tag = dwarf::DW_TAG_formal_parameter;
1505 break;
1506 case dwarf::DW_TAG_auto_variable: // fall thru
1507 default:
1508 Tag = dwarf::DW_TAG_variable;
1509 break;
1510 }
1511
1512 // Define variable debug information entry.
1513 DIE *VariableDie = new DIE(Tag);
1514
1515
1516 DIE *AbsDIE = NULL;
1517 if (DbgVariable *AV = DV->getAbstractVariable())
1518 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001519
Devang Patel90a0fe32009-11-10 23:06:00 +00001520 if (AbsDIE) {
1521 DIScope DS(Scope->getScopeNode());
1522 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001523 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001524 (void) OriginSPDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001525 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1526 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1527 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001528 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001529 dwarf::DW_FORM_ref4, AbsDIE);
1530 }
1531 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001532 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1533 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001534
1535 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001536 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001537 // addresses instead.
1538 if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001539 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001540 else
Devang Patelc50078e2009-11-21 02:48:08 +00001541 addType(Unit, VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001542 }
1543
1544 // Add variable address.
1545 if (!Scope->isAbstractScope()) {
1546 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001547 unsigned FrameReg;
1548 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1549 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001550
Devang Patel90a0fe32009-11-10 23:06:00 +00001551 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001552 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001553 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001554 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001555 else
Devang Patelc50078e2009-11-21 02:48:08 +00001556 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001557 }
1558 DV->setDIE(VariableDie);
1559 return VariableDie;
1560
1561}
Devang Patelc50078e2009-11-21 02:48:08 +00001562
Devang Patelec13b4f2009-11-24 01:14:22 +00001563void DwarfDebug::addPubTypes(DISubprogram SP) {
1564 DICompositeType SPTy = SP.getType();
1565 unsigned SPTag = SPTy.getTag();
1566 if (SPTag != dwarf::DW_TAG_subroutine_type)
1567 return;
1568
1569 DIArray Args = SPTy.getTypeArray();
1570 if (Args.isNull())
1571 return;
1572
1573 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1574 DIType ATy(Args.getElement(i).getNode());
1575 if (ATy.isNull())
1576 continue;
1577 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001578 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001579 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1580 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1581 }
1582 }
1583}
1584
Devang Patelc50078e2009-11-21 02:48:08 +00001585/// constructScopeDIE - Construct a DIE for this scope.
1586DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001587 if (!Scope)
1588 return NULL;
1589 DIScope DS(Scope->getScopeNode());
1590 if (DS.isNull())
1591 return NULL;
1592
1593 DIE *ScopeDIE = NULL;
1594 if (Scope->getInlinedAt())
Devang Patelc50078e2009-11-21 02:48:08 +00001595 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001596 else if (DS.isSubprogram()) {
1597 if (Scope->isAbstractScope())
Devang Pateld90672c2009-11-20 21:37:22 +00001598 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001599 else
Devang Patelc50078e2009-11-21 02:48:08 +00001600 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001601 }
1602 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001603 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001604 if (!ScopeDIE) return NULL;
1605 }
1606
1607 // Add variables to scope.
1608 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1609 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelc50078e2009-11-21 02:48:08 +00001610 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope, ModuleCU);
Jim Grosbach652b7432009-11-21 23:12:12 +00001611 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001612 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001613 }
1614
1615 // Add nested scopes.
1616 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1617 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1618 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001619 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001620 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001621 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001622 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001623
1624 if (DS.isSubprogram())
1625 addPubTypes(DISubprogram(DS.getNode()));
1626
1627 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001628}
1629
Bill Wendlingf5839192009-05-20 23:19:06 +00001630/// GetOrCreateSourceID - Look up the source id with the given directory and
1631/// source file names. If none currently exists, create a new id and insert it
1632/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1633/// maps as well.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001634unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001635 unsigned DId;
1636 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1637 if (DI != DirectoryIdMap.end()) {
1638 DId = DI->getValue();
1639 } else {
1640 DId = DirectoryNames.size() + 1;
1641 DirectoryIdMap[DirName] = DId;
1642 DirectoryNames.push_back(DirName);
1643 }
1644
1645 unsigned FId;
1646 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1647 if (FI != SourceFileIdMap.end()) {
1648 FId = FI->getValue();
1649 } else {
1650 FId = SourceFileNames.size() + 1;
1651 SourceFileIdMap[FileName] = FId;
1652 SourceFileNames.push_back(FileName);
1653 }
1654
1655 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1656 SourceIdMap.find(std::make_pair(DId, FId));
1657 if (SI != SourceIdMap.end())
1658 return SI->second;
1659
1660 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1661 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1662 SourceIds.push_back(std::make_pair(DId, FId));
1663
1664 return SrcId;
1665}
1666
Devang Patel641f8202009-12-07 21:41:32 +00001667/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1668DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace &NS) {
1669 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1670 if (NDie)
1671 return NDie;
1672
1673 NDie = new DIE(dwarf::DW_TAG_namespace);
1674 ModuleCU->insertDIE(NS.getNode(), NDie);
1675 if (!NS.getName().empty())
1676 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1677 addSourceLine(NDie, &NS);
1678 DIDescriptor NSContext = NS.getContext();
1679 DIE *Context = NULL;
1680 if (NSContext.isNameSpace()) {
1681 DINameSpace NS2(NSContext.getNode());
1682 Context = getOrCreateNameSpace(NS2);
1683 }
1684 else
1685 Context = ModuleCU->getCUDie();
1686 Context->addChild(NDie);
1687 return NDie;
1688}
1689
Devang Patelc50078e2009-11-21 02:48:08 +00001690void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001691 DICompileUnit DIUnit(N);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001692 StringRef FN = DIUnit.getFilename();
1693 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001694 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001695
1696 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001697 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf5839192009-05-20 23:19:06 +00001698 DWLabel("section_line", 0), DWLabel("section_line", 0),
1699 false);
Devang Patelc50078e2009-11-21 02:48:08 +00001700 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001701 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001702 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001703 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001704 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001705
Devang Patel7f75bbe2009-11-25 17:36:49 +00001706 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001707 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001708 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001709 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001710
Devang Patel7f75bbe2009-11-25 17:36:49 +00001711 StringRef Flags = DIUnit.getFlags();
1712 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001713 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001714
1715 unsigned RVer = DIUnit.getRunTimeVersion();
1716 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001717 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001718 dwarf::DW_FORM_data1, RVer);
1719
1720 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel5a3d37f2009-06-29 20:45:18 +00001721 if (!ModuleCU && DIUnit.isMain()) {
Devang Patelf97a05a2009-06-29 20:38:13 +00001722 // Use first compile unit marked as isMain as the compile unit
1723 // for this module.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001724 ModuleCU = Unit;
Devang Patelf97a05a2009-06-29 20:38:13 +00001725 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001726
Devang Patel15e723d2009-08-28 23:24:31 +00001727 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf5839192009-05-20 23:19:06 +00001728 CompileUnits.push_back(Unit);
1729}
1730
Devang Patelc50078e2009-11-21 02:48:08 +00001731void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001732 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001733
Devang Patel0c03f062009-09-04 23:59:07 +00001734 // If debug information is malformed then ignore it.
1735 if (DI_GV.Verify() == false)
1736 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001737
1738 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001739 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001740 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001741
Devang Patelc50078e2009-11-21 02:48:08 +00001742 DIE *VariableDie = createGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf5839192009-05-20 23:19:06 +00001743
Bill Wendlingf5839192009-05-20 23:19:06 +00001744 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001745 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001746
1747 // Add to context owner.
Devang Patelc1df8792009-12-03 01:25:38 +00001748 if (TopLevelDIEs.insert(VariableDie))
1749 TopLevelDIEsVector.push_back(VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001750
1751 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001752 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001753
1754 DIType GTy = DI_GV.getType();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001755 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001756 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1757 assert (Entry && "Missing global type!");
1758 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1759 }
Devang Patel166f8432009-06-26 01:49:18 +00001760 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001761}
1762
Devang Patelc50078e2009-11-21 02:48:08 +00001763void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001764 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001765
1766 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001767 if (ModuleCU->getDIE(N))
Devang Patel166f8432009-06-26 01:49:18 +00001768 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001769
1770 if (!SP.isDefinition())
1771 // This is a method declaration which will be handled while constructing
1772 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001773 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001774
Devang Patelc50078e2009-11-21 02:48:08 +00001775 DIE *SubprogramDie = createSubprogramDIE(ModuleCU, SP);
Bill Wendlingf5839192009-05-20 23:19:06 +00001776
1777 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001778 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001779
1780 // Add to context owner.
Devang Patel641f8202009-12-07 21:41:32 +00001781 DIDescriptor SPContext = SP.getContext();
1782 if (SPContext.isCompileUnit()
1783 && SPContext.getNode() == SP.getCompileUnit().getNode()) {
Devang Patelc1df8792009-12-03 01:25:38 +00001784 if (TopLevelDIEs.insert(SubprogramDie))
1785 TopLevelDIEsVector.push_back(SubprogramDie);
Devang Patel641f8202009-12-07 21:41:32 +00001786 } else if (SPContext.isNameSpace()) {
1787 DINameSpace NS(SPContext.getNode());
1788 DIE *NDie = getOrCreateNameSpace(NS);
1789 NDie->addChild(SubprogramDie);
1790 }
1791
Bill Wendlingf5839192009-05-20 23:19:06 +00001792 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001793 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001794
Devang Patel166f8432009-06-26 01:49:18 +00001795 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001796}
1797
Devang Patelc50078e2009-11-21 02:48:08 +00001798/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001799/// content. Create global DIEs and emit initial debug info sections.
1800/// This is inovked by the target AsmPrinter.
Devang Patelc50078e2009-11-21 02:48:08 +00001801void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel59a1d422009-06-25 22:36:02 +00001802 this->M = M;
1803
Bill Wendlingf5839192009-05-20 23:19:06 +00001804 if (TimePassesIsEnabled)
1805 DebugTimer->startTimer();
1806
Devang Patel1b4d6832009-11-11 19:55:08 +00001807 if (!MAI->doesSupportDebugInformation())
1808 return;
1809
Devang Patelfda766d2009-07-30 18:56:46 +00001810 DebugInfoFinder DbgFinder;
1811 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001812
Bill Wendlingf5839192009-05-20 23:19:06 +00001813 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001814 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1815 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001816 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001817
1818 if (CompileUnits.empty()) {
1819 if (TimePassesIsEnabled)
1820 DebugTimer->stopTimer();
1821
1822 return;
1823 }
1824
Devang Patelf97a05a2009-06-29 20:38:13 +00001825 // If main compile unit for this module is not seen than randomly
1826 // select first compile unit.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001827 if (!ModuleCU)
1828 ModuleCU = CompileUnits[0];
Devang Patelf97a05a2009-06-29 20:38:13 +00001829
Devang Patel166f8432009-06-26 01:49:18 +00001830 // Create DIEs for each of the externally visible global variables.
Devang Patelfda766d2009-07-30 18:56:46 +00001831 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patel695c8b02009-10-05 23:40:42 +00001832 E = DbgFinder.global_variable_end(); I != E; ++I) {
1833 DIGlobalVariable GV(*I);
Devang Patel641f8202009-12-07 21:41:32 +00001834 DIDescriptor GVContext = GV.getContext();
1835 if (GVContext.isCompileUnit()
1836 && GVContext.getNode() == GV.getCompileUnit().getNode())
Devang Patelc50078e2009-11-21 02:48:08 +00001837 constructGlobalVariableDIE(*I);
Devang Patel641f8202009-12-07 21:41:32 +00001838 else if (GVContext.isNameSpace()) {
1839 DIE *GVDie = createGlobalVariableDIE(ModuleCU, GV);
Devang Patel23a55d32009-12-08 15:01:35 +00001840 if (GVDie) {
1841 DINameSpace NS(GVContext.getNode());
1842 DIE *NDie = getOrCreateNameSpace(NS);
1843 NDie->addChild(GVDie);
1844 }
Devang Patel641f8202009-12-07 21:41:32 +00001845 }
1846 else
1847 ScopedGVs.push_back(*I);
Devang Patel695c8b02009-10-05 23:40:42 +00001848 }
Devang Patel166f8432009-06-26 01:49:18 +00001849
Devang Patel90a0fe32009-11-10 23:06:00 +00001850 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001851 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1852 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001853 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001854
Bill Wendlingf5839192009-05-20 23:19:06 +00001855 MMI = mmi;
1856 shouldEmit = true;
1857 MMI->setDebugInfoAvailability(true);
1858
1859 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001860 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001861
1862 // Print out .file directives to specify files for .loc directives. These are
1863 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001864 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001865 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1866 // Remember source id starts at 1.
1867 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1868 sys::Path FullPath(getSourceDirectoryName(Id.first));
1869 bool AppendOk =
1870 FullPath.appendComponent(getSourceFileName(Id.second));
1871 assert(AppendOk && "Could not append filename to directory!");
1872 AppendOk = false;
Chris Lattnerb1aa85b2009-08-23 22:45:37 +00001873 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001874 Asm->EOL();
1875 }
1876 }
1877
1878 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001879 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001880
1881 if (TimePassesIsEnabled)
1882 DebugTimer->stopTimer();
1883}
1884
Devang Patelc50078e2009-11-21 02:48:08 +00001885/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001886///
Devang Patelc50078e2009-11-21 02:48:08 +00001887void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001888 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001889 return;
1890
1891 if (TimePassesIsEnabled)
1892 DebugTimer->startTimer();
1893
Devang Patel90a0fe32009-11-10 23:06:00 +00001894 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1895 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1896 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1897 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001898 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001899 }
1900
Devang Patelc1df8792009-12-03 01:25:38 +00001901 // Insert top level DIEs.
1902 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1903 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1904 ModuleCU->getCUDie()->addChild(*TI);
1905
Devang Patel188c85d2009-12-03 19:11:07 +00001906 for (DenseMap<DIE *, WeakVH>::iterator CI = ContainingTypeMap.begin(),
1907 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1908 DIE *SPDie = CI->first;
1909 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1910 if (!N) continue;
1911 DIE *NDie = ModuleCU->getDIE(N);
1912 if (!NDie) continue;
1913 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1914 addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1915 }
1916
Bill Wendlingf5839192009-05-20 23:19:06 +00001917 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001918 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001919 EmitLabel("text_end", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00001920 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001921 EmitLabel("data_end", 0);
1922
1923 // End text sections.
1924 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001925 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf5839192009-05-20 23:19:06 +00001926 EmitLabel("section_end", i);
1927 }
1928
1929 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001930 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001931
1932 // Emit function debug frame information
1933 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1934 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001935 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001936
1937 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001938 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001939
1940 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001941 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001942
1943 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001944 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001945
1946 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001947 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001948
1949 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001950 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001951
Devang Patelec13b4f2009-11-24 01:14:22 +00001952 // Emit info into a debug pubtypes section.
1953 emitDebugPubTypes();
1954
Bill Wendlingf5839192009-05-20 23:19:06 +00001955 // Emit info into a debug str section.
Devang Patelc50078e2009-11-21 02:48:08 +00001956 emitDebugStr();
Bill Wendlingf5839192009-05-20 23:19:06 +00001957
1958 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001959 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001960
1961 // Emit info into a debug aranges section.
1962 EmitDebugARanges();
1963
1964 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001965 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001966
1967 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001968 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001969
1970 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001971 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001972
1973 if (TimePassesIsEnabled)
1974 DebugTimer->stopTimer();
1975}
1976
Devang Patel90a0fe32009-11-10 23:06:00 +00001977/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001978DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1979 unsigned FrameIdx,
Devang Patel90a0fe32009-11-10 23:06:00 +00001980 DILocation &ScopeLoc) {
1981
1982 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1983 if (AbsDbgVariable)
1984 return AbsDbgVariable;
1985
1986 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1987 if (!Scope)
1988 return NULL;
1989
1990 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patelc50078e2009-11-21 02:48:08 +00001991 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001992 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1993 return AbsDbgVariable;
1994}
1995
Devang Patelc50078e2009-11-21 02:48:08 +00001996/// collectVariableInfo - Populate DbgScope entries with variables' info.
1997void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001998 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001999
Devang Patel84139992009-10-06 01:26:37 +00002000 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2001 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2002 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel40c80212009-10-09 22:42:28 +00002003 MetadataBase *MB = VI->first;
2004 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel90a0fe32009-11-10 23:06:00 +00002005 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00002006 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00002007 std::pair< unsigned, MDNode *> VP = VI->second;
2008 DILocation ScopeLoc(VP.second);
2009
2010 DbgScope *Scope =
2011 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
2012 if (!Scope)
Jim Grosbach652b7432009-11-21 23:12:12 +00002013 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00002014 // If variable scope is not found then skip this variable.
2015 if (!Scope)
2016 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00002017
2018 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00002019 Scope->addVariable(RegVar);
Jim Grosbachb23f2422009-11-22 19:20:36 +00002020 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
2021 ScopeLoc))
Devang Patel90a0fe32009-11-10 23:06:00 +00002022 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel84139992009-10-06 01:26:37 +00002023 }
2024}
2025
Devang Patelc50078e2009-11-21 02:48:08 +00002026/// beginScope - Process beginning of a scope starting at Label.
2027void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00002028 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
2029 if (I == DbgScopeBeginMap.end())
2030 return;
Dan Gohman8d34f972009-11-23 21:30:55 +00002031 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002032 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00002033 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00002034 (*SDI)->setStartLabelID(Label);
2035}
2036
Devang Patelc50078e2009-11-21 02:48:08 +00002037/// endScope - Process end of a scope.
2038void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00002039 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00002040 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00002041 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00002042
2043 unsigned Label = MMI->NextLabelID();
2044 Asm->printLabel(Label);
Dan Gohmancfca6e32009-12-05 01:42:34 +00002045 O << '\n';
Devang Patel90a0fe32009-11-10 23:06:00 +00002046
Devang Patel393a46d2009-10-06 01:50:42 +00002047 SmallVector<DbgScope *, 2> &SD = I->second;
2048 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00002049 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00002050 (*SDI)->setEndLabelID(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00002051 return;
2052}
2053
2054/// createDbgScope - Create DbgScope for the scope.
2055void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
2056
2057 if (!InlinedAt) {
2058 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2059 if (WScope)
2060 return;
2061 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2062 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00002063 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00002064 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00002065 return;
2066 }
2067
2068 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2069 if (WScope)
2070 return;
2071
2072 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2073 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2074 DILocation DL(InlinedAt);
2075 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00002076}
2077
Devang Patelc50078e2009-11-21 02:48:08 +00002078/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00002079/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patelc50078e2009-11-21 02:48:08 +00002080bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patel6a260102009-10-01 20:31:14 +00002081 // If scope information was extracted using .dbg intrinsics then there is not
2082 // any need to extract these information by scanning each instruction.
2083 if (!DbgScopeMap.empty())
2084 return false;
2085
Devang Patel90a0fe32009-11-10 23:06:00 +00002086 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00002087 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2088 I != E; ++I) {
2089 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2090 II != IE; ++II) {
2091 const MachineInstr *MInsn = II;
2092 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00002093 if (DL.isUnknown()) continue;
Devang Patel6a260102009-10-01 20:31:14 +00002094 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel90a0fe32009-11-10 23:06:00 +00002095 if (!DLT.Scope) continue;
Devang Patel6a260102009-10-01 20:31:14 +00002096 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002097 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00002098 // into a scope DIE at the end.
Devang Patel90a0fe32009-11-10 23:06:00 +00002099 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2100 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
2101 }
2102 }
2103
2104
2105 // Build scope hierarchy using working set of scopes.
2106 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2107 I != E; ++I) {
2108 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2109 II != IE; ++II) {
2110 const MachineInstr *MInsn = II;
2111 DebugLoc DL = MInsn->getDebugLoc();
2112 if (DL.isUnknown()) continue;
2113 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
2114 if (!DLT.Scope) continue;
2115 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002116 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00002117 // into a scope DIE at the end.
2118 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2119 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
2120 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00002121 }
2122 }
2123
2124 // If a scope's last instruction is not set then use its child scope's
2125 // last instruction as this scope's last instrunction.
Devang Patelf5278f22009-10-27 20:47:17 +00002126 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00002127 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002128 if (DI->second->isAbstractScope())
2129 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002130 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patelc50078e2009-11-21 02:48:08 +00002131 DI->second->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +00002132 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2133 }
2134
2135 // Each scope has first instruction and last instruction to mark beginning
2136 // and end of a scope respectively. Create an inverse map that list scopes
2137 // starts (and ends) with an instruction. One instruction may start (or end)
2138 // multiple scopes.
Devang Patelf5278f22009-10-27 20:47:17 +00002139 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00002140 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2141 DbgScope *S = DI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002142 if (S->isAbstractScope())
2143 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002144 const MachineInstr *MI = S->getFirstInsn();
2145 assert (MI && "DbgScope does not have first instruction!");
2146
2147 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2148 if (IDI != DbgScopeBeginMap.end())
2149 IDI->second.push_back(S);
2150 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002151 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002152
2153 MI = S->getLastInsn();
2154 assert (MI && "DbgScope does not have last instruction!");
2155 IDI = DbgScopeEndMap.find(MI);
2156 if (IDI != DbgScopeEndMap.end())
2157 IDI->second.push_back(S);
2158 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002159 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002160 }
2161
2162 return !DbgScopeMap.empty();
2163}
2164
Devang Patelc50078e2009-11-21 02:48:08 +00002165/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002166/// emitted immediately after the function entry point.
Devang Patelc50078e2009-11-21 02:48:08 +00002167void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002168 this->MF = MF;
2169
2170 if (!ShouldEmitDwarfDebug()) return;
2171
2172 if (TimePassesIsEnabled)
2173 DebugTimer->startTimer();
2174
Devang Patelc50078e2009-11-21 02:48:08 +00002175 if (!extractScopeInformation(MF))
Devang Patel0feae422009-10-06 18:37:31 +00002176 return;
Devang Patelc50078e2009-11-21 02:48:08 +00002177
2178 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002179
Bill Wendlingf5839192009-05-20 23:19:06 +00002180 // Begin accumulating function debug information.
2181 MMI->BeginFunction(MF);
2182
2183 // Assumes in correct section after the entry point.
2184 EmitLabel("func_begin", ++SubprogramCount);
2185
2186 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2187 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002188 DebugLoc FDL = MF->getDefaultDebugLoc();
2189 if (!FDL.isUnknown()) {
2190 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2191 unsigned LabelID = 0;
Devang Patelfc1df342009-10-13 23:28:53 +00002192 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002193 if (!SP.isNull())
Devang Patelc50078e2009-11-21 02:48:08 +00002194 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002195 else
Devang Patelc50078e2009-11-21 02:48:08 +00002196 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002197 Asm->printLabel(LabelID);
2198 O << '\n';
Bill Wendlingf5839192009-05-20 23:19:06 +00002199 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002200 if (TimePassesIsEnabled)
2201 DebugTimer->stopTimer();
2202}
2203
Devang Patelc50078e2009-11-21 02:48:08 +00002204/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002205///
Devang Patelc50078e2009-11-21 02:48:08 +00002206void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002207 if (!ShouldEmitDwarfDebug()) return;
2208
2209 if (TimePassesIsEnabled)
2210 DebugTimer->startTimer();
2211
Devang Patel40c80212009-10-09 22:42:28 +00002212 if (DbgScopeMap.empty())
2213 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002214
Bill Wendlingf5839192009-05-20 23:19:06 +00002215 // Define end label for subprogram.
2216 EmitLabel("func_end", SubprogramCount);
2217
2218 // Get function line info.
2219 if (!Lines.empty()) {
2220 // Get section line info.
Chris Lattnerebd055c2009-08-03 23:20:21 +00002221 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00002222 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2223 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2224 // Append the function info to section info.
2225 SectionLineInfos.insert(SectionLineInfos.end(),
2226 Lines.begin(), Lines.end());
2227 }
2228
Devang Patel90a0fe32009-11-10 23:06:00 +00002229 // Construct abstract scopes.
2230 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach652b7432009-11-21 23:12:12 +00002231 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patelc50078e2009-11-21 02:48:08 +00002232 constructScopeDIE(*AI);
Bill Wendlingf5839192009-05-20 23:19:06 +00002233
Devang Patelc50078e2009-11-21 02:48:08 +00002234 constructScopeDIE(CurrentFnDbgScope);
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002235
Bill Wendlingf5839192009-05-20 23:19:06 +00002236 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2237 MMI->getFrameMoves()));
2238
2239 // Clear debug info
Devang Patel67533ab2009-12-01 18:13:48 +00002240 CurrentFnDbgScope = NULL;
2241 DbgScopeMap.clear();
2242 DbgScopeBeginMap.clear();
2243 DbgScopeEndMap.clear();
2244 ConcreteScopes.clear();
2245 AbstractScopesList.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002246
2247 Lines.clear();
Devang Patel67533ab2009-12-01 18:13:48 +00002248
Bill Wendlingf5839192009-05-20 23:19:06 +00002249 if (TimePassesIsEnabled)
2250 DebugTimer->stopTimer();
2251}
2252
Devang Patelc50078e2009-11-21 02:48:08 +00002253/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf5839192009-05-20 23:19:06 +00002254/// label. Returns a unique label ID used to generate a label and provide
2255/// correspondence to the source line list.
Jim Grosbach652b7432009-11-21 23:12:12 +00002256unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel946d0ae2009-10-05 18:03:19 +00002257 MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002258 if (!MMI)
2259 return 0;
2260
Bill Wendlingf5839192009-05-20 23:19:06 +00002261 if (TimePassesIsEnabled)
2262 DebugTimer->startTimer();
2263
Devang Patel7f75bbe2009-11-25 17:36:49 +00002264 StringRef Dir;
2265 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002266
2267 DIDescriptor Scope(S);
2268 if (Scope.isCompileUnit()) {
2269 DICompileUnit CU(S);
2270 Dir = CU.getDirectory();
2271 Fn = CU.getFilename();
2272 } else if (Scope.isSubprogram()) {
2273 DISubprogram SP(S);
2274 Dir = SP.getDirectory();
2275 Fn = SP.getFilename();
2276 } else if (Scope.isLexicalBlock()) {
2277 DILexicalBlock DB(S);
2278 Dir = DB.getDirectory();
2279 Fn = DB.getFilename();
2280 } else
2281 assert (0 && "Unexpected scope info");
2282
2283 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf5839192009-05-20 23:19:06 +00002284 unsigned ID = MMI->NextLabelID();
2285 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2286
2287 if (TimePassesIsEnabled)
2288 DebugTimer->stopTimer();
2289
2290 return ID;
2291}
2292
2293/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2294/// timed. Look up the source id with the given directory and source file
2295/// names. If none currently exists, create a new id and insert it in the
2296/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2297/// well.
2298unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2299 const std::string &FileName) {
2300 if (TimePassesIsEnabled)
2301 DebugTimer->startTimer();
2302
Devang Patelaaf012e2009-09-29 18:40:58 +00002303 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002304
2305 if (TimePassesIsEnabled)
2306 DebugTimer->stopTimer();
2307
2308 return SrcId;
2309}
2310
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002311//===----------------------------------------------------------------------===//
2312// Emit Methods
2313//===----------------------------------------------------------------------===//
2314
Devang Patelc50078e2009-11-21 02:48:08 +00002315/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002316///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002317unsigned
2318DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002319 // Get the children.
2320 const std::vector<DIE *> &Children = Die->getChildren();
2321
2322 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002323 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002324
2325 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002326 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002327
2328 // Get the abbreviation for this DIE.
2329 unsigned AbbrevNumber = Die->getAbbrevNumber();
2330 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2331
2332 // Set DIE offset
2333 Die->setOffset(Offset);
2334
2335 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002336 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002337
2338 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2339 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2340
2341 // Size the DIE attribute values.
2342 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2343 // Size attribute value.
2344 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2345
2346 // Size the DIE children if any.
2347 if (!Children.empty()) {
2348 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2349 "Children flag not set");
2350
2351 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002352 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002353
2354 // End of children marker.
2355 Offset += sizeof(int8_t);
2356 }
2357
2358 Die->setSize(Offset - Die->getOffset());
2359 return Offset;
2360}
2361
Devang Patelc50078e2009-11-21 02:48:08 +00002362/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002363///
Devang Patelc50078e2009-11-21 02:48:08 +00002364void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002365 // Compute size of compile unit header.
2366 static unsigned Offset =
2367 sizeof(int32_t) + // Length of Compilation Unit Info
2368 sizeof(int16_t) + // DWARF version number
2369 sizeof(int32_t) + // Offset Into Abbrev. Section
2370 sizeof(int8_t); // Pointer Size (in bytes)
2371
Devang Patelc50078e2009-11-21 02:48:08 +00002372 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002373 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002374}
2375
Devang Patelc50078e2009-11-21 02:48:08 +00002376/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002377/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002378void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002379 // Check to see if we already emitted intial headers.
2380 if (didInitial) return;
2381 didInitial = true;
2382
Chris Lattner73266f92009-08-19 05:49:37 +00002383 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002384
Bill Wendling55fccda2009-05-20 23:21:38 +00002385 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002386 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002387 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002388 EmitLabel("section_debug_frame", 0);
2389 }
2390
Chris Lattner73266f92009-08-19 05:49:37 +00002391 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002392 EmitLabel("section_info", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002393 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002394 EmitLabel("section_abbrev", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002395 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002396 EmitLabel("section_aranges", 0);
2397
Chris Lattner73266f92009-08-19 05:49:37 +00002398 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2399 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling55fccda2009-05-20 23:21:38 +00002400 EmitLabel("section_macinfo", 0);
2401 }
2402
Chris Lattner73266f92009-08-19 05:49:37 +00002403 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002404 EmitLabel("section_line", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002405 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002406 EmitLabel("section_loc", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002407 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002408 EmitLabel("section_pubnames", 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00002409 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2410 EmitLabel("section_pubtypes", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002411 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002412 EmitLabel("section_str", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002413 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002414 EmitLabel("section_ranges", 0);
2415
Chris Lattner73266f92009-08-19 05:49:37 +00002416 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002417 EmitLabel("text_begin", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002418 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002419 EmitLabel("data_begin", 0);
2420}
2421
Devang Patelc50078e2009-11-21 02:48:08 +00002422/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002423///
Devang Patelc50078e2009-11-21 02:48:08 +00002424void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002425 // Get the abbreviation for this DIE.
2426 unsigned AbbrevNumber = Die->getAbbrevNumber();
2427 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2428
2429 Asm->EOL();
2430
2431 // Emit the code (index) for the abbreviation.
2432 Asm->EmitULEB128Bytes(AbbrevNumber);
2433
2434 if (Asm->isVerbose())
2435 Asm->EOL(std::string("Abbrev [" +
2436 utostr(AbbrevNumber) +
2437 "] 0x" + utohexstr(Die->getOffset()) +
2438 ":0x" + utohexstr(Die->getSize()) + " " +
2439 dwarf::TagString(Abbrev->getTag())));
2440 else
2441 Asm->EOL();
2442
2443 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2444 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2445
2446 // Emit the DIE attribute values.
2447 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2448 unsigned Attr = AbbrevData[i].getAttribute();
2449 unsigned Form = AbbrevData[i].getForm();
2450 assert(Form && "Too many attributes for DIE (check abbreviation)");
2451
2452 switch (Attr) {
2453 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002454 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002455 break;
2456 case dwarf::DW_AT_abstract_origin: {
2457 DIEEntry *E = cast<DIEEntry>(Values[i]);
2458 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002459 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002460 Asm->EmitInt32(Addr);
2461 break;
2462 }
2463 default:
2464 // Emit an attribute using the defined form.
2465 Values[i]->EmitValue(this, Form);
2466 break;
2467 }
2468
2469 Asm->EOL(dwarf::AttributeString(Attr));
2470 }
2471
2472 // Emit the DIE children if any.
2473 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2474 const std::vector<DIE *> &Children = Die->getChildren();
2475
2476 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002477 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002478
2479 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2480 }
2481}
2482
Devang Patelc50078e2009-11-21 02:48:08 +00002483/// emitDebugInfo / emitDebugInfoPerCU - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002484///
Devang Patelc50078e2009-11-21 02:48:08 +00002485void DwarfDebug::emitDebugInfoPerCU(CompileUnit *Unit) {
Devang Pateld90672c2009-11-20 21:37:22 +00002486 DIE *Die = Unit->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002487
2488 // Emit the compile units header.
2489 EmitLabel("info_begin", Unit->getID());
2490
2491 // Emit size of content not including length itself
2492 unsigned ContentSize = Die->getSize() +
2493 sizeof(int16_t) + // DWARF version number
2494 sizeof(int32_t) + // Offset Into Abbrev. Section
2495 sizeof(int8_t) + // Pointer Size (in bytes)
2496 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2497
2498 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2499 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2500 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2501 Asm->EOL("Offset Into Abbrev. Section");
2502 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2503
Devang Patelc50078e2009-11-21 02:48:08 +00002504 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002505 // FIXME - extra padding for gdb bug.
2506 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2507 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2508 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2509 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2510 EmitLabel("info_end", Unit->getID());
2511
2512 Asm->EOL();
2513}
2514
Devang Patelc50078e2009-11-21 02:48:08 +00002515void DwarfDebug::emitDebugInfo() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002516 // Start debug info section.
Chris Lattner73266f92009-08-19 05:49:37 +00002517 Asm->OutStreamer.SwitchSection(
2518 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002519
Devang Patelc50078e2009-11-21 02:48:08 +00002520 emitDebugInfoPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002521}
2522
Devang Patelc50078e2009-11-21 02:48:08 +00002523/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002524///
Devang Patelc50078e2009-11-21 02:48:08 +00002525void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002526 // Check to see if it is worth the effort.
2527 if (!Abbreviations.empty()) {
2528 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002529 Asm->OutStreamer.SwitchSection(
2530 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002531
2532 EmitLabel("abbrev_begin", 0);
2533
2534 // For each abbrevation.
2535 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2536 // Get abbreviation data
2537 const DIEAbbrev *Abbrev = Abbreviations[i];
2538
2539 // Emit the abbrevations code (base 1 index.)
2540 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2541 Asm->EOL("Abbreviation Code");
2542
2543 // Emit the abbreviations data.
2544 Abbrev->Emit(Asm);
2545
2546 Asm->EOL();
2547 }
2548
2549 // Mark end of abbreviations.
2550 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2551
2552 EmitLabel("abbrev_end", 0);
2553 Asm->EOL();
2554 }
2555}
2556
Devang Patelc50078e2009-11-21 02:48:08 +00002557/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002558/// the line matrix.
2559///
Devang Patelc50078e2009-11-21 02:48:08 +00002560void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002561 // Define last address of section.
2562 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2563 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2564 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2565 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2566
2567 // Mark end of matrix.
2568 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2569 Asm->EmitULEB128Bytes(1); Asm->EOL();
2570 Asm->EmitInt8(1); Asm->EOL();
2571}
2572
Devang Patelc50078e2009-11-21 02:48:08 +00002573/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002574///
Devang Patelc50078e2009-11-21 02:48:08 +00002575void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002576 // If the target is using .loc/.file, the assembler will be emitting the
2577 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002578 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002579 return;
2580
2581 // Minimum line delta, thus ranging from -10..(255-10).
2582 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2583 // Maximum line delta, thus ranging from -10..(255-10).
2584 const int MaxLineDelta = 255 + MinLineDelta;
2585
2586 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002587 Asm->OutStreamer.SwitchSection(
2588 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002589
2590 // Construct the section header.
2591 EmitDifference("line_end", 0, "line_begin", 0, true);
2592 Asm->EOL("Length of Source Line Info");
2593 EmitLabel("line_begin", 0);
2594
2595 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2596
2597 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2598 Asm->EOL("Prolog Length");
2599 EmitLabel("line_prolog_begin", 0);
2600
2601 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2602
2603 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2604
2605 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2606
2607 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2608
2609 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2610
2611 // Line number standard opcode encodings argument count
2612 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2613 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2614 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2615 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2616 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2617 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2618 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2619 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2620 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2621
2622 // Emit directories.
2623 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2624 Asm->EmitString(getSourceDirectoryName(DI));
2625 Asm->EOL("Directory");
2626 }
2627
2628 Asm->EmitInt8(0); Asm->EOL("End of directories");
2629
2630 // Emit files.
2631 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2632 // Remember source id starts at 1.
2633 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2634 Asm->EmitString(getSourceFileName(Id.second));
2635 Asm->EOL("Source");
2636 Asm->EmitULEB128Bytes(Id.first);
2637 Asm->EOL("Directory #");
2638 Asm->EmitULEB128Bytes(0);
2639 Asm->EOL("Mod date");
2640 Asm->EmitULEB128Bytes(0);
2641 Asm->EOL("File size");
2642 }
2643
2644 Asm->EmitInt8(0); Asm->EOL("End of files");
2645
2646 EmitLabel("line_prolog_end", 0);
2647
2648 // A sequence for each text section.
2649 unsigned SecSrcLinesSize = SectionSourceLines.size();
2650
2651 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2652 // Isolate current sections line info.
2653 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2654
Chris Lattner26aabb92009-08-08 23:39:42 +00002655 /*if (Asm->isVerbose()) {
Chris Lattnere6ad12f2009-07-31 18:48:30 +00002656 const MCSection *S = SectionMap[j + 1];
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002657 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling55fccda2009-05-20 23:21:38 +00002658 << S->getName() << '\n';
Chris Lattner26aabb92009-08-08 23:39:42 +00002659 }*/
2660 Asm->EOL();
Bill Wendling55fccda2009-05-20 23:21:38 +00002661
2662 // Dwarf assumes we start with first line of first source file.
2663 unsigned Source = 1;
2664 unsigned Line = 1;
2665
2666 // Construct rows of the address, source, line, column matrix.
2667 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2668 const SrcLineInfo &LineInfo = LineInfos[i];
2669 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2670 if (!LabelID) continue;
2671
Caroline Tice9da96d82009-09-11 18:25:54 +00002672 if (LineInfo.getLine() == 0) continue;
2673
Bill Wendling55fccda2009-05-20 23:21:38 +00002674 if (!Asm->isVerbose())
2675 Asm->EOL();
2676 else {
2677 std::pair<unsigned, unsigned> SourceID =
2678 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002679 O << '\t' << MAI->getCommentString() << ' '
Dan Gohman1792bc62009-12-05 02:00:34 +00002680 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling55fccda2009-05-20 23:21:38 +00002681 << getSourceFileName(SourceID.second)
Dan Gohman1792bc62009-12-05 02:00:34 +00002682 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002683 }
2684
2685 // Define the line address.
2686 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2687 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2688 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2689 EmitReference("label", LabelID); Asm->EOL("Location label");
2690
2691 // If change of source, then switch to the new source.
2692 if (Source != LineInfo.getSourceID()) {
2693 Source = LineInfo.getSourceID();
2694 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2695 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2696 }
2697
2698 // If change of line.
2699 if (Line != LineInfo.getLine()) {
2700 // Determine offset.
2701 int Offset = LineInfo.getLine() - Line;
2702 int Delta = Offset - MinLineDelta;
2703
2704 // Update line.
2705 Line = LineInfo.getLine();
2706
2707 // If delta is small enough and in range...
2708 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2709 // ... then use fast opcode.
2710 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2711 } else {
2712 // ... otherwise use long hand.
2713 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2714 Asm->EOL("DW_LNS_advance_line");
2715 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2716 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2717 }
2718 } else {
2719 // Copy the previous row (different address or source)
2720 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2721 }
2722 }
2723
Devang Patelc50078e2009-11-21 02:48:08 +00002724 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002725 }
2726
2727 if (SecSrcLinesSize == 0)
2728 // Because we're emitting a debug_line section, we still need a line
2729 // table. The linker and friends expect it to exist. If there's nothing to
2730 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002731 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002732
2733 EmitLabel("line_end", 0);
2734 Asm->EOL();
2735}
2736
Devang Patelc50078e2009-11-21 02:48:08 +00002737/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002738///
Devang Patelc50078e2009-11-21 02:48:08 +00002739void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002740 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002741 return;
2742
2743 int stackGrowth =
2744 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2745 TargetFrameInfo::StackGrowsUp ?
2746 TD->getPointerSize() : -TD->getPointerSize();
2747
2748 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002749 Asm->OutStreamer.SwitchSection(
2750 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002751
2752 EmitLabel("debug_frame_common", 0);
2753 EmitDifference("debug_frame_common_end", 0,
2754 "debug_frame_common_begin", 0, true);
2755 Asm->EOL("Length of Common Information Entry");
2756
2757 EmitLabel("debug_frame_common_begin", 0);
2758 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2759 Asm->EOL("CIE Identifier Tag");
2760 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2761 Asm->EOL("CIE Version");
2762 Asm->EmitString("");
2763 Asm->EOL("CIE Augmentation");
2764 Asm->EmitULEB128Bytes(1);
2765 Asm->EOL("CIE Code Alignment Factor");
2766 Asm->EmitSLEB128Bytes(stackGrowth);
2767 Asm->EOL("CIE Data Alignment Factor");
2768 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2769 Asm->EOL("CIE RA Column");
2770
2771 std::vector<MachineMove> Moves;
2772 RI->getInitialFrameState(Moves);
2773
2774 EmitFrameMoves(NULL, 0, Moves, false);
2775
2776 Asm->EmitAlignment(2, 0, 0, false);
2777 EmitLabel("debug_frame_common_end", 0);
2778
2779 Asm->EOL();
2780}
2781
Devang Patelc50078e2009-11-21 02:48:08 +00002782/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002783/// section.
2784void
Devang Patelc50078e2009-11-21 02:48:08 +00002785DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002786 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002787 return;
2788
2789 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002790 Asm->OutStreamer.SwitchSection(
2791 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002792
2793 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2794 "debug_frame_begin", DebugFrameInfo.Number, true);
2795 Asm->EOL("Length of Frame Information Entry");
2796
2797 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2798
2799 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2800 0, 0, true, false);
2801 Asm->EOL("FDE CIE offset");
2802
2803 EmitReference("func_begin", DebugFrameInfo.Number);
2804 Asm->EOL("FDE initial location");
2805 EmitDifference("func_end", DebugFrameInfo.Number,
2806 "func_begin", DebugFrameInfo.Number);
2807 Asm->EOL("FDE address range");
2808
2809 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2810 false);
2811
2812 Asm->EmitAlignment(2, 0, 0, false);
2813 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2814
2815 Asm->EOL();
2816}
2817
Devang Patelc50078e2009-11-21 02:48:08 +00002818void DwarfDebug::emitDebugPubNamesPerCU(CompileUnit *Unit) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002819 EmitDifference("pubnames_end", Unit->getID(),
2820 "pubnames_begin", Unit->getID(), true);
2821 Asm->EOL("Length of Public Names Info");
2822
2823 EmitLabel("pubnames_begin", Unit->getID());
2824
2825 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2826
2827 EmitSectionOffset("info_begin", "section_info",
2828 Unit->getID(), 0, true, false);
2829 Asm->EOL("Offset of Compilation Unit Info");
2830
2831 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2832 true);
2833 Asm->EOL("Compilation Unit Length");
2834
Devang Patelec13b4f2009-11-24 01:14:22 +00002835 const StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00002836 for (StringMap<DIE*>::const_iterator
2837 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2838 const char *Name = GI->getKeyData();
2839 DIE * Entity = GI->second;
2840
2841 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2842 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2843 }
2844
2845 Asm->EmitInt32(0); Asm->EOL("End Mark");
2846 EmitLabel("pubnames_end", Unit->getID());
2847
2848 Asm->EOL();
2849}
2850
Devang Patelc50078e2009-11-21 02:48:08 +00002851/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002852///
Devang Patelc50078e2009-11-21 02:48:08 +00002853void DwarfDebug::emitDebugPubNames() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002854 // Start the dwarf pubnames section.
Chris Lattner73266f92009-08-19 05:49:37 +00002855 Asm->OutStreamer.SwitchSection(
2856 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002857
Devang Patelc50078e2009-11-21 02:48:08 +00002858 emitDebugPubNamesPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002859}
2860
Devang Patelec13b4f2009-11-24 01:14:22 +00002861void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00002862 // Start the dwarf pubnames section.
2863 Asm->OutStreamer.SwitchSection(
2864 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patelec13b4f2009-11-24 01:14:22 +00002865 EmitDifference("pubtypes_end", ModuleCU->getID(),
2866 "pubtypes_begin", ModuleCU->getID(), true);
2867 Asm->EOL("Length of Public Types Info");
2868
2869 EmitLabel("pubtypes_begin", ModuleCU->getID());
2870
2871 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2872
2873 EmitSectionOffset("info_begin", "section_info",
2874 ModuleCU->getID(), 0, true, false);
2875 Asm->EOL("Offset of Compilation ModuleCU Info");
2876
2877 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2878 true);
2879 Asm->EOL("Compilation ModuleCU Length");
2880
2881 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2882 for (StringMap<DIE*>::const_iterator
2883 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2884 const char *Name = GI->getKeyData();
2885 DIE * Entity = GI->second;
2886
2887 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2888 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2889 }
2890
2891 Asm->EmitInt32(0); Asm->EOL("End Mark");
2892 EmitLabel("pubtypes_end", ModuleCU->getID());
2893
2894 Asm->EOL();
2895}
2896
Devang Patelc50078e2009-11-21 02:48:08 +00002897/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002898///
Devang Patelc50078e2009-11-21 02:48:08 +00002899void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002900 // Check to see if it is worth the effort.
2901 if (!StringPool.empty()) {
2902 // Start the dwarf str section.
Chris Lattner73266f92009-08-19 05:49:37 +00002903 Asm->OutStreamer.SwitchSection(
2904 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002905
2906 // For each of strings in the string pool.
2907 for (unsigned StringID = 1, N = StringPool.size();
2908 StringID <= N; ++StringID) {
2909 // Emit a label for reference from debug information entries.
2910 EmitLabel("string", StringID);
2911
2912 // Emit the string itself.
2913 const std::string &String = StringPool[StringID];
2914 Asm->EmitString(String); Asm->EOL();
2915 }
2916
2917 Asm->EOL();
2918 }
2919}
2920
Devang Patelc50078e2009-11-21 02:48:08 +00002921/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002922///
Devang Patelc50078e2009-11-21 02:48:08 +00002923void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002924 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002925 Asm->OutStreamer.SwitchSection(
2926 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002927 Asm->EOL();
2928}
2929
2930/// EmitDebugARanges - Emit visible names into a debug aranges section.
2931///
2932void DwarfDebug::EmitDebugARanges() {
2933 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002934 Asm->OutStreamer.SwitchSection(
2935 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002936
2937 // FIXME - Mock up
2938#if 0
2939 CompileUnit *Unit = GetBaseCompileUnit();
2940
2941 // Don't include size of length
2942 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2943
2944 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2945
2946 EmitReference("info_begin", Unit->getID());
2947 Asm->EOL("Offset of Compilation Unit Info");
2948
2949 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2950
2951 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2952
2953 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2954 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2955
2956 // Range 1
2957 EmitReference("text_begin", 0); Asm->EOL("Address");
2958 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2959
2960 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2961 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2962#endif
2963
2964 Asm->EOL();
2965}
2966
Devang Patelc50078e2009-11-21 02:48:08 +00002967/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002968///
Devang Patelc50078e2009-11-21 02:48:08 +00002969void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002970 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002971 Asm->OutStreamer.SwitchSection(
2972 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002973 Asm->EOL();
2974}
2975
Devang Patelc50078e2009-11-21 02:48:08 +00002976/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002977///
Devang Patelc50078e2009-11-21 02:48:08 +00002978void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00002979 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00002980 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002981 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00002982 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002983 Asm->EOL();
2984 }
2985}
2986
Devang Patelc50078e2009-11-21 02:48:08 +00002987/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00002988/// Section Header:
2989/// 1. length of section
2990/// 2. Dwarf version number
2991/// 3. address size.
2992///
2993/// Entries (one "entry" for each function that was inlined):
2994///
2995/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2996/// otherwise offset into __debug_str for regular function name.
2997/// 2. offset into __debug_str section for regular function name.
2998/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2999/// instances for the function.
3000///
3001/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3002/// inlined instance; the die_offset points to the inlined_subroutine die in the
3003/// __debug_info section, and the low_pc is the starting address for the
3004/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00003005void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00003006 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003007 return;
3008
Devang Patel5a3d37f2009-06-29 20:45:18 +00003009 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00003010 return;
3011
Chris Lattner73266f92009-08-19 05:49:37 +00003012 Asm->OutStreamer.SwitchSection(
3013 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003014 Asm->EOL();
3015 EmitDifference("debug_inlined_end", 1,
3016 "debug_inlined_begin", 1, true);
3017 Asm->EOL("Length of Debug Inlined Information Entry");
3018
3019 EmitLabel("debug_inlined_begin", 1);
3020
3021 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
3022 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
3023
Devang Patel90a0fe32009-11-10 23:06:00 +00003024 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
3025 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00003026
Devang Patel90a0fe32009-11-10 23:06:00 +00003027// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
3028 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
3029 MDNode *Node = *I;
Jim Grosbachb23f2422009-11-22 19:20:36 +00003030 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
3031 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00003032 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00003033 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00003034 StringRef LName = SP.getLinkageName();
3035 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00003036
Devang Patel7f75bbe2009-11-25 17:36:49 +00003037 if (LName.empty())
Devang Patel76031e82009-07-16 01:01:22 +00003038 Asm->EmitString(Name);
3039 else {
Chris Lattner73266f92009-08-19 05:49:37 +00003040 // Skip special LLVM prefix that is used to inform the asm printer to not
3041 // emit usual symbol prefix before the symbol name. This happens for
3042 // Objective-C symbol names and symbol whose name is replaced using GCC's
3043 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00003044 if (LName[0] == 1)
Benjamin Kramer62b81882009-11-25 18:26:09 +00003045 LName = LName.substr(1);
Devang Patel90a0fe32009-11-10 23:06:00 +00003046// Asm->EmitString(LName);
3047 EmitSectionOffset("string", "section_str",
3048 StringPool.idFor(LName), false, true);
3049
Devang Patel76031e82009-07-16 01:01:22 +00003050 }
Bill Wendling55fccda2009-05-20 23:21:38 +00003051 Asm->EOL("MIPS linkage name");
Jim Grosbach652b7432009-11-21 23:12:12 +00003052// Asm->EmitString(Name);
Devang Patel90a0fe32009-11-10 23:06:00 +00003053 EmitSectionOffset("string", "section_str",
3054 StringPool.idFor(Name), false, true);
3055 Asm->EOL("Function name");
Bill Wendling55fccda2009-05-20 23:21:38 +00003056 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
3057
Devang Patel90a0fe32009-11-10 23:06:00 +00003058 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00003059 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00003060 DIE *SP = LI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00003061 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
3062
3063 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattnera5ef4d32009-08-22 21:43:10 +00003064 O << MAI->getData32bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00003065 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +00003066 O << MAI->getData64bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00003067
Devang Patel90a0fe32009-11-10 23:06:00 +00003068 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling55fccda2009-05-20 23:21:38 +00003069 }
3070 }
3071
3072 EmitLabel("debug_inlined_end", 1);
3073 Asm->EOL();
3074}