blob: bbaf1ad1aae8ebe3b5c4de0f3e45e4049aa2df6e [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"
Chris Lattner334fd1f2009-09-16 00:08:41 +000026#include "llvm/Support/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000027#include "llvm/Support/Timer.h"
Devang Patele4b27562009-08-28 23:24:31 +000028#include "llvm/Support/Debug.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000030using namespace llvm;
31
32static TimerGroup &getDwarfTimerGroup() {
33 static TimerGroup DwarfTimerGroup("Dwarf Debugging");
34 return DwarfTimerGroup;
35}
36
37//===----------------------------------------------------------------------===//
38
39/// Configuration values for initial hash set sizes (log2).
40///
41static const unsigned InitDiesSetSize = 9; // log2(512)
42static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
43static const unsigned InitValuesSetSize = 9; // log2(512)
44
45namespace llvm {
46
47//===----------------------------------------------------------------------===//
48/// CompileUnit - This dwarf writer support class manages information associate
49/// with a source file.
50class VISIBILITY_HIDDEN CompileUnit {
51 /// ID - File identifier for source.
52 ///
53 unsigned ID;
54
55 /// Die - Compile unit debug information entry.
56 ///
57 DIE *Die;
58
59 /// GVToDieMap - Tracks the mapping of unit level debug informaton
60 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000061 /// FIXME : Rename GVToDieMap -> NodeToDieMap
62 std::map<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000063
64 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
65 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000066 /// FIXME : Rename
67 std::map<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000068
69 /// Globals - A map of globally visible named entities for this unit.
70 ///
71 StringMap<DIE*> Globals;
72
73 /// DiesSet - Used to uniquely define dies within the compile unit.
74 ///
75 FoldingSet<DIE> DiesSet;
76public:
77 CompileUnit(unsigned I, DIE *D)
Bill Wendling39dd6962009-05-20 23:31:45 +000078 : ID(I), Die(D), DiesSet(InitDiesSetSize) {}
79 ~CompileUnit() { delete Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000080
81 // Accessors.
Bill Wendling39dd6962009-05-20 23:31:45 +000082 unsigned getID() const { return ID; }
83 DIE* getDie() const { return Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000084 StringMap<DIE*> &getGlobals() { return Globals; }
85
86 /// hasContent - Return true if this compile unit has something to write out.
87 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000088 bool hasContent() const { return !Die->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +000089
90 /// AddGlobal - Add a new global entity to the compile unit.
91 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000092 void AddGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000093
94 /// getDieMapSlotFor - Returns the debug information entry map slot for the
95 /// specified debug variable.
Devang Patele4b27562009-08-28 23:24:31 +000096 DIE *&getDieMapSlotFor(MDNode *N) { return GVToDieMap[N]; }
Bill Wendling0310d762009-05-15 09:23:25 +000097
Chris Lattner1cda87c2009-07-14 04:50:12 +000098 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for
99 /// the specified debug variable.
Devang Patele4b27562009-08-28 23:24:31 +0000100 DIEEntry *&getDIEEntrySlotFor(MDNode *N) {
101 return GVToDIEEntryMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +0000102 }
103
104 /// AddDie - Adds or interns the DIE to the compile unit.
105 ///
106 DIE *AddDie(DIE &Buffer) {
107 FoldingSetNodeID ID;
108 Buffer.Profile(ID);
109 void *Where;
110 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
111
112 if (!Die) {
113 Die = new DIE(Buffer);
114 DiesSet.InsertNode(Die, Where);
115 this->Die->AddChild(Die);
116 Buffer.Detach();
117 }
118
119 return Die;
120 }
121};
122
123//===----------------------------------------------------------------------===//
124/// DbgVariable - This class is used to track local variable information.
125///
126class VISIBILITY_HIDDEN DbgVariable {
127 DIVariable Var; // Variable Descriptor.
128 unsigned FrameIndex; // Variable frame index.
Bill Wendling1180c782009-05-18 23:08:55 +0000129 bool InlinedFnVar; // Variable for an inlined function.
Bill Wendling0310d762009-05-15 09:23:25 +0000130public:
Bill Wendling1180c782009-05-18 23:08:55 +0000131 DbgVariable(DIVariable V, unsigned I, bool IFV)
132 : Var(V), FrameIndex(I), InlinedFnVar(IFV) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000133
134 // Accessors.
Bill Wendling1180c782009-05-18 23:08:55 +0000135 DIVariable getVariable() const { return Var; }
Bill Wendling0310d762009-05-15 09:23:25 +0000136 unsigned getFrameIndex() const { return FrameIndex; }
Bill Wendling1180c782009-05-18 23:08:55 +0000137 bool isInlinedFnVar() const { return InlinedFnVar; }
Bill Wendling0310d762009-05-15 09:23:25 +0000138};
139
140//===----------------------------------------------------------------------===//
141/// DbgScope - This class is used to track scope information.
142///
143class DbgConcreteScope;
144class VISIBILITY_HIDDEN DbgScope {
145 DbgScope *Parent; // Parent to this scope.
146 DIDescriptor Desc; // Debug info descriptor for scope.
147 // Either subprogram or block.
148 unsigned StartLabelID; // Label ID of the beginning of scope.
149 unsigned EndLabelID; // Label ID of the end of scope.
Devang Pateld38dd112009-10-01 18:25:23 +0000150 const MachineInstr *LastInsn; // Last instruction of this scope.
151 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000152 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
153 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
154 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000155
Owen Anderson04c05f72009-06-24 22:53:20 +0000156 // Private state for dump()
157 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000158public:
159 DbgScope(DbgScope *P, DIDescriptor D)
Devang Pateld38dd112009-10-01 18:25:23 +0000160 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), LastInsn(0),
161 FirstInsn(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000162 virtual ~DbgScope();
163
164 // Accessors.
165 DbgScope *getParent() const { return Parent; }
166 DIDescriptor getDesc() const { return Desc; }
167 unsigned getStartLabelID() const { return StartLabelID; }
168 unsigned getEndLabelID() const { return EndLabelID; }
169 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
170 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
171 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; }
172 void setStartLabelID(unsigned S) { StartLabelID = S; }
173 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Pateld38dd112009-10-01 18:25:23 +0000174 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
175 const MachineInstr *getLastInsn() { return LastInsn; }
176 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
177 const MachineInstr *getFirstInsn() { return FirstInsn; }
Bill Wendling0310d762009-05-15 09:23:25 +0000178 /// AddScope - Add a scope to the scope.
179 ///
180 void AddScope(DbgScope *S) { Scopes.push_back(S); }
181
182 /// AddVariable - Add a variable to the scope.
183 ///
184 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
185
186 /// AddConcreteInst - Add a concrete instance to the scope.
187 ///
188 void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); }
189
Devang Patelaf9e8472009-10-01 20:31:14 +0000190 void FixInstructionMarkers() {
191 assert (getFirstInsn() && "First instruction is missing!");
192 if (getLastInsn())
193 return;
194
195 // If a scope does not have an instruction to mark an end then use
196 // the end of last child scope.
197 SmallVector<DbgScope *, 4> &Scopes = getScopes();
198 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
199 DbgScope *L = Scopes.back();
200 if (!L->getLastInsn())
201 L->FixInstructionMarkers();
202 setLastInsn(L->getLastInsn());
203 }
204
Bill Wendling0310d762009-05-15 09:23:25 +0000205#ifndef NDEBUG
206 void dump() const;
207#endif
208};
209
210#ifndef NDEBUG
211void DbgScope::dump() const {
Chris Lattnerc281de12009-08-23 00:51:00 +0000212 raw_ostream &err = errs();
213 err.indent(IndentLevel);
214 Desc.dump();
215 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000216
217 IndentLevel += 2;
218
219 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
220 if (Scopes[i] != this)
221 Scopes[i]->dump();
222
223 IndentLevel -= 2;
224}
225#endif
226
227//===----------------------------------------------------------------------===//
228/// DbgConcreteScope - This class is used to track a scope that holds concrete
229/// instance information.
230///
231class VISIBILITY_HIDDEN DbgConcreteScope : public DbgScope {
232 CompileUnit *Unit;
233 DIE *Die; // Debug info for this concrete scope.
234public:
235 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
236
237 // Accessors.
238 DIE *getDie() const { return Die; }
239 void setDie(DIE *D) { Die = D; }
240};
241
242DbgScope::~DbgScope() {
243 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
244 delete Scopes[i];
245 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
246 delete Variables[j];
247 for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k)
248 delete ConcreteInsts[k];
249}
250
251} // end llvm namespace
252
Chris Lattneraf76e592009-08-22 20:48:53 +0000253DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000254 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000255 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000256 ValuesSet(InitValuesSetSize), Values(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000257 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel43da8fb2009-07-13 21:26:33 +0000258 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000259 if (TimePassesIsEnabled)
260 DebugTimer = new Timer("Dwarf Debug Writer",
261 getDwarfTimerGroup());
262}
263DwarfDebug::~DwarfDebug() {
264 for (unsigned j = 0, M = Values.size(); j < M; ++j)
265 delete Values[j];
266
Devang Patele4b27562009-08-28 23:24:31 +0000267 for (DenseMap<const MDNode *, DbgScope *>::iterator
Bill Wendling0310d762009-05-15 09:23:25 +0000268 I = AbstractInstanceRootMap.begin(),
269 E = AbstractInstanceRootMap.end(); I != E;++I)
270 delete I->second;
271
272 delete DebugTimer;
273}
274
275/// AssignAbbrevNumber - Define a unique number for the abbreviation.
276///
277void DwarfDebug::AssignAbbrevNumber(DIEAbbrev &Abbrev) {
278 // Profile the node so that we can make it unique.
279 FoldingSetNodeID ID;
280 Abbrev.Profile(ID);
281
282 // Check the set for priors.
283 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
284
285 // If it's newly added.
286 if (InSet == &Abbrev) {
287 // Add to abbreviation list.
288 Abbreviations.push_back(&Abbrev);
289
290 // Assign the vector position + 1 as its number.
291 Abbrev.setNumber(Abbreviations.size());
292 } else {
293 // Assign existing abbreviation number.
294 Abbrev.setNumber(InSet->getNumber());
295 }
296}
297
Bill Wendling995f80a2009-05-20 23:24:48 +0000298/// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
299/// information entry.
300DIEEntry *DwarfDebug::CreateDIEEntry(DIE *Entry) {
Bill Wendling0310d762009-05-15 09:23:25 +0000301 DIEEntry *Value;
302
303 if (Entry) {
304 FoldingSetNodeID ID;
305 DIEEntry::Profile(ID, Entry);
306 void *Where;
307 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
308
309 if (Value) return Value;
310
311 Value = new DIEEntry(Entry);
312 ValuesSet.InsertNode(Value, Where);
313 } else {
314 Value = new DIEEntry(Entry);
315 }
316
317 Values.push_back(Value);
318 return Value;
319}
320
321/// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
322///
323void DwarfDebug::SetDIEEntry(DIEEntry *Value, DIE *Entry) {
324 Value->setEntry(Entry);
325
326 // Add to values set if not already there. If it is, we merely have a
327 // duplicate in the values list (no harm.)
328 ValuesSet.GetOrInsertNode(Value);
329}
330
331/// AddUInt - Add an unsigned integer attribute data and value.
332///
333void DwarfDebug::AddUInt(DIE *Die, unsigned Attribute,
334 unsigned Form, uint64_t Integer) {
335 if (!Form) Form = DIEInteger::BestForm(false, Integer);
336
337 FoldingSetNodeID ID;
338 DIEInteger::Profile(ID, Integer);
339 void *Where;
340 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
341
342 if (!Value) {
343 Value = new DIEInteger(Integer);
344 ValuesSet.InsertNode(Value, Where);
345 Values.push_back(Value);
346 }
347
348 Die->AddValue(Attribute, Form, Value);
349}
350
351/// AddSInt - Add an signed integer attribute data and value.
352///
353void DwarfDebug::AddSInt(DIE *Die, unsigned Attribute,
354 unsigned Form, int64_t Integer) {
355 if (!Form) Form = DIEInteger::BestForm(true, Integer);
356
357 FoldingSetNodeID ID;
358 DIEInteger::Profile(ID, (uint64_t)Integer);
359 void *Where;
360 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
361
362 if (!Value) {
363 Value = new DIEInteger(Integer);
364 ValuesSet.InsertNode(Value, Where);
365 Values.push_back(Value);
366 }
367
368 Die->AddValue(Attribute, Form, Value);
369}
370
371/// AddString - Add a string attribute data and value.
372///
373void DwarfDebug::AddString(DIE *Die, unsigned Attribute, unsigned Form,
374 const std::string &String) {
375 FoldingSetNodeID ID;
376 DIEString::Profile(ID, String);
377 void *Where;
378 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
379
380 if (!Value) {
381 Value = new DIEString(String);
382 ValuesSet.InsertNode(Value, Where);
383 Values.push_back(Value);
384 }
385
386 Die->AddValue(Attribute, Form, Value);
387}
388
389/// AddLabel - Add a Dwarf label attribute data and value.
390///
391void DwarfDebug::AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
392 const DWLabel &Label) {
393 FoldingSetNodeID ID;
394 DIEDwarfLabel::Profile(ID, Label);
395 void *Where;
396 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
397
398 if (!Value) {
399 Value = new DIEDwarfLabel(Label);
400 ValuesSet.InsertNode(Value, Where);
401 Values.push_back(Value);
402 }
403
404 Die->AddValue(Attribute, Form, Value);
405}
406
407/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
408///
409void DwarfDebug::AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
410 const std::string &Label) {
411 FoldingSetNodeID ID;
412 DIEObjectLabel::Profile(ID, Label);
413 void *Where;
414 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
415
416 if (!Value) {
417 Value = new DIEObjectLabel(Label);
418 ValuesSet.InsertNode(Value, Where);
419 Values.push_back(Value);
420 }
421
422 Die->AddValue(Attribute, Form, Value);
423}
424
425/// AddSectionOffset - Add a section offset label attribute data and value.
426///
427void DwarfDebug::AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
428 const DWLabel &Label, const DWLabel &Section,
429 bool isEH, bool useSet) {
430 FoldingSetNodeID ID;
431 DIESectionOffset::Profile(ID, Label, Section);
432 void *Where;
433 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
434
435 if (!Value) {
436 Value = new DIESectionOffset(Label, Section, isEH, useSet);
437 ValuesSet.InsertNode(Value, Where);
438 Values.push_back(Value);
439 }
440
441 Die->AddValue(Attribute, Form, Value);
442}
443
444/// AddDelta - Add a label delta attribute data and value.
445///
446void DwarfDebug::AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
447 const DWLabel &Hi, const DWLabel &Lo) {
448 FoldingSetNodeID ID;
449 DIEDelta::Profile(ID, Hi, Lo);
450 void *Where;
451 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
452
453 if (!Value) {
454 Value = new DIEDelta(Hi, Lo);
455 ValuesSet.InsertNode(Value, Where);
456 Values.push_back(Value);
457 }
458
459 Die->AddValue(Attribute, Form, Value);
460}
461
462/// AddBlock - Add block data.
463///
464void DwarfDebug::AddBlock(DIE *Die, unsigned Attribute, unsigned Form,
465 DIEBlock *Block) {
466 Block->ComputeSize(TD);
467 FoldingSetNodeID ID;
468 Block->Profile(ID);
469 void *Where;
470 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
471
472 if (!Value) {
473 Value = Block;
474 ValuesSet.InsertNode(Value, Where);
475 Values.push_back(Value);
476 } else {
477 // Already exists, reuse the previous one.
478 delete Block;
479 Block = cast<DIEBlock>(Value);
480 }
481
482 Die->AddValue(Attribute, Block->BestForm(), Value);
483}
484
485/// AddSourceLine - Add location information to specified debug information
486/// entry.
487void DwarfDebug::AddSourceLine(DIE *Die, const DIVariable *V) {
488 // If there is no compile unit specified, don't add a line #.
489 if (V->getCompileUnit().isNull())
490 return;
491
492 unsigned Line = V->getLineNumber();
493 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
494 assert(FileID && "Invalid file id");
495 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
496 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
497}
498
499/// AddSourceLine - Add location information to specified debug information
500/// entry.
501void DwarfDebug::AddSourceLine(DIE *Die, const DIGlobal *G) {
502 // If there is no compile unit specified, don't add a line #.
503 if (G->getCompileUnit().isNull())
504 return;
505
506 unsigned Line = G->getLineNumber();
507 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
508 assert(FileID && "Invalid file id");
509 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
510 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
511}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000512
513/// AddSourceLine - Add location information to specified debug information
514/// entry.
515void DwarfDebug::AddSourceLine(DIE *Die, const DISubprogram *SP) {
516 // If there is no compile unit specified, don't add a line #.
517 if (SP->getCompileUnit().isNull())
518 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000519 // If the line number is 0, don't add it.
520 if (SP->getLineNumber() == 0)
521 return;
522
Devang Patel82dfc0c2009-08-31 22:47:13 +0000523
524 unsigned Line = SP->getLineNumber();
525 unsigned FileID = FindCompileUnit(SP->getCompileUnit()).getID();
526 assert(FileID && "Invalid file id");
527 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
528 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
529}
530
531/// AddSourceLine - Add location information to specified debug information
532/// entry.
Bill Wendling0310d762009-05-15 09:23:25 +0000533void DwarfDebug::AddSourceLine(DIE *Die, const DIType *Ty) {
534 // If there is no compile unit specified, don't add a line #.
535 DICompileUnit CU = Ty->getCompileUnit();
536 if (CU.isNull())
537 return;
538
539 unsigned Line = Ty->getLineNumber();
540 unsigned FileID = FindCompileUnit(CU).getID();
541 assert(FileID && "Invalid file id");
542 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
543 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
544}
545
Caroline Ticedc8f6042009-08-31 21:19:37 +0000546/* Byref variables, in Blocks, are declared by the programmer as
547 "SomeType VarName;", but the compiler creates a
548 __Block_byref_x_VarName struct, and gives the variable VarName
549 either the struct, or a pointer to the struct, as its type. This
550 is necessary for various behind-the-scenes things the compiler
551 needs to do with by-reference variables in blocks.
552
553 However, as far as the original *programmer* is concerned, the
554 variable should still have type 'SomeType', as originally declared.
555
556 The following function dives into the __Block_byref_x_VarName
557 struct to find the original type of the variable. This will be
558 passed back to the code generating the type for the Debug
559 Information Entry for the variable 'VarName'. 'VarName' will then
560 have the original type 'SomeType' in its debug information.
561
562 The original type 'SomeType' will be the type of the field named
563 'VarName' inside the __Block_byref_x_VarName struct.
564
565 NOTE: In order for this to not completely fail on the debugger
566 side, the Debug Information Entry for the variable VarName needs to
567 have a DW_AT_location that tells the debugger how to unwind through
568 the pointers and __Block_byref_x_VarName struct to find the actual
569 value of the variable. The function AddBlockByrefType does this. */
570
571/// Find the type the programmer originally declared the variable to be
572/// and return that type.
573///
574DIType DwarfDebug::GetBlockByrefType(DIType Ty, std::string Name) {
575
576 DIType subType = Ty;
577 unsigned tag = Ty.getTag();
578
579 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000580 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000581 subType = DTy.getTypeDerivedFrom();
582 }
583
584 DICompositeType blockStruct = DICompositeType(subType.getNode());
585
586 DIArray Elements = blockStruct.getTypeArray();
587
588 if (Elements.isNull())
589 return Ty;
590
591 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
592 DIDescriptor Element = Elements.getElement(i);
593 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel5ccdd102009-09-29 18:40:58 +0000594 if (strcmp(Name.c_str(), DT.getName()) == 0)
Caroline Ticedc8f6042009-08-31 21:19:37 +0000595 return (DT.getTypeDerivedFrom());
596 }
597
598 return Ty;
599}
600
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000601/// AddComplexAddress - Start with the address based on the location provided,
602/// and generate the DWARF information necessary to find the actual variable
603/// given the extra address information encoded in the DIVariable, starting from
604/// the starting location. Add the DWARF information to the die.
605///
606void DwarfDebug::AddComplexAddress(DbgVariable *&DV, DIE *Die,
607 unsigned Attribute,
608 const MachineLocation &Location) {
609 const DIVariable &VD = DV->getVariable();
610 DIType Ty = VD.getType();
611
612 // Decode the original location, and use that as the start of the byref
613 // variable's location.
614 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
615 DIEBlock *Block = new DIEBlock();
616
617 if (Location.isReg()) {
618 if (Reg < 32) {
619 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
620 } else {
621 Reg = Reg - dwarf::DW_OP_reg0;
622 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
623 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
624 }
625 } else {
626 if (Reg < 32)
627 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
628 else {
629 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
630 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
631 }
632
633 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
634 }
635
636 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
637 uint64_t Element = VD.getAddrElement(i);
638
639 if (Element == DIFactory::OpPlus) {
640 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
641 AddUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
642 } else if (Element == DIFactory::OpDeref) {
643 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
644 } else llvm_unreachable("unknown DIFactory Opcode");
645 }
646
647 // Now attach the location information to the DIE.
648 AddBlock(Die, Attribute, 0, Block);
649}
650
Caroline Ticedc8f6042009-08-31 21:19:37 +0000651/* Byref variables, in Blocks, are declared by the programmer as "SomeType
652 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
653 gives the variable VarName either the struct, or a pointer to the struct, as
654 its type. This is necessary for various behind-the-scenes things the
655 compiler needs to do with by-reference variables in Blocks.
656
657 However, as far as the original *programmer* is concerned, the variable
658 should still have type 'SomeType', as originally declared.
659
660 The function GetBlockByrefType dives into the __Block_byref_x_VarName
661 struct to find the original type of the variable, which is then assigned to
662 the variable's Debug Information Entry as its real type. So far, so good.
663 However now the debugger will expect the variable VarName to have the type
664 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000665 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000666 pointers and struct to find the actual variable of type SomeType.
667
668 The following function does just that. We start by getting
669 the "normal" location for the variable. This will be the location
670 of either the struct __Block_byref_x_VarName or the pointer to the
671 struct __Block_byref_x_VarName.
672
673 The struct will look something like:
674
675 struct __Block_byref_x_VarName {
676 ... <various fields>
677 struct __Block_byref_x_VarName *forwarding;
678 ... <various other fields>
679 SomeType VarName;
680 ... <maybe more fields>
681 };
682
683 If we are given the struct directly (as our starting point) we
684 need to tell the debugger to:
685
686 1). Add the offset of the forwarding field.
687
688 2). Follow that pointer to get the the real __Block_byref_x_VarName
689 struct to use (the real one may have been copied onto the heap).
690
691 3). Add the offset for the field VarName, to find the actual variable.
692
693 If we started with a pointer to the struct, then we need to
694 dereference that pointer first, before the other steps.
695 Translating this into DWARF ops, we will need to append the following
696 to the current location description for the variable:
697
698 DW_OP_deref -- optional, if we start with a pointer
699 DW_OP_plus_uconst <forward_fld_offset>
700 DW_OP_deref
701 DW_OP_plus_uconst <varName_fld_offset>
702
703 That is what this function does. */
704
705/// AddBlockByrefAddress - Start with the address based on the location
706/// provided, and generate the DWARF information necessary to find the
707/// actual Block variable (navigating the Block struct) based on the
708/// starting location. Add the DWARF information to the die. For
709/// more information, read large comment just above here.
710///
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000711void DwarfDebug::AddBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000712 unsigned Attribute,
713 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000714 const DIVariable &VD = DV->getVariable();
715 DIType Ty = VD.getType();
716 DIType TmpTy = Ty;
717 unsigned Tag = Ty.getTag();
718 bool isPointer = false;
719
Devang Patel5ccdd102009-09-29 18:40:58 +0000720 const char *varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000721
722 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000723 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000724 TmpTy = DTy.getTypeDerivedFrom();
725 isPointer = true;
726 }
727
728 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
729
Daniel Dunbar00564992009-09-19 20:40:14 +0000730 // Find the __forwarding field and the variable field in the __Block_byref
731 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000732 DIArray Fields = blockStruct.getTypeArray();
733 DIDescriptor varField = DIDescriptor();
734 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000735
736
Daniel Dunbar00564992009-09-19 20:40:14 +0000737 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
738 DIDescriptor Element = Fields.getElement(i);
739 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel5ccdd102009-09-29 18:40:58 +0000740 const char *fieldName = DT.getName();
741 if (strcmp(fieldName, "__forwarding") == 0)
Daniel Dunbar00564992009-09-19 20:40:14 +0000742 forwardingField = Element;
Devang Patel5ccdd102009-09-29 18:40:58 +0000743 else if (strcmp(fieldName, varName) == 0)
Daniel Dunbar00564992009-09-19 20:40:14 +0000744 varField = Element;
745 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000746
Mike Stump7e3720d2009-09-24 23:21:26 +0000747 assert(!varField.isNull() && "Can't find byref variable in Block struct");
748 assert(!forwardingField.isNull()
749 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000750
Daniel Dunbar00564992009-09-19 20:40:14 +0000751 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000752 unsigned int forwardingFieldOffset =
753 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
754 unsigned int varFieldOffset =
755 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000756
Mike Stump7e3720d2009-09-24 23:21:26 +0000757 // Decode the original location, and use that as the start of the byref
758 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000759 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
760 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000761
Daniel Dunbar00564992009-09-19 20:40:14 +0000762 if (Location.isReg()) {
763 if (Reg < 32)
764 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
765 else {
766 Reg = Reg - dwarf::DW_OP_reg0;
767 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
768 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
769 }
770 } else {
771 if (Reg < 32)
772 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
773 else {
774 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
775 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
776 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000777
Daniel Dunbar00564992009-09-19 20:40:14 +0000778 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
779 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000780
Mike Stump7e3720d2009-09-24 23:21:26 +0000781 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000782 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000783 if (isPointer)
784 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000785
Daniel Dunbar00564992009-09-19 20:40:14 +0000786 // Next add the offset for the '__forwarding' field:
787 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
788 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000789 if (forwardingFieldOffset > 0) {
790 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
791 AddUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
792 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000793
Daniel Dunbar00564992009-09-19 20:40:14 +0000794 // Now dereference the __forwarding field to get to the real __Block_byref
795 // struct: DW_OP_deref.
Daniel Dunbar00564992009-09-19 20:40:14 +0000796 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000797
Daniel Dunbar00564992009-09-19 20:40:14 +0000798 // Now that we've got the real __Block_byref... struct, add the offset
799 // for the variable's field to get to the location of the actual variable:
800 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000801 if (varFieldOffset > 0) {
802 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
803 AddUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
804 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000805
Daniel Dunbar00564992009-09-19 20:40:14 +0000806 // Now attach the location information to the DIE.
Daniel Dunbar00564992009-09-19 20:40:14 +0000807 AddBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000808}
809
Bill Wendling0310d762009-05-15 09:23:25 +0000810/// AddAddress - Add an address attribute to a die based on the location
811/// provided.
812void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute,
813 const MachineLocation &Location) {
814 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
815 DIEBlock *Block = new DIEBlock();
816
817 if (Location.isReg()) {
818 if (Reg < 32) {
819 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
820 } else {
821 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
822 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
823 }
824 } else {
825 if (Reg < 32) {
826 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
827 } else {
828 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
829 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
830 }
831
832 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
833 }
834
835 AddBlock(Die, Attribute, 0, Block);
836}
837
838/// AddType - Add a new type attribute to the specified entity.
839void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
840 if (Ty.isNull())
841 return;
842
843 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +0000844 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000845
846 // If it exists then use the existing value.
847 if (Slot) {
848 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
849 return;
850 }
851
852 // Set up proxy.
Bill Wendling995f80a2009-05-20 23:24:48 +0000853 Slot = CreateDIEEntry();
Bill Wendling0310d762009-05-15 09:23:25 +0000854
855 // Construct type.
856 DIE Buffer(dwarf::DW_TAG_base_type);
Devang Patel6ceea332009-08-31 18:49:10 +0000857 if (Ty.isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000858 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getNode()));
Devang Patel6ceea332009-08-31 18:49:10 +0000859 else if (Ty.isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000860 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000861 else {
Devang Patel6ceea332009-08-31 18:49:10 +0000862 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patele4b27562009-08-28 23:24:31 +0000863 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000864 }
865
866 // Add debug information entry to entity and appropriate context.
867 DIE *Die = NULL;
868 DIDescriptor Context = Ty.getContext();
869 if (!Context.isNull())
Devang Patele4b27562009-08-28 23:24:31 +0000870 Die = DW_Unit->getDieMapSlotFor(Context.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000871
872 if (Die) {
873 DIE *Child = new DIE(Buffer);
874 Die->AddChild(Child);
875 Buffer.Detach();
876 SetDIEEntry(Slot, Child);
877 } else {
878 Die = DW_Unit->AddDie(Buffer);
879 SetDIEEntry(Slot, Die);
880 }
881
882 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
883}
884
885/// ConstructTypeDIE - Construct basic type die from DIBasicType.
886void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
887 DIBasicType BTy) {
888 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000889 const char *Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000890 Buffer.setTag(dwarf::DW_TAG_base_type);
891 AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
892 BTy.getEncoding());
893
894 // Add name if not anonymous or intermediate type.
Devang Patel5ccdd102009-09-29 18:40:58 +0000895 if (Name)
Bill Wendling0310d762009-05-15 09:23:25 +0000896 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
897 uint64_t Size = BTy.getSizeInBits() >> 3;
898 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
899}
900
901/// ConstructTypeDIE - Construct derived type die from DIDerivedType.
902void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
903 DIDerivedType DTy) {
904 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000905 const char *Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000906 uint64_t Size = DTy.getSizeInBits() >> 3;
907 unsigned Tag = DTy.getTag();
908
909 // FIXME - Workaround for templates.
910 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
911
912 Buffer.setTag(Tag);
913
914 // Map to main type, void will not have a type.
915 DIType FromTy = DTy.getTypeDerivedFrom();
916 AddType(DW_Unit, &Buffer, FromTy);
917
918 // Add name if not anonymous or intermediate type.
Devang Patel5ccdd102009-09-29 18:40:58 +0000919 if (Name)
Bill Wendling0310d762009-05-15 09:23:25 +0000920 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
921
922 // Add size if non-zero (derived types might be zero-sized.)
923 if (Size)
924 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
925
926 // Add source line info if available and TyDesc is not a forward declaration.
927 if (!DTy.isForwardDecl())
928 AddSourceLine(&Buffer, &DTy);
929}
930
931/// ConstructTypeDIE - Construct type DIE from DICompositeType.
932void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
933 DICompositeType CTy) {
934 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000935 const char *Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000936
937 uint64_t Size = CTy.getSizeInBits() >> 3;
938 unsigned Tag = CTy.getTag();
939 Buffer.setTag(Tag);
940
941 switch (Tag) {
942 case dwarf::DW_TAG_vector_type:
943 case dwarf::DW_TAG_array_type:
944 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
945 break;
946 case dwarf::DW_TAG_enumeration_type: {
947 DIArray Elements = CTy.getTypeArray();
948
949 // Add enumerators to enumeration type.
950 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
951 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000952 DIEnumerator Enum(Elements.getElement(i).getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000953 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
954 Buffer.AddChild(ElemDie);
955 }
956 }
957 break;
958 case dwarf::DW_TAG_subroutine_type: {
959 // Add return type.
960 DIArray Elements = CTy.getTypeArray();
961 DIDescriptor RTy = Elements.getElement(0);
Devang Patele4b27562009-08-28 23:24:31 +0000962 AddType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000963
964 // Add prototype flag.
965 AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
966
967 // Add arguments.
968 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
969 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
970 DIDescriptor Ty = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000971 AddType(DW_Unit, Arg, DIType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000972 Buffer.AddChild(Arg);
973 }
974 }
975 break;
976 case dwarf::DW_TAG_structure_type:
977 case dwarf::DW_TAG_union_type:
978 case dwarf::DW_TAG_class_type: {
979 // Add elements to structure type.
980 DIArray Elements = CTy.getTypeArray();
981
982 // A forward struct declared type may not have elements available.
983 if (Elements.isNull())
984 break;
985
986 // Add elements to structure type.
987 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
988 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000989 if (Element.isNull())
990 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000991 DIE *ElemDie = NULL;
992 if (Element.getTag() == dwarf::DW_TAG_subprogram)
993 ElemDie = CreateSubprogramDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000994 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000995 else
996 ElemDie = CreateMemberDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000997 DIDerivedType(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000998 Buffer.AddChild(ElemDie);
999 }
1000
Devang Patela1ba2692009-08-27 23:51:51 +00001001 if (CTy.isAppleBlockExtension())
Bill Wendling0310d762009-05-15 09:23:25 +00001002 AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
1003
1004 unsigned RLang = CTy.getRunTimeLang();
1005 if (RLang)
1006 AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1007 dwarf::DW_FORM_data1, RLang);
1008 break;
1009 }
1010 default:
1011 break;
1012 }
1013
1014 // Add name if not anonymous or intermediate type.
Devang Patel5ccdd102009-09-29 18:40:58 +00001015 if (Name)
Bill Wendling0310d762009-05-15 09:23:25 +00001016 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1017
1018 if (Tag == dwarf::DW_TAG_enumeration_type ||
1019 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1020 // Add size if non-zero (derived types might be zero-sized.)
1021 if (Size)
1022 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
1023 else {
1024 // Add zero size if it is not a forward declaration.
1025 if (CTy.isForwardDecl())
1026 AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1027 else
1028 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1029 }
1030
1031 // Add source line info if available.
1032 if (!CTy.isForwardDecl())
1033 AddSourceLine(&Buffer, &CTy);
1034 }
1035}
1036
1037/// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1038void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
1039 int64_t L = SR.getLo();
1040 int64_t H = SR.getHi();
1041 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1042
Devang Patel6325a532009-08-14 20:59:16 +00001043 AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
1044 if (L)
1045 AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
1046 if (H)
Daniel Dunbar00564992009-09-19 20:40:14 +00001047 AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001048
1049 Buffer.AddChild(DW_Subrange);
1050}
1051
1052/// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1053void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1054 DICompositeType *CTy) {
1055 Buffer.setTag(dwarf::DW_TAG_array_type);
1056 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1057 AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
1058
1059 // Emit derived type.
1060 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
1061 DIArray Elements = CTy->getTypeArray();
1062
1063 // Construct an anonymous type for index type.
1064 DIE IdxBuffer(dwarf::DW_TAG_base_type);
1065 AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1066 AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1067 dwarf::DW_ATE_signed);
1068 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1069
1070 // Add subranges to array type.
1071 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1072 DIDescriptor Element = Elements.getElement(i);
1073 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patele4b27562009-08-28 23:24:31 +00001074 ConstructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IndexTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001075 }
1076}
1077
1078/// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1079DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
1080 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel5ccdd102009-09-29 18:40:58 +00001081 const char *Name = ETy->getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001082 AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1083 int64_t Value = ETy->getEnumValue();
1084 AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1085 return Enumerator;
1086}
1087
1088/// CreateGlobalVariableDIE - Create new DIE using GV.
1089DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit,
1090 const DIGlobalVariable &GV) {
1091 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Devang Patel5ccdd102009-09-29 18:40:58 +00001092 AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1093 GV.getDisplayName());
1094
1095 const char *LinkageName = GV.getLinkageName();
1096 if (LinkageName) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001097 // Skip special LLVM prefix that is used to inform the asm printer to not
1098 // emit usual symbol prefix before the symbol name. This happens for
1099 // Objective-C symbol names and symbol whose name is replaced using GCC's
1100 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001101 if (LinkageName[0] == 1)
1102 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001103 AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001104 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001105 }
Daniel Dunbar00564992009-09-19 20:40:14 +00001106 AddType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001107 if (!GV.isLocalToUnit())
1108 AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1109 AddSourceLine(GVDie, &GV);
1110 return GVDie;
1111}
1112
1113/// CreateMemberDIE - Create new member DIE.
1114DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
1115 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel5ccdd102009-09-29 18:40:58 +00001116 if (const char *Name = DT.getName())
Bill Wendling0310d762009-05-15 09:23:25 +00001117 AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1118
1119 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1120
1121 AddSourceLine(MemberDie, &DT);
1122
1123 uint64_t Size = DT.getSizeInBits();
1124 uint64_t FieldSize = DT.getOriginalTypeSize();
1125
1126 if (Size != FieldSize) {
1127 // Handle bitfield.
1128 AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1129 AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1130
1131 uint64_t Offset = DT.getOffsetInBits();
1132 uint64_t FieldOffset = Offset;
1133 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1134 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1135 FieldOffset = (HiMark - FieldSize);
1136 Offset -= FieldOffset;
1137
1138 // Maybe we need to work from the other end.
1139 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1140 AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1141 }
1142
1143 DIEBlock *Block = new DIEBlock();
1144 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1145 AddUInt(Block, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1146 AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, Block);
1147
1148 if (DT.isProtected())
1149 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1150 dwarf::DW_ACCESS_protected);
1151 else if (DT.isPrivate())
1152 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1153 dwarf::DW_ACCESS_private);
1154
1155 return MemberDie;
1156}
1157
1158/// CreateSubprogramDIE - Create new DIE using SP.
1159DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit,
1160 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +00001161 bool IsConstructor,
1162 bool IsInlined) {
Bill Wendling0310d762009-05-15 09:23:25 +00001163 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1164
Devang Patel5ccdd102009-09-29 18:40:58 +00001165 const char * Name = SP.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001166 AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1167
Devang Patel5ccdd102009-09-29 18:40:58 +00001168 const char *LinkageName = SP.getLinkageName();
1169 if (LinkageName) {
Devang Patel53cb17d2009-07-16 01:01:22 +00001170 // Skip special LLVM prefix that is used to inform the asm printer to not emit
1171 // usual symbol prefix before the symbol name. This happens for Objective-C
1172 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
1173 if (LinkageName[0] == 1)
1174 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001175 AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001176 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001177 }
Bill Wendling0310d762009-05-15 09:23:25 +00001178 AddSourceLine(SPDie, &SP);
1179
1180 DICompositeType SPTy = SP.getType();
1181 DIArray Args = SPTy.getTypeArray();
1182
1183 // Add prototyped tag, if C or ObjC.
1184 unsigned Lang = SP.getCompileUnit().getLanguage();
1185 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1186 Lang == dwarf::DW_LANG_ObjC)
1187 AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
1188
1189 // Add Return Type.
1190 unsigned SPTag = SPTy.getTag();
1191 if (!IsConstructor) {
1192 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1193 AddType(DW_Unit, SPDie, SPTy);
1194 else
Devang Patele4b27562009-08-28 23:24:31 +00001195 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001196 }
1197
1198 if (!SP.isDefinition()) {
1199 AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1200
1201 // Add arguments. Do not add arguments for subprogram definition. They will
1202 // be handled through RecordVariable.
1203 if (SPTag == dwarf::DW_TAG_subroutine_type)
1204 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1205 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patele4b27562009-08-28 23:24:31 +00001206 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001207 AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1208 SPDie->AddChild(Arg);
1209 }
1210 }
1211
Bill Wendling6679ee42009-05-18 22:02:36 +00001212 if (!SP.isLocalToUnit() && !IsInlined)
Bill Wendling0310d762009-05-15 09:23:25 +00001213 AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1214
1215 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patele4b27562009-08-28 23:24:31 +00001216 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001217 Slot = SPDie;
1218 return SPDie;
1219}
1220
1221/// FindCompileUnit - Get the compile unit for the given descriptor.
1222///
1223CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const {
1224 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001225 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001226 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1227 return *I->second;
1228}
1229
Bill Wendling995f80a2009-05-20 23:24:48 +00001230/// CreateDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +00001231///
Bill Wendling995f80a2009-05-20 23:24:48 +00001232DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001233 // Get the descriptor.
1234 const DIVariable &VD = DV->getVariable();
1235
1236 // Translate tag to proper Dwarf tag. The result variable is dropped for
1237 // now.
1238 unsigned Tag;
1239 switch (VD.getTag()) {
1240 case dwarf::DW_TAG_return_variable:
1241 return NULL;
1242 case dwarf::DW_TAG_arg_variable:
1243 Tag = dwarf::DW_TAG_formal_parameter;
1244 break;
1245 case dwarf::DW_TAG_auto_variable: // fall thru
1246 default:
1247 Tag = dwarf::DW_TAG_variable;
1248 break;
1249 }
1250
1251 // Define variable debug information entry.
1252 DIE *VariableDie = new DIE(Tag);
Devang Patel5ccdd102009-09-29 18:40:58 +00001253 const char *Name = VD.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001254 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1255
1256 // Add source line info if available.
1257 AddSourceLine(VariableDie, &VD);
1258
1259 // Add variable type.
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001260 // FIXME: isBlockByrefVariable should be reformulated in terms of complex addresses instead.
Caroline Ticedc8f6042009-08-31 21:19:37 +00001261 if (VD.isBlockByrefVariable())
1262 AddType(Unit, VariableDie, GetBlockByrefType(VD.getType(), Name));
1263 else
1264 AddType(Unit, VariableDie, VD.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001265
1266 // Add variable address.
Bill Wendling1180c782009-05-18 23:08:55 +00001267 if (!DV->isInlinedFnVar()) {
1268 // Variables for abstract instances of inlined functions don't get a
1269 // location.
1270 MachineLocation Location;
1271 Location.set(RI->getFrameRegister(*MF),
1272 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Caroline Ticedc8f6042009-08-31 21:19:37 +00001273
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001274
1275 if (VD.hasComplexAddress())
1276 AddComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1277 else if (VD.isBlockByrefVariable())
Mike Stumpee4b8a72009-09-24 23:11:08 +00001278 AddBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Caroline Ticedc8f6042009-08-31 21:19:37 +00001279 else
1280 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling1180c782009-05-18 23:08:55 +00001281 }
Bill Wendling0310d762009-05-15 09:23:25 +00001282
1283 return VariableDie;
1284}
1285
1286/// getOrCreateScope - Returns the scope associated with the given descriptor.
1287///
Devang Patelaf9e8472009-10-01 20:31:14 +00001288DbgScope *DwarfDebug::getDbgScope(MDNode *N, const MachineInstr *MI) {
1289 DbgScope *&Slot = DbgScopeMap[N];
1290 if (Slot) return Slot;
1291
1292 DbgScope *Parent = NULL;
1293
1294 DIDescriptor Scope(N);
1295 if (Scope.isCompileUnit()) {
1296 return NULL;
1297 } else if (Scope.isSubprogram()) {
1298 DISubprogram SP(N);
1299 DIDescriptor ParentDesc = SP.getContext();
1300 if (!ParentDesc.isNull() && !ParentDesc.isCompileUnit())
1301 Parent = getDbgScope(ParentDesc.getNode(), MI);
1302 } else if (Scope.isLexicalBlock()) {
1303 DILexicalBlock DB(N);
1304 DIDescriptor ParentDesc = DB.getContext();
1305 if (!ParentDesc.isNull())
1306 Parent = getDbgScope(ParentDesc.getNode(), MI);
1307 } else
1308 assert (0 && "Unexpected scope info");
1309
1310 Slot = new DbgScope(Parent, DIDescriptor(N));
1311 Slot->setFirstInsn(MI);
1312
1313 if (Parent)
1314 Parent->AddScope(Slot);
1315 else
1316 // First function is top level function.
1317 // FIXME - Dpatel - What is FunctionDbgScope ?
1318 if (!FunctionDbgScope)
1319 FunctionDbgScope = Slot;
1320
1321 return Slot;
1322}
1323
1324
1325/// getOrCreateScope - Returns the scope associated with the given descriptor.
1326/// FIXME - Remove this method.
Devang Patele4b27562009-08-28 23:24:31 +00001327DbgScope *DwarfDebug::getOrCreateScope(MDNode *N) {
1328 DbgScope *&Slot = DbgScopeMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +00001329 if (Slot) return Slot;
1330
1331 DbgScope *Parent = NULL;
Devang Patel5e005d82009-08-31 22:00:15 +00001332 DILexicalBlock Block(N);
Bill Wendling0310d762009-05-15 09:23:25 +00001333
Bill Wendling8fff19b2009-06-01 20:18:46 +00001334 // Don't create a new scope if we already created one for an inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001335 DenseMap<const MDNode *, DbgScope *>::iterator
1336 II = AbstractInstanceRootMap.find(N);
Bill Wendling8fff19b2009-06-01 20:18:46 +00001337 if (II != AbstractInstanceRootMap.end())
1338 return LexicalScopeStack.back();
1339
Bill Wendling0310d762009-05-15 09:23:25 +00001340 if (!Block.isNull()) {
1341 DIDescriptor ParentDesc = Block.getContext();
1342 Parent =
Devang Patele4b27562009-08-28 23:24:31 +00001343 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001344 }
1345
Devang Patele4b27562009-08-28 23:24:31 +00001346 Slot = new DbgScope(Parent, DIDescriptor(N));
Bill Wendling0310d762009-05-15 09:23:25 +00001347
1348 if (Parent)
1349 Parent->AddScope(Slot);
1350 else
1351 // First function is top level function.
1352 FunctionDbgScope = Slot;
1353
1354 return Slot;
1355}
1356
1357/// ConstructDbgScope - Construct the components of a scope.
1358///
1359void DwarfDebug::ConstructDbgScope(DbgScope *ParentScope,
1360 unsigned ParentStartID,
1361 unsigned ParentEndID,
1362 DIE *ParentDie, CompileUnit *Unit) {
1363 // Add variables to scope.
1364 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
1365 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Bill Wendling995f80a2009-05-20 23:24:48 +00001366 DIE *VariableDie = CreateDbgScopeVariable(Variables[i], Unit);
Bill Wendling0310d762009-05-15 09:23:25 +00001367 if (VariableDie) ParentDie->AddChild(VariableDie);
1368 }
1369
1370 // Add concrete instances to scope.
1371 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts =
1372 ParentScope->getConcreteInsts();
1373 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
1374 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
1375 DIE *Die = ConcreteInst->getDie();
1376
1377 unsigned StartID = ConcreteInst->getStartLabelID();
1378 unsigned EndID = ConcreteInst->getEndLabelID();
1379
1380 // Add the scope bounds.
1381 if (StartID)
1382 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1383 DWLabel("label", StartID));
1384 else
1385 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1386 DWLabel("func_begin", SubprogramCount));
1387
1388 if (EndID)
1389 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1390 DWLabel("label", EndID));
1391 else
1392 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1393 DWLabel("func_end", SubprogramCount));
1394
1395 ParentDie->AddChild(Die);
1396 }
1397
1398 // Add nested scopes.
1399 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
1400 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1401 // Define the Scope debug information entry.
1402 DbgScope *Scope = Scopes[j];
1403
1404 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1405 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1406
1407 // Ignore empty scopes.
1408 if (StartID == EndID && StartID != 0) continue;
1409
1410 // Do not ignore inlined scopes even if they don't have any variables or
1411 // scopes.
1412 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
1413 Scope->getConcreteInsts().empty())
1414 continue;
1415
1416 if (StartID == ParentStartID && EndID == ParentEndID) {
1417 // Just add stuff to the parent scope.
1418 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1419 } else {
1420 DIE *ScopeDie = new DIE(dwarf::DW_TAG_lexical_block);
1421
1422 // Add the scope bounds.
1423 if (StartID)
1424 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1425 DWLabel("label", StartID));
1426 else
1427 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1428 DWLabel("func_begin", SubprogramCount));
1429
1430 if (EndID)
1431 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1432 DWLabel("label", EndID));
1433 else
1434 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1435 DWLabel("func_end", SubprogramCount));
1436
1437 // Add the scope's contents.
1438 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1439 ParentDie->AddChild(ScopeDie);
1440 }
1441 }
1442}
1443
1444/// ConstructFunctionDbgScope - Construct the scope for the subprogram.
1445///
Bill Wendling17956162009-05-20 23:28:48 +00001446void DwarfDebug::ConstructFunctionDbgScope(DbgScope *RootScope,
1447 bool AbstractScope) {
Bill Wendling0310d762009-05-15 09:23:25 +00001448 // Exit if there is no root scope.
1449 if (!RootScope) return;
1450 DIDescriptor Desc = RootScope->getDesc();
1451 if (Desc.isNull())
1452 return;
1453
1454 // Get the subprogram debug information entry.
Devang Patele4b27562009-08-28 23:24:31 +00001455 DISubprogram SPD(Desc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001456
Bill Wendling0310d762009-05-15 09:23:25 +00001457 // Get the subprogram die.
Devang Patele4b27562009-08-28 23:24:31 +00001458 DIE *SPDie = ModuleCU->getDieMapSlotFor(SPD.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001459 assert(SPDie && "Missing subprogram descriptor");
1460
Bill Wendling17956162009-05-20 23:28:48 +00001461 if (!AbstractScope) {
1462 // Add the function bounds.
1463 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1464 DWLabel("func_begin", SubprogramCount));
1465 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1466 DWLabel("func_end", SubprogramCount));
1467 MachineLocation Location(RI->getFrameRegister(*MF));
1468 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
1469 }
Bill Wendling0310d762009-05-15 09:23:25 +00001470
Devang Patel1dbc7712009-06-29 20:45:18 +00001471 ConstructDbgScope(RootScope, 0, 0, SPDie, ModuleCU);
Bill Wendling0310d762009-05-15 09:23:25 +00001472}
1473
Bill Wendling0310d762009-05-15 09:23:25 +00001474/// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1475///
1476void DwarfDebug::ConstructDefaultDbgScope(MachineFunction *MF) {
Devang Patel1dbc7712009-06-29 20:45:18 +00001477 StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Daniel Dunbar460f6562009-07-26 09:48:23 +00001478 StringMap<DIE*>::iterator GI = Globals.find(MF->getFunction()->getName());
Devang Patel70f44262009-06-29 20:38:13 +00001479 if (GI != Globals.end()) {
1480 DIE *SPDie = GI->second;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001481
Devang Patel70f44262009-06-29 20:38:13 +00001482 // Add the function bounds.
1483 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1484 DWLabel("func_begin", SubprogramCount));
1485 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1486 DWLabel("func_end", SubprogramCount));
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001487
Devang Patel70f44262009-06-29 20:38:13 +00001488 MachineLocation Location(RI->getFrameRegister(*MF));
1489 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001490 }
Bill Wendling0310d762009-05-15 09:23:25 +00001491}
1492
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001493/// GetOrCreateSourceID - Look up the source id with the given directory and
1494/// source file names. If none currently exists, create a new id and insert it
1495/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1496/// maps as well.
Devang Patel5ccdd102009-09-29 18:40:58 +00001497unsigned DwarfDebug::GetOrCreateSourceID(const char *DirName,
1498 const char *FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001499 unsigned DId;
1500 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1501 if (DI != DirectoryIdMap.end()) {
1502 DId = DI->getValue();
1503 } else {
1504 DId = DirectoryNames.size() + 1;
1505 DirectoryIdMap[DirName] = DId;
1506 DirectoryNames.push_back(DirName);
1507 }
1508
1509 unsigned FId;
1510 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1511 if (FI != SourceFileIdMap.end()) {
1512 FId = FI->getValue();
1513 } else {
1514 FId = SourceFileNames.size() + 1;
1515 SourceFileIdMap[FileName] = FId;
1516 SourceFileNames.push_back(FileName);
1517 }
1518
1519 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1520 SourceIdMap.find(std::make_pair(DId, FId));
1521 if (SI != SourceIdMap.end())
1522 return SI->second;
1523
1524 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1525 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1526 SourceIds.push_back(std::make_pair(DId, FId));
1527
1528 return SrcId;
1529}
1530
Devang Patele4b27562009-08-28 23:24:31 +00001531void DwarfDebug::ConstructCompileUnit(MDNode *N) {
1532 DICompileUnit DIUnit(N);
Devang Patel5ccdd102009-09-29 18:40:58 +00001533 const char *FN = DIUnit.getFilename();
1534 const char *Dir = DIUnit.getDirectory();
1535 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001536
1537 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
1538 AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
1539 DWLabel("section_line", 0), DWLabel("section_line", 0),
1540 false);
1541 AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001542 DIUnit.getProducer());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001543 AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
1544 DIUnit.getLanguage());
1545 AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
1546
Devang Patel5ccdd102009-09-29 18:40:58 +00001547 if (Dir)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001548 AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
1549 if (DIUnit.isOptimized())
1550 AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1551
Devang Patel5ccdd102009-09-29 18:40:58 +00001552 if (const char *Flags = DIUnit.getFlags())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001553 AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
1554
1555 unsigned RVer = DIUnit.getRunTimeVersion();
1556 if (RVer)
1557 AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1558 dwarf::DW_FORM_data1, RVer);
1559
1560 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001561 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001562 // Use first compile unit marked as isMain as the compile unit
1563 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001564 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001565 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001566
Devang Patele4b27562009-08-28 23:24:31 +00001567 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001568 CompileUnits.push_back(Unit);
1569}
1570
Devang Patele4b27562009-08-28 23:24:31 +00001571void DwarfDebug::ConstructGlobalVariableDIE(MDNode *N) {
1572 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001573
Devang Patel905cf5e2009-09-04 23:59:07 +00001574 // If debug information is malformed then ignore it.
1575 if (DI_GV.Verify() == false)
1576 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001577
1578 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001579 DIE *&Slot = ModuleCU->getDieMapSlotFor(DI_GV.getNode());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001580 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001581 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001582
Devang Patel1dbc7712009-06-29 20:45:18 +00001583 DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001584
1585 // Add address.
1586 DIEBlock *Block = new DIEBlock();
1587 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001588 AddObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner334fd1f2009-09-16 00:08:41 +00001589 Asm->Mang->getMangledName(DI_GV.getGlobal()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001590 AddBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1591
1592 // Add to map.
1593 Slot = VariableDie;
1594
1595 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001596 ModuleCU->getDie()->AddChild(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001597
1598 // Expose as global. FIXME - need to check external flag.
Devang Patel5ccdd102009-09-29 18:40:58 +00001599 ModuleCU->AddGlobal(DI_GV.getName(), VariableDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001600 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001601}
1602
Devang Patele4b27562009-08-28 23:24:31 +00001603void DwarfDebug::ConstructSubprogram(MDNode *N) {
1604 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001605
1606 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001607 DIE *&Slot = ModuleCU->getDieMapSlotFor(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001608 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001609 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001610
1611 if (!SP.isDefinition())
1612 // This is a method declaration which will be handled while constructing
1613 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001614 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001615
Devang Patel1dbc7712009-06-29 20:45:18 +00001616 DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001617
1618 // Add to map.
1619 Slot = SubprogramDie;
1620
1621 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001622 ModuleCU->getDie()->AddChild(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001623
1624 // Expose as global.
Devang Patel5ccdd102009-09-29 18:40:58 +00001625 ModuleCU->AddGlobal(SP.getName(), SubprogramDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001626 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001627}
1628
Daniel Dunbar00564992009-09-19 20:40:14 +00001629/// BeginModule - Emit all Dwarf sections that should come prior to the
1630/// content. Create global DIEs and emit initial debug info sections.
1631/// This is inovked by the target AsmPrinter.
Devang Patel208622d2009-06-25 22:36:02 +00001632void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) {
1633 this->M = M;
1634
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001635 if (TimePassesIsEnabled)
1636 DebugTimer->startTimer();
1637
Devang Patel78ab9e22009-07-30 18:56:46 +00001638 DebugInfoFinder DbgFinder;
1639 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001640
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001641 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001642 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1643 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001644 ConstructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001645
1646 if (CompileUnits.empty()) {
1647 if (TimePassesIsEnabled)
1648 DebugTimer->stopTimer();
1649
1650 return;
1651 }
1652
Devang Patel70f44262009-06-29 20:38:13 +00001653 // If main compile unit for this module is not seen than randomly
1654 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001655 if (!ModuleCU)
1656 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001657
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001658 // If there is not any debug info available for any global variables and any
1659 // subprograms then there is not any debug info to emit.
Devang Patel78ab9e22009-07-30 18:56:46 +00001660 if (DbgFinder.global_variable_count() == 0
1661 && DbgFinder.subprogram_count() == 0) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001662 if (TimePassesIsEnabled)
1663 DebugTimer->stopTimer();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001664 return;
1665 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001666
Devang Patel13e16b62009-06-26 01:49:18 +00001667 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001668 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1669 E = DbgFinder.global_variable_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001670 ConstructGlobalVariableDIE(*I);
1671
1672 // Create DIEs for each of the externally visible subprograms.
Devang Patel78ab9e22009-07-30 18:56:46 +00001673 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1674 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001675 ConstructSubprogram(*I);
1676
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001677 MMI = mmi;
1678 shouldEmit = true;
1679 MMI->setDebugInfoAvailability(true);
1680
1681 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001682 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001683
1684 // Print out .file directives to specify files for .loc directives. These are
1685 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001686 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001687 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1688 // Remember source id starts at 1.
1689 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1690 sys::Path FullPath(getSourceDirectoryName(Id.first));
1691 bool AppendOk =
1692 FullPath.appendComponent(getSourceFileName(Id.second));
1693 assert(AppendOk && "Could not append filename to directory!");
1694 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001695 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001696 Asm->EOL();
1697 }
1698 }
1699
1700 // Emit initial sections
1701 EmitInitial();
1702
1703 if (TimePassesIsEnabled)
1704 DebugTimer->stopTimer();
1705}
1706
1707/// EndModule - Emit all Dwarf sections that should come after the content.
1708///
1709void DwarfDebug::EndModule() {
1710 if (!ShouldEmitDwarfDebug())
1711 return;
1712
1713 if (TimePassesIsEnabled)
1714 DebugTimer->startTimer();
1715
1716 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001717 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001718 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001719 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001720 EmitLabel("data_end", 0);
1721
1722 // End text sections.
1723 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001724 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001725 EmitLabel("section_end", i);
1726 }
1727
1728 // Emit common frame information.
1729 EmitCommonDebugFrame();
1730
1731 // Emit function debug frame information
1732 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1733 E = DebugFrames.end(); I != E; ++I)
1734 EmitFunctionDebugFrame(*I);
1735
1736 // Compute DIE offsets and sizes.
1737 SizeAndOffsets();
1738
1739 // Emit all the DIEs into a debug info section
1740 EmitDebugInfo();
1741
1742 // Corresponding abbreviations into a abbrev section.
1743 EmitAbbreviations();
1744
1745 // Emit source line correspondence into a debug line section.
1746 EmitDebugLines();
1747
1748 // Emit info into a debug pubnames section.
1749 EmitDebugPubNames();
1750
1751 // Emit info into a debug str section.
1752 EmitDebugStr();
1753
1754 // Emit info into a debug loc section.
1755 EmitDebugLoc();
1756
1757 // Emit info into a debug aranges section.
1758 EmitDebugARanges();
1759
1760 // Emit info into a debug ranges section.
1761 EmitDebugRanges();
1762
1763 // Emit info into a debug macinfo section.
1764 EmitDebugMacInfo();
1765
1766 // Emit inline info.
1767 EmitDebugInlineInfo();
1768
1769 if (TimePassesIsEnabled)
1770 DebugTimer->stopTimer();
1771}
1772
Devang Patelaf9e8472009-10-01 20:31:14 +00001773/// ExtractScopeInformation - Scan machine instructions in this function
1774/// and collect DbgScopes. Return true, if atleast one scope was found.
1775bool DwarfDebug::ExtractScopeInformation(MachineFunction *MF) {
1776 // If scope information was extracted using .dbg intrinsics then there is not
1777 // any need to extract these information by scanning each instruction.
1778 if (!DbgScopeMap.empty())
1779 return false;
1780
1781 // Scan each instruction and create scopes.
1782 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1783 I != E; ++I) {
1784 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1785 II != IE; ++II) {
1786 const MachineInstr *MInsn = II;
1787 DebugLoc DL = MInsn->getDebugLoc();
1788 if (DL.isUnknown())
1789 continue;
1790 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
1791 if (!DLT.CompileUnit)
1792 continue;
1793 // There is no need to create another DIE for compile unit. For all
1794 // other scopes, create one DbgScope now. This will be translated
1795 // into a scope DIE at the end.
1796 DIDescriptor D(DLT.CompileUnit);
1797 if (!D.isCompileUnit()) {
1798 DbgScope *Scope = getDbgScope(DLT.CompileUnit, MInsn);
1799 Scope->setLastInsn(MInsn);
1800 }
1801 }
1802 }
1803
1804 // If a scope's last instruction is not set then use its child scope's
1805 // last instruction as this scope's last instrunction.
1806 for (DenseMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
1807 DE = DbgScopeMap.end(); DI != DE; ++DI) {
1808 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
1809 DI->second->FixInstructionMarkers();
1810 assert (DI->second->getLastInsn() && "Invalid last instruction!");
1811 }
1812
1813 // Each scope has first instruction and last instruction to mark beginning
1814 // and end of a scope respectively. Create an inverse map that list scopes
1815 // starts (and ends) with an instruction. One instruction may start (or end)
1816 // multiple scopes.
1817 for (DenseMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
1818 DE = DbgScopeMap.end(); DI != DE; ++DI) {
1819 DbgScope *S = DI->second;
1820 assert (S && "DbgScope is missing!");
1821 const MachineInstr *MI = S->getFirstInsn();
1822 assert (MI && "DbgScope does not have first instruction!");
1823
1824 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
1825 if (IDI != DbgScopeBeginMap.end())
1826 IDI->second.push_back(S);
1827 else
1828 DbgScopeBeginMap.insert(std::make_pair(MI,
1829 SmallVector<DbgScope *, 2>(2, S)));
1830
1831 MI = S->getLastInsn();
1832 assert (MI && "DbgScope does not have last instruction!");
1833 IDI = DbgScopeEndMap.find(MI);
1834 if (IDI != DbgScopeEndMap.end())
1835 IDI->second.push_back(S);
1836 else
1837 DbgScopeEndMap.insert(std::make_pair(MI,
1838 SmallVector<DbgScope *, 2>(2, S)));
1839 }
1840
1841 return !DbgScopeMap.empty();
1842}
1843
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001844/// BeginFunction - Gather pre-function debug information. Assumes being
1845/// emitted immediately after the function entry point.
1846void DwarfDebug::BeginFunction(MachineFunction *MF) {
1847 this->MF = MF;
1848
1849 if (!ShouldEmitDwarfDebug()) return;
1850
1851 if (TimePassesIsEnabled)
1852 DebugTimer->startTimer();
1853
1854 // Begin accumulating function debug information.
1855 MMI->BeginFunction(MF);
1856
1857 // Assumes in correct section after the entry point.
1858 EmitLabel("func_begin", ++SubprogramCount);
1859
1860 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1861 // function.
1862 DebugLoc FDL = MF->getDefaultDebugLoc();
1863 if (!FDL.isUnknown()) {
1864 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
Devang Patel3d910832009-09-30 22:51:28 +00001865 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col, DLT.CompileUnit);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001866 Asm->printLabel(LabelID);
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001867 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001868 }
1869
1870 if (TimePassesIsEnabled)
1871 DebugTimer->stopTimer();
1872}
1873
1874/// EndFunction - Gather and emit post-function debug information.
1875///
1876void DwarfDebug::EndFunction(MachineFunction *MF) {
1877 if (!ShouldEmitDwarfDebug()) return;
1878
1879 if (TimePassesIsEnabled)
1880 DebugTimer->startTimer();
1881
1882 // Define end label for subprogram.
1883 EmitLabel("func_end", SubprogramCount);
1884
1885 // Get function line info.
1886 if (!Lines.empty()) {
1887 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00001888 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001889 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
1890 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
1891 // Append the function info to section info.
1892 SectionLineInfos.insert(SectionLineInfos.end(),
1893 Lines.begin(), Lines.end());
1894 }
1895
1896 // Construct the DbgScope for abstract instances.
1897 for (SmallVector<DbgScope *, 32>::iterator
1898 I = AbstractInstanceRootList.begin(),
1899 E = AbstractInstanceRootList.end(); I != E; ++I)
Bill Wendling17956162009-05-20 23:28:48 +00001900 ConstructFunctionDbgScope(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001901
1902 // Construct scopes for subprogram.
1903 if (FunctionDbgScope)
1904 ConstructFunctionDbgScope(FunctionDbgScope);
1905 else
1906 // FIXME: This is wrong. We are essentially getting past a problem with
1907 // debug information not being able to handle unreachable blocks that have
1908 // debug information in them. In particular, those unreachable blocks that
1909 // have "region end" info in them. That situation results in the "root
1910 // scope" not being created. If that's the case, then emit a "default"
1911 // scope, i.e., one that encompasses the whole function. This isn't
1912 // desirable. And a better way of handling this (and all of the debugging
1913 // information) needs to be explored.
1914 ConstructDefaultDbgScope(MF);
1915
1916 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
1917 MMI->getFrameMoves()));
1918
1919 // Clear debug info
1920 if (FunctionDbgScope) {
1921 delete FunctionDbgScope;
1922 DbgScopeMap.clear();
Devang Patelaf9e8472009-10-01 20:31:14 +00001923 DbgScopeBeginMap.clear();
1924 DbgScopeEndMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001925 DbgAbstractScopeMap.clear();
1926 DbgConcreteScopeMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001927 FunctionDbgScope = NULL;
1928 LexicalScopeStack.clear();
1929 AbstractInstanceRootList.clear();
Devang Patel9217f792009-06-12 19:24:05 +00001930 AbstractInstanceRootMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001931 }
1932
1933 Lines.clear();
1934
1935 if (TimePassesIsEnabled)
1936 DebugTimer->stopTimer();
1937}
1938
1939/// RecordSourceLine - Records location information and associates it with a
1940/// label. Returns a unique label ID used to generate a label and provide
1941/// correspondence to the source line list.
1942unsigned DwarfDebug::RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
1943 if (TimePassesIsEnabled)
1944 DebugTimer->startTimer();
1945
1946 CompileUnit *Unit = CompileUnitMap[V];
1947 assert(Unit && "Unable to find CompileUnit");
1948 unsigned ID = MMI->NextLabelID();
1949 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
1950
1951 if (TimePassesIsEnabled)
1952 DebugTimer->stopTimer();
1953
1954 return ID;
1955}
1956
1957/// RecordSourceLine - Records location information and associates it with a
1958/// label. Returns a unique label ID used to generate a label and provide
1959/// correspondence to the source line list.
Devang Patel3d910832009-09-30 22:51:28 +00001960unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col,
1961 MDNode *Scope) {
Devang Patele4b27562009-08-28 23:24:31 +00001962 if (!MMI)
1963 return 0;
1964
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001965 if (TimePassesIsEnabled)
1966 DebugTimer->startTimer();
1967
Devang Patel3d910832009-09-30 22:51:28 +00001968 DICompileUnit CU(Scope);
Devang Patel5ccdd102009-09-29 18:40:58 +00001969 unsigned Src = GetOrCreateSourceID(CU.getDirectory(),
1970 CU.getFilename());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001971 unsigned ID = MMI->NextLabelID();
1972 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
1973
1974 if (TimePassesIsEnabled)
1975 DebugTimer->stopTimer();
1976
1977 return ID;
1978}
1979
1980/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
1981/// timed. Look up the source id with the given directory and source file
1982/// names. If none currently exists, create a new id and insert it in the
1983/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
1984/// well.
1985unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
1986 const std::string &FileName) {
1987 if (TimePassesIsEnabled)
1988 DebugTimer->startTimer();
1989
Devang Patel5ccdd102009-09-29 18:40:58 +00001990 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001991
1992 if (TimePassesIsEnabled)
1993 DebugTimer->stopTimer();
1994
1995 return SrcId;
1996}
1997
1998/// RecordRegionStart - Indicate the start of a region.
Devang Patele4b27562009-08-28 23:24:31 +00001999unsigned DwarfDebug::RecordRegionStart(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002000 if (TimePassesIsEnabled)
2001 DebugTimer->startTimer();
2002
Devang Patele4b27562009-08-28 23:24:31 +00002003 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002004 unsigned ID = MMI->NextLabelID();
2005 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
2006 LexicalScopeStack.push_back(Scope);
2007
2008 if (TimePassesIsEnabled)
2009 DebugTimer->stopTimer();
2010
2011 return ID;
2012}
2013
2014/// RecordRegionEnd - Indicate the end of a region.
Devang Patele4b27562009-08-28 23:24:31 +00002015unsigned DwarfDebug::RecordRegionEnd(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002016 if (TimePassesIsEnabled)
2017 DebugTimer->startTimer();
2018
Devang Patele4b27562009-08-28 23:24:31 +00002019 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002020 unsigned ID = MMI->NextLabelID();
2021 Scope->setEndLabelID(ID);
Devang Pateldaf9e022009-06-13 02:16:18 +00002022 // FIXME : region.end() may not be in the last basic block.
2023 // For now, do not pop last lexical scope because next basic
2024 // block may start new inlined function's body.
2025 unsigned LSSize = LexicalScopeStack.size();
2026 if (LSSize != 0 && LSSize != 1)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002027 LexicalScopeStack.pop_back();
2028
2029 if (TimePassesIsEnabled)
2030 DebugTimer->stopTimer();
2031
2032 return ID;
2033}
2034
2035/// RecordVariable - Indicate the declaration of a local variable.
Devang Patele4b27562009-08-28 23:24:31 +00002036void DwarfDebug::RecordVariable(MDNode *N, unsigned FrameIndex) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002037 if (TimePassesIsEnabled)
2038 DebugTimer->startTimer();
2039
Devang Patele4b27562009-08-28 23:24:31 +00002040 DIDescriptor Desc(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002041 DbgScope *Scope = NULL;
2042 bool InlinedFnVar = false;
2043
Devang Patele4b27562009-08-28 23:24:31 +00002044 if (Desc.getTag() == dwarf::DW_TAG_variable)
2045 Scope = getOrCreateScope(DIGlobalVariable(N).getContext().getNode());
2046 else {
Devang Patel24f20e02009-08-22 17:12:53 +00002047 bool InlinedVar = false;
Devang Patele4b27562009-08-28 23:24:31 +00002048 MDNode *Context = DIVariable(N).getContext().getNode();
2049 DISubprogram SP(Context);
Devang Patel24f20e02009-08-22 17:12:53 +00002050 if (!SP.isNull()) {
2051 // SP is inserted into DbgAbstractScopeMap when inlined function
2052 // start was recorded by RecordInlineFnStart.
Devang Patele4b27562009-08-28 23:24:31 +00002053 DenseMap<MDNode *, DbgScope *>::iterator
2054 I = DbgAbstractScopeMap.find(SP.getNode());
Devang Patel24f20e02009-08-22 17:12:53 +00002055 if (I != DbgAbstractScopeMap.end()) {
2056 InlinedVar = true;
2057 Scope = I->second;
2058 }
2059 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002060 if (!InlinedVar)
Devang Patele4b27562009-08-28 23:24:31 +00002061 Scope = getOrCreateScope(Context);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002062 }
2063
2064 assert(Scope && "Unable to find the variable's scope");
Devang Patele4b27562009-08-28 23:24:31 +00002065 DbgVariable *DV = new DbgVariable(DIVariable(N), FrameIndex, InlinedFnVar);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002066 Scope->AddVariable(DV);
2067
2068 if (TimePassesIsEnabled)
2069 DebugTimer->stopTimer();
2070}
2071
2072//// RecordInlinedFnStart - Indicate the start of inlined subroutine.
2073unsigned DwarfDebug::RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
2074 unsigned Line, unsigned Col) {
2075 unsigned LabelID = MMI->NextLabelID();
2076
Chris Lattner33adcfb2009-08-22 21:43:10 +00002077 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002078 return LabelID;
2079
2080 if (TimePassesIsEnabled)
2081 DebugTimer->startTimer();
2082
Devang Patele4b27562009-08-28 23:24:31 +00002083 MDNode *Node = SP.getNode();
2084 DenseMap<const MDNode *, DbgScope *>::iterator
2085 II = AbstractInstanceRootMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002086
2087 if (II == AbstractInstanceRootMap.end()) {
2088 // Create an abstract instance entry for this inlined function if it doesn't
2089 // already exist.
Devang Patele4b27562009-08-28 23:24:31 +00002090 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002091
2092 // Get the compile unit context.
Devang Patele4b27562009-08-28 23:24:31 +00002093 DIE *SPDie = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002094 if (!SPDie)
Devang Patel1dbc7712009-06-29 20:45:18 +00002095 SPDie = CreateSubprogramDIE(ModuleCU, SP, false, true);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002096
2097 // Mark as being inlined. This makes this subprogram entry an abstract
2098 // instance root.
2099 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
2100 // that it's defined. That probably won't change in the future. However,
2101 // this could be more elegant.
2102 AddUInt(SPDie, dwarf::DW_AT_inline, 0, dwarf::DW_INL_declared_not_inlined);
2103
2104 // Keep track of the abstract scope for this function.
Devang Patele4b27562009-08-28 23:24:31 +00002105 DbgAbstractScopeMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002106
Devang Patele4b27562009-08-28 23:24:31 +00002107 AbstractInstanceRootMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002108 AbstractInstanceRootList.push_back(Scope);
2109 }
2110
2111 // Create a concrete inlined instance for this inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00002112 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002113 DIE *ScopeDie = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel1dbc7712009-06-29 20:45:18 +00002114 ScopeDie->setAbstractCompileUnit(ModuleCU);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002115
Devang Patele4b27562009-08-28 23:24:31 +00002116 DIE *Origin = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002117 AddDIEEntry(ScopeDie, dwarf::DW_AT_abstract_origin,
2118 dwarf::DW_FORM_ref4, Origin);
Devang Patel1dbc7712009-06-29 20:45:18 +00002119 AddUInt(ScopeDie, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002120 AddUInt(ScopeDie, dwarf::DW_AT_call_line, 0, Line);
2121 AddUInt(ScopeDie, dwarf::DW_AT_call_column, 0, Col);
2122
2123 ConcreteScope->setDie(ScopeDie);
2124 ConcreteScope->setStartLabelID(LabelID);
2125 MMI->RecordUsedDbgLabel(LabelID);
2126
2127 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
2128
2129 // Keep track of the concrete scope that's inlined into this function.
Devang Patele4b27562009-08-28 23:24:31 +00002130 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
2131 SI = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002132
2133 if (SI == DbgConcreteScopeMap.end())
Devang Patele4b27562009-08-28 23:24:31 +00002134 DbgConcreteScopeMap[Node].push_back(ConcreteScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002135 else
2136 SI->second.push_back(ConcreteScope);
2137
2138 // Track the start label for this inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00002139 DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
2140 I = InlineInfo.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002141
2142 if (I == InlineInfo.end())
Devang Patele4b27562009-08-28 23:24:31 +00002143 InlineInfo[Node].push_back(LabelID);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002144 else
2145 I->second.push_back(LabelID);
2146
2147 if (TimePassesIsEnabled)
2148 DebugTimer->stopTimer();
2149
2150 return LabelID;
2151}
2152
2153/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
2154unsigned DwarfDebug::RecordInlinedFnEnd(DISubprogram &SP) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002155 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002156 return 0;
2157
2158 if (TimePassesIsEnabled)
2159 DebugTimer->startTimer();
2160
Devang Patele4b27562009-08-28 23:24:31 +00002161 MDNode *Node = SP.getNode();
2162 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
2163 I = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002164
2165 if (I == DbgConcreteScopeMap.end()) {
2166 // FIXME: Can this situation actually happen? And if so, should it?
2167 if (TimePassesIsEnabled)
2168 DebugTimer->stopTimer();
2169
2170 return 0;
2171 }
2172
2173 SmallVector<DbgScope *, 8> &Scopes = I->second;
Devang Patel11a407f2009-06-15 21:45:50 +00002174 if (Scopes.empty()) {
2175 // Returned ID is 0 if this is unbalanced "end of inlined
2176 // scope". This could happen if optimizer eats dbg intrinsics
2177 // or "beginning of inlined scope" is not recoginized due to
2178 // missing location info. In such cases, ignore this region.end.
2179 return 0;
2180 }
2181
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002182 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
2183 unsigned ID = MMI->NextLabelID();
2184 MMI->RecordUsedDbgLabel(ID);
2185 Scope->setEndLabelID(ID);
2186
2187 if (TimePassesIsEnabled)
2188 DebugTimer->stopTimer();
2189
2190 return ID;
2191}
2192
Bill Wendling829e67b2009-05-20 23:22:40 +00002193//===----------------------------------------------------------------------===//
2194// Emit Methods
2195//===----------------------------------------------------------------------===//
2196
Bill Wendling94d04b82009-05-20 23:21:38 +00002197/// SizeAndOffsetDie - Compute the size and offset of a DIE.
2198///
2199unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2200 // Get the children.
2201 const std::vector<DIE *> &Children = Die->getChildren();
2202
2203 // If not last sibling and has children then add sibling offset attribute.
2204 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2205
2206 // Record the abbreviation.
2207 AssignAbbrevNumber(Die->getAbbrev());
2208
2209 // Get the abbreviation for this DIE.
2210 unsigned AbbrevNumber = Die->getAbbrevNumber();
2211 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2212
2213 // Set DIE offset
2214 Die->setOffset(Offset);
2215
2216 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002217 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002218
2219 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2220 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2221
2222 // Size the DIE attribute values.
2223 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2224 // Size attribute value.
2225 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2226
2227 // Size the DIE children if any.
2228 if (!Children.empty()) {
2229 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2230 "Children flag not set");
2231
2232 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2233 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2234
2235 // End of children marker.
2236 Offset += sizeof(int8_t);
2237 }
2238
2239 Die->setSize(Offset - Die->getOffset());
2240 return Offset;
2241}
2242
2243/// SizeAndOffsets - Compute the size and offset of all the DIEs.
2244///
2245void DwarfDebug::SizeAndOffsets() {
2246 // Compute size of compile unit header.
2247 static unsigned Offset =
2248 sizeof(int32_t) + // Length of Compilation Unit Info
2249 sizeof(int16_t) + // DWARF version number
2250 sizeof(int32_t) + // Offset Into Abbrev. Section
2251 sizeof(int8_t); // Pointer Size (in bytes)
2252
Devang Patel1dbc7712009-06-29 20:45:18 +00002253 SizeAndOffsetDie(ModuleCU->getDie(), Offset, true);
2254 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002255}
2256
2257/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2258/// tools to recognize the object file contains Dwarf information.
2259void DwarfDebug::EmitInitial() {
2260 // Check to see if we already emitted intial headers.
2261 if (didInitial) return;
2262 didInitial = true;
2263
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002264 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002265
Bill Wendling94d04b82009-05-20 23:21:38 +00002266 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002267 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002268 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002269 EmitLabel("section_debug_frame", 0);
2270 }
2271
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002272 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002273 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002274 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002275 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002276 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002277 EmitLabel("section_aranges", 0);
2278
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002279 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2280 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002281 EmitLabel("section_macinfo", 0);
2282 }
2283
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002284 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002285 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002286 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002287 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002288 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002289 EmitLabel("section_pubnames", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002290 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002291 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002292 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002293 EmitLabel("section_ranges", 0);
2294
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002295 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002296 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002297 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002298 EmitLabel("data_begin", 0);
2299}
2300
2301/// EmitDIE - Recusively Emits a debug information entry.
2302///
2303void DwarfDebug::EmitDIE(DIE *Die) {
2304 // Get the abbreviation for this DIE.
2305 unsigned AbbrevNumber = Die->getAbbrevNumber();
2306 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2307
2308 Asm->EOL();
2309
2310 // Emit the code (index) for the abbreviation.
2311 Asm->EmitULEB128Bytes(AbbrevNumber);
2312
2313 if (Asm->isVerbose())
2314 Asm->EOL(std::string("Abbrev [" +
2315 utostr(AbbrevNumber) +
2316 "] 0x" + utohexstr(Die->getOffset()) +
2317 ":0x" + utohexstr(Die->getSize()) + " " +
2318 dwarf::TagString(Abbrev->getTag())));
2319 else
2320 Asm->EOL();
2321
2322 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2323 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2324
2325 // Emit the DIE attribute values.
2326 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2327 unsigned Attr = AbbrevData[i].getAttribute();
2328 unsigned Form = AbbrevData[i].getForm();
2329 assert(Form && "Too many attributes for DIE (check abbreviation)");
2330
2331 switch (Attr) {
2332 case dwarf::DW_AT_sibling:
2333 Asm->EmitInt32(Die->SiblingOffset());
2334 break;
2335 case dwarf::DW_AT_abstract_origin: {
2336 DIEEntry *E = cast<DIEEntry>(Values[i]);
2337 DIE *Origin = E->getEntry();
2338 unsigned Addr =
2339 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
2340 Origin->getOffset();
2341
2342 Asm->EmitInt32(Addr);
2343 break;
2344 }
2345 default:
2346 // Emit an attribute using the defined form.
2347 Values[i]->EmitValue(this, Form);
2348 break;
2349 }
2350
2351 Asm->EOL(dwarf::AttributeString(Attr));
2352 }
2353
2354 // Emit the DIE children if any.
2355 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2356 const std::vector<DIE *> &Children = Die->getChildren();
2357
2358 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2359 EmitDIE(Children[j]);
2360
2361 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2362 }
2363}
2364
2365/// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
2366///
2367void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) {
2368 DIE *Die = Unit->getDie();
2369
2370 // Emit the compile units header.
2371 EmitLabel("info_begin", Unit->getID());
2372
2373 // Emit size of content not including length itself
2374 unsigned ContentSize = Die->getSize() +
2375 sizeof(int16_t) + // DWARF version number
2376 sizeof(int32_t) + // Offset Into Abbrev. Section
2377 sizeof(int8_t) + // Pointer Size (in bytes)
2378 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2379
2380 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2381 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2382 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2383 Asm->EOL("Offset Into Abbrev. Section");
2384 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2385
2386 EmitDIE(Die);
2387 // FIXME - extra padding for gdb bug.
2388 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2389 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2390 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2391 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2392 EmitLabel("info_end", Unit->getID());
2393
2394 Asm->EOL();
2395}
2396
2397void DwarfDebug::EmitDebugInfo() {
2398 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002399 Asm->OutStreamer.SwitchSection(
2400 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002401
Devang Patel1dbc7712009-06-29 20:45:18 +00002402 EmitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002403}
2404
2405/// EmitAbbreviations - Emit the abbreviation section.
2406///
2407void DwarfDebug::EmitAbbreviations() const {
2408 // Check to see if it is worth the effort.
2409 if (!Abbreviations.empty()) {
2410 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002411 Asm->OutStreamer.SwitchSection(
2412 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002413
2414 EmitLabel("abbrev_begin", 0);
2415
2416 // For each abbrevation.
2417 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2418 // Get abbreviation data
2419 const DIEAbbrev *Abbrev = Abbreviations[i];
2420
2421 // Emit the abbrevations code (base 1 index.)
2422 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2423 Asm->EOL("Abbreviation Code");
2424
2425 // Emit the abbreviations data.
2426 Abbrev->Emit(Asm);
2427
2428 Asm->EOL();
2429 }
2430
2431 // Mark end of abbreviations.
2432 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2433
2434 EmitLabel("abbrev_end", 0);
2435 Asm->EOL();
2436 }
2437}
2438
2439/// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2440/// the line matrix.
2441///
2442void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) {
2443 // Define last address of section.
2444 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2445 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2446 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2447 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2448
2449 // Mark end of matrix.
2450 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2451 Asm->EmitULEB128Bytes(1); Asm->EOL();
2452 Asm->EmitInt8(1); Asm->EOL();
2453}
2454
2455/// EmitDebugLines - Emit source line information.
2456///
2457void DwarfDebug::EmitDebugLines() {
2458 // If the target is using .loc/.file, the assembler will be emitting the
2459 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002460 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002461 return;
2462
2463 // Minimum line delta, thus ranging from -10..(255-10).
2464 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2465 // Maximum line delta, thus ranging from -10..(255-10).
2466 const int MaxLineDelta = 255 + MinLineDelta;
2467
2468 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002469 Asm->OutStreamer.SwitchSection(
2470 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002471
2472 // Construct the section header.
2473 EmitDifference("line_end", 0, "line_begin", 0, true);
2474 Asm->EOL("Length of Source Line Info");
2475 EmitLabel("line_begin", 0);
2476
2477 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2478
2479 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2480 Asm->EOL("Prolog Length");
2481 EmitLabel("line_prolog_begin", 0);
2482
2483 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2484
2485 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2486
2487 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2488
2489 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2490
2491 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2492
2493 // Line number standard opcode encodings argument count
2494 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2495 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2496 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2497 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2498 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2499 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2500 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2501 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2502 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2503
2504 // Emit directories.
2505 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2506 Asm->EmitString(getSourceDirectoryName(DI));
2507 Asm->EOL("Directory");
2508 }
2509
2510 Asm->EmitInt8(0); Asm->EOL("End of directories");
2511
2512 // Emit files.
2513 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2514 // Remember source id starts at 1.
2515 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2516 Asm->EmitString(getSourceFileName(Id.second));
2517 Asm->EOL("Source");
2518 Asm->EmitULEB128Bytes(Id.first);
2519 Asm->EOL("Directory #");
2520 Asm->EmitULEB128Bytes(0);
2521 Asm->EOL("Mod date");
2522 Asm->EmitULEB128Bytes(0);
2523 Asm->EOL("File size");
2524 }
2525
2526 Asm->EmitInt8(0); Asm->EOL("End of files");
2527
2528 EmitLabel("line_prolog_end", 0);
2529
2530 // A sequence for each text section.
2531 unsigned SecSrcLinesSize = SectionSourceLines.size();
2532
2533 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2534 // Isolate current sections line info.
2535 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2536
Chris Lattner93b6db32009-08-08 23:39:42 +00002537 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002538 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002539 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002540 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002541 }*/
2542 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002543
2544 // Dwarf assumes we start with first line of first source file.
2545 unsigned Source = 1;
2546 unsigned Line = 1;
2547
2548 // Construct rows of the address, source, line, column matrix.
2549 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2550 const SrcLineInfo &LineInfo = LineInfos[i];
2551 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2552 if (!LabelID) continue;
2553
Caroline Ticec6f9d622009-09-11 18:25:54 +00002554 if (LineInfo.getLine() == 0) continue;
2555
Bill Wendling94d04b82009-05-20 23:21:38 +00002556 if (!Asm->isVerbose())
2557 Asm->EOL();
2558 else {
2559 std::pair<unsigned, unsigned> SourceID =
2560 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002561 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002562 << getSourceDirectoryName(SourceID.first) << ' '
2563 << getSourceFileName(SourceID.second)
2564 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2565 }
2566
2567 // Define the line address.
2568 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2569 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2570 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2571 EmitReference("label", LabelID); Asm->EOL("Location label");
2572
2573 // If change of source, then switch to the new source.
2574 if (Source != LineInfo.getSourceID()) {
2575 Source = LineInfo.getSourceID();
2576 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2577 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2578 }
2579
2580 // If change of line.
2581 if (Line != LineInfo.getLine()) {
2582 // Determine offset.
2583 int Offset = LineInfo.getLine() - Line;
2584 int Delta = Offset - MinLineDelta;
2585
2586 // Update line.
2587 Line = LineInfo.getLine();
2588
2589 // If delta is small enough and in range...
2590 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2591 // ... then use fast opcode.
2592 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2593 } else {
2594 // ... otherwise use long hand.
2595 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2596 Asm->EOL("DW_LNS_advance_line");
2597 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2598 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2599 }
2600 } else {
2601 // Copy the previous row (different address or source)
2602 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2603 }
2604 }
2605
2606 EmitEndOfLineMatrix(j + 1);
2607 }
2608
2609 if (SecSrcLinesSize == 0)
2610 // Because we're emitting a debug_line section, we still need a line
2611 // table. The linker and friends expect it to exist. If there's nothing to
2612 // put into it, emit an empty table.
2613 EmitEndOfLineMatrix(1);
2614
2615 EmitLabel("line_end", 0);
2616 Asm->EOL();
2617}
2618
2619/// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2620///
2621void DwarfDebug::EmitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002622 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002623 return;
2624
2625 int stackGrowth =
2626 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2627 TargetFrameInfo::StackGrowsUp ?
2628 TD->getPointerSize() : -TD->getPointerSize();
2629
2630 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002631 Asm->OutStreamer.SwitchSection(
2632 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002633
2634 EmitLabel("debug_frame_common", 0);
2635 EmitDifference("debug_frame_common_end", 0,
2636 "debug_frame_common_begin", 0, true);
2637 Asm->EOL("Length of Common Information Entry");
2638
2639 EmitLabel("debug_frame_common_begin", 0);
2640 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2641 Asm->EOL("CIE Identifier Tag");
2642 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2643 Asm->EOL("CIE Version");
2644 Asm->EmitString("");
2645 Asm->EOL("CIE Augmentation");
2646 Asm->EmitULEB128Bytes(1);
2647 Asm->EOL("CIE Code Alignment Factor");
2648 Asm->EmitSLEB128Bytes(stackGrowth);
2649 Asm->EOL("CIE Data Alignment Factor");
2650 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2651 Asm->EOL("CIE RA Column");
2652
2653 std::vector<MachineMove> Moves;
2654 RI->getInitialFrameState(Moves);
2655
2656 EmitFrameMoves(NULL, 0, Moves, false);
2657
2658 Asm->EmitAlignment(2, 0, 0, false);
2659 EmitLabel("debug_frame_common_end", 0);
2660
2661 Asm->EOL();
2662}
2663
2664/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2665/// section.
2666void
2667DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002668 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002669 return;
2670
2671 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002672 Asm->OutStreamer.SwitchSection(
2673 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002674
2675 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2676 "debug_frame_begin", DebugFrameInfo.Number, true);
2677 Asm->EOL("Length of Frame Information Entry");
2678
2679 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2680
2681 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2682 0, 0, true, false);
2683 Asm->EOL("FDE CIE offset");
2684
2685 EmitReference("func_begin", DebugFrameInfo.Number);
2686 Asm->EOL("FDE initial location");
2687 EmitDifference("func_end", DebugFrameInfo.Number,
2688 "func_begin", DebugFrameInfo.Number);
2689 Asm->EOL("FDE address range");
2690
2691 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2692 false);
2693
2694 Asm->EmitAlignment(2, 0, 0, false);
2695 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2696
2697 Asm->EOL();
2698}
2699
2700void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2701 EmitDifference("pubnames_end", Unit->getID(),
2702 "pubnames_begin", Unit->getID(), true);
2703 Asm->EOL("Length of Public Names Info");
2704
2705 EmitLabel("pubnames_begin", Unit->getID());
2706
2707 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2708
2709 EmitSectionOffset("info_begin", "section_info",
2710 Unit->getID(), 0, true, false);
2711 Asm->EOL("Offset of Compilation Unit Info");
2712
2713 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2714 true);
2715 Asm->EOL("Compilation Unit Length");
2716
2717 StringMap<DIE*> &Globals = Unit->getGlobals();
2718 for (StringMap<DIE*>::const_iterator
2719 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2720 const char *Name = GI->getKeyData();
2721 DIE * Entity = GI->second;
2722
2723 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2724 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2725 }
2726
2727 Asm->EmitInt32(0); Asm->EOL("End Mark");
2728 EmitLabel("pubnames_end", Unit->getID());
2729
2730 Asm->EOL();
2731}
2732
2733/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2734///
2735void DwarfDebug::EmitDebugPubNames() {
2736 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002737 Asm->OutStreamer.SwitchSection(
2738 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002739
Devang Patel1dbc7712009-06-29 20:45:18 +00002740 EmitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002741}
2742
2743/// EmitDebugStr - Emit visible names into a debug str section.
2744///
2745void DwarfDebug::EmitDebugStr() {
2746 // Check to see if it is worth the effort.
2747 if (!StringPool.empty()) {
2748 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002749 Asm->OutStreamer.SwitchSection(
2750 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002751
2752 // For each of strings in the string pool.
2753 for (unsigned StringID = 1, N = StringPool.size();
2754 StringID <= N; ++StringID) {
2755 // Emit a label for reference from debug information entries.
2756 EmitLabel("string", StringID);
2757
2758 // Emit the string itself.
2759 const std::string &String = StringPool[StringID];
2760 Asm->EmitString(String); Asm->EOL();
2761 }
2762
2763 Asm->EOL();
2764 }
2765}
2766
2767/// EmitDebugLoc - Emit visible names into a debug loc section.
2768///
2769void DwarfDebug::EmitDebugLoc() {
2770 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002771 Asm->OutStreamer.SwitchSection(
2772 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002773 Asm->EOL();
2774}
2775
2776/// EmitDebugARanges - Emit visible names into a debug aranges section.
2777///
2778void DwarfDebug::EmitDebugARanges() {
2779 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002780 Asm->OutStreamer.SwitchSection(
2781 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002782
2783 // FIXME - Mock up
2784#if 0
2785 CompileUnit *Unit = GetBaseCompileUnit();
2786
2787 // Don't include size of length
2788 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2789
2790 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2791
2792 EmitReference("info_begin", Unit->getID());
2793 Asm->EOL("Offset of Compilation Unit Info");
2794
2795 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2796
2797 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2798
2799 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2800 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2801
2802 // Range 1
2803 EmitReference("text_begin", 0); Asm->EOL("Address");
2804 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2805
2806 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2807 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2808#endif
2809
2810 Asm->EOL();
2811}
2812
2813/// EmitDebugRanges - Emit visible names into a debug ranges section.
2814///
2815void DwarfDebug::EmitDebugRanges() {
2816 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002817 Asm->OutStreamer.SwitchSection(
2818 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002819 Asm->EOL();
2820}
2821
2822/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2823///
2824void DwarfDebug::EmitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002825 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002826 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002827 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002828 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002829 Asm->EOL();
2830 }
2831}
2832
2833/// EmitDebugInlineInfo - Emit inline info using following format.
2834/// Section Header:
2835/// 1. length of section
2836/// 2. Dwarf version number
2837/// 3. address size.
2838///
2839/// Entries (one "entry" for each function that was inlined):
2840///
2841/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2842/// otherwise offset into __debug_str for regular function name.
2843/// 2. offset into __debug_str section for regular function name.
2844/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2845/// instances for the function.
2846///
2847/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2848/// inlined instance; the die_offset points to the inlined_subroutine die in the
2849/// __debug_info section, and the low_pc is the starting address for the
2850/// inlining instance.
2851void DwarfDebug::EmitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002852 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002853 return;
2854
Devang Patel1dbc7712009-06-29 20:45:18 +00002855 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002856 return;
2857
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002858 Asm->OutStreamer.SwitchSection(
2859 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002860 Asm->EOL();
2861 EmitDifference("debug_inlined_end", 1,
2862 "debug_inlined_begin", 1, true);
2863 Asm->EOL("Length of Debug Inlined Information Entry");
2864
2865 EmitLabel("debug_inlined_begin", 1);
2866
2867 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2868 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2869
Devang Patele4b27562009-08-28 23:24:31 +00002870 for (DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
Bill Wendling94d04b82009-05-20 23:21:38 +00002871 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
Devang Patele4b27562009-08-28 23:24:31 +00002872 MDNode *Node = I->first;
Bill Wendling94d04b82009-05-20 23:21:38 +00002873 SmallVector<unsigned, 4> &Labels = I->second;
Devang Patele4b27562009-08-28 23:24:31 +00002874 DISubprogram SP(Node);
Devang Patel5ccdd102009-09-29 18:40:58 +00002875 const char *LName = SP.getLinkageName();
2876 const char *Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002877
Devang Patel5ccdd102009-09-29 18:40:58 +00002878 if (!LName)
Devang Patel53cb17d2009-07-16 01:01:22 +00002879 Asm->EmitString(Name);
2880 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002881 // Skip special LLVM prefix that is used to inform the asm printer to not
2882 // emit usual symbol prefix before the symbol name. This happens for
2883 // Objective-C symbol names and symbol whose name is replaced using GCC's
2884 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002885 if (LName[0] == 1)
2886 LName = &LName[1];
2887 Asm->EmitString(LName);
2888 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002889 Asm->EOL("MIPS linkage name");
2890
2891 Asm->EmitString(Name); Asm->EOL("Function name");
2892
2893 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2894
2895 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2896 LE = Labels.end(); LI != LE; ++LI) {
Devang Patele4b27562009-08-28 23:24:31 +00002897 DIE *SP = ModuleCU->getDieMapSlotFor(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00002898 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2899
2900 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002901 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002902 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002903 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002904
2905 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2906 }
2907 }
2908
2909 EmitLabel("debug_inlined_end", 1);
2910 Asm->EOL();
2911}