blob: dc4c9096ab00963e87f87cd16d6273ace03fa72d [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Devang Patele4b27562009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
Chris Lattner334fd1f2009-09-16 00:08:41 +000028#include "llvm/Support/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/Support/Timer.h"
30#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000031using namespace llvm;
32
33static TimerGroup &getDwarfTimerGroup() {
34 static TimerGroup DwarfTimerGroup("Dwarf Debugging");
35 return DwarfTimerGroup;
36}
37
38//===----------------------------------------------------------------------===//
39
40/// Configuration values for initial hash set sizes (log2).
41///
42static const unsigned InitDiesSetSize = 9; // log2(512)
43static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
44static const unsigned InitValuesSetSize = 9; // log2(512)
45
46namespace llvm {
47
48//===----------------------------------------------------------------------===//
49/// CompileUnit - This dwarf writer support class manages information associate
50/// with a source file.
51class VISIBILITY_HIDDEN CompileUnit {
52 /// ID - File identifier for source.
53 ///
54 unsigned ID;
55
56 /// Die - Compile unit debug information entry.
57 ///
58 DIE *Die;
59
60 /// GVToDieMap - Tracks the mapping of unit level debug informaton
61 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000062 /// FIXME : Rename GVToDieMap -> NodeToDieMap
63 std::map<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000064
65 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
66 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000067 /// FIXME : Rename
68 std::map<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000069
70 /// Globals - A map of globally visible named entities for this unit.
71 ///
72 StringMap<DIE*> Globals;
73
74 /// DiesSet - Used to uniquely define dies within the compile unit.
75 ///
76 FoldingSet<DIE> DiesSet;
77public:
78 CompileUnit(unsigned I, DIE *D)
Bill Wendling39dd6962009-05-20 23:31:45 +000079 : ID(I), Die(D), DiesSet(InitDiesSetSize) {}
80 ~CompileUnit() { delete Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000081
82 // Accessors.
Bill Wendling39dd6962009-05-20 23:31:45 +000083 unsigned getID() const { return ID; }
84 DIE* getDie() const { return Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000085 StringMap<DIE*> &getGlobals() { return Globals; }
86
87 /// hasContent - Return true if this compile unit has something to write out.
88 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000089 bool hasContent() const { return !Die->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +000090
91 /// AddGlobal - Add a new global entity to the compile unit.
92 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000093 void AddGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000094
95 /// getDieMapSlotFor - Returns the debug information entry map slot for the
96 /// specified debug variable.
Devang Patele4b27562009-08-28 23:24:31 +000097 DIE *&getDieMapSlotFor(MDNode *N) { return GVToDieMap[N]; }
Bill Wendling0310d762009-05-15 09:23:25 +000098
Chris Lattner1cda87c2009-07-14 04:50:12 +000099 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for
100 /// the specified debug variable.
Devang Patele4b27562009-08-28 23:24:31 +0000101 DIEEntry *&getDIEEntrySlotFor(MDNode *N) {
102 return GVToDIEEntryMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +0000103 }
104
105 /// AddDie - Adds or interns the DIE to the compile unit.
106 ///
107 DIE *AddDie(DIE &Buffer) {
108 FoldingSetNodeID ID;
109 Buffer.Profile(ID);
110 void *Where;
111 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
112
113 if (!Die) {
114 Die = new DIE(Buffer);
115 DiesSet.InsertNode(Die, Where);
116 this->Die->AddChild(Die);
117 Buffer.Detach();
118 }
119
120 return Die;
121 }
122};
123
124//===----------------------------------------------------------------------===//
125/// DbgVariable - This class is used to track local variable information.
126///
Jeffrey Yasskin36d52bf2009-11-03 06:29:36 +0000127class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000128 DIVariable Var; // Variable Descriptor.
129 unsigned FrameIndex; // Variable frame index.
Bill Wendling1180c782009-05-18 23:08:55 +0000130 bool InlinedFnVar; // Variable for an inlined function.
Bill Wendling0310d762009-05-15 09:23:25 +0000131public:
Bill Wendling1180c782009-05-18 23:08:55 +0000132 DbgVariable(DIVariable V, unsigned I, bool IFV)
133 : Var(V), FrameIndex(I), InlinedFnVar(IFV) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000134
135 // Accessors.
Bill Wendling1180c782009-05-18 23:08:55 +0000136 DIVariable getVariable() const { return Var; }
Bill Wendling0310d762009-05-15 09:23:25 +0000137 unsigned getFrameIndex() const { return FrameIndex; }
Bill Wendling1180c782009-05-18 23:08:55 +0000138 bool isInlinedFnVar() const { return InlinedFnVar; }
Bill Wendling0310d762009-05-15 09:23:25 +0000139};
140
141//===----------------------------------------------------------------------===//
142/// DbgScope - This class is used to track scope information.
143///
144class DbgConcreteScope;
Jeffrey Yasskin36d52bf2009-11-03 06:29:36 +0000145class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000146 DbgScope *Parent; // Parent to this scope.
147 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelc90aefe2009-10-14 21:08:09 +0000148 // FIXME use WeakVH for Desc.
149 WeakVH InlinedAt; // If this scope represents inlined
150 // function body then this is the location
151 // where this body is inlined.
Bill Wendling0310d762009-05-15 09:23:25 +0000152 unsigned StartLabelID; // Label ID of the beginning of scope.
153 unsigned EndLabelID; // Label ID of the end of scope.
Devang Pateld38dd112009-10-01 18:25:23 +0000154 const MachineInstr *LastInsn; // Last instruction of this scope.
155 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000156 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
157 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
158 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000159
Owen Anderson04c05f72009-06-24 22:53:20 +0000160 // Private state for dump()
161 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000162public:
Devang Patelc90aefe2009-10-14 21:08:09 +0000163 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
164 : Parent(P), Desc(D), InlinedAt(I), StartLabelID(0), EndLabelID(0),
165 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000166 virtual ~DbgScope();
167
168 // Accessors.
169 DbgScope *getParent() const { return Parent; }
170 DIDescriptor getDesc() const { return Desc; }
Devang Patelc90aefe2009-10-14 21:08:09 +0000171 MDNode *getInlinedAt() const {
172 return dyn_cast_or_null<MDNode>(InlinedAt);
173 }
Bill Wendling0310d762009-05-15 09:23:25 +0000174 unsigned getStartLabelID() const { return StartLabelID; }
175 unsigned getEndLabelID() const { return EndLabelID; }
176 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
177 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
178 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; }
179 void setStartLabelID(unsigned S) { StartLabelID = S; }
180 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Pateld38dd112009-10-01 18:25:23 +0000181 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
182 const MachineInstr *getLastInsn() { return LastInsn; }
183 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
184 const MachineInstr *getFirstInsn() { return FirstInsn; }
Bill Wendling0310d762009-05-15 09:23:25 +0000185 /// AddScope - Add a scope to the scope.
186 ///
187 void AddScope(DbgScope *S) { Scopes.push_back(S); }
188
189 /// AddVariable - Add a variable to the scope.
190 ///
191 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
192
193 /// AddConcreteInst - Add a concrete instance to the scope.
194 ///
195 void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); }
196
Devang Patelaf9e8472009-10-01 20:31:14 +0000197 void FixInstructionMarkers() {
198 assert (getFirstInsn() && "First instruction is missing!");
199 if (getLastInsn())
200 return;
201
202 // If a scope does not have an instruction to mark an end then use
203 // the end of last child scope.
204 SmallVector<DbgScope *, 4> &Scopes = getScopes();
205 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
206 DbgScope *L = Scopes.back();
207 if (!L->getLastInsn())
208 L->FixInstructionMarkers();
209 setLastInsn(L->getLastInsn());
210 }
211
Bill Wendling0310d762009-05-15 09:23:25 +0000212#ifndef NDEBUG
213 void dump() const;
214#endif
215};
216
217#ifndef NDEBUG
218void DbgScope::dump() const {
Chris Lattnerc281de12009-08-23 00:51:00 +0000219 raw_ostream &err = errs();
220 err.indent(IndentLevel);
221 Desc.dump();
222 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000223
224 IndentLevel += 2;
225
226 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
227 if (Scopes[i] != this)
228 Scopes[i]->dump();
229
230 IndentLevel -= 2;
231}
232#endif
233
234//===----------------------------------------------------------------------===//
235/// DbgConcreteScope - This class is used to track a scope that holds concrete
236/// instance information.
237///
238class VISIBILITY_HIDDEN DbgConcreteScope : public DbgScope {
239 CompileUnit *Unit;
240 DIE *Die; // Debug info for this concrete scope.
241public:
242 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
243
244 // Accessors.
245 DIE *getDie() const { return Die; }
246 void setDie(DIE *D) { Die = D; }
247};
248
249DbgScope::~DbgScope() {
250 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
251 delete Scopes[i];
252 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
253 delete Variables[j];
254 for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k)
255 delete ConcreteInsts[k];
256}
257
258} // end llvm namespace
259
Chris Lattneraf76e592009-08-22 20:48:53 +0000260DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000261 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000262 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000263 ValuesSet(InitValuesSetSize), Values(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000264 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel43da8fb2009-07-13 21:26:33 +0000265 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000266 if (TimePassesIsEnabled)
267 DebugTimer = new Timer("Dwarf Debug Writer",
268 getDwarfTimerGroup());
269}
270DwarfDebug::~DwarfDebug() {
271 for (unsigned j = 0, M = Values.size(); j < M; ++j)
272 delete Values[j];
273
Devang Patele4b27562009-08-28 23:24:31 +0000274 for (DenseMap<const MDNode *, DbgScope *>::iterator
Bill Wendling0310d762009-05-15 09:23:25 +0000275 I = AbstractInstanceRootMap.begin(),
276 E = AbstractInstanceRootMap.end(); I != E;++I)
277 delete I->second;
278
279 delete DebugTimer;
280}
281
282/// AssignAbbrevNumber - Define a unique number for the abbreviation.
283///
284void DwarfDebug::AssignAbbrevNumber(DIEAbbrev &Abbrev) {
285 // Profile the node so that we can make it unique.
286 FoldingSetNodeID ID;
287 Abbrev.Profile(ID);
288
289 // Check the set for priors.
290 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
291
292 // If it's newly added.
293 if (InSet == &Abbrev) {
294 // Add to abbreviation list.
295 Abbreviations.push_back(&Abbrev);
296
297 // Assign the vector position + 1 as its number.
298 Abbrev.setNumber(Abbreviations.size());
299 } else {
300 // Assign existing abbreviation number.
301 Abbrev.setNumber(InSet->getNumber());
302 }
303}
304
Bill Wendling995f80a2009-05-20 23:24:48 +0000305/// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
306/// information entry.
307DIEEntry *DwarfDebug::CreateDIEEntry(DIE *Entry) {
Bill Wendling0310d762009-05-15 09:23:25 +0000308 DIEEntry *Value;
309
310 if (Entry) {
311 FoldingSetNodeID ID;
312 DIEEntry::Profile(ID, Entry);
313 void *Where;
314 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
315
316 if (Value) return Value;
317
318 Value = new DIEEntry(Entry);
319 ValuesSet.InsertNode(Value, Where);
320 } else {
321 Value = new DIEEntry(Entry);
322 }
323
324 Values.push_back(Value);
325 return Value;
326}
327
328/// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
329///
330void DwarfDebug::SetDIEEntry(DIEEntry *Value, DIE *Entry) {
331 Value->setEntry(Entry);
332
333 // Add to values set if not already there. If it is, we merely have a
334 // duplicate in the values list (no harm.)
335 ValuesSet.GetOrInsertNode(Value);
336}
337
338/// AddUInt - Add an unsigned integer attribute data and value.
339///
340void DwarfDebug::AddUInt(DIE *Die, unsigned Attribute,
341 unsigned Form, uint64_t Integer) {
342 if (!Form) Form = DIEInteger::BestForm(false, Integer);
343
344 FoldingSetNodeID ID;
345 DIEInteger::Profile(ID, Integer);
346 void *Where;
347 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
348
349 if (!Value) {
350 Value = new DIEInteger(Integer);
351 ValuesSet.InsertNode(Value, Where);
352 Values.push_back(Value);
353 }
354
355 Die->AddValue(Attribute, Form, Value);
356}
357
358/// AddSInt - Add an signed integer attribute data and value.
359///
360void DwarfDebug::AddSInt(DIE *Die, unsigned Attribute,
361 unsigned Form, int64_t Integer) {
362 if (!Form) Form = DIEInteger::BestForm(true, Integer);
363
364 FoldingSetNodeID ID;
365 DIEInteger::Profile(ID, (uint64_t)Integer);
366 void *Where;
367 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
368
369 if (!Value) {
370 Value = new DIEInteger(Integer);
371 ValuesSet.InsertNode(Value, Where);
372 Values.push_back(Value);
373 }
374
375 Die->AddValue(Attribute, Form, Value);
376}
377
378/// AddString - Add a string attribute data and value.
379///
380void DwarfDebug::AddString(DIE *Die, unsigned Attribute, unsigned Form,
381 const std::string &String) {
382 FoldingSetNodeID ID;
383 DIEString::Profile(ID, String);
384 void *Where;
385 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
386
387 if (!Value) {
388 Value = new DIEString(String);
389 ValuesSet.InsertNode(Value, Where);
390 Values.push_back(Value);
391 }
392
393 Die->AddValue(Attribute, Form, Value);
394}
395
396/// AddLabel - Add a Dwarf label attribute data and value.
397///
398void DwarfDebug::AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
399 const DWLabel &Label) {
400 FoldingSetNodeID ID;
401 DIEDwarfLabel::Profile(ID, Label);
402 void *Where;
403 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
404
405 if (!Value) {
406 Value = new DIEDwarfLabel(Label);
407 ValuesSet.InsertNode(Value, Where);
408 Values.push_back(Value);
409 }
410
411 Die->AddValue(Attribute, Form, Value);
412}
413
414/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
415///
416void DwarfDebug::AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
417 const std::string &Label) {
418 FoldingSetNodeID ID;
419 DIEObjectLabel::Profile(ID, Label);
420 void *Where;
421 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
422
423 if (!Value) {
424 Value = new DIEObjectLabel(Label);
425 ValuesSet.InsertNode(Value, Where);
426 Values.push_back(Value);
427 }
428
429 Die->AddValue(Attribute, Form, Value);
430}
431
432/// AddSectionOffset - Add a section offset label attribute data and value.
433///
434void DwarfDebug::AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
435 const DWLabel &Label, const DWLabel &Section,
436 bool isEH, bool useSet) {
437 FoldingSetNodeID ID;
438 DIESectionOffset::Profile(ID, Label, Section);
439 void *Where;
440 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
441
442 if (!Value) {
443 Value = new DIESectionOffset(Label, Section, isEH, useSet);
444 ValuesSet.InsertNode(Value, Where);
445 Values.push_back(Value);
446 }
447
448 Die->AddValue(Attribute, Form, Value);
449}
450
451/// AddDelta - Add a label delta attribute data and value.
452///
453void DwarfDebug::AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
454 const DWLabel &Hi, const DWLabel &Lo) {
455 FoldingSetNodeID ID;
456 DIEDelta::Profile(ID, Hi, Lo);
457 void *Where;
458 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
459
460 if (!Value) {
461 Value = new DIEDelta(Hi, Lo);
462 ValuesSet.InsertNode(Value, Where);
463 Values.push_back(Value);
464 }
465
466 Die->AddValue(Attribute, Form, Value);
467}
468
469/// AddBlock - Add block data.
470///
471void DwarfDebug::AddBlock(DIE *Die, unsigned Attribute, unsigned Form,
472 DIEBlock *Block) {
473 Block->ComputeSize(TD);
474 FoldingSetNodeID ID;
475 Block->Profile(ID);
476 void *Where;
477 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
478
479 if (!Value) {
480 Value = Block;
481 ValuesSet.InsertNode(Value, Where);
482 Values.push_back(Value);
483 } else {
484 // Already exists, reuse the previous one.
485 delete Block;
486 Block = cast<DIEBlock>(Value);
487 }
488
489 Die->AddValue(Attribute, Block->BestForm(), Value);
490}
491
492/// AddSourceLine - Add location information to specified debug information
493/// entry.
494void DwarfDebug::AddSourceLine(DIE *Die, const DIVariable *V) {
495 // If there is no compile unit specified, don't add a line #.
496 if (V->getCompileUnit().isNull())
497 return;
498
499 unsigned Line = V->getLineNumber();
500 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
501 assert(FileID && "Invalid file id");
502 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
503 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
504}
505
506/// AddSourceLine - Add location information to specified debug information
507/// entry.
508void DwarfDebug::AddSourceLine(DIE *Die, const DIGlobal *G) {
509 // If there is no compile unit specified, don't add a line #.
510 if (G->getCompileUnit().isNull())
511 return;
512
513 unsigned Line = G->getLineNumber();
514 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
515 assert(FileID && "Invalid file id");
516 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
517 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
518}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000519
520/// AddSourceLine - Add location information to specified debug information
521/// entry.
522void DwarfDebug::AddSourceLine(DIE *Die, const DISubprogram *SP) {
523 // If there is no compile unit specified, don't add a line #.
524 if (SP->getCompileUnit().isNull())
525 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000526 // If the line number is 0, don't add it.
527 if (SP->getLineNumber() == 0)
528 return;
529
Devang Patel82dfc0c2009-08-31 22:47:13 +0000530
531 unsigned Line = SP->getLineNumber();
532 unsigned FileID = FindCompileUnit(SP->getCompileUnit()).getID();
533 assert(FileID && "Invalid file id");
534 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
535 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
536}
537
538/// AddSourceLine - Add location information to specified debug information
539/// entry.
Bill Wendling0310d762009-05-15 09:23:25 +0000540void DwarfDebug::AddSourceLine(DIE *Die, const DIType *Ty) {
541 // If there is no compile unit specified, don't add a line #.
542 DICompileUnit CU = Ty->getCompileUnit();
543 if (CU.isNull())
544 return;
545
546 unsigned Line = Ty->getLineNumber();
547 unsigned FileID = FindCompileUnit(CU).getID();
548 assert(FileID && "Invalid file id");
549 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
550 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
551}
552
Caroline Ticedc8f6042009-08-31 21:19:37 +0000553/* Byref variables, in Blocks, are declared by the programmer as
554 "SomeType VarName;", but the compiler creates a
555 __Block_byref_x_VarName struct, and gives the variable VarName
556 either the struct, or a pointer to the struct, as its type. This
557 is necessary for various behind-the-scenes things the compiler
558 needs to do with by-reference variables in blocks.
559
560 However, as far as the original *programmer* is concerned, the
561 variable should still have type 'SomeType', as originally declared.
562
563 The following function dives into the __Block_byref_x_VarName
564 struct to find the original type of the variable. This will be
565 passed back to the code generating the type for the Debug
566 Information Entry for the variable 'VarName'. 'VarName' will then
567 have the original type 'SomeType' in its debug information.
568
569 The original type 'SomeType' will be the type of the field named
570 'VarName' inside the __Block_byref_x_VarName struct.
571
572 NOTE: In order for this to not completely fail on the debugger
573 side, the Debug Information Entry for the variable VarName needs to
574 have a DW_AT_location that tells the debugger how to unwind through
575 the pointers and __Block_byref_x_VarName struct to find the actual
576 value of the variable. The function AddBlockByrefType does this. */
577
578/// Find the type the programmer originally declared the variable to be
579/// and return that type.
580///
581DIType DwarfDebug::GetBlockByrefType(DIType Ty, std::string Name) {
582
583 DIType subType = Ty;
584 unsigned tag = Ty.getTag();
585
586 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000587 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000588 subType = DTy.getTypeDerivedFrom();
589 }
590
591 DICompositeType blockStruct = DICompositeType(subType.getNode());
592
593 DIArray Elements = blockStruct.getTypeArray();
594
595 if (Elements.isNull())
596 return Ty;
597
598 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
599 DIDescriptor Element = Elements.getElement(i);
600 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel5ccdd102009-09-29 18:40:58 +0000601 if (strcmp(Name.c_str(), DT.getName()) == 0)
Caroline Ticedc8f6042009-08-31 21:19:37 +0000602 return (DT.getTypeDerivedFrom());
603 }
604
605 return Ty;
606}
607
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000608/// AddComplexAddress - Start with the address based on the location provided,
609/// and generate the DWARF information necessary to find the actual variable
610/// given the extra address information encoded in the DIVariable, starting from
611/// the starting location. Add the DWARF information to the die.
612///
613void DwarfDebug::AddComplexAddress(DbgVariable *&DV, DIE *Die,
614 unsigned Attribute,
615 const MachineLocation &Location) {
616 const DIVariable &VD = DV->getVariable();
617 DIType Ty = VD.getType();
618
619 // Decode the original location, and use that as the start of the byref
620 // variable's location.
621 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
622 DIEBlock *Block = new DIEBlock();
623
624 if (Location.isReg()) {
625 if (Reg < 32) {
626 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
627 } else {
628 Reg = Reg - dwarf::DW_OP_reg0;
629 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
630 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
631 }
632 } else {
633 if (Reg < 32)
634 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
635 else {
636 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
637 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
638 }
639
640 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
641 }
642
643 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
644 uint64_t Element = VD.getAddrElement(i);
645
646 if (Element == DIFactory::OpPlus) {
647 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
648 AddUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
649 } else if (Element == DIFactory::OpDeref) {
650 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
651 } else llvm_unreachable("unknown DIFactory Opcode");
652 }
653
654 // Now attach the location information to the DIE.
655 AddBlock(Die, Attribute, 0, Block);
656}
657
Caroline Ticedc8f6042009-08-31 21:19:37 +0000658/* Byref variables, in Blocks, are declared by the programmer as "SomeType
659 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
660 gives the variable VarName either the struct, or a pointer to the struct, as
661 its type. This is necessary for various behind-the-scenes things the
662 compiler needs to do with by-reference variables in Blocks.
663
664 However, as far as the original *programmer* is concerned, the variable
665 should still have type 'SomeType', as originally declared.
666
667 The function GetBlockByrefType dives into the __Block_byref_x_VarName
668 struct to find the original type of the variable, which is then assigned to
669 the variable's Debug Information Entry as its real type. So far, so good.
670 However now the debugger will expect the variable VarName to have the type
671 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000672 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000673 pointers and struct to find the actual variable of type SomeType.
674
675 The following function does just that. We start by getting
676 the "normal" location for the variable. This will be the location
677 of either the struct __Block_byref_x_VarName or the pointer to the
678 struct __Block_byref_x_VarName.
679
680 The struct will look something like:
681
682 struct __Block_byref_x_VarName {
683 ... <various fields>
684 struct __Block_byref_x_VarName *forwarding;
685 ... <various other fields>
686 SomeType VarName;
687 ... <maybe more fields>
688 };
689
690 If we are given the struct directly (as our starting point) we
691 need to tell the debugger to:
692
693 1). Add the offset of the forwarding field.
694
695 2). Follow that pointer to get the the real __Block_byref_x_VarName
696 struct to use (the real one may have been copied onto the heap).
697
698 3). Add the offset for the field VarName, to find the actual variable.
699
700 If we started with a pointer to the struct, then we need to
701 dereference that pointer first, before the other steps.
702 Translating this into DWARF ops, we will need to append the following
703 to the current location description for the variable:
704
705 DW_OP_deref -- optional, if we start with a pointer
706 DW_OP_plus_uconst <forward_fld_offset>
707 DW_OP_deref
708 DW_OP_plus_uconst <varName_fld_offset>
709
710 That is what this function does. */
711
712/// AddBlockByrefAddress - Start with the address based on the location
713/// provided, and generate the DWARF information necessary to find the
714/// actual Block variable (navigating the Block struct) based on the
715/// starting location. Add the DWARF information to the die. For
716/// more information, read large comment just above here.
717///
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000718void DwarfDebug::AddBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000719 unsigned Attribute,
720 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000721 const DIVariable &VD = DV->getVariable();
722 DIType Ty = VD.getType();
723 DIType TmpTy = Ty;
724 unsigned Tag = Ty.getTag();
725 bool isPointer = false;
726
Devang Patel5ccdd102009-09-29 18:40:58 +0000727 const char *varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000728
729 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000730 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000731 TmpTy = DTy.getTypeDerivedFrom();
732 isPointer = true;
733 }
734
735 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
736
Daniel Dunbar00564992009-09-19 20:40:14 +0000737 // Find the __forwarding field and the variable field in the __Block_byref
738 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000739 DIArray Fields = blockStruct.getTypeArray();
740 DIDescriptor varField = DIDescriptor();
741 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000742
743
Daniel Dunbar00564992009-09-19 20:40:14 +0000744 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
745 DIDescriptor Element = Fields.getElement(i);
746 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel5ccdd102009-09-29 18:40:58 +0000747 const char *fieldName = DT.getName();
748 if (strcmp(fieldName, "__forwarding") == 0)
Daniel Dunbar00564992009-09-19 20:40:14 +0000749 forwardingField = Element;
Devang Patel5ccdd102009-09-29 18:40:58 +0000750 else if (strcmp(fieldName, varName) == 0)
Daniel Dunbar00564992009-09-19 20:40:14 +0000751 varField = Element;
752 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000753
Mike Stump7e3720d2009-09-24 23:21:26 +0000754 assert(!varField.isNull() && "Can't find byref variable in Block struct");
755 assert(!forwardingField.isNull()
756 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000757
Daniel Dunbar00564992009-09-19 20:40:14 +0000758 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000759 unsigned int forwardingFieldOffset =
760 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
761 unsigned int varFieldOffset =
762 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000763
Mike Stump7e3720d2009-09-24 23:21:26 +0000764 // Decode the original location, and use that as the start of the byref
765 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000766 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
767 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000768
Daniel Dunbar00564992009-09-19 20:40:14 +0000769 if (Location.isReg()) {
770 if (Reg < 32)
771 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
772 else {
773 Reg = Reg - dwarf::DW_OP_reg0;
774 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
775 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
776 }
777 } else {
778 if (Reg < 32)
779 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
780 else {
781 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
782 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
783 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000784
Daniel Dunbar00564992009-09-19 20:40:14 +0000785 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
786 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000787
Mike Stump7e3720d2009-09-24 23:21:26 +0000788 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000789 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000790 if (isPointer)
791 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000792
Daniel Dunbar00564992009-09-19 20:40:14 +0000793 // Next add the offset for the '__forwarding' field:
794 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
795 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000796 if (forwardingFieldOffset > 0) {
797 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
798 AddUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
799 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000800
Daniel Dunbar00564992009-09-19 20:40:14 +0000801 // Now dereference the __forwarding field to get to the real __Block_byref
802 // struct: DW_OP_deref.
Daniel Dunbar00564992009-09-19 20:40:14 +0000803 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000804
Daniel Dunbar00564992009-09-19 20:40:14 +0000805 // Now that we've got the real __Block_byref... struct, add the offset
806 // for the variable's field to get to the location of the actual variable:
807 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000808 if (varFieldOffset > 0) {
809 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
810 AddUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
811 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000812
Daniel Dunbar00564992009-09-19 20:40:14 +0000813 // Now attach the location information to the DIE.
Daniel Dunbar00564992009-09-19 20:40:14 +0000814 AddBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000815}
816
Bill Wendling0310d762009-05-15 09:23:25 +0000817/// AddAddress - Add an address attribute to a die based on the location
818/// provided.
819void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute,
820 const MachineLocation &Location) {
821 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
822 DIEBlock *Block = new DIEBlock();
823
824 if (Location.isReg()) {
825 if (Reg < 32) {
826 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
827 } else {
828 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
829 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
830 }
831 } else {
832 if (Reg < 32) {
833 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
834 } else {
835 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
836 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
837 }
838
839 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
840 }
841
842 AddBlock(Die, Attribute, 0, Block);
843}
844
845/// AddType - Add a new type attribute to the specified entity.
846void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
847 if (Ty.isNull())
848 return;
849
850 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +0000851 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000852
853 // If it exists then use the existing value.
854 if (Slot) {
855 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
856 return;
857 }
858
859 // Set up proxy.
Bill Wendling995f80a2009-05-20 23:24:48 +0000860 Slot = CreateDIEEntry();
Bill Wendling0310d762009-05-15 09:23:25 +0000861
862 // Construct type.
863 DIE Buffer(dwarf::DW_TAG_base_type);
Devang Patel6ceea332009-08-31 18:49:10 +0000864 if (Ty.isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000865 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getNode()));
Devang Patel6ceea332009-08-31 18:49:10 +0000866 else if (Ty.isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000867 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000868 else {
Devang Patel6ceea332009-08-31 18:49:10 +0000869 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patele4b27562009-08-28 23:24:31 +0000870 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000871 }
872
873 // Add debug information entry to entity and appropriate context.
874 DIE *Die = NULL;
875 DIDescriptor Context = Ty.getContext();
876 if (!Context.isNull())
Devang Patele4b27562009-08-28 23:24:31 +0000877 Die = DW_Unit->getDieMapSlotFor(Context.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000878
879 if (Die) {
880 DIE *Child = new DIE(Buffer);
881 Die->AddChild(Child);
882 Buffer.Detach();
883 SetDIEEntry(Slot, Child);
884 } else {
885 Die = DW_Unit->AddDie(Buffer);
886 SetDIEEntry(Slot, Die);
887 }
888
889 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
890}
891
892/// ConstructTypeDIE - Construct basic type die from DIBasicType.
893void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
894 DIBasicType BTy) {
895 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000896 const char *Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000897 Buffer.setTag(dwarf::DW_TAG_base_type);
898 AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
899 BTy.getEncoding());
900
901 // Add name if not anonymous or intermediate type.
Devang Patel5ccdd102009-09-29 18:40:58 +0000902 if (Name)
Bill Wendling0310d762009-05-15 09:23:25 +0000903 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
904 uint64_t Size = BTy.getSizeInBits() >> 3;
905 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
906}
907
908/// ConstructTypeDIE - Construct derived type die from DIDerivedType.
909void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
910 DIDerivedType DTy) {
911 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000912 const char *Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000913 uint64_t Size = DTy.getSizeInBits() >> 3;
914 unsigned Tag = DTy.getTag();
915
916 // FIXME - Workaround for templates.
917 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
918
919 Buffer.setTag(Tag);
920
921 // Map to main type, void will not have a type.
922 DIType FromTy = DTy.getTypeDerivedFrom();
923 AddType(DW_Unit, &Buffer, FromTy);
924
925 // Add name if not anonymous or intermediate type.
Devang Patel808b8262009-10-16 21:27:43 +0000926 if (Name && Tag != dwarf::DW_TAG_pointer_type)
Bill Wendling0310d762009-05-15 09:23:25 +0000927 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
928
929 // Add size if non-zero (derived types might be zero-sized.)
930 if (Size)
931 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
932
933 // Add source line info if available and TyDesc is not a forward declaration.
934 if (!DTy.isForwardDecl())
935 AddSourceLine(&Buffer, &DTy);
936}
937
938/// ConstructTypeDIE - Construct type DIE from DICompositeType.
939void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
940 DICompositeType CTy) {
941 // Get core information.
Devang Patel5ccdd102009-09-29 18:40:58 +0000942 const char *Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000943
944 uint64_t Size = CTy.getSizeInBits() >> 3;
945 unsigned Tag = CTy.getTag();
946 Buffer.setTag(Tag);
947
948 switch (Tag) {
949 case dwarf::DW_TAG_vector_type:
950 case dwarf::DW_TAG_array_type:
951 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
952 break;
953 case dwarf::DW_TAG_enumeration_type: {
954 DIArray Elements = CTy.getTypeArray();
955
956 // Add enumerators to enumeration type.
957 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
958 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000959 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelc5254722009-10-09 17:51:49 +0000960 if (!Enum.isNull()) {
961 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
962 Buffer.AddChild(ElemDie);
963 }
Bill Wendling0310d762009-05-15 09:23:25 +0000964 }
965 }
966 break;
967 case dwarf::DW_TAG_subroutine_type: {
968 // Add return type.
969 DIArray Elements = CTy.getTypeArray();
970 DIDescriptor RTy = Elements.getElement(0);
Devang Patele4b27562009-08-28 23:24:31 +0000971 AddType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000972
973 // Add prototype flag.
974 AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
975
976 // Add arguments.
977 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
978 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
979 DIDescriptor Ty = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000980 AddType(DW_Unit, Arg, DIType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000981 Buffer.AddChild(Arg);
982 }
983 }
984 break;
985 case dwarf::DW_TAG_structure_type:
986 case dwarf::DW_TAG_union_type:
987 case dwarf::DW_TAG_class_type: {
988 // Add elements to structure type.
989 DIArray Elements = CTy.getTypeArray();
990
991 // A forward struct declared type may not have elements available.
992 if (Elements.isNull())
993 break;
994
995 // Add elements to structure type.
996 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
997 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000998 if (Element.isNull())
999 continue;
Bill Wendling0310d762009-05-15 09:23:25 +00001000 DIE *ElemDie = NULL;
1001 if (Element.getTag() == dwarf::DW_TAG_subprogram)
1002 ElemDie = CreateSubprogramDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +00001003 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001004 else
1005 ElemDie = CreateMemberDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +00001006 DIDerivedType(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001007 Buffer.AddChild(ElemDie);
1008 }
1009
Devang Patela1ba2692009-08-27 23:51:51 +00001010 if (CTy.isAppleBlockExtension())
Bill Wendling0310d762009-05-15 09:23:25 +00001011 AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
1012
1013 unsigned RLang = CTy.getRunTimeLang();
1014 if (RLang)
1015 AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1016 dwarf::DW_FORM_data1, RLang);
1017 break;
1018 }
1019 default:
1020 break;
1021 }
1022
1023 // Add name if not anonymous or intermediate type.
Devang Patel5ccdd102009-09-29 18:40:58 +00001024 if (Name)
Bill Wendling0310d762009-05-15 09:23:25 +00001025 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1026
1027 if (Tag == dwarf::DW_TAG_enumeration_type ||
1028 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1029 // Add size if non-zero (derived types might be zero-sized.)
1030 if (Size)
1031 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
1032 else {
1033 // Add zero size if it is not a forward declaration.
1034 if (CTy.isForwardDecl())
1035 AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1036 else
1037 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1038 }
1039
1040 // Add source line info if available.
1041 if (!CTy.isForwardDecl())
1042 AddSourceLine(&Buffer, &CTy);
1043 }
1044}
1045
1046/// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1047void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
1048 int64_t L = SR.getLo();
1049 int64_t H = SR.getHi();
1050 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1051
Devang Patel6325a532009-08-14 20:59:16 +00001052 AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
1053 if (L)
1054 AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
1055 if (H)
Daniel Dunbar00564992009-09-19 20:40:14 +00001056 AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001057
1058 Buffer.AddChild(DW_Subrange);
1059}
1060
1061/// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1062void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1063 DICompositeType *CTy) {
1064 Buffer.setTag(dwarf::DW_TAG_array_type);
1065 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1066 AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
1067
1068 // Emit derived type.
1069 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
1070 DIArray Elements = CTy->getTypeArray();
1071
1072 // Construct an anonymous type for index type.
1073 DIE IdxBuffer(dwarf::DW_TAG_base_type);
1074 AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1075 AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1076 dwarf::DW_ATE_signed);
1077 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1078
1079 // Add subranges to array type.
1080 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1081 DIDescriptor Element = Elements.getElement(i);
1082 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patele4b27562009-08-28 23:24:31 +00001083 ConstructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IndexTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001084 }
1085}
1086
1087/// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1088DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
1089 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel5ccdd102009-09-29 18:40:58 +00001090 const char *Name = ETy->getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001091 AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1092 int64_t Value = ETy->getEnumValue();
1093 AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1094 return Enumerator;
1095}
1096
1097/// CreateGlobalVariableDIE - Create new DIE using GV.
1098DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit,
1099 const DIGlobalVariable &GV) {
1100 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Devang Patel5ccdd102009-09-29 18:40:58 +00001101 AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1102 GV.getDisplayName());
1103
1104 const char *LinkageName = GV.getLinkageName();
1105 if (LinkageName) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001106 // Skip special LLVM prefix that is used to inform the asm printer to not
1107 // emit usual symbol prefix before the symbol name. This happens for
1108 // Objective-C symbol names and symbol whose name is replaced using GCC's
1109 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001110 if (LinkageName[0] == 1)
1111 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001112 AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001113 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001114 }
Daniel Dunbar00564992009-09-19 20:40:14 +00001115 AddType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001116 if (!GV.isLocalToUnit())
1117 AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1118 AddSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001119
1120 // Add address.
1121 DIEBlock *Block = new DIEBlock();
1122 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1123 AddObjectLabel(Block, 0, dwarf::DW_FORM_udata,
1124 Asm->Mang->getMangledName(GV.getGlobal()));
1125 AddBlock(GVDie, dwarf::DW_AT_location, 0, Block);
1126
Bill Wendling0310d762009-05-15 09:23:25 +00001127 return GVDie;
1128}
1129
1130/// CreateMemberDIE - Create new member DIE.
1131DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
1132 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel5ccdd102009-09-29 18:40:58 +00001133 if (const char *Name = DT.getName())
Bill Wendling0310d762009-05-15 09:23:25 +00001134 AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1135
1136 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1137
1138 AddSourceLine(MemberDie, &DT);
1139
Devang Patel33db5082009-11-04 22:06:12 +00001140 DIEBlock *MemLocationDie = new DIEBlock();
1141 AddUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1142
Bill Wendling0310d762009-05-15 09:23:25 +00001143 uint64_t Size = DT.getSizeInBits();
Devang Patelc416d3b2009-11-04 19:37:40 +00001144 uint64_t FieldSize = Size;
1145 if (DT.getTypeDerivedFrom().getTag() != dwarf::DW_TAG_array_type)
1146 FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001147
1148 if (Size != FieldSize) {
1149 // Handle bitfield.
1150 AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1151 AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1152
1153 uint64_t Offset = DT.getOffsetInBits();
1154 uint64_t FieldOffset = Offset;
1155 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1156 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1157 FieldOffset = (HiMark - FieldSize);
1158 Offset -= FieldOffset;
1159
1160 // Maybe we need to work from the other end.
1161 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1162 AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001163
Devang Patel33db5082009-11-04 22:06:12 +00001164 // Here WD_AT_data_member_location points to the anonymous
1165 // field that includes this bit field.
1166 AddUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1167
1168 } else
1169 // This is not a bitfield.
1170 AddUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1171
1172 AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001173
1174 if (DT.isProtected())
1175 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1176 dwarf::DW_ACCESS_protected);
1177 else if (DT.isPrivate())
1178 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1179 dwarf::DW_ACCESS_private);
1180
1181 return MemberDie;
1182}
1183
1184/// CreateSubprogramDIE - Create new DIE using SP.
1185DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit,
1186 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +00001187 bool IsConstructor,
1188 bool IsInlined) {
Bill Wendling0310d762009-05-15 09:23:25 +00001189 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1190
Devang Patel5ccdd102009-09-29 18:40:58 +00001191 const char * Name = SP.getName();
Bill Wendling0310d762009-05-15 09:23:25 +00001192 AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1193
Devang Patel5ccdd102009-09-29 18:40:58 +00001194 const char *LinkageName = SP.getLinkageName();
1195 if (LinkageName) {
Devang Patel53cb17d2009-07-16 01:01:22 +00001196 // Skip special LLVM prefix that is used to inform the asm printer to not emit
1197 // usual symbol prefix before the symbol name. This happens for Objective-C
1198 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
1199 if (LinkageName[0] == 1)
1200 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001201 AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001202 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001203 }
Bill Wendling0310d762009-05-15 09:23:25 +00001204 AddSourceLine(SPDie, &SP);
1205
1206 DICompositeType SPTy = SP.getType();
1207 DIArray Args = SPTy.getTypeArray();
1208
1209 // Add prototyped tag, if C or ObjC.
1210 unsigned Lang = SP.getCompileUnit().getLanguage();
1211 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1212 Lang == dwarf::DW_LANG_ObjC)
1213 AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
1214
1215 // Add Return Type.
1216 unsigned SPTag = SPTy.getTag();
1217 if (!IsConstructor) {
1218 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1219 AddType(DW_Unit, SPDie, SPTy);
1220 else
Devang Patele4b27562009-08-28 23:24:31 +00001221 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001222 }
1223
1224 if (!SP.isDefinition()) {
1225 AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1226
1227 // Add arguments. Do not add arguments for subprogram definition. They will
1228 // be handled through RecordVariable.
1229 if (SPTag == dwarf::DW_TAG_subroutine_type)
1230 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1231 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patele4b27562009-08-28 23:24:31 +00001232 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001233 AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1234 SPDie->AddChild(Arg);
1235 }
1236 }
1237
Bill Wendling6679ee42009-05-18 22:02:36 +00001238 if (!SP.isLocalToUnit() && !IsInlined)
Bill Wendling0310d762009-05-15 09:23:25 +00001239 AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1240
1241 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patele4b27562009-08-28 23:24:31 +00001242 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001243 Slot = SPDie;
1244 return SPDie;
1245}
1246
1247/// FindCompileUnit - Get the compile unit for the given descriptor.
1248///
1249CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const {
1250 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001251 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001252 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1253 return *I->second;
1254}
1255
Bill Wendling995f80a2009-05-20 23:24:48 +00001256/// CreateDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +00001257///
Bill Wendling995f80a2009-05-20 23:24:48 +00001258DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001259 // Get the descriptor.
1260 const DIVariable &VD = DV->getVariable();
Devang Patel1b845972009-11-03 18:30:27 +00001261 const char *Name = VD.getName();
1262 if (!Name)
1263 return NULL;
Bill Wendling0310d762009-05-15 09:23:25 +00001264
1265 // Translate tag to proper Dwarf tag. The result variable is dropped for
1266 // now.
1267 unsigned Tag;
1268 switch (VD.getTag()) {
1269 case dwarf::DW_TAG_return_variable:
1270 return NULL;
1271 case dwarf::DW_TAG_arg_variable:
1272 Tag = dwarf::DW_TAG_formal_parameter;
1273 break;
1274 case dwarf::DW_TAG_auto_variable: // fall thru
1275 default:
1276 Tag = dwarf::DW_TAG_variable;
1277 break;
1278 }
1279
1280 // Define variable debug information entry.
1281 DIE *VariableDie = new DIE(Tag);
Bill Wendling0310d762009-05-15 09:23:25 +00001282 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1283
1284 // Add source line info if available.
1285 AddSourceLine(VariableDie, &VD);
1286
1287 // Add variable type.
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001288 // FIXME: isBlockByrefVariable should be reformulated in terms of complex addresses instead.
Caroline Ticedc8f6042009-08-31 21:19:37 +00001289 if (VD.isBlockByrefVariable())
1290 AddType(Unit, VariableDie, GetBlockByrefType(VD.getType(), Name));
1291 else
1292 AddType(Unit, VariableDie, VD.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001293
1294 // Add variable address.
Bill Wendling1180c782009-05-18 23:08:55 +00001295 if (!DV->isInlinedFnVar()) {
1296 // Variables for abstract instances of inlined functions don't get a
1297 // location.
1298 MachineLocation Location;
1299 Location.set(RI->getFrameRegister(*MF),
1300 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Caroline Ticedc8f6042009-08-31 21:19:37 +00001301
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001302
1303 if (VD.hasComplexAddress())
1304 AddComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1305 else if (VD.isBlockByrefVariable())
Mike Stumpee4b8a72009-09-24 23:11:08 +00001306 AddBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Caroline Ticedc8f6042009-08-31 21:19:37 +00001307 else
1308 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling1180c782009-05-18 23:08:55 +00001309 }
Bill Wendling0310d762009-05-15 09:23:25 +00001310
1311 return VariableDie;
1312}
1313
1314/// getOrCreateScope - Returns the scope associated with the given descriptor.
1315///
Devang Patelc90aefe2009-10-14 21:08:09 +00001316DbgScope *DwarfDebug::getDbgScope(MDNode *N, const MachineInstr *MI,
1317 MDNode *InlinedAt) {
Devang Patelbdf45cb2009-10-27 20:47:17 +00001318 ValueMap<MDNode *, DbgScope *>::iterator VI = DbgScopeMap.find(N);
1319 if (VI != DbgScopeMap.end())
1320 return VI->second;
Devang Patelaf9e8472009-10-01 20:31:14 +00001321
1322 DbgScope *Parent = NULL;
1323
Devang Patelc90aefe2009-10-14 21:08:09 +00001324 if (InlinedAt) {
1325 DILocation IL(InlinedAt);
1326 assert (!IL.isNull() && "Invalid InlindAt location!");
Devang Patelbdf45cb2009-10-27 20:47:17 +00001327 ValueMap<MDNode *, DbgScope *>::iterator DSI =
Devang Patelc90aefe2009-10-14 21:08:09 +00001328 DbgScopeMap.find(IL.getScope().getNode());
1329 assert (DSI != DbgScopeMap.end() && "Unable to find InlineAt scope!");
1330 Parent = DSI->second;
1331 } else {
1332 DIDescriptor Scope(N);
1333 if (Scope.isCompileUnit()) {
1334 return NULL;
1335 } else if (Scope.isSubprogram()) {
1336 DISubprogram SP(N);
1337 DIDescriptor ParentDesc = SP.getContext();
1338 if (!ParentDesc.isNull() && !ParentDesc.isCompileUnit())
1339 Parent = getDbgScope(ParentDesc.getNode(), MI, InlinedAt);
1340 } else if (Scope.isLexicalBlock()) {
1341 DILexicalBlock DB(N);
1342 DIDescriptor ParentDesc = DB.getContext();
1343 if (!ParentDesc.isNull())
1344 Parent = getDbgScope(ParentDesc.getNode(), MI, InlinedAt);
1345 } else
1346 assert (0 && "Unexpected scope info");
1347 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001348
Devang Patelbdf45cb2009-10-27 20:47:17 +00001349 DbgScope *NScope = new DbgScope(Parent, DIDescriptor(N), InlinedAt);
1350 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001351
1352 if (Parent)
Devang Patelbdf45cb2009-10-27 20:47:17 +00001353 Parent->AddScope(NScope);
Devang Patelaf9e8472009-10-01 20:31:14 +00001354 else
1355 // First function is top level function.
Devang Patelaf9e8472009-10-01 20:31:14 +00001356 if (!FunctionDbgScope)
Devang Patelbdf45cb2009-10-27 20:47:17 +00001357 FunctionDbgScope = NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001358
Devang Patelbdf45cb2009-10-27 20:47:17 +00001359 DbgScopeMap.insert(std::make_pair(N, NScope));
1360 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001361}
1362
1363
1364/// getOrCreateScope - Returns the scope associated with the given descriptor.
1365/// FIXME - Remove this method.
Devang Patele4b27562009-08-28 23:24:31 +00001366DbgScope *DwarfDebug::getOrCreateScope(MDNode *N) {
1367 DbgScope *&Slot = DbgScopeMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +00001368 if (Slot) return Slot;
1369
1370 DbgScope *Parent = NULL;
Devang Patel5e005d82009-08-31 22:00:15 +00001371 DILexicalBlock Block(N);
Bill Wendling0310d762009-05-15 09:23:25 +00001372
Bill Wendling8fff19b2009-06-01 20:18:46 +00001373 // Don't create a new scope if we already created one for an inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001374 DenseMap<const MDNode *, DbgScope *>::iterator
1375 II = AbstractInstanceRootMap.find(N);
Bill Wendling8fff19b2009-06-01 20:18:46 +00001376 if (II != AbstractInstanceRootMap.end())
1377 return LexicalScopeStack.back();
1378
Bill Wendling0310d762009-05-15 09:23:25 +00001379 if (!Block.isNull()) {
1380 DIDescriptor ParentDesc = Block.getContext();
1381 Parent =
Devang Patele4b27562009-08-28 23:24:31 +00001382 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001383 }
1384
Devang Patele4b27562009-08-28 23:24:31 +00001385 Slot = new DbgScope(Parent, DIDescriptor(N));
Bill Wendling0310d762009-05-15 09:23:25 +00001386
1387 if (Parent)
1388 Parent->AddScope(Slot);
1389 else
1390 // First function is top level function.
1391 FunctionDbgScope = Slot;
1392
1393 return Slot;
1394}
1395
1396/// ConstructDbgScope - Construct the components of a scope.
1397///
1398void DwarfDebug::ConstructDbgScope(DbgScope *ParentScope,
1399 unsigned ParentStartID,
1400 unsigned ParentEndID,
1401 DIE *ParentDie, CompileUnit *Unit) {
1402 // Add variables to scope.
1403 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
1404 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Bill Wendling995f80a2009-05-20 23:24:48 +00001405 DIE *VariableDie = CreateDbgScopeVariable(Variables[i], Unit);
Bill Wendling0310d762009-05-15 09:23:25 +00001406 if (VariableDie) ParentDie->AddChild(VariableDie);
1407 }
1408
1409 // Add concrete instances to scope.
1410 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts =
1411 ParentScope->getConcreteInsts();
1412 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
1413 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
1414 DIE *Die = ConcreteInst->getDie();
1415
1416 unsigned StartID = ConcreteInst->getStartLabelID();
1417 unsigned EndID = ConcreteInst->getEndLabelID();
1418
1419 // Add the scope bounds.
1420 if (StartID)
1421 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1422 DWLabel("label", StartID));
1423 else
1424 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1425 DWLabel("func_begin", SubprogramCount));
1426
1427 if (EndID)
1428 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1429 DWLabel("label", EndID));
1430 else
1431 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1432 DWLabel("func_end", SubprogramCount));
1433
1434 ParentDie->AddChild(Die);
1435 }
1436
1437 // Add nested scopes.
1438 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
1439 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1440 // Define the Scope debug information entry.
1441 DbgScope *Scope = Scopes[j];
1442
1443 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1444 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1445
1446 // Ignore empty scopes.
1447 if (StartID == EndID && StartID != 0) continue;
1448
1449 // Do not ignore inlined scopes even if they don't have any variables or
1450 // scopes.
1451 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
1452 Scope->getConcreteInsts().empty())
1453 continue;
1454
1455 if (StartID == ParentStartID && EndID == ParentEndID) {
1456 // Just add stuff to the parent scope.
1457 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1458 } else {
1459 DIE *ScopeDie = new DIE(dwarf::DW_TAG_lexical_block);
1460
1461 // Add the scope bounds.
1462 if (StartID)
1463 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1464 DWLabel("label", StartID));
1465 else
1466 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1467 DWLabel("func_begin", SubprogramCount));
1468
1469 if (EndID)
1470 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1471 DWLabel("label", EndID));
1472 else
1473 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1474 DWLabel("func_end", SubprogramCount));
1475
1476 // Add the scope's contents.
1477 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1478 ParentDie->AddChild(ScopeDie);
1479 }
1480 }
1481}
1482
1483/// ConstructFunctionDbgScope - Construct the scope for the subprogram.
1484///
Bill Wendling17956162009-05-20 23:28:48 +00001485void DwarfDebug::ConstructFunctionDbgScope(DbgScope *RootScope,
1486 bool AbstractScope) {
Bill Wendling0310d762009-05-15 09:23:25 +00001487 // Exit if there is no root scope.
1488 if (!RootScope) return;
1489 DIDescriptor Desc = RootScope->getDesc();
1490 if (Desc.isNull())
1491 return;
1492
1493 // Get the subprogram debug information entry.
Devang Patele4b27562009-08-28 23:24:31 +00001494 DISubprogram SPD(Desc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001495
Bill Wendling0310d762009-05-15 09:23:25 +00001496 // Get the subprogram die.
Devang Patele4b27562009-08-28 23:24:31 +00001497 DIE *SPDie = ModuleCU->getDieMapSlotFor(SPD.getNode());
Devang Patelffd9c3d2009-10-05 23:59:00 +00001498 if (!SPDie) {
1499 ConstructSubprogram(SPD.getNode());
1500 SPDie = ModuleCU->getDieMapSlotFor(SPD.getNode());
1501 }
Bill Wendling0310d762009-05-15 09:23:25 +00001502 assert(SPDie && "Missing subprogram descriptor");
1503
Bill Wendling17956162009-05-20 23:28:48 +00001504 if (!AbstractScope) {
1505 // Add the function bounds.
1506 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1507 DWLabel("func_begin", SubprogramCount));
1508 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1509 DWLabel("func_end", SubprogramCount));
1510 MachineLocation Location(RI->getFrameRegister(*MF));
1511 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
1512 }
Bill Wendling0310d762009-05-15 09:23:25 +00001513
Devang Patel1dbc7712009-06-29 20:45:18 +00001514 ConstructDbgScope(RootScope, 0, 0, SPDie, ModuleCU);
Devang Patelfd07cf52009-10-05 23:40:42 +00001515 // If there are global variables at this scope then add their dies.
1516 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
1517 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1518 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1519 if (!N) continue;
1520 DIGlobalVariable GV(N);
1521 if (GV.getContext().getNode() == RootScope->getDesc().getNode()) {
1522 DIE *ScopedGVDie = CreateGlobalVariableDIE(ModuleCU, GV);
1523 SPDie->AddChild(ScopedGVDie);
1524 }
1525 }
Bill Wendling0310d762009-05-15 09:23:25 +00001526}
1527
Bill Wendling0310d762009-05-15 09:23:25 +00001528/// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1529///
1530void DwarfDebug::ConstructDefaultDbgScope(MachineFunction *MF) {
Devang Patel1dbc7712009-06-29 20:45:18 +00001531 StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Daniel Dunbar460f6562009-07-26 09:48:23 +00001532 StringMap<DIE*>::iterator GI = Globals.find(MF->getFunction()->getName());
Devang Patel70f44262009-06-29 20:38:13 +00001533 if (GI != Globals.end()) {
1534 DIE *SPDie = GI->second;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001535
Devang Patel70f44262009-06-29 20:38:13 +00001536 // Add the function bounds.
1537 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1538 DWLabel("func_begin", SubprogramCount));
1539 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1540 DWLabel("func_end", SubprogramCount));
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001541
Devang Patel70f44262009-06-29 20:38:13 +00001542 MachineLocation Location(RI->getFrameRegister(*MF));
1543 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001544 }
Bill Wendling0310d762009-05-15 09:23:25 +00001545}
1546
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001547/// GetOrCreateSourceID - Look up the source id with the given directory and
1548/// source file names. If none currently exists, create a new id and insert it
1549/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1550/// maps as well.
Devang Patel5ccdd102009-09-29 18:40:58 +00001551unsigned DwarfDebug::GetOrCreateSourceID(const char *DirName,
1552 const char *FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001553 unsigned DId;
1554 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1555 if (DI != DirectoryIdMap.end()) {
1556 DId = DI->getValue();
1557 } else {
1558 DId = DirectoryNames.size() + 1;
1559 DirectoryIdMap[DirName] = DId;
1560 DirectoryNames.push_back(DirName);
1561 }
1562
1563 unsigned FId;
1564 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1565 if (FI != SourceFileIdMap.end()) {
1566 FId = FI->getValue();
1567 } else {
1568 FId = SourceFileNames.size() + 1;
1569 SourceFileIdMap[FileName] = FId;
1570 SourceFileNames.push_back(FileName);
1571 }
1572
1573 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1574 SourceIdMap.find(std::make_pair(DId, FId));
1575 if (SI != SourceIdMap.end())
1576 return SI->second;
1577
1578 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1579 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1580 SourceIds.push_back(std::make_pair(DId, FId));
1581
1582 return SrcId;
1583}
1584
Devang Patele4b27562009-08-28 23:24:31 +00001585void DwarfDebug::ConstructCompileUnit(MDNode *N) {
1586 DICompileUnit DIUnit(N);
Devang Patel5ccdd102009-09-29 18:40:58 +00001587 const char *FN = DIUnit.getFilename();
1588 const char *Dir = DIUnit.getDirectory();
1589 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001590
1591 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
1592 AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
1593 DWLabel("section_line", 0), DWLabel("section_line", 0),
1594 false);
1595 AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001596 DIUnit.getProducer());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001597 AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
1598 DIUnit.getLanguage());
1599 AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
1600
Devang Patel5ccdd102009-09-29 18:40:58 +00001601 if (Dir)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001602 AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
1603 if (DIUnit.isOptimized())
1604 AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1605
Devang Patel5ccdd102009-09-29 18:40:58 +00001606 if (const char *Flags = DIUnit.getFlags())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001607 AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
1608
1609 unsigned RVer = DIUnit.getRunTimeVersion();
1610 if (RVer)
1611 AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1612 dwarf::DW_FORM_data1, RVer);
1613
1614 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001615 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001616 // Use first compile unit marked as isMain as the compile unit
1617 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001618 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001619 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001620
Devang Patele4b27562009-08-28 23:24:31 +00001621 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001622 CompileUnits.push_back(Unit);
1623}
1624
Devang Patele4b27562009-08-28 23:24:31 +00001625void DwarfDebug::ConstructGlobalVariableDIE(MDNode *N) {
1626 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001627
Devang Patel905cf5e2009-09-04 23:59:07 +00001628 // If debug information is malformed then ignore it.
1629 if (DI_GV.Verify() == false)
1630 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001631
1632 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001633 DIE *&Slot = ModuleCU->getDieMapSlotFor(DI_GV.getNode());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001634 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001635 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001636
Devang Patel1dbc7712009-06-29 20:45:18 +00001637 DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001638
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001639 // Add to map.
1640 Slot = VariableDie;
1641
1642 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001643 ModuleCU->getDie()->AddChild(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001644
1645 // Expose as global. FIXME - need to check external flag.
Devang Patel5ccdd102009-09-29 18:40:58 +00001646 ModuleCU->AddGlobal(DI_GV.getName(), VariableDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001647 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001648}
1649
Devang Patele4b27562009-08-28 23:24:31 +00001650void DwarfDebug::ConstructSubprogram(MDNode *N) {
1651 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001652
1653 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001654 DIE *&Slot = ModuleCU->getDieMapSlotFor(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001655 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001656 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001657
1658 if (!SP.isDefinition())
1659 // This is a method declaration which will be handled while constructing
1660 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001661 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001662
Devang Patel1dbc7712009-06-29 20:45:18 +00001663 DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001664
1665 // Add to map.
1666 Slot = SubprogramDie;
1667
1668 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001669 ModuleCU->getDie()->AddChild(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001670
1671 // Expose as global.
Devang Patel5ccdd102009-09-29 18:40:58 +00001672 ModuleCU->AddGlobal(SP.getName(), SubprogramDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001673 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001674}
1675
Daniel Dunbar00564992009-09-19 20:40:14 +00001676/// BeginModule - Emit all Dwarf sections that should come prior to the
1677/// content. Create global DIEs and emit initial debug info sections.
1678/// This is inovked by the target AsmPrinter.
Devang Patel208622d2009-06-25 22:36:02 +00001679void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) {
1680 this->M = M;
1681
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001682 if (TimePassesIsEnabled)
1683 DebugTimer->startTimer();
1684
Devang Patel78ab9e22009-07-30 18:56:46 +00001685 DebugInfoFinder DbgFinder;
1686 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001687
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001688 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001689 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1690 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001691 ConstructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001692
1693 if (CompileUnits.empty()) {
1694 if (TimePassesIsEnabled)
1695 DebugTimer->stopTimer();
1696
1697 return;
1698 }
1699
Devang Patel70f44262009-06-29 20:38:13 +00001700 // If main compile unit for this module is not seen than randomly
1701 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001702 if (!ModuleCU)
1703 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001704
Devang Patel13e16b62009-06-26 01:49:18 +00001705 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001706 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patelfd07cf52009-10-05 23:40:42 +00001707 E = DbgFinder.global_variable_end(); I != E; ++I) {
1708 DIGlobalVariable GV(*I);
1709 if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
1710 ScopedGVs.push_back(*I);
1711 else
1712 ConstructGlobalVariableDIE(*I);
1713 }
Devang Patel13e16b62009-06-26 01:49:18 +00001714
1715 // Create DIEs for each of the externally visible subprograms.
Devang Patel78ab9e22009-07-30 18:56:46 +00001716 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1717 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001718 ConstructSubprogram(*I);
1719
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001720 MMI = mmi;
1721 shouldEmit = true;
1722 MMI->setDebugInfoAvailability(true);
1723
1724 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001725 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001726
1727 // Print out .file directives to specify files for .loc directives. These are
1728 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001729 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001730 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1731 // Remember source id starts at 1.
1732 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1733 sys::Path FullPath(getSourceDirectoryName(Id.first));
1734 bool AppendOk =
1735 FullPath.appendComponent(getSourceFileName(Id.second));
1736 assert(AppendOk && "Could not append filename to directory!");
1737 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001738 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001739 Asm->EOL();
1740 }
1741 }
1742
1743 // Emit initial sections
1744 EmitInitial();
1745
1746 if (TimePassesIsEnabled)
1747 DebugTimer->stopTimer();
1748}
1749
1750/// EndModule - Emit all Dwarf sections that should come after the content.
1751///
1752void DwarfDebug::EndModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001753 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001754 return;
1755
1756 if (TimePassesIsEnabled)
1757 DebugTimer->startTimer();
1758
1759 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001760 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001761 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001762 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001763 EmitLabel("data_end", 0);
1764
1765 // End text sections.
1766 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001767 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001768 EmitLabel("section_end", i);
1769 }
1770
1771 // Emit common frame information.
1772 EmitCommonDebugFrame();
1773
1774 // Emit function debug frame information
1775 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1776 E = DebugFrames.end(); I != E; ++I)
1777 EmitFunctionDebugFrame(*I);
1778
1779 // Compute DIE offsets and sizes.
1780 SizeAndOffsets();
1781
1782 // Emit all the DIEs into a debug info section
1783 EmitDebugInfo();
1784
1785 // Corresponding abbreviations into a abbrev section.
1786 EmitAbbreviations();
1787
1788 // Emit source line correspondence into a debug line section.
1789 EmitDebugLines();
1790
1791 // Emit info into a debug pubnames section.
1792 EmitDebugPubNames();
1793
1794 // Emit info into a debug str section.
1795 EmitDebugStr();
1796
1797 // Emit info into a debug loc section.
1798 EmitDebugLoc();
1799
1800 // Emit info into a debug aranges section.
1801 EmitDebugARanges();
1802
1803 // Emit info into a debug ranges section.
1804 EmitDebugRanges();
1805
1806 // Emit info into a debug macinfo section.
1807 EmitDebugMacInfo();
1808
1809 // Emit inline info.
1810 EmitDebugInlineInfo();
1811
1812 if (TimePassesIsEnabled)
1813 DebugTimer->stopTimer();
1814}
1815
Devang Patele717faa2009-10-06 01:26:37 +00001816/// CollectVariableInfo - Populate DbgScope entries with variables' info.
Devang Patelac1ceb32009-10-09 22:42:28 +00001817void DwarfDebug::CollectVariableInfo() {
1818 if (!MMI) return;
Devang Patele717faa2009-10-06 01:26:37 +00001819 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1820 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1821 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac1ceb32009-10-09 22:42:28 +00001822 MetadataBase *MB = VI->first;
1823 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Pateleda31212009-10-08 18:48:03 +00001824 DIVariable DV (Var);
1825 if (DV.isNull()) continue;
Devang Patelac1ceb32009-10-09 22:42:28 +00001826 unsigned VSlot = VI->second;
Devang Patel149aa7c2009-10-16 18:18:03 +00001827 DbgScope *Scope = NULL;
Devang Patelbdf45cb2009-10-27 20:47:17 +00001828 ValueMap<MDNode *, DbgScope *>::iterator DSI =
Devang Patelc90aefe2009-10-14 21:08:09 +00001829 DbgScopeMap.find(DV.getContext().getNode());
Devang Patel149aa7c2009-10-16 18:18:03 +00001830 if (DSI != DbgScopeMap.end())
1831 Scope = DSI->second;
1832 else
1833 // There is not any instruction assocated with this scope, so get
1834 // a new scope.
1835 Scope = getDbgScope(DV.getContext().getNode(),
1836 NULL /* Not an instruction */,
1837 NULL /* Not inlined */);
1838 assert (Scope && "Unable to find variable scope!");
Devang Pateleda31212009-10-08 18:48:03 +00001839 Scope->AddVariable(new DbgVariable(DV, VSlot, false));
Devang Patele717faa2009-10-06 01:26:37 +00001840 }
1841}
1842
Devang Patel0d20ac82009-10-06 01:50:42 +00001843/// SetDbgScopeBeginLabels - Update DbgScope begin labels for the scopes that
1844/// start with this machine instruction.
1845void DwarfDebug::SetDbgScopeBeginLabels(const MachineInstr *MI, unsigned Label) {
1846 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1847 if (I == DbgScopeBeginMap.end())
1848 return;
1849 SmallVector<DbgScope *, 2> &SD = I->second;
1850 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
1851 SDI != SDE; ++SDI)
1852 (*SDI)->setStartLabelID(Label);
1853}
1854
1855/// SetDbgScopeEndLabels - Update DbgScope end labels for the scopes that
1856/// end with this machine instruction.
1857void DwarfDebug::SetDbgScopeEndLabels(const MachineInstr *MI, unsigned Label) {
1858 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00001859 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00001860 return;
1861 SmallVector<DbgScope *, 2> &SD = I->second;
1862 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
1863 SDI != SDE; ++SDI)
1864 (*SDI)->setEndLabelID(Label);
1865}
1866
Devang Patelaf9e8472009-10-01 20:31:14 +00001867/// ExtractScopeInformation - Scan machine instructions in this function
1868/// and collect DbgScopes. Return true, if atleast one scope was found.
1869bool DwarfDebug::ExtractScopeInformation(MachineFunction *MF) {
1870 // If scope information was extracted using .dbg intrinsics then there is not
1871 // any need to extract these information by scanning each instruction.
1872 if (!DbgScopeMap.empty())
1873 return false;
1874
1875 // Scan each instruction and create scopes.
1876 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1877 I != E; ++I) {
1878 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1879 II != IE; ++II) {
1880 const MachineInstr *MInsn = II;
1881 DebugLoc DL = MInsn->getDebugLoc();
1882 if (DL.isUnknown())
1883 continue;
1884 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel1619dc32009-10-13 23:28:53 +00001885 if (!DLT.Scope)
Devang Patelaf9e8472009-10-01 20:31:14 +00001886 continue;
1887 // There is no need to create another DIE for compile unit. For all
1888 // other scopes, create one DbgScope now. This will be translated
1889 // into a scope DIE at the end.
Devang Patel1619dc32009-10-13 23:28:53 +00001890 DIDescriptor D(DLT.Scope);
Devang Patelaf9e8472009-10-01 20:31:14 +00001891 if (!D.isCompileUnit()) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001892 DbgScope *Scope = getDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
Devang Patelaf9e8472009-10-01 20:31:14 +00001893 Scope->setLastInsn(MInsn);
1894 }
1895 }
1896 }
1897
1898 // If a scope's last instruction is not set then use its child scope's
1899 // last instruction as this scope's last instrunction.
Devang Patelbdf45cb2009-10-27 20:47:17 +00001900 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00001901 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patelbdf45cb2009-10-27 20:47:17 +00001902 DbgScope *S = DI->second;
1903 if (!S) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001904 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
1905 DI->second->FixInstructionMarkers();
1906 assert (DI->second->getLastInsn() && "Invalid last instruction!");
1907 }
1908
1909 // Each scope has first instruction and last instruction to mark beginning
1910 // and end of a scope respectively. Create an inverse map that list scopes
1911 // starts (and ends) with an instruction. One instruction may start (or end)
1912 // multiple scopes.
Devang Patelbdf45cb2009-10-27 20:47:17 +00001913 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00001914 DE = DbgScopeMap.end(); DI != DE; ++DI) {
1915 DbgScope *S = DI->second;
Devang Patelbdf45cb2009-10-27 20:47:17 +00001916 if (!S) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001917 const MachineInstr *MI = S->getFirstInsn();
1918 assert (MI && "DbgScope does not have first instruction!");
1919
1920 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
1921 if (IDI != DbgScopeBeginMap.end())
1922 IDI->second.push_back(S);
1923 else
1924 DbgScopeBeginMap.insert(std::make_pair(MI,
1925 SmallVector<DbgScope *, 2>(2, S)));
1926
1927 MI = S->getLastInsn();
1928 assert (MI && "DbgScope does not have last instruction!");
1929 IDI = DbgScopeEndMap.find(MI);
1930 if (IDI != DbgScopeEndMap.end())
1931 IDI->second.push_back(S);
1932 else
1933 DbgScopeEndMap.insert(std::make_pair(MI,
1934 SmallVector<DbgScope *, 2>(2, S)));
1935 }
1936
1937 return !DbgScopeMap.empty();
1938}
1939
Devang Patel6ef75172009-10-12 23:11:24 +00001940static DISubprogram getDISubprogram(MDNode *N) {
1941
1942 DIDescriptor D(N);
1943 if (D.isNull())
1944 return DISubprogram();
1945
1946 if (D.isCompileUnit())
1947 return DISubprogram();
1948
1949 if (D.isSubprogram())
1950 return DISubprogram(N);
1951
1952 if (D.isLexicalBlock())
1953 return getDISubprogram(DILexicalBlock(N).getContext().getNode());
1954
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +00001955 llvm_unreachable("Unexpected Descriptor!");
Devang Patel6ef75172009-10-12 23:11:24 +00001956}
1957
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001958/// BeginFunction - Gather pre-function debug information. Assumes being
1959/// emitted immediately after the function entry point.
1960void DwarfDebug::BeginFunction(MachineFunction *MF) {
1961 this->MF = MF;
1962
1963 if (!ShouldEmitDwarfDebug()) return;
1964
1965 if (TimePassesIsEnabled)
1966 DebugTimer->startTimer();
1967
Devang Patel60b35bd2009-10-06 18:37:31 +00001968#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
1969 if (!ExtractScopeInformation(MF))
1970 return;
Devang Patelac1ceb32009-10-09 22:42:28 +00001971 CollectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00001972#endif
1973
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001974 // Begin accumulating function debug information.
1975 MMI->BeginFunction(MF);
1976
1977 // Assumes in correct section after the entry point.
1978 EmitLabel("func_begin", ++SubprogramCount);
1979
1980 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1981 // function.
Devang Pateleda31212009-10-08 18:48:03 +00001982#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
Devang Patelac1ceb32009-10-09 22:42:28 +00001983 DebugLoc FDL = MF->getDefaultDebugLoc();
1984 if (!FDL.isUnknown()) {
1985 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
1986 unsigned LabelID = 0;
Devang Patel1619dc32009-10-13 23:28:53 +00001987 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00001988 if (!SP.isNull())
Devang Patel1619dc32009-10-13 23:28:53 +00001989 LabelID = RecordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00001990 else
Devang Patel1619dc32009-10-13 23:28:53 +00001991 LabelID = RecordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00001992 Asm->printLabel(LabelID);
1993 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001994 }
Devang Patelac1ceb32009-10-09 22:42:28 +00001995#else
1996 DebugLoc FDL = MF->getDefaultDebugLoc();
1997 if (!FDL.isUnknown()) {
1998 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
Devang Patel1619dc32009-10-13 23:28:53 +00001999 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002000 Asm->printLabel(LabelID);
2001 O << '\n';
2002 }
2003#endif
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002004 if (TimePassesIsEnabled)
2005 DebugTimer->stopTimer();
2006}
2007
2008/// EndFunction - Gather and emit post-function debug information.
2009///
2010void DwarfDebug::EndFunction(MachineFunction *MF) {
2011 if (!ShouldEmitDwarfDebug()) return;
2012
2013 if (TimePassesIsEnabled)
2014 DebugTimer->startTimer();
2015
Devang Patelac1ceb32009-10-09 22:42:28 +00002016#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
2017 if (DbgScopeMap.empty())
2018 return;
2019#endif
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002020 // Define end label for subprogram.
2021 EmitLabel("func_end", SubprogramCount);
2022
2023 // Get function line info.
2024 if (!Lines.empty()) {
2025 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00002026 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002027 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2028 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2029 // Append the function info to section info.
2030 SectionLineInfos.insert(SectionLineInfos.end(),
2031 Lines.begin(), Lines.end());
2032 }
2033
2034 // Construct the DbgScope for abstract instances.
2035 for (SmallVector<DbgScope *, 32>::iterator
2036 I = AbstractInstanceRootList.begin(),
2037 E = AbstractInstanceRootList.end(); I != E; ++I)
Bill Wendling17956162009-05-20 23:28:48 +00002038 ConstructFunctionDbgScope(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002039
2040 // Construct scopes for subprogram.
2041 if (FunctionDbgScope)
2042 ConstructFunctionDbgScope(FunctionDbgScope);
2043 else
2044 // FIXME: This is wrong. We are essentially getting past a problem with
2045 // debug information not being able to handle unreachable blocks that have
2046 // debug information in them. In particular, those unreachable blocks that
2047 // have "region end" info in them. That situation results in the "root
2048 // scope" not being created. If that's the case, then emit a "default"
2049 // scope, i.e., one that encompasses the whole function. This isn't
2050 // desirable. And a better way of handling this (and all of the debugging
2051 // information) needs to be explored.
2052 ConstructDefaultDbgScope(MF);
2053
2054 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2055 MMI->getFrameMoves()));
2056
2057 // Clear debug info
2058 if (FunctionDbgScope) {
2059 delete FunctionDbgScope;
2060 DbgScopeMap.clear();
Devang Patelaf9e8472009-10-01 20:31:14 +00002061 DbgScopeBeginMap.clear();
2062 DbgScopeEndMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002063 DbgAbstractScopeMap.clear();
2064 DbgConcreteScopeMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002065 FunctionDbgScope = NULL;
2066 LexicalScopeStack.clear();
2067 AbstractInstanceRootList.clear();
Devang Patel9217f792009-06-12 19:24:05 +00002068 AbstractInstanceRootMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002069 }
2070
2071 Lines.clear();
2072
2073 if (TimePassesIsEnabled)
2074 DebugTimer->stopTimer();
2075}
2076
2077/// RecordSourceLine - Records location information and associates it with a
2078/// label. Returns a unique label ID used to generate a label and provide
2079/// correspondence to the source line list.
Devang Patel3d910832009-09-30 22:51:28 +00002080unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002081 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002082 if (!MMI)
2083 return 0;
2084
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002085 if (TimePassesIsEnabled)
2086 DebugTimer->startTimer();
2087
Devang Patelf84548d2009-10-05 18:03:19 +00002088 const char *Dir = NULL;
2089 const char *Fn = NULL;
2090
2091 DIDescriptor Scope(S);
2092 if (Scope.isCompileUnit()) {
2093 DICompileUnit CU(S);
2094 Dir = CU.getDirectory();
2095 Fn = CU.getFilename();
2096 } else if (Scope.isSubprogram()) {
2097 DISubprogram SP(S);
2098 Dir = SP.getDirectory();
2099 Fn = SP.getFilename();
2100 } else if (Scope.isLexicalBlock()) {
2101 DILexicalBlock DB(S);
2102 Dir = DB.getDirectory();
2103 Fn = DB.getFilename();
2104 } else
2105 assert (0 && "Unexpected scope info");
2106
2107 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002108 unsigned ID = MMI->NextLabelID();
2109 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2110
2111 if (TimePassesIsEnabled)
2112 DebugTimer->stopTimer();
2113
2114 return ID;
2115}
2116
2117/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2118/// timed. Look up the source id with the given directory and source file
2119/// names. If none currently exists, create a new id and insert it in the
2120/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2121/// well.
2122unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2123 const std::string &FileName) {
2124 if (TimePassesIsEnabled)
2125 DebugTimer->startTimer();
2126
Devang Patel5ccdd102009-09-29 18:40:58 +00002127 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002128
2129 if (TimePassesIsEnabled)
2130 DebugTimer->stopTimer();
2131
2132 return SrcId;
2133}
2134
2135/// RecordRegionStart - Indicate the start of a region.
Devang Patele4b27562009-08-28 23:24:31 +00002136unsigned DwarfDebug::RecordRegionStart(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002137 if (TimePassesIsEnabled)
2138 DebugTimer->startTimer();
2139
Devang Patele4b27562009-08-28 23:24:31 +00002140 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002141 unsigned ID = MMI->NextLabelID();
2142 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
2143 LexicalScopeStack.push_back(Scope);
2144
2145 if (TimePassesIsEnabled)
2146 DebugTimer->stopTimer();
2147
2148 return ID;
2149}
2150
2151/// RecordRegionEnd - Indicate the end of a region.
Devang Patele4b27562009-08-28 23:24:31 +00002152unsigned DwarfDebug::RecordRegionEnd(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002153 if (TimePassesIsEnabled)
2154 DebugTimer->startTimer();
2155
Devang Patele4b27562009-08-28 23:24:31 +00002156 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002157 unsigned ID = MMI->NextLabelID();
2158 Scope->setEndLabelID(ID);
Devang Pateldaf9e022009-06-13 02:16:18 +00002159 // FIXME : region.end() may not be in the last basic block.
2160 // For now, do not pop last lexical scope because next basic
2161 // block may start new inlined function's body.
2162 unsigned LSSize = LexicalScopeStack.size();
2163 if (LSSize != 0 && LSSize != 1)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002164 LexicalScopeStack.pop_back();
2165
2166 if (TimePassesIsEnabled)
2167 DebugTimer->stopTimer();
2168
2169 return ID;
2170}
2171
2172/// RecordVariable - Indicate the declaration of a local variable.
Devang Patele4b27562009-08-28 23:24:31 +00002173void DwarfDebug::RecordVariable(MDNode *N, unsigned FrameIndex) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002174 if (TimePassesIsEnabled)
2175 DebugTimer->startTimer();
2176
Devang Patele4b27562009-08-28 23:24:31 +00002177 DIDescriptor Desc(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002178 DbgScope *Scope = NULL;
2179 bool InlinedFnVar = false;
2180
Devang Patele4b27562009-08-28 23:24:31 +00002181 if (Desc.getTag() == dwarf::DW_TAG_variable)
2182 Scope = getOrCreateScope(DIGlobalVariable(N).getContext().getNode());
2183 else {
Devang Patel24f20e02009-08-22 17:12:53 +00002184 bool InlinedVar = false;
Devang Patele4b27562009-08-28 23:24:31 +00002185 MDNode *Context = DIVariable(N).getContext().getNode();
2186 DISubprogram SP(Context);
Devang Patel24f20e02009-08-22 17:12:53 +00002187 if (!SP.isNull()) {
2188 // SP is inserted into DbgAbstractScopeMap when inlined function
2189 // start was recorded by RecordInlineFnStart.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002190 ValueMap<MDNode *, DbgScope *>::iterator
Devang Patele4b27562009-08-28 23:24:31 +00002191 I = DbgAbstractScopeMap.find(SP.getNode());
Devang Patel24f20e02009-08-22 17:12:53 +00002192 if (I != DbgAbstractScopeMap.end()) {
2193 InlinedVar = true;
2194 Scope = I->second;
2195 }
2196 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002197 if (!InlinedVar)
Devang Patele4b27562009-08-28 23:24:31 +00002198 Scope = getOrCreateScope(Context);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002199 }
2200
2201 assert(Scope && "Unable to find the variable's scope");
Devang Patele4b27562009-08-28 23:24:31 +00002202 DbgVariable *DV = new DbgVariable(DIVariable(N), FrameIndex, InlinedFnVar);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002203 Scope->AddVariable(DV);
2204
2205 if (TimePassesIsEnabled)
2206 DebugTimer->stopTimer();
2207}
2208
2209//// RecordInlinedFnStart - Indicate the start of inlined subroutine.
2210unsigned DwarfDebug::RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
2211 unsigned Line, unsigned Col) {
2212 unsigned LabelID = MMI->NextLabelID();
2213
Chris Lattner33adcfb2009-08-22 21:43:10 +00002214 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002215 return LabelID;
2216
2217 if (TimePassesIsEnabled)
2218 DebugTimer->startTimer();
2219
Devang Patele4b27562009-08-28 23:24:31 +00002220 MDNode *Node = SP.getNode();
2221 DenseMap<const MDNode *, DbgScope *>::iterator
2222 II = AbstractInstanceRootMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002223
2224 if (II == AbstractInstanceRootMap.end()) {
2225 // Create an abstract instance entry for this inlined function if it doesn't
2226 // already exist.
Devang Patele4b27562009-08-28 23:24:31 +00002227 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002228
2229 // Get the compile unit context.
Devang Patele4b27562009-08-28 23:24:31 +00002230 DIE *SPDie = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002231 if (!SPDie)
Devang Patel1dbc7712009-06-29 20:45:18 +00002232 SPDie = CreateSubprogramDIE(ModuleCU, SP, false, true);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002233
2234 // Mark as being inlined. This makes this subprogram entry an abstract
2235 // instance root.
2236 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
2237 // that it's defined. That probably won't change in the future. However,
2238 // this could be more elegant.
2239 AddUInt(SPDie, dwarf::DW_AT_inline, 0, dwarf::DW_INL_declared_not_inlined);
2240
2241 // Keep track of the abstract scope for this function.
Devang Patele4b27562009-08-28 23:24:31 +00002242 DbgAbstractScopeMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002243
Devang Patele4b27562009-08-28 23:24:31 +00002244 AbstractInstanceRootMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002245 AbstractInstanceRootList.push_back(Scope);
2246 }
2247
2248 // Create a concrete inlined instance for this inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00002249 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002250 DIE *ScopeDie = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel1dbc7712009-06-29 20:45:18 +00002251 ScopeDie->setAbstractCompileUnit(ModuleCU);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002252
Devang Patele4b27562009-08-28 23:24:31 +00002253 DIE *Origin = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002254 AddDIEEntry(ScopeDie, dwarf::DW_AT_abstract_origin,
2255 dwarf::DW_FORM_ref4, Origin);
Devang Patel1dbc7712009-06-29 20:45:18 +00002256 AddUInt(ScopeDie, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002257 AddUInt(ScopeDie, dwarf::DW_AT_call_line, 0, Line);
2258 AddUInt(ScopeDie, dwarf::DW_AT_call_column, 0, Col);
2259
2260 ConcreteScope->setDie(ScopeDie);
2261 ConcreteScope->setStartLabelID(LabelID);
2262 MMI->RecordUsedDbgLabel(LabelID);
2263
2264 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
2265
2266 // Keep track of the concrete scope that's inlined into this function.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002267 ValueMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
Devang Patele4b27562009-08-28 23:24:31 +00002268 SI = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002269
2270 if (SI == DbgConcreteScopeMap.end())
Devang Patele4b27562009-08-28 23:24:31 +00002271 DbgConcreteScopeMap[Node].push_back(ConcreteScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002272 else
2273 SI->second.push_back(ConcreteScope);
2274
2275 // Track the start label for this inlined function.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002276 ValueMap<MDNode *, SmallVector<unsigned, 4> >::iterator
Devang Patele4b27562009-08-28 23:24:31 +00002277 I = InlineInfo.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002278
2279 if (I == InlineInfo.end())
Devang Patele4b27562009-08-28 23:24:31 +00002280 InlineInfo[Node].push_back(LabelID);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002281 else
2282 I->second.push_back(LabelID);
2283
2284 if (TimePassesIsEnabled)
2285 DebugTimer->stopTimer();
2286
2287 return LabelID;
2288}
2289
2290/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
2291unsigned DwarfDebug::RecordInlinedFnEnd(DISubprogram &SP) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002292 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002293 return 0;
2294
2295 if (TimePassesIsEnabled)
2296 DebugTimer->startTimer();
2297
Devang Patele4b27562009-08-28 23:24:31 +00002298 MDNode *Node = SP.getNode();
Devang Patelbdf45cb2009-10-27 20:47:17 +00002299 ValueMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
Devang Patele4b27562009-08-28 23:24:31 +00002300 I = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002301
2302 if (I == DbgConcreteScopeMap.end()) {
2303 // FIXME: Can this situation actually happen? And if so, should it?
2304 if (TimePassesIsEnabled)
2305 DebugTimer->stopTimer();
2306
2307 return 0;
2308 }
2309
2310 SmallVector<DbgScope *, 8> &Scopes = I->second;
Devang Patel11a407f2009-06-15 21:45:50 +00002311 if (Scopes.empty()) {
2312 // Returned ID is 0 if this is unbalanced "end of inlined
2313 // scope". This could happen if optimizer eats dbg intrinsics
2314 // or "beginning of inlined scope" is not recoginized due to
2315 // missing location info. In such cases, ignore this region.end.
2316 return 0;
2317 }
2318
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002319 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
2320 unsigned ID = MMI->NextLabelID();
2321 MMI->RecordUsedDbgLabel(ID);
2322 Scope->setEndLabelID(ID);
2323
2324 if (TimePassesIsEnabled)
2325 DebugTimer->stopTimer();
2326
2327 return ID;
2328}
2329
Bill Wendling829e67b2009-05-20 23:22:40 +00002330//===----------------------------------------------------------------------===//
2331// Emit Methods
2332//===----------------------------------------------------------------------===//
2333
Bill Wendling94d04b82009-05-20 23:21:38 +00002334/// SizeAndOffsetDie - Compute the size and offset of a DIE.
2335///
2336unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2337 // Get the children.
2338 const std::vector<DIE *> &Children = Die->getChildren();
2339
2340 // If not last sibling and has children then add sibling offset attribute.
2341 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2342
2343 // Record the abbreviation.
2344 AssignAbbrevNumber(Die->getAbbrev());
2345
2346 // Get the abbreviation for this DIE.
2347 unsigned AbbrevNumber = Die->getAbbrevNumber();
2348 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2349
2350 // Set DIE offset
2351 Die->setOffset(Offset);
2352
2353 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002354 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002355
2356 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2357 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2358
2359 // Size the DIE attribute values.
2360 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2361 // Size attribute value.
2362 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2363
2364 // Size the DIE children if any.
2365 if (!Children.empty()) {
2366 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2367 "Children flag not set");
2368
2369 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2370 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2371
2372 // End of children marker.
2373 Offset += sizeof(int8_t);
2374 }
2375
2376 Die->setSize(Offset - Die->getOffset());
2377 return Offset;
2378}
2379
2380/// SizeAndOffsets - Compute the size and offset of all the DIEs.
2381///
2382void DwarfDebug::SizeAndOffsets() {
2383 // Compute size of compile unit header.
2384 static unsigned Offset =
2385 sizeof(int32_t) + // Length of Compilation Unit Info
2386 sizeof(int16_t) + // DWARF version number
2387 sizeof(int32_t) + // Offset Into Abbrev. Section
2388 sizeof(int8_t); // Pointer Size (in bytes)
2389
Devang Patel1dbc7712009-06-29 20:45:18 +00002390 SizeAndOffsetDie(ModuleCU->getDie(), Offset, true);
2391 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002392}
2393
2394/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2395/// tools to recognize the object file contains Dwarf information.
2396void DwarfDebug::EmitInitial() {
2397 // Check to see if we already emitted intial headers.
2398 if (didInitial) return;
2399 didInitial = true;
2400
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002401 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002402
Bill Wendling94d04b82009-05-20 23:21:38 +00002403 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002404 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002405 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002406 EmitLabel("section_debug_frame", 0);
2407 }
2408
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002409 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002410 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002411 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002412 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002413 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002414 EmitLabel("section_aranges", 0);
2415
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002416 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2417 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002418 EmitLabel("section_macinfo", 0);
2419 }
2420
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002421 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002422 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002423 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002424 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002425 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002426 EmitLabel("section_pubnames", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002427 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002428 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002429 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002430 EmitLabel("section_ranges", 0);
2431
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002432 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002433 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002434 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002435 EmitLabel("data_begin", 0);
2436}
2437
2438/// EmitDIE - Recusively Emits a debug information entry.
2439///
2440void DwarfDebug::EmitDIE(DIE *Die) {
2441 // Get the abbreviation for this DIE.
2442 unsigned AbbrevNumber = Die->getAbbrevNumber();
2443 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2444
2445 Asm->EOL();
2446
2447 // Emit the code (index) for the abbreviation.
2448 Asm->EmitULEB128Bytes(AbbrevNumber);
2449
2450 if (Asm->isVerbose())
2451 Asm->EOL(std::string("Abbrev [" +
2452 utostr(AbbrevNumber) +
2453 "] 0x" + utohexstr(Die->getOffset()) +
2454 ":0x" + utohexstr(Die->getSize()) + " " +
2455 dwarf::TagString(Abbrev->getTag())));
2456 else
2457 Asm->EOL();
2458
2459 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2460 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2461
2462 // Emit the DIE attribute values.
2463 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2464 unsigned Attr = AbbrevData[i].getAttribute();
2465 unsigned Form = AbbrevData[i].getForm();
2466 assert(Form && "Too many attributes for DIE (check abbreviation)");
2467
2468 switch (Attr) {
2469 case dwarf::DW_AT_sibling:
2470 Asm->EmitInt32(Die->SiblingOffset());
2471 break;
2472 case dwarf::DW_AT_abstract_origin: {
2473 DIEEntry *E = cast<DIEEntry>(Values[i]);
2474 DIE *Origin = E->getEntry();
2475 unsigned Addr =
2476 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
2477 Origin->getOffset();
2478
2479 Asm->EmitInt32(Addr);
2480 break;
2481 }
2482 default:
2483 // Emit an attribute using the defined form.
2484 Values[i]->EmitValue(this, Form);
2485 break;
2486 }
2487
2488 Asm->EOL(dwarf::AttributeString(Attr));
2489 }
2490
2491 // Emit the DIE children if any.
2492 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2493 const std::vector<DIE *> &Children = Die->getChildren();
2494
2495 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2496 EmitDIE(Children[j]);
2497
2498 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2499 }
2500}
2501
2502/// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
2503///
2504void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) {
2505 DIE *Die = Unit->getDie();
2506
2507 // Emit the compile units header.
2508 EmitLabel("info_begin", Unit->getID());
2509
2510 // Emit size of content not including length itself
2511 unsigned ContentSize = Die->getSize() +
2512 sizeof(int16_t) + // DWARF version number
2513 sizeof(int32_t) + // Offset Into Abbrev. Section
2514 sizeof(int8_t) + // Pointer Size (in bytes)
2515 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2516
2517 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2518 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2519 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2520 Asm->EOL("Offset Into Abbrev. Section");
2521 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2522
2523 EmitDIE(Die);
2524 // FIXME - extra padding for gdb bug.
2525 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2526 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2527 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2528 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2529 EmitLabel("info_end", Unit->getID());
2530
2531 Asm->EOL();
2532}
2533
2534void DwarfDebug::EmitDebugInfo() {
2535 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002536 Asm->OutStreamer.SwitchSection(
2537 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002538
Devang Patel1dbc7712009-06-29 20:45:18 +00002539 EmitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002540}
2541
2542/// EmitAbbreviations - Emit the abbreviation section.
2543///
2544void DwarfDebug::EmitAbbreviations() const {
2545 // Check to see if it is worth the effort.
2546 if (!Abbreviations.empty()) {
2547 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002548 Asm->OutStreamer.SwitchSection(
2549 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002550
2551 EmitLabel("abbrev_begin", 0);
2552
2553 // For each abbrevation.
2554 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2555 // Get abbreviation data
2556 const DIEAbbrev *Abbrev = Abbreviations[i];
2557
2558 // Emit the abbrevations code (base 1 index.)
2559 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2560 Asm->EOL("Abbreviation Code");
2561
2562 // Emit the abbreviations data.
2563 Abbrev->Emit(Asm);
2564
2565 Asm->EOL();
2566 }
2567
2568 // Mark end of abbreviations.
2569 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2570
2571 EmitLabel("abbrev_end", 0);
2572 Asm->EOL();
2573 }
2574}
2575
2576/// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2577/// the line matrix.
2578///
2579void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) {
2580 // Define last address of section.
2581 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2582 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2583 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2584 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2585
2586 // Mark end of matrix.
2587 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2588 Asm->EmitULEB128Bytes(1); Asm->EOL();
2589 Asm->EmitInt8(1); Asm->EOL();
2590}
2591
2592/// EmitDebugLines - Emit source line information.
2593///
2594void DwarfDebug::EmitDebugLines() {
2595 // If the target is using .loc/.file, the assembler will be emitting the
2596 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002597 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002598 return;
2599
2600 // Minimum line delta, thus ranging from -10..(255-10).
2601 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2602 // Maximum line delta, thus ranging from -10..(255-10).
2603 const int MaxLineDelta = 255 + MinLineDelta;
2604
2605 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002606 Asm->OutStreamer.SwitchSection(
2607 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002608
2609 // Construct the section header.
2610 EmitDifference("line_end", 0, "line_begin", 0, true);
2611 Asm->EOL("Length of Source Line Info");
2612 EmitLabel("line_begin", 0);
2613
2614 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2615
2616 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2617 Asm->EOL("Prolog Length");
2618 EmitLabel("line_prolog_begin", 0);
2619
2620 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2621
2622 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2623
2624 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2625
2626 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2627
2628 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2629
2630 // Line number standard opcode encodings argument count
2631 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2632 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2633 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2634 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2635 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2636 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2637 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2638 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2639 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2640
2641 // Emit directories.
2642 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2643 Asm->EmitString(getSourceDirectoryName(DI));
2644 Asm->EOL("Directory");
2645 }
2646
2647 Asm->EmitInt8(0); Asm->EOL("End of directories");
2648
2649 // Emit files.
2650 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2651 // Remember source id starts at 1.
2652 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2653 Asm->EmitString(getSourceFileName(Id.second));
2654 Asm->EOL("Source");
2655 Asm->EmitULEB128Bytes(Id.first);
2656 Asm->EOL("Directory #");
2657 Asm->EmitULEB128Bytes(0);
2658 Asm->EOL("Mod date");
2659 Asm->EmitULEB128Bytes(0);
2660 Asm->EOL("File size");
2661 }
2662
2663 Asm->EmitInt8(0); Asm->EOL("End of files");
2664
2665 EmitLabel("line_prolog_end", 0);
2666
2667 // A sequence for each text section.
2668 unsigned SecSrcLinesSize = SectionSourceLines.size();
2669
2670 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2671 // Isolate current sections line info.
2672 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2673
Chris Lattner93b6db32009-08-08 23:39:42 +00002674 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002675 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002676 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002677 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002678 }*/
2679 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002680
2681 // Dwarf assumes we start with first line of first source file.
2682 unsigned Source = 1;
2683 unsigned Line = 1;
2684
2685 // Construct rows of the address, source, line, column matrix.
2686 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2687 const SrcLineInfo &LineInfo = LineInfos[i];
2688 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2689 if (!LabelID) continue;
2690
Caroline Ticec6f9d622009-09-11 18:25:54 +00002691 if (LineInfo.getLine() == 0) continue;
2692
Bill Wendling94d04b82009-05-20 23:21:38 +00002693 if (!Asm->isVerbose())
2694 Asm->EOL();
2695 else {
2696 std::pair<unsigned, unsigned> SourceID =
2697 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002698 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002699 << getSourceDirectoryName(SourceID.first) << ' '
2700 << getSourceFileName(SourceID.second)
2701 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2702 }
2703
2704 // Define the line address.
2705 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2706 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2707 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2708 EmitReference("label", LabelID); Asm->EOL("Location label");
2709
2710 // If change of source, then switch to the new source.
2711 if (Source != LineInfo.getSourceID()) {
2712 Source = LineInfo.getSourceID();
2713 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2714 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2715 }
2716
2717 // If change of line.
2718 if (Line != LineInfo.getLine()) {
2719 // Determine offset.
2720 int Offset = LineInfo.getLine() - Line;
2721 int Delta = Offset - MinLineDelta;
2722
2723 // Update line.
2724 Line = LineInfo.getLine();
2725
2726 // If delta is small enough and in range...
2727 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2728 // ... then use fast opcode.
2729 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2730 } else {
2731 // ... otherwise use long hand.
2732 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2733 Asm->EOL("DW_LNS_advance_line");
2734 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2735 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2736 }
2737 } else {
2738 // Copy the previous row (different address or source)
2739 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2740 }
2741 }
2742
2743 EmitEndOfLineMatrix(j + 1);
2744 }
2745
2746 if (SecSrcLinesSize == 0)
2747 // Because we're emitting a debug_line section, we still need a line
2748 // table. The linker and friends expect it to exist. If there's nothing to
2749 // put into it, emit an empty table.
2750 EmitEndOfLineMatrix(1);
2751
2752 EmitLabel("line_end", 0);
2753 Asm->EOL();
2754}
2755
2756/// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2757///
2758void DwarfDebug::EmitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002759 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002760 return;
2761
2762 int stackGrowth =
2763 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2764 TargetFrameInfo::StackGrowsUp ?
2765 TD->getPointerSize() : -TD->getPointerSize();
2766
2767 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002768 Asm->OutStreamer.SwitchSection(
2769 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002770
2771 EmitLabel("debug_frame_common", 0);
2772 EmitDifference("debug_frame_common_end", 0,
2773 "debug_frame_common_begin", 0, true);
2774 Asm->EOL("Length of Common Information Entry");
2775
2776 EmitLabel("debug_frame_common_begin", 0);
2777 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2778 Asm->EOL("CIE Identifier Tag");
2779 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2780 Asm->EOL("CIE Version");
2781 Asm->EmitString("");
2782 Asm->EOL("CIE Augmentation");
2783 Asm->EmitULEB128Bytes(1);
2784 Asm->EOL("CIE Code Alignment Factor");
2785 Asm->EmitSLEB128Bytes(stackGrowth);
2786 Asm->EOL("CIE Data Alignment Factor");
2787 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2788 Asm->EOL("CIE RA Column");
2789
2790 std::vector<MachineMove> Moves;
2791 RI->getInitialFrameState(Moves);
2792
2793 EmitFrameMoves(NULL, 0, Moves, false);
2794
2795 Asm->EmitAlignment(2, 0, 0, false);
2796 EmitLabel("debug_frame_common_end", 0);
2797
2798 Asm->EOL();
2799}
2800
2801/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2802/// section.
2803void
2804DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002805 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002806 return;
2807
2808 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002809 Asm->OutStreamer.SwitchSection(
2810 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002811
2812 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2813 "debug_frame_begin", DebugFrameInfo.Number, true);
2814 Asm->EOL("Length of Frame Information Entry");
2815
2816 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2817
2818 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2819 0, 0, true, false);
2820 Asm->EOL("FDE CIE offset");
2821
2822 EmitReference("func_begin", DebugFrameInfo.Number);
2823 Asm->EOL("FDE initial location");
2824 EmitDifference("func_end", DebugFrameInfo.Number,
2825 "func_begin", DebugFrameInfo.Number);
2826 Asm->EOL("FDE address range");
2827
2828 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2829 false);
2830
2831 Asm->EmitAlignment(2, 0, 0, false);
2832 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2833
2834 Asm->EOL();
2835}
2836
2837void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2838 EmitDifference("pubnames_end", Unit->getID(),
2839 "pubnames_begin", Unit->getID(), true);
2840 Asm->EOL("Length of Public Names Info");
2841
2842 EmitLabel("pubnames_begin", Unit->getID());
2843
2844 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2845
2846 EmitSectionOffset("info_begin", "section_info",
2847 Unit->getID(), 0, true, false);
2848 Asm->EOL("Offset of Compilation Unit Info");
2849
2850 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2851 true);
2852 Asm->EOL("Compilation Unit Length");
2853
2854 StringMap<DIE*> &Globals = Unit->getGlobals();
2855 for (StringMap<DIE*>::const_iterator
2856 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2857 const char *Name = GI->getKeyData();
2858 DIE * Entity = GI->second;
2859
2860 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2861 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2862 }
2863
2864 Asm->EmitInt32(0); Asm->EOL("End Mark");
2865 EmitLabel("pubnames_end", Unit->getID());
2866
2867 Asm->EOL();
2868}
2869
2870/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2871///
2872void DwarfDebug::EmitDebugPubNames() {
2873 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002874 Asm->OutStreamer.SwitchSection(
2875 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002876
Devang Patel1dbc7712009-06-29 20:45:18 +00002877 EmitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002878}
2879
2880/// EmitDebugStr - Emit visible names into a debug str section.
2881///
2882void DwarfDebug::EmitDebugStr() {
2883 // Check to see if it is worth the effort.
2884 if (!StringPool.empty()) {
2885 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002886 Asm->OutStreamer.SwitchSection(
2887 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002888
2889 // For each of strings in the string pool.
2890 for (unsigned StringID = 1, N = StringPool.size();
2891 StringID <= N; ++StringID) {
2892 // Emit a label for reference from debug information entries.
2893 EmitLabel("string", StringID);
2894
2895 // Emit the string itself.
2896 const std::string &String = StringPool[StringID];
2897 Asm->EmitString(String); Asm->EOL();
2898 }
2899
2900 Asm->EOL();
2901 }
2902}
2903
2904/// EmitDebugLoc - Emit visible names into a debug loc section.
2905///
2906void DwarfDebug::EmitDebugLoc() {
2907 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002908 Asm->OutStreamer.SwitchSection(
2909 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002910 Asm->EOL();
2911}
2912
2913/// EmitDebugARanges - Emit visible names into a debug aranges section.
2914///
2915void DwarfDebug::EmitDebugARanges() {
2916 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002917 Asm->OutStreamer.SwitchSection(
2918 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002919
2920 // FIXME - Mock up
2921#if 0
2922 CompileUnit *Unit = GetBaseCompileUnit();
2923
2924 // Don't include size of length
2925 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2926
2927 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2928
2929 EmitReference("info_begin", Unit->getID());
2930 Asm->EOL("Offset of Compilation Unit Info");
2931
2932 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2933
2934 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2935
2936 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2937 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2938
2939 // Range 1
2940 EmitReference("text_begin", 0); Asm->EOL("Address");
2941 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2942
2943 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2944 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2945#endif
2946
2947 Asm->EOL();
2948}
2949
2950/// EmitDebugRanges - Emit visible names into a debug ranges section.
2951///
2952void DwarfDebug::EmitDebugRanges() {
2953 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002954 Asm->OutStreamer.SwitchSection(
2955 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002956 Asm->EOL();
2957}
2958
2959/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2960///
2961void DwarfDebug::EmitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002962 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002963 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002964 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002965 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002966 Asm->EOL();
2967 }
2968}
2969
2970/// EmitDebugInlineInfo - Emit inline info using following format.
2971/// Section Header:
2972/// 1. length of section
2973/// 2. Dwarf version number
2974/// 3. address size.
2975///
2976/// Entries (one "entry" for each function that was inlined):
2977///
2978/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2979/// otherwise offset into __debug_str for regular function name.
2980/// 2. offset into __debug_str section for regular function name.
2981/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2982/// instances for the function.
2983///
2984/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2985/// inlined instance; the die_offset points to the inlined_subroutine die in the
2986/// __debug_info section, and the low_pc is the starting address for the
2987/// inlining instance.
2988void DwarfDebug::EmitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002989 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002990 return;
2991
Devang Patel1dbc7712009-06-29 20:45:18 +00002992 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002993 return;
2994
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002995 Asm->OutStreamer.SwitchSection(
2996 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002997 Asm->EOL();
2998 EmitDifference("debug_inlined_end", 1,
2999 "debug_inlined_begin", 1, true);
3000 Asm->EOL("Length of Debug Inlined Information Entry");
3001
3002 EmitLabel("debug_inlined_begin", 1);
3003
3004 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
3005 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
3006
Devang Patelbdf45cb2009-10-27 20:47:17 +00003007 for (ValueMap<MDNode *, SmallVector<unsigned, 4> >::iterator
Bill Wendling94d04b82009-05-20 23:21:38 +00003008 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
Devang Patele4b27562009-08-28 23:24:31 +00003009 MDNode *Node = I->first;
Bill Wendling94d04b82009-05-20 23:21:38 +00003010 SmallVector<unsigned, 4> &Labels = I->second;
Devang Patele4b27562009-08-28 23:24:31 +00003011 DISubprogram SP(Node);
Devang Patel5ccdd102009-09-29 18:40:58 +00003012 const char *LName = SP.getLinkageName();
3013 const char *Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003014
Devang Patel5ccdd102009-09-29 18:40:58 +00003015 if (!LName)
Devang Patel53cb17d2009-07-16 01:01:22 +00003016 Asm->EmitString(Name);
3017 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003018 // Skip special LLVM prefix that is used to inform the asm printer to not
3019 // emit usual symbol prefix before the symbol name. This happens for
3020 // Objective-C symbol names and symbol whose name is replaced using GCC's
3021 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00003022 if (LName[0] == 1)
3023 LName = &LName[1];
3024 Asm->EmitString(LName);
3025 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003026 Asm->EOL("MIPS linkage name");
3027
3028 Asm->EmitString(Name); Asm->EOL("Function name");
3029
3030 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
3031
3032 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
3033 LE = Labels.end(); LI != LE; ++LI) {
Devang Patele4b27562009-08-28 23:24:31 +00003034 DIE *SP = ModuleCU->getDieMapSlotFor(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00003035 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
3036
3037 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00003038 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00003039 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00003040 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00003041
3042 PrintLabelName("label", *LI); Asm->EOL("low_pc");
3043 }
3044 }
3045
3046 EmitLabel("debug_inlined_end", 1);
3047 Asm->EOL();
3048}