blob: 82b48acbbb8a12dc268306bf51bb9faa8f68bc37 [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.
51class VISIBILITY_HIDDEN CompileUnit {
52 /// 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
63 std::map<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
68 std::map<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; }
84 DIE* getDie() 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
95 /// getDieMapSlotFor - Returns the debug information entry map slot for the
96 /// specified debug variable.
Devang Patele4b27562009-08-28 23:24:31 +000097 DIE *&getDieMapSlotFor(MDNode *N) { return GVToDieMap[N]; }
Bill Wendling0310d762009-05-15 09:23:25 +000098
Chris Lattner1cda87c2009-07-14 04:50:12 +000099 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for
100 /// the specified debug variable.
Devang Patele4b27562009-08-28 23:24:31 +0000101 DIEEntry *&getDIEEntrySlotFor(MDNode *N) {
102 return GVToDIEEntryMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +0000103 }
104
105 /// AddDie - Adds or interns the DIE to the compile unit.
106 ///
107 DIE *AddDie(DIE &Buffer) {
108 FoldingSetNodeID ID;
109 Buffer.Profile(ID);
110 void *Where;
111 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
112
113 if (!Die) {
114 Die = new DIE(Buffer);
115 DiesSet.InsertNode(Die, Where);
116 this->Die->AddChild(Die);
117 Buffer.Detach();
118 }
119
120 return Die;
121 }
122};
123
124//===----------------------------------------------------------------------===//
125/// DbgVariable - This class is used to track local variable information.
126///
Jeffrey Yasskin36d52bf2009-11-03 06:29:36 +0000127class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000128 DIVariable Var; // Variable Descriptor.
129 unsigned FrameIndex; // Variable frame index.
Devang Patel53bb5c92009-11-10 23:06:00 +0000130 DbgVariable *AbstractVar; // Abstract variable for this variable.
131 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000132public:
Devang Patel53bb5c92009-11-10 23:06:00 +0000133 DbgVariable(DIVariable V, unsigned I)
134 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000135
136 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000137 DIVariable getVariable() const { return Var; }
138 unsigned getFrameIndex() const { return FrameIndex; }
139 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
140 DbgVariable *getAbstractVariable() const { return AbstractVar; }
141 void setDIE(DIE *D) { TheDIE = D; }
142 DIE *getDIE() const { return TheDIE; }
Bill Wendling0310d762009-05-15 09:23:25 +0000143};
144
145//===----------------------------------------------------------------------===//
146/// DbgScope - This class is used to track scope information.
147///
148class DbgConcreteScope;
Jeffrey Yasskin36d52bf2009-11-03 06:29:36 +0000149class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000150 DbgScope *Parent; // Parent to this scope.
Devang Patel53bb5c92009-11-10 23:06:00 +0000151 DIDescriptor Desc; // Debug info descriptor for scope.
152 WeakVH InlinedAtLocation; // Location at which scope is inlined.
153 bool AbstractScope; // Abstract Scope
Bill Wendling0310d762009-05-15 09:23:25 +0000154 unsigned StartLabelID; // Label ID of the beginning of scope.
155 unsigned EndLabelID; // Label ID of the end of scope.
Devang Pateld38dd112009-10-01 18:25:23 +0000156 const MachineInstr *LastInsn; // Last instruction of this scope.
157 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000158 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
159 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000160
Owen Anderson04c05f72009-06-24 22:53:20 +0000161 // Private state for dump()
162 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000163public:
Devang Patelc90aefe2009-10-14 21:08:09 +0000164 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000165 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
166 StartLabelID(0), EndLabelID(0),
Devang Patelc90aefe2009-10-14 21:08:09 +0000167 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000168 virtual ~DbgScope();
169
170 // Accessors.
171 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000172 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000173 DIDescriptor getDesc() const { return Desc; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000174 MDNode *getInlinedAt() const {
175 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Patelc90aefe2009-10-14 21:08:09 +0000176 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000177 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000178 unsigned getStartLabelID() const { return StartLabelID; }
179 unsigned getEndLabelID() const { return EndLabelID; }
180 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
181 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendling0310d762009-05-15 09:23:25 +0000182 void setStartLabelID(unsigned S) { StartLabelID = S; }
183 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Pateld38dd112009-10-01 18:25:23 +0000184 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
185 const MachineInstr *getLastInsn() { return LastInsn; }
186 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000187 void setAbstractScope() { AbstractScope = true; }
188 bool isAbstractScope() const { return AbstractScope; }
Devang Pateld38dd112009-10-01 18:25:23 +0000189 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000190
Bill Wendling0310d762009-05-15 09:23:25 +0000191 /// AddScope - Add a scope to the scope.
192 ///
193 void AddScope(DbgScope *S) { Scopes.push_back(S); }
194
195 /// AddVariable - Add a variable to the scope.
196 ///
197 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
198
Devang Patelaf9e8472009-10-01 20:31:14 +0000199 void FixInstructionMarkers() {
200 assert (getFirstInsn() && "First instruction is missing!");
201 if (getLastInsn())
202 return;
203
204 // If a scope does not have an instruction to mark an end then use
205 // the end of last child scope.
206 SmallVector<DbgScope *, 4> &Scopes = getScopes();
207 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
208 DbgScope *L = Scopes.back();
209 if (!L->getLastInsn())
210 L->FixInstructionMarkers();
211 setLastInsn(L->getLastInsn());
212 }
213
Bill Wendling0310d762009-05-15 09:23:25 +0000214#ifndef NDEBUG
215 void dump() const;
216#endif
217};
218
219#ifndef NDEBUG
220void DbgScope::dump() const {
Chris Lattnerc281de12009-08-23 00:51:00 +0000221 raw_ostream &err = errs();
222 err.indent(IndentLevel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000223 MDNode *N = Desc.getNode();
224 N->dump();
Chris Lattnerc281de12009-08-23 00:51:00 +0000225 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel53bb5c92009-11-10 23:06:00 +0000226 if (AbstractScope)
227 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000228
229 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000230 if (!Scopes.empty())
231 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000232 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
233 if (Scopes[i] != this)
234 Scopes[i]->dump();
235
236 IndentLevel -= 2;
237}
238#endif
239
240//===----------------------------------------------------------------------===//
241/// DbgConcreteScope - This class is used to track a scope that holds concrete
242/// instance information.
243///
244class VISIBILITY_HIDDEN DbgConcreteScope : public DbgScope {
245 CompileUnit *Unit;
246 DIE *Die; // Debug info for this concrete scope.
247public:
248 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
249
250 // Accessors.
251 DIE *getDie() const { return Die; }
252 void setDie(DIE *D) { Die = D; }
253};
254
255DbgScope::~DbgScope() {
256 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
257 delete Scopes[i];
258 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
259 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000260}
261
262} // end llvm namespace
263
Chris Lattneraf76e592009-08-22 20:48:53 +0000264DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000265 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000266 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000267 ValuesSet(InitValuesSetSize), Values(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000268 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel53bb5c92009-11-10 23:06:00 +0000269 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000270 if (TimePassesIsEnabled)
271 DebugTimer = new Timer("Dwarf Debug Writer",
272 getDwarfTimerGroup());
273}
274DwarfDebug::~DwarfDebug() {
275 for (unsigned j = 0, M = Values.size(); j < M; ++j)
276 delete Values[j];
277
Bill Wendling0310d762009-05-15 09:23:25 +0000278 delete DebugTimer;
279}
280
281/// AssignAbbrevNumber - Define a unique number for the abbreviation.
282///
283void DwarfDebug::AssignAbbrevNumber(DIEAbbrev &Abbrev) {
284 // Profile the node so that we can make it unique.
285 FoldingSetNodeID ID;
286 Abbrev.Profile(ID);
287
288 // Check the set for priors.
289 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
290
291 // If it's newly added.
292 if (InSet == &Abbrev) {
293 // Add to abbreviation list.
294 Abbreviations.push_back(&Abbrev);
295
296 // Assign the vector position + 1 as its number.
297 Abbrev.setNumber(Abbreviations.size());
298 } else {
299 // Assign existing abbreviation number.
300 Abbrev.setNumber(InSet->getNumber());
301 }
302}
303
Bill Wendling995f80a2009-05-20 23:24:48 +0000304/// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
305/// information entry.
306DIEEntry *DwarfDebug::CreateDIEEntry(DIE *Entry) {
Bill Wendling0310d762009-05-15 09:23:25 +0000307 DIEEntry *Value;
308
309 if (Entry) {
310 FoldingSetNodeID ID;
311 DIEEntry::Profile(ID, Entry);
312 void *Where;
313 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
314
315 if (Value) return Value;
316
317 Value = new DIEEntry(Entry);
318 ValuesSet.InsertNode(Value, Where);
319 } else {
320 Value = new DIEEntry(Entry);
321 }
322
323 Values.push_back(Value);
324 return Value;
325}
326
327/// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
328///
329void DwarfDebug::SetDIEEntry(DIEEntry *Value, DIE *Entry) {
330 Value->setEntry(Entry);
331
332 // Add to values set if not already there. If it is, we merely have a
333 // duplicate in the values list (no harm.)
334 ValuesSet.GetOrInsertNode(Value);
335}
336
337/// AddUInt - Add an unsigned integer attribute data and value.
338///
339void DwarfDebug::AddUInt(DIE *Die, unsigned Attribute,
340 unsigned Form, uint64_t Integer) {
341 if (!Form) Form = DIEInteger::BestForm(false, Integer);
342
343 FoldingSetNodeID ID;
344 DIEInteger::Profile(ID, Integer);
345 void *Where;
346 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
347
348 if (!Value) {
349 Value = new DIEInteger(Integer);
350 ValuesSet.InsertNode(Value, Where);
351 Values.push_back(Value);
352 }
353
354 Die->AddValue(Attribute, Form, Value);
355}
356
357/// AddSInt - Add an signed integer attribute data and value.
358///
359void DwarfDebug::AddSInt(DIE *Die, unsigned Attribute,
360 unsigned Form, int64_t Integer) {
361 if (!Form) Form = DIEInteger::BestForm(true, Integer);
362
363 FoldingSetNodeID ID;
364 DIEInteger::Profile(ID, (uint64_t)Integer);
365 void *Where;
366 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
367
368 if (!Value) {
369 Value = new DIEInteger(Integer);
370 ValuesSet.InsertNode(Value, Where);
371 Values.push_back(Value);
372 }
373
374 Die->AddValue(Attribute, Form, Value);
375}
376
377/// AddString - Add a string attribute data and value.
378///
379void DwarfDebug::AddString(DIE *Die, unsigned Attribute, unsigned Form,
380 const std::string &String) {
381 FoldingSetNodeID ID;
382 DIEString::Profile(ID, String);
383 void *Where;
384 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
385
386 if (!Value) {
387 Value = new DIEString(String);
388 ValuesSet.InsertNode(Value, Where);
389 Values.push_back(Value);
390 }
391
392 Die->AddValue(Attribute, Form, Value);
393}
394
395/// AddLabel - Add a Dwarf label attribute data and value.
396///
397void DwarfDebug::AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
398 const DWLabel &Label) {
399 FoldingSetNodeID ID;
400 DIEDwarfLabel::Profile(ID, Label);
401 void *Where;
402 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
403
404 if (!Value) {
405 Value = new DIEDwarfLabel(Label);
406 ValuesSet.InsertNode(Value, Where);
407 Values.push_back(Value);
408 }
409
410 Die->AddValue(Attribute, Form, Value);
411}
412
413/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
414///
415void DwarfDebug::AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
416 const std::string &Label) {
417 FoldingSetNodeID ID;
418 DIEObjectLabel::Profile(ID, Label);
419 void *Where;
420 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
421
422 if (!Value) {
423 Value = new DIEObjectLabel(Label);
424 ValuesSet.InsertNode(Value, Where);
425 Values.push_back(Value);
426 }
427
428 Die->AddValue(Attribute, Form, Value);
429}
430
431/// AddSectionOffset - Add a section offset label attribute data and value.
432///
433void DwarfDebug::AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
434 const DWLabel &Label, const DWLabel &Section,
435 bool isEH, bool useSet) {
436 FoldingSetNodeID ID;
437 DIESectionOffset::Profile(ID, Label, Section);
438 void *Where;
439 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
440
441 if (!Value) {
442 Value = new DIESectionOffset(Label, Section, isEH, useSet);
443 ValuesSet.InsertNode(Value, Where);
444 Values.push_back(Value);
445 }
446
447 Die->AddValue(Attribute, Form, Value);
448}
449
450/// AddDelta - Add a label delta attribute data and value.
451///
452void DwarfDebug::AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
453 const DWLabel &Hi, const DWLabel &Lo) {
454 FoldingSetNodeID ID;
455 DIEDelta::Profile(ID, Hi, Lo);
456 void *Where;
457 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
458
459 if (!Value) {
460 Value = new DIEDelta(Hi, Lo);
461 ValuesSet.InsertNode(Value, Where);
462 Values.push_back(Value);
463 }
464
465 Die->AddValue(Attribute, Form, Value);
466}
467
468/// AddBlock - Add block data.
469///
470void DwarfDebug::AddBlock(DIE *Die, unsigned Attribute, unsigned Form,
471 DIEBlock *Block) {
472 Block->ComputeSize(TD);
473 FoldingSetNodeID ID;
474 Block->Profile(ID);
475 void *Where;
476 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
477
478 if (!Value) {
479 Value = Block;
480 ValuesSet.InsertNode(Value, Where);
481 Values.push_back(Value);
482 } else {
483 // Already exists, reuse the previous one.
484 delete Block;
485 Block = cast<DIEBlock>(Value);
486 }
487
488 Die->AddValue(Attribute, Block->BestForm(), Value);
489}
490
491/// AddSourceLine - Add location information to specified debug information
492/// entry.
493void DwarfDebug::AddSourceLine(DIE *Die, const DIVariable *V) {
494 // If there is no compile unit specified, don't add a line #.
495 if (V->getCompileUnit().isNull())
496 return;
497
498 unsigned Line = V->getLineNumber();
499 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
500 assert(FileID && "Invalid file id");
501 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
502 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
503}
504
505/// AddSourceLine - Add location information to specified debug information
506/// entry.
507void DwarfDebug::AddSourceLine(DIE *Die, const DIGlobal *G) {
508 // If there is no compile unit specified, don't add a line #.
509 if (G->getCompileUnit().isNull())
510 return;
511
512 unsigned Line = G->getLineNumber();
513 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
514 assert(FileID && "Invalid file id");
515 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
516 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
517}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000518
519/// AddSourceLine - Add location information to specified debug information
520/// entry.
521void DwarfDebug::AddSourceLine(DIE *Die, const DISubprogram *SP) {
522 // If there is no compile unit specified, don't add a line #.
523 if (SP->getCompileUnit().isNull())
524 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000525 // If the line number is 0, don't add it.
526 if (SP->getLineNumber() == 0)
527 return;
528
Devang Patel82dfc0c2009-08-31 22:47:13 +0000529
530 unsigned Line = SP->getLineNumber();
531 unsigned FileID = FindCompileUnit(SP->getCompileUnit()).getID();
532 assert(FileID && "Invalid file id");
533 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
534 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
535}
536
537/// AddSourceLine - Add location information to specified debug information
538/// entry.
Bill Wendling0310d762009-05-15 09:23:25 +0000539void DwarfDebug::AddSourceLine(DIE *Die, const DIType *Ty) {
540 // If there is no compile unit specified, don't add a line #.
541 DICompileUnit CU = Ty->getCompileUnit();
542 if (CU.isNull())
543 return;
544
545 unsigned Line = Ty->getLineNumber();
546 unsigned FileID = FindCompileUnit(CU).getID();
547 assert(FileID && "Invalid file id");
548 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
549 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
550}
551
Caroline Ticedc8f6042009-08-31 21:19:37 +0000552/* Byref variables, in Blocks, are declared by the programmer as
553 "SomeType VarName;", but the compiler creates a
554 __Block_byref_x_VarName struct, and gives the variable VarName
555 either the struct, or a pointer to the struct, as its type. This
556 is necessary for various behind-the-scenes things the compiler
557 needs to do with by-reference variables in blocks.
558
559 However, as far as the original *programmer* is concerned, the
560 variable should still have type 'SomeType', as originally declared.
561
562 The following function dives into the __Block_byref_x_VarName
563 struct to find the original type of the variable. This will be
564 passed back to the code generating the type for the Debug
565 Information Entry for the variable 'VarName'. 'VarName' will then
566 have the original type 'SomeType' in its debug information.
567
568 The original type 'SomeType' will be the type of the field named
569 'VarName' inside the __Block_byref_x_VarName struct.
570
571 NOTE: In order for this to not completely fail on the debugger
572 side, the Debug Information Entry for the variable VarName needs to
573 have a DW_AT_location that tells the debugger how to unwind through
574 the pointers and __Block_byref_x_VarName struct to find the actual
575 value of the variable. The function AddBlockByrefType does this. */
576
577/// Find the type the programmer originally declared the variable to be
578/// and return that type.
579///
580DIType DwarfDebug::GetBlockByrefType(DIType Ty, std::string Name) {
581
582 DIType subType = Ty;
583 unsigned tag = Ty.getTag();
584
585 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000586 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000587 subType = DTy.getTypeDerivedFrom();
588 }
589
590 DICompositeType blockStruct = DICompositeType(subType.getNode());
591
592 DIArray Elements = blockStruct.getTypeArray();
593
594 if (Elements.isNull())
595 return Ty;
596
597 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
598 DIDescriptor Element = Elements.getElement(i);
599 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel5ccdd102009-09-29 18:40:58 +0000600 if (strcmp(Name.c_str(), DT.getName()) == 0)
Caroline Ticedc8f6042009-08-31 21:19:37 +0000601 return (DT.getTypeDerivedFrom());
602 }
603
604 return Ty;
605}
606
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000607/// AddComplexAddress - Start with the address based on the location provided,
608/// and generate the DWARF information necessary to find the actual variable
609/// given the extra address information encoded in the DIVariable, starting from
610/// the starting location. Add the DWARF information to the die.
611///
612void DwarfDebug::AddComplexAddress(DbgVariable *&DV, DIE *Die,
613 unsigned Attribute,
614 const MachineLocation &Location) {
615 const DIVariable &VD = DV->getVariable();
616 DIType Ty = VD.getType();
617
618 // Decode the original location, and use that as the start of the byref
619 // variable's location.
620 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
621 DIEBlock *Block = new DIEBlock();
622
623 if (Location.isReg()) {
624 if (Reg < 32) {
625 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
626 } else {
627 Reg = Reg - dwarf::DW_OP_reg0;
628 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
629 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
630 }
631 } else {
632 if (Reg < 32)
633 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
634 else {
635 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
636 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
637 }
638
639 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
640 }
641
642 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
643 uint64_t Element = VD.getAddrElement(i);
644
645 if (Element == DIFactory::OpPlus) {
646 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
647 AddUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
648 } else if (Element == DIFactory::OpDeref) {
649 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
650 } else llvm_unreachable("unknown DIFactory Opcode");
651 }
652
653 // Now attach the location information to the DIE.
654 AddBlock(Die, Attribute, 0, Block);
655}
656
Caroline Ticedc8f6042009-08-31 21:19:37 +0000657/* Byref variables, in Blocks, are declared by the programmer as "SomeType
658 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
659 gives the variable VarName either the struct, or a pointer to the struct, as
660 its type. This is necessary for various behind-the-scenes things the
661 compiler needs to do with by-reference variables in Blocks.
662
663 However, as far as the original *programmer* is concerned, the variable
664 should still have type 'SomeType', as originally declared.
665
666 The function GetBlockByrefType dives into the __Block_byref_x_VarName
667 struct to find the original type of the variable, which is then assigned to
668 the variable's Debug Information Entry as its real type. So far, so good.
669 However now the debugger will expect the variable VarName to have the type
670 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000671 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000672 pointers and struct to find the actual variable of type SomeType.
673
674 The following function does just that. We start by getting
675 the "normal" location for the variable. This will be the location
676 of either the struct __Block_byref_x_VarName or the pointer to the
677 struct __Block_byref_x_VarName.
678
679 The struct will look something like:
680
681 struct __Block_byref_x_VarName {
682 ... <various fields>
683 struct __Block_byref_x_VarName *forwarding;
684 ... <various other fields>
685 SomeType VarName;
686 ... <maybe more fields>
687 };
688
689 If we are given the struct directly (as our starting point) we
690 need to tell the debugger to:
691
692 1). Add the offset of the forwarding field.
693
694 2). Follow that pointer to get the the real __Block_byref_x_VarName
695 struct to use (the real one may have been copied onto the heap).
696
697 3). Add the offset for the field VarName, to find the actual variable.
698
699 If we started with a pointer to the struct, then we need to
700 dereference that pointer first, before the other steps.
701 Translating this into DWARF ops, we will need to append the following
702 to the current location description for the variable:
703
704 DW_OP_deref -- optional, if we start with a pointer
705 DW_OP_plus_uconst <forward_fld_offset>
706 DW_OP_deref
707 DW_OP_plus_uconst <varName_fld_offset>
708
709 That is what this function does. */
710
711/// AddBlockByrefAddress - Start with the address based on the location
712/// provided, and generate the DWARF information necessary to find the
713/// actual Block variable (navigating the Block struct) based on the
714/// starting location. Add the DWARF information to the die. For
715/// more information, read large comment just above here.
716///
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000717void DwarfDebug::AddBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000718 unsigned Attribute,
719 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000720 const DIVariable &VD = DV->getVariable();
721 DIType Ty = VD.getType();
722 DIType TmpTy = Ty;
723 unsigned Tag = Ty.getTag();
724 bool isPointer = false;
725
Devang Patel5ccdd102009-09-29 18:40:58 +0000726 const char *varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000727
728 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000729 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000730 TmpTy = DTy.getTypeDerivedFrom();
731 isPointer = true;
732 }
733
734 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
735
Daniel Dunbar00564992009-09-19 20:40:14 +0000736 // Find the __forwarding field and the variable field in the __Block_byref
737 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000738 DIArray Fields = blockStruct.getTypeArray();
739 DIDescriptor varField = DIDescriptor();
740 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000741
742
Daniel Dunbar00564992009-09-19 20:40:14 +0000743 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
744 DIDescriptor Element = Fields.getElement(i);
745 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel5ccdd102009-09-29 18:40:58 +0000746 const char *fieldName = DT.getName();
747 if (strcmp(fieldName, "__forwarding") == 0)
Daniel Dunbar00564992009-09-19 20:40:14 +0000748 forwardingField = Element;
Devang Patel5ccdd102009-09-29 18:40:58 +0000749 else if (strcmp(fieldName, varName) == 0)
Daniel Dunbar00564992009-09-19 20:40:14 +0000750 varField = Element;
751 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000752
Mike Stump7e3720d2009-09-24 23:21:26 +0000753 assert(!varField.isNull() && "Can't find byref variable in Block struct");
754 assert(!forwardingField.isNull()
755 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000756
Daniel Dunbar00564992009-09-19 20:40:14 +0000757 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000758 unsigned int forwardingFieldOffset =
759 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
760 unsigned int varFieldOffset =
761 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000762
Mike Stump7e3720d2009-09-24 23:21:26 +0000763 // Decode the original location, and use that as the start of the byref
764 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000765 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
766 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000767
Daniel Dunbar00564992009-09-19 20:40:14 +0000768 if (Location.isReg()) {
769 if (Reg < 32)
770 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
771 else {
772 Reg = Reg - dwarf::DW_OP_reg0;
773 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
774 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
775 }
776 } else {
777 if (Reg < 32)
778 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
779 else {
780 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
781 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
782 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000783
Daniel Dunbar00564992009-09-19 20:40:14 +0000784 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
785 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000786
Mike Stump7e3720d2009-09-24 23:21:26 +0000787 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000788 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000789 if (isPointer)
790 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000791
Daniel Dunbar00564992009-09-19 20:40:14 +0000792 // Next add the offset for the '__forwarding' field:
793 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
794 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000795 if (forwardingFieldOffset > 0) {
796 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
797 AddUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
798 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000799
Daniel Dunbar00564992009-09-19 20:40:14 +0000800 // Now dereference the __forwarding field to get to the real __Block_byref
801 // struct: DW_OP_deref.
Daniel Dunbar00564992009-09-19 20:40:14 +0000802 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000803
Daniel Dunbar00564992009-09-19 20:40:14 +0000804 // Now that we've got the real __Block_byref... struct, add the offset
805 // for the variable's field to get to the location of the actual variable:
806 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000807 if (varFieldOffset > 0) {
808 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
809 AddUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
810 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000811
Daniel Dunbar00564992009-09-19 20:40:14 +0000812 // Now attach the location information to the DIE.
Daniel Dunbar00564992009-09-19 20:40:14 +0000813 AddBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000814}
815
Bill Wendling0310d762009-05-15 09:23:25 +0000816/// AddAddress - Add an address attribute to a die based on the location
817/// provided.
818void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute,
819 const MachineLocation &Location) {
820 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
821 DIEBlock *Block = new DIEBlock();
822
823 if (Location.isReg()) {
824 if (Reg < 32) {
825 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
826 } else {
827 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
828 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
829 }
830 } else {
831 if (Reg < 32) {
832 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
833 } else {
834 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
835 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
836 }
837
838 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
839 }
840
841 AddBlock(Die, Attribute, 0, Block);
842}
843
844/// AddType - Add a new type attribute to the specified entity.
845void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
846 if (Ty.isNull())
847 return;
848
849 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +0000850 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000851
852 // If it exists then use the existing value.
853 if (Slot) {
854 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
855 return;
856 }
857
858 // Set up proxy.
Bill Wendling995f80a2009-05-20 23:24:48 +0000859 Slot = CreateDIEEntry();
Bill Wendling0310d762009-05-15 09:23:25 +0000860
861 // Construct type.
862 DIE Buffer(dwarf::DW_TAG_base_type);
Devang Patel6ceea332009-08-31 18:49:10 +0000863 if (Ty.isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000864 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getNode()));
Devang Patel6ceea332009-08-31 18:49:10 +0000865 else if (Ty.isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000866 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000867 else {
Devang Patel6ceea332009-08-31 18:49:10 +0000868 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patele4b27562009-08-28 23:24:31 +0000869 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000870 }
871
872 // Add debug information entry to entity and appropriate context.
873 DIE *Die = NULL;
874 DIDescriptor Context = Ty.getContext();
875 if (!Context.isNull())
Devang Patele4b27562009-08-28 23:24:31 +0000876 Die = DW_Unit->getDieMapSlotFor(Context.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000877
878 if (Die) {
879 DIE *Child = new DIE(Buffer);
880 Die->AddChild(Child);
881 Buffer.Detach();
882 SetDIEEntry(Slot, Child);
883 } else {
884 Die = DW_Unit->AddDie(Buffer);
885 SetDIEEntry(Slot, Die);
886 }
887
888 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
889}
890
891/// ConstructTypeDIE - Construct basic type die from DIBasicType.
892void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
893 DIBasicType BTy) {
894 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000895 const char *Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000896 Buffer.setTag(dwarf::DW_TAG_base_type);
897 AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
898 BTy.getEncoding());
899
900 // Add name if not anonymous or intermediate type.
Devang Patel5ccdd102009-09-29 18:40:58 +0000901 if (Name)
Bill Wendling0310d762009-05-15 09:23:25 +0000902 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
903 uint64_t Size = BTy.getSizeInBits() >> 3;
904 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
905}
906
907/// ConstructTypeDIE - Construct derived type die from DIDerivedType.
908void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
909 DIDerivedType DTy) {
910 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000911 const char *Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000912 uint64_t Size = DTy.getSizeInBits() >> 3;
913 unsigned Tag = DTy.getTag();
914
915 // FIXME - Workaround for templates.
916 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
917
918 Buffer.setTag(Tag);
919
920 // Map to main type, void will not have a type.
921 DIType FromTy = DTy.getTypeDerivedFrom();
922 AddType(DW_Unit, &Buffer, FromTy);
923
924 // Add name if not anonymous or intermediate type.
Devang Patel808b8262009-10-16 21:27:43 +0000925 if (Name && Tag != dwarf::DW_TAG_pointer_type)
Bill Wendling0310d762009-05-15 09:23:25 +0000926 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
927
928 // Add size if non-zero (derived types might be zero-sized.)
929 if (Size)
930 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
931
932 // Add source line info if available and TyDesc is not a forward declaration.
933 if (!DTy.isForwardDecl())
934 AddSourceLine(&Buffer, &DTy);
935}
936
937/// ConstructTypeDIE - Construct type DIE from DICompositeType.
938void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
939 DICompositeType CTy) {
940 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000941 const char *Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000942
943 uint64_t Size = CTy.getSizeInBits() >> 3;
944 unsigned Tag = CTy.getTag();
945 Buffer.setTag(Tag);
946
947 switch (Tag) {
948 case dwarf::DW_TAG_vector_type:
949 case dwarf::DW_TAG_array_type:
950 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
951 break;
952 case dwarf::DW_TAG_enumeration_type: {
953 DIArray Elements = CTy.getTypeArray();
954
955 // Add enumerators to enumeration type.
956 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
957 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000958 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelc5254722009-10-09 17:51:49 +0000959 if (!Enum.isNull()) {
960 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
961 Buffer.AddChild(ElemDie);
962 }
Bill Wendling0310d762009-05-15 09:23:25 +0000963 }
964 }
965 break;
966 case dwarf::DW_TAG_subroutine_type: {
967 // Add return type.
968 DIArray Elements = CTy.getTypeArray();
969 DIDescriptor RTy = Elements.getElement(0);
Devang Patele4b27562009-08-28 23:24:31 +0000970 AddType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000971
972 // Add prototype flag.
973 AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
974
975 // Add arguments.
976 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
977 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
978 DIDescriptor Ty = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000979 AddType(DW_Unit, Arg, DIType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000980 Buffer.AddChild(Arg);
981 }
982 }
983 break;
984 case dwarf::DW_TAG_structure_type:
985 case dwarf::DW_TAG_union_type:
986 case dwarf::DW_TAG_class_type: {
987 // Add elements to structure type.
988 DIArray Elements = CTy.getTypeArray();
989
990 // A forward struct declared type may not have elements available.
991 if (Elements.isNull())
992 break;
993
994 // Add elements to structure type.
995 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
996 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000997 if (Element.isNull())
998 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000999 DIE *ElemDie = NULL;
1000 if (Element.getTag() == dwarf::DW_TAG_subprogram)
1001 ElemDie = CreateSubprogramDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +00001002 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001003 else
1004 ElemDie = CreateMemberDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +00001005 DIDerivedType(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001006 Buffer.AddChild(ElemDie);
1007 }
1008
Devang Patela1ba2692009-08-27 23:51:51 +00001009 if (CTy.isAppleBlockExtension())
Bill Wendling0310d762009-05-15 09:23:25 +00001010 AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
1011
1012 unsigned RLang = CTy.getRunTimeLang();
1013 if (RLang)
1014 AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1015 dwarf::DW_FORM_data1, RLang);
1016 break;
1017 }
1018 default:
1019 break;
1020 }
1021
1022 // Add name if not anonymous or intermediate type.
Devang Patel5ccdd102009-09-29 18:40:58 +00001023 if (Name)
Bill Wendling0310d762009-05-15 09:23:25 +00001024 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1025
1026 if (Tag == dwarf::DW_TAG_enumeration_type ||
1027 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1028 // Add size if non-zero (derived types might be zero-sized.)
1029 if (Size)
1030 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
1031 else {
1032 // Add zero size if it is not a forward declaration.
1033 if (CTy.isForwardDecl())
1034 AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1035 else
1036 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1037 }
1038
1039 // Add source line info if available.
1040 if (!CTy.isForwardDecl())
1041 AddSourceLine(&Buffer, &CTy);
1042 }
1043}
1044
1045/// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1046void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
1047 int64_t L = SR.getLo();
1048 int64_t H = SR.getHi();
1049 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1050
Devang Patel6325a532009-08-14 20:59:16 +00001051 AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
1052 if (L)
1053 AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
1054 if (H)
Daniel Dunbar00564992009-09-19 20:40:14 +00001055 AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001056
1057 Buffer.AddChild(DW_Subrange);
1058}
1059
1060/// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1061void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1062 DICompositeType *CTy) {
1063 Buffer.setTag(dwarf::DW_TAG_array_type);
1064 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1065 AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
1066
1067 // Emit derived type.
1068 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
1069 DIArray Elements = CTy->getTypeArray();
1070
1071 // Construct an anonymous type for index type.
1072 DIE IdxBuffer(dwarf::DW_TAG_base_type);
1073 AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1074 AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1075 dwarf::DW_ATE_signed);
1076 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1077
1078 // Add subranges to array type.
1079 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1080 DIDescriptor Element = Elements.getElement(i);
1081 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patele4b27562009-08-28 23:24:31 +00001082 ConstructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IndexTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001083 }
1084}
1085
1086/// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1087DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
1088 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel5ccdd102009-09-29 18:40:58 +00001089 const char *Name = ETy->getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001090 AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1091 int64_t Value = ETy->getEnumValue();
1092 AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1093 return Enumerator;
1094}
1095
1096/// CreateGlobalVariableDIE - Create new DIE using GV.
1097DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit,
1098 const DIGlobalVariable &GV) {
Devang Patel465c3be2009-11-06 01:30:04 +00001099 // If the global variable was optmized out then no need to create debug info entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001100 if (!GV.getGlobal()) return NULL;
1101 if (!GV.getDisplayName()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001102
Bill Wendling0310d762009-05-15 09:23:25 +00001103 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Devang Patel5ccdd102009-09-29 18:40:58 +00001104 AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1105 GV.getDisplayName());
1106
1107 const char *LinkageName = GV.getLinkageName();
1108 if (LinkageName) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001109 // Skip special LLVM prefix that is used to inform the asm printer to not
1110 // emit usual symbol prefix before the symbol name. This happens for
1111 // Objective-C symbol names and symbol whose name is replaced using GCC's
1112 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001113 if (LinkageName[0] == 1)
1114 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001115 AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001116 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001117 }
Daniel Dunbar00564992009-09-19 20:40:14 +00001118 AddType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001119 if (!GV.isLocalToUnit())
1120 AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1121 AddSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001122
1123 // Add address.
1124 DIEBlock *Block = new DIEBlock();
1125 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1126 AddObjectLabel(Block, 0, dwarf::DW_FORM_udata,
1127 Asm->Mang->getMangledName(GV.getGlobal()));
1128 AddBlock(GVDie, dwarf::DW_AT_location, 0, Block);
1129
Bill Wendling0310d762009-05-15 09:23:25 +00001130 return GVDie;
1131}
1132
1133/// CreateMemberDIE - Create new member DIE.
1134DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
1135 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel5ccdd102009-09-29 18:40:58 +00001136 if (const char *Name = DT.getName())
Bill Wendling0310d762009-05-15 09:23:25 +00001137 AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1138
1139 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1140
1141 AddSourceLine(MemberDie, &DT);
1142
Devang Patel33db5082009-11-04 22:06:12 +00001143 DIEBlock *MemLocationDie = new DIEBlock();
1144 AddUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1145
Bill Wendling0310d762009-05-15 09:23:25 +00001146 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001147 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001148
1149 if (Size != FieldSize) {
1150 // Handle bitfield.
1151 AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1152 AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1153
1154 uint64_t Offset = DT.getOffsetInBits();
1155 uint64_t FieldOffset = Offset;
1156 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1157 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1158 FieldOffset = (HiMark - FieldSize);
1159 Offset -= FieldOffset;
1160
1161 // Maybe we need to work from the other end.
1162 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1163 AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001164
Devang Patel33db5082009-11-04 22:06:12 +00001165 // Here WD_AT_data_member_location points to the anonymous
1166 // field that includes this bit field.
1167 AddUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1168
1169 } else
1170 // This is not a bitfield.
1171 AddUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1172
1173 AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001174
1175 if (DT.isProtected())
1176 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1177 dwarf::DW_ACCESS_protected);
1178 else if (DT.isPrivate())
1179 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1180 dwarf::DW_ACCESS_private);
1181
1182 return MemberDie;
1183}
1184
1185/// CreateSubprogramDIE - Create new DIE using SP.
1186DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit,
1187 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +00001188 bool IsConstructor,
1189 bool IsInlined) {
Bill Wendling0310d762009-05-15 09:23:25 +00001190 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1191
Devang Patel5ccdd102009-09-29 18:40:58 +00001192 const char * Name = SP.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001193 AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1194
Devang Patel5ccdd102009-09-29 18:40:58 +00001195 const char *LinkageName = SP.getLinkageName();
1196 if (LinkageName) {
Devang Patel53cb17d2009-07-16 01:01:22 +00001197 // Skip special LLVM prefix that is used to inform the asm printer to not emit
1198 // usual symbol prefix before the symbol name. This happens for Objective-C
1199 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
1200 if (LinkageName[0] == 1)
1201 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001202 AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001203 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001204 }
Bill Wendling0310d762009-05-15 09:23:25 +00001205 AddSourceLine(SPDie, &SP);
1206
1207 DICompositeType SPTy = SP.getType();
1208 DIArray Args = SPTy.getTypeArray();
1209
1210 // Add prototyped tag, if C or ObjC.
1211 unsigned Lang = SP.getCompileUnit().getLanguage();
1212 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1213 Lang == dwarf::DW_LANG_ObjC)
1214 AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
1215
1216 // Add Return Type.
1217 unsigned SPTag = SPTy.getTag();
1218 if (!IsConstructor) {
1219 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1220 AddType(DW_Unit, SPDie, SPTy);
1221 else
Devang Patele4b27562009-08-28 23:24:31 +00001222 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001223 }
1224
1225 if (!SP.isDefinition()) {
1226 AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1227
1228 // Add arguments. Do not add arguments for subprogram definition. They will
1229 // be handled through RecordVariable.
1230 if (SPTag == dwarf::DW_TAG_subroutine_type)
1231 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1232 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patele4b27562009-08-28 23:24:31 +00001233 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001234 AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1235 SPDie->AddChild(Arg);
1236 }
1237 }
1238
Bill Wendling0310d762009-05-15 09:23:25 +00001239 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patele4b27562009-08-28 23:24:31 +00001240 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001241 Slot = SPDie;
1242 return SPDie;
1243}
1244
1245/// FindCompileUnit - Get the compile unit for the given descriptor.
1246///
1247CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const {
1248 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001249 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001250 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1251 return *I->second;
1252}
1253
Bill Wendling995f80a2009-05-20 23:24:48 +00001254/// CreateDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +00001255///
Bill Wendling995f80a2009-05-20 23:24:48 +00001256DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001257 // Get the descriptor.
1258 const DIVariable &VD = DV->getVariable();
Devang Patel1b845972009-11-03 18:30:27 +00001259 const char *Name = VD.getName();
1260 if (!Name)
1261 return NULL;
Bill Wendling0310d762009-05-15 09:23:25 +00001262
1263 // Translate tag to proper Dwarf tag. The result variable is dropped for
1264 // now.
1265 unsigned Tag;
1266 switch (VD.getTag()) {
1267 case dwarf::DW_TAG_return_variable:
1268 return NULL;
1269 case dwarf::DW_TAG_arg_variable:
1270 Tag = dwarf::DW_TAG_formal_parameter;
1271 break;
1272 case dwarf::DW_TAG_auto_variable: // fall thru
1273 default:
1274 Tag = dwarf::DW_TAG_variable;
1275 break;
1276 }
1277
1278 // Define variable debug information entry.
1279 DIE *VariableDie = new DIE(Tag);
Bill Wendling0310d762009-05-15 09:23:25 +00001280 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1281
1282 // Add source line info if available.
1283 AddSourceLine(VariableDie, &VD);
1284
1285 // Add variable type.
Devang Patel53bb5c92009-11-10 23:06:00 +00001286 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
1287 // addresses instead.
Caroline Ticedc8f6042009-08-31 21:19:37 +00001288 if (VD.isBlockByrefVariable())
1289 AddType(Unit, VariableDie, GetBlockByrefType(VD.getType(), Name));
1290 else
1291 AddType(Unit, VariableDie, VD.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001292
1293 // Add variable address.
Devang Patel53bb5c92009-11-10 23:06:00 +00001294 // Variables for abstract instances of inlined functions don't get a
1295 // location.
1296 MachineLocation Location;
1297 Location.set(RI->getFrameRegister(*MF),
1298 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1299
1300
1301 if (VD.hasComplexAddress())
1302 AddComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1303 else if (VD.isBlockByrefVariable())
1304 AddBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1305 else
1306 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001307
1308 return VariableDie;
1309}
1310
Devang Patel53bb5c92009-11-10 23:06:00 +00001311/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1312/// Initialize scope and update scope hierarchy.
1313DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1314 MDNode *InlinedAt) {
1315 assert (N && "Invalid Scope encoding!");
1316 assert (MI && "Missing machine instruction!");
1317 bool GetConcreteScope = (MI && InlinedAt);
1318
1319 DbgScope *NScope = NULL;
1320
1321 if (InlinedAt)
1322 NScope = DbgScopeMap.lookup(InlinedAt);
1323 else
1324 NScope = DbgScopeMap.lookup(N);
1325 assert (NScope && "Unable to find working scope!");
1326
1327 if (NScope->getFirstInsn())
1328 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001329
1330 DbgScope *Parent = NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001331 if (GetConcreteScope) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001332 DILocation IL(InlinedAt);
Devang Patel53bb5c92009-11-10 23:06:00 +00001333 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
1334 IL.getOrigLocation().getNode());
1335 assert (Parent && "Unable to find Parent scope!");
1336 NScope->setParent(Parent);
1337 Parent->AddScope(NScope);
1338 } else if (DIDescriptor(N).isLexicalBlock()) {
1339 DILexicalBlock DB(N);
1340 if (!DB.getContext().isNull()) {
1341 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1342 NScope->setParent(Parent);
1343 Parent->AddScope(NScope);
1344 }
Devang Patelc90aefe2009-10-14 21:08:09 +00001345 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001346
Devang Patelbdf45cb2009-10-27 20:47:17 +00001347 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001348
Devang Patel53bb5c92009-11-10 23:06:00 +00001349 if (!Parent && !InlinedAt) {
1350 assert (!CurrentFnDbgScope && "Unexpected function scope!");
1351 CurrentFnDbgScope = NScope;
1352 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001353
Devang Patel53bb5c92009-11-10 23:06:00 +00001354 if (GetConcreteScope) {
1355 ConcreteScopes[InlinedAt] = NScope;
1356 getOrCreateAbstractScope(N);
1357 }
1358
Devang Patelbdf45cb2009-10-27 20:47:17 +00001359 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001360}
1361
Devang Patel53bb5c92009-11-10 23:06:00 +00001362DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1363 assert (N && "Invalid Scope encoding!");
1364
1365 DbgScope *AScope = AbstractScopes.lookup(N);
1366 if (AScope)
1367 return AScope;
1368
1369 DbgScope *Parent = NULL;
1370
1371 DIDescriptor Scope(N);
1372 if (Scope.isLexicalBlock()) {
1373 DILexicalBlock DB(N);
1374 DIDescriptor ParentDesc = DB.getContext();
1375 if (!ParentDesc.isNull())
1376 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1377 }
1378
1379 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1380
1381 if (Parent)
1382 Parent->AddScope(AScope);
1383 AScope->setAbstractScope();
1384 AbstractScopes[N] = AScope;
1385 if (DIDescriptor(N).isSubprogram())
1386 AbstractScopesList.push_back(AScope);
1387 return AScope;
1388}
Devang Patelaf9e8472009-10-01 20:31:14 +00001389
1390/// getOrCreateScope - Returns the scope associated with the given descriptor.
1391/// FIXME - Remove this method.
Devang Patele4b27562009-08-28 23:24:31 +00001392DbgScope *DwarfDebug::getOrCreateScope(MDNode *N) {
1393 DbgScope *&Slot = DbgScopeMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +00001394 if (Slot) return Slot;
1395
1396 DbgScope *Parent = NULL;
Devang Patel5e005d82009-08-31 22:00:15 +00001397 DILexicalBlock Block(N);
Bill Wendling0310d762009-05-15 09:23:25 +00001398
Bill Wendling0310d762009-05-15 09:23:25 +00001399 if (!Block.isNull()) {
1400 DIDescriptor ParentDesc = Block.getContext();
1401 Parent =
Devang Patele4b27562009-08-28 23:24:31 +00001402 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001403 }
1404
Devang Patele4b27562009-08-28 23:24:31 +00001405 Slot = new DbgScope(Parent, DIDescriptor(N));
Bill Wendling0310d762009-05-15 09:23:25 +00001406
1407 if (Parent)
1408 Parent->AddScope(Slot);
1409 else
1410 // First function is top level function.
Devang Patel53bb5c92009-11-10 23:06:00 +00001411 CurrentFnDbgScope = Slot;
Bill Wendling0310d762009-05-15 09:23:25 +00001412
1413 return Slot;
1414}
1415
Devang Patel53bb5c92009-11-10 23:06:00 +00001416static DISubprogram getDISubprogram(MDNode *N) {
1417
1418 DIDescriptor D(N);
1419 if (D.isNull())
1420 return DISubprogram();
1421
1422 if (D.isCompileUnit())
1423 return DISubprogram();
1424
1425 if (D.isSubprogram())
1426 return DISubprogram(N);
1427
1428 if (D.isLexicalBlock())
1429 return getDISubprogram(DILexicalBlock(N).getContext().getNode());
1430
1431 llvm_unreachable("Unexpected Descriptor!");
1432}
1433
1434DIE *DwarfDebug::UpdateSubprogramScopeDIE(MDNode *SPNode) {
1435
1436 DIE *SPDie = ModuleCU->getDieMapSlotFor(SPNode);
1437 assert (SPDie && "Unable to find subprogram DIE!");
1438 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1439 DWLabel("func_begin", SubprogramCount));
1440 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1441 DWLabel("func_end", SubprogramCount));
1442 MachineLocation Location(RI->getFrameRegister(*MF));
1443 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
1444
1445 if (!DISubprogram(SPNode).isLocalToUnit())
1446 AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1447
1448 // If there are global variables at this scope then add their dies.
1449 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
1450 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1451 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1452 if (!N) continue;
1453 DIGlobalVariable GV(N);
1454 if (GV.getContext().getNode() == SPNode) {
1455 DIE *ScopedGVDie = CreateGlobalVariableDIE(ModuleCU, GV);
1456 SPDie->AddChild(ScopedGVDie);
1457 }
1458 }
1459 return SPDie;
1460}
1461
1462DIE *DwarfDebug::ConstructLexicalScopeDIE(DbgScope *Scope) {
1463 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1464 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1465
1466 // Ignore empty scopes.
1467 if (StartID == EndID && StartID != 0)
1468 return NULL;
1469
1470 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1471 if (Scope->isAbstractScope())
1472 return ScopeDIE;
1473
1474 AddLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1475 StartID ?
1476 DWLabel("label", StartID)
1477 : DWLabel("func_begin", SubprogramCount));
1478 AddLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1479 EndID ?
1480 DWLabel("label", EndID)
1481 : DWLabel("func_end", SubprogramCount));
1482
1483
1484
1485 return ScopeDIE;
1486}
1487
1488DIE *DwarfDebug::ConstructInlinedScopeDIE(DbgScope *Scope) {
1489 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1490 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1491 assert (StartID && "Invalid starting label for an inlined scope!");
1492 assert (EndID && "Invalid end label for an inlined scope!");
1493 // Ignore empty scopes.
1494 if (StartID == EndID && StartID != 0)
1495 return NULL;
1496
1497 DIScope DS(Scope->getScopeNode());
1498 if (DS.isNull())
1499 return NULL;
1500 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1501
1502 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
1503 DIE *&OriginDIE = ModuleCU->getDieMapSlotFor(InlinedSP.getNode());
1504 assert (OriginDIE && "Unable to find Origin DIE!");
1505 AddDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
1506 dwarf::DW_FORM_ref4, OriginDIE);
1507
1508 AddLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1509 DWLabel("label", StartID));
1510 AddLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1511 DWLabel("label", EndID));
1512
1513 InlinedSubprogramDIEs.insert(OriginDIE);
1514
1515 // Track the start label for this inlined function.
1516 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1517 I = InlineInfo.find(InlinedSP.getNode());
1518
1519 if (I == InlineInfo.end()) {
1520 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID, ScopeDIE));
1521 InlinedSPNodes.push_back(InlinedSP.getNode());
1522 } else
1523 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1524
1525 StringPool.insert(InlinedSP.getName());
1526 StringPool.insert(InlinedSP.getLinkageName());
1527 DILocation DL(Scope->getInlinedAt());
1528 AddUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1529 AddUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
1530
1531 return ScopeDIE;
1532}
1533
1534DIE *DwarfDebug::ConstructVariableDIE(DbgVariable *DV,
1535 DbgScope *Scope, CompileUnit *Unit) {
1536 // Get the descriptor.
1537 const DIVariable &VD = DV->getVariable();
1538
1539 // Translate tag to proper Dwarf tag. The result variable is dropped for
1540 // now.
1541 unsigned Tag;
1542 switch (VD.getTag()) {
1543 case dwarf::DW_TAG_return_variable:
1544 return NULL;
1545 case dwarf::DW_TAG_arg_variable:
1546 Tag = dwarf::DW_TAG_formal_parameter;
1547 break;
1548 case dwarf::DW_TAG_auto_variable: // fall thru
1549 default:
1550 Tag = dwarf::DW_TAG_variable;
1551 break;
1552 }
1553
1554 // Define variable debug information entry.
1555 DIE *VariableDie = new DIE(Tag);
1556
1557
1558 DIE *AbsDIE = NULL;
1559 if (DbgVariable *AV = DV->getAbstractVariable())
1560 AbsDIE = AV->getDIE();
1561
1562 if (AbsDIE) {
1563 DIScope DS(Scope->getScopeNode());
1564 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
1565 DIE *&OriginSPDIE = ModuleCU->getDieMapSlotFor(InlinedSP.getNode());
1566 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1567 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1568 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
1569 AddDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1570 dwarf::DW_FORM_ref4, AbsDIE);
1571 }
1572 else {
1573 const char *Name = VD.getName();
1574 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1575 AddSourceLine(VariableDie, &VD);
1576
1577 // Add variable type.
1578 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
1579 // addresses instead.
1580 if (VD.isBlockByrefVariable())
1581 AddType(Unit, VariableDie, GetBlockByrefType(VD.getType(), Name));
1582 else
1583 AddType(Unit, VariableDie, VD.getType());
1584 }
1585
1586 // Add variable address.
1587 if (!Scope->isAbstractScope()) {
1588 MachineLocation Location;
1589 Location.set(RI->getFrameRegister(*MF),
1590 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1591
1592
1593 if (VD.hasComplexAddress())
1594 AddComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1595 else if (VD.isBlockByrefVariable())
1596 AddBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1597 else
1598 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
1599 }
1600 DV->setDIE(VariableDie);
1601 return VariableDie;
1602
1603}
1604DIE *DwarfDebug::ConstructScopeDIE(DbgScope *Scope) {
1605 if (!Scope)
1606 return NULL;
1607 DIScope DS(Scope->getScopeNode());
1608 if (DS.isNull())
1609 return NULL;
1610
1611 DIE *ScopeDIE = NULL;
1612 if (Scope->getInlinedAt())
1613 ScopeDIE = ConstructInlinedScopeDIE(Scope);
1614 else if (DS.isSubprogram()) {
1615 if (Scope->isAbstractScope())
1616 ScopeDIE = ModuleCU->getDieMapSlotFor(DS.getNode());
1617 else
1618 ScopeDIE = UpdateSubprogramScopeDIE(DS.getNode());
1619 }
1620 else {
1621 ScopeDIE = ConstructLexicalScopeDIE(Scope);
1622 if (!ScopeDIE) return NULL;
1623 }
1624
1625 // Add variables to scope.
1626 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1627 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1628 DIE *VariableDIE = ConstructVariableDIE(Variables[i], Scope, ModuleCU);
1629 if (VariableDIE)
1630 ScopeDIE->AddChild(VariableDIE);
1631 }
1632
1633 // Add nested scopes.
1634 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1635 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1636 // Define the Scope debug information entry.
1637 DIE *NestedDIE = ConstructScopeDIE(Scopes[j]);
1638 if (NestedDIE)
1639 ScopeDIE->AddChild(NestedDIE);
1640 }
1641 return ScopeDIE;
1642}
1643
Bill Wendling0310d762009-05-15 09:23:25 +00001644/// ConstructDbgScope - Construct the components of a scope.
Devang Patel53bb5c92009-11-10 23:06:00 +00001645/// FIXME: Remove
Bill Wendling0310d762009-05-15 09:23:25 +00001646void DwarfDebug::ConstructDbgScope(DbgScope *ParentScope,
1647 unsigned ParentStartID,
1648 unsigned ParentEndID,
1649 DIE *ParentDie, CompileUnit *Unit) {
1650 // Add variables to scope.
1651 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
1652 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Bill Wendling995f80a2009-05-20 23:24:48 +00001653 DIE *VariableDie = CreateDbgScopeVariable(Variables[i], Unit);
Bill Wendling0310d762009-05-15 09:23:25 +00001654 if (VariableDie) ParentDie->AddChild(VariableDie);
1655 }
1656
Bill Wendling0310d762009-05-15 09:23:25 +00001657 // Add nested scopes.
1658 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
1659 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1660 // Define the Scope debug information entry.
1661 DbgScope *Scope = Scopes[j];
1662
1663 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1664 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1665
1666 // Ignore empty scopes.
1667 if (StartID == EndID && StartID != 0) continue;
1668
1669 // Do not ignore inlined scopes even if they don't have any variables or
1670 // scopes.
Devang Patel53bb5c92009-11-10 23:06:00 +00001671 if (Scope->getScopes().empty() && Scope->getVariables().empty())
Bill Wendling0310d762009-05-15 09:23:25 +00001672 continue;
1673
1674 if (StartID == ParentStartID && EndID == ParentEndID) {
1675 // Just add stuff to the parent scope.
1676 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1677 } else {
1678 DIE *ScopeDie = new DIE(dwarf::DW_TAG_lexical_block);
1679
1680 // Add the scope bounds.
1681 if (StartID)
1682 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1683 DWLabel("label", StartID));
1684 else
1685 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1686 DWLabel("func_begin", SubprogramCount));
1687
1688 if (EndID)
1689 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1690 DWLabel("label", EndID));
1691 else
1692 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1693 DWLabel("func_end", SubprogramCount));
1694
1695 // Add the scope's contents.
1696 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1697 ParentDie->AddChild(ScopeDie);
1698 }
1699 }
1700}
1701
Devang Patel53bb5c92009-11-10 23:06:00 +00001702/// ConstructCurrentFnDbgScope - Construct the scope for the subprogram.
1703/// FIXME: Remove
1704void DwarfDebug::ConstructCurrentFnDbgScope(DbgScope *RootScope,
Bill Wendling17956162009-05-20 23:28:48 +00001705 bool AbstractScope) {
Bill Wendling0310d762009-05-15 09:23:25 +00001706 // Exit if there is no root scope.
1707 if (!RootScope) return;
1708 DIDescriptor Desc = RootScope->getDesc();
1709 if (Desc.isNull())
1710 return;
1711
1712 // Get the subprogram debug information entry.
Devang Patele4b27562009-08-28 23:24:31 +00001713 DISubprogram SPD(Desc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001714
Bill Wendling0310d762009-05-15 09:23:25 +00001715 // Get the subprogram die.
Devang Patele4b27562009-08-28 23:24:31 +00001716 DIE *SPDie = ModuleCU->getDieMapSlotFor(SPD.getNode());
Devang Patelffd9c3d2009-10-05 23:59:00 +00001717 if (!SPDie) {
1718 ConstructSubprogram(SPD.getNode());
1719 SPDie = ModuleCU->getDieMapSlotFor(SPD.getNode());
1720 }
Bill Wendling0310d762009-05-15 09:23:25 +00001721 assert(SPDie && "Missing subprogram descriptor");
1722
Bill Wendling17956162009-05-20 23:28:48 +00001723 if (!AbstractScope) {
1724 // Add the function bounds.
1725 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1726 DWLabel("func_begin", SubprogramCount));
1727 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1728 DWLabel("func_end", SubprogramCount));
1729 MachineLocation Location(RI->getFrameRegister(*MF));
1730 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
1731 }
Bill Wendling0310d762009-05-15 09:23:25 +00001732
Devang Patel1dbc7712009-06-29 20:45:18 +00001733 ConstructDbgScope(RootScope, 0, 0, SPDie, ModuleCU);
Devang Patelfd07cf52009-10-05 23:40:42 +00001734 // If there are global variables at this scope then add their dies.
1735 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
1736 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1737 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1738 if (!N) continue;
1739 DIGlobalVariable GV(N);
1740 if (GV.getContext().getNode() == RootScope->getDesc().getNode()) {
1741 DIE *ScopedGVDie = CreateGlobalVariableDIE(ModuleCU, GV);
Devang Patel465c3be2009-11-06 01:30:04 +00001742 if (ScopedGVDie)
1743 SPDie->AddChild(ScopedGVDie);
Devang Patelfd07cf52009-10-05 23:40:42 +00001744 }
1745 }
Bill Wendling0310d762009-05-15 09:23:25 +00001746}
1747
Bill Wendling0310d762009-05-15 09:23:25 +00001748/// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
Devang Patel53bb5c92009-11-10 23:06:00 +00001749/// FIXME: Remove
Bill Wendling0310d762009-05-15 09:23:25 +00001750void DwarfDebug::ConstructDefaultDbgScope(MachineFunction *MF) {
Devang Patel1dbc7712009-06-29 20:45:18 +00001751 StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Daniel Dunbar460f6562009-07-26 09:48:23 +00001752 StringMap<DIE*>::iterator GI = Globals.find(MF->getFunction()->getName());
Devang Patel70f44262009-06-29 20:38:13 +00001753 if (GI != Globals.end()) {
1754 DIE *SPDie = GI->second;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001755
Devang Patel70f44262009-06-29 20:38:13 +00001756 // Add the function bounds.
1757 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1758 DWLabel("func_begin", SubprogramCount));
1759 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1760 DWLabel("func_end", SubprogramCount));
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001761
Devang Patel70f44262009-06-29 20:38:13 +00001762 MachineLocation Location(RI->getFrameRegister(*MF));
1763 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001764 }
Bill Wendling0310d762009-05-15 09:23:25 +00001765}
1766
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001767/// GetOrCreateSourceID - Look up the source id with the given directory and
1768/// source file names. If none currently exists, create a new id and insert it
1769/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1770/// maps as well.
Devang Patel5ccdd102009-09-29 18:40:58 +00001771unsigned DwarfDebug::GetOrCreateSourceID(const char *DirName,
1772 const char *FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001773 unsigned DId;
1774 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1775 if (DI != DirectoryIdMap.end()) {
1776 DId = DI->getValue();
1777 } else {
1778 DId = DirectoryNames.size() + 1;
1779 DirectoryIdMap[DirName] = DId;
1780 DirectoryNames.push_back(DirName);
1781 }
1782
1783 unsigned FId;
1784 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1785 if (FI != SourceFileIdMap.end()) {
1786 FId = FI->getValue();
1787 } else {
1788 FId = SourceFileNames.size() + 1;
1789 SourceFileIdMap[FileName] = FId;
1790 SourceFileNames.push_back(FileName);
1791 }
1792
1793 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1794 SourceIdMap.find(std::make_pair(DId, FId));
1795 if (SI != SourceIdMap.end())
1796 return SI->second;
1797
1798 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1799 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1800 SourceIds.push_back(std::make_pair(DId, FId));
1801
1802 return SrcId;
1803}
1804
Devang Patele4b27562009-08-28 23:24:31 +00001805void DwarfDebug::ConstructCompileUnit(MDNode *N) {
1806 DICompileUnit DIUnit(N);
Devang Patel5ccdd102009-09-29 18:40:58 +00001807 const char *FN = DIUnit.getFilename();
1808 const char *Dir = DIUnit.getDirectory();
1809 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001810
1811 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
1812 AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
1813 DWLabel("section_line", 0), DWLabel("section_line", 0),
1814 false);
1815 AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001816 DIUnit.getProducer());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001817 AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
1818 DIUnit.getLanguage());
1819 AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
1820
Devang Patel5ccdd102009-09-29 18:40:58 +00001821 if (Dir)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001822 AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
1823 if (DIUnit.isOptimized())
1824 AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1825
Devang Patel5ccdd102009-09-29 18:40:58 +00001826 if (const char *Flags = DIUnit.getFlags())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001827 AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
1828
1829 unsigned RVer = DIUnit.getRunTimeVersion();
1830 if (RVer)
1831 AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1832 dwarf::DW_FORM_data1, RVer);
1833
1834 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001835 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001836 // Use first compile unit marked as isMain as the compile unit
1837 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001838 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001839 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001840
Devang Patele4b27562009-08-28 23:24:31 +00001841 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001842 CompileUnits.push_back(Unit);
1843}
1844
Devang Patele4b27562009-08-28 23:24:31 +00001845void DwarfDebug::ConstructGlobalVariableDIE(MDNode *N) {
1846 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001847
Devang Patel905cf5e2009-09-04 23:59:07 +00001848 // If debug information is malformed then ignore it.
1849 if (DI_GV.Verify() == false)
1850 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001851
1852 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001853 DIE *&Slot = ModuleCU->getDieMapSlotFor(DI_GV.getNode());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001854 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001855 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001856
Devang Patel1dbc7712009-06-29 20:45:18 +00001857 DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001858
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001859 // Add to map.
1860 Slot = VariableDie;
1861
1862 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001863 ModuleCU->getDie()->AddChild(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001864
1865 // Expose as global. FIXME - need to check external flag.
Devang Patel5ccdd102009-09-29 18:40:58 +00001866 ModuleCU->AddGlobal(DI_GV.getName(), VariableDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001867 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001868}
1869
Devang Patele4b27562009-08-28 23:24:31 +00001870void DwarfDebug::ConstructSubprogram(MDNode *N) {
1871 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001872
1873 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001874 DIE *&Slot = ModuleCU->getDieMapSlotFor(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001875 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001876 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001877
1878 if (!SP.isDefinition())
1879 // This is a method declaration which will be handled while constructing
1880 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001881 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001882
Devang Patel1dbc7712009-06-29 20:45:18 +00001883 DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001884
1885 // Add to map.
1886 Slot = SubprogramDie;
1887
1888 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001889 ModuleCU->getDie()->AddChild(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001890
1891 // Expose as global.
Devang Patel5ccdd102009-09-29 18:40:58 +00001892 ModuleCU->AddGlobal(SP.getName(), SubprogramDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001893 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001894}
1895
Daniel Dunbar00564992009-09-19 20:40:14 +00001896/// BeginModule - Emit all Dwarf sections that should come prior to the
1897/// content. Create global DIEs and emit initial debug info sections.
1898/// This is inovked by the target AsmPrinter.
Devang Patel208622d2009-06-25 22:36:02 +00001899void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) {
1900 this->M = M;
1901
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001902 if (TimePassesIsEnabled)
1903 DebugTimer->startTimer();
1904
Devang Patel78ab9e22009-07-30 18:56:46 +00001905 DebugInfoFinder DbgFinder;
1906 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001907
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001908 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001909 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1910 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001911 ConstructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001912
1913 if (CompileUnits.empty()) {
1914 if (TimePassesIsEnabled)
1915 DebugTimer->stopTimer();
1916
1917 return;
1918 }
1919
Devang Patel70f44262009-06-29 20:38:13 +00001920 // If main compile unit for this module is not seen than randomly
1921 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001922 if (!ModuleCU)
1923 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001924
Devang Patel13e16b62009-06-26 01:49:18 +00001925 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001926 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patelfd07cf52009-10-05 23:40:42 +00001927 E = DbgFinder.global_variable_end(); I != E; ++I) {
1928 DIGlobalVariable GV(*I);
1929 if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
1930 ScopedGVs.push_back(*I);
1931 else
1932 ConstructGlobalVariableDIE(*I);
1933 }
Devang Patel13e16b62009-06-26 01:49:18 +00001934
Devang Patel53bb5c92009-11-10 23:06:00 +00001935 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001936 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1937 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001938 ConstructSubprogram(*I);
1939
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001940 MMI = mmi;
1941 shouldEmit = true;
1942 MMI->setDebugInfoAvailability(true);
1943
1944 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001945 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001946
1947 // Print out .file directives to specify files for .loc directives. These are
1948 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001949 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001950 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1951 // Remember source id starts at 1.
1952 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1953 sys::Path FullPath(getSourceDirectoryName(Id.first));
1954 bool AppendOk =
1955 FullPath.appendComponent(getSourceFileName(Id.second));
1956 assert(AppendOk && "Could not append filename to directory!");
1957 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001958 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001959 Asm->EOL();
1960 }
1961 }
1962
1963 // Emit initial sections
1964 EmitInitial();
1965
1966 if (TimePassesIsEnabled)
1967 DebugTimer->stopTimer();
1968}
1969
1970/// EndModule - Emit all Dwarf sections that should come after the content.
1971///
1972void DwarfDebug::EndModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001973 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001974 return;
1975
1976 if (TimePassesIsEnabled)
1977 DebugTimer->startTimer();
1978
Devang Patel53bb5c92009-11-10 23:06:00 +00001979 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1980 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1981 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1982 DIE *ISP = *AI;
1983 AddUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
1984 }
1985
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001986 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001987 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001988 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001989 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001990 EmitLabel("data_end", 0);
1991
1992 // End text sections.
1993 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001994 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001995 EmitLabel("section_end", i);
1996 }
1997
1998 // Emit common frame information.
1999 EmitCommonDebugFrame();
2000
2001 // Emit function debug frame information
2002 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2003 E = DebugFrames.end(); I != E; ++I)
2004 EmitFunctionDebugFrame(*I);
2005
2006 // Compute DIE offsets and sizes.
2007 SizeAndOffsets();
2008
2009 // Emit all the DIEs into a debug info section
2010 EmitDebugInfo();
2011
2012 // Corresponding abbreviations into a abbrev section.
2013 EmitAbbreviations();
2014
2015 // Emit source line correspondence into a debug line section.
2016 EmitDebugLines();
2017
2018 // Emit info into a debug pubnames section.
2019 EmitDebugPubNames();
2020
2021 // Emit info into a debug str section.
2022 EmitDebugStr();
2023
2024 // Emit info into a debug loc section.
2025 EmitDebugLoc();
2026
2027 // Emit info into a debug aranges section.
2028 EmitDebugARanges();
2029
2030 // Emit info into a debug ranges section.
2031 EmitDebugRanges();
2032
2033 // Emit info into a debug macinfo section.
2034 EmitDebugMacInfo();
2035
2036 // Emit inline info.
2037 EmitDebugInlineInfo();
2038
2039 if (TimePassesIsEnabled)
2040 DebugTimer->stopTimer();
2041}
2042
Devang Patel53bb5c92009-11-10 23:06:00 +00002043/// findAbstractVariable - Find abstract variable, if any, associated with Var.
2044DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
2045 DILocation &ScopeLoc) {
2046
2047 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
2048 if (AbsDbgVariable)
2049 return AbsDbgVariable;
2050
2051 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
2052 if (!Scope)
2053 return NULL;
2054
2055 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
2056 Scope->AddVariable(AbsDbgVariable);
2057 AbstractVariables[Var.getNode()] = AbsDbgVariable;
2058 return AbsDbgVariable;
2059}
2060
Devang Patele717faa2009-10-06 01:26:37 +00002061/// CollectVariableInfo - Populate DbgScope entries with variables' info.
Devang Patelac1ceb32009-10-09 22:42:28 +00002062void DwarfDebug::CollectVariableInfo() {
2063 if (!MMI) return;
Devang Patel53bb5c92009-11-10 23:06:00 +00002064
Devang Patele717faa2009-10-06 01:26:37 +00002065 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2066 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2067 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac1ceb32009-10-09 22:42:28 +00002068 MetadataBase *MB = VI->first;
2069 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel53bb5c92009-11-10 23:06:00 +00002070 if (!Var) continue;
Devang Pateleda31212009-10-08 18:48:03 +00002071 DIVariable DV (Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00002072 std::pair< unsigned, MDNode *> VP = VI->second;
2073 DILocation ScopeLoc(VP.second);
2074
2075 DbgScope *Scope =
2076 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
2077 if (!Scope)
2078 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patel149aa7c2009-10-16 18:18:03 +00002079 assert (Scope && "Unable to find variable scope!");
Devang Patel53bb5c92009-11-10 23:06:00 +00002080
2081 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
2082 Scope->AddVariable(RegVar);
2083 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, ScopeLoc))
2084 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patele717faa2009-10-06 01:26:37 +00002085 }
2086}
2087
Devang Patel53bb5c92009-11-10 23:06:00 +00002088/// BeginScope - Process beginning of a scope starting at Label.
2089void DwarfDebug::BeginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel0d20ac82009-10-06 01:50:42 +00002090 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
2091 if (I == DbgScopeBeginMap.end())
2092 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00002093 ScopeVector &SD = DbgScopeBeginMap[MI];
2094 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Devang Patel0d20ac82009-10-06 01:50:42 +00002095 SDI != SDE; ++SDI)
2096 (*SDI)->setStartLabelID(Label);
2097}
2098
Devang Patel53bb5c92009-11-10 23:06:00 +00002099/// EndScope - Process end of a scope.
2100void DwarfDebug::EndScope(const MachineInstr *MI) {
Devang Patel0d20ac82009-10-06 01:50:42 +00002101 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00002102 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00002103 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00002104
2105 unsigned Label = MMI->NextLabelID();
2106 Asm->printLabel(Label);
2107
Devang Patel0d20ac82009-10-06 01:50:42 +00002108 SmallVector<DbgScope *, 2> &SD = I->second;
2109 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
2110 SDI != SDE; ++SDI)
2111 (*SDI)->setEndLabelID(Label);
Devang Patel53bb5c92009-11-10 23:06:00 +00002112 return;
2113}
2114
2115/// createDbgScope - Create DbgScope for the scope.
2116void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
2117
2118 if (!InlinedAt) {
2119 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2120 if (WScope)
2121 return;
2122 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2123 DbgScopeMap.insert(std::make_pair(Scope, WScope));
2124 return;
2125 }
2126
2127 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2128 if (WScope)
2129 return;
2130
2131 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2132 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2133 DILocation DL(InlinedAt);
2134 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel0d20ac82009-10-06 01:50:42 +00002135}
2136
Devang Patelaf9e8472009-10-01 20:31:14 +00002137/// ExtractScopeInformation - Scan machine instructions in this function
2138/// and collect DbgScopes. Return true, if atleast one scope was found.
2139bool DwarfDebug::ExtractScopeInformation(MachineFunction *MF) {
2140 // If scope information was extracted using .dbg intrinsics then there is not
2141 // any need to extract these information by scanning each instruction.
2142 if (!DbgScopeMap.empty())
2143 return false;
2144
Devang Patel53bb5c92009-11-10 23:06:00 +00002145 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelaf9e8472009-10-01 20:31:14 +00002146 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2147 I != E; ++I) {
2148 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2149 II != IE; ++II) {
2150 const MachineInstr *MInsn = II;
2151 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00002152 if (DL.isUnknown()) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002153 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel53bb5c92009-11-10 23:06:00 +00002154 if (!DLT.Scope) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002155 // There is no need to create another DIE for compile unit. For all
2156 // other scopes, create one DbgScope now. This will be translated
2157 // into a scope DIE at the end.
Devang Patel53bb5c92009-11-10 23:06:00 +00002158 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2159 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
2160 }
2161 }
2162
2163
2164 // Build scope hierarchy using working set of scopes.
2165 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2166 I != E; ++I) {
2167 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2168 II != IE; ++II) {
2169 const MachineInstr *MInsn = II;
2170 DebugLoc DL = MInsn->getDebugLoc();
2171 if (DL.isUnknown()) continue;
2172 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
2173 if (!DLT.Scope) continue;
2174 // There is no need to create another DIE for compile unit. For all
2175 // other scopes, create one DbgScope now. This will be translated
2176 // into a scope DIE at the end.
2177 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2178 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
2179 Scope->setLastInsn(MInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +00002180 }
2181 }
2182
2183 // If a scope's last instruction is not set then use its child scope's
2184 // last instruction as this scope's last instrunction.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002185 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002186 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002187 if (DI->second->isAbstractScope())
2188 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002189 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
2190 DI->second->FixInstructionMarkers();
2191 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2192 }
2193
2194 // Each scope has first instruction and last instruction to mark beginning
2195 // and end of a scope respectively. Create an inverse map that list scopes
2196 // starts (and ends) with an instruction. One instruction may start (or end)
2197 // multiple scopes.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002198 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002199 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2200 DbgScope *S = DI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002201 if (S->isAbstractScope())
2202 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002203 const MachineInstr *MI = S->getFirstInsn();
2204 assert (MI && "DbgScope does not have first instruction!");
2205
2206 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2207 if (IDI != DbgScopeBeginMap.end())
2208 IDI->second.push_back(S);
2209 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002210 DbgScopeBeginMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002211
2212 MI = S->getLastInsn();
2213 assert (MI && "DbgScope does not have last instruction!");
2214 IDI = DbgScopeEndMap.find(MI);
2215 if (IDI != DbgScopeEndMap.end())
2216 IDI->second.push_back(S);
2217 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002218 DbgScopeEndMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002219 }
2220
2221 return !DbgScopeMap.empty();
2222}
2223
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002224/// BeginFunction - Gather pre-function debug information. Assumes being
2225/// emitted immediately after the function entry point.
2226void DwarfDebug::BeginFunction(MachineFunction *MF) {
2227 this->MF = MF;
2228
2229 if (!ShouldEmitDwarfDebug()) return;
2230
2231 if (TimePassesIsEnabled)
2232 DebugTimer->startTimer();
2233
Devang Patel60b35bd2009-10-06 18:37:31 +00002234#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
2235 if (!ExtractScopeInformation(MF))
2236 return;
Devang Patelac1ceb32009-10-09 22:42:28 +00002237 CollectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002238#endif
2239
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002240 // Begin accumulating function debug information.
2241 MMI->BeginFunction(MF);
2242
2243 // Assumes in correct section after the entry point.
2244 EmitLabel("func_begin", ++SubprogramCount);
2245
2246 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2247 // function.
Devang Pateleda31212009-10-08 18:48:03 +00002248#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
Devang Patelac1ceb32009-10-09 22:42:28 +00002249 DebugLoc FDL = MF->getDefaultDebugLoc();
2250 if (!FDL.isUnknown()) {
2251 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2252 unsigned LabelID = 0;
Devang Patel1619dc32009-10-13 23:28:53 +00002253 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002254 if (!SP.isNull())
Devang Patel1619dc32009-10-13 23:28:53 +00002255 LabelID = RecordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002256 else
Devang Patel1619dc32009-10-13 23:28:53 +00002257 LabelID = RecordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002258 Asm->printLabel(LabelID);
2259 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002260 }
Devang Patelac1ceb32009-10-09 22:42:28 +00002261#else
2262 DebugLoc FDL = MF->getDefaultDebugLoc();
2263 if (!FDL.isUnknown()) {
2264 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
Devang Patel1619dc32009-10-13 23:28:53 +00002265 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002266 Asm->printLabel(LabelID);
2267 O << '\n';
2268 }
2269#endif
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002270 if (TimePassesIsEnabled)
2271 DebugTimer->stopTimer();
2272}
2273
2274/// EndFunction - Gather and emit post-function debug information.
2275///
2276void DwarfDebug::EndFunction(MachineFunction *MF) {
2277 if (!ShouldEmitDwarfDebug()) return;
2278
2279 if (TimePassesIsEnabled)
2280 DebugTimer->startTimer();
2281
Devang Patelac1ceb32009-10-09 22:42:28 +00002282#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
2283 if (DbgScopeMap.empty())
2284 return;
2285#endif
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002286 // Define end label for subprogram.
2287 EmitLabel("func_end", SubprogramCount);
2288
2289 // Get function line info.
2290 if (!Lines.empty()) {
2291 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00002292 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002293 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2294 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2295 // Append the function info to section info.
2296 SectionLineInfos.insert(SectionLineInfos.end(),
2297 Lines.begin(), Lines.end());
2298 }
2299
Devang Patel53bb5c92009-11-10 23:06:00 +00002300#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002301 // Construct scopes for subprogram.
Devang Patel53bb5c92009-11-10 23:06:00 +00002302 if (CurrentFnDbgScope)
2303 ConstructCurrentFnDbgScope(CurrentFnDbgScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002304 else
2305 // FIXME: This is wrong. We are essentially getting past a problem with
2306 // debug information not being able to handle unreachable blocks that have
2307 // debug information in them. In particular, those unreachable blocks that
2308 // have "region end" info in them. That situation results in the "root
2309 // scope" not being created. If that's the case, then emit a "default"
2310 // scope, i.e., one that encompasses the whole function. This isn't
2311 // desirable. And a better way of handling this (and all of the debugging
2312 // information) needs to be explored.
2313 ConstructDefaultDbgScope(MF);
Devang Patel53bb5c92009-11-10 23:06:00 +00002314#else
2315 // Construct abstract scopes.
2316 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2317 AE = AbstractScopesList.end(); AI != AE; ++AI)
2318 ConstructScopeDIE(*AI);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002319
Devang Patel53bb5c92009-11-10 23:06:00 +00002320 ConstructScopeDIE(CurrentFnDbgScope);
2321#endif
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002322 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2323 MMI->getFrameMoves()));
2324
2325 // Clear debug info
Devang Patel53bb5c92009-11-10 23:06:00 +00002326 if (CurrentFnDbgScope) {
2327 CurrentFnDbgScope = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002328 DbgScopeMap.clear();
Devang Patelaf9e8472009-10-01 20:31:14 +00002329 DbgScopeBeginMap.clear();
2330 DbgScopeEndMap.clear();
Devang Patel53bb5c92009-11-10 23:06:00 +00002331 ConcreteScopes.clear();
2332 AbstractScopesList.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002333 }
2334
2335 Lines.clear();
2336
2337 if (TimePassesIsEnabled)
2338 DebugTimer->stopTimer();
2339}
2340
2341/// RecordSourceLine - Records location information and associates it with a
2342/// label. Returns a unique label ID used to generate a label and provide
2343/// correspondence to the source line list.
Devang Patel3d910832009-09-30 22:51:28 +00002344unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002345 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002346 if (!MMI)
2347 return 0;
2348
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002349 if (TimePassesIsEnabled)
2350 DebugTimer->startTimer();
2351
Devang Patelf84548d2009-10-05 18:03:19 +00002352 const char *Dir = NULL;
2353 const char *Fn = NULL;
2354
2355 DIDescriptor Scope(S);
2356 if (Scope.isCompileUnit()) {
2357 DICompileUnit CU(S);
2358 Dir = CU.getDirectory();
2359 Fn = CU.getFilename();
2360 } else if (Scope.isSubprogram()) {
2361 DISubprogram SP(S);
2362 Dir = SP.getDirectory();
2363 Fn = SP.getFilename();
2364 } else if (Scope.isLexicalBlock()) {
2365 DILexicalBlock DB(S);
2366 Dir = DB.getDirectory();
2367 Fn = DB.getFilename();
2368 } else
2369 assert (0 && "Unexpected scope info");
2370
2371 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002372 unsigned ID = MMI->NextLabelID();
2373 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2374
2375 if (TimePassesIsEnabled)
2376 DebugTimer->stopTimer();
2377
2378 return ID;
2379}
2380
2381/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2382/// timed. Look up the source id with the given directory and source file
2383/// names. If none currently exists, create a new id and insert it in the
2384/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2385/// well.
2386unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2387 const std::string &FileName) {
2388 if (TimePassesIsEnabled)
2389 DebugTimer->startTimer();
2390
Devang Patel5ccdd102009-09-29 18:40:58 +00002391 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002392
2393 if (TimePassesIsEnabled)
2394 DebugTimer->stopTimer();
2395
2396 return SrcId;
2397}
2398
2399/// RecordRegionStart - Indicate the start of a region.
Devang Patele4b27562009-08-28 23:24:31 +00002400unsigned DwarfDebug::RecordRegionStart(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002401 if (TimePassesIsEnabled)
2402 DebugTimer->startTimer();
2403
Devang Patele4b27562009-08-28 23:24:31 +00002404 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002405 unsigned ID = MMI->NextLabelID();
2406 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002407
2408 if (TimePassesIsEnabled)
2409 DebugTimer->stopTimer();
2410
2411 return ID;
2412}
2413
2414/// RecordRegionEnd - Indicate the end of a region.
Devang Patele4b27562009-08-28 23:24:31 +00002415unsigned DwarfDebug::RecordRegionEnd(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002416 if (TimePassesIsEnabled)
2417 DebugTimer->startTimer();
2418
Devang Patele4b27562009-08-28 23:24:31 +00002419 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002420 unsigned ID = MMI->NextLabelID();
2421 Scope->setEndLabelID(ID);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002422
2423 if (TimePassesIsEnabled)
2424 DebugTimer->stopTimer();
2425
2426 return ID;
2427}
2428
2429/// RecordVariable - Indicate the declaration of a local variable.
Devang Patele4b27562009-08-28 23:24:31 +00002430void DwarfDebug::RecordVariable(MDNode *N, unsigned FrameIndex) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002431 if (TimePassesIsEnabled)
2432 DebugTimer->startTimer();
2433
Devang Patele4b27562009-08-28 23:24:31 +00002434 DIDescriptor Desc(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002435 DbgScope *Scope = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002436
Devang Patele4b27562009-08-28 23:24:31 +00002437 if (Desc.getTag() == dwarf::DW_TAG_variable)
2438 Scope = getOrCreateScope(DIGlobalVariable(N).getContext().getNode());
2439 else {
Devang Patele4b27562009-08-28 23:24:31 +00002440 MDNode *Context = DIVariable(N).getContext().getNode();
Devang Patel53bb5c92009-11-10 23:06:00 +00002441 Scope = getOrCreateScope(Context);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002442 }
2443
2444 assert(Scope && "Unable to find the variable's scope");
Devang Patel53bb5c92009-11-10 23:06:00 +00002445 DbgVariable *DV = new DbgVariable(DIVariable(N), FrameIndex);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002446 Scope->AddVariable(DV);
2447
2448 if (TimePassesIsEnabled)
2449 DebugTimer->stopTimer();
2450}
2451
Bill Wendling829e67b2009-05-20 23:22:40 +00002452//===----------------------------------------------------------------------===//
2453// Emit Methods
2454//===----------------------------------------------------------------------===//
2455
Bill Wendling94d04b82009-05-20 23:21:38 +00002456/// SizeAndOffsetDie - Compute the size and offset of a DIE.
2457///
2458unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2459 // Get the children.
2460 const std::vector<DIE *> &Children = Die->getChildren();
2461
2462 // If not last sibling and has children then add sibling offset attribute.
2463 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2464
2465 // Record the abbreviation.
2466 AssignAbbrevNumber(Die->getAbbrev());
2467
2468 // Get the abbreviation for this DIE.
2469 unsigned AbbrevNumber = Die->getAbbrevNumber();
2470 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2471
2472 // Set DIE offset
2473 Die->setOffset(Offset);
2474
2475 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002476 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002477
2478 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2479 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2480
2481 // Size the DIE attribute values.
2482 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2483 // Size attribute value.
2484 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2485
2486 // Size the DIE children if any.
2487 if (!Children.empty()) {
2488 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2489 "Children flag not set");
2490
2491 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2492 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2493
2494 // End of children marker.
2495 Offset += sizeof(int8_t);
2496 }
2497
2498 Die->setSize(Offset - Die->getOffset());
2499 return Offset;
2500}
2501
2502/// SizeAndOffsets - Compute the size and offset of all the DIEs.
2503///
2504void DwarfDebug::SizeAndOffsets() {
2505 // Compute size of compile unit header.
2506 static unsigned Offset =
2507 sizeof(int32_t) + // Length of Compilation Unit Info
2508 sizeof(int16_t) + // DWARF version number
2509 sizeof(int32_t) + // Offset Into Abbrev. Section
2510 sizeof(int8_t); // Pointer Size (in bytes)
2511
Devang Patel1dbc7712009-06-29 20:45:18 +00002512 SizeAndOffsetDie(ModuleCU->getDie(), Offset, true);
2513 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002514}
2515
2516/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2517/// tools to recognize the object file contains Dwarf information.
2518void DwarfDebug::EmitInitial() {
2519 // Check to see if we already emitted intial headers.
2520 if (didInitial) return;
2521 didInitial = true;
2522
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002523 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002524
Bill Wendling94d04b82009-05-20 23:21:38 +00002525 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002526 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002527 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002528 EmitLabel("section_debug_frame", 0);
2529 }
2530
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002531 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002532 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002533 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002534 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002535 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002536 EmitLabel("section_aranges", 0);
2537
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002538 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2539 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002540 EmitLabel("section_macinfo", 0);
2541 }
2542
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002543 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002544 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002545 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002546 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002547 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002548 EmitLabel("section_pubnames", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002549 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002550 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002551 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002552 EmitLabel("section_ranges", 0);
2553
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002554 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002555 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002556 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002557 EmitLabel("data_begin", 0);
2558}
2559
2560/// EmitDIE - Recusively Emits a debug information entry.
2561///
2562void DwarfDebug::EmitDIE(DIE *Die) {
2563 // Get the abbreviation for this DIE.
2564 unsigned AbbrevNumber = Die->getAbbrevNumber();
2565 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2566
2567 Asm->EOL();
2568
2569 // Emit the code (index) for the abbreviation.
2570 Asm->EmitULEB128Bytes(AbbrevNumber);
2571
2572 if (Asm->isVerbose())
2573 Asm->EOL(std::string("Abbrev [" +
2574 utostr(AbbrevNumber) +
2575 "] 0x" + utohexstr(Die->getOffset()) +
2576 ":0x" + utohexstr(Die->getSize()) + " " +
2577 dwarf::TagString(Abbrev->getTag())));
2578 else
2579 Asm->EOL();
2580
2581 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2582 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2583
2584 // Emit the DIE attribute values.
2585 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2586 unsigned Attr = AbbrevData[i].getAttribute();
2587 unsigned Form = AbbrevData[i].getForm();
2588 assert(Form && "Too many attributes for DIE (check abbreviation)");
2589
2590 switch (Attr) {
2591 case dwarf::DW_AT_sibling:
2592 Asm->EmitInt32(Die->SiblingOffset());
2593 break;
2594 case dwarf::DW_AT_abstract_origin: {
2595 DIEEntry *E = cast<DIEEntry>(Values[i]);
2596 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002597 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002598 Asm->EmitInt32(Addr);
2599 break;
2600 }
2601 default:
2602 // Emit an attribute using the defined form.
2603 Values[i]->EmitValue(this, Form);
2604 break;
2605 }
2606
2607 Asm->EOL(dwarf::AttributeString(Attr));
2608 }
2609
2610 // Emit the DIE children if any.
2611 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2612 const std::vector<DIE *> &Children = Die->getChildren();
2613
2614 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2615 EmitDIE(Children[j]);
2616
2617 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2618 }
2619}
2620
2621/// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
2622///
2623void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) {
2624 DIE *Die = Unit->getDie();
2625
2626 // Emit the compile units header.
2627 EmitLabel("info_begin", Unit->getID());
2628
2629 // Emit size of content not including length itself
2630 unsigned ContentSize = Die->getSize() +
2631 sizeof(int16_t) + // DWARF version number
2632 sizeof(int32_t) + // Offset Into Abbrev. Section
2633 sizeof(int8_t) + // Pointer Size (in bytes)
2634 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2635
2636 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2637 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2638 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2639 Asm->EOL("Offset Into Abbrev. Section");
2640 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2641
2642 EmitDIE(Die);
2643 // FIXME - extra padding for gdb bug.
2644 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2645 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2646 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2647 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2648 EmitLabel("info_end", Unit->getID());
2649
2650 Asm->EOL();
2651}
2652
2653void DwarfDebug::EmitDebugInfo() {
2654 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002655 Asm->OutStreamer.SwitchSection(
2656 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002657
Devang Patel1dbc7712009-06-29 20:45:18 +00002658 EmitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002659}
2660
2661/// EmitAbbreviations - Emit the abbreviation section.
2662///
2663void DwarfDebug::EmitAbbreviations() const {
2664 // Check to see if it is worth the effort.
2665 if (!Abbreviations.empty()) {
2666 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002667 Asm->OutStreamer.SwitchSection(
2668 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002669
2670 EmitLabel("abbrev_begin", 0);
2671
2672 // For each abbrevation.
2673 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2674 // Get abbreviation data
2675 const DIEAbbrev *Abbrev = Abbreviations[i];
2676
2677 // Emit the abbrevations code (base 1 index.)
2678 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2679 Asm->EOL("Abbreviation Code");
2680
2681 // Emit the abbreviations data.
2682 Abbrev->Emit(Asm);
2683
2684 Asm->EOL();
2685 }
2686
2687 // Mark end of abbreviations.
2688 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2689
2690 EmitLabel("abbrev_end", 0);
2691 Asm->EOL();
2692 }
2693}
2694
2695/// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2696/// the line matrix.
2697///
2698void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) {
2699 // Define last address of section.
2700 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2701 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2702 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2703 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2704
2705 // Mark end of matrix.
2706 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2707 Asm->EmitULEB128Bytes(1); Asm->EOL();
2708 Asm->EmitInt8(1); Asm->EOL();
2709}
2710
2711/// EmitDebugLines - Emit source line information.
2712///
2713void DwarfDebug::EmitDebugLines() {
2714 // If the target is using .loc/.file, the assembler will be emitting the
2715 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002716 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002717 return;
2718
2719 // Minimum line delta, thus ranging from -10..(255-10).
2720 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2721 // Maximum line delta, thus ranging from -10..(255-10).
2722 const int MaxLineDelta = 255 + MinLineDelta;
2723
2724 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002725 Asm->OutStreamer.SwitchSection(
2726 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002727
2728 // Construct the section header.
2729 EmitDifference("line_end", 0, "line_begin", 0, true);
2730 Asm->EOL("Length of Source Line Info");
2731 EmitLabel("line_begin", 0);
2732
2733 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2734
2735 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2736 Asm->EOL("Prolog Length");
2737 EmitLabel("line_prolog_begin", 0);
2738
2739 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2740
2741 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2742
2743 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2744
2745 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2746
2747 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2748
2749 // Line number standard opcode encodings argument count
2750 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2751 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2752 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2753 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2754 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2755 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2756 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2757 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2758 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2759
2760 // Emit directories.
2761 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2762 Asm->EmitString(getSourceDirectoryName(DI));
2763 Asm->EOL("Directory");
2764 }
2765
2766 Asm->EmitInt8(0); Asm->EOL("End of directories");
2767
2768 // Emit files.
2769 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2770 // Remember source id starts at 1.
2771 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2772 Asm->EmitString(getSourceFileName(Id.second));
2773 Asm->EOL("Source");
2774 Asm->EmitULEB128Bytes(Id.first);
2775 Asm->EOL("Directory #");
2776 Asm->EmitULEB128Bytes(0);
2777 Asm->EOL("Mod date");
2778 Asm->EmitULEB128Bytes(0);
2779 Asm->EOL("File size");
2780 }
2781
2782 Asm->EmitInt8(0); Asm->EOL("End of files");
2783
2784 EmitLabel("line_prolog_end", 0);
2785
2786 // A sequence for each text section.
2787 unsigned SecSrcLinesSize = SectionSourceLines.size();
2788
2789 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2790 // Isolate current sections line info.
2791 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2792
Chris Lattner93b6db32009-08-08 23:39:42 +00002793 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002794 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002795 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002796 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002797 }*/
2798 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002799
2800 // Dwarf assumes we start with first line of first source file.
2801 unsigned Source = 1;
2802 unsigned Line = 1;
2803
2804 // Construct rows of the address, source, line, column matrix.
2805 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2806 const SrcLineInfo &LineInfo = LineInfos[i];
2807 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2808 if (!LabelID) continue;
2809
Caroline Ticec6f9d622009-09-11 18:25:54 +00002810 if (LineInfo.getLine() == 0) continue;
2811
Bill Wendling94d04b82009-05-20 23:21:38 +00002812 if (!Asm->isVerbose())
2813 Asm->EOL();
2814 else {
2815 std::pair<unsigned, unsigned> SourceID =
2816 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002817 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002818 << getSourceDirectoryName(SourceID.first) << ' '
2819 << getSourceFileName(SourceID.second)
2820 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2821 }
2822
2823 // Define the line address.
2824 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2825 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2826 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2827 EmitReference("label", LabelID); Asm->EOL("Location label");
2828
2829 // If change of source, then switch to the new source.
2830 if (Source != LineInfo.getSourceID()) {
2831 Source = LineInfo.getSourceID();
2832 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2833 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2834 }
2835
2836 // If change of line.
2837 if (Line != LineInfo.getLine()) {
2838 // Determine offset.
2839 int Offset = LineInfo.getLine() - Line;
2840 int Delta = Offset - MinLineDelta;
2841
2842 // Update line.
2843 Line = LineInfo.getLine();
2844
2845 // If delta is small enough and in range...
2846 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2847 // ... then use fast opcode.
2848 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2849 } else {
2850 // ... otherwise use long hand.
2851 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2852 Asm->EOL("DW_LNS_advance_line");
2853 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2854 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2855 }
2856 } else {
2857 // Copy the previous row (different address or source)
2858 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2859 }
2860 }
2861
2862 EmitEndOfLineMatrix(j + 1);
2863 }
2864
2865 if (SecSrcLinesSize == 0)
2866 // Because we're emitting a debug_line section, we still need a line
2867 // table. The linker and friends expect it to exist. If there's nothing to
2868 // put into it, emit an empty table.
2869 EmitEndOfLineMatrix(1);
2870
2871 EmitLabel("line_end", 0);
2872 Asm->EOL();
2873}
2874
2875/// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2876///
2877void DwarfDebug::EmitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002878 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002879 return;
2880
2881 int stackGrowth =
2882 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2883 TargetFrameInfo::StackGrowsUp ?
2884 TD->getPointerSize() : -TD->getPointerSize();
2885
2886 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002887 Asm->OutStreamer.SwitchSection(
2888 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002889
2890 EmitLabel("debug_frame_common", 0);
2891 EmitDifference("debug_frame_common_end", 0,
2892 "debug_frame_common_begin", 0, true);
2893 Asm->EOL("Length of Common Information Entry");
2894
2895 EmitLabel("debug_frame_common_begin", 0);
2896 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2897 Asm->EOL("CIE Identifier Tag");
2898 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2899 Asm->EOL("CIE Version");
2900 Asm->EmitString("");
2901 Asm->EOL("CIE Augmentation");
2902 Asm->EmitULEB128Bytes(1);
2903 Asm->EOL("CIE Code Alignment Factor");
2904 Asm->EmitSLEB128Bytes(stackGrowth);
2905 Asm->EOL("CIE Data Alignment Factor");
2906 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2907 Asm->EOL("CIE RA Column");
2908
2909 std::vector<MachineMove> Moves;
2910 RI->getInitialFrameState(Moves);
2911
2912 EmitFrameMoves(NULL, 0, Moves, false);
2913
2914 Asm->EmitAlignment(2, 0, 0, false);
2915 EmitLabel("debug_frame_common_end", 0);
2916
2917 Asm->EOL();
2918}
2919
2920/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2921/// section.
2922void
2923DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002924 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002925 return;
2926
2927 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002928 Asm->OutStreamer.SwitchSection(
2929 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002930
2931 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2932 "debug_frame_begin", DebugFrameInfo.Number, true);
2933 Asm->EOL("Length of Frame Information Entry");
2934
2935 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2936
2937 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2938 0, 0, true, false);
2939 Asm->EOL("FDE CIE offset");
2940
2941 EmitReference("func_begin", DebugFrameInfo.Number);
2942 Asm->EOL("FDE initial location");
2943 EmitDifference("func_end", DebugFrameInfo.Number,
2944 "func_begin", DebugFrameInfo.Number);
2945 Asm->EOL("FDE address range");
2946
2947 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2948 false);
2949
2950 Asm->EmitAlignment(2, 0, 0, false);
2951 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2952
2953 Asm->EOL();
2954}
2955
2956void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2957 EmitDifference("pubnames_end", Unit->getID(),
2958 "pubnames_begin", Unit->getID(), true);
2959 Asm->EOL("Length of Public Names Info");
2960
2961 EmitLabel("pubnames_begin", Unit->getID());
2962
2963 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2964
2965 EmitSectionOffset("info_begin", "section_info",
2966 Unit->getID(), 0, true, false);
2967 Asm->EOL("Offset of Compilation Unit Info");
2968
2969 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2970 true);
2971 Asm->EOL("Compilation Unit Length");
2972
2973 StringMap<DIE*> &Globals = Unit->getGlobals();
2974 for (StringMap<DIE*>::const_iterator
2975 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2976 const char *Name = GI->getKeyData();
2977 DIE * Entity = GI->second;
2978
2979 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2980 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2981 }
2982
2983 Asm->EmitInt32(0); Asm->EOL("End Mark");
2984 EmitLabel("pubnames_end", Unit->getID());
2985
2986 Asm->EOL();
2987}
2988
2989/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2990///
2991void DwarfDebug::EmitDebugPubNames() {
2992 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002993 Asm->OutStreamer.SwitchSection(
2994 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002995
Devang Patel1dbc7712009-06-29 20:45:18 +00002996 EmitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002997}
2998
2999/// EmitDebugStr - Emit visible names into a debug str section.
3000///
3001void DwarfDebug::EmitDebugStr() {
3002 // Check to see if it is worth the effort.
3003 if (!StringPool.empty()) {
3004 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003005 Asm->OutStreamer.SwitchSection(
3006 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003007
3008 // For each of strings in the string pool.
3009 for (unsigned StringID = 1, N = StringPool.size();
3010 StringID <= N; ++StringID) {
3011 // Emit a label for reference from debug information entries.
3012 EmitLabel("string", StringID);
3013
3014 // Emit the string itself.
3015 const std::string &String = StringPool[StringID];
3016 Asm->EmitString(String); Asm->EOL();
3017 }
3018
3019 Asm->EOL();
3020 }
3021}
3022
3023/// EmitDebugLoc - Emit visible names into a debug loc section.
3024///
3025void DwarfDebug::EmitDebugLoc() {
3026 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003027 Asm->OutStreamer.SwitchSection(
3028 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003029 Asm->EOL();
3030}
3031
3032/// EmitDebugARanges - Emit visible names into a debug aranges section.
3033///
3034void DwarfDebug::EmitDebugARanges() {
3035 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003036 Asm->OutStreamer.SwitchSection(
3037 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003038
3039 // FIXME - Mock up
3040#if 0
3041 CompileUnit *Unit = GetBaseCompileUnit();
3042
3043 // Don't include size of length
3044 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
3045
3046 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
3047
3048 EmitReference("info_begin", Unit->getID());
3049 Asm->EOL("Offset of Compilation Unit Info");
3050
3051 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
3052
3053 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
3054
3055 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
3056 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
3057
3058 // Range 1
3059 EmitReference("text_begin", 0); Asm->EOL("Address");
3060 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
3061
3062 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
3063 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
3064#endif
3065
3066 Asm->EOL();
3067}
3068
3069/// EmitDebugRanges - Emit visible names into a debug ranges section.
3070///
3071void DwarfDebug::EmitDebugRanges() {
3072 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003073 Asm->OutStreamer.SwitchSection(
3074 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003075 Asm->EOL();
3076}
3077
3078/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
3079///
3080void DwarfDebug::EmitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003081 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003082 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003083 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003084 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003085 Asm->EOL();
3086 }
3087}
3088
3089/// EmitDebugInlineInfo - Emit inline info using following format.
3090/// Section Header:
3091/// 1. length of section
3092/// 2. Dwarf version number
3093/// 3. address size.
3094///
3095/// Entries (one "entry" for each function that was inlined):
3096///
3097/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3098/// otherwise offset into __debug_str for regular function name.
3099/// 2. offset into __debug_str section for regular function name.
3100/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3101/// instances for the function.
3102///
3103/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3104/// inlined instance; the die_offset points to the inlined_subroutine die in the
3105/// __debug_info section, and the low_pc is the starting address for the
3106/// inlining instance.
3107void DwarfDebug::EmitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00003108 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003109 return;
3110
Devang Patel1dbc7712009-06-29 20:45:18 +00003111 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003112 return;
3113
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003114 Asm->OutStreamer.SwitchSection(
3115 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003116 Asm->EOL();
3117 EmitDifference("debug_inlined_end", 1,
3118 "debug_inlined_begin", 1, true);
3119 Asm->EOL("Length of Debug Inlined Information Entry");
3120
3121 EmitLabel("debug_inlined_begin", 1);
3122
3123 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
3124 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
3125
Devang Patel53bb5c92009-11-10 23:06:00 +00003126 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
3127 E = InlinedSPNodes.end(); I != E; ++I) {
3128
3129// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
3130 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
3131 MDNode *Node = *I;
3132 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II = InlineInfo.find(Node);
3133 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003134 DISubprogram SP(Node);
Devang Patel5ccdd102009-09-29 18:40:58 +00003135 const char *LName = SP.getLinkageName();
3136 const char *Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003137
Devang Patel5ccdd102009-09-29 18:40:58 +00003138 if (!LName)
Devang Patel53cb17d2009-07-16 01:01:22 +00003139 Asm->EmitString(Name);
3140 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003141 // Skip special LLVM prefix that is used to inform the asm printer to not
3142 // emit usual symbol prefix before the symbol name. This happens for
3143 // Objective-C symbol names and symbol whose name is replaced using GCC's
3144 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00003145 if (LName[0] == 1)
3146 LName = &LName[1];
Devang Patel53bb5c92009-11-10 23:06:00 +00003147// Asm->EmitString(LName);
3148 EmitSectionOffset("string", "section_str",
3149 StringPool.idFor(LName), false, true);
3150
Devang Patel53cb17d2009-07-16 01:01:22 +00003151 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003152 Asm->EOL("MIPS linkage name");
Devang Patel53bb5c92009-11-10 23:06:00 +00003153// Asm->EmitString(Name);
3154 EmitSectionOffset("string", "section_str",
3155 StringPool.idFor(Name), false, true);
3156 Asm->EOL("Function name");
Bill Wendling94d04b82009-05-20 23:21:38 +00003157 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
3158
Devang Patel53bb5c92009-11-10 23:06:00 +00003159 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003160 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00003161 DIE *SP = LI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00003162 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
3163
3164 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00003165 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00003166 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00003167 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00003168
Devang Patel53bb5c92009-11-10 23:06:00 +00003169 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling94d04b82009-05-20 23:21:38 +00003170 }
3171 }
3172
3173 EmitLabel("debug_inlined_end", 1);
3174 Asm->EOL();
3175}