blob: 067db8b848e2a54a3e4d3fb0bcc9234524638f3d [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 Patel2a610c72009-08-25 05:24:07 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000026#include "llvm/Support/Timer.h"
Devang Patel2a610c72009-08-25 05:24:07 +000027#include "llvm/Support/Debug.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000028#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000029using namespace llvm;
30
31static TimerGroup &getDwarfTimerGroup() {
32 static TimerGroup DwarfTimerGroup("Dwarf Debugging");
33 return DwarfTimerGroup;
34}
35
36//===----------------------------------------------------------------------===//
37
38/// Configuration values for initial hash set sizes (log2).
39///
40static const unsigned InitDiesSetSize = 9; // log2(512)
41static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
42static const unsigned InitValuesSetSize = 9; // log2(512)
43
44namespace llvm {
45
46//===----------------------------------------------------------------------===//
47/// CompileUnit - This dwarf writer support class manages information associate
48/// with a source file.
49class VISIBILITY_HIDDEN CompileUnit {
50 /// ID - File identifier for source.
51 ///
52 unsigned ID;
53
54 /// Die - Compile unit debug information entry.
55 ///
56 DIE *Die;
57
58 /// GVToDieMap - Tracks the mapping of unit level debug informaton
59 /// variables to debug information entries.
Devang Patel2a610c72009-08-25 05:24:07 +000060 /// FIXME : Rename GVToDieMap -> NodeToDieMap
61 std::map<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000062
63 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
64 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel2a610c72009-08-25 05:24:07 +000065 /// FIXME : Rename
66 std::map<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000067
68 /// Globals - A map of globally visible named entities for this unit.
69 ///
70 StringMap<DIE*> Globals;
71
72 /// DiesSet - Used to uniquely define dies within the compile unit.
73 ///
74 FoldingSet<DIE> DiesSet;
75public:
76 CompileUnit(unsigned I, DIE *D)
Bill Wendling39dd6962009-05-20 23:31:45 +000077 : ID(I), Die(D), DiesSet(InitDiesSetSize) {}
78 ~CompileUnit() { delete Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000079
80 // Accessors.
Bill Wendling39dd6962009-05-20 23:31:45 +000081 unsigned getID() const { return ID; }
82 DIE* getDie() const { return Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000083 StringMap<DIE*> &getGlobals() { return Globals; }
84
85 /// hasContent - Return true if this compile unit has something to write out.
86 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000087 bool hasContent() const { return !Die->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +000088
89 /// AddGlobal - Add a new global entity to the compile unit.
90 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000091 void AddGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000092
93 /// getDieMapSlotFor - Returns the debug information entry map slot for the
94 /// specified debug variable.
Devang Patel2a610c72009-08-25 05:24:07 +000095 DIE *&getDieMapSlotFor(MDNode *N) { return GVToDieMap[N]; }
Bill Wendling0310d762009-05-15 09:23:25 +000096
Chris Lattner1cda87c2009-07-14 04:50:12 +000097 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for
98 /// the specified debug variable.
Devang Patel2a610c72009-08-25 05:24:07 +000099 DIEEntry *&getDIEEntrySlotFor(MDNode *N) {
100 return GVToDIEEntryMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +0000101 }
102
103 /// AddDie - Adds or interns the DIE to the compile unit.
104 ///
105 DIE *AddDie(DIE &Buffer) {
106 FoldingSetNodeID ID;
107 Buffer.Profile(ID);
108 void *Where;
109 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
110
111 if (!Die) {
112 Die = new DIE(Buffer);
113 DiesSet.InsertNode(Die, Where);
114 this->Die->AddChild(Die);
115 Buffer.Detach();
116 }
117
118 return Die;
119 }
120};
121
122//===----------------------------------------------------------------------===//
123/// DbgVariable - This class is used to track local variable information.
124///
125class VISIBILITY_HIDDEN DbgVariable {
126 DIVariable Var; // Variable Descriptor.
127 unsigned FrameIndex; // Variable frame index.
Bill Wendling1180c782009-05-18 23:08:55 +0000128 bool InlinedFnVar; // Variable for an inlined function.
Bill Wendling0310d762009-05-15 09:23:25 +0000129public:
Bill Wendling1180c782009-05-18 23:08:55 +0000130 DbgVariable(DIVariable V, unsigned I, bool IFV)
131 : Var(V), FrameIndex(I), InlinedFnVar(IFV) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000132
133 // Accessors.
Bill Wendling1180c782009-05-18 23:08:55 +0000134 DIVariable getVariable() const { return Var; }
Bill Wendling0310d762009-05-15 09:23:25 +0000135 unsigned getFrameIndex() const { return FrameIndex; }
Bill Wendling1180c782009-05-18 23:08:55 +0000136 bool isInlinedFnVar() const { return InlinedFnVar; }
Bill Wendling0310d762009-05-15 09:23:25 +0000137};
138
139//===----------------------------------------------------------------------===//
140/// DbgScope - This class is used to track scope information.
141///
142class DbgConcreteScope;
143class VISIBILITY_HIDDEN DbgScope {
144 DbgScope *Parent; // Parent to this scope.
145 DIDescriptor Desc; // Debug info descriptor for scope.
146 // Either subprogram or block.
147 unsigned StartLabelID; // Label ID of the beginning of scope.
148 unsigned EndLabelID; // Label ID of the end of scope.
149 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
150 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
151 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs.
Owen Anderson04c05f72009-06-24 22:53:20 +0000152
153 // Private state for dump()
154 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000155public:
156 DbgScope(DbgScope *P, DIDescriptor D)
Owen Anderson04c05f72009-06-24 22:53:20 +0000157 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000158 virtual ~DbgScope();
159
160 // Accessors.
161 DbgScope *getParent() const { return Parent; }
162 DIDescriptor getDesc() const { return Desc; }
163 unsigned getStartLabelID() const { return StartLabelID; }
164 unsigned getEndLabelID() const { return EndLabelID; }
165 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
166 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
167 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; }
168 void setStartLabelID(unsigned S) { StartLabelID = S; }
169 void setEndLabelID(unsigned E) { EndLabelID = E; }
170
171 /// AddScope - Add a scope to the scope.
172 ///
173 void AddScope(DbgScope *S) { Scopes.push_back(S); }
174
175 /// AddVariable - Add a variable to the scope.
176 ///
177 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
178
179 /// AddConcreteInst - Add a concrete instance to the scope.
180 ///
181 void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); }
182
183#ifndef NDEBUG
184 void dump() const;
185#endif
186};
187
188#ifndef NDEBUG
189void DbgScope::dump() const {
Chris Lattnerc281de12009-08-23 00:51:00 +0000190 raw_ostream &err = errs();
191 err.indent(IndentLevel);
192 Desc.dump();
193 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000194
195 IndentLevel += 2;
196
197 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
198 if (Scopes[i] != this)
199 Scopes[i]->dump();
200
201 IndentLevel -= 2;
202}
203#endif
204
205//===----------------------------------------------------------------------===//
206/// DbgConcreteScope - This class is used to track a scope that holds concrete
207/// instance information.
208///
209class VISIBILITY_HIDDEN DbgConcreteScope : public DbgScope {
210 CompileUnit *Unit;
211 DIE *Die; // Debug info for this concrete scope.
212public:
213 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
214
215 // Accessors.
216 DIE *getDie() const { return Die; }
217 void setDie(DIE *D) { Die = D; }
218};
219
220DbgScope::~DbgScope() {
221 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
222 delete Scopes[i];
223 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
224 delete Variables[j];
225 for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k)
226 delete ConcreteInsts[k];
227}
228
229} // end llvm namespace
230
Chris Lattneraf76e592009-08-22 20:48:53 +0000231DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000232 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000233 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Chris Lattnera87dea42009-07-31 18:48:30 +0000234 ValuesSet(InitValuesSetSize), Values(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000235 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel43da8fb2009-07-13 21:26:33 +0000236 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000237 if (TimePassesIsEnabled)
238 DebugTimer = new Timer("Dwarf Debug Writer",
239 getDwarfTimerGroup());
240}
241DwarfDebug::~DwarfDebug() {
242 for (unsigned j = 0, M = Values.size(); j < M; ++j)
243 delete Values[j];
244
Devang Patel2a610c72009-08-25 05:24:07 +0000245 for (DenseMap<const MDNode *, DbgScope *>::iterator
Bill Wendling0310d762009-05-15 09:23:25 +0000246 I = AbstractInstanceRootMap.begin(),
247 E = AbstractInstanceRootMap.end(); I != E;++I)
248 delete I->second;
249
250 delete DebugTimer;
251}
252
253/// AssignAbbrevNumber - Define a unique number for the abbreviation.
254///
255void DwarfDebug::AssignAbbrevNumber(DIEAbbrev &Abbrev) {
256 // Profile the node so that we can make it unique.
257 FoldingSetNodeID ID;
258 Abbrev.Profile(ID);
259
260 // Check the set for priors.
261 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
262
263 // If it's newly added.
264 if (InSet == &Abbrev) {
265 // Add to abbreviation list.
266 Abbreviations.push_back(&Abbrev);
267
268 // Assign the vector position + 1 as its number.
269 Abbrev.setNumber(Abbreviations.size());
270 } else {
271 // Assign existing abbreviation number.
272 Abbrev.setNumber(InSet->getNumber());
273 }
274}
275
Bill Wendling995f80a2009-05-20 23:24:48 +0000276/// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
277/// information entry.
278DIEEntry *DwarfDebug::CreateDIEEntry(DIE *Entry) {
Bill Wendling0310d762009-05-15 09:23:25 +0000279 DIEEntry *Value;
280
281 if (Entry) {
282 FoldingSetNodeID ID;
283 DIEEntry::Profile(ID, Entry);
284 void *Where;
285 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
286
287 if (Value) return Value;
288
289 Value = new DIEEntry(Entry);
290 ValuesSet.InsertNode(Value, Where);
291 } else {
292 Value = new DIEEntry(Entry);
293 }
294
295 Values.push_back(Value);
296 return Value;
297}
298
299/// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
300///
301void DwarfDebug::SetDIEEntry(DIEEntry *Value, DIE *Entry) {
302 Value->setEntry(Entry);
303
304 // Add to values set if not already there. If it is, we merely have a
305 // duplicate in the values list (no harm.)
306 ValuesSet.GetOrInsertNode(Value);
307}
308
309/// AddUInt - Add an unsigned integer attribute data and value.
310///
311void DwarfDebug::AddUInt(DIE *Die, unsigned Attribute,
312 unsigned Form, uint64_t Integer) {
313 if (!Form) Form = DIEInteger::BestForm(false, Integer);
314
315 FoldingSetNodeID ID;
316 DIEInteger::Profile(ID, Integer);
317 void *Where;
318 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
319
320 if (!Value) {
321 Value = new DIEInteger(Integer);
322 ValuesSet.InsertNode(Value, Where);
323 Values.push_back(Value);
324 }
325
326 Die->AddValue(Attribute, Form, Value);
327}
328
329/// AddSInt - Add an signed integer attribute data and value.
330///
331void DwarfDebug::AddSInt(DIE *Die, unsigned Attribute,
332 unsigned Form, int64_t Integer) {
333 if (!Form) Form = DIEInteger::BestForm(true, Integer);
334
335 FoldingSetNodeID ID;
336 DIEInteger::Profile(ID, (uint64_t)Integer);
337 void *Where;
338 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
339
340 if (!Value) {
341 Value = new DIEInteger(Integer);
342 ValuesSet.InsertNode(Value, Where);
343 Values.push_back(Value);
344 }
345
346 Die->AddValue(Attribute, Form, Value);
347}
348
349/// AddString - Add a string attribute data and value.
350///
351void DwarfDebug::AddString(DIE *Die, unsigned Attribute, unsigned Form,
352 const std::string &String) {
353 FoldingSetNodeID ID;
354 DIEString::Profile(ID, String);
355 void *Where;
356 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
357
358 if (!Value) {
359 Value = new DIEString(String);
360 ValuesSet.InsertNode(Value, Where);
361 Values.push_back(Value);
362 }
363
364 Die->AddValue(Attribute, Form, Value);
365}
366
367/// AddLabel - Add a Dwarf label attribute data and value.
368///
369void DwarfDebug::AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
370 const DWLabel &Label) {
371 FoldingSetNodeID ID;
372 DIEDwarfLabel::Profile(ID, Label);
373 void *Where;
374 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
375
376 if (!Value) {
377 Value = new DIEDwarfLabel(Label);
378 ValuesSet.InsertNode(Value, Where);
379 Values.push_back(Value);
380 }
381
382 Die->AddValue(Attribute, Form, Value);
383}
384
385/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
386///
387void DwarfDebug::AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
388 const std::string &Label) {
389 FoldingSetNodeID ID;
390 DIEObjectLabel::Profile(ID, Label);
391 void *Where;
392 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
393
394 if (!Value) {
395 Value = new DIEObjectLabel(Label);
396 ValuesSet.InsertNode(Value, Where);
397 Values.push_back(Value);
398 }
399
400 Die->AddValue(Attribute, Form, Value);
401}
402
403/// AddSectionOffset - Add a section offset label attribute data and value.
404///
405void DwarfDebug::AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
406 const DWLabel &Label, const DWLabel &Section,
407 bool isEH, bool useSet) {
408 FoldingSetNodeID ID;
409 DIESectionOffset::Profile(ID, Label, Section);
410 void *Where;
411 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
412
413 if (!Value) {
414 Value = new DIESectionOffset(Label, Section, isEH, useSet);
415 ValuesSet.InsertNode(Value, Where);
416 Values.push_back(Value);
417 }
418
419 Die->AddValue(Attribute, Form, Value);
420}
421
422/// AddDelta - Add a label delta attribute data and value.
423///
424void DwarfDebug::AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
425 const DWLabel &Hi, const DWLabel &Lo) {
426 FoldingSetNodeID ID;
427 DIEDelta::Profile(ID, Hi, Lo);
428 void *Where;
429 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
430
431 if (!Value) {
432 Value = new DIEDelta(Hi, Lo);
433 ValuesSet.InsertNode(Value, Where);
434 Values.push_back(Value);
435 }
436
437 Die->AddValue(Attribute, Form, Value);
438}
439
440/// AddBlock - Add block data.
441///
442void DwarfDebug::AddBlock(DIE *Die, unsigned Attribute, unsigned Form,
443 DIEBlock *Block) {
444 Block->ComputeSize(TD);
445 FoldingSetNodeID ID;
446 Block->Profile(ID);
447 void *Where;
448 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
449
450 if (!Value) {
451 Value = Block;
452 ValuesSet.InsertNode(Value, Where);
453 Values.push_back(Value);
454 } else {
455 // Already exists, reuse the previous one.
456 delete Block;
457 Block = cast<DIEBlock>(Value);
458 }
459
460 Die->AddValue(Attribute, Block->BestForm(), Value);
461}
462
463/// AddSourceLine - Add location information to specified debug information
464/// entry.
465void DwarfDebug::AddSourceLine(DIE *Die, const DIVariable *V) {
466 // If there is no compile unit specified, don't add a line #.
467 if (V->getCompileUnit().isNull())
468 return;
469
470 unsigned Line = V->getLineNumber();
471 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
472 assert(FileID && "Invalid file id");
473 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
474 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
475}
476
477/// AddSourceLine - Add location information to specified debug information
478/// entry.
479void DwarfDebug::AddSourceLine(DIE *Die, const DIGlobal *G) {
480 // If there is no compile unit specified, don't add a line #.
481 if (G->getCompileUnit().isNull())
482 return;
483
484 unsigned Line = G->getLineNumber();
485 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
486 assert(FileID && "Invalid file id");
487 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
488 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
489}
490void DwarfDebug::AddSourceLine(DIE *Die, const DIType *Ty) {
491 // If there is no compile unit specified, don't add a line #.
492 DICompileUnit CU = Ty->getCompileUnit();
493 if (CU.isNull())
494 return;
495
496 unsigned Line = Ty->getLineNumber();
497 unsigned FileID = FindCompileUnit(CU).getID();
498 assert(FileID && "Invalid file id");
499 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
500 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
501}
502
503/// AddAddress - Add an address attribute to a die based on the location
504/// provided.
505void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute,
506 const MachineLocation &Location) {
507 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
508 DIEBlock *Block = new DIEBlock();
509
510 if (Location.isReg()) {
511 if (Reg < 32) {
512 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
513 } else {
514 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
515 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
516 }
517 } else {
518 if (Reg < 32) {
519 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
520 } else {
521 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
522 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
523 }
524
525 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
526 }
527
528 AddBlock(Die, Attribute, 0, Block);
529}
530
531/// AddType - Add a new type attribute to the specified entity.
532void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
533 if (Ty.isNull())
534 return;
535
536 // Check for pre-existence.
Devang Patel2a610c72009-08-25 05:24:07 +0000537 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000538
539 // If it exists then use the existing value.
540 if (Slot) {
541 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
542 return;
543 }
544
545 // Set up proxy.
Bill Wendling995f80a2009-05-20 23:24:48 +0000546 Slot = CreateDIEEntry();
Bill Wendling0310d762009-05-15 09:23:25 +0000547
548 // Construct type.
549 DIE Buffer(dwarf::DW_TAG_base_type);
550 if (Ty.isBasicType(Ty.getTag()))
Devang Patel2a610c72009-08-25 05:24:07 +0000551 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getNode()));
552 else if (Ty.isCompositeType(Ty.getTag()))
553 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000554 else {
Devang Patel2a610c72009-08-25 05:24:07 +0000555 assert(Ty.isDerivedType(Ty.getTag()) && "Unknown kind of DIType");
556 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getNode()));
557
Bill Wendling0310d762009-05-15 09:23:25 +0000558 }
559
560 // Add debug information entry to entity and appropriate context.
561 DIE *Die = NULL;
562 DIDescriptor Context = Ty.getContext();
563 if (!Context.isNull())
Devang Patel2a610c72009-08-25 05:24:07 +0000564 Die = DW_Unit->getDieMapSlotFor(Context.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000565
566 if (Die) {
567 DIE *Child = new DIE(Buffer);
568 Die->AddChild(Child);
569 Buffer.Detach();
570 SetDIEEntry(Slot, Child);
571 } else {
572 Die = DW_Unit->AddDie(Buffer);
573 SetDIEEntry(Slot, Die);
574 }
575
576 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
577}
578
579/// ConstructTypeDIE - Construct basic type die from DIBasicType.
580void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
581 DIBasicType BTy) {
582 // Get core information.
583 std::string Name;
584 BTy.getName(Name);
585 Buffer.setTag(dwarf::DW_TAG_base_type);
586 AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
587 BTy.getEncoding());
588
589 // Add name if not anonymous or intermediate type.
590 if (!Name.empty())
591 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
592 uint64_t Size = BTy.getSizeInBits() >> 3;
593 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
594}
595
596/// ConstructTypeDIE - Construct derived type die from DIDerivedType.
597void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
598 DIDerivedType DTy) {
599 // Get core information.
600 std::string Name;
601 DTy.getName(Name);
602 uint64_t Size = DTy.getSizeInBits() >> 3;
603 unsigned Tag = DTy.getTag();
604
605 // FIXME - Workaround for templates.
606 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
607
608 Buffer.setTag(Tag);
609
610 // Map to main type, void will not have a type.
611 DIType FromTy = DTy.getTypeDerivedFrom();
612 AddType(DW_Unit, &Buffer, FromTy);
613
614 // Add name if not anonymous or intermediate type.
615 if (!Name.empty())
616 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
617
618 // Add size if non-zero (derived types might be zero-sized.)
619 if (Size)
620 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
621
622 // Add source line info if available and TyDesc is not a forward declaration.
623 if (!DTy.isForwardDecl())
624 AddSourceLine(&Buffer, &DTy);
625}
626
627/// ConstructTypeDIE - Construct type DIE from DICompositeType.
628void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
629 DICompositeType CTy) {
630 // Get core information.
631 std::string Name;
632 CTy.getName(Name);
633
634 uint64_t Size = CTy.getSizeInBits() >> 3;
635 unsigned Tag = CTy.getTag();
636 Buffer.setTag(Tag);
637
638 switch (Tag) {
639 case dwarf::DW_TAG_vector_type:
640 case dwarf::DW_TAG_array_type:
641 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
642 break;
643 case dwarf::DW_TAG_enumeration_type: {
644 DIArray Elements = CTy.getTypeArray();
645
646 // Add enumerators to enumeration type.
647 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
648 DIE *ElemDie = NULL;
Devang Patel2a610c72009-08-25 05:24:07 +0000649 DIEnumerator Enum(Elements.getElement(i).getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000650 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
651 Buffer.AddChild(ElemDie);
652 }
653 }
654 break;
655 case dwarf::DW_TAG_subroutine_type: {
656 // Add return type.
657 DIArray Elements = CTy.getTypeArray();
658 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2a610c72009-08-25 05:24:07 +0000659 AddType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000660
661 // Add prototype flag.
662 AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
663
664 // Add arguments.
665 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
666 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
667 DIDescriptor Ty = Elements.getElement(i);
Devang Patel2a610c72009-08-25 05:24:07 +0000668 AddType(DW_Unit, Arg, DIType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000669 Buffer.AddChild(Arg);
670 }
671 }
672 break;
673 case dwarf::DW_TAG_structure_type:
674 case dwarf::DW_TAG_union_type:
675 case dwarf::DW_TAG_class_type: {
676 // Add elements to structure type.
677 DIArray Elements = CTy.getTypeArray();
678
679 // A forward struct declared type may not have elements available.
680 if (Elements.isNull())
681 break;
682
683 // Add elements to structure type.
684 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
685 DIDescriptor Element = Elements.getElement(i);
Devang Patel2a610c72009-08-25 05:24:07 +0000686 if (Element.isNull())
687 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000688 DIE *ElemDie = NULL;
689 if (Element.getTag() == dwarf::DW_TAG_subprogram)
690 ElemDie = CreateSubprogramDIE(DW_Unit,
Devang Patel2a610c72009-08-25 05:24:07 +0000691 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000692 else
693 ElemDie = CreateMemberDIE(DW_Unit,
Devang Patel2a610c72009-08-25 05:24:07 +0000694 DIDerivedType(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000695 Buffer.AddChild(ElemDie);
696 }
697
698 // FIXME: We'd like an API to register additional attributes for the
699 // frontend to use while synthesizing, and then we'd use that api in clang
700 // instead of this.
701 if (Name == "__block_literal_generic")
702 AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
703
704 unsigned RLang = CTy.getRunTimeLang();
705 if (RLang)
706 AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
707 dwarf::DW_FORM_data1, RLang);
708 break;
709 }
710 default:
711 break;
712 }
713
714 // Add name if not anonymous or intermediate type.
715 if (!Name.empty())
716 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
717
718 if (Tag == dwarf::DW_TAG_enumeration_type ||
719 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
720 // Add size if non-zero (derived types might be zero-sized.)
721 if (Size)
722 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
723 else {
724 // Add zero size if it is not a forward declaration.
725 if (CTy.isForwardDecl())
726 AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
727 else
728 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
729 }
730
731 // Add source line info if available.
732 if (!CTy.isForwardDecl())
733 AddSourceLine(&Buffer, &CTy);
734 }
735}
736
737/// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
738void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
739 int64_t L = SR.getLo();
740 int64_t H = SR.getHi();
741 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
742
Devang Patel6325a532009-08-14 20:59:16 +0000743 AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
744 if (L)
745 AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
746 if (H)
747 AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000748
749 Buffer.AddChild(DW_Subrange);
750}
751
752/// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
753void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
754 DICompositeType *CTy) {
755 Buffer.setTag(dwarf::DW_TAG_array_type);
756 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
757 AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
758
759 // Emit derived type.
760 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
761 DIArray Elements = CTy->getTypeArray();
762
763 // Construct an anonymous type for index type.
764 DIE IdxBuffer(dwarf::DW_TAG_base_type);
765 AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
766 AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
767 dwarf::DW_ATE_signed);
768 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
769
770 // Add subranges to array type.
771 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
772 DIDescriptor Element = Elements.getElement(i);
773 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2a610c72009-08-25 05:24:07 +0000774 ConstructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IndexTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000775 }
776}
777
778/// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
779DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
780 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
781 std::string Name;
782 ETy->getName(Name);
783 AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
784 int64_t Value = ETy->getEnumValue();
785 AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
786 return Enumerator;
787}
788
789/// CreateGlobalVariableDIE - Create new DIE using GV.
790DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit,
791 const DIGlobalVariable &GV) {
792 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
793 std::string Name;
794 GV.getDisplayName(Name);
795 AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
796 std::string LinkageName;
797 GV.getLinkageName(LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +0000798 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000799 // Skip special LLVM prefix that is used to inform the asm printer to not
800 // emit usual symbol prefix before the symbol name. This happens for
801 // Objective-C symbol names and symbol whose name is replaced using GCC's
802 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +0000803 if (LinkageName[0] == 1)
804 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +0000805 AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +0000806 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +0000807 }
808 AddType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +0000809 if (!GV.isLocalToUnit())
810 AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
811 AddSourceLine(GVDie, &GV);
812 return GVDie;
813}
814
815/// CreateMemberDIE - Create new member DIE.
816DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
817 DIE *MemberDie = new DIE(DT.getTag());
818 std::string Name;
819 DT.getName(Name);
820 if (!Name.empty())
821 AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
822
823 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
824
825 AddSourceLine(MemberDie, &DT);
826
827 uint64_t Size = DT.getSizeInBits();
828 uint64_t FieldSize = DT.getOriginalTypeSize();
829
830 if (Size != FieldSize) {
831 // Handle bitfield.
832 AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
833 AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
834
835 uint64_t Offset = DT.getOffsetInBits();
836 uint64_t FieldOffset = Offset;
837 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
838 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
839 FieldOffset = (HiMark - FieldSize);
840 Offset -= FieldOffset;
841
842 // Maybe we need to work from the other end.
843 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
844 AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
845 }
846
847 DIEBlock *Block = new DIEBlock();
848 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
849 AddUInt(Block, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
850 AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, Block);
851
852 if (DT.isProtected())
853 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
854 dwarf::DW_ACCESS_protected);
855 else if (DT.isPrivate())
856 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
857 dwarf::DW_ACCESS_private);
858
859 return MemberDie;
860}
861
862/// CreateSubprogramDIE - Create new DIE using SP.
863DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit,
864 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +0000865 bool IsConstructor,
866 bool IsInlined) {
Bill Wendling0310d762009-05-15 09:23:25 +0000867 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
868
869 std::string Name;
870 SP.getName(Name);
871 AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
872
873 std::string LinkageName;
874 SP.getLinkageName(LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +0000875 if (!LinkageName.empty()) {
876 // Skip special LLVM prefix that is used to inform the asm printer to not emit
877 // usual symbol prefix before the symbol name. This happens for Objective-C
878 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
879 if (LinkageName[0] == 1)
880 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +0000881 AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +0000882 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +0000883 }
Bill Wendling0310d762009-05-15 09:23:25 +0000884 AddSourceLine(SPDie, &SP);
885
886 DICompositeType SPTy = SP.getType();
887 DIArray Args = SPTy.getTypeArray();
888
889 // Add prototyped tag, if C or ObjC.
890 unsigned Lang = SP.getCompileUnit().getLanguage();
891 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
892 Lang == dwarf::DW_LANG_ObjC)
893 AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
894
895 // Add Return Type.
896 unsigned SPTag = SPTy.getTag();
897 if (!IsConstructor) {
898 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
899 AddType(DW_Unit, SPDie, SPTy);
900 else
Devang Patel2a610c72009-08-25 05:24:07 +0000901 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000902 }
903
904 if (!SP.isDefinition()) {
905 AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
906
907 // Add arguments. Do not add arguments for subprogram definition. They will
908 // be handled through RecordVariable.
909 if (SPTag == dwarf::DW_TAG_subroutine_type)
910 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
911 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2a610c72009-08-25 05:24:07 +0000912 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000913 AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
914 SPDie->AddChild(Arg);
915 }
916 }
917
Bill Wendling6679ee42009-05-18 22:02:36 +0000918 if (!SP.isLocalToUnit() && !IsInlined)
Bill Wendling0310d762009-05-15 09:23:25 +0000919 AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
920
921 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel2a610c72009-08-25 05:24:07 +0000922 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000923 Slot = SPDie;
924 return SPDie;
925}
926
927/// FindCompileUnit - Get the compile unit for the given descriptor.
928///
929CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const {
930 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patel2a610c72009-08-25 05:24:07 +0000931 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000932 assert(I != CompileUnitMap.end() && "Missing compile unit.");
933 return *I->second;
934}
935
Bill Wendling995f80a2009-05-20 23:24:48 +0000936/// CreateDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +0000937///
Bill Wendling995f80a2009-05-20 23:24:48 +0000938DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +0000939 // Get the descriptor.
940 const DIVariable &VD = DV->getVariable();
941
942 // Translate tag to proper Dwarf tag. The result variable is dropped for
943 // now.
944 unsigned Tag;
945 switch (VD.getTag()) {
946 case dwarf::DW_TAG_return_variable:
947 return NULL;
948 case dwarf::DW_TAG_arg_variable:
949 Tag = dwarf::DW_TAG_formal_parameter;
950 break;
951 case dwarf::DW_TAG_auto_variable: // fall thru
952 default:
953 Tag = dwarf::DW_TAG_variable;
954 break;
955 }
956
957 // Define variable debug information entry.
958 DIE *VariableDie = new DIE(Tag);
959 std::string Name;
960 VD.getName(Name);
961 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
962
963 // Add source line info if available.
964 AddSourceLine(VariableDie, &VD);
965
966 // Add variable type.
967 AddType(Unit, VariableDie, VD.getType());
968
969 // Add variable address.
Bill Wendling1180c782009-05-18 23:08:55 +0000970 if (!DV->isInlinedFnVar()) {
971 // Variables for abstract instances of inlined functions don't get a
972 // location.
973 MachineLocation Location;
974 Location.set(RI->getFrameRegister(*MF),
975 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
976 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
977 }
Bill Wendling0310d762009-05-15 09:23:25 +0000978
979 return VariableDie;
980}
981
982/// getOrCreateScope - Returns the scope associated with the given descriptor.
983///
Devang Patel2a610c72009-08-25 05:24:07 +0000984DbgScope *DwarfDebug::getOrCreateScope(MDNode *N) {
985 DbgScope *&Slot = DbgScopeMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +0000986 if (Slot) return Slot;
987
988 DbgScope *Parent = NULL;
Devang Patel2a610c72009-08-25 05:24:07 +0000989 DIBlock Block(N);
Bill Wendling0310d762009-05-15 09:23:25 +0000990
Bill Wendling8fff19b2009-06-01 20:18:46 +0000991 // Don't create a new scope if we already created one for an inlined function.
Devang Patel2a610c72009-08-25 05:24:07 +0000992 DenseMap<const MDNode *, DbgScope *>::iterator
993 II = AbstractInstanceRootMap.find(N);
Bill Wendling8fff19b2009-06-01 20:18:46 +0000994 if (II != AbstractInstanceRootMap.end())
995 return LexicalScopeStack.back();
996
Bill Wendling0310d762009-05-15 09:23:25 +0000997 if (!Block.isNull()) {
998 DIDescriptor ParentDesc = Block.getContext();
999 Parent =
Devang Patel2a610c72009-08-25 05:24:07 +00001000 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001001 }
1002
Devang Patel2a610c72009-08-25 05:24:07 +00001003 Slot = new DbgScope(Parent, DIDescriptor(N));
Bill Wendling0310d762009-05-15 09:23:25 +00001004
1005 if (Parent)
1006 Parent->AddScope(Slot);
1007 else
1008 // First function is top level function.
1009 FunctionDbgScope = Slot;
1010
1011 return Slot;
1012}
1013
1014/// ConstructDbgScope - Construct the components of a scope.
1015///
1016void DwarfDebug::ConstructDbgScope(DbgScope *ParentScope,
1017 unsigned ParentStartID,
1018 unsigned ParentEndID,
1019 DIE *ParentDie, CompileUnit *Unit) {
1020 // Add variables to scope.
1021 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
1022 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Bill Wendling995f80a2009-05-20 23:24:48 +00001023 DIE *VariableDie = CreateDbgScopeVariable(Variables[i], Unit);
Bill Wendling0310d762009-05-15 09:23:25 +00001024 if (VariableDie) ParentDie->AddChild(VariableDie);
1025 }
1026
1027 // Add concrete instances to scope.
1028 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts =
1029 ParentScope->getConcreteInsts();
1030 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
1031 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
1032 DIE *Die = ConcreteInst->getDie();
1033
1034 unsigned StartID = ConcreteInst->getStartLabelID();
1035 unsigned EndID = ConcreteInst->getEndLabelID();
1036
1037 // Add the scope bounds.
1038 if (StartID)
1039 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1040 DWLabel("label", StartID));
1041 else
1042 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1043 DWLabel("func_begin", SubprogramCount));
1044
1045 if (EndID)
1046 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1047 DWLabel("label", EndID));
1048 else
1049 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1050 DWLabel("func_end", SubprogramCount));
1051
1052 ParentDie->AddChild(Die);
1053 }
1054
1055 // Add nested scopes.
1056 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
1057 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1058 // Define the Scope debug information entry.
1059 DbgScope *Scope = Scopes[j];
1060
1061 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1062 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1063
1064 // Ignore empty scopes.
1065 if (StartID == EndID && StartID != 0) continue;
1066
1067 // Do not ignore inlined scopes even if they don't have any variables or
1068 // scopes.
1069 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
1070 Scope->getConcreteInsts().empty())
1071 continue;
1072
1073 if (StartID == ParentStartID && EndID == ParentEndID) {
1074 // Just add stuff to the parent scope.
1075 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1076 } else {
1077 DIE *ScopeDie = new DIE(dwarf::DW_TAG_lexical_block);
1078
1079 // Add the scope bounds.
1080 if (StartID)
1081 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1082 DWLabel("label", StartID));
1083 else
1084 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1085 DWLabel("func_begin", SubprogramCount));
1086
1087 if (EndID)
1088 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1089 DWLabel("label", EndID));
1090 else
1091 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1092 DWLabel("func_end", SubprogramCount));
1093
1094 // Add the scope's contents.
1095 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1096 ParentDie->AddChild(ScopeDie);
1097 }
1098 }
1099}
1100
1101/// ConstructFunctionDbgScope - Construct the scope for the subprogram.
1102///
Bill Wendling17956162009-05-20 23:28:48 +00001103void DwarfDebug::ConstructFunctionDbgScope(DbgScope *RootScope,
1104 bool AbstractScope) {
Bill Wendling0310d762009-05-15 09:23:25 +00001105 // Exit if there is no root scope.
1106 if (!RootScope) return;
1107 DIDescriptor Desc = RootScope->getDesc();
1108 if (Desc.isNull())
1109 return;
1110
1111 // Get the subprogram debug information entry.
Devang Patel2a610c72009-08-25 05:24:07 +00001112 DISubprogram SPD(Desc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001113
Bill Wendling0310d762009-05-15 09:23:25 +00001114 // Get the subprogram die.
Devang Patel2a610c72009-08-25 05:24:07 +00001115 DIE *SPDie = ModuleCU->getDieMapSlotFor(SPD.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001116 assert(SPDie && "Missing subprogram descriptor");
1117
Bill Wendling17956162009-05-20 23:28:48 +00001118 if (!AbstractScope) {
1119 // Add the function bounds.
1120 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1121 DWLabel("func_begin", SubprogramCount));
1122 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1123 DWLabel("func_end", SubprogramCount));
1124 MachineLocation Location(RI->getFrameRegister(*MF));
1125 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
1126 }
Bill Wendling0310d762009-05-15 09:23:25 +00001127
Devang Patel1dbc7712009-06-29 20:45:18 +00001128 ConstructDbgScope(RootScope, 0, 0, SPDie, ModuleCU);
Bill Wendling0310d762009-05-15 09:23:25 +00001129}
1130
Bill Wendling0310d762009-05-15 09:23:25 +00001131/// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1132///
1133void DwarfDebug::ConstructDefaultDbgScope(MachineFunction *MF) {
Devang Patel1dbc7712009-06-29 20:45:18 +00001134 StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Daniel Dunbar460f6562009-07-26 09:48:23 +00001135 StringMap<DIE*>::iterator GI = Globals.find(MF->getFunction()->getName());
Devang Patel70f44262009-06-29 20:38:13 +00001136 if (GI != Globals.end()) {
1137 DIE *SPDie = GI->second;
1138
1139 // Add the function bounds.
1140 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1141 DWLabel("func_begin", SubprogramCount));
1142 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1143 DWLabel("func_end", SubprogramCount));
1144
1145 MachineLocation Location(RI->getFrameRegister(*MF));
1146 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001147 }
Bill Wendling0310d762009-05-15 09:23:25 +00001148}
1149
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001150/// GetOrCreateSourceID - Look up the source id with the given directory and
1151/// source file names. If none currently exists, create a new id and insert it
1152/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1153/// maps as well.
1154unsigned DwarfDebug::GetOrCreateSourceID(const std::string &DirName,
1155 const std::string &FileName) {
1156 unsigned DId;
1157 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1158 if (DI != DirectoryIdMap.end()) {
1159 DId = DI->getValue();
1160 } else {
1161 DId = DirectoryNames.size() + 1;
1162 DirectoryIdMap[DirName] = DId;
1163 DirectoryNames.push_back(DirName);
1164 }
1165
1166 unsigned FId;
1167 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1168 if (FI != SourceFileIdMap.end()) {
1169 FId = FI->getValue();
1170 } else {
1171 FId = SourceFileNames.size() + 1;
1172 SourceFileIdMap[FileName] = FId;
1173 SourceFileNames.push_back(FileName);
1174 }
1175
1176 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1177 SourceIdMap.find(std::make_pair(DId, FId));
1178 if (SI != SourceIdMap.end())
1179 return SI->second;
1180
1181 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1182 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1183 SourceIds.push_back(std::make_pair(DId, FId));
1184
1185 return SrcId;
1186}
1187
Devang Patel2a610c72009-08-25 05:24:07 +00001188void DwarfDebug::ConstructCompileUnit(MDNode *N) {
1189 DICompileUnit DIUnit(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001190 std::string Dir, FN, Prod;
1191 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
1192 DIUnit.getFilename(FN));
1193
1194 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
1195 AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
1196 DWLabel("section_line", 0), DWLabel("section_line", 0),
1197 false);
1198 AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
1199 DIUnit.getProducer(Prod));
1200 AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
1201 DIUnit.getLanguage());
1202 AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
1203
1204 if (!Dir.empty())
1205 AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
1206 if (DIUnit.isOptimized())
1207 AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1208
1209 std::string Flags;
1210 DIUnit.getFlags(Flags);
1211 if (!Flags.empty())
1212 AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
1213
1214 unsigned RVer = DIUnit.getRunTimeVersion();
1215 if (RVer)
1216 AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1217 dwarf::DW_FORM_data1, RVer);
1218
1219 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001220 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001221 // Use first compile unit marked as isMain as the compile unit
1222 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001223 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001224 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001225
Devang Patel2a610c72009-08-25 05:24:07 +00001226 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001227 CompileUnits.push_back(Unit);
1228}
1229
Devang Patel2a610c72009-08-25 05:24:07 +00001230void DwarfDebug::ConstructGlobalVariableDIE(MDNode *N) {
1231 DIGlobalVariable DI_GV(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001232
1233 // Check for pre-existence.
Devang Patel2a610c72009-08-25 05:24:07 +00001234 DIE *&Slot = ModuleCU->getDieMapSlotFor(DI_GV.getNode());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001235 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001236 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001237
Devang Patel1dbc7712009-06-29 20:45:18 +00001238 DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001239
1240 // Add address.
1241 DIEBlock *Block = new DIEBlock();
1242 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1243 std::string GLN;
1244 AddObjectLabel(Block, 0, dwarf::DW_FORM_udata,
1245 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
1246 AddBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1247
1248 // Add to map.
1249 Slot = VariableDie;
1250
1251 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001252 ModuleCU->getDie()->AddChild(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001253
1254 // Expose as global. FIXME - need to check external flag.
1255 std::string Name;
Devang Patel1dbc7712009-06-29 20:45:18 +00001256 ModuleCU->AddGlobal(DI_GV.getName(Name), VariableDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001257 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001258}
1259
Devang Patel2a610c72009-08-25 05:24:07 +00001260void DwarfDebug::ConstructSubprogram(MDNode *N) {
1261 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001262
1263 // Check for pre-existence.
Devang Patel2a610c72009-08-25 05:24:07 +00001264 DIE *&Slot = ModuleCU->getDieMapSlotFor(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001265 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001266 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001267
1268 if (!SP.isDefinition())
1269 // This is a method declaration which will be handled while constructing
1270 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001271 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001272
Devang Patel1dbc7712009-06-29 20:45:18 +00001273 DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001274
1275 // Add to map.
1276 Slot = SubprogramDie;
1277
1278 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001279 ModuleCU->getDie()->AddChild(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001280
1281 // Expose as global.
1282 std::string Name;
Devang Patel1dbc7712009-06-29 20:45:18 +00001283 ModuleCU->AddGlobal(SP.getName(Name), SubprogramDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001284 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001285}
1286
Devang Patel208622d2009-06-25 22:36:02 +00001287 /// BeginModule - Emit all Dwarf sections that should come prior to the
1288 /// content. Create global DIEs and emit initial debug info sections.
1289 /// This is inovked by the target AsmPrinter.
1290void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) {
1291 this->M = M;
1292
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001293 if (TimePassesIsEnabled)
1294 DebugTimer->startTimer();
1295
Devang Patel78ab9e22009-07-30 18:56:46 +00001296 DebugInfoFinder DbgFinder;
1297 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001298
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001299 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001300 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1301 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001302 ConstructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001303
1304 if (CompileUnits.empty()) {
1305 if (TimePassesIsEnabled)
1306 DebugTimer->stopTimer();
1307
1308 return;
1309 }
1310
Devang Patel70f44262009-06-29 20:38:13 +00001311 // If main compile unit for this module is not seen than randomly
1312 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001313 if (!ModuleCU)
1314 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001315
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001316 // If there is not any debug info available for any global variables and any
1317 // subprograms then there is not any debug info to emit.
Devang Patel78ab9e22009-07-30 18:56:46 +00001318 if (DbgFinder.global_variable_count() == 0
1319 && DbgFinder.subprogram_count() == 0) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001320 if (TimePassesIsEnabled)
1321 DebugTimer->stopTimer();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001322 return;
1323 }
Devang Patel78ab9e22009-07-30 18:56:46 +00001324
Devang Patel13e16b62009-06-26 01:49:18 +00001325 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001326 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1327 E = DbgFinder.global_variable_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001328 ConstructGlobalVariableDIE(*I);
1329
1330 // Create DIEs for each of the externally visible subprograms.
Devang Patel78ab9e22009-07-30 18:56:46 +00001331 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1332 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001333 ConstructSubprogram(*I);
1334
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001335 MMI = mmi;
1336 shouldEmit = true;
1337 MMI->setDebugInfoAvailability(true);
1338
1339 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001340 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001341
1342 // Print out .file directives to specify files for .loc directives. These are
1343 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001344 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001345 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1346 // Remember source id starts at 1.
1347 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1348 sys::Path FullPath(getSourceDirectoryName(Id.first));
1349 bool AppendOk =
1350 FullPath.appendComponent(getSourceFileName(Id.second));
1351 assert(AppendOk && "Could not append filename to directory!");
1352 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001353 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001354 Asm->EOL();
1355 }
1356 }
1357
1358 // Emit initial sections
1359 EmitInitial();
1360
1361 if (TimePassesIsEnabled)
1362 DebugTimer->stopTimer();
1363}
1364
1365/// EndModule - Emit all Dwarf sections that should come after the content.
1366///
1367void DwarfDebug::EndModule() {
1368 if (!ShouldEmitDwarfDebug())
1369 return;
1370
1371 if (TimePassesIsEnabled)
1372 DebugTimer->startTimer();
1373
1374 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001375 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001376 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001377 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001378 EmitLabel("data_end", 0);
1379
1380 // End text sections.
1381 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001382 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001383 EmitLabel("section_end", i);
1384 }
1385
1386 // Emit common frame information.
1387 EmitCommonDebugFrame();
1388
1389 // Emit function debug frame information
1390 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1391 E = DebugFrames.end(); I != E; ++I)
1392 EmitFunctionDebugFrame(*I);
1393
1394 // Compute DIE offsets and sizes.
1395 SizeAndOffsets();
1396
1397 // Emit all the DIEs into a debug info section
1398 EmitDebugInfo();
1399
1400 // Corresponding abbreviations into a abbrev section.
1401 EmitAbbreviations();
1402
1403 // Emit source line correspondence into a debug line section.
1404 EmitDebugLines();
1405
1406 // Emit info into a debug pubnames section.
1407 EmitDebugPubNames();
1408
1409 // Emit info into a debug str section.
1410 EmitDebugStr();
1411
1412 // Emit info into a debug loc section.
1413 EmitDebugLoc();
1414
1415 // Emit info into a debug aranges section.
1416 EmitDebugARanges();
1417
1418 // Emit info into a debug ranges section.
1419 EmitDebugRanges();
1420
1421 // Emit info into a debug macinfo section.
1422 EmitDebugMacInfo();
1423
1424 // Emit inline info.
1425 EmitDebugInlineInfo();
1426
1427 if (TimePassesIsEnabled)
1428 DebugTimer->stopTimer();
1429}
1430
1431/// BeginFunction - Gather pre-function debug information. Assumes being
1432/// emitted immediately after the function entry point.
1433void DwarfDebug::BeginFunction(MachineFunction *MF) {
1434 this->MF = MF;
1435
1436 if (!ShouldEmitDwarfDebug()) return;
1437
1438 if (TimePassesIsEnabled)
1439 DebugTimer->startTimer();
1440
1441 // Begin accumulating function debug information.
1442 MMI->BeginFunction(MF);
1443
1444 // Assumes in correct section after the entry point.
1445 EmitLabel("func_begin", ++SubprogramCount);
1446
1447 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1448 // function.
1449 DebugLoc FDL = MF->getDefaultDebugLoc();
1450 if (!FDL.isUnknown()) {
1451 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
1452 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
1453 DICompileUnit(DLT.CompileUnit));
1454 Asm->printLabel(LabelID);
1455 }
1456
1457 if (TimePassesIsEnabled)
1458 DebugTimer->stopTimer();
1459}
1460
1461/// EndFunction - Gather and emit post-function debug information.
1462///
1463void DwarfDebug::EndFunction(MachineFunction *MF) {
1464 if (!ShouldEmitDwarfDebug()) return;
1465
1466 if (TimePassesIsEnabled)
1467 DebugTimer->startTimer();
1468
1469 // Define end label for subprogram.
1470 EmitLabel("func_end", SubprogramCount);
1471
1472 // Get function line info.
1473 if (!Lines.empty()) {
1474 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00001475 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001476 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
1477 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
1478 // Append the function info to section info.
1479 SectionLineInfos.insert(SectionLineInfos.end(),
1480 Lines.begin(), Lines.end());
1481 }
1482
1483 // Construct the DbgScope for abstract instances.
1484 for (SmallVector<DbgScope *, 32>::iterator
1485 I = AbstractInstanceRootList.begin(),
1486 E = AbstractInstanceRootList.end(); I != E; ++I)
Bill Wendling17956162009-05-20 23:28:48 +00001487 ConstructFunctionDbgScope(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001488
1489 // Construct scopes for subprogram.
1490 if (FunctionDbgScope)
1491 ConstructFunctionDbgScope(FunctionDbgScope);
1492 else
1493 // FIXME: This is wrong. We are essentially getting past a problem with
1494 // debug information not being able to handle unreachable blocks that have
1495 // debug information in them. In particular, those unreachable blocks that
1496 // have "region end" info in them. That situation results in the "root
1497 // scope" not being created. If that's the case, then emit a "default"
1498 // scope, i.e., one that encompasses the whole function. This isn't
1499 // desirable. And a better way of handling this (and all of the debugging
1500 // information) needs to be explored.
1501 ConstructDefaultDbgScope(MF);
1502
1503 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
1504 MMI->getFrameMoves()));
1505
1506 // Clear debug info
1507 if (FunctionDbgScope) {
1508 delete FunctionDbgScope;
1509 DbgScopeMap.clear();
1510 DbgAbstractScopeMap.clear();
1511 DbgConcreteScopeMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001512 FunctionDbgScope = NULL;
1513 LexicalScopeStack.clear();
1514 AbstractInstanceRootList.clear();
Devang Patel9217f792009-06-12 19:24:05 +00001515 AbstractInstanceRootMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001516 }
1517
1518 Lines.clear();
1519
1520 if (TimePassesIsEnabled)
1521 DebugTimer->stopTimer();
1522}
1523
1524/// RecordSourceLine - Records location information and associates it with a
1525/// label. Returns a unique label ID used to generate a label and provide
1526/// correspondence to the source line list.
1527unsigned DwarfDebug::RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
1528 if (TimePassesIsEnabled)
1529 DebugTimer->startTimer();
1530
1531 CompileUnit *Unit = CompileUnitMap[V];
1532 assert(Unit && "Unable to find CompileUnit");
1533 unsigned ID = MMI->NextLabelID();
1534 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
1535
1536 if (TimePassesIsEnabled)
1537 DebugTimer->stopTimer();
1538
1539 return ID;
1540}
1541
1542/// RecordSourceLine - Records location information and associates it with a
1543/// label. Returns a unique label ID used to generate a label and provide
1544/// correspondence to the source line list.
1545unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col,
1546 DICompileUnit CU) {
Devang Patel2a610c72009-08-25 05:24:07 +00001547 if (!MMI)
1548 return 0;
1549
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001550 if (TimePassesIsEnabled)
1551 DebugTimer->startTimer();
1552
1553 std::string Dir, Fn;
1554 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
1555 CU.getFilename(Fn));
1556 unsigned ID = MMI->NextLabelID();
1557 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
1558
1559 if (TimePassesIsEnabled)
1560 DebugTimer->stopTimer();
1561
1562 return ID;
1563}
1564
1565/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
1566/// timed. Look up the source id with the given directory and source file
1567/// names. If none currently exists, create a new id and insert it in the
1568/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
1569/// well.
1570unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
1571 const std::string &FileName) {
1572 if (TimePassesIsEnabled)
1573 DebugTimer->startTimer();
1574
1575 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
1576
1577 if (TimePassesIsEnabled)
1578 DebugTimer->stopTimer();
1579
1580 return SrcId;
1581}
1582
1583/// RecordRegionStart - Indicate the start of a region.
Devang Patel2a610c72009-08-25 05:24:07 +00001584unsigned DwarfDebug::RecordRegionStart(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001585 if (TimePassesIsEnabled)
1586 DebugTimer->startTimer();
1587
Devang Patel2a610c72009-08-25 05:24:07 +00001588 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001589 unsigned ID = MMI->NextLabelID();
1590 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1591 LexicalScopeStack.push_back(Scope);
1592
1593 if (TimePassesIsEnabled)
1594 DebugTimer->stopTimer();
1595
1596 return ID;
1597}
1598
1599/// RecordRegionEnd - Indicate the end of a region.
Devang Patel2a610c72009-08-25 05:24:07 +00001600unsigned DwarfDebug::RecordRegionEnd(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001601 if (TimePassesIsEnabled)
1602 DebugTimer->startTimer();
1603
Devang Patel2a610c72009-08-25 05:24:07 +00001604 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001605 unsigned ID = MMI->NextLabelID();
1606 Scope->setEndLabelID(ID);
Devang Pateldaf9e022009-06-13 02:16:18 +00001607 // FIXME : region.end() may not be in the last basic block.
1608 // For now, do not pop last lexical scope because next basic
1609 // block may start new inlined function's body.
1610 unsigned LSSize = LexicalScopeStack.size();
1611 if (LSSize != 0 && LSSize != 1)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001612 LexicalScopeStack.pop_back();
1613
1614 if (TimePassesIsEnabled)
1615 DebugTimer->stopTimer();
1616
1617 return ID;
1618}
1619
1620/// RecordVariable - Indicate the declaration of a local variable.
Devang Patel2a610c72009-08-25 05:24:07 +00001621void DwarfDebug::RecordVariable(MDNode *N, unsigned FrameIndex) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001622 if (TimePassesIsEnabled)
1623 DebugTimer->startTimer();
1624
Devang Patel2a610c72009-08-25 05:24:07 +00001625 DIDescriptor Desc(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001626 DbgScope *Scope = NULL;
1627 bool InlinedFnVar = false;
1628
Devang Patel2a610c72009-08-25 05:24:07 +00001629 if (Desc.getTag() == dwarf::DW_TAG_variable)
1630 Scope = getOrCreateScope(DIGlobalVariable(N).getContext().getNode());
1631 else {
Devang Patel24f20e02009-08-22 17:12:53 +00001632 bool InlinedVar = false;
Devang Patel2a610c72009-08-25 05:24:07 +00001633 MDNode *Context = DIVariable(N).getContext().getNode();
1634 DISubprogram SP(Context);
Devang Patel24f20e02009-08-22 17:12:53 +00001635 if (!SP.isNull()) {
1636 // SP is inserted into DbgAbstractScopeMap when inlined function
1637 // start was recorded by RecordInlineFnStart.
Devang Patel2a610c72009-08-25 05:24:07 +00001638 DenseMap<MDNode *, DbgScope *>::iterator
1639 I = DbgAbstractScopeMap.find(SP.getNode());
Devang Patel24f20e02009-08-22 17:12:53 +00001640 if (I != DbgAbstractScopeMap.end()) {
1641 InlinedVar = true;
1642 Scope = I->second;
1643 }
1644 }
Devang Patel2a610c72009-08-25 05:24:07 +00001645 if (!InlinedVar)
1646 Scope = getOrCreateScope(Context);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001647 }
1648
1649 assert(Scope && "Unable to find the variable's scope");
Devang Patel2a610c72009-08-25 05:24:07 +00001650 DbgVariable *DV = new DbgVariable(DIVariable(N), FrameIndex, InlinedFnVar);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001651 Scope->AddVariable(DV);
1652
1653 if (TimePassesIsEnabled)
1654 DebugTimer->stopTimer();
1655}
1656
1657//// RecordInlinedFnStart - Indicate the start of inlined subroutine.
1658unsigned DwarfDebug::RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
1659 unsigned Line, unsigned Col) {
1660 unsigned LabelID = MMI->NextLabelID();
1661
Chris Lattner33adcfb2009-08-22 21:43:10 +00001662 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001663 return LabelID;
1664
1665 if (TimePassesIsEnabled)
1666 DebugTimer->startTimer();
1667
Devang Patel2a610c72009-08-25 05:24:07 +00001668 MDNode *Node = SP.getNode();
1669 DenseMap<const MDNode *, DbgScope *>::iterator
1670 II = AbstractInstanceRootMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001671
1672 if (II == AbstractInstanceRootMap.end()) {
1673 // Create an abstract instance entry for this inlined function if it doesn't
1674 // already exist.
Devang Patel2a610c72009-08-25 05:24:07 +00001675 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001676
1677 // Get the compile unit context.
Devang Patel2a610c72009-08-25 05:24:07 +00001678 DIE *SPDie = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001679 if (!SPDie)
Devang Patel1dbc7712009-06-29 20:45:18 +00001680 SPDie = CreateSubprogramDIE(ModuleCU, SP, false, true);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001681
1682 // Mark as being inlined. This makes this subprogram entry an abstract
1683 // instance root.
1684 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
1685 // that it's defined. That probably won't change in the future. However,
1686 // this could be more elegant.
1687 AddUInt(SPDie, dwarf::DW_AT_inline, 0, dwarf::DW_INL_declared_not_inlined);
1688
1689 // Keep track of the abstract scope for this function.
Devang Patel2a610c72009-08-25 05:24:07 +00001690 DbgAbstractScopeMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001691
Devang Patel2a610c72009-08-25 05:24:07 +00001692 AbstractInstanceRootMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001693 AbstractInstanceRootList.push_back(Scope);
1694 }
1695
1696 // Create a concrete inlined instance for this inlined function.
Devang Patel2a610c72009-08-25 05:24:07 +00001697 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001698 DIE *ScopeDie = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel1dbc7712009-06-29 20:45:18 +00001699 ScopeDie->setAbstractCompileUnit(ModuleCU);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001700
Devang Patel2a610c72009-08-25 05:24:07 +00001701 DIE *Origin = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001702 AddDIEEntry(ScopeDie, dwarf::DW_AT_abstract_origin,
1703 dwarf::DW_FORM_ref4, Origin);
Devang Patel1dbc7712009-06-29 20:45:18 +00001704 AddUInt(ScopeDie, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001705 AddUInt(ScopeDie, dwarf::DW_AT_call_line, 0, Line);
1706 AddUInt(ScopeDie, dwarf::DW_AT_call_column, 0, Col);
1707
1708 ConcreteScope->setDie(ScopeDie);
1709 ConcreteScope->setStartLabelID(LabelID);
1710 MMI->RecordUsedDbgLabel(LabelID);
1711
1712 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
1713
1714 // Keep track of the concrete scope that's inlined into this function.
Devang Patel2a610c72009-08-25 05:24:07 +00001715 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
1716 SI = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001717
1718 if (SI == DbgConcreteScopeMap.end())
Devang Patel2a610c72009-08-25 05:24:07 +00001719 DbgConcreteScopeMap[Node].push_back(ConcreteScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001720 else
1721 SI->second.push_back(ConcreteScope);
1722
1723 // Track the start label for this inlined function.
Devang Patel2a610c72009-08-25 05:24:07 +00001724 DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
1725 I = InlineInfo.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001726
1727 if (I == InlineInfo.end())
Devang Patel2a610c72009-08-25 05:24:07 +00001728 InlineInfo[Node].push_back(LabelID);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001729 else
1730 I->second.push_back(LabelID);
1731
1732 if (TimePassesIsEnabled)
1733 DebugTimer->stopTimer();
1734
1735 return LabelID;
1736}
1737
1738/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
1739unsigned DwarfDebug::RecordInlinedFnEnd(DISubprogram &SP) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001740 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001741 return 0;
1742
1743 if (TimePassesIsEnabled)
1744 DebugTimer->startTimer();
1745
Devang Patel2a610c72009-08-25 05:24:07 +00001746 MDNode *Node = SP.getNode();
1747 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
1748 I = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001749
1750 if (I == DbgConcreteScopeMap.end()) {
1751 // FIXME: Can this situation actually happen? And if so, should it?
1752 if (TimePassesIsEnabled)
1753 DebugTimer->stopTimer();
1754
1755 return 0;
1756 }
1757
1758 SmallVector<DbgScope *, 8> &Scopes = I->second;
Devang Patel11a407f2009-06-15 21:45:50 +00001759 if (Scopes.empty()) {
1760 // Returned ID is 0 if this is unbalanced "end of inlined
1761 // scope". This could happen if optimizer eats dbg intrinsics
1762 // or "beginning of inlined scope" is not recoginized due to
1763 // missing location info. In such cases, ignore this region.end.
1764 return 0;
1765 }
1766
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001767 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
1768 unsigned ID = MMI->NextLabelID();
1769 MMI->RecordUsedDbgLabel(ID);
1770 Scope->setEndLabelID(ID);
1771
1772 if (TimePassesIsEnabled)
1773 DebugTimer->stopTimer();
1774
1775 return ID;
1776}
1777
Bill Wendling829e67b2009-05-20 23:22:40 +00001778//===----------------------------------------------------------------------===//
1779// Emit Methods
1780//===----------------------------------------------------------------------===//
1781
Bill Wendling94d04b82009-05-20 23:21:38 +00001782/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1783///
1784unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1785 // Get the children.
1786 const std::vector<DIE *> &Children = Die->getChildren();
1787
1788 // If not last sibling and has children then add sibling offset attribute.
1789 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1790
1791 // Record the abbreviation.
1792 AssignAbbrevNumber(Die->getAbbrev());
1793
1794 // Get the abbreviation for this DIE.
1795 unsigned AbbrevNumber = Die->getAbbrevNumber();
1796 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1797
1798 // Set DIE offset
1799 Die->setOffset(Offset);
1800
1801 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001802 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001803
1804 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1805 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1806
1807 // Size the DIE attribute values.
1808 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1809 // Size attribute value.
1810 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
1811
1812 // Size the DIE children if any.
1813 if (!Children.empty()) {
1814 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1815 "Children flag not set");
1816
1817 for (unsigned j = 0, M = Children.size(); j < M; ++j)
1818 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
1819
1820 // End of children marker.
1821 Offset += sizeof(int8_t);
1822 }
1823
1824 Die->setSize(Offset - Die->getOffset());
1825 return Offset;
1826}
1827
1828/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1829///
1830void DwarfDebug::SizeAndOffsets() {
1831 // Compute size of compile unit header.
1832 static unsigned Offset =
1833 sizeof(int32_t) + // Length of Compilation Unit Info
1834 sizeof(int16_t) + // DWARF version number
1835 sizeof(int32_t) + // Offset Into Abbrev. Section
1836 sizeof(int8_t); // Pointer Size (in bytes)
1837
Devang Patel1dbc7712009-06-29 20:45:18 +00001838 SizeAndOffsetDie(ModuleCU->getDie(), Offset, true);
1839 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00001840}
1841
1842/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1843/// tools to recognize the object file contains Dwarf information.
1844void DwarfDebug::EmitInitial() {
1845 // Check to see if we already emitted intial headers.
1846 if (didInitial) return;
1847 didInitial = true;
1848
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001849 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1850
Bill Wendling94d04b82009-05-20 23:21:38 +00001851 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001852 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001853 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001854 EmitLabel("section_debug_frame", 0);
1855 }
1856
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001857 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001858 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001859 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001860 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001861 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001862 EmitLabel("section_aranges", 0);
1863
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001864 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
1865 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00001866 EmitLabel("section_macinfo", 0);
1867 }
1868
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001869 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001870 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001871 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001872 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001873 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001874 EmitLabel("section_pubnames", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001875 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001876 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001877 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001878 EmitLabel("section_ranges", 0);
1879
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001880 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001881 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001882 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001883 EmitLabel("data_begin", 0);
1884}
1885
1886/// EmitDIE - Recusively Emits a debug information entry.
1887///
1888void DwarfDebug::EmitDIE(DIE *Die) {
1889 // Get the abbreviation for this DIE.
1890 unsigned AbbrevNumber = Die->getAbbrevNumber();
1891 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1892
1893 Asm->EOL();
1894
1895 // Emit the code (index) for the abbreviation.
1896 Asm->EmitULEB128Bytes(AbbrevNumber);
1897
1898 if (Asm->isVerbose())
1899 Asm->EOL(std::string("Abbrev [" +
1900 utostr(AbbrevNumber) +
1901 "] 0x" + utohexstr(Die->getOffset()) +
1902 ":0x" + utohexstr(Die->getSize()) + " " +
1903 dwarf::TagString(Abbrev->getTag())));
1904 else
1905 Asm->EOL();
1906
1907 SmallVector<DIEValue*, 32> &Values = Die->getValues();
1908 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1909
1910 // Emit the DIE attribute values.
1911 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1912 unsigned Attr = AbbrevData[i].getAttribute();
1913 unsigned Form = AbbrevData[i].getForm();
1914 assert(Form && "Too many attributes for DIE (check abbreviation)");
1915
1916 switch (Attr) {
1917 case dwarf::DW_AT_sibling:
1918 Asm->EmitInt32(Die->SiblingOffset());
1919 break;
1920 case dwarf::DW_AT_abstract_origin: {
1921 DIEEntry *E = cast<DIEEntry>(Values[i]);
1922 DIE *Origin = E->getEntry();
1923 unsigned Addr =
1924 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
1925 Origin->getOffset();
1926
1927 Asm->EmitInt32(Addr);
1928 break;
1929 }
1930 default:
1931 // Emit an attribute using the defined form.
1932 Values[i]->EmitValue(this, Form);
1933 break;
1934 }
1935
1936 Asm->EOL(dwarf::AttributeString(Attr));
1937 }
1938
1939 // Emit the DIE children if any.
1940 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1941 const std::vector<DIE *> &Children = Die->getChildren();
1942
1943 for (unsigned j = 0, M = Children.size(); j < M; ++j)
1944 EmitDIE(Children[j]);
1945
1946 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
1947 }
1948}
1949
1950/// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
1951///
1952void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) {
1953 DIE *Die = Unit->getDie();
1954
1955 // Emit the compile units header.
1956 EmitLabel("info_begin", Unit->getID());
1957
1958 // Emit size of content not including length itself
1959 unsigned ContentSize = Die->getSize() +
1960 sizeof(int16_t) + // DWARF version number
1961 sizeof(int32_t) + // Offset Into Abbrev. Section
1962 sizeof(int8_t) + // Pointer Size (in bytes)
1963 sizeof(int32_t); // FIXME - extra pad for gdb bug.
1964
1965 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
1966 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
1967 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
1968 Asm->EOL("Offset Into Abbrev. Section");
1969 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
1970
1971 EmitDIE(Die);
1972 // FIXME - extra padding for gdb bug.
1973 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
1974 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
1975 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
1976 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
1977 EmitLabel("info_end", Unit->getID());
1978
1979 Asm->EOL();
1980}
1981
1982void DwarfDebug::EmitDebugInfo() {
1983 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001984 Asm->OutStreamer.SwitchSection(
1985 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001986
Devang Patel1dbc7712009-06-29 20:45:18 +00001987 EmitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00001988}
1989
1990/// EmitAbbreviations - Emit the abbreviation section.
1991///
1992void DwarfDebug::EmitAbbreviations() const {
1993 // Check to see if it is worth the effort.
1994 if (!Abbreviations.empty()) {
1995 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001996 Asm->OutStreamer.SwitchSection(
1997 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001998
1999 EmitLabel("abbrev_begin", 0);
2000
2001 // For each abbrevation.
2002 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2003 // Get abbreviation data
2004 const DIEAbbrev *Abbrev = Abbreviations[i];
2005
2006 // Emit the abbrevations code (base 1 index.)
2007 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2008 Asm->EOL("Abbreviation Code");
2009
2010 // Emit the abbreviations data.
2011 Abbrev->Emit(Asm);
2012
2013 Asm->EOL();
2014 }
2015
2016 // Mark end of abbreviations.
2017 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2018
2019 EmitLabel("abbrev_end", 0);
2020 Asm->EOL();
2021 }
2022}
2023
2024/// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2025/// the line matrix.
2026///
2027void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) {
2028 // Define last address of section.
2029 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2030 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2031 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2032 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2033
2034 // Mark end of matrix.
2035 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2036 Asm->EmitULEB128Bytes(1); Asm->EOL();
2037 Asm->EmitInt8(1); Asm->EOL();
2038}
2039
2040/// EmitDebugLines - Emit source line information.
2041///
2042void DwarfDebug::EmitDebugLines() {
2043 // If the target is using .loc/.file, the assembler will be emitting the
2044 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002045 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002046 return;
2047
2048 // Minimum line delta, thus ranging from -10..(255-10).
2049 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2050 // Maximum line delta, thus ranging from -10..(255-10).
2051 const int MaxLineDelta = 255 + MinLineDelta;
2052
2053 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002054 Asm->OutStreamer.SwitchSection(
2055 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002056
2057 // Construct the section header.
2058 EmitDifference("line_end", 0, "line_begin", 0, true);
2059 Asm->EOL("Length of Source Line Info");
2060 EmitLabel("line_begin", 0);
2061
2062 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2063
2064 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2065 Asm->EOL("Prolog Length");
2066 EmitLabel("line_prolog_begin", 0);
2067
2068 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2069
2070 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2071
2072 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2073
2074 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2075
2076 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2077
2078 // Line number standard opcode encodings argument count
2079 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2080 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2081 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2082 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2083 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2084 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2085 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2086 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2087 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2088
2089 // Emit directories.
2090 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2091 Asm->EmitString(getSourceDirectoryName(DI));
2092 Asm->EOL("Directory");
2093 }
2094
2095 Asm->EmitInt8(0); Asm->EOL("End of directories");
2096
2097 // Emit files.
2098 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2099 // Remember source id starts at 1.
2100 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2101 Asm->EmitString(getSourceFileName(Id.second));
2102 Asm->EOL("Source");
2103 Asm->EmitULEB128Bytes(Id.first);
2104 Asm->EOL("Directory #");
2105 Asm->EmitULEB128Bytes(0);
2106 Asm->EOL("Mod date");
2107 Asm->EmitULEB128Bytes(0);
2108 Asm->EOL("File size");
2109 }
2110
2111 Asm->EmitInt8(0); Asm->EOL("End of files");
2112
2113 EmitLabel("line_prolog_end", 0);
2114
2115 // A sequence for each text section.
2116 unsigned SecSrcLinesSize = SectionSourceLines.size();
2117
2118 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2119 // Isolate current sections line info.
2120 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2121
Chris Lattner93b6db32009-08-08 23:39:42 +00002122 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002123 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002124 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002125 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002126 }*/
2127 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002128
2129 // Dwarf assumes we start with first line of first source file.
2130 unsigned Source = 1;
2131 unsigned Line = 1;
2132
2133 // Construct rows of the address, source, line, column matrix.
2134 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2135 const SrcLineInfo &LineInfo = LineInfos[i];
2136 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2137 if (!LabelID) continue;
2138
2139 if (!Asm->isVerbose())
2140 Asm->EOL();
2141 else {
2142 std::pair<unsigned, unsigned> SourceID =
2143 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002144 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002145 << getSourceDirectoryName(SourceID.first) << ' '
2146 << getSourceFileName(SourceID.second)
2147 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2148 }
2149
2150 // Define the line address.
2151 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2152 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2153 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2154 EmitReference("label", LabelID); Asm->EOL("Location label");
2155
2156 // If change of source, then switch to the new source.
2157 if (Source != LineInfo.getSourceID()) {
2158 Source = LineInfo.getSourceID();
2159 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2160 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2161 }
2162
2163 // If change of line.
2164 if (Line != LineInfo.getLine()) {
2165 // Determine offset.
2166 int Offset = LineInfo.getLine() - Line;
2167 int Delta = Offset - MinLineDelta;
2168
2169 // Update line.
2170 Line = LineInfo.getLine();
2171
2172 // If delta is small enough and in range...
2173 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2174 // ... then use fast opcode.
2175 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2176 } else {
2177 // ... otherwise use long hand.
2178 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2179 Asm->EOL("DW_LNS_advance_line");
2180 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2181 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2182 }
2183 } else {
2184 // Copy the previous row (different address or source)
2185 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2186 }
2187 }
2188
2189 EmitEndOfLineMatrix(j + 1);
2190 }
2191
2192 if (SecSrcLinesSize == 0)
2193 // Because we're emitting a debug_line section, we still need a line
2194 // table. The linker and friends expect it to exist. If there's nothing to
2195 // put into it, emit an empty table.
2196 EmitEndOfLineMatrix(1);
2197
2198 EmitLabel("line_end", 0);
2199 Asm->EOL();
2200}
2201
2202/// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2203///
2204void DwarfDebug::EmitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002205 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002206 return;
2207
2208 int stackGrowth =
2209 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2210 TargetFrameInfo::StackGrowsUp ?
2211 TD->getPointerSize() : -TD->getPointerSize();
2212
2213 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002214 Asm->OutStreamer.SwitchSection(
2215 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002216
2217 EmitLabel("debug_frame_common", 0);
2218 EmitDifference("debug_frame_common_end", 0,
2219 "debug_frame_common_begin", 0, true);
2220 Asm->EOL("Length of Common Information Entry");
2221
2222 EmitLabel("debug_frame_common_begin", 0);
2223 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2224 Asm->EOL("CIE Identifier Tag");
2225 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2226 Asm->EOL("CIE Version");
2227 Asm->EmitString("");
2228 Asm->EOL("CIE Augmentation");
2229 Asm->EmitULEB128Bytes(1);
2230 Asm->EOL("CIE Code Alignment Factor");
2231 Asm->EmitSLEB128Bytes(stackGrowth);
2232 Asm->EOL("CIE Data Alignment Factor");
2233 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2234 Asm->EOL("CIE RA Column");
2235
2236 std::vector<MachineMove> Moves;
2237 RI->getInitialFrameState(Moves);
2238
2239 EmitFrameMoves(NULL, 0, Moves, false);
2240
2241 Asm->EmitAlignment(2, 0, 0, false);
2242 EmitLabel("debug_frame_common_end", 0);
2243
2244 Asm->EOL();
2245}
2246
2247/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2248/// section.
2249void
2250DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002251 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002252 return;
2253
2254 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002255 Asm->OutStreamer.SwitchSection(
2256 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002257
2258 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2259 "debug_frame_begin", DebugFrameInfo.Number, true);
2260 Asm->EOL("Length of Frame Information Entry");
2261
2262 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2263
2264 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2265 0, 0, true, false);
2266 Asm->EOL("FDE CIE offset");
2267
2268 EmitReference("func_begin", DebugFrameInfo.Number);
2269 Asm->EOL("FDE initial location");
2270 EmitDifference("func_end", DebugFrameInfo.Number,
2271 "func_begin", DebugFrameInfo.Number);
2272 Asm->EOL("FDE address range");
2273
2274 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2275 false);
2276
2277 Asm->EmitAlignment(2, 0, 0, false);
2278 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2279
2280 Asm->EOL();
2281}
2282
2283void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2284 EmitDifference("pubnames_end", Unit->getID(),
2285 "pubnames_begin", Unit->getID(), true);
2286 Asm->EOL("Length of Public Names Info");
2287
2288 EmitLabel("pubnames_begin", Unit->getID());
2289
2290 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2291
2292 EmitSectionOffset("info_begin", "section_info",
2293 Unit->getID(), 0, true, false);
2294 Asm->EOL("Offset of Compilation Unit Info");
2295
2296 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2297 true);
2298 Asm->EOL("Compilation Unit Length");
2299
2300 StringMap<DIE*> &Globals = Unit->getGlobals();
2301 for (StringMap<DIE*>::const_iterator
2302 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2303 const char *Name = GI->getKeyData();
2304 DIE * Entity = GI->second;
2305
2306 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2307 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2308 }
2309
2310 Asm->EmitInt32(0); Asm->EOL("End Mark");
2311 EmitLabel("pubnames_end", Unit->getID());
2312
2313 Asm->EOL();
2314}
2315
2316/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2317///
2318void DwarfDebug::EmitDebugPubNames() {
2319 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002320 Asm->OutStreamer.SwitchSection(
2321 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002322
Devang Patel1dbc7712009-06-29 20:45:18 +00002323 EmitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002324}
2325
2326/// EmitDebugStr - Emit visible names into a debug str section.
2327///
2328void DwarfDebug::EmitDebugStr() {
2329 // Check to see if it is worth the effort.
2330 if (!StringPool.empty()) {
2331 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002332 Asm->OutStreamer.SwitchSection(
2333 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002334
2335 // For each of strings in the string pool.
2336 for (unsigned StringID = 1, N = StringPool.size();
2337 StringID <= N; ++StringID) {
2338 // Emit a label for reference from debug information entries.
2339 EmitLabel("string", StringID);
2340
2341 // Emit the string itself.
2342 const std::string &String = StringPool[StringID];
2343 Asm->EmitString(String); Asm->EOL();
2344 }
2345
2346 Asm->EOL();
2347 }
2348}
2349
2350/// EmitDebugLoc - Emit visible names into a debug loc section.
2351///
2352void DwarfDebug::EmitDebugLoc() {
2353 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002354 Asm->OutStreamer.SwitchSection(
2355 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002356 Asm->EOL();
2357}
2358
2359/// EmitDebugARanges - Emit visible names into a debug aranges section.
2360///
2361void DwarfDebug::EmitDebugARanges() {
2362 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002363 Asm->OutStreamer.SwitchSection(
2364 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002365
2366 // FIXME - Mock up
2367#if 0
2368 CompileUnit *Unit = GetBaseCompileUnit();
2369
2370 // Don't include size of length
2371 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2372
2373 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2374
2375 EmitReference("info_begin", Unit->getID());
2376 Asm->EOL("Offset of Compilation Unit Info");
2377
2378 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2379
2380 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2381
2382 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2383 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2384
2385 // Range 1
2386 EmitReference("text_begin", 0); Asm->EOL("Address");
2387 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2388
2389 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2390 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2391#endif
2392
2393 Asm->EOL();
2394}
2395
2396/// EmitDebugRanges - Emit visible names into a debug ranges section.
2397///
2398void DwarfDebug::EmitDebugRanges() {
2399 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002400 Asm->OutStreamer.SwitchSection(
2401 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002402 Asm->EOL();
2403}
2404
2405/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2406///
2407void DwarfDebug::EmitDebugMacInfo() {
Chris Lattner18a4c162009-08-02 07:24:22 +00002408 if (const MCSection *LineInfo =
2409 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002410 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002411 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002412 Asm->EOL();
2413 }
2414}
2415
2416/// EmitDebugInlineInfo - Emit inline info using following format.
2417/// Section Header:
2418/// 1. length of section
2419/// 2. Dwarf version number
2420/// 3. address size.
2421///
2422/// Entries (one "entry" for each function that was inlined):
2423///
2424/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2425/// otherwise offset into __debug_str for regular function name.
2426/// 2. offset into __debug_str section for regular function name.
2427/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2428/// instances for the function.
2429///
2430/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2431/// inlined instance; the die_offset points to the inlined_subroutine die in the
2432/// __debug_info section, and the low_pc is the starting address for the
2433/// inlining instance.
2434void DwarfDebug::EmitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002435 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002436 return;
2437
Devang Patel1dbc7712009-06-29 20:45:18 +00002438 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002439 return;
2440
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002441 Asm->OutStreamer.SwitchSection(
2442 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002443 Asm->EOL();
2444 EmitDifference("debug_inlined_end", 1,
2445 "debug_inlined_begin", 1, true);
2446 Asm->EOL("Length of Debug Inlined Information Entry");
2447
2448 EmitLabel("debug_inlined_begin", 1);
2449
2450 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2451 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2452
Devang Patel2a610c72009-08-25 05:24:07 +00002453 for (DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
Bill Wendling94d04b82009-05-20 23:21:38 +00002454 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
Devang Patel2a610c72009-08-25 05:24:07 +00002455 MDNode *Node = I->first;
Bill Wendling94d04b82009-05-20 23:21:38 +00002456 SmallVector<unsigned, 4> &Labels = I->second;
Devang Patel2a610c72009-08-25 05:24:07 +00002457 DISubprogram SP(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00002458 std::string Name;
2459 std::string LName;
2460
2461 SP.getLinkageName(LName);
2462 SP.getName(Name);
2463
Devang Patel53cb17d2009-07-16 01:01:22 +00002464 if (LName.empty())
2465 Asm->EmitString(Name);
2466 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002467 // Skip special LLVM prefix that is used to inform the asm printer to not
2468 // emit usual symbol prefix before the symbol name. This happens for
2469 // Objective-C symbol names and symbol whose name is replaced using GCC's
2470 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002471 if (LName[0] == 1)
2472 LName = &LName[1];
2473 Asm->EmitString(LName);
2474 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002475 Asm->EOL("MIPS linkage name");
2476
2477 Asm->EmitString(Name); Asm->EOL("Function name");
2478
2479 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2480
2481 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2482 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel2a610c72009-08-25 05:24:07 +00002483 DIE *SP = ModuleCU->getDieMapSlotFor(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00002484 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2485
2486 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002487 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002488 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002489 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002490
2491 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2492 }
2493 }
2494
2495 EmitLabel("debug_inlined_end", 1);
2496 Asm->EOL();
2497}