blob: 1b62f5e62eb01bf5c7579c2b990c74a17ada4417 [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Devang Patele4b27562009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
Chris Lattner334fd1f2009-09-16 00:08:41 +000028#include "llvm/Support/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/Support/Timer.h"
30#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000031using namespace llvm;
32
33static TimerGroup &getDwarfTimerGroup() {
34 static TimerGroup DwarfTimerGroup("Dwarf Debugging");
35 return DwarfTimerGroup;
36}
37
38//===----------------------------------------------------------------------===//
39
40/// Configuration values for initial hash set sizes (log2).
41///
42static const unsigned InitDiesSetSize = 9; // log2(512)
43static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
44static const unsigned InitValuesSetSize = 9; // log2(512)
45
46namespace llvm {
47
48//===----------------------------------------------------------------------===//
49/// CompileUnit - This dwarf writer support class manages information associate
50/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000051class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000052 /// ID - File identifier for source.
53 ///
54 unsigned ID;
55
56 /// Die - Compile unit debug information entry.
57 ///
58 DIE *Die;
59
60 /// GVToDieMap - Tracks the mapping of unit level debug informaton
61 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000062 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel017d1212009-11-20 21:37:22 +000063 ValueMap<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000064
65 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
66 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000067 /// FIXME : Rename
Devang Patel017d1212009-11-20 21:37:22 +000068 ValueMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000069
70 /// Globals - A map of globally visible named entities for this unit.
71 ///
72 StringMap<DIE*> Globals;
73
74 /// DiesSet - Used to uniquely define dies within the compile unit.
75 ///
76 FoldingSet<DIE> DiesSet;
77public:
78 CompileUnit(unsigned I, DIE *D)
Bill Wendling39dd6962009-05-20 23:31:45 +000079 : ID(I), Die(D), DiesSet(InitDiesSetSize) {}
80 ~CompileUnit() { delete Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000081
82 // Accessors.
Bill Wendling39dd6962009-05-20 23:31:45 +000083 unsigned getID() const { return ID; }
Devang Patel017d1212009-11-20 21:37:22 +000084 DIE* getCUDie() const { return Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000085 StringMap<DIE*> &getGlobals() { return Globals; }
86
87 /// hasContent - Return true if this compile unit has something to write out.
88 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000089 bool hasContent() const { return !Die->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +000090
91 /// AddGlobal - Add a new global entity to the compile unit.
92 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000093 void AddGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000094
Devang Patel017d1212009-11-20 21:37:22 +000095 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +000096 /// specified debug variable.
Devang Patel017d1212009-11-20 21:37:22 +000097 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
98
99 /// insertDIE - Insert DIE into the map.
100 void insertDIE(MDNode *N, DIE *D) {
101 GVToDieMap.insert(std::make_pair(N, D));
102 }
Bill Wendling0310d762009-05-15 09:23:25 +0000103
Devang Patel017d1212009-11-20 21:37:22 +0000104 /// getDIEEntry - Returns the debug information entry for the speciefied
105 /// debug variable.
106 DIEEntry *getDIEEntry(MDNode *N) { return GVToDIEEntryMap.lookup(N); }
107
108 /// insertDIEEntry - Insert debug information entry into the map.
109 void insertDIEEntry(MDNode *N, DIEEntry *E) {
110 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000111 }
112
113 /// AddDie - Adds or interns the DIE to the compile unit.
114 ///
115 DIE *AddDie(DIE &Buffer) {
116 FoldingSetNodeID ID;
117 Buffer.Profile(ID);
118 void *Where;
119 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
120
121 if (!Die) {
122 Die = new DIE(Buffer);
123 DiesSet.InsertNode(Die, Where);
124 this->Die->AddChild(Die);
125 Buffer.Detach();
126 }
127
128 return Die;
129 }
130};
131
132//===----------------------------------------------------------------------===//
133/// DbgVariable - This class is used to track local variable information.
134///
Devang Patelf76a3d62009-11-16 21:53:40 +0000135class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000136 DIVariable Var; // Variable Descriptor.
137 unsigned FrameIndex; // Variable frame index.
Devang Patel53bb5c92009-11-10 23:06:00 +0000138 DbgVariable *AbstractVar; // Abstract variable for this variable.
139 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000140public:
Devang Patel53bb5c92009-11-10 23:06:00 +0000141 DbgVariable(DIVariable V, unsigned I)
142 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000143
144 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000145 DIVariable getVariable() const { return Var; }
146 unsigned getFrameIndex() const { return FrameIndex; }
147 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
148 DbgVariable *getAbstractVariable() const { return AbstractVar; }
149 void setDIE(DIE *D) { TheDIE = D; }
150 DIE *getDIE() const { return TheDIE; }
Bill Wendling0310d762009-05-15 09:23:25 +0000151};
152
153//===----------------------------------------------------------------------===//
154/// DbgScope - This class is used to track scope information.
155///
Devang Patelf76a3d62009-11-16 21:53:40 +0000156class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000157 DbgScope *Parent; // Parent to this scope.
Devang Patel53bb5c92009-11-10 23:06:00 +0000158 DIDescriptor Desc; // Debug info descriptor for scope.
159 WeakVH InlinedAtLocation; // Location at which scope is inlined.
160 bool AbstractScope; // Abstract Scope
Bill Wendling0310d762009-05-15 09:23:25 +0000161 unsigned StartLabelID; // Label ID of the beginning of scope.
162 unsigned EndLabelID; // Label ID of the end of scope.
Devang Pateld38dd112009-10-01 18:25:23 +0000163 const MachineInstr *LastInsn; // Last instruction of this scope.
164 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000165 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
166 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000167
Owen Anderson04c05f72009-06-24 22:53:20 +0000168 // Private state for dump()
169 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000170public:
Devang Patelc90aefe2009-10-14 21:08:09 +0000171 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000172 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
173 StartLabelID(0), EndLabelID(0),
Devang Patelc90aefe2009-10-14 21:08:09 +0000174 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000175 virtual ~DbgScope();
176
177 // Accessors.
178 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000179 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000180 DIDescriptor getDesc() const { return Desc; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000181 MDNode *getInlinedAt() const {
182 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Patelc90aefe2009-10-14 21:08:09 +0000183 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000184 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000185 unsigned getStartLabelID() const { return StartLabelID; }
186 unsigned getEndLabelID() const { return EndLabelID; }
187 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
188 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendling0310d762009-05-15 09:23:25 +0000189 void setStartLabelID(unsigned S) { StartLabelID = S; }
190 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Pateld38dd112009-10-01 18:25:23 +0000191 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
192 const MachineInstr *getLastInsn() { return LastInsn; }
193 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000194 void setAbstractScope() { AbstractScope = true; }
195 bool isAbstractScope() const { return AbstractScope; }
Devang Pateld38dd112009-10-01 18:25:23 +0000196 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000197
Bill Wendling0310d762009-05-15 09:23:25 +0000198 /// AddScope - Add a scope to the scope.
199 ///
200 void AddScope(DbgScope *S) { Scopes.push_back(S); }
201
202 /// AddVariable - Add a variable to the scope.
203 ///
204 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
205
Devang Patelaf9e8472009-10-01 20:31:14 +0000206 void FixInstructionMarkers() {
207 assert (getFirstInsn() && "First instruction is missing!");
208 if (getLastInsn())
209 return;
210
211 // If a scope does not have an instruction to mark an end then use
212 // the end of last child scope.
213 SmallVector<DbgScope *, 4> &Scopes = getScopes();
214 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
215 DbgScope *L = Scopes.back();
216 if (!L->getLastInsn())
217 L->FixInstructionMarkers();
218 setLastInsn(L->getLastInsn());
219 }
220
Bill Wendling0310d762009-05-15 09:23:25 +0000221#ifndef NDEBUG
222 void dump() const;
223#endif
224};
225
226#ifndef NDEBUG
227void DbgScope::dump() const {
Chris Lattnerc281de12009-08-23 00:51:00 +0000228 raw_ostream &err = errs();
229 err.indent(IndentLevel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000230 MDNode *N = Desc.getNode();
231 N->dump();
Chris Lattnerc281de12009-08-23 00:51:00 +0000232 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel53bb5c92009-11-10 23:06:00 +0000233 if (AbstractScope)
234 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000235
236 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000237 if (!Scopes.empty())
238 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000239 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
240 if (Scopes[i] != this)
241 Scopes[i]->dump();
242
243 IndentLevel -= 2;
244}
245#endif
246
247//===----------------------------------------------------------------------===//
248/// DbgConcreteScope - This class is used to track a scope that holds concrete
249/// instance information.
250///
Nick Lewycky5f9843f2009-11-17 08:11:44 +0000251class DbgConcreteScope : public DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000252 CompileUnit *Unit;
253 DIE *Die; // Debug info for this concrete scope.
254public:
255 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
256
257 // Accessors.
258 DIE *getDie() const { return Die; }
259 void setDie(DIE *D) { Die = D; }
260};
261
262DbgScope::~DbgScope() {
263 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
264 delete Scopes[i];
265 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
266 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000267}
268
269} // end llvm namespace
270
Chris Lattneraf76e592009-08-22 20:48:53 +0000271DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000272 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000273 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000274 ValuesSet(InitValuesSetSize), Values(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000275 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel53bb5c92009-11-10 23:06:00 +0000276 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000277 if (TimePassesIsEnabled)
278 DebugTimer = new Timer("Dwarf Debug Writer",
279 getDwarfTimerGroup());
280}
281DwarfDebug::~DwarfDebug() {
282 for (unsigned j = 0, M = Values.size(); j < M; ++j)
283 delete Values[j];
284
Bill Wendling0310d762009-05-15 09:23:25 +0000285 delete DebugTimer;
286}
287
288/// AssignAbbrevNumber - Define a unique number for the abbreviation.
289///
290void DwarfDebug::AssignAbbrevNumber(DIEAbbrev &Abbrev) {
291 // Profile the node so that we can make it unique.
292 FoldingSetNodeID ID;
293 Abbrev.Profile(ID);
294
295 // Check the set for priors.
296 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
297
298 // If it's newly added.
299 if (InSet == &Abbrev) {
300 // Add to abbreviation list.
301 Abbreviations.push_back(&Abbrev);
302
303 // Assign the vector position + 1 as its number.
304 Abbrev.setNumber(Abbreviations.size());
305 } else {
306 // Assign existing abbreviation number.
307 Abbrev.setNumber(InSet->getNumber());
308 }
309}
310
Bill Wendling995f80a2009-05-20 23:24:48 +0000311/// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
312/// information entry.
313DIEEntry *DwarfDebug::CreateDIEEntry(DIE *Entry) {
Bill Wendling0310d762009-05-15 09:23:25 +0000314 DIEEntry *Value;
315
316 if (Entry) {
317 FoldingSetNodeID ID;
318 DIEEntry::Profile(ID, Entry);
319 void *Where;
320 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
321
322 if (Value) return Value;
323
324 Value = new DIEEntry(Entry);
325 ValuesSet.InsertNode(Value, Where);
326 } else {
327 Value = new DIEEntry(Entry);
328 }
329
330 Values.push_back(Value);
331 return Value;
332}
333
334/// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
335///
336void DwarfDebug::SetDIEEntry(DIEEntry *Value, DIE *Entry) {
337 Value->setEntry(Entry);
338
339 // Add to values set if not already there. If it is, we merely have a
340 // duplicate in the values list (no harm.)
341 ValuesSet.GetOrInsertNode(Value);
342}
343
344/// AddUInt - Add an unsigned integer attribute data and value.
345///
346void DwarfDebug::AddUInt(DIE *Die, unsigned Attribute,
347 unsigned Form, uint64_t Integer) {
348 if (!Form) Form = DIEInteger::BestForm(false, Integer);
349
350 FoldingSetNodeID ID;
351 DIEInteger::Profile(ID, Integer);
352 void *Where;
353 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
354
355 if (!Value) {
356 Value = new DIEInteger(Integer);
357 ValuesSet.InsertNode(Value, Where);
358 Values.push_back(Value);
359 }
360
361 Die->AddValue(Attribute, Form, Value);
362}
363
364/// AddSInt - Add an signed integer attribute data and value.
365///
366void DwarfDebug::AddSInt(DIE *Die, unsigned Attribute,
367 unsigned Form, int64_t Integer) {
368 if (!Form) Form = DIEInteger::BestForm(true, Integer);
369
370 FoldingSetNodeID ID;
371 DIEInteger::Profile(ID, (uint64_t)Integer);
372 void *Where;
373 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
374
375 if (!Value) {
376 Value = new DIEInteger(Integer);
377 ValuesSet.InsertNode(Value, Where);
378 Values.push_back(Value);
379 }
380
381 Die->AddValue(Attribute, Form, Value);
382}
383
384/// AddString - Add a string attribute data and value.
385///
386void DwarfDebug::AddString(DIE *Die, unsigned Attribute, unsigned Form,
387 const std::string &String) {
388 FoldingSetNodeID ID;
389 DIEString::Profile(ID, String);
390 void *Where;
391 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
392
393 if (!Value) {
394 Value = new DIEString(String);
395 ValuesSet.InsertNode(Value, Where);
396 Values.push_back(Value);
397 }
398
399 Die->AddValue(Attribute, Form, Value);
400}
401
402/// AddLabel - Add a Dwarf label attribute data and value.
403///
404void DwarfDebug::AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
405 const DWLabel &Label) {
406 FoldingSetNodeID ID;
407 DIEDwarfLabel::Profile(ID, Label);
408 void *Where;
409 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
410
411 if (!Value) {
412 Value = new DIEDwarfLabel(Label);
413 ValuesSet.InsertNode(Value, Where);
414 Values.push_back(Value);
415 }
416
417 Die->AddValue(Attribute, Form, Value);
418}
419
420/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
421///
422void DwarfDebug::AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
423 const std::string &Label) {
424 FoldingSetNodeID ID;
425 DIEObjectLabel::Profile(ID, Label);
426 void *Where;
427 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
428
429 if (!Value) {
430 Value = new DIEObjectLabel(Label);
431 ValuesSet.InsertNode(Value, Where);
432 Values.push_back(Value);
433 }
434
435 Die->AddValue(Attribute, Form, Value);
436}
437
438/// AddSectionOffset - Add a section offset label attribute data and value.
439///
440void DwarfDebug::AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
441 const DWLabel &Label, const DWLabel &Section,
442 bool isEH, bool useSet) {
443 FoldingSetNodeID ID;
444 DIESectionOffset::Profile(ID, Label, Section);
445 void *Where;
446 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
447
448 if (!Value) {
449 Value = new DIESectionOffset(Label, Section, isEH, useSet);
450 ValuesSet.InsertNode(Value, Where);
451 Values.push_back(Value);
452 }
453
454 Die->AddValue(Attribute, Form, Value);
455}
456
457/// AddDelta - Add a label delta attribute data and value.
458///
459void DwarfDebug::AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
460 const DWLabel &Hi, const DWLabel &Lo) {
461 FoldingSetNodeID ID;
462 DIEDelta::Profile(ID, Hi, Lo);
463 void *Where;
464 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
465
466 if (!Value) {
467 Value = new DIEDelta(Hi, Lo);
468 ValuesSet.InsertNode(Value, Where);
469 Values.push_back(Value);
470 }
471
472 Die->AddValue(Attribute, Form, Value);
473}
474
475/// AddBlock - Add block data.
476///
477void DwarfDebug::AddBlock(DIE *Die, unsigned Attribute, unsigned Form,
478 DIEBlock *Block) {
479 Block->ComputeSize(TD);
480 FoldingSetNodeID ID;
481 Block->Profile(ID);
482 void *Where;
483 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
484
485 if (!Value) {
486 Value = Block;
487 ValuesSet.InsertNode(Value, Where);
488 Values.push_back(Value);
489 } else {
490 // Already exists, reuse the previous one.
491 delete Block;
492 Block = cast<DIEBlock>(Value);
493 }
494
495 Die->AddValue(Attribute, Block->BestForm(), Value);
496}
497
498/// AddSourceLine - Add location information to specified debug information
499/// entry.
500void DwarfDebug::AddSourceLine(DIE *Die, const DIVariable *V) {
501 // If there is no compile unit specified, don't add a line #.
502 if (V->getCompileUnit().isNull())
503 return;
504
505 unsigned Line = V->getLineNumber();
506 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
507 assert(FileID && "Invalid file id");
508 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
509 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
510}
511
512/// AddSourceLine - Add location information to specified debug information
513/// entry.
514void DwarfDebug::AddSourceLine(DIE *Die, const DIGlobal *G) {
515 // If there is no compile unit specified, don't add a line #.
516 if (G->getCompileUnit().isNull())
517 return;
518
519 unsigned Line = G->getLineNumber();
520 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
521 assert(FileID && "Invalid file id");
522 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
523 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
524}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000525
526/// AddSourceLine - Add location information to specified debug information
527/// entry.
528void DwarfDebug::AddSourceLine(DIE *Die, const DISubprogram *SP) {
529 // If there is no compile unit specified, don't add a line #.
530 if (SP->getCompileUnit().isNull())
531 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000532 // If the line number is 0, don't add it.
533 if (SP->getLineNumber() == 0)
534 return;
535
Devang Patel82dfc0c2009-08-31 22:47:13 +0000536
537 unsigned Line = SP->getLineNumber();
538 unsigned FileID = FindCompileUnit(SP->getCompileUnit()).getID();
539 assert(FileID && "Invalid file id");
540 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
541 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
542}
543
544/// AddSourceLine - Add location information to specified debug information
545/// entry.
Bill Wendling0310d762009-05-15 09:23:25 +0000546void DwarfDebug::AddSourceLine(DIE *Die, const DIType *Ty) {
547 // If there is no compile unit specified, don't add a line #.
548 DICompileUnit CU = Ty->getCompileUnit();
549 if (CU.isNull())
550 return;
551
552 unsigned Line = Ty->getLineNumber();
553 unsigned FileID = FindCompileUnit(CU).getID();
554 assert(FileID && "Invalid file id");
555 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
556 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
557}
558
Caroline Ticedc8f6042009-08-31 21:19:37 +0000559/* Byref variables, in Blocks, are declared by the programmer as
560 "SomeType VarName;", but the compiler creates a
561 __Block_byref_x_VarName struct, and gives the variable VarName
562 either the struct, or a pointer to the struct, as its type. This
563 is necessary for various behind-the-scenes things the compiler
564 needs to do with by-reference variables in blocks.
565
566 However, as far as the original *programmer* is concerned, the
567 variable should still have type 'SomeType', as originally declared.
568
569 The following function dives into the __Block_byref_x_VarName
570 struct to find the original type of the variable. This will be
571 passed back to the code generating the type for the Debug
572 Information Entry for the variable 'VarName'. 'VarName' will then
573 have the original type 'SomeType' in its debug information.
574
575 The original type 'SomeType' will be the type of the field named
576 'VarName' inside the __Block_byref_x_VarName struct.
577
578 NOTE: In order for this to not completely fail on the debugger
579 side, the Debug Information Entry for the variable VarName needs to
580 have a DW_AT_location that tells the debugger how to unwind through
581 the pointers and __Block_byref_x_VarName struct to find the actual
582 value of the variable. The function AddBlockByrefType does this. */
583
584/// Find the type the programmer originally declared the variable to be
585/// and return that type.
586///
587DIType DwarfDebug::GetBlockByrefType(DIType Ty, std::string Name) {
588
589 DIType subType = Ty;
590 unsigned tag = Ty.getTag();
591
592 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000593 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000594 subType = DTy.getTypeDerivedFrom();
595 }
596
597 DICompositeType blockStruct = DICompositeType(subType.getNode());
598
599 DIArray Elements = blockStruct.getTypeArray();
600
601 if (Elements.isNull())
602 return Ty;
603
604 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
605 DIDescriptor Element = Elements.getElement(i);
606 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel5ccdd102009-09-29 18:40:58 +0000607 if (strcmp(Name.c_str(), DT.getName()) == 0)
Caroline Ticedc8f6042009-08-31 21:19:37 +0000608 return (DT.getTypeDerivedFrom());
609 }
610
611 return Ty;
612}
613
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000614/// AddComplexAddress - Start with the address based on the location provided,
615/// and generate the DWARF information necessary to find the actual variable
616/// given the extra address information encoded in the DIVariable, starting from
617/// the starting location. Add the DWARF information to the die.
618///
619void DwarfDebug::AddComplexAddress(DbgVariable *&DV, DIE *Die,
620 unsigned Attribute,
621 const MachineLocation &Location) {
622 const DIVariable &VD = DV->getVariable();
623 DIType Ty = VD.getType();
624
625 // Decode the original location, and use that as the start of the byref
626 // variable's location.
627 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
628 DIEBlock *Block = new DIEBlock();
629
630 if (Location.isReg()) {
631 if (Reg < 32) {
632 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
633 } else {
634 Reg = Reg - dwarf::DW_OP_reg0;
635 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
636 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
637 }
638 } else {
639 if (Reg < 32)
640 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
641 else {
642 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
643 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
644 }
645
646 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
647 }
648
649 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
650 uint64_t Element = VD.getAddrElement(i);
651
652 if (Element == DIFactory::OpPlus) {
653 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
654 AddUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
655 } else if (Element == DIFactory::OpDeref) {
656 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
657 } else llvm_unreachable("unknown DIFactory Opcode");
658 }
659
660 // Now attach the location information to the DIE.
661 AddBlock(Die, Attribute, 0, Block);
662}
663
Caroline Ticedc8f6042009-08-31 21:19:37 +0000664/* Byref variables, in Blocks, are declared by the programmer as "SomeType
665 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
666 gives the variable VarName either the struct, or a pointer to the struct, as
667 its type. This is necessary for various behind-the-scenes things the
668 compiler needs to do with by-reference variables in Blocks.
669
670 However, as far as the original *programmer* is concerned, the variable
671 should still have type 'SomeType', as originally declared.
672
673 The function GetBlockByrefType dives into the __Block_byref_x_VarName
674 struct to find the original type of the variable, which is then assigned to
675 the variable's Debug Information Entry as its real type. So far, so good.
676 However now the debugger will expect the variable VarName to have the type
677 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000678 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000679 pointers and struct to find the actual variable of type SomeType.
680
681 The following function does just that. We start by getting
682 the "normal" location for the variable. This will be the location
683 of either the struct __Block_byref_x_VarName or the pointer to the
684 struct __Block_byref_x_VarName.
685
686 The struct will look something like:
687
688 struct __Block_byref_x_VarName {
689 ... <various fields>
690 struct __Block_byref_x_VarName *forwarding;
691 ... <various other fields>
692 SomeType VarName;
693 ... <maybe more fields>
694 };
695
696 If we are given the struct directly (as our starting point) we
697 need to tell the debugger to:
698
699 1). Add the offset of the forwarding field.
700
701 2). Follow that pointer to get the the real __Block_byref_x_VarName
702 struct to use (the real one may have been copied onto the heap).
703
704 3). Add the offset for the field VarName, to find the actual variable.
705
706 If we started with a pointer to the struct, then we need to
707 dereference that pointer first, before the other steps.
708 Translating this into DWARF ops, we will need to append the following
709 to the current location description for the variable:
710
711 DW_OP_deref -- optional, if we start with a pointer
712 DW_OP_plus_uconst <forward_fld_offset>
713 DW_OP_deref
714 DW_OP_plus_uconst <varName_fld_offset>
715
716 That is what this function does. */
717
718/// AddBlockByrefAddress - Start with the address based on the location
719/// provided, and generate the DWARF information necessary to find the
720/// actual Block variable (navigating the Block struct) based on the
721/// starting location. Add the DWARF information to the die. For
722/// more information, read large comment just above here.
723///
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000724void DwarfDebug::AddBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000725 unsigned Attribute,
726 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000727 const DIVariable &VD = DV->getVariable();
728 DIType Ty = VD.getType();
729 DIType TmpTy = Ty;
730 unsigned Tag = Ty.getTag();
731 bool isPointer = false;
732
Devang Patel5ccdd102009-09-29 18:40:58 +0000733 const char *varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000734
735 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000736 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000737 TmpTy = DTy.getTypeDerivedFrom();
738 isPointer = true;
739 }
740
741 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
742
Daniel Dunbar00564992009-09-19 20:40:14 +0000743 // Find the __forwarding field and the variable field in the __Block_byref
744 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000745 DIArray Fields = blockStruct.getTypeArray();
746 DIDescriptor varField = DIDescriptor();
747 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000748
749
Daniel Dunbar00564992009-09-19 20:40:14 +0000750 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
751 DIDescriptor Element = Fields.getElement(i);
752 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel5ccdd102009-09-29 18:40:58 +0000753 const char *fieldName = DT.getName();
754 if (strcmp(fieldName, "__forwarding") == 0)
Daniel Dunbar00564992009-09-19 20:40:14 +0000755 forwardingField = Element;
Devang Patel5ccdd102009-09-29 18:40:58 +0000756 else if (strcmp(fieldName, varName) == 0)
Daniel Dunbar00564992009-09-19 20:40:14 +0000757 varField = Element;
758 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000759
Mike Stump7e3720d2009-09-24 23:21:26 +0000760 assert(!varField.isNull() && "Can't find byref variable in Block struct");
761 assert(!forwardingField.isNull()
762 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000763
Daniel Dunbar00564992009-09-19 20:40:14 +0000764 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000765 unsigned int forwardingFieldOffset =
766 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
767 unsigned int varFieldOffset =
768 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000769
Mike Stump7e3720d2009-09-24 23:21:26 +0000770 // Decode the original location, and use that as the start of the byref
771 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000772 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
773 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000774
Daniel Dunbar00564992009-09-19 20:40:14 +0000775 if (Location.isReg()) {
776 if (Reg < 32)
777 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
778 else {
779 Reg = Reg - dwarf::DW_OP_reg0;
780 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
781 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
782 }
783 } else {
784 if (Reg < 32)
785 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
786 else {
787 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
788 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
789 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000790
Daniel Dunbar00564992009-09-19 20:40:14 +0000791 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
792 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000793
Mike Stump7e3720d2009-09-24 23:21:26 +0000794 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000795 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000796 if (isPointer)
797 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000798
Daniel Dunbar00564992009-09-19 20:40:14 +0000799 // Next add the offset for the '__forwarding' field:
800 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
801 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000802 if (forwardingFieldOffset > 0) {
803 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
804 AddUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
805 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000806
Daniel Dunbar00564992009-09-19 20:40:14 +0000807 // Now dereference the __forwarding field to get to the real __Block_byref
808 // struct: DW_OP_deref.
Daniel Dunbar00564992009-09-19 20:40:14 +0000809 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000810
Daniel Dunbar00564992009-09-19 20:40:14 +0000811 // Now that we've got the real __Block_byref... struct, add the offset
812 // for the variable's field to get to the location of the actual variable:
813 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000814 if (varFieldOffset > 0) {
815 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
816 AddUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
817 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000818
Daniel Dunbar00564992009-09-19 20:40:14 +0000819 // Now attach the location information to the DIE.
Daniel Dunbar00564992009-09-19 20:40:14 +0000820 AddBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000821}
822
Bill Wendling0310d762009-05-15 09:23:25 +0000823/// AddAddress - Add an address attribute to a die based on the location
824/// provided.
825void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute,
826 const MachineLocation &Location) {
827 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
828 DIEBlock *Block = new DIEBlock();
829
830 if (Location.isReg()) {
831 if (Reg < 32) {
832 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
833 } else {
834 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
835 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
836 }
837 } else {
838 if (Reg < 32) {
839 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
840 } else {
841 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
842 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
843 }
844
845 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
846 }
847
848 AddBlock(Die, Attribute, 0, Block);
849}
850
851/// AddType - Add a new type attribute to the specified entity.
852void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
853 if (Ty.isNull())
854 return;
855
856 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +0000857 DIEEntry *Slot = DW_Unit->getDIEEntry(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000858
859 // If it exists then use the existing value.
860 if (Slot) {
861 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
862 return;
863 }
864
865 // Set up proxy.
Bill Wendling995f80a2009-05-20 23:24:48 +0000866 Slot = CreateDIEEntry();
Devang Patel017d1212009-11-20 21:37:22 +0000867 DW_Unit->insertDIEEntry(Ty.getNode(), Slot);
Bill Wendling0310d762009-05-15 09:23:25 +0000868
869 // Construct type.
870 DIE Buffer(dwarf::DW_TAG_base_type);
Devang Patel6ceea332009-08-31 18:49:10 +0000871 if (Ty.isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000872 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getNode()));
Devang Patel6ceea332009-08-31 18:49:10 +0000873 else if (Ty.isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000874 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000875 else {
Devang Patel6ceea332009-08-31 18:49:10 +0000876 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patele4b27562009-08-28 23:24:31 +0000877 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000878 }
879
880 // Add debug information entry to entity and appropriate context.
881 DIE *Die = NULL;
882 DIDescriptor Context = Ty.getContext();
883 if (!Context.isNull())
Devang Patel017d1212009-11-20 21:37:22 +0000884 Die = DW_Unit->getDIE(Context.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000885
886 if (Die) {
887 DIE *Child = new DIE(Buffer);
888 Die->AddChild(Child);
889 Buffer.Detach();
890 SetDIEEntry(Slot, Child);
891 } else {
892 Die = DW_Unit->AddDie(Buffer);
893 SetDIEEntry(Slot, Die);
894 }
895
896 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
897}
898
899/// ConstructTypeDIE - Construct basic type die from DIBasicType.
900void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
901 DIBasicType BTy) {
902 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000903 const char *Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000904 Buffer.setTag(dwarf::DW_TAG_base_type);
905 AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
906 BTy.getEncoding());
907
908 // Add name if not anonymous or intermediate type.
Devang Patel5ccdd102009-09-29 18:40:58 +0000909 if (Name)
Bill Wendling0310d762009-05-15 09:23:25 +0000910 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
911 uint64_t Size = BTy.getSizeInBits() >> 3;
912 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
913}
914
915/// ConstructTypeDIE - Construct derived type die from DIDerivedType.
916void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
917 DIDerivedType DTy) {
918 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000919 const char *Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000920 uint64_t Size = DTy.getSizeInBits() >> 3;
921 unsigned Tag = DTy.getTag();
922
923 // FIXME - Workaround for templates.
924 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
925
926 Buffer.setTag(Tag);
927
928 // Map to main type, void will not have a type.
929 DIType FromTy = DTy.getTypeDerivedFrom();
930 AddType(DW_Unit, &Buffer, FromTy);
931
932 // Add name if not anonymous or intermediate type.
Devang Patel808b8262009-10-16 21:27:43 +0000933 if (Name && Tag != dwarf::DW_TAG_pointer_type)
Bill Wendling0310d762009-05-15 09:23:25 +0000934 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
935
936 // Add size if non-zero (derived types might be zero-sized.)
937 if (Size)
938 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
939
940 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel1f37b5b2009-11-20 21:05:37 +0000941 if (!DTy.isForwardDecl() && Tag != dwarf::DW_TAG_pointer_type)
Bill Wendling0310d762009-05-15 09:23:25 +0000942 AddSourceLine(&Buffer, &DTy);
943}
944
945/// ConstructTypeDIE - Construct type DIE from DICompositeType.
946void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
947 DICompositeType CTy) {
948 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000949 const char *Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000950
951 uint64_t Size = CTy.getSizeInBits() >> 3;
952 unsigned Tag = CTy.getTag();
953 Buffer.setTag(Tag);
954
955 switch (Tag) {
956 case dwarf::DW_TAG_vector_type:
957 case dwarf::DW_TAG_array_type:
958 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
959 break;
960 case dwarf::DW_TAG_enumeration_type: {
961 DIArray Elements = CTy.getTypeArray();
962
963 // Add enumerators to enumeration type.
964 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
965 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000966 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelc5254722009-10-09 17:51:49 +0000967 if (!Enum.isNull()) {
968 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
969 Buffer.AddChild(ElemDie);
970 }
Bill Wendling0310d762009-05-15 09:23:25 +0000971 }
972 }
973 break;
974 case dwarf::DW_TAG_subroutine_type: {
975 // Add return type.
976 DIArray Elements = CTy.getTypeArray();
977 DIDescriptor RTy = Elements.getElement(0);
Devang Patele4b27562009-08-28 23:24:31 +0000978 AddType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000979
980 // Add prototype flag.
981 AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
982
983 // Add arguments.
984 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
985 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
986 DIDescriptor Ty = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000987 AddType(DW_Unit, Arg, DIType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000988 Buffer.AddChild(Arg);
989 }
990 }
991 break;
992 case dwarf::DW_TAG_structure_type:
993 case dwarf::DW_TAG_union_type:
994 case dwarf::DW_TAG_class_type: {
995 // Add elements to structure type.
996 DIArray Elements = CTy.getTypeArray();
997
998 // A forward struct declared type may not have elements available.
999 if (Elements.isNull())
1000 break;
1001
1002 // Add elements to structure type.
1003 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1004 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001005 if (Element.isNull())
1006 continue;
Bill Wendling0310d762009-05-15 09:23:25 +00001007 DIE *ElemDie = NULL;
1008 if (Element.getTag() == dwarf::DW_TAG_subprogram)
1009 ElemDie = CreateSubprogramDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +00001010 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001011 else
1012 ElemDie = CreateMemberDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +00001013 DIDerivedType(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001014 Buffer.AddChild(ElemDie);
1015 }
1016
Devang Patela1ba2692009-08-27 23:51:51 +00001017 if (CTy.isAppleBlockExtension())
Bill Wendling0310d762009-05-15 09:23:25 +00001018 AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
1019
1020 unsigned RLang = CTy.getRunTimeLang();
1021 if (RLang)
1022 AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1023 dwarf::DW_FORM_data1, RLang);
1024 break;
1025 }
1026 default:
1027 break;
1028 }
1029
1030 // Add name if not anonymous or intermediate type.
Devang Patel5ccdd102009-09-29 18:40:58 +00001031 if (Name)
Bill Wendling0310d762009-05-15 09:23:25 +00001032 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1033
1034 if (Tag == dwarf::DW_TAG_enumeration_type ||
1035 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1036 // Add size if non-zero (derived types might be zero-sized.)
1037 if (Size)
1038 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
1039 else {
1040 // Add zero size if it is not a forward declaration.
1041 if (CTy.isForwardDecl())
1042 AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1043 else
1044 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1045 }
1046
1047 // Add source line info if available.
1048 if (!CTy.isForwardDecl())
1049 AddSourceLine(&Buffer, &CTy);
1050 }
1051}
1052
1053/// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1054void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
1055 int64_t L = SR.getLo();
1056 int64_t H = SR.getHi();
1057 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1058
Devang Patel6325a532009-08-14 20:59:16 +00001059 AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
1060 if (L)
1061 AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
1062 if (H)
Daniel Dunbar00564992009-09-19 20:40:14 +00001063 AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001064
1065 Buffer.AddChild(DW_Subrange);
1066}
1067
1068/// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1069void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1070 DICompositeType *CTy) {
1071 Buffer.setTag(dwarf::DW_TAG_array_type);
1072 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1073 AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
1074
1075 // Emit derived type.
1076 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
1077 DIArray Elements = CTy->getTypeArray();
1078
1079 // Construct an anonymous type for index type.
1080 DIE IdxBuffer(dwarf::DW_TAG_base_type);
1081 AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1082 AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1083 dwarf::DW_ATE_signed);
1084 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1085
1086 // Add subranges to array type.
1087 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1088 DIDescriptor Element = Elements.getElement(i);
1089 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patele4b27562009-08-28 23:24:31 +00001090 ConstructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IndexTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001091 }
1092}
1093
1094/// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1095DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
1096 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel5ccdd102009-09-29 18:40:58 +00001097 const char *Name = ETy->getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001098 AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1099 int64_t Value = ETy->getEnumValue();
1100 AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1101 return Enumerator;
1102}
1103
1104/// CreateGlobalVariableDIE - Create new DIE using GV.
1105DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit,
1106 const DIGlobalVariable &GV) {
Devang Patel465c3be2009-11-06 01:30:04 +00001107 // If the global variable was optmized out then no need to create debug info entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001108 if (!GV.getGlobal()) return NULL;
1109 if (!GV.getDisplayName()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001110
Bill Wendling0310d762009-05-15 09:23:25 +00001111 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Devang Patel5ccdd102009-09-29 18:40:58 +00001112 AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1113 GV.getDisplayName());
1114
1115 const char *LinkageName = GV.getLinkageName();
1116 if (LinkageName) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001117 // Skip special LLVM prefix that is used to inform the asm printer to not
1118 // emit usual symbol prefix before the symbol name. This happens for
1119 // Objective-C symbol names and symbol whose name is replaced using GCC's
1120 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001121 if (LinkageName[0] == 1)
1122 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001123 AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001124 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001125 }
Daniel Dunbar00564992009-09-19 20:40:14 +00001126 AddType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001127 if (!GV.isLocalToUnit())
1128 AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1129 AddSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001130
1131 // Add address.
1132 DIEBlock *Block = new DIEBlock();
1133 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1134 AddObjectLabel(Block, 0, dwarf::DW_FORM_udata,
1135 Asm->Mang->getMangledName(GV.getGlobal()));
1136 AddBlock(GVDie, dwarf::DW_AT_location, 0, Block);
1137
Bill Wendling0310d762009-05-15 09:23:25 +00001138 return GVDie;
1139}
1140
1141/// CreateMemberDIE - Create new member DIE.
1142DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
1143 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel5ccdd102009-09-29 18:40:58 +00001144 if (const char *Name = DT.getName())
Bill Wendling0310d762009-05-15 09:23:25 +00001145 AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1146
1147 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1148
1149 AddSourceLine(MemberDie, &DT);
1150
Devang Patel33db5082009-11-04 22:06:12 +00001151 DIEBlock *MemLocationDie = new DIEBlock();
1152 AddUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1153
Bill Wendling0310d762009-05-15 09:23:25 +00001154 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001155 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001156
1157 if (Size != FieldSize) {
1158 // Handle bitfield.
1159 AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1160 AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1161
1162 uint64_t Offset = DT.getOffsetInBits();
1163 uint64_t FieldOffset = Offset;
1164 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1165 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1166 FieldOffset = (HiMark - FieldSize);
1167 Offset -= FieldOffset;
1168
1169 // Maybe we need to work from the other end.
1170 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1171 AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001172
Devang Patel33db5082009-11-04 22:06:12 +00001173 // Here WD_AT_data_member_location points to the anonymous
1174 // field that includes this bit field.
1175 AddUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1176
1177 } else
1178 // This is not a bitfield.
1179 AddUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1180
1181 AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001182
1183 if (DT.isProtected())
1184 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1185 dwarf::DW_ACCESS_protected);
1186 else if (DT.isPrivate())
1187 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1188 dwarf::DW_ACCESS_private);
1189
1190 return MemberDie;
1191}
1192
1193/// CreateSubprogramDIE - Create new DIE using SP.
1194DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit,
1195 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +00001196 bool IsConstructor,
1197 bool IsInlined) {
Bill Wendling0310d762009-05-15 09:23:25 +00001198 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1199
Devang Patel5ccdd102009-09-29 18:40:58 +00001200 const char * Name = SP.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001201 AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1202
Devang Patel5ccdd102009-09-29 18:40:58 +00001203 const char *LinkageName = SP.getLinkageName();
1204 if (LinkageName) {
Devang Patel53cb17d2009-07-16 01:01:22 +00001205 // Skip special LLVM prefix that is used to inform the asm printer to not emit
1206 // usual symbol prefix before the symbol name. This happens for Objective-C
1207 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
1208 if (LinkageName[0] == 1)
1209 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001210 AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001211 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001212 }
Bill Wendling0310d762009-05-15 09:23:25 +00001213 AddSourceLine(SPDie, &SP);
1214
1215 DICompositeType SPTy = SP.getType();
1216 DIArray Args = SPTy.getTypeArray();
1217
1218 // Add prototyped tag, if C or ObjC.
1219 unsigned Lang = SP.getCompileUnit().getLanguage();
1220 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1221 Lang == dwarf::DW_LANG_ObjC)
1222 AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
1223
1224 // Add Return Type.
1225 unsigned SPTag = SPTy.getTag();
1226 if (!IsConstructor) {
1227 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1228 AddType(DW_Unit, SPDie, SPTy);
1229 else
Devang Patele4b27562009-08-28 23:24:31 +00001230 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001231 }
1232
1233 if (!SP.isDefinition()) {
1234 AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1235
1236 // Add arguments. Do not add arguments for subprogram definition. They will
1237 // be handled through RecordVariable.
1238 if (SPTag == dwarf::DW_TAG_subroutine_type)
1239 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1240 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patele4b27562009-08-28 23:24:31 +00001241 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001242 AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1243 SPDie->AddChild(Arg);
1244 }
1245 }
1246
Bill Wendling0310d762009-05-15 09:23:25 +00001247 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel017d1212009-11-20 21:37:22 +00001248 DW_Unit->insertDIE(SP.getNode(), SPDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001249 return SPDie;
1250}
1251
1252/// FindCompileUnit - Get the compile unit for the given descriptor.
1253///
1254CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const {
1255 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001256 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001257 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1258 return *I->second;
1259}
1260
Bill Wendling995f80a2009-05-20 23:24:48 +00001261/// CreateDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +00001262///
Bill Wendling995f80a2009-05-20 23:24:48 +00001263DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001264 // Get the descriptor.
1265 const DIVariable &VD = DV->getVariable();
Devang Patel1b845972009-11-03 18:30:27 +00001266 const char *Name = VD.getName();
1267 if (!Name)
1268 return NULL;
Bill Wendling0310d762009-05-15 09:23:25 +00001269
1270 // Translate tag to proper Dwarf tag. The result variable is dropped for
1271 // now.
1272 unsigned Tag;
1273 switch (VD.getTag()) {
1274 case dwarf::DW_TAG_return_variable:
1275 return NULL;
1276 case dwarf::DW_TAG_arg_variable:
1277 Tag = dwarf::DW_TAG_formal_parameter;
1278 break;
1279 case dwarf::DW_TAG_auto_variable: // fall thru
1280 default:
1281 Tag = dwarf::DW_TAG_variable;
1282 break;
1283 }
1284
1285 // Define variable debug information entry.
1286 DIE *VariableDie = new DIE(Tag);
Bill Wendling0310d762009-05-15 09:23:25 +00001287 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1288
1289 // Add source line info if available.
1290 AddSourceLine(VariableDie, &VD);
1291
1292 // Add variable type.
Devang Patel53bb5c92009-11-10 23:06:00 +00001293 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
1294 // addresses instead.
Caroline Ticedc8f6042009-08-31 21:19:37 +00001295 if (VD.isBlockByrefVariable())
1296 AddType(Unit, VariableDie, GetBlockByrefType(VD.getType(), Name));
1297 else
1298 AddType(Unit, VariableDie, VD.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001299
1300 // Add variable address.
Devang Patel53bb5c92009-11-10 23:06:00 +00001301 // Variables for abstract instances of inlined functions don't get a
1302 // location.
1303 MachineLocation Location;
1304 Location.set(RI->getFrameRegister(*MF),
1305 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1306
1307
1308 if (VD.hasComplexAddress())
1309 AddComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1310 else if (VD.isBlockByrefVariable())
1311 AddBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1312 else
1313 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001314
1315 return VariableDie;
1316}
1317
Devang Patel53bb5c92009-11-10 23:06:00 +00001318/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1319/// Initialize scope and update scope hierarchy.
1320DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1321 MDNode *InlinedAt) {
1322 assert (N && "Invalid Scope encoding!");
1323 assert (MI && "Missing machine instruction!");
1324 bool GetConcreteScope = (MI && InlinedAt);
1325
1326 DbgScope *NScope = NULL;
1327
1328 if (InlinedAt)
1329 NScope = DbgScopeMap.lookup(InlinedAt);
1330 else
1331 NScope = DbgScopeMap.lookup(N);
1332 assert (NScope && "Unable to find working scope!");
1333
1334 if (NScope->getFirstInsn())
1335 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001336
1337 DbgScope *Parent = NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001338 if (GetConcreteScope) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001339 DILocation IL(InlinedAt);
Devang Patel53bb5c92009-11-10 23:06:00 +00001340 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
1341 IL.getOrigLocation().getNode());
1342 assert (Parent && "Unable to find Parent scope!");
1343 NScope->setParent(Parent);
1344 Parent->AddScope(NScope);
1345 } else if (DIDescriptor(N).isLexicalBlock()) {
1346 DILexicalBlock DB(N);
1347 if (!DB.getContext().isNull()) {
1348 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1349 NScope->setParent(Parent);
1350 Parent->AddScope(NScope);
1351 }
Devang Patelc90aefe2009-10-14 21:08:09 +00001352 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001353
Devang Patelbdf45cb2009-10-27 20:47:17 +00001354 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001355
Devang Patel53bb5c92009-11-10 23:06:00 +00001356 if (!Parent && !InlinedAt) {
Devang Patel39ae3ff2009-11-11 00:31:36 +00001357 StringRef SPName = DISubprogram(N).getLinkageName();
1358 if (SPName == MF->getFunction()->getName())
1359 CurrentFnDbgScope = NScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001360 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001361
Devang Patel53bb5c92009-11-10 23:06:00 +00001362 if (GetConcreteScope) {
1363 ConcreteScopes[InlinedAt] = NScope;
1364 getOrCreateAbstractScope(N);
1365 }
1366
Devang Patelbdf45cb2009-10-27 20:47:17 +00001367 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001368}
1369
Devang Patel53bb5c92009-11-10 23:06:00 +00001370DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1371 assert (N && "Invalid Scope encoding!");
1372
1373 DbgScope *AScope = AbstractScopes.lookup(N);
1374 if (AScope)
1375 return AScope;
1376
1377 DbgScope *Parent = NULL;
1378
1379 DIDescriptor Scope(N);
1380 if (Scope.isLexicalBlock()) {
1381 DILexicalBlock DB(N);
1382 DIDescriptor ParentDesc = DB.getContext();
1383 if (!ParentDesc.isNull())
1384 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1385 }
1386
1387 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1388
1389 if (Parent)
1390 Parent->AddScope(AScope);
1391 AScope->setAbstractScope();
1392 AbstractScopes[N] = AScope;
1393 if (DIDescriptor(N).isSubprogram())
1394 AbstractScopesList.push_back(AScope);
1395 return AScope;
1396}
Devang Patelaf9e8472009-10-01 20:31:14 +00001397
Devang Patel53bb5c92009-11-10 23:06:00 +00001398static DISubprogram getDISubprogram(MDNode *N) {
1399
1400 DIDescriptor D(N);
1401 if (D.isNull())
1402 return DISubprogram();
1403
1404 if (D.isCompileUnit())
1405 return DISubprogram();
1406
1407 if (D.isSubprogram())
1408 return DISubprogram(N);
1409
1410 if (D.isLexicalBlock())
1411 return getDISubprogram(DILexicalBlock(N).getContext().getNode());
1412
1413 llvm_unreachable("Unexpected Descriptor!");
1414}
1415
1416DIE *DwarfDebug::UpdateSubprogramScopeDIE(MDNode *SPNode) {
1417
Devang Patel017d1212009-11-20 21:37:22 +00001418 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel53bb5c92009-11-10 23:06:00 +00001419 assert (SPDie && "Unable to find subprogram DIE!");
1420 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1421 DWLabel("func_begin", SubprogramCount));
1422 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1423 DWLabel("func_end", SubprogramCount));
1424 MachineLocation Location(RI->getFrameRegister(*MF));
1425 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
1426
1427 if (!DISubprogram(SPNode).isLocalToUnit())
1428 AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1429
1430 // If there are global variables at this scope then add their dies.
1431 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
1432 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1433 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1434 if (!N) continue;
1435 DIGlobalVariable GV(N);
1436 if (GV.getContext().getNode() == SPNode) {
1437 DIE *ScopedGVDie = CreateGlobalVariableDIE(ModuleCU, GV);
Devang Patelfb0ee432009-11-10 23:20:04 +00001438 if (ScopedGVDie)
1439 SPDie->AddChild(ScopedGVDie);
Devang Patel53bb5c92009-11-10 23:06:00 +00001440 }
1441 }
1442 return SPDie;
1443}
1444
1445DIE *DwarfDebug::ConstructLexicalScopeDIE(DbgScope *Scope) {
1446 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1447 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1448
1449 // Ignore empty scopes.
1450 if (StartID == EndID && StartID != 0)
1451 return NULL;
1452
1453 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1454 if (Scope->isAbstractScope())
1455 return ScopeDIE;
1456
1457 AddLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1458 StartID ?
1459 DWLabel("label", StartID)
1460 : DWLabel("func_begin", SubprogramCount));
1461 AddLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1462 EndID ?
1463 DWLabel("label", EndID)
1464 : DWLabel("func_end", SubprogramCount));
1465
1466
1467
1468 return ScopeDIE;
1469}
1470
1471DIE *DwarfDebug::ConstructInlinedScopeDIE(DbgScope *Scope) {
1472 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1473 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1474 assert (StartID && "Invalid starting label for an inlined scope!");
1475 assert (EndID && "Invalid end label for an inlined scope!");
1476 // Ignore empty scopes.
1477 if (StartID == EndID && StartID != 0)
1478 return NULL;
1479
1480 DIScope DS(Scope->getScopeNode());
1481 if (DS.isNull())
1482 return NULL;
1483 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1484
1485 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001486 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001487 assert (OriginDIE && "Unable to find Origin DIE!");
1488 AddDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
1489 dwarf::DW_FORM_ref4, OriginDIE);
1490
1491 AddLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1492 DWLabel("label", StartID));
1493 AddLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1494 DWLabel("label", EndID));
1495
1496 InlinedSubprogramDIEs.insert(OriginDIE);
1497
1498 // Track the start label for this inlined function.
1499 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1500 I = InlineInfo.find(InlinedSP.getNode());
1501
1502 if (I == InlineInfo.end()) {
1503 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID, ScopeDIE));
1504 InlinedSPNodes.push_back(InlinedSP.getNode());
1505 } else
1506 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1507
1508 StringPool.insert(InlinedSP.getName());
1509 StringPool.insert(InlinedSP.getLinkageName());
1510 DILocation DL(Scope->getInlinedAt());
1511 AddUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1512 AddUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
1513
1514 return ScopeDIE;
1515}
1516
1517DIE *DwarfDebug::ConstructVariableDIE(DbgVariable *DV,
1518 DbgScope *Scope, CompileUnit *Unit) {
1519 // Get the descriptor.
1520 const DIVariable &VD = DV->getVariable();
Devang Patel3fb6bd62009-11-13 02:25:26 +00001521 const char *Name = VD.getName();
1522 if (!Name)
1523 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001524
1525 // Translate tag to proper Dwarf tag. The result variable is dropped for
1526 // now.
1527 unsigned Tag;
1528 switch (VD.getTag()) {
1529 case dwarf::DW_TAG_return_variable:
1530 return NULL;
1531 case dwarf::DW_TAG_arg_variable:
1532 Tag = dwarf::DW_TAG_formal_parameter;
1533 break;
1534 case dwarf::DW_TAG_auto_variable: // fall thru
1535 default:
1536 Tag = dwarf::DW_TAG_variable;
1537 break;
1538 }
1539
1540 // Define variable debug information entry.
1541 DIE *VariableDie = new DIE(Tag);
1542
1543
1544 DIE *AbsDIE = NULL;
1545 if (DbgVariable *AV = DV->getAbstractVariable())
1546 AbsDIE = AV->getDIE();
1547
1548 if (AbsDIE) {
1549 DIScope DS(Scope->getScopeNode());
1550 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001551 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc0326792009-11-11 03:09:50 +00001552 (void) OriginSPDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001553 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1554 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1555 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
1556 AddDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1557 dwarf::DW_FORM_ref4, AbsDIE);
1558 }
1559 else {
Devang Patel53bb5c92009-11-10 23:06:00 +00001560 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1561 AddSourceLine(VariableDie, &VD);
1562
1563 // Add variable type.
1564 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
1565 // addresses instead.
1566 if (VD.isBlockByrefVariable())
1567 AddType(Unit, VariableDie, GetBlockByrefType(VD.getType(), Name));
1568 else
1569 AddType(Unit, VariableDie, VD.getType());
1570 }
1571
1572 // Add variable address.
1573 if (!Scope->isAbstractScope()) {
1574 MachineLocation Location;
1575 Location.set(RI->getFrameRegister(*MF),
1576 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1577
1578
1579 if (VD.hasComplexAddress())
1580 AddComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1581 else if (VD.isBlockByrefVariable())
1582 AddBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1583 else
1584 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
1585 }
1586 DV->setDIE(VariableDie);
1587 return VariableDie;
1588
1589}
1590DIE *DwarfDebug::ConstructScopeDIE(DbgScope *Scope) {
1591 if (!Scope)
1592 return NULL;
1593 DIScope DS(Scope->getScopeNode());
1594 if (DS.isNull())
1595 return NULL;
1596
1597 DIE *ScopeDIE = NULL;
1598 if (Scope->getInlinedAt())
1599 ScopeDIE = ConstructInlinedScopeDIE(Scope);
1600 else if (DS.isSubprogram()) {
1601 if (Scope->isAbstractScope())
Devang Patel017d1212009-11-20 21:37:22 +00001602 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001603 else
1604 ScopeDIE = UpdateSubprogramScopeDIE(DS.getNode());
1605 }
1606 else {
1607 ScopeDIE = ConstructLexicalScopeDIE(Scope);
1608 if (!ScopeDIE) return NULL;
1609 }
1610
1611 // Add variables to scope.
1612 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1613 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1614 DIE *VariableDIE = ConstructVariableDIE(Variables[i], Scope, ModuleCU);
1615 if (VariableDIE)
1616 ScopeDIE->AddChild(VariableDIE);
1617 }
1618
1619 // Add nested scopes.
1620 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1621 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1622 // Define the Scope debug information entry.
1623 DIE *NestedDIE = ConstructScopeDIE(Scopes[j]);
1624 if (NestedDIE)
1625 ScopeDIE->AddChild(NestedDIE);
1626 }
1627 return ScopeDIE;
1628}
1629
Bill Wendlingf0fb9872009-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 Patel5ccdd102009-09-29 18:40:58 +00001634unsigned DwarfDebug::GetOrCreateSourceID(const char *DirName,
1635 const char *FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001636 unsigned DId;
1637 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1638 if (DI != DirectoryIdMap.end()) {
1639 DId = DI->getValue();
1640 } else {
1641 DId = DirectoryNames.size() + 1;
1642 DirectoryIdMap[DirName] = DId;
1643 DirectoryNames.push_back(DirName);
1644 }
1645
1646 unsigned FId;
1647 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1648 if (FI != SourceFileIdMap.end()) {
1649 FId = FI->getValue();
1650 } else {
1651 FId = SourceFileNames.size() + 1;
1652 SourceFileIdMap[FileName] = FId;
1653 SourceFileNames.push_back(FileName);
1654 }
1655
1656 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1657 SourceIdMap.find(std::make_pair(DId, FId));
1658 if (SI != SourceIdMap.end())
1659 return SI->second;
1660
1661 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1662 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1663 SourceIds.push_back(std::make_pair(DId, FId));
1664
1665 return SrcId;
1666}
1667
Devang Patele4b27562009-08-28 23:24:31 +00001668void DwarfDebug::ConstructCompileUnit(MDNode *N) {
1669 DICompileUnit DIUnit(N);
Devang Patel5ccdd102009-09-29 18:40:58 +00001670 const char *FN = DIUnit.getFilename();
1671 const char *Dir = DIUnit.getDirectory();
1672 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001673
1674 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
1675 AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
1676 DWLabel("section_line", 0), DWLabel("section_line", 0),
1677 false);
1678 AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001679 DIUnit.getProducer());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001680 AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
1681 DIUnit.getLanguage());
1682 AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
1683
Devang Patel5ccdd102009-09-29 18:40:58 +00001684 if (Dir)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001685 AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
1686 if (DIUnit.isOptimized())
1687 AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1688
Devang Patel5ccdd102009-09-29 18:40:58 +00001689 if (const char *Flags = DIUnit.getFlags())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001690 AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
1691
1692 unsigned RVer = DIUnit.getRunTimeVersion();
1693 if (RVer)
1694 AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1695 dwarf::DW_FORM_data1, RVer);
1696
1697 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001698 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001699 // Use first compile unit marked as isMain as the compile unit
1700 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001701 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001702 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001703
Devang Patele4b27562009-08-28 23:24:31 +00001704 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001705 CompileUnits.push_back(Unit);
1706}
1707
Devang Patele4b27562009-08-28 23:24:31 +00001708void DwarfDebug::ConstructGlobalVariableDIE(MDNode *N) {
1709 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001710
Devang Patel905cf5e2009-09-04 23:59:07 +00001711 // If debug information is malformed then ignore it.
1712 if (DI_GV.Verify() == false)
1713 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001714
1715 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001716 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel13e16b62009-06-26 01:49:18 +00001717 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001718
Devang Patel1dbc7712009-06-29 20:45:18 +00001719 DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001720
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001721 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001722 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001723
1724 // Add to context owner.
Devang Patel017d1212009-11-20 21:37:22 +00001725 ModuleCU->getCUDie()->AddChild(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001726
1727 // Expose as global. FIXME - need to check external flag.
Devang Patel5ccdd102009-09-29 18:40:58 +00001728 ModuleCU->AddGlobal(DI_GV.getName(), VariableDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001729 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001730}
1731
Devang Patele4b27562009-08-28 23:24:31 +00001732void DwarfDebug::ConstructSubprogram(MDNode *N) {
1733 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001734
1735 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001736 if (ModuleCU->getDIE(N))
Devang Patel13e16b62009-06-26 01:49:18 +00001737 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001738
1739 if (!SP.isDefinition())
1740 // This is a method declaration which will be handled while constructing
1741 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001742 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001743
Devang Patel1dbc7712009-06-29 20:45:18 +00001744 DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001745
1746 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001747 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001748
1749 // Add to context owner.
Devang Patel017d1212009-11-20 21:37:22 +00001750 ModuleCU->getCUDie()->AddChild(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001751
1752 // Expose as global.
Devang Patel5ccdd102009-09-29 18:40:58 +00001753 ModuleCU->AddGlobal(SP.getName(), SubprogramDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001754 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001755}
1756
Daniel Dunbar00564992009-09-19 20:40:14 +00001757/// BeginModule - Emit all Dwarf sections that should come prior to the
1758/// content. Create global DIEs and emit initial debug info sections.
1759/// This is inovked by the target AsmPrinter.
Devang Patel208622d2009-06-25 22:36:02 +00001760void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) {
1761 this->M = M;
1762
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001763 if (TimePassesIsEnabled)
1764 DebugTimer->startTimer();
1765
Devang Patel3380cc52009-11-11 19:55:08 +00001766 if (!MAI->doesSupportDebugInformation())
1767 return;
1768
Devang Patel78ab9e22009-07-30 18:56:46 +00001769 DebugInfoFinder DbgFinder;
1770 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001771
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001772 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001773 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1774 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001775 ConstructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001776
1777 if (CompileUnits.empty()) {
1778 if (TimePassesIsEnabled)
1779 DebugTimer->stopTimer();
1780
1781 return;
1782 }
1783
Devang Patel70f44262009-06-29 20:38:13 +00001784 // If main compile unit for this module is not seen than randomly
1785 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001786 if (!ModuleCU)
1787 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001788
Devang Patel13e16b62009-06-26 01:49:18 +00001789 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001790 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patelfd07cf52009-10-05 23:40:42 +00001791 E = DbgFinder.global_variable_end(); I != E; ++I) {
1792 DIGlobalVariable GV(*I);
1793 if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
1794 ScopedGVs.push_back(*I);
1795 else
1796 ConstructGlobalVariableDIE(*I);
1797 }
Devang Patel13e16b62009-06-26 01:49:18 +00001798
Devang Patel53bb5c92009-11-10 23:06:00 +00001799 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001800 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1801 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001802 ConstructSubprogram(*I);
1803
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001804 MMI = mmi;
1805 shouldEmit = true;
1806 MMI->setDebugInfoAvailability(true);
1807
1808 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001809 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001810
1811 // Print out .file directives to specify files for .loc directives. These are
1812 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001813 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001814 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1815 // Remember source id starts at 1.
1816 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1817 sys::Path FullPath(getSourceDirectoryName(Id.first));
1818 bool AppendOk =
1819 FullPath.appendComponent(getSourceFileName(Id.second));
1820 assert(AppendOk && "Could not append filename to directory!");
1821 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001822 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001823 Asm->EOL();
1824 }
1825 }
1826
1827 // Emit initial sections
1828 EmitInitial();
1829
1830 if (TimePassesIsEnabled)
1831 DebugTimer->stopTimer();
1832}
1833
1834/// EndModule - Emit all Dwarf sections that should come after the content.
1835///
1836void DwarfDebug::EndModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001837 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001838 return;
1839
1840 if (TimePassesIsEnabled)
1841 DebugTimer->startTimer();
1842
Devang Patel53bb5c92009-11-10 23:06:00 +00001843 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1844 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1845 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1846 DIE *ISP = *AI;
1847 AddUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
1848 }
1849
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001850 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001851 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001852 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001853 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001854 EmitLabel("data_end", 0);
1855
1856 // End text sections.
1857 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001858 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001859 EmitLabel("section_end", i);
1860 }
1861
1862 // Emit common frame information.
1863 EmitCommonDebugFrame();
1864
1865 // Emit function debug frame information
1866 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1867 E = DebugFrames.end(); I != E; ++I)
1868 EmitFunctionDebugFrame(*I);
1869
1870 // Compute DIE offsets and sizes.
1871 SizeAndOffsets();
1872
1873 // Emit all the DIEs into a debug info section
1874 EmitDebugInfo();
1875
1876 // Corresponding abbreviations into a abbrev section.
1877 EmitAbbreviations();
1878
1879 // Emit source line correspondence into a debug line section.
1880 EmitDebugLines();
1881
1882 // Emit info into a debug pubnames section.
1883 EmitDebugPubNames();
1884
1885 // Emit info into a debug str section.
1886 EmitDebugStr();
1887
1888 // Emit info into a debug loc section.
1889 EmitDebugLoc();
1890
1891 // Emit info into a debug aranges section.
1892 EmitDebugARanges();
1893
1894 // Emit info into a debug ranges section.
1895 EmitDebugRanges();
1896
1897 // Emit info into a debug macinfo section.
1898 EmitDebugMacInfo();
1899
1900 // Emit inline info.
1901 EmitDebugInlineInfo();
1902
1903 if (TimePassesIsEnabled)
1904 DebugTimer->stopTimer();
1905}
1906
Devang Patel53bb5c92009-11-10 23:06:00 +00001907/// findAbstractVariable - Find abstract variable, if any, associated with Var.
1908DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
1909 DILocation &ScopeLoc) {
1910
1911 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1912 if (AbsDbgVariable)
1913 return AbsDbgVariable;
1914
1915 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1916 if (!Scope)
1917 return NULL;
1918
1919 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
1920 Scope->AddVariable(AbsDbgVariable);
1921 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1922 return AbsDbgVariable;
1923}
1924
Devang Patele717faa2009-10-06 01:26:37 +00001925/// CollectVariableInfo - Populate DbgScope entries with variables' info.
Devang Patelac1ceb32009-10-09 22:42:28 +00001926void DwarfDebug::CollectVariableInfo() {
1927 if (!MMI) return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001928
Devang Patele717faa2009-10-06 01:26:37 +00001929 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1930 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1931 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac1ceb32009-10-09 22:42:28 +00001932 MetadataBase *MB = VI->first;
1933 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel53bb5c92009-11-10 23:06:00 +00001934 if (!Var) continue;
Devang Pateleda31212009-10-08 18:48:03 +00001935 DIVariable DV (Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001936 std::pair< unsigned, MDNode *> VP = VI->second;
1937 DILocation ScopeLoc(VP.second);
1938
1939 DbgScope *Scope =
1940 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1941 if (!Scope)
1942 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelfb0ee432009-11-10 23:20:04 +00001943 // If variable scope is not found then skip this variable.
1944 if (!Scope)
1945 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001946
1947 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
1948 Scope->AddVariable(RegVar);
1949 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, ScopeLoc))
1950 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patele717faa2009-10-06 01:26:37 +00001951 }
1952}
1953
Devang Patel53bb5c92009-11-10 23:06:00 +00001954/// BeginScope - Process beginning of a scope starting at Label.
1955void DwarfDebug::BeginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001956 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1957 if (I == DbgScopeBeginMap.end())
1958 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001959 ScopeVector &SD = DbgScopeBeginMap[MI];
1960 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Devang Patel0d20ac82009-10-06 01:50:42 +00001961 SDI != SDE; ++SDI)
1962 (*SDI)->setStartLabelID(Label);
1963}
1964
Devang Patel53bb5c92009-11-10 23:06:00 +00001965/// EndScope - Process end of a scope.
1966void DwarfDebug::EndScope(const MachineInstr *MI) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001967 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00001968 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00001969 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001970
1971 unsigned Label = MMI->NextLabelID();
1972 Asm->printLabel(Label);
1973
Devang Patel0d20ac82009-10-06 01:50:42 +00001974 SmallVector<DbgScope *, 2> &SD = I->second;
1975 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
1976 SDI != SDE; ++SDI)
1977 (*SDI)->setEndLabelID(Label);
Devang Patel53bb5c92009-11-10 23:06:00 +00001978 return;
1979}
1980
1981/// createDbgScope - Create DbgScope for the scope.
1982void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1983
1984 if (!InlinedAt) {
1985 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1986 if (WScope)
1987 return;
1988 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1989 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patel2f105c62009-11-11 00:18:40 +00001990 if (DIDescriptor(Scope).isLexicalBlock())
1991 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001992 return;
1993 }
1994
1995 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1996 if (WScope)
1997 return;
1998
1999 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2000 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2001 DILocation DL(InlinedAt);
2002 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel0d20ac82009-10-06 01:50:42 +00002003}
2004
Devang Patelaf9e8472009-10-01 20:31:14 +00002005/// ExtractScopeInformation - Scan machine instructions in this function
2006/// and collect DbgScopes. Return true, if atleast one scope was found.
2007bool DwarfDebug::ExtractScopeInformation(MachineFunction *MF) {
2008 // If scope information was extracted using .dbg intrinsics then there is not
2009 // any need to extract these information by scanning each instruction.
2010 if (!DbgScopeMap.empty())
2011 return false;
2012
Devang Patel53bb5c92009-11-10 23:06:00 +00002013 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelaf9e8472009-10-01 20:31:14 +00002014 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2015 I != E; ++I) {
2016 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2017 II != IE; ++II) {
2018 const MachineInstr *MInsn = II;
2019 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00002020 if (DL.isUnknown()) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002021 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel53bb5c92009-11-10 23:06:00 +00002022 if (!DLT.Scope) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002023 // There is no need to create another DIE for compile unit. For all
2024 // other scopes, create one DbgScope now. This will be translated
2025 // into a scope DIE at the end.
Devang Patel53bb5c92009-11-10 23:06:00 +00002026 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2027 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
2028 }
2029 }
2030
2031
2032 // Build scope hierarchy using working set of scopes.
2033 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2034 I != E; ++I) {
2035 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2036 II != IE; ++II) {
2037 const MachineInstr *MInsn = II;
2038 DebugLoc DL = MInsn->getDebugLoc();
2039 if (DL.isUnknown()) continue;
2040 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
2041 if (!DLT.Scope) continue;
2042 // There is no need to create another DIE for compile unit. For all
2043 // other scopes, create one DbgScope now. This will be translated
2044 // into a scope DIE at the end.
2045 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2046 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
2047 Scope->setLastInsn(MInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +00002048 }
2049 }
2050
2051 // If a scope's last instruction is not set then use its child scope's
2052 // last instruction as this scope's last instrunction.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002053 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002054 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002055 if (DI->second->isAbstractScope())
2056 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002057 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
2058 DI->second->FixInstructionMarkers();
2059 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2060 }
2061
2062 // Each scope has first instruction and last instruction to mark beginning
2063 // and end of a scope respectively. Create an inverse map that list scopes
2064 // starts (and ends) with an instruction. One instruction may start (or end)
2065 // multiple scopes.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002066 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002067 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2068 DbgScope *S = DI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002069 if (S->isAbstractScope())
2070 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002071 const MachineInstr *MI = S->getFirstInsn();
2072 assert (MI && "DbgScope does not have first instruction!");
2073
2074 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2075 if (IDI != DbgScopeBeginMap.end())
2076 IDI->second.push_back(S);
2077 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002078 DbgScopeBeginMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002079
2080 MI = S->getLastInsn();
2081 assert (MI && "DbgScope does not have last instruction!");
2082 IDI = DbgScopeEndMap.find(MI);
2083 if (IDI != DbgScopeEndMap.end())
2084 IDI->second.push_back(S);
2085 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002086 DbgScopeEndMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002087 }
2088
2089 return !DbgScopeMap.empty();
2090}
2091
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002092/// BeginFunction - Gather pre-function debug information. Assumes being
2093/// emitted immediately after the function entry point.
2094void DwarfDebug::BeginFunction(MachineFunction *MF) {
2095 this->MF = MF;
2096
2097 if (!ShouldEmitDwarfDebug()) return;
2098
2099 if (TimePassesIsEnabled)
2100 DebugTimer->startTimer();
2101
Devang Patel60b35bd2009-10-06 18:37:31 +00002102 if (!ExtractScopeInformation(MF))
2103 return;
Devang Patelac1ceb32009-10-09 22:42:28 +00002104 CollectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002105
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002106 // Begin accumulating function debug information.
2107 MMI->BeginFunction(MF);
2108
2109 // Assumes in correct section after the entry point.
2110 EmitLabel("func_begin", ++SubprogramCount);
2111
2112 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2113 // function.
Devang Patelac1ceb32009-10-09 22:42:28 +00002114 DebugLoc FDL = MF->getDefaultDebugLoc();
2115 if (!FDL.isUnknown()) {
2116 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2117 unsigned LabelID = 0;
Devang Patel1619dc32009-10-13 23:28:53 +00002118 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002119 if (!SP.isNull())
Devang Patel1619dc32009-10-13 23:28:53 +00002120 LabelID = RecordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002121 else
Devang Patel1619dc32009-10-13 23:28:53 +00002122 LabelID = RecordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002123 Asm->printLabel(LabelID);
2124 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002125 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002126 if (TimePassesIsEnabled)
2127 DebugTimer->stopTimer();
2128}
2129
2130/// EndFunction - Gather and emit post-function debug information.
2131///
2132void DwarfDebug::EndFunction(MachineFunction *MF) {
2133 if (!ShouldEmitDwarfDebug()) return;
2134
2135 if (TimePassesIsEnabled)
2136 DebugTimer->startTimer();
2137
Devang Patelac1ceb32009-10-09 22:42:28 +00002138 if (DbgScopeMap.empty())
2139 return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002140
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002141 // Define end label for subprogram.
2142 EmitLabel("func_end", SubprogramCount);
2143
2144 // Get function line info.
2145 if (!Lines.empty()) {
2146 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00002147 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002148 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2149 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2150 // Append the function info to section info.
2151 SectionLineInfos.insert(SectionLineInfos.end(),
2152 Lines.begin(), Lines.end());
2153 }
2154
Devang Patel53bb5c92009-11-10 23:06:00 +00002155 // Construct abstract scopes.
2156 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2157 AE = AbstractScopesList.end(); AI != AE; ++AI)
2158 ConstructScopeDIE(*AI);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002159
Devang Patel53bb5c92009-11-10 23:06:00 +00002160 ConstructScopeDIE(CurrentFnDbgScope);
Devang Patel70d75ca2009-11-12 19:02:56 +00002161
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002162 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2163 MMI->getFrameMoves()));
2164
2165 // Clear debug info
Devang Patel53bb5c92009-11-10 23:06:00 +00002166 if (CurrentFnDbgScope) {
2167 CurrentFnDbgScope = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002168 DbgScopeMap.clear();
Devang Patelaf9e8472009-10-01 20:31:14 +00002169 DbgScopeBeginMap.clear();
2170 DbgScopeEndMap.clear();
Devang Patel53bb5c92009-11-10 23:06:00 +00002171 ConcreteScopes.clear();
2172 AbstractScopesList.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002173 }
2174
2175 Lines.clear();
2176
2177 if (TimePassesIsEnabled)
2178 DebugTimer->stopTimer();
2179}
2180
2181/// RecordSourceLine - Records location information and associates it with a
2182/// label. Returns a unique label ID used to generate a label and provide
2183/// correspondence to the source line list.
Devang Patel3d910832009-09-30 22:51:28 +00002184unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002185 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002186 if (!MMI)
2187 return 0;
2188
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002189 if (TimePassesIsEnabled)
2190 DebugTimer->startTimer();
2191
Devang Patelf84548d2009-10-05 18:03:19 +00002192 const char *Dir = NULL;
2193 const char *Fn = NULL;
2194
2195 DIDescriptor Scope(S);
2196 if (Scope.isCompileUnit()) {
2197 DICompileUnit CU(S);
2198 Dir = CU.getDirectory();
2199 Fn = CU.getFilename();
2200 } else if (Scope.isSubprogram()) {
2201 DISubprogram SP(S);
2202 Dir = SP.getDirectory();
2203 Fn = SP.getFilename();
2204 } else if (Scope.isLexicalBlock()) {
2205 DILexicalBlock DB(S);
2206 Dir = DB.getDirectory();
2207 Fn = DB.getFilename();
2208 } else
2209 assert (0 && "Unexpected scope info");
2210
2211 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002212 unsigned ID = MMI->NextLabelID();
2213 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2214
2215 if (TimePassesIsEnabled)
2216 DebugTimer->stopTimer();
2217
2218 return ID;
2219}
2220
2221/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2222/// timed. Look up the source id with the given directory and source file
2223/// names. If none currently exists, create a new id and insert it in the
2224/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2225/// well.
2226unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2227 const std::string &FileName) {
2228 if (TimePassesIsEnabled)
2229 DebugTimer->startTimer();
2230
Devang Patel5ccdd102009-09-29 18:40:58 +00002231 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002232
2233 if (TimePassesIsEnabled)
2234 DebugTimer->stopTimer();
2235
2236 return SrcId;
2237}
2238
Bill Wendling829e67b2009-05-20 23:22:40 +00002239//===----------------------------------------------------------------------===//
2240// Emit Methods
2241//===----------------------------------------------------------------------===//
2242
Bill Wendling94d04b82009-05-20 23:21:38 +00002243/// SizeAndOffsetDie - Compute the size and offset of a DIE.
2244///
2245unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2246 // Get the children.
2247 const std::vector<DIE *> &Children = Die->getChildren();
2248
2249 // If not last sibling and has children then add sibling offset attribute.
2250 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2251
2252 // Record the abbreviation.
2253 AssignAbbrevNumber(Die->getAbbrev());
2254
2255 // Get the abbreviation for this DIE.
2256 unsigned AbbrevNumber = Die->getAbbrevNumber();
2257 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2258
2259 // Set DIE offset
2260 Die->setOffset(Offset);
2261
2262 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002263 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002264
2265 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2266 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2267
2268 // Size the DIE attribute values.
2269 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2270 // Size attribute value.
2271 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2272
2273 // Size the DIE children if any.
2274 if (!Children.empty()) {
2275 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2276 "Children flag not set");
2277
2278 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2279 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2280
2281 // End of children marker.
2282 Offset += sizeof(int8_t);
2283 }
2284
2285 Die->setSize(Offset - Die->getOffset());
2286 return Offset;
2287}
2288
2289/// SizeAndOffsets - Compute the size and offset of all the DIEs.
2290///
2291void DwarfDebug::SizeAndOffsets() {
2292 // Compute size of compile unit header.
2293 static unsigned Offset =
2294 sizeof(int32_t) + // Length of Compilation Unit Info
2295 sizeof(int16_t) + // DWARF version number
2296 sizeof(int32_t) + // Offset Into Abbrev. Section
2297 sizeof(int8_t); // Pointer Size (in bytes)
2298
Devang Patel017d1212009-11-20 21:37:22 +00002299 SizeAndOffsetDie(ModuleCU->getCUDie(), Offset, true);
Devang Patel1dbc7712009-06-29 20:45:18 +00002300 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002301}
2302
2303/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2304/// tools to recognize the object file contains Dwarf information.
2305void DwarfDebug::EmitInitial() {
2306 // Check to see if we already emitted intial headers.
2307 if (didInitial) return;
2308 didInitial = true;
2309
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002310 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002311
Bill Wendling94d04b82009-05-20 23:21:38 +00002312 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002313 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002314 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002315 EmitLabel("section_debug_frame", 0);
2316 }
2317
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002318 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002319 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002320 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002321 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002322 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002323 EmitLabel("section_aranges", 0);
2324
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002325 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2326 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002327 EmitLabel("section_macinfo", 0);
2328 }
2329
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002330 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002331 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002332 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002333 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002334 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002335 EmitLabel("section_pubnames", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002336 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002337 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002338 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002339 EmitLabel("section_ranges", 0);
2340
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002341 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002342 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002343 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002344 EmitLabel("data_begin", 0);
2345}
2346
2347/// EmitDIE - Recusively Emits a debug information entry.
2348///
2349void DwarfDebug::EmitDIE(DIE *Die) {
2350 // Get the abbreviation for this DIE.
2351 unsigned AbbrevNumber = Die->getAbbrevNumber();
2352 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2353
2354 Asm->EOL();
2355
2356 // Emit the code (index) for the abbreviation.
2357 Asm->EmitULEB128Bytes(AbbrevNumber);
2358
2359 if (Asm->isVerbose())
2360 Asm->EOL(std::string("Abbrev [" +
2361 utostr(AbbrevNumber) +
2362 "] 0x" + utohexstr(Die->getOffset()) +
2363 ":0x" + utohexstr(Die->getSize()) + " " +
2364 dwarf::TagString(Abbrev->getTag())));
2365 else
2366 Asm->EOL();
2367
2368 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2369 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2370
2371 // Emit the DIE attribute values.
2372 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2373 unsigned Attr = AbbrevData[i].getAttribute();
2374 unsigned Form = AbbrevData[i].getForm();
2375 assert(Form && "Too many attributes for DIE (check abbreviation)");
2376
2377 switch (Attr) {
2378 case dwarf::DW_AT_sibling:
2379 Asm->EmitInt32(Die->SiblingOffset());
2380 break;
2381 case dwarf::DW_AT_abstract_origin: {
2382 DIEEntry *E = cast<DIEEntry>(Values[i]);
2383 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002384 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002385 Asm->EmitInt32(Addr);
2386 break;
2387 }
2388 default:
2389 // Emit an attribute using the defined form.
2390 Values[i]->EmitValue(this, Form);
2391 break;
2392 }
2393
2394 Asm->EOL(dwarf::AttributeString(Attr));
2395 }
2396
2397 // Emit the DIE children if any.
2398 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2399 const std::vector<DIE *> &Children = Die->getChildren();
2400
2401 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2402 EmitDIE(Children[j]);
2403
2404 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2405 }
2406}
2407
2408/// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
2409///
2410void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) {
Devang Patel017d1212009-11-20 21:37:22 +00002411 DIE *Die = Unit->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002412
2413 // Emit the compile units header.
2414 EmitLabel("info_begin", Unit->getID());
2415
2416 // Emit size of content not including length itself
2417 unsigned ContentSize = Die->getSize() +
2418 sizeof(int16_t) + // DWARF version number
2419 sizeof(int32_t) + // Offset Into Abbrev. Section
2420 sizeof(int8_t) + // Pointer Size (in bytes)
2421 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2422
2423 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2424 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2425 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2426 Asm->EOL("Offset Into Abbrev. Section");
2427 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2428
2429 EmitDIE(Die);
2430 // FIXME - extra padding for gdb bug.
2431 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2432 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2433 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2434 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2435 EmitLabel("info_end", Unit->getID());
2436
2437 Asm->EOL();
2438}
2439
2440void DwarfDebug::EmitDebugInfo() {
2441 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002442 Asm->OutStreamer.SwitchSection(
2443 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002444
Devang Patel1dbc7712009-06-29 20:45:18 +00002445 EmitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002446}
2447
2448/// EmitAbbreviations - Emit the abbreviation section.
2449///
2450void DwarfDebug::EmitAbbreviations() const {
2451 // Check to see if it is worth the effort.
2452 if (!Abbreviations.empty()) {
2453 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002454 Asm->OutStreamer.SwitchSection(
2455 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002456
2457 EmitLabel("abbrev_begin", 0);
2458
2459 // For each abbrevation.
2460 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2461 // Get abbreviation data
2462 const DIEAbbrev *Abbrev = Abbreviations[i];
2463
2464 // Emit the abbrevations code (base 1 index.)
2465 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2466 Asm->EOL("Abbreviation Code");
2467
2468 // Emit the abbreviations data.
2469 Abbrev->Emit(Asm);
2470
2471 Asm->EOL();
2472 }
2473
2474 // Mark end of abbreviations.
2475 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2476
2477 EmitLabel("abbrev_end", 0);
2478 Asm->EOL();
2479 }
2480}
2481
2482/// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2483/// the line matrix.
2484///
2485void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) {
2486 // Define last address of section.
2487 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2488 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2489 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2490 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2491
2492 // Mark end of matrix.
2493 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2494 Asm->EmitULEB128Bytes(1); Asm->EOL();
2495 Asm->EmitInt8(1); Asm->EOL();
2496}
2497
2498/// EmitDebugLines - Emit source line information.
2499///
2500void DwarfDebug::EmitDebugLines() {
2501 // If the target is using .loc/.file, the assembler will be emitting the
2502 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002503 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002504 return;
2505
2506 // Minimum line delta, thus ranging from -10..(255-10).
2507 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2508 // Maximum line delta, thus ranging from -10..(255-10).
2509 const int MaxLineDelta = 255 + MinLineDelta;
2510
2511 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002512 Asm->OutStreamer.SwitchSection(
2513 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002514
2515 // Construct the section header.
2516 EmitDifference("line_end", 0, "line_begin", 0, true);
2517 Asm->EOL("Length of Source Line Info");
2518 EmitLabel("line_begin", 0);
2519
2520 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2521
2522 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2523 Asm->EOL("Prolog Length");
2524 EmitLabel("line_prolog_begin", 0);
2525
2526 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2527
2528 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2529
2530 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2531
2532 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2533
2534 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2535
2536 // Line number standard opcode encodings argument count
2537 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2538 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2539 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2540 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2541 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2542 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2543 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2544 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2545 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2546
2547 // Emit directories.
2548 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2549 Asm->EmitString(getSourceDirectoryName(DI));
2550 Asm->EOL("Directory");
2551 }
2552
2553 Asm->EmitInt8(0); Asm->EOL("End of directories");
2554
2555 // Emit files.
2556 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2557 // Remember source id starts at 1.
2558 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2559 Asm->EmitString(getSourceFileName(Id.second));
2560 Asm->EOL("Source");
2561 Asm->EmitULEB128Bytes(Id.first);
2562 Asm->EOL("Directory #");
2563 Asm->EmitULEB128Bytes(0);
2564 Asm->EOL("Mod date");
2565 Asm->EmitULEB128Bytes(0);
2566 Asm->EOL("File size");
2567 }
2568
2569 Asm->EmitInt8(0); Asm->EOL("End of files");
2570
2571 EmitLabel("line_prolog_end", 0);
2572
2573 // A sequence for each text section.
2574 unsigned SecSrcLinesSize = SectionSourceLines.size();
2575
2576 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2577 // Isolate current sections line info.
2578 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2579
Chris Lattner93b6db32009-08-08 23:39:42 +00002580 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002581 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002582 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002583 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002584 }*/
2585 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002586
2587 // Dwarf assumes we start with first line of first source file.
2588 unsigned Source = 1;
2589 unsigned Line = 1;
2590
2591 // Construct rows of the address, source, line, column matrix.
2592 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2593 const SrcLineInfo &LineInfo = LineInfos[i];
2594 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2595 if (!LabelID) continue;
2596
Caroline Ticec6f9d622009-09-11 18:25:54 +00002597 if (LineInfo.getLine() == 0) continue;
2598
Bill Wendling94d04b82009-05-20 23:21:38 +00002599 if (!Asm->isVerbose())
2600 Asm->EOL();
2601 else {
2602 std::pair<unsigned, unsigned> SourceID =
2603 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002604 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002605 << getSourceDirectoryName(SourceID.first) << ' '
2606 << getSourceFileName(SourceID.second)
2607 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2608 }
2609
2610 // Define the line address.
2611 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2612 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2613 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2614 EmitReference("label", LabelID); Asm->EOL("Location label");
2615
2616 // If change of source, then switch to the new source.
2617 if (Source != LineInfo.getSourceID()) {
2618 Source = LineInfo.getSourceID();
2619 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2620 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2621 }
2622
2623 // If change of line.
2624 if (Line != LineInfo.getLine()) {
2625 // Determine offset.
2626 int Offset = LineInfo.getLine() - Line;
2627 int Delta = Offset - MinLineDelta;
2628
2629 // Update line.
2630 Line = LineInfo.getLine();
2631
2632 // If delta is small enough and in range...
2633 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2634 // ... then use fast opcode.
2635 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2636 } else {
2637 // ... otherwise use long hand.
2638 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2639 Asm->EOL("DW_LNS_advance_line");
2640 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2641 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2642 }
2643 } else {
2644 // Copy the previous row (different address or source)
2645 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2646 }
2647 }
2648
2649 EmitEndOfLineMatrix(j + 1);
2650 }
2651
2652 if (SecSrcLinesSize == 0)
2653 // Because we're emitting a debug_line section, we still need a line
2654 // table. The linker and friends expect it to exist. If there's nothing to
2655 // put into it, emit an empty table.
2656 EmitEndOfLineMatrix(1);
2657
2658 EmitLabel("line_end", 0);
2659 Asm->EOL();
2660}
2661
2662/// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2663///
2664void DwarfDebug::EmitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002665 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002666 return;
2667
2668 int stackGrowth =
2669 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2670 TargetFrameInfo::StackGrowsUp ?
2671 TD->getPointerSize() : -TD->getPointerSize();
2672
2673 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002674 Asm->OutStreamer.SwitchSection(
2675 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002676
2677 EmitLabel("debug_frame_common", 0);
2678 EmitDifference("debug_frame_common_end", 0,
2679 "debug_frame_common_begin", 0, true);
2680 Asm->EOL("Length of Common Information Entry");
2681
2682 EmitLabel("debug_frame_common_begin", 0);
2683 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2684 Asm->EOL("CIE Identifier Tag");
2685 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2686 Asm->EOL("CIE Version");
2687 Asm->EmitString("");
2688 Asm->EOL("CIE Augmentation");
2689 Asm->EmitULEB128Bytes(1);
2690 Asm->EOL("CIE Code Alignment Factor");
2691 Asm->EmitSLEB128Bytes(stackGrowth);
2692 Asm->EOL("CIE Data Alignment Factor");
2693 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2694 Asm->EOL("CIE RA Column");
2695
2696 std::vector<MachineMove> Moves;
2697 RI->getInitialFrameState(Moves);
2698
2699 EmitFrameMoves(NULL, 0, Moves, false);
2700
2701 Asm->EmitAlignment(2, 0, 0, false);
2702 EmitLabel("debug_frame_common_end", 0);
2703
2704 Asm->EOL();
2705}
2706
2707/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2708/// section.
2709void
2710DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002711 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002712 return;
2713
2714 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002715 Asm->OutStreamer.SwitchSection(
2716 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002717
2718 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2719 "debug_frame_begin", DebugFrameInfo.Number, true);
2720 Asm->EOL("Length of Frame Information Entry");
2721
2722 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2723
2724 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2725 0, 0, true, false);
2726 Asm->EOL("FDE CIE offset");
2727
2728 EmitReference("func_begin", DebugFrameInfo.Number);
2729 Asm->EOL("FDE initial location");
2730 EmitDifference("func_end", DebugFrameInfo.Number,
2731 "func_begin", DebugFrameInfo.Number);
2732 Asm->EOL("FDE address range");
2733
2734 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2735 false);
2736
2737 Asm->EmitAlignment(2, 0, 0, false);
2738 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2739
2740 Asm->EOL();
2741}
2742
2743void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2744 EmitDifference("pubnames_end", Unit->getID(),
2745 "pubnames_begin", Unit->getID(), true);
2746 Asm->EOL("Length of Public Names Info");
2747
2748 EmitLabel("pubnames_begin", Unit->getID());
2749
2750 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2751
2752 EmitSectionOffset("info_begin", "section_info",
2753 Unit->getID(), 0, true, false);
2754 Asm->EOL("Offset of Compilation Unit Info");
2755
2756 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2757 true);
2758 Asm->EOL("Compilation Unit Length");
2759
2760 StringMap<DIE*> &Globals = Unit->getGlobals();
2761 for (StringMap<DIE*>::const_iterator
2762 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2763 const char *Name = GI->getKeyData();
2764 DIE * Entity = GI->second;
2765
2766 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2767 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2768 }
2769
2770 Asm->EmitInt32(0); Asm->EOL("End Mark");
2771 EmitLabel("pubnames_end", Unit->getID());
2772
2773 Asm->EOL();
2774}
2775
2776/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2777///
2778void DwarfDebug::EmitDebugPubNames() {
2779 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002780 Asm->OutStreamer.SwitchSection(
2781 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002782
Devang Patel1dbc7712009-06-29 20:45:18 +00002783 EmitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002784}
2785
2786/// EmitDebugStr - Emit visible names into a debug str section.
2787///
2788void DwarfDebug::EmitDebugStr() {
2789 // Check to see if it is worth the effort.
2790 if (!StringPool.empty()) {
2791 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002792 Asm->OutStreamer.SwitchSection(
2793 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002794
2795 // For each of strings in the string pool.
2796 for (unsigned StringID = 1, N = StringPool.size();
2797 StringID <= N; ++StringID) {
2798 // Emit a label for reference from debug information entries.
2799 EmitLabel("string", StringID);
2800
2801 // Emit the string itself.
2802 const std::string &String = StringPool[StringID];
2803 Asm->EmitString(String); Asm->EOL();
2804 }
2805
2806 Asm->EOL();
2807 }
2808}
2809
2810/// EmitDebugLoc - Emit visible names into a debug loc section.
2811///
2812void DwarfDebug::EmitDebugLoc() {
2813 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002814 Asm->OutStreamer.SwitchSection(
2815 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002816 Asm->EOL();
2817}
2818
2819/// EmitDebugARanges - Emit visible names into a debug aranges section.
2820///
2821void DwarfDebug::EmitDebugARanges() {
2822 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002823 Asm->OutStreamer.SwitchSection(
2824 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002825
2826 // FIXME - Mock up
2827#if 0
2828 CompileUnit *Unit = GetBaseCompileUnit();
2829
2830 // Don't include size of length
2831 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2832
2833 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2834
2835 EmitReference("info_begin", Unit->getID());
2836 Asm->EOL("Offset of Compilation Unit Info");
2837
2838 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2839
2840 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2841
2842 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2843 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2844
2845 // Range 1
2846 EmitReference("text_begin", 0); Asm->EOL("Address");
2847 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2848
2849 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2850 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2851#endif
2852
2853 Asm->EOL();
2854}
2855
2856/// EmitDebugRanges - Emit visible names into a debug ranges section.
2857///
2858void DwarfDebug::EmitDebugRanges() {
2859 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002860 Asm->OutStreamer.SwitchSection(
2861 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002862 Asm->EOL();
2863}
2864
2865/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2866///
2867void DwarfDebug::EmitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002868 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002869 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002870 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002871 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002872 Asm->EOL();
2873 }
2874}
2875
2876/// EmitDebugInlineInfo - Emit inline info using following format.
2877/// Section Header:
2878/// 1. length of section
2879/// 2. Dwarf version number
2880/// 3. address size.
2881///
2882/// Entries (one "entry" for each function that was inlined):
2883///
2884/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2885/// otherwise offset into __debug_str for regular function name.
2886/// 2. offset into __debug_str section for regular function name.
2887/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2888/// instances for the function.
2889///
2890/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2891/// inlined instance; the die_offset points to the inlined_subroutine die in the
2892/// __debug_info section, and the low_pc is the starting address for the
2893/// inlining instance.
2894void DwarfDebug::EmitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002895 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002896 return;
2897
Devang Patel1dbc7712009-06-29 20:45:18 +00002898 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002899 return;
2900
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002901 Asm->OutStreamer.SwitchSection(
2902 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002903 Asm->EOL();
2904 EmitDifference("debug_inlined_end", 1,
2905 "debug_inlined_begin", 1, true);
2906 Asm->EOL("Length of Debug Inlined Information Entry");
2907
2908 EmitLabel("debug_inlined_begin", 1);
2909
2910 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2911 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2912
Devang Patel53bb5c92009-11-10 23:06:00 +00002913 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2914 E = InlinedSPNodes.end(); I != E; ++I) {
2915
2916// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2917 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2918 MDNode *Node = *I;
2919 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II = InlineInfo.find(Node);
2920 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002921 DISubprogram SP(Node);
Devang Patel5ccdd102009-09-29 18:40:58 +00002922 const char *LName = SP.getLinkageName();
2923 const char *Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002924
Devang Patel5ccdd102009-09-29 18:40:58 +00002925 if (!LName)
Devang Patel53cb17d2009-07-16 01:01:22 +00002926 Asm->EmitString(Name);
2927 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002928 // Skip special LLVM prefix that is used to inform the asm printer to not
2929 // emit usual symbol prefix before the symbol name. This happens for
2930 // Objective-C symbol names and symbol whose name is replaced using GCC's
2931 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002932 if (LName[0] == 1)
2933 LName = &LName[1];
Devang Patel53bb5c92009-11-10 23:06:00 +00002934// Asm->EmitString(LName);
2935 EmitSectionOffset("string", "section_str",
2936 StringPool.idFor(LName), false, true);
2937
Devang Patel53cb17d2009-07-16 01:01:22 +00002938 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002939 Asm->EOL("MIPS linkage name");
Devang Patel53bb5c92009-11-10 23:06:00 +00002940// Asm->EmitString(Name);
2941 EmitSectionOffset("string", "section_str",
2942 StringPool.idFor(Name), false, true);
2943 Asm->EOL("Function name");
Bill Wendling94d04b82009-05-20 23:21:38 +00002944 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2945
Devang Patel53bb5c92009-11-10 23:06:00 +00002946 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002947 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002948 DIE *SP = LI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00002949 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2950
2951 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002952 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002953 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002954 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002955
Devang Patel53bb5c92009-11-10 23:06:00 +00002956 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling94d04b82009-05-20 23:21:38 +00002957 }
2958 }
2959
2960 EmitLabel("debug_inlined_end", 1);
2961 Asm->EOL();
2962}