blob: f4f6fce457725ea442588a4532f20d0b88fc868b [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);
1840 DINameSpace NS(GVContext.getNode());
1841 DIE *NDie = getOrCreateNameSpace(NS);
1842 NDie->addChild(GVDie);
1843 }
1844 else
1845 ScopedGVs.push_back(*I);
Devang Patel695c8b02009-10-05 23:40:42 +00001846 }
Devang Patel166f8432009-06-26 01:49:18 +00001847
Devang Patel90a0fe32009-11-10 23:06:00 +00001848 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001849 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1850 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001851 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001852
Bill Wendlingf5839192009-05-20 23:19:06 +00001853 MMI = mmi;
1854 shouldEmit = true;
1855 MMI->setDebugInfoAvailability(true);
1856
1857 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001858 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001859
1860 // Print out .file directives to specify files for .loc directives. These are
1861 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001862 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001863 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1864 // Remember source id starts at 1.
1865 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1866 sys::Path FullPath(getSourceDirectoryName(Id.first));
1867 bool AppendOk =
1868 FullPath.appendComponent(getSourceFileName(Id.second));
1869 assert(AppendOk && "Could not append filename to directory!");
1870 AppendOk = false;
Chris Lattnerb1aa85b2009-08-23 22:45:37 +00001871 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001872 Asm->EOL();
1873 }
1874 }
1875
1876 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001877 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001878
1879 if (TimePassesIsEnabled)
1880 DebugTimer->stopTimer();
1881}
1882
Devang Patelc50078e2009-11-21 02:48:08 +00001883/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001884///
Devang Patelc50078e2009-11-21 02:48:08 +00001885void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001886 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001887 return;
1888
1889 if (TimePassesIsEnabled)
1890 DebugTimer->startTimer();
1891
Devang Patel90a0fe32009-11-10 23:06:00 +00001892 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1893 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1894 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1895 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001896 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001897 }
1898
Devang Patelc1df8792009-12-03 01:25:38 +00001899 // Insert top level DIEs.
1900 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1901 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1902 ModuleCU->getCUDie()->addChild(*TI);
1903
Devang Patel188c85d2009-12-03 19:11:07 +00001904 for (DenseMap<DIE *, WeakVH>::iterator CI = ContainingTypeMap.begin(),
1905 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1906 DIE *SPDie = CI->first;
1907 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1908 if (!N) continue;
1909 DIE *NDie = ModuleCU->getDIE(N);
1910 if (!NDie) continue;
1911 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1912 addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1913 }
1914
Bill Wendlingf5839192009-05-20 23:19:06 +00001915 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001916 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001917 EmitLabel("text_end", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00001918 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001919 EmitLabel("data_end", 0);
1920
1921 // End text sections.
1922 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001923 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf5839192009-05-20 23:19:06 +00001924 EmitLabel("section_end", i);
1925 }
1926
1927 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001928 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001929
1930 // Emit function debug frame information
1931 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1932 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001933 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001934
1935 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001936 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001937
1938 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001939 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001940
1941 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001942 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001943
1944 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001945 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001946
1947 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001948 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001949
Devang Patelec13b4f2009-11-24 01:14:22 +00001950 // Emit info into a debug pubtypes section.
1951 emitDebugPubTypes();
1952
Bill Wendlingf5839192009-05-20 23:19:06 +00001953 // Emit info into a debug str section.
Devang Patelc50078e2009-11-21 02:48:08 +00001954 emitDebugStr();
Bill Wendlingf5839192009-05-20 23:19:06 +00001955
1956 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001957 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001958
1959 // Emit info into a debug aranges section.
1960 EmitDebugARanges();
1961
1962 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001963 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001964
1965 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001966 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001967
1968 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001969 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001970
1971 if (TimePassesIsEnabled)
1972 DebugTimer->stopTimer();
1973}
1974
Devang Patel90a0fe32009-11-10 23:06:00 +00001975/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001976DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1977 unsigned FrameIdx,
Devang Patel90a0fe32009-11-10 23:06:00 +00001978 DILocation &ScopeLoc) {
1979
1980 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1981 if (AbsDbgVariable)
1982 return AbsDbgVariable;
1983
1984 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1985 if (!Scope)
1986 return NULL;
1987
1988 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patelc50078e2009-11-21 02:48:08 +00001989 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001990 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1991 return AbsDbgVariable;
1992}
1993
Devang Patelc50078e2009-11-21 02:48:08 +00001994/// collectVariableInfo - Populate DbgScope entries with variables' info.
1995void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001996 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001997
Devang Patel84139992009-10-06 01:26:37 +00001998 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1999 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2000 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel40c80212009-10-09 22:42:28 +00002001 MetadataBase *MB = VI->first;
2002 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel90a0fe32009-11-10 23:06:00 +00002003 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00002004 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00002005 std::pair< unsigned, MDNode *> VP = VI->second;
2006 DILocation ScopeLoc(VP.second);
2007
2008 DbgScope *Scope =
2009 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
2010 if (!Scope)
Jim Grosbach652b7432009-11-21 23:12:12 +00002011 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00002012 // If variable scope is not found then skip this variable.
2013 if (!Scope)
2014 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00002015
2016 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00002017 Scope->addVariable(RegVar);
Jim Grosbachb23f2422009-11-22 19:20:36 +00002018 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
2019 ScopeLoc))
Devang Patel90a0fe32009-11-10 23:06:00 +00002020 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel84139992009-10-06 01:26:37 +00002021 }
2022}
2023
Devang Patelc50078e2009-11-21 02:48:08 +00002024/// beginScope - Process beginning of a scope starting at Label.
2025void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00002026 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
2027 if (I == DbgScopeBeginMap.end())
2028 return;
Dan Gohman8d34f972009-11-23 21:30:55 +00002029 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002030 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00002031 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00002032 (*SDI)->setStartLabelID(Label);
2033}
2034
Devang Patelc50078e2009-11-21 02:48:08 +00002035/// endScope - Process end of a scope.
2036void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00002037 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00002038 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00002039 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00002040
2041 unsigned Label = MMI->NextLabelID();
2042 Asm->printLabel(Label);
Dan Gohmancfca6e32009-12-05 01:42:34 +00002043 O << '\n';
Devang Patel90a0fe32009-11-10 23:06:00 +00002044
Devang Patel393a46d2009-10-06 01:50:42 +00002045 SmallVector<DbgScope *, 2> &SD = I->second;
2046 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00002047 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00002048 (*SDI)->setEndLabelID(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00002049 return;
2050}
2051
2052/// createDbgScope - Create DbgScope for the scope.
2053void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
2054
2055 if (!InlinedAt) {
2056 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2057 if (WScope)
2058 return;
2059 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2060 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00002061 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00002062 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00002063 return;
2064 }
2065
2066 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2067 if (WScope)
2068 return;
2069
2070 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2071 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2072 DILocation DL(InlinedAt);
2073 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00002074}
2075
Devang Patelc50078e2009-11-21 02:48:08 +00002076/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00002077/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patelc50078e2009-11-21 02:48:08 +00002078bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patel6a260102009-10-01 20:31:14 +00002079 // If scope information was extracted using .dbg intrinsics then there is not
2080 // any need to extract these information by scanning each instruction.
2081 if (!DbgScopeMap.empty())
2082 return false;
2083
Devang Patel90a0fe32009-11-10 23:06:00 +00002084 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00002085 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2086 I != E; ++I) {
2087 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2088 II != IE; ++II) {
2089 const MachineInstr *MInsn = II;
2090 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00002091 if (DL.isUnknown()) continue;
Devang Patel6a260102009-10-01 20:31:14 +00002092 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel90a0fe32009-11-10 23:06:00 +00002093 if (!DLT.Scope) continue;
Devang Patel6a260102009-10-01 20:31:14 +00002094 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002095 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00002096 // into a scope DIE at the end.
Devang Patel90a0fe32009-11-10 23:06:00 +00002097 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2098 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
2099 }
2100 }
2101
2102
2103 // Build scope hierarchy using working set of scopes.
2104 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2105 I != E; ++I) {
2106 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2107 II != IE; ++II) {
2108 const MachineInstr *MInsn = II;
2109 DebugLoc DL = MInsn->getDebugLoc();
2110 if (DL.isUnknown()) continue;
2111 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
2112 if (!DLT.Scope) continue;
2113 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002114 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00002115 // into a scope DIE at the end.
2116 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2117 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
2118 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00002119 }
2120 }
2121
2122 // If a scope's last instruction is not set then use its child scope's
2123 // last instruction as this scope's last instrunction.
Devang Patelf5278f22009-10-27 20:47:17 +00002124 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00002125 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002126 if (DI->second->isAbstractScope())
2127 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002128 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patelc50078e2009-11-21 02:48:08 +00002129 DI->second->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +00002130 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2131 }
2132
2133 // Each scope has first instruction and last instruction to mark beginning
2134 // and end of a scope respectively. Create an inverse map that list scopes
2135 // starts (and ends) with an instruction. One instruction may start (or end)
2136 // multiple scopes.
Devang Patelf5278f22009-10-27 20:47:17 +00002137 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00002138 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2139 DbgScope *S = DI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002140 if (S->isAbstractScope())
2141 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002142 const MachineInstr *MI = S->getFirstInsn();
2143 assert (MI && "DbgScope does not have first instruction!");
2144
2145 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2146 if (IDI != DbgScopeBeginMap.end())
2147 IDI->second.push_back(S);
2148 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002149 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002150
2151 MI = S->getLastInsn();
2152 assert (MI && "DbgScope does not have last instruction!");
2153 IDI = DbgScopeEndMap.find(MI);
2154 if (IDI != DbgScopeEndMap.end())
2155 IDI->second.push_back(S);
2156 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002157 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002158 }
2159
2160 return !DbgScopeMap.empty();
2161}
2162
Devang Patelc50078e2009-11-21 02:48:08 +00002163/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002164/// emitted immediately after the function entry point.
Devang Patelc50078e2009-11-21 02:48:08 +00002165void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002166 this->MF = MF;
2167
2168 if (!ShouldEmitDwarfDebug()) return;
2169
2170 if (TimePassesIsEnabled)
2171 DebugTimer->startTimer();
2172
Devang Patelc50078e2009-11-21 02:48:08 +00002173 if (!extractScopeInformation(MF))
Devang Patel0feae422009-10-06 18:37:31 +00002174 return;
Devang Patelc50078e2009-11-21 02:48:08 +00002175
2176 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002177
Bill Wendlingf5839192009-05-20 23:19:06 +00002178 // Begin accumulating function debug information.
2179 MMI->BeginFunction(MF);
2180
2181 // Assumes in correct section after the entry point.
2182 EmitLabel("func_begin", ++SubprogramCount);
2183
2184 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2185 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002186 DebugLoc FDL = MF->getDefaultDebugLoc();
2187 if (!FDL.isUnknown()) {
2188 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2189 unsigned LabelID = 0;
Devang Patelfc1df342009-10-13 23:28:53 +00002190 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002191 if (!SP.isNull())
Devang Patelc50078e2009-11-21 02:48:08 +00002192 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002193 else
Devang Patelc50078e2009-11-21 02:48:08 +00002194 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002195 Asm->printLabel(LabelID);
2196 O << '\n';
Bill Wendlingf5839192009-05-20 23:19:06 +00002197 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002198 if (TimePassesIsEnabled)
2199 DebugTimer->stopTimer();
2200}
2201
Devang Patelc50078e2009-11-21 02:48:08 +00002202/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002203///
Devang Patelc50078e2009-11-21 02:48:08 +00002204void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002205 if (!ShouldEmitDwarfDebug()) return;
2206
2207 if (TimePassesIsEnabled)
2208 DebugTimer->startTimer();
2209
Devang Patel40c80212009-10-09 22:42:28 +00002210 if (DbgScopeMap.empty())
2211 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002212
Bill Wendlingf5839192009-05-20 23:19:06 +00002213 // Define end label for subprogram.
2214 EmitLabel("func_end", SubprogramCount);
2215
2216 // Get function line info.
2217 if (!Lines.empty()) {
2218 // Get section line info.
Chris Lattnerebd055c2009-08-03 23:20:21 +00002219 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00002220 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2221 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2222 // Append the function info to section info.
2223 SectionLineInfos.insert(SectionLineInfos.end(),
2224 Lines.begin(), Lines.end());
2225 }
2226
Devang Patel90a0fe32009-11-10 23:06:00 +00002227 // Construct abstract scopes.
2228 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach652b7432009-11-21 23:12:12 +00002229 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patelc50078e2009-11-21 02:48:08 +00002230 constructScopeDIE(*AI);
Bill Wendlingf5839192009-05-20 23:19:06 +00002231
Devang Patelc50078e2009-11-21 02:48:08 +00002232 constructScopeDIE(CurrentFnDbgScope);
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002233
Bill Wendlingf5839192009-05-20 23:19:06 +00002234 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2235 MMI->getFrameMoves()));
2236
2237 // Clear debug info
Devang Patel67533ab2009-12-01 18:13:48 +00002238 CurrentFnDbgScope = NULL;
2239 DbgScopeMap.clear();
2240 DbgScopeBeginMap.clear();
2241 DbgScopeEndMap.clear();
2242 ConcreteScopes.clear();
2243 AbstractScopesList.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002244
2245 Lines.clear();
Devang Patel67533ab2009-12-01 18:13:48 +00002246
Bill Wendlingf5839192009-05-20 23:19:06 +00002247 if (TimePassesIsEnabled)
2248 DebugTimer->stopTimer();
2249}
2250
Devang Patelc50078e2009-11-21 02:48:08 +00002251/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf5839192009-05-20 23:19:06 +00002252/// label. Returns a unique label ID used to generate a label and provide
2253/// correspondence to the source line list.
Jim Grosbach652b7432009-11-21 23:12:12 +00002254unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel946d0ae2009-10-05 18:03:19 +00002255 MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002256 if (!MMI)
2257 return 0;
2258
Bill Wendlingf5839192009-05-20 23:19:06 +00002259 if (TimePassesIsEnabled)
2260 DebugTimer->startTimer();
2261
Devang Patel7f75bbe2009-11-25 17:36:49 +00002262 StringRef Dir;
2263 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002264
2265 DIDescriptor Scope(S);
2266 if (Scope.isCompileUnit()) {
2267 DICompileUnit CU(S);
2268 Dir = CU.getDirectory();
2269 Fn = CU.getFilename();
2270 } else if (Scope.isSubprogram()) {
2271 DISubprogram SP(S);
2272 Dir = SP.getDirectory();
2273 Fn = SP.getFilename();
2274 } else if (Scope.isLexicalBlock()) {
2275 DILexicalBlock DB(S);
2276 Dir = DB.getDirectory();
2277 Fn = DB.getFilename();
2278 } else
2279 assert (0 && "Unexpected scope info");
2280
2281 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf5839192009-05-20 23:19:06 +00002282 unsigned ID = MMI->NextLabelID();
2283 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2284
2285 if (TimePassesIsEnabled)
2286 DebugTimer->stopTimer();
2287
2288 return ID;
2289}
2290
2291/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2292/// timed. Look up the source id with the given directory and source file
2293/// names. If none currently exists, create a new id and insert it in the
2294/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2295/// well.
2296unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2297 const std::string &FileName) {
2298 if (TimePassesIsEnabled)
2299 DebugTimer->startTimer();
2300
Devang Patelaaf012e2009-09-29 18:40:58 +00002301 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002302
2303 if (TimePassesIsEnabled)
2304 DebugTimer->stopTimer();
2305
2306 return SrcId;
2307}
2308
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002309//===----------------------------------------------------------------------===//
2310// Emit Methods
2311//===----------------------------------------------------------------------===//
2312
Devang Patelc50078e2009-11-21 02:48:08 +00002313/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002314///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002315unsigned
2316DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002317 // Get the children.
2318 const std::vector<DIE *> &Children = Die->getChildren();
2319
2320 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002321 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002322
2323 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002324 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002325
2326 // Get the abbreviation for this DIE.
2327 unsigned AbbrevNumber = Die->getAbbrevNumber();
2328 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2329
2330 // Set DIE offset
2331 Die->setOffset(Offset);
2332
2333 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002334 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002335
2336 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2337 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2338
2339 // Size the DIE attribute values.
2340 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2341 // Size attribute value.
2342 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2343
2344 // Size the DIE children if any.
2345 if (!Children.empty()) {
2346 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2347 "Children flag not set");
2348
2349 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002350 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002351
2352 // End of children marker.
2353 Offset += sizeof(int8_t);
2354 }
2355
2356 Die->setSize(Offset - Die->getOffset());
2357 return Offset;
2358}
2359
Devang Patelc50078e2009-11-21 02:48:08 +00002360/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002361///
Devang Patelc50078e2009-11-21 02:48:08 +00002362void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002363 // Compute size of compile unit header.
2364 static unsigned Offset =
2365 sizeof(int32_t) + // Length of Compilation Unit Info
2366 sizeof(int16_t) + // DWARF version number
2367 sizeof(int32_t) + // Offset Into Abbrev. Section
2368 sizeof(int8_t); // Pointer Size (in bytes)
2369
Devang Patelc50078e2009-11-21 02:48:08 +00002370 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002371 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002372}
2373
Devang Patelc50078e2009-11-21 02:48:08 +00002374/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002375/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002376void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002377 // Check to see if we already emitted intial headers.
2378 if (didInitial) return;
2379 didInitial = true;
2380
Chris Lattner73266f92009-08-19 05:49:37 +00002381 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002382
Bill Wendling55fccda2009-05-20 23:21:38 +00002383 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002384 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002385 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002386 EmitLabel("section_debug_frame", 0);
2387 }
2388
Chris Lattner73266f92009-08-19 05:49:37 +00002389 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002390 EmitLabel("section_info", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002391 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002392 EmitLabel("section_abbrev", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002393 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002394 EmitLabel("section_aranges", 0);
2395
Chris Lattner73266f92009-08-19 05:49:37 +00002396 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2397 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling55fccda2009-05-20 23:21:38 +00002398 EmitLabel("section_macinfo", 0);
2399 }
2400
Chris Lattner73266f92009-08-19 05:49:37 +00002401 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002402 EmitLabel("section_line", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002403 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002404 EmitLabel("section_loc", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002405 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002406 EmitLabel("section_pubnames", 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00002407 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2408 EmitLabel("section_pubtypes", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002409 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002410 EmitLabel("section_str", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002411 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002412 EmitLabel("section_ranges", 0);
2413
Chris Lattner73266f92009-08-19 05:49:37 +00002414 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002415 EmitLabel("text_begin", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002416 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002417 EmitLabel("data_begin", 0);
2418}
2419
Devang Patelc50078e2009-11-21 02:48:08 +00002420/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002421///
Devang Patelc50078e2009-11-21 02:48:08 +00002422void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002423 // Get the abbreviation for this DIE.
2424 unsigned AbbrevNumber = Die->getAbbrevNumber();
2425 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2426
2427 Asm->EOL();
2428
2429 // Emit the code (index) for the abbreviation.
2430 Asm->EmitULEB128Bytes(AbbrevNumber);
2431
2432 if (Asm->isVerbose())
2433 Asm->EOL(std::string("Abbrev [" +
2434 utostr(AbbrevNumber) +
2435 "] 0x" + utohexstr(Die->getOffset()) +
2436 ":0x" + utohexstr(Die->getSize()) + " " +
2437 dwarf::TagString(Abbrev->getTag())));
2438 else
2439 Asm->EOL();
2440
2441 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2442 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2443
2444 // Emit the DIE attribute values.
2445 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2446 unsigned Attr = AbbrevData[i].getAttribute();
2447 unsigned Form = AbbrevData[i].getForm();
2448 assert(Form && "Too many attributes for DIE (check abbreviation)");
2449
2450 switch (Attr) {
2451 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002452 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002453 break;
2454 case dwarf::DW_AT_abstract_origin: {
2455 DIEEntry *E = cast<DIEEntry>(Values[i]);
2456 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002457 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002458 Asm->EmitInt32(Addr);
2459 break;
2460 }
2461 default:
2462 // Emit an attribute using the defined form.
2463 Values[i]->EmitValue(this, Form);
2464 break;
2465 }
2466
2467 Asm->EOL(dwarf::AttributeString(Attr));
2468 }
2469
2470 // Emit the DIE children if any.
2471 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2472 const std::vector<DIE *> &Children = Die->getChildren();
2473
2474 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002475 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002476
2477 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2478 }
2479}
2480
Devang Patelc50078e2009-11-21 02:48:08 +00002481/// emitDebugInfo / emitDebugInfoPerCU - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002482///
Devang Patelc50078e2009-11-21 02:48:08 +00002483void DwarfDebug::emitDebugInfoPerCU(CompileUnit *Unit) {
Devang Pateld90672c2009-11-20 21:37:22 +00002484 DIE *Die = Unit->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002485
2486 // Emit the compile units header.
2487 EmitLabel("info_begin", Unit->getID());
2488
2489 // Emit size of content not including length itself
2490 unsigned ContentSize = Die->getSize() +
2491 sizeof(int16_t) + // DWARF version number
2492 sizeof(int32_t) + // Offset Into Abbrev. Section
2493 sizeof(int8_t) + // Pointer Size (in bytes)
2494 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2495
2496 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2497 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2498 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2499 Asm->EOL("Offset Into Abbrev. Section");
2500 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2501
Devang Patelc50078e2009-11-21 02:48:08 +00002502 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002503 // FIXME - extra padding for gdb bug.
2504 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2505 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2506 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2507 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2508 EmitLabel("info_end", Unit->getID());
2509
2510 Asm->EOL();
2511}
2512
Devang Patelc50078e2009-11-21 02:48:08 +00002513void DwarfDebug::emitDebugInfo() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002514 // Start debug info section.
Chris Lattner73266f92009-08-19 05:49:37 +00002515 Asm->OutStreamer.SwitchSection(
2516 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002517
Devang Patelc50078e2009-11-21 02:48:08 +00002518 emitDebugInfoPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002519}
2520
Devang Patelc50078e2009-11-21 02:48:08 +00002521/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002522///
Devang Patelc50078e2009-11-21 02:48:08 +00002523void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002524 // Check to see if it is worth the effort.
2525 if (!Abbreviations.empty()) {
2526 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002527 Asm->OutStreamer.SwitchSection(
2528 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002529
2530 EmitLabel("abbrev_begin", 0);
2531
2532 // For each abbrevation.
2533 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2534 // Get abbreviation data
2535 const DIEAbbrev *Abbrev = Abbreviations[i];
2536
2537 // Emit the abbrevations code (base 1 index.)
2538 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2539 Asm->EOL("Abbreviation Code");
2540
2541 // Emit the abbreviations data.
2542 Abbrev->Emit(Asm);
2543
2544 Asm->EOL();
2545 }
2546
2547 // Mark end of abbreviations.
2548 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2549
2550 EmitLabel("abbrev_end", 0);
2551 Asm->EOL();
2552 }
2553}
2554
Devang Patelc50078e2009-11-21 02:48:08 +00002555/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002556/// the line matrix.
2557///
Devang Patelc50078e2009-11-21 02:48:08 +00002558void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002559 // Define last address of section.
2560 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2561 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2562 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2563 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2564
2565 // Mark end of matrix.
2566 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2567 Asm->EmitULEB128Bytes(1); Asm->EOL();
2568 Asm->EmitInt8(1); Asm->EOL();
2569}
2570
Devang Patelc50078e2009-11-21 02:48:08 +00002571/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002572///
Devang Patelc50078e2009-11-21 02:48:08 +00002573void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002574 // If the target is using .loc/.file, the assembler will be emitting the
2575 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002576 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002577 return;
2578
2579 // Minimum line delta, thus ranging from -10..(255-10).
2580 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2581 // Maximum line delta, thus ranging from -10..(255-10).
2582 const int MaxLineDelta = 255 + MinLineDelta;
2583
2584 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002585 Asm->OutStreamer.SwitchSection(
2586 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002587
2588 // Construct the section header.
2589 EmitDifference("line_end", 0, "line_begin", 0, true);
2590 Asm->EOL("Length of Source Line Info");
2591 EmitLabel("line_begin", 0);
2592
2593 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2594
2595 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2596 Asm->EOL("Prolog Length");
2597 EmitLabel("line_prolog_begin", 0);
2598
2599 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2600
2601 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2602
2603 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2604
2605 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2606
2607 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2608
2609 // Line number standard opcode encodings argument count
2610 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2611 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2612 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2613 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2614 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2615 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2616 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2617 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2618 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2619
2620 // Emit directories.
2621 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2622 Asm->EmitString(getSourceDirectoryName(DI));
2623 Asm->EOL("Directory");
2624 }
2625
2626 Asm->EmitInt8(0); Asm->EOL("End of directories");
2627
2628 // Emit files.
2629 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2630 // Remember source id starts at 1.
2631 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2632 Asm->EmitString(getSourceFileName(Id.second));
2633 Asm->EOL("Source");
2634 Asm->EmitULEB128Bytes(Id.first);
2635 Asm->EOL("Directory #");
2636 Asm->EmitULEB128Bytes(0);
2637 Asm->EOL("Mod date");
2638 Asm->EmitULEB128Bytes(0);
2639 Asm->EOL("File size");
2640 }
2641
2642 Asm->EmitInt8(0); Asm->EOL("End of files");
2643
2644 EmitLabel("line_prolog_end", 0);
2645
2646 // A sequence for each text section.
2647 unsigned SecSrcLinesSize = SectionSourceLines.size();
2648
2649 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2650 // Isolate current sections line info.
2651 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2652
Chris Lattner26aabb92009-08-08 23:39:42 +00002653 /*if (Asm->isVerbose()) {
Chris Lattnere6ad12f2009-07-31 18:48:30 +00002654 const MCSection *S = SectionMap[j + 1];
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002655 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling55fccda2009-05-20 23:21:38 +00002656 << S->getName() << '\n';
Chris Lattner26aabb92009-08-08 23:39:42 +00002657 }*/
2658 Asm->EOL();
Bill Wendling55fccda2009-05-20 23:21:38 +00002659
2660 // Dwarf assumes we start with first line of first source file.
2661 unsigned Source = 1;
2662 unsigned Line = 1;
2663
2664 // Construct rows of the address, source, line, column matrix.
2665 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2666 const SrcLineInfo &LineInfo = LineInfos[i];
2667 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2668 if (!LabelID) continue;
2669
Caroline Tice9da96d82009-09-11 18:25:54 +00002670 if (LineInfo.getLine() == 0) continue;
2671
Bill Wendling55fccda2009-05-20 23:21:38 +00002672 if (!Asm->isVerbose())
2673 Asm->EOL();
2674 else {
2675 std::pair<unsigned, unsigned> SourceID =
2676 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002677 O << '\t' << MAI->getCommentString() << ' '
Dan Gohman1792bc62009-12-05 02:00:34 +00002678 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling55fccda2009-05-20 23:21:38 +00002679 << getSourceFileName(SourceID.second)
Dan Gohman1792bc62009-12-05 02:00:34 +00002680 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002681 }
2682
2683 // Define the line address.
2684 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2685 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2686 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2687 EmitReference("label", LabelID); Asm->EOL("Location label");
2688
2689 // If change of source, then switch to the new source.
2690 if (Source != LineInfo.getSourceID()) {
2691 Source = LineInfo.getSourceID();
2692 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2693 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2694 }
2695
2696 // If change of line.
2697 if (Line != LineInfo.getLine()) {
2698 // Determine offset.
2699 int Offset = LineInfo.getLine() - Line;
2700 int Delta = Offset - MinLineDelta;
2701
2702 // Update line.
2703 Line = LineInfo.getLine();
2704
2705 // If delta is small enough and in range...
2706 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2707 // ... then use fast opcode.
2708 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2709 } else {
2710 // ... otherwise use long hand.
2711 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2712 Asm->EOL("DW_LNS_advance_line");
2713 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2714 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2715 }
2716 } else {
2717 // Copy the previous row (different address or source)
2718 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2719 }
2720 }
2721
Devang Patelc50078e2009-11-21 02:48:08 +00002722 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002723 }
2724
2725 if (SecSrcLinesSize == 0)
2726 // Because we're emitting a debug_line section, we still need a line
2727 // table. The linker and friends expect it to exist. If there's nothing to
2728 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002729 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002730
2731 EmitLabel("line_end", 0);
2732 Asm->EOL();
2733}
2734
Devang Patelc50078e2009-11-21 02:48:08 +00002735/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002736///
Devang Patelc50078e2009-11-21 02:48:08 +00002737void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002738 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002739 return;
2740
2741 int stackGrowth =
2742 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2743 TargetFrameInfo::StackGrowsUp ?
2744 TD->getPointerSize() : -TD->getPointerSize();
2745
2746 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002747 Asm->OutStreamer.SwitchSection(
2748 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002749
2750 EmitLabel("debug_frame_common", 0);
2751 EmitDifference("debug_frame_common_end", 0,
2752 "debug_frame_common_begin", 0, true);
2753 Asm->EOL("Length of Common Information Entry");
2754
2755 EmitLabel("debug_frame_common_begin", 0);
2756 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2757 Asm->EOL("CIE Identifier Tag");
2758 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2759 Asm->EOL("CIE Version");
2760 Asm->EmitString("");
2761 Asm->EOL("CIE Augmentation");
2762 Asm->EmitULEB128Bytes(1);
2763 Asm->EOL("CIE Code Alignment Factor");
2764 Asm->EmitSLEB128Bytes(stackGrowth);
2765 Asm->EOL("CIE Data Alignment Factor");
2766 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2767 Asm->EOL("CIE RA Column");
2768
2769 std::vector<MachineMove> Moves;
2770 RI->getInitialFrameState(Moves);
2771
2772 EmitFrameMoves(NULL, 0, Moves, false);
2773
2774 Asm->EmitAlignment(2, 0, 0, false);
2775 EmitLabel("debug_frame_common_end", 0);
2776
2777 Asm->EOL();
2778}
2779
Devang Patelc50078e2009-11-21 02:48:08 +00002780/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002781/// section.
2782void
Devang Patelc50078e2009-11-21 02:48:08 +00002783DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002784 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002785 return;
2786
2787 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002788 Asm->OutStreamer.SwitchSection(
2789 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002790
2791 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2792 "debug_frame_begin", DebugFrameInfo.Number, true);
2793 Asm->EOL("Length of Frame Information Entry");
2794
2795 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2796
2797 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2798 0, 0, true, false);
2799 Asm->EOL("FDE CIE offset");
2800
2801 EmitReference("func_begin", DebugFrameInfo.Number);
2802 Asm->EOL("FDE initial location");
2803 EmitDifference("func_end", DebugFrameInfo.Number,
2804 "func_begin", DebugFrameInfo.Number);
2805 Asm->EOL("FDE address range");
2806
2807 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2808 false);
2809
2810 Asm->EmitAlignment(2, 0, 0, false);
2811 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2812
2813 Asm->EOL();
2814}
2815
Devang Patelc50078e2009-11-21 02:48:08 +00002816void DwarfDebug::emitDebugPubNamesPerCU(CompileUnit *Unit) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002817 EmitDifference("pubnames_end", Unit->getID(),
2818 "pubnames_begin", Unit->getID(), true);
2819 Asm->EOL("Length of Public Names Info");
2820
2821 EmitLabel("pubnames_begin", Unit->getID());
2822
2823 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2824
2825 EmitSectionOffset("info_begin", "section_info",
2826 Unit->getID(), 0, true, false);
2827 Asm->EOL("Offset of Compilation Unit Info");
2828
2829 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2830 true);
2831 Asm->EOL("Compilation Unit Length");
2832
Devang Patelec13b4f2009-11-24 01:14:22 +00002833 const StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00002834 for (StringMap<DIE*>::const_iterator
2835 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2836 const char *Name = GI->getKeyData();
2837 DIE * Entity = GI->second;
2838
2839 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2840 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2841 }
2842
2843 Asm->EmitInt32(0); Asm->EOL("End Mark");
2844 EmitLabel("pubnames_end", Unit->getID());
2845
2846 Asm->EOL();
2847}
2848
Devang Patelc50078e2009-11-21 02:48:08 +00002849/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002850///
Devang Patelc50078e2009-11-21 02:48:08 +00002851void DwarfDebug::emitDebugPubNames() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002852 // Start the dwarf pubnames section.
Chris Lattner73266f92009-08-19 05:49:37 +00002853 Asm->OutStreamer.SwitchSection(
2854 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002855
Devang Patelc50078e2009-11-21 02:48:08 +00002856 emitDebugPubNamesPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002857}
2858
Devang Patelec13b4f2009-11-24 01:14:22 +00002859void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00002860 // Start the dwarf pubnames section.
2861 Asm->OutStreamer.SwitchSection(
2862 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patelec13b4f2009-11-24 01:14:22 +00002863 EmitDifference("pubtypes_end", ModuleCU->getID(),
2864 "pubtypes_begin", ModuleCU->getID(), true);
2865 Asm->EOL("Length of Public Types Info");
2866
2867 EmitLabel("pubtypes_begin", ModuleCU->getID());
2868
2869 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2870
2871 EmitSectionOffset("info_begin", "section_info",
2872 ModuleCU->getID(), 0, true, false);
2873 Asm->EOL("Offset of Compilation ModuleCU Info");
2874
2875 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2876 true);
2877 Asm->EOL("Compilation ModuleCU Length");
2878
2879 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2880 for (StringMap<DIE*>::const_iterator
2881 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2882 const char *Name = GI->getKeyData();
2883 DIE * Entity = GI->second;
2884
2885 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2886 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2887 }
2888
2889 Asm->EmitInt32(0); Asm->EOL("End Mark");
2890 EmitLabel("pubtypes_end", ModuleCU->getID());
2891
2892 Asm->EOL();
2893}
2894
Devang Patelc50078e2009-11-21 02:48:08 +00002895/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002896///
Devang Patelc50078e2009-11-21 02:48:08 +00002897void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002898 // Check to see if it is worth the effort.
2899 if (!StringPool.empty()) {
2900 // Start the dwarf str section.
Chris Lattner73266f92009-08-19 05:49:37 +00002901 Asm->OutStreamer.SwitchSection(
2902 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002903
2904 // For each of strings in the string pool.
2905 for (unsigned StringID = 1, N = StringPool.size();
2906 StringID <= N; ++StringID) {
2907 // Emit a label for reference from debug information entries.
2908 EmitLabel("string", StringID);
2909
2910 // Emit the string itself.
2911 const std::string &String = StringPool[StringID];
2912 Asm->EmitString(String); Asm->EOL();
2913 }
2914
2915 Asm->EOL();
2916 }
2917}
2918
Devang Patelc50078e2009-11-21 02:48:08 +00002919/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002920///
Devang Patelc50078e2009-11-21 02:48:08 +00002921void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002922 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002923 Asm->OutStreamer.SwitchSection(
2924 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002925 Asm->EOL();
2926}
2927
2928/// EmitDebugARanges - Emit visible names into a debug aranges section.
2929///
2930void DwarfDebug::EmitDebugARanges() {
2931 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002932 Asm->OutStreamer.SwitchSection(
2933 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002934
2935 // FIXME - Mock up
2936#if 0
2937 CompileUnit *Unit = GetBaseCompileUnit();
2938
2939 // Don't include size of length
2940 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2941
2942 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2943
2944 EmitReference("info_begin", Unit->getID());
2945 Asm->EOL("Offset of Compilation Unit Info");
2946
2947 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2948
2949 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2950
2951 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2952 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2953
2954 // Range 1
2955 EmitReference("text_begin", 0); Asm->EOL("Address");
2956 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2957
2958 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2959 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2960#endif
2961
2962 Asm->EOL();
2963}
2964
Devang Patelc50078e2009-11-21 02:48:08 +00002965/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002966///
Devang Patelc50078e2009-11-21 02:48:08 +00002967void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002968 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002969 Asm->OutStreamer.SwitchSection(
2970 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002971 Asm->EOL();
2972}
2973
Devang Patelc50078e2009-11-21 02:48:08 +00002974/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002975///
Devang Patelc50078e2009-11-21 02:48:08 +00002976void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00002977 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00002978 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002979 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00002980 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002981 Asm->EOL();
2982 }
2983}
2984
Devang Patelc50078e2009-11-21 02:48:08 +00002985/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00002986/// Section Header:
2987/// 1. length of section
2988/// 2. Dwarf version number
2989/// 3. address size.
2990///
2991/// Entries (one "entry" for each function that was inlined):
2992///
2993/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2994/// otherwise offset into __debug_str for regular function name.
2995/// 2. offset into __debug_str section for regular function name.
2996/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2997/// instances for the function.
2998///
2999/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3000/// inlined instance; the die_offset points to the inlined_subroutine die in the
3001/// __debug_info section, and the low_pc is the starting address for the
3002/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00003003void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00003004 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003005 return;
3006
Devang Patel5a3d37f2009-06-29 20:45:18 +00003007 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00003008 return;
3009
Chris Lattner73266f92009-08-19 05:49:37 +00003010 Asm->OutStreamer.SwitchSection(
3011 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003012 Asm->EOL();
3013 EmitDifference("debug_inlined_end", 1,
3014 "debug_inlined_begin", 1, true);
3015 Asm->EOL("Length of Debug Inlined Information Entry");
3016
3017 EmitLabel("debug_inlined_begin", 1);
3018
3019 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
3020 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
3021
Devang Patel90a0fe32009-11-10 23:06:00 +00003022 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
3023 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00003024
Devang Patel90a0fe32009-11-10 23:06:00 +00003025// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
3026 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
3027 MDNode *Node = *I;
Jim Grosbachb23f2422009-11-22 19:20:36 +00003028 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
3029 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00003030 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00003031 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00003032 StringRef LName = SP.getLinkageName();
3033 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00003034
Devang Patel7f75bbe2009-11-25 17:36:49 +00003035 if (LName.empty())
Devang Patel76031e82009-07-16 01:01:22 +00003036 Asm->EmitString(Name);
3037 else {
Chris Lattner73266f92009-08-19 05:49:37 +00003038 // Skip special LLVM prefix that is used to inform the asm printer to not
3039 // emit usual symbol prefix before the symbol name. This happens for
3040 // Objective-C symbol names and symbol whose name is replaced using GCC's
3041 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00003042 if (LName[0] == 1)
Benjamin Kramer62b81882009-11-25 18:26:09 +00003043 LName = LName.substr(1);
Devang Patel90a0fe32009-11-10 23:06:00 +00003044// Asm->EmitString(LName);
3045 EmitSectionOffset("string", "section_str",
3046 StringPool.idFor(LName), false, true);
3047
Devang Patel76031e82009-07-16 01:01:22 +00003048 }
Bill Wendling55fccda2009-05-20 23:21:38 +00003049 Asm->EOL("MIPS linkage name");
Jim Grosbach652b7432009-11-21 23:12:12 +00003050// Asm->EmitString(Name);
Devang Patel90a0fe32009-11-10 23:06:00 +00003051 EmitSectionOffset("string", "section_str",
3052 StringPool.idFor(Name), false, true);
3053 Asm->EOL("Function name");
Bill Wendling55fccda2009-05-20 23:21:38 +00003054 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
3055
Devang Patel90a0fe32009-11-10 23:06:00 +00003056 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00003057 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00003058 DIE *SP = LI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00003059 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
3060
3061 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattnera5ef4d32009-08-22 21:43:10 +00003062 O << MAI->getData32bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00003063 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +00003064 O << MAI->getData64bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00003065
Devang Patel90a0fe32009-11-10 23:06:00 +00003066 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling55fccda2009-05-20 23:21:38 +00003067 }
3068 }
3069
3070 EmitLabel("debug_inlined_end", 1);
3071 Asm->EOL();
3072}