blob: 40ff3e49cf76fc9688873b965652ada1d45f0a85 [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Devang Patele4b27562009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000026#include "llvm/Support/Timer.h"
Devang Patele4b27562009-08-28 23:24:31 +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 Patele4b27562009-08-28 23:24:31 +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 Patele4b27562009-08-28 23:24:31 +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 Patele4b27562009-08-28 23:24:31 +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 Patele4b27562009-08-28 23:24:31 +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 Patele4b27562009-08-28 23:24:31 +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
Caroline Ticedc8f6042009-08-31 21:19:37 +0000503/* Byref variables, in Blocks, are declared by the programmer as
504 "SomeType VarName;", but the compiler creates a
505 __Block_byref_x_VarName struct, and gives the variable VarName
506 either the struct, or a pointer to the struct, as its type. This
507 is necessary for various behind-the-scenes things the compiler
508 needs to do with by-reference variables in blocks.
509
510 However, as far as the original *programmer* is concerned, the
511 variable should still have type 'SomeType', as originally declared.
512
513 The following function dives into the __Block_byref_x_VarName
514 struct to find the original type of the variable. This will be
515 passed back to the code generating the type for the Debug
516 Information Entry for the variable 'VarName'. 'VarName' will then
517 have the original type 'SomeType' in its debug information.
518
519 The original type 'SomeType' will be the type of the field named
520 'VarName' inside the __Block_byref_x_VarName struct.
521
522 NOTE: In order for this to not completely fail on the debugger
523 side, the Debug Information Entry for the variable VarName needs to
524 have a DW_AT_location that tells the debugger how to unwind through
525 the pointers and __Block_byref_x_VarName struct to find the actual
526 value of the variable. The function AddBlockByrefType does this. */
527
528/// Find the type the programmer originally declared the variable to be
529/// and return that type.
530///
531DIType DwarfDebug::GetBlockByrefType(DIType Ty, std::string Name) {
532
533 DIType subType = Ty;
534 unsigned tag = Ty.getTag();
535
536 if (tag == dwarf::DW_TAG_pointer_type) {
537 DIDerivedType DTy = DIDerivedType (Ty.getNode());
538 subType = DTy.getTypeDerivedFrom();
539 }
540
541 DICompositeType blockStruct = DICompositeType(subType.getNode());
542
543 DIArray Elements = blockStruct.getTypeArray();
544
545 if (Elements.isNull())
546 return Ty;
547
548 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
549 DIDescriptor Element = Elements.getElement(i);
550 DIDerivedType DT = DIDerivedType(Element.getNode());
551 std::string Name2;
552 DT.getName(Name2);
553 if (Name == Name2)
554 return (DT.getTypeDerivedFrom());
555 }
556
557 return Ty;
558}
559
560/* Byref variables, in Blocks, are declared by the programmer as "SomeType
561 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
562 gives the variable VarName either the struct, or a pointer to the struct, as
563 its type. This is necessary for various behind-the-scenes things the
564 compiler needs to do with by-reference variables in Blocks.
565
566 However, as far as the original *programmer* is concerned, the variable
567 should still have type 'SomeType', as originally declared.
568
569 The function GetBlockByrefType dives into the __Block_byref_x_VarName
570 struct to find the original type of the variable, which is then assigned to
571 the variable's Debug Information Entry as its real type. So far, so good.
572 However now the debugger will expect the variable VarName to have the type
573 SomeType. So we need the location attribute for the variable to be an
574 expression that explains to the debugger how to navigate through the
575 pointers and struct to find the actual variable of type SomeType.
576
577 The following function does just that. We start by getting
578 the "normal" location for the variable. This will be the location
579 of either the struct __Block_byref_x_VarName or the pointer to the
580 struct __Block_byref_x_VarName.
581
582 The struct will look something like:
583
584 struct __Block_byref_x_VarName {
585 ... <various fields>
586 struct __Block_byref_x_VarName *forwarding;
587 ... <various other fields>
588 SomeType VarName;
589 ... <maybe more fields>
590 };
591
592 If we are given the struct directly (as our starting point) we
593 need to tell the debugger to:
594
595 1). Add the offset of the forwarding field.
596
597 2). Follow that pointer to get the the real __Block_byref_x_VarName
598 struct to use (the real one may have been copied onto the heap).
599
600 3). Add the offset for the field VarName, to find the actual variable.
601
602 If we started with a pointer to the struct, then we need to
603 dereference that pointer first, before the other steps.
604 Translating this into DWARF ops, we will need to append the following
605 to the current location description for the variable:
606
607 DW_OP_deref -- optional, if we start with a pointer
608 DW_OP_plus_uconst <forward_fld_offset>
609 DW_OP_deref
610 DW_OP_plus_uconst <varName_fld_offset>
611
612 That is what this function does. */
613
614/// AddBlockByrefAddress - Start with the address based on the location
615/// provided, and generate the DWARF information necessary to find the
616/// actual Block variable (navigating the Block struct) based on the
617/// starting location. Add the DWARF information to the die. For
618/// more information, read large comment just above here.
619///
620void DwarfDebug::AddBlockByrefAddress(DbgVariable *&DV, DIE *Die,
621 unsigned Attribute,
622 const MachineLocation &Location) {
623 const DIVariable &VD = DV->getVariable();
624 DIType Ty = VD.getType();
625 DIType TmpTy = Ty;
626 unsigned Tag = Ty.getTag();
627 bool isPointer = false;
628
629 std::string varName;
630 VD.getName(varName);
631
632 if (Tag == dwarf::DW_TAG_pointer_type) {
633 DIDerivedType DTy = DIDerivedType (Ty.getNode());
634 TmpTy = DTy.getTypeDerivedFrom();
635 isPointer = true;
636 }
637
638 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
639
640 std::string typeName;
641 blockStruct.getName(typeName);
642
643 assert(typeName.find ("__Block_byref_") == 0
644 && "Attempting to get Block location of non-Block variable!");
645
646 // Find the __forwarding field and the variable field in the __Block_byref
647 // struct.
648
649 DIArray Fields = blockStruct.getTypeArray();
650 DIDescriptor varField = DIDescriptor();
651 DIDescriptor forwardingField = DIDescriptor();
652
653
654 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
655 DIDescriptor Element = Fields.getElement(i);
656 DIDerivedType DT = DIDerivedType(Element.getNode());
657 std::string fieldName;
658 DT.getName(fieldName);
659 if (fieldName == "__forwarding")
660 forwardingField = Element;
661 else if (fieldName == varName)
662 varField = Element;
663 }
664
665 assert (!varField.isNull() && "Can't find byref variable in Block struct");
666 assert (!forwardingField.isNull()
667 && "Can't find forwarding field in Block struct");
668
669 // Get the offsets for the forwarding field and the variable field.
670
671 unsigned int forwardingFieldOffset =
672 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
673 unsigned int varFieldOffset =
674 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
675
676 // Decode the original location, and use that as the start of the
677 // byref variable's location.
678
679 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
680 DIEBlock *Block = new DIEBlock();
681
682 if (Location.isReg()) {
683 if (Reg < 32)
684 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
685 else {
686 Reg = Reg - dwarf::DW_OP_reg0;
687 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
688 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
689 }
690 } else {
691 if (Reg < 32)
692 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
693 else {
694 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
695 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
696 }
697
698 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
699 }
700
701 // If we started with a pointer to the__Block_byref... struct, then
702 // the first thing we need to do is dereference the pointer (DW_OP_deref).
703
704 if (isPointer)
705 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
706
707 // Next add the offset for the '__forwarding' field:
708 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
709 // adding the offset if it's 0.
710
711 if (forwardingFieldOffset > 0) {
712 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
713 AddUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
714 }
715
716 // Now dereference the __forwarding field to get to the real __Block_byref
717 // struct: DW_OP_deref.
718
719 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
720
721 // Now that we've got the real __Block_byref... struct, add the offset
722 // for the variable's field to get to the location of the actual variable:
723 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
724
725 if (varFieldOffset > 0) {
726 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
727 AddUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
728 }
729
730 // Now attach the location information to the DIE.
731
732 AddBlock(Die, Attribute, 0, Block);
733}
734
Bill Wendling0310d762009-05-15 09:23:25 +0000735/// AddAddress - Add an address attribute to a die based on the location
736/// provided.
737void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute,
738 const MachineLocation &Location) {
739 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
740 DIEBlock *Block = new DIEBlock();
741
742 if (Location.isReg()) {
743 if (Reg < 32) {
744 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
745 } else {
746 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
747 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
748 }
749 } else {
750 if (Reg < 32) {
751 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
752 } else {
753 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
754 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
755 }
756
757 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
758 }
759
760 AddBlock(Die, Attribute, 0, Block);
761}
762
763/// AddType - Add a new type attribute to the specified entity.
764void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
765 if (Ty.isNull())
766 return;
767
768 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +0000769 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000770
771 // If it exists then use the existing value.
772 if (Slot) {
773 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
774 return;
775 }
776
777 // Set up proxy.
Bill Wendling995f80a2009-05-20 23:24:48 +0000778 Slot = CreateDIEEntry();
Bill Wendling0310d762009-05-15 09:23:25 +0000779
780 // Construct type.
781 DIE Buffer(dwarf::DW_TAG_base_type);
Devang Patel6ceea332009-08-31 18:49:10 +0000782 if (Ty.isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000783 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getNode()));
Devang Patel6ceea332009-08-31 18:49:10 +0000784 else if (Ty.isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000785 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000786 else {
Devang Patel6ceea332009-08-31 18:49:10 +0000787 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patele4b27562009-08-28 23:24:31 +0000788 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getNode()));
789
Bill Wendling0310d762009-05-15 09:23:25 +0000790 }
791
792 // Add debug information entry to entity and appropriate context.
793 DIE *Die = NULL;
794 DIDescriptor Context = Ty.getContext();
795 if (!Context.isNull())
Devang Patele4b27562009-08-28 23:24:31 +0000796 Die = DW_Unit->getDieMapSlotFor(Context.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000797
798 if (Die) {
799 DIE *Child = new DIE(Buffer);
800 Die->AddChild(Child);
801 Buffer.Detach();
802 SetDIEEntry(Slot, Child);
803 } else {
804 Die = DW_Unit->AddDie(Buffer);
805 SetDIEEntry(Slot, Die);
806 }
807
808 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
809}
810
811/// ConstructTypeDIE - Construct basic type die from DIBasicType.
812void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
813 DIBasicType BTy) {
814 // Get core information.
815 std::string Name;
816 BTy.getName(Name);
817 Buffer.setTag(dwarf::DW_TAG_base_type);
818 AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
819 BTy.getEncoding());
820
821 // Add name if not anonymous or intermediate type.
822 if (!Name.empty())
823 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
824 uint64_t Size = BTy.getSizeInBits() >> 3;
825 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
826}
827
828/// ConstructTypeDIE - Construct derived type die from DIDerivedType.
829void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
830 DIDerivedType DTy) {
831 // Get core information.
832 std::string Name;
833 DTy.getName(Name);
834 uint64_t Size = DTy.getSizeInBits() >> 3;
835 unsigned Tag = DTy.getTag();
836
837 // FIXME - Workaround for templates.
838 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
839
840 Buffer.setTag(Tag);
841
842 // Map to main type, void will not have a type.
843 DIType FromTy = DTy.getTypeDerivedFrom();
844 AddType(DW_Unit, &Buffer, FromTy);
845
846 // Add name if not anonymous or intermediate type.
847 if (!Name.empty())
848 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
849
850 // Add size if non-zero (derived types might be zero-sized.)
851 if (Size)
852 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
853
854 // Add source line info if available and TyDesc is not a forward declaration.
855 if (!DTy.isForwardDecl())
856 AddSourceLine(&Buffer, &DTy);
857}
858
859/// ConstructTypeDIE - Construct type DIE from DICompositeType.
860void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
861 DICompositeType CTy) {
862 // Get core information.
863 std::string Name;
864 CTy.getName(Name);
865
866 uint64_t Size = CTy.getSizeInBits() >> 3;
867 unsigned Tag = CTy.getTag();
868 Buffer.setTag(Tag);
869
870 switch (Tag) {
871 case dwarf::DW_TAG_vector_type:
872 case dwarf::DW_TAG_array_type:
873 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
874 break;
875 case dwarf::DW_TAG_enumeration_type: {
876 DIArray Elements = CTy.getTypeArray();
877
878 // Add enumerators to enumeration type.
879 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
880 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000881 DIEnumerator Enum(Elements.getElement(i).getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000882 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
883 Buffer.AddChild(ElemDie);
884 }
885 }
886 break;
887 case dwarf::DW_TAG_subroutine_type: {
888 // Add return type.
889 DIArray Elements = CTy.getTypeArray();
890 DIDescriptor RTy = Elements.getElement(0);
Devang Patele4b27562009-08-28 23:24:31 +0000891 AddType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000892
893 // Add prototype flag.
894 AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
895
896 // Add arguments.
897 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
898 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
899 DIDescriptor Ty = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000900 AddType(DW_Unit, Arg, DIType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000901 Buffer.AddChild(Arg);
902 }
903 }
904 break;
905 case dwarf::DW_TAG_structure_type:
906 case dwarf::DW_TAG_union_type:
907 case dwarf::DW_TAG_class_type: {
908 // Add elements to structure type.
909 DIArray Elements = CTy.getTypeArray();
910
911 // A forward struct declared type may not have elements available.
912 if (Elements.isNull())
913 break;
914
915 // Add elements to structure type.
916 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
917 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000918 if (Element.isNull())
919 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000920 DIE *ElemDie = NULL;
921 if (Element.getTag() == dwarf::DW_TAG_subprogram)
922 ElemDie = CreateSubprogramDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000923 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000924 else
925 ElemDie = CreateMemberDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000926 DIDerivedType(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000927 Buffer.AddChild(ElemDie);
928 }
929
Devang Patela1ba2692009-08-27 23:51:51 +0000930 if (CTy.isAppleBlockExtension())
Bill Wendling0310d762009-05-15 09:23:25 +0000931 AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
932
933 unsigned RLang = CTy.getRunTimeLang();
934 if (RLang)
935 AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
936 dwarf::DW_FORM_data1, RLang);
937 break;
938 }
939 default:
940 break;
941 }
942
943 // Add name if not anonymous or intermediate type.
944 if (!Name.empty())
945 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
946
947 if (Tag == dwarf::DW_TAG_enumeration_type ||
948 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
949 // Add size if non-zero (derived types might be zero-sized.)
950 if (Size)
951 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
952 else {
953 // Add zero size if it is not a forward declaration.
954 if (CTy.isForwardDecl())
955 AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
956 else
957 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
958 }
959
960 // Add source line info if available.
961 if (!CTy.isForwardDecl())
962 AddSourceLine(&Buffer, &CTy);
963 }
964}
965
966/// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
967void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
968 int64_t L = SR.getLo();
969 int64_t H = SR.getHi();
970 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
971
Devang Patel6325a532009-08-14 20:59:16 +0000972 AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
973 if (L)
974 AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
975 if (H)
976 AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000977
978 Buffer.AddChild(DW_Subrange);
979}
980
981/// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
982void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
983 DICompositeType *CTy) {
984 Buffer.setTag(dwarf::DW_TAG_array_type);
985 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
986 AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
987
988 // Emit derived type.
989 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
990 DIArray Elements = CTy->getTypeArray();
991
992 // Construct an anonymous type for index type.
993 DIE IdxBuffer(dwarf::DW_TAG_base_type);
994 AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
995 AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
996 dwarf::DW_ATE_signed);
997 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
998
999 // Add subranges to array type.
1000 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1001 DIDescriptor Element = Elements.getElement(i);
1002 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patele4b27562009-08-28 23:24:31 +00001003 ConstructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IndexTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001004 }
1005}
1006
1007/// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1008DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
1009 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1010 std::string Name;
1011 ETy->getName(Name);
1012 AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1013 int64_t Value = ETy->getEnumValue();
1014 AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1015 return Enumerator;
1016}
1017
1018/// CreateGlobalVariableDIE - Create new DIE using GV.
1019DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit,
1020 const DIGlobalVariable &GV) {
1021 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
1022 std::string Name;
1023 GV.getDisplayName(Name);
1024 AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1025 std::string LinkageName;
1026 GV.getLinkageName(LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001027 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001028 // Skip special LLVM prefix that is used to inform the asm printer to not
1029 // emit usual symbol prefix before the symbol name. This happens for
1030 // Objective-C symbol names and symbol whose name is replaced using GCC's
1031 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001032 if (LinkageName[0] == 1)
1033 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001034 AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001035 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001036 }
1037 AddType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001038 if (!GV.isLocalToUnit())
1039 AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1040 AddSourceLine(GVDie, &GV);
1041 return GVDie;
1042}
1043
1044/// CreateMemberDIE - Create new member DIE.
1045DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
1046 DIE *MemberDie = new DIE(DT.getTag());
1047 std::string Name;
1048 DT.getName(Name);
1049 if (!Name.empty())
1050 AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1051
1052 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1053
1054 AddSourceLine(MemberDie, &DT);
1055
1056 uint64_t Size = DT.getSizeInBits();
1057 uint64_t FieldSize = DT.getOriginalTypeSize();
1058
1059 if (Size != FieldSize) {
1060 // Handle bitfield.
1061 AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1062 AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1063
1064 uint64_t Offset = DT.getOffsetInBits();
1065 uint64_t FieldOffset = Offset;
1066 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1067 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1068 FieldOffset = (HiMark - FieldSize);
1069 Offset -= FieldOffset;
1070
1071 // Maybe we need to work from the other end.
1072 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1073 AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1074 }
1075
1076 DIEBlock *Block = new DIEBlock();
1077 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1078 AddUInt(Block, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1079 AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, Block);
1080
1081 if (DT.isProtected())
1082 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1083 dwarf::DW_ACCESS_protected);
1084 else if (DT.isPrivate())
1085 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1086 dwarf::DW_ACCESS_private);
1087
1088 return MemberDie;
1089}
1090
1091/// CreateSubprogramDIE - Create new DIE using SP.
1092DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit,
1093 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +00001094 bool IsConstructor,
1095 bool IsInlined) {
Bill Wendling0310d762009-05-15 09:23:25 +00001096 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1097
1098 std::string Name;
1099 SP.getName(Name);
1100 AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1101
1102 std::string LinkageName;
1103 SP.getLinkageName(LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001104 if (!LinkageName.empty()) {
1105 // Skip special LLVM prefix that is used to inform the asm printer to not emit
1106 // usual symbol prefix before the symbol name. This happens for Objective-C
1107 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
1108 if (LinkageName[0] == 1)
1109 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001110 AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001111 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001112 }
Bill Wendling0310d762009-05-15 09:23:25 +00001113 AddSourceLine(SPDie, &SP);
1114
1115 DICompositeType SPTy = SP.getType();
1116 DIArray Args = SPTy.getTypeArray();
1117
1118 // Add prototyped tag, if C or ObjC.
1119 unsigned Lang = SP.getCompileUnit().getLanguage();
1120 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1121 Lang == dwarf::DW_LANG_ObjC)
1122 AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
1123
1124 // Add Return Type.
1125 unsigned SPTag = SPTy.getTag();
1126 if (!IsConstructor) {
1127 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1128 AddType(DW_Unit, SPDie, SPTy);
1129 else
Devang Patele4b27562009-08-28 23:24:31 +00001130 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001131 }
1132
1133 if (!SP.isDefinition()) {
1134 AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1135
1136 // Add arguments. Do not add arguments for subprogram definition. They will
1137 // be handled through RecordVariable.
1138 if (SPTag == dwarf::DW_TAG_subroutine_type)
1139 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1140 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patele4b27562009-08-28 23:24:31 +00001141 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001142 AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1143 SPDie->AddChild(Arg);
1144 }
1145 }
1146
Bill Wendling6679ee42009-05-18 22:02:36 +00001147 if (!SP.isLocalToUnit() && !IsInlined)
Bill Wendling0310d762009-05-15 09:23:25 +00001148 AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1149
1150 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patele4b27562009-08-28 23:24:31 +00001151 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001152 Slot = SPDie;
1153 return SPDie;
1154}
1155
1156/// FindCompileUnit - Get the compile unit for the given descriptor.
1157///
1158CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const {
1159 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001160 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001161 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1162 return *I->second;
1163}
1164
Bill Wendling995f80a2009-05-20 23:24:48 +00001165/// CreateDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +00001166///
Bill Wendling995f80a2009-05-20 23:24:48 +00001167DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001168 // Get the descriptor.
1169 const DIVariable &VD = DV->getVariable();
1170
1171 // Translate tag to proper Dwarf tag. The result variable is dropped for
1172 // now.
1173 unsigned Tag;
1174 switch (VD.getTag()) {
1175 case dwarf::DW_TAG_return_variable:
1176 return NULL;
1177 case dwarf::DW_TAG_arg_variable:
1178 Tag = dwarf::DW_TAG_formal_parameter;
1179 break;
1180 case dwarf::DW_TAG_auto_variable: // fall thru
1181 default:
1182 Tag = dwarf::DW_TAG_variable;
1183 break;
1184 }
1185
1186 // Define variable debug information entry.
1187 DIE *VariableDie = new DIE(Tag);
1188 std::string Name;
1189 VD.getName(Name);
1190 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1191
1192 // Add source line info if available.
1193 AddSourceLine(VariableDie, &VD);
1194
1195 // Add variable type.
Caroline Ticedc8f6042009-08-31 21:19:37 +00001196 if (VD.isBlockByrefVariable())
1197 AddType(Unit, VariableDie, GetBlockByrefType(VD.getType(), Name));
1198 else
1199 AddType(Unit, VariableDie, VD.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001200
1201 // Add variable address.
Bill Wendling1180c782009-05-18 23:08:55 +00001202 if (!DV->isInlinedFnVar()) {
1203 // Variables for abstract instances of inlined functions don't get a
1204 // location.
1205 MachineLocation Location;
1206 Location.set(RI->getFrameRegister(*MF),
1207 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Caroline Ticedc8f6042009-08-31 21:19:37 +00001208
1209 if (VD.isBlockByrefVariable())
1210 AddBlockByrefAddress (DV, VariableDie, dwarf::DW_AT_location, Location);
1211 else
1212 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling1180c782009-05-18 23:08:55 +00001213 }
Bill Wendling0310d762009-05-15 09:23:25 +00001214
1215 return VariableDie;
1216}
1217
1218/// getOrCreateScope - Returns the scope associated with the given descriptor.
1219///
Devang Patele4b27562009-08-28 23:24:31 +00001220DbgScope *DwarfDebug::getOrCreateScope(MDNode *N) {
1221 DbgScope *&Slot = DbgScopeMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +00001222 if (Slot) return Slot;
1223
1224 DbgScope *Parent = NULL;
Devang Patele4b27562009-08-28 23:24:31 +00001225 DIBlock Block(N);
Bill Wendling0310d762009-05-15 09:23:25 +00001226
Bill Wendling8fff19b2009-06-01 20:18:46 +00001227 // Don't create a new scope if we already created one for an inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001228 DenseMap<const MDNode *, DbgScope *>::iterator
1229 II = AbstractInstanceRootMap.find(N);
Bill Wendling8fff19b2009-06-01 20:18:46 +00001230 if (II != AbstractInstanceRootMap.end())
1231 return LexicalScopeStack.back();
1232
Bill Wendling0310d762009-05-15 09:23:25 +00001233 if (!Block.isNull()) {
1234 DIDescriptor ParentDesc = Block.getContext();
1235 Parent =
Devang Patele4b27562009-08-28 23:24:31 +00001236 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001237 }
1238
Devang Patele4b27562009-08-28 23:24:31 +00001239 Slot = new DbgScope(Parent, DIDescriptor(N));
Bill Wendling0310d762009-05-15 09:23:25 +00001240
1241 if (Parent)
1242 Parent->AddScope(Slot);
1243 else
1244 // First function is top level function.
1245 FunctionDbgScope = Slot;
1246
1247 return Slot;
1248}
1249
1250/// ConstructDbgScope - Construct the components of a scope.
1251///
1252void DwarfDebug::ConstructDbgScope(DbgScope *ParentScope,
1253 unsigned ParentStartID,
1254 unsigned ParentEndID,
1255 DIE *ParentDie, CompileUnit *Unit) {
1256 // Add variables to scope.
1257 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
1258 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Bill Wendling995f80a2009-05-20 23:24:48 +00001259 DIE *VariableDie = CreateDbgScopeVariable(Variables[i], Unit);
Bill Wendling0310d762009-05-15 09:23:25 +00001260 if (VariableDie) ParentDie->AddChild(VariableDie);
1261 }
1262
1263 // Add concrete instances to scope.
1264 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts =
1265 ParentScope->getConcreteInsts();
1266 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
1267 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
1268 DIE *Die = ConcreteInst->getDie();
1269
1270 unsigned StartID = ConcreteInst->getStartLabelID();
1271 unsigned EndID = ConcreteInst->getEndLabelID();
1272
1273 // Add the scope bounds.
1274 if (StartID)
1275 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1276 DWLabel("label", StartID));
1277 else
1278 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1279 DWLabel("func_begin", SubprogramCount));
1280
1281 if (EndID)
1282 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1283 DWLabel("label", EndID));
1284 else
1285 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1286 DWLabel("func_end", SubprogramCount));
1287
1288 ParentDie->AddChild(Die);
1289 }
1290
1291 // Add nested scopes.
1292 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
1293 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1294 // Define the Scope debug information entry.
1295 DbgScope *Scope = Scopes[j];
1296
1297 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1298 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1299
1300 // Ignore empty scopes.
1301 if (StartID == EndID && StartID != 0) continue;
1302
1303 // Do not ignore inlined scopes even if they don't have any variables or
1304 // scopes.
1305 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
1306 Scope->getConcreteInsts().empty())
1307 continue;
1308
1309 if (StartID == ParentStartID && EndID == ParentEndID) {
1310 // Just add stuff to the parent scope.
1311 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1312 } else {
1313 DIE *ScopeDie = new DIE(dwarf::DW_TAG_lexical_block);
1314
1315 // Add the scope bounds.
1316 if (StartID)
1317 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1318 DWLabel("label", StartID));
1319 else
1320 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1321 DWLabel("func_begin", SubprogramCount));
1322
1323 if (EndID)
1324 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1325 DWLabel("label", EndID));
1326 else
1327 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1328 DWLabel("func_end", SubprogramCount));
1329
1330 // Add the scope's contents.
1331 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1332 ParentDie->AddChild(ScopeDie);
1333 }
1334 }
1335}
1336
1337/// ConstructFunctionDbgScope - Construct the scope for the subprogram.
1338///
Bill Wendling17956162009-05-20 23:28:48 +00001339void DwarfDebug::ConstructFunctionDbgScope(DbgScope *RootScope,
1340 bool AbstractScope) {
Bill Wendling0310d762009-05-15 09:23:25 +00001341 // Exit if there is no root scope.
1342 if (!RootScope) return;
1343 DIDescriptor Desc = RootScope->getDesc();
1344 if (Desc.isNull())
1345 return;
1346
1347 // Get the subprogram debug information entry.
Devang Patele4b27562009-08-28 23:24:31 +00001348 DISubprogram SPD(Desc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001349
Bill Wendling0310d762009-05-15 09:23:25 +00001350 // Get the subprogram die.
Devang Patele4b27562009-08-28 23:24:31 +00001351 DIE *SPDie = ModuleCU->getDieMapSlotFor(SPD.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001352 assert(SPDie && "Missing subprogram descriptor");
1353
Bill Wendling17956162009-05-20 23:28:48 +00001354 if (!AbstractScope) {
1355 // Add the function bounds.
1356 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1357 DWLabel("func_begin", SubprogramCount));
1358 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1359 DWLabel("func_end", SubprogramCount));
1360 MachineLocation Location(RI->getFrameRegister(*MF));
1361 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
1362 }
Bill Wendling0310d762009-05-15 09:23:25 +00001363
Devang Patel1dbc7712009-06-29 20:45:18 +00001364 ConstructDbgScope(RootScope, 0, 0, SPDie, ModuleCU);
Bill Wendling0310d762009-05-15 09:23:25 +00001365}
1366
Bill Wendling0310d762009-05-15 09:23:25 +00001367/// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1368///
1369void DwarfDebug::ConstructDefaultDbgScope(MachineFunction *MF) {
Devang Patel1dbc7712009-06-29 20:45:18 +00001370 StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Daniel Dunbar460f6562009-07-26 09:48:23 +00001371 StringMap<DIE*>::iterator GI = Globals.find(MF->getFunction()->getName());
Devang Patel70f44262009-06-29 20:38:13 +00001372 if (GI != Globals.end()) {
1373 DIE *SPDie = GI->second;
1374
1375 // Add the function bounds.
1376 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1377 DWLabel("func_begin", SubprogramCount));
1378 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1379 DWLabel("func_end", SubprogramCount));
1380
1381 MachineLocation Location(RI->getFrameRegister(*MF));
1382 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001383 }
Bill Wendling0310d762009-05-15 09:23:25 +00001384}
1385
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001386/// GetOrCreateSourceID - Look up the source id with the given directory and
1387/// source file names. If none currently exists, create a new id and insert it
1388/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1389/// maps as well.
1390unsigned DwarfDebug::GetOrCreateSourceID(const std::string &DirName,
1391 const std::string &FileName) {
1392 unsigned DId;
1393 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1394 if (DI != DirectoryIdMap.end()) {
1395 DId = DI->getValue();
1396 } else {
1397 DId = DirectoryNames.size() + 1;
1398 DirectoryIdMap[DirName] = DId;
1399 DirectoryNames.push_back(DirName);
1400 }
1401
1402 unsigned FId;
1403 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1404 if (FI != SourceFileIdMap.end()) {
1405 FId = FI->getValue();
1406 } else {
1407 FId = SourceFileNames.size() + 1;
1408 SourceFileIdMap[FileName] = FId;
1409 SourceFileNames.push_back(FileName);
1410 }
1411
1412 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1413 SourceIdMap.find(std::make_pair(DId, FId));
1414 if (SI != SourceIdMap.end())
1415 return SI->second;
1416
1417 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1418 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1419 SourceIds.push_back(std::make_pair(DId, FId));
1420
1421 return SrcId;
1422}
1423
Devang Patele4b27562009-08-28 23:24:31 +00001424void DwarfDebug::ConstructCompileUnit(MDNode *N) {
1425 DICompileUnit DIUnit(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001426 std::string Dir, FN, Prod;
1427 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
1428 DIUnit.getFilename(FN));
1429
1430 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
1431 AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
1432 DWLabel("section_line", 0), DWLabel("section_line", 0),
1433 false);
1434 AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
1435 DIUnit.getProducer(Prod));
1436 AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
1437 DIUnit.getLanguage());
1438 AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
1439
1440 if (!Dir.empty())
1441 AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
1442 if (DIUnit.isOptimized())
1443 AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1444
1445 std::string Flags;
1446 DIUnit.getFlags(Flags);
1447 if (!Flags.empty())
1448 AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
1449
1450 unsigned RVer = DIUnit.getRunTimeVersion();
1451 if (RVer)
1452 AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1453 dwarf::DW_FORM_data1, RVer);
1454
1455 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001456 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001457 // Use first compile unit marked as isMain as the compile unit
1458 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001459 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001460 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001461
Devang Patele4b27562009-08-28 23:24:31 +00001462 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001463 CompileUnits.push_back(Unit);
1464}
1465
Devang Patele4b27562009-08-28 23:24:31 +00001466void DwarfDebug::ConstructGlobalVariableDIE(MDNode *N) {
1467 DIGlobalVariable DI_GV(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001468
1469 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001470 DIE *&Slot = ModuleCU->getDieMapSlotFor(DI_GV.getNode());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001471 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001472 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001473
Devang Patel1dbc7712009-06-29 20:45:18 +00001474 DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001475
1476 // Add address.
1477 DIEBlock *Block = new DIEBlock();
1478 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1479 std::string GLN;
1480 AddObjectLabel(Block, 0, dwarf::DW_FORM_udata,
1481 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
1482 AddBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1483
1484 // Add to map.
1485 Slot = VariableDie;
1486
1487 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001488 ModuleCU->getDie()->AddChild(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001489
1490 // Expose as global. FIXME - need to check external flag.
1491 std::string Name;
Devang Patel1dbc7712009-06-29 20:45:18 +00001492 ModuleCU->AddGlobal(DI_GV.getName(Name), VariableDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001493 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001494}
1495
Devang Patele4b27562009-08-28 23:24:31 +00001496void DwarfDebug::ConstructSubprogram(MDNode *N) {
1497 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001498
1499 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001500 DIE *&Slot = ModuleCU->getDieMapSlotFor(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001501 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001502 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001503
1504 if (!SP.isDefinition())
1505 // This is a method declaration which will be handled while constructing
1506 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001507 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001508
Devang Patel1dbc7712009-06-29 20:45:18 +00001509 DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001510
1511 // Add to map.
1512 Slot = SubprogramDie;
1513
1514 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001515 ModuleCU->getDie()->AddChild(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001516
1517 // Expose as global.
1518 std::string Name;
Devang Patel1dbc7712009-06-29 20:45:18 +00001519 ModuleCU->AddGlobal(SP.getName(Name), SubprogramDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001520 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001521}
1522
Devang Patel208622d2009-06-25 22:36:02 +00001523 /// BeginModule - Emit all Dwarf sections that should come prior to the
1524 /// content. Create global DIEs and emit initial debug info sections.
1525 /// This is inovked by the target AsmPrinter.
1526void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) {
1527 this->M = M;
1528
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001529 if (TimePassesIsEnabled)
1530 DebugTimer->startTimer();
1531
Devang Patel78ab9e22009-07-30 18:56:46 +00001532 DebugInfoFinder DbgFinder;
1533 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001534
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001535 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001536 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1537 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001538 ConstructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001539
1540 if (CompileUnits.empty()) {
1541 if (TimePassesIsEnabled)
1542 DebugTimer->stopTimer();
1543
1544 return;
1545 }
1546
Devang Patel70f44262009-06-29 20:38:13 +00001547 // If main compile unit for this module is not seen than randomly
1548 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001549 if (!ModuleCU)
1550 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001551
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001552 // If there is not any debug info available for any global variables and any
1553 // subprograms then there is not any debug info to emit.
Devang Patel78ab9e22009-07-30 18:56:46 +00001554 if (DbgFinder.global_variable_count() == 0
1555 && DbgFinder.subprogram_count() == 0) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001556 if (TimePassesIsEnabled)
1557 DebugTimer->stopTimer();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001558 return;
1559 }
Devang Patel78ab9e22009-07-30 18:56:46 +00001560
Devang Patel13e16b62009-06-26 01:49:18 +00001561 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001562 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1563 E = DbgFinder.global_variable_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001564 ConstructGlobalVariableDIE(*I);
1565
1566 // Create DIEs for each of the externally visible subprograms.
Devang Patel78ab9e22009-07-30 18:56:46 +00001567 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1568 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001569 ConstructSubprogram(*I);
1570
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001571 MMI = mmi;
1572 shouldEmit = true;
1573 MMI->setDebugInfoAvailability(true);
1574
1575 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001576 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001577
1578 // Print out .file directives to specify files for .loc directives. These are
1579 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001580 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001581 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1582 // Remember source id starts at 1.
1583 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1584 sys::Path FullPath(getSourceDirectoryName(Id.first));
1585 bool AppendOk =
1586 FullPath.appendComponent(getSourceFileName(Id.second));
1587 assert(AppendOk && "Could not append filename to directory!");
1588 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001589 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001590 Asm->EOL();
1591 }
1592 }
1593
1594 // Emit initial sections
1595 EmitInitial();
1596
1597 if (TimePassesIsEnabled)
1598 DebugTimer->stopTimer();
1599}
1600
1601/// EndModule - Emit all Dwarf sections that should come after the content.
1602///
1603void DwarfDebug::EndModule() {
1604 if (!ShouldEmitDwarfDebug())
1605 return;
1606
1607 if (TimePassesIsEnabled)
1608 DebugTimer->startTimer();
1609
1610 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001611 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001612 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001613 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001614 EmitLabel("data_end", 0);
1615
1616 // End text sections.
1617 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001618 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001619 EmitLabel("section_end", i);
1620 }
1621
1622 // Emit common frame information.
1623 EmitCommonDebugFrame();
1624
1625 // Emit function debug frame information
1626 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1627 E = DebugFrames.end(); I != E; ++I)
1628 EmitFunctionDebugFrame(*I);
1629
1630 // Compute DIE offsets and sizes.
1631 SizeAndOffsets();
1632
1633 // Emit all the DIEs into a debug info section
1634 EmitDebugInfo();
1635
1636 // Corresponding abbreviations into a abbrev section.
1637 EmitAbbreviations();
1638
1639 // Emit source line correspondence into a debug line section.
1640 EmitDebugLines();
1641
1642 // Emit info into a debug pubnames section.
1643 EmitDebugPubNames();
1644
1645 // Emit info into a debug str section.
1646 EmitDebugStr();
1647
1648 // Emit info into a debug loc section.
1649 EmitDebugLoc();
1650
1651 // Emit info into a debug aranges section.
1652 EmitDebugARanges();
1653
1654 // Emit info into a debug ranges section.
1655 EmitDebugRanges();
1656
1657 // Emit info into a debug macinfo section.
1658 EmitDebugMacInfo();
1659
1660 // Emit inline info.
1661 EmitDebugInlineInfo();
1662
1663 if (TimePassesIsEnabled)
1664 DebugTimer->stopTimer();
1665}
1666
1667/// BeginFunction - Gather pre-function debug information. Assumes being
1668/// emitted immediately after the function entry point.
1669void DwarfDebug::BeginFunction(MachineFunction *MF) {
1670 this->MF = MF;
1671
1672 if (!ShouldEmitDwarfDebug()) return;
1673
1674 if (TimePassesIsEnabled)
1675 DebugTimer->startTimer();
1676
1677 // Begin accumulating function debug information.
1678 MMI->BeginFunction(MF);
1679
1680 // Assumes in correct section after the entry point.
1681 EmitLabel("func_begin", ++SubprogramCount);
1682
1683 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1684 // function.
1685 DebugLoc FDL = MF->getDefaultDebugLoc();
1686 if (!FDL.isUnknown()) {
1687 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
1688 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
1689 DICompileUnit(DLT.CompileUnit));
1690 Asm->printLabel(LabelID);
1691 }
1692
1693 if (TimePassesIsEnabled)
1694 DebugTimer->stopTimer();
1695}
1696
1697/// EndFunction - Gather and emit post-function debug information.
1698///
1699void DwarfDebug::EndFunction(MachineFunction *MF) {
1700 if (!ShouldEmitDwarfDebug()) return;
1701
1702 if (TimePassesIsEnabled)
1703 DebugTimer->startTimer();
1704
1705 // Define end label for subprogram.
1706 EmitLabel("func_end", SubprogramCount);
1707
1708 // Get function line info.
1709 if (!Lines.empty()) {
1710 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00001711 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001712 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
1713 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
1714 // Append the function info to section info.
1715 SectionLineInfos.insert(SectionLineInfos.end(),
1716 Lines.begin(), Lines.end());
1717 }
1718
1719 // Construct the DbgScope for abstract instances.
1720 for (SmallVector<DbgScope *, 32>::iterator
1721 I = AbstractInstanceRootList.begin(),
1722 E = AbstractInstanceRootList.end(); I != E; ++I)
Bill Wendling17956162009-05-20 23:28:48 +00001723 ConstructFunctionDbgScope(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001724
1725 // Construct scopes for subprogram.
1726 if (FunctionDbgScope)
1727 ConstructFunctionDbgScope(FunctionDbgScope);
1728 else
1729 // FIXME: This is wrong. We are essentially getting past a problem with
1730 // debug information not being able to handle unreachable blocks that have
1731 // debug information in them. In particular, those unreachable blocks that
1732 // have "region end" info in them. That situation results in the "root
1733 // scope" not being created. If that's the case, then emit a "default"
1734 // scope, i.e., one that encompasses the whole function. This isn't
1735 // desirable. And a better way of handling this (and all of the debugging
1736 // information) needs to be explored.
1737 ConstructDefaultDbgScope(MF);
1738
1739 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
1740 MMI->getFrameMoves()));
1741
1742 // Clear debug info
1743 if (FunctionDbgScope) {
1744 delete FunctionDbgScope;
1745 DbgScopeMap.clear();
1746 DbgAbstractScopeMap.clear();
1747 DbgConcreteScopeMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001748 FunctionDbgScope = NULL;
1749 LexicalScopeStack.clear();
1750 AbstractInstanceRootList.clear();
Devang Patel9217f792009-06-12 19:24:05 +00001751 AbstractInstanceRootMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001752 }
1753
1754 Lines.clear();
1755
1756 if (TimePassesIsEnabled)
1757 DebugTimer->stopTimer();
1758}
1759
1760/// RecordSourceLine - Records location information and associates it with a
1761/// label. Returns a unique label ID used to generate a label and provide
1762/// correspondence to the source line list.
1763unsigned DwarfDebug::RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
1764 if (TimePassesIsEnabled)
1765 DebugTimer->startTimer();
1766
1767 CompileUnit *Unit = CompileUnitMap[V];
1768 assert(Unit && "Unable to find CompileUnit");
1769 unsigned ID = MMI->NextLabelID();
1770 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
1771
1772 if (TimePassesIsEnabled)
1773 DebugTimer->stopTimer();
1774
1775 return ID;
1776}
1777
1778/// RecordSourceLine - Records location information and associates it with a
1779/// label. Returns a unique label ID used to generate a label and provide
1780/// correspondence to the source line list.
1781unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col,
1782 DICompileUnit CU) {
Devang Patele4b27562009-08-28 23:24:31 +00001783 if (!MMI)
1784 return 0;
1785
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001786 if (TimePassesIsEnabled)
1787 DebugTimer->startTimer();
1788
1789 std::string Dir, Fn;
1790 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
1791 CU.getFilename(Fn));
1792 unsigned ID = MMI->NextLabelID();
1793 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
1794
1795 if (TimePassesIsEnabled)
1796 DebugTimer->stopTimer();
1797
1798 return ID;
1799}
1800
1801/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
1802/// timed. Look up the source id with the given directory and source file
1803/// names. If none currently exists, create a new id and insert it in the
1804/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
1805/// well.
1806unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
1807 const std::string &FileName) {
1808 if (TimePassesIsEnabled)
1809 DebugTimer->startTimer();
1810
1811 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
1812
1813 if (TimePassesIsEnabled)
1814 DebugTimer->stopTimer();
1815
1816 return SrcId;
1817}
1818
1819/// RecordRegionStart - Indicate the start of a region.
Devang Patele4b27562009-08-28 23:24:31 +00001820unsigned DwarfDebug::RecordRegionStart(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001821 if (TimePassesIsEnabled)
1822 DebugTimer->startTimer();
1823
Devang Patele4b27562009-08-28 23:24:31 +00001824 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001825 unsigned ID = MMI->NextLabelID();
1826 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1827 LexicalScopeStack.push_back(Scope);
1828
1829 if (TimePassesIsEnabled)
1830 DebugTimer->stopTimer();
1831
1832 return ID;
1833}
1834
1835/// RecordRegionEnd - Indicate the end of a region.
Devang Patele4b27562009-08-28 23:24:31 +00001836unsigned DwarfDebug::RecordRegionEnd(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001837 if (TimePassesIsEnabled)
1838 DebugTimer->startTimer();
1839
Devang Patele4b27562009-08-28 23:24:31 +00001840 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001841 unsigned ID = MMI->NextLabelID();
1842 Scope->setEndLabelID(ID);
Devang Pateldaf9e022009-06-13 02:16:18 +00001843 // FIXME : region.end() may not be in the last basic block.
1844 // For now, do not pop last lexical scope because next basic
1845 // block may start new inlined function's body.
1846 unsigned LSSize = LexicalScopeStack.size();
1847 if (LSSize != 0 && LSSize != 1)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001848 LexicalScopeStack.pop_back();
1849
1850 if (TimePassesIsEnabled)
1851 DebugTimer->stopTimer();
1852
1853 return ID;
1854}
1855
1856/// RecordVariable - Indicate the declaration of a local variable.
Devang Patele4b27562009-08-28 23:24:31 +00001857void DwarfDebug::RecordVariable(MDNode *N, unsigned FrameIndex) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001858 if (TimePassesIsEnabled)
1859 DebugTimer->startTimer();
1860
Devang Patele4b27562009-08-28 23:24:31 +00001861 DIDescriptor Desc(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001862 DbgScope *Scope = NULL;
1863 bool InlinedFnVar = false;
1864
Devang Patele4b27562009-08-28 23:24:31 +00001865 if (Desc.getTag() == dwarf::DW_TAG_variable)
1866 Scope = getOrCreateScope(DIGlobalVariable(N).getContext().getNode());
1867 else {
Devang Patel24f20e02009-08-22 17:12:53 +00001868 bool InlinedVar = false;
Devang Patele4b27562009-08-28 23:24:31 +00001869 MDNode *Context = DIVariable(N).getContext().getNode();
1870 DISubprogram SP(Context);
Devang Patel24f20e02009-08-22 17:12:53 +00001871 if (!SP.isNull()) {
1872 // SP is inserted into DbgAbstractScopeMap when inlined function
1873 // start was recorded by RecordInlineFnStart.
Devang Patele4b27562009-08-28 23:24:31 +00001874 DenseMap<MDNode *, DbgScope *>::iterator
1875 I = DbgAbstractScopeMap.find(SP.getNode());
Devang Patel24f20e02009-08-22 17:12:53 +00001876 if (I != DbgAbstractScopeMap.end()) {
1877 InlinedVar = true;
1878 Scope = I->second;
1879 }
1880 }
Devang Patele4b27562009-08-28 23:24:31 +00001881 if (!InlinedVar)
1882 Scope = getOrCreateScope(Context);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001883 }
1884
1885 assert(Scope && "Unable to find the variable's scope");
Devang Patele4b27562009-08-28 23:24:31 +00001886 DbgVariable *DV = new DbgVariable(DIVariable(N), FrameIndex, InlinedFnVar);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001887 Scope->AddVariable(DV);
1888
1889 if (TimePassesIsEnabled)
1890 DebugTimer->stopTimer();
1891}
1892
1893//// RecordInlinedFnStart - Indicate the start of inlined subroutine.
1894unsigned DwarfDebug::RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
1895 unsigned Line, unsigned Col) {
1896 unsigned LabelID = MMI->NextLabelID();
1897
Chris Lattner33adcfb2009-08-22 21:43:10 +00001898 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001899 return LabelID;
1900
1901 if (TimePassesIsEnabled)
1902 DebugTimer->startTimer();
1903
Devang Patele4b27562009-08-28 23:24:31 +00001904 MDNode *Node = SP.getNode();
1905 DenseMap<const MDNode *, DbgScope *>::iterator
1906 II = AbstractInstanceRootMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001907
1908 if (II == AbstractInstanceRootMap.end()) {
1909 // Create an abstract instance entry for this inlined function if it doesn't
1910 // already exist.
Devang Patele4b27562009-08-28 23:24:31 +00001911 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001912
1913 // Get the compile unit context.
Devang Patele4b27562009-08-28 23:24:31 +00001914 DIE *SPDie = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001915 if (!SPDie)
Devang Patel1dbc7712009-06-29 20:45:18 +00001916 SPDie = CreateSubprogramDIE(ModuleCU, SP, false, true);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001917
1918 // Mark as being inlined. This makes this subprogram entry an abstract
1919 // instance root.
1920 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
1921 // that it's defined. That probably won't change in the future. However,
1922 // this could be more elegant.
1923 AddUInt(SPDie, dwarf::DW_AT_inline, 0, dwarf::DW_INL_declared_not_inlined);
1924
1925 // Keep track of the abstract scope for this function.
Devang Patele4b27562009-08-28 23:24:31 +00001926 DbgAbstractScopeMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001927
Devang Patele4b27562009-08-28 23:24:31 +00001928 AbstractInstanceRootMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001929 AbstractInstanceRootList.push_back(Scope);
1930 }
1931
1932 // Create a concrete inlined instance for this inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001933 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001934 DIE *ScopeDie = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel1dbc7712009-06-29 20:45:18 +00001935 ScopeDie->setAbstractCompileUnit(ModuleCU);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001936
Devang Patele4b27562009-08-28 23:24:31 +00001937 DIE *Origin = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001938 AddDIEEntry(ScopeDie, dwarf::DW_AT_abstract_origin,
1939 dwarf::DW_FORM_ref4, Origin);
Devang Patel1dbc7712009-06-29 20:45:18 +00001940 AddUInt(ScopeDie, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001941 AddUInt(ScopeDie, dwarf::DW_AT_call_line, 0, Line);
1942 AddUInt(ScopeDie, dwarf::DW_AT_call_column, 0, Col);
1943
1944 ConcreteScope->setDie(ScopeDie);
1945 ConcreteScope->setStartLabelID(LabelID);
1946 MMI->RecordUsedDbgLabel(LabelID);
1947
1948 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
1949
1950 // Keep track of the concrete scope that's inlined into this function.
Devang Patele4b27562009-08-28 23:24:31 +00001951 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
1952 SI = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001953
1954 if (SI == DbgConcreteScopeMap.end())
Devang Patele4b27562009-08-28 23:24:31 +00001955 DbgConcreteScopeMap[Node].push_back(ConcreteScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001956 else
1957 SI->second.push_back(ConcreteScope);
1958
1959 // Track the start label for this inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001960 DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
1961 I = InlineInfo.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001962
1963 if (I == InlineInfo.end())
Devang Patele4b27562009-08-28 23:24:31 +00001964 InlineInfo[Node].push_back(LabelID);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001965 else
1966 I->second.push_back(LabelID);
1967
1968 if (TimePassesIsEnabled)
1969 DebugTimer->stopTimer();
1970
1971 return LabelID;
1972}
1973
1974/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
1975unsigned DwarfDebug::RecordInlinedFnEnd(DISubprogram &SP) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001976 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001977 return 0;
1978
1979 if (TimePassesIsEnabled)
1980 DebugTimer->startTimer();
1981
Devang Patele4b27562009-08-28 23:24:31 +00001982 MDNode *Node = SP.getNode();
1983 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
1984 I = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001985
1986 if (I == DbgConcreteScopeMap.end()) {
1987 // FIXME: Can this situation actually happen? And if so, should it?
1988 if (TimePassesIsEnabled)
1989 DebugTimer->stopTimer();
1990
1991 return 0;
1992 }
1993
1994 SmallVector<DbgScope *, 8> &Scopes = I->second;
Devang Patel11a407f2009-06-15 21:45:50 +00001995 if (Scopes.empty()) {
1996 // Returned ID is 0 if this is unbalanced "end of inlined
1997 // scope". This could happen if optimizer eats dbg intrinsics
1998 // or "beginning of inlined scope" is not recoginized due to
1999 // missing location info. In such cases, ignore this region.end.
2000 return 0;
2001 }
2002
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002003 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
2004 unsigned ID = MMI->NextLabelID();
2005 MMI->RecordUsedDbgLabel(ID);
2006 Scope->setEndLabelID(ID);
2007
2008 if (TimePassesIsEnabled)
2009 DebugTimer->stopTimer();
2010
2011 return ID;
2012}
2013
Bill Wendling829e67b2009-05-20 23:22:40 +00002014//===----------------------------------------------------------------------===//
2015// Emit Methods
2016//===----------------------------------------------------------------------===//
2017
Bill Wendling94d04b82009-05-20 23:21:38 +00002018/// SizeAndOffsetDie - Compute the size and offset of a DIE.
2019///
2020unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2021 // Get the children.
2022 const std::vector<DIE *> &Children = Die->getChildren();
2023
2024 // If not last sibling and has children then add sibling offset attribute.
2025 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2026
2027 // Record the abbreviation.
2028 AssignAbbrevNumber(Die->getAbbrev());
2029
2030 // Get the abbreviation for this DIE.
2031 unsigned AbbrevNumber = Die->getAbbrevNumber();
2032 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2033
2034 // Set DIE offset
2035 Die->setOffset(Offset);
2036
2037 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002038 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002039
2040 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2041 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2042
2043 // Size the DIE attribute values.
2044 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2045 // Size attribute value.
2046 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2047
2048 // Size the DIE children if any.
2049 if (!Children.empty()) {
2050 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2051 "Children flag not set");
2052
2053 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2054 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2055
2056 // End of children marker.
2057 Offset += sizeof(int8_t);
2058 }
2059
2060 Die->setSize(Offset - Die->getOffset());
2061 return Offset;
2062}
2063
2064/// SizeAndOffsets - Compute the size and offset of all the DIEs.
2065///
2066void DwarfDebug::SizeAndOffsets() {
2067 // Compute size of compile unit header.
2068 static unsigned Offset =
2069 sizeof(int32_t) + // Length of Compilation Unit Info
2070 sizeof(int16_t) + // DWARF version number
2071 sizeof(int32_t) + // Offset Into Abbrev. Section
2072 sizeof(int8_t); // Pointer Size (in bytes)
2073
Devang Patel1dbc7712009-06-29 20:45:18 +00002074 SizeAndOffsetDie(ModuleCU->getDie(), Offset, true);
2075 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002076}
2077
2078/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2079/// tools to recognize the object file contains Dwarf information.
2080void DwarfDebug::EmitInitial() {
2081 // Check to see if we already emitted intial headers.
2082 if (didInitial) return;
2083 didInitial = true;
2084
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002085 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
2086
Bill Wendling94d04b82009-05-20 23:21:38 +00002087 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002088 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002089 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002090 EmitLabel("section_debug_frame", 0);
2091 }
2092
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002093 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002094 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002095 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002096 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002097 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002098 EmitLabel("section_aranges", 0);
2099
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002100 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2101 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002102 EmitLabel("section_macinfo", 0);
2103 }
2104
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002105 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002106 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002107 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002108 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002109 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002110 EmitLabel("section_pubnames", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002111 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002112 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002113 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002114 EmitLabel("section_ranges", 0);
2115
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002116 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002117 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002118 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002119 EmitLabel("data_begin", 0);
2120}
2121
2122/// EmitDIE - Recusively Emits a debug information entry.
2123///
2124void DwarfDebug::EmitDIE(DIE *Die) {
2125 // Get the abbreviation for this DIE.
2126 unsigned AbbrevNumber = Die->getAbbrevNumber();
2127 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2128
2129 Asm->EOL();
2130
2131 // Emit the code (index) for the abbreviation.
2132 Asm->EmitULEB128Bytes(AbbrevNumber);
2133
2134 if (Asm->isVerbose())
2135 Asm->EOL(std::string("Abbrev [" +
2136 utostr(AbbrevNumber) +
2137 "] 0x" + utohexstr(Die->getOffset()) +
2138 ":0x" + utohexstr(Die->getSize()) + " " +
2139 dwarf::TagString(Abbrev->getTag())));
2140 else
2141 Asm->EOL();
2142
2143 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2144 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2145
2146 // Emit the DIE attribute values.
2147 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2148 unsigned Attr = AbbrevData[i].getAttribute();
2149 unsigned Form = AbbrevData[i].getForm();
2150 assert(Form && "Too many attributes for DIE (check abbreviation)");
2151
2152 switch (Attr) {
2153 case dwarf::DW_AT_sibling:
2154 Asm->EmitInt32(Die->SiblingOffset());
2155 break;
2156 case dwarf::DW_AT_abstract_origin: {
2157 DIEEntry *E = cast<DIEEntry>(Values[i]);
2158 DIE *Origin = E->getEntry();
2159 unsigned Addr =
2160 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
2161 Origin->getOffset();
2162
2163 Asm->EmitInt32(Addr);
2164 break;
2165 }
2166 default:
2167 // Emit an attribute using the defined form.
2168 Values[i]->EmitValue(this, Form);
2169 break;
2170 }
2171
2172 Asm->EOL(dwarf::AttributeString(Attr));
2173 }
2174
2175 // Emit the DIE children if any.
2176 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2177 const std::vector<DIE *> &Children = Die->getChildren();
2178
2179 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2180 EmitDIE(Children[j]);
2181
2182 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2183 }
2184}
2185
2186/// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
2187///
2188void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) {
2189 DIE *Die = Unit->getDie();
2190
2191 // Emit the compile units header.
2192 EmitLabel("info_begin", Unit->getID());
2193
2194 // Emit size of content not including length itself
2195 unsigned ContentSize = Die->getSize() +
2196 sizeof(int16_t) + // DWARF version number
2197 sizeof(int32_t) + // Offset Into Abbrev. Section
2198 sizeof(int8_t) + // Pointer Size (in bytes)
2199 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2200
2201 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2202 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2203 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2204 Asm->EOL("Offset Into Abbrev. Section");
2205 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2206
2207 EmitDIE(Die);
2208 // FIXME - extra padding for gdb bug.
2209 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2210 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2211 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2212 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2213 EmitLabel("info_end", Unit->getID());
2214
2215 Asm->EOL();
2216}
2217
2218void DwarfDebug::EmitDebugInfo() {
2219 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002220 Asm->OutStreamer.SwitchSection(
2221 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002222
Devang Patel1dbc7712009-06-29 20:45:18 +00002223 EmitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002224}
2225
2226/// EmitAbbreviations - Emit the abbreviation section.
2227///
2228void DwarfDebug::EmitAbbreviations() const {
2229 // Check to see if it is worth the effort.
2230 if (!Abbreviations.empty()) {
2231 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002232 Asm->OutStreamer.SwitchSection(
2233 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002234
2235 EmitLabel("abbrev_begin", 0);
2236
2237 // For each abbrevation.
2238 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2239 // Get abbreviation data
2240 const DIEAbbrev *Abbrev = Abbreviations[i];
2241
2242 // Emit the abbrevations code (base 1 index.)
2243 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2244 Asm->EOL("Abbreviation Code");
2245
2246 // Emit the abbreviations data.
2247 Abbrev->Emit(Asm);
2248
2249 Asm->EOL();
2250 }
2251
2252 // Mark end of abbreviations.
2253 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2254
2255 EmitLabel("abbrev_end", 0);
2256 Asm->EOL();
2257 }
2258}
2259
2260/// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2261/// the line matrix.
2262///
2263void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) {
2264 // Define last address of section.
2265 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2266 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2267 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2268 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2269
2270 // Mark end of matrix.
2271 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2272 Asm->EmitULEB128Bytes(1); Asm->EOL();
2273 Asm->EmitInt8(1); Asm->EOL();
2274}
2275
2276/// EmitDebugLines - Emit source line information.
2277///
2278void DwarfDebug::EmitDebugLines() {
2279 // If the target is using .loc/.file, the assembler will be emitting the
2280 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002281 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002282 return;
2283
2284 // Minimum line delta, thus ranging from -10..(255-10).
2285 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2286 // Maximum line delta, thus ranging from -10..(255-10).
2287 const int MaxLineDelta = 255 + MinLineDelta;
2288
2289 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002290 Asm->OutStreamer.SwitchSection(
2291 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002292
2293 // Construct the section header.
2294 EmitDifference("line_end", 0, "line_begin", 0, true);
2295 Asm->EOL("Length of Source Line Info");
2296 EmitLabel("line_begin", 0);
2297
2298 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2299
2300 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2301 Asm->EOL("Prolog Length");
2302 EmitLabel("line_prolog_begin", 0);
2303
2304 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2305
2306 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2307
2308 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2309
2310 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2311
2312 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2313
2314 // Line number standard opcode encodings argument count
2315 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2316 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2317 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2318 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2319 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2320 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2321 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2322 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2323 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2324
2325 // Emit directories.
2326 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2327 Asm->EmitString(getSourceDirectoryName(DI));
2328 Asm->EOL("Directory");
2329 }
2330
2331 Asm->EmitInt8(0); Asm->EOL("End of directories");
2332
2333 // Emit files.
2334 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2335 // Remember source id starts at 1.
2336 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2337 Asm->EmitString(getSourceFileName(Id.second));
2338 Asm->EOL("Source");
2339 Asm->EmitULEB128Bytes(Id.first);
2340 Asm->EOL("Directory #");
2341 Asm->EmitULEB128Bytes(0);
2342 Asm->EOL("Mod date");
2343 Asm->EmitULEB128Bytes(0);
2344 Asm->EOL("File size");
2345 }
2346
2347 Asm->EmitInt8(0); Asm->EOL("End of files");
2348
2349 EmitLabel("line_prolog_end", 0);
2350
2351 // A sequence for each text section.
2352 unsigned SecSrcLinesSize = SectionSourceLines.size();
2353
2354 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2355 // Isolate current sections line info.
2356 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2357
Chris Lattner93b6db32009-08-08 23:39:42 +00002358 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002359 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002360 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002361 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002362 }*/
2363 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002364
2365 // Dwarf assumes we start with first line of first source file.
2366 unsigned Source = 1;
2367 unsigned Line = 1;
2368
2369 // Construct rows of the address, source, line, column matrix.
2370 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2371 const SrcLineInfo &LineInfo = LineInfos[i];
2372 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2373 if (!LabelID) continue;
2374
2375 if (!Asm->isVerbose())
2376 Asm->EOL();
2377 else {
2378 std::pair<unsigned, unsigned> SourceID =
2379 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002380 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002381 << getSourceDirectoryName(SourceID.first) << ' '
2382 << getSourceFileName(SourceID.second)
2383 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2384 }
2385
2386 // Define the line address.
2387 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2388 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2389 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2390 EmitReference("label", LabelID); Asm->EOL("Location label");
2391
2392 // If change of source, then switch to the new source.
2393 if (Source != LineInfo.getSourceID()) {
2394 Source = LineInfo.getSourceID();
2395 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2396 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2397 }
2398
2399 // If change of line.
2400 if (Line != LineInfo.getLine()) {
2401 // Determine offset.
2402 int Offset = LineInfo.getLine() - Line;
2403 int Delta = Offset - MinLineDelta;
2404
2405 // Update line.
2406 Line = LineInfo.getLine();
2407
2408 // If delta is small enough and in range...
2409 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2410 // ... then use fast opcode.
2411 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2412 } else {
2413 // ... otherwise use long hand.
2414 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2415 Asm->EOL("DW_LNS_advance_line");
2416 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2417 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2418 }
2419 } else {
2420 // Copy the previous row (different address or source)
2421 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2422 }
2423 }
2424
2425 EmitEndOfLineMatrix(j + 1);
2426 }
2427
2428 if (SecSrcLinesSize == 0)
2429 // Because we're emitting a debug_line section, we still need a line
2430 // table. The linker and friends expect it to exist. If there's nothing to
2431 // put into it, emit an empty table.
2432 EmitEndOfLineMatrix(1);
2433
2434 EmitLabel("line_end", 0);
2435 Asm->EOL();
2436}
2437
2438/// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2439///
2440void DwarfDebug::EmitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002441 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002442 return;
2443
2444 int stackGrowth =
2445 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2446 TargetFrameInfo::StackGrowsUp ?
2447 TD->getPointerSize() : -TD->getPointerSize();
2448
2449 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002450 Asm->OutStreamer.SwitchSection(
2451 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002452
2453 EmitLabel("debug_frame_common", 0);
2454 EmitDifference("debug_frame_common_end", 0,
2455 "debug_frame_common_begin", 0, true);
2456 Asm->EOL("Length of Common Information Entry");
2457
2458 EmitLabel("debug_frame_common_begin", 0);
2459 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2460 Asm->EOL("CIE Identifier Tag");
2461 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2462 Asm->EOL("CIE Version");
2463 Asm->EmitString("");
2464 Asm->EOL("CIE Augmentation");
2465 Asm->EmitULEB128Bytes(1);
2466 Asm->EOL("CIE Code Alignment Factor");
2467 Asm->EmitSLEB128Bytes(stackGrowth);
2468 Asm->EOL("CIE Data Alignment Factor");
2469 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2470 Asm->EOL("CIE RA Column");
2471
2472 std::vector<MachineMove> Moves;
2473 RI->getInitialFrameState(Moves);
2474
2475 EmitFrameMoves(NULL, 0, Moves, false);
2476
2477 Asm->EmitAlignment(2, 0, 0, false);
2478 EmitLabel("debug_frame_common_end", 0);
2479
2480 Asm->EOL();
2481}
2482
2483/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2484/// section.
2485void
2486DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002487 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002488 return;
2489
2490 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002491 Asm->OutStreamer.SwitchSection(
2492 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002493
2494 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2495 "debug_frame_begin", DebugFrameInfo.Number, true);
2496 Asm->EOL("Length of Frame Information Entry");
2497
2498 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2499
2500 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2501 0, 0, true, false);
2502 Asm->EOL("FDE CIE offset");
2503
2504 EmitReference("func_begin", DebugFrameInfo.Number);
2505 Asm->EOL("FDE initial location");
2506 EmitDifference("func_end", DebugFrameInfo.Number,
2507 "func_begin", DebugFrameInfo.Number);
2508 Asm->EOL("FDE address range");
2509
2510 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2511 false);
2512
2513 Asm->EmitAlignment(2, 0, 0, false);
2514 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2515
2516 Asm->EOL();
2517}
2518
2519void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2520 EmitDifference("pubnames_end", Unit->getID(),
2521 "pubnames_begin", Unit->getID(), true);
2522 Asm->EOL("Length of Public Names Info");
2523
2524 EmitLabel("pubnames_begin", Unit->getID());
2525
2526 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2527
2528 EmitSectionOffset("info_begin", "section_info",
2529 Unit->getID(), 0, true, false);
2530 Asm->EOL("Offset of Compilation Unit Info");
2531
2532 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2533 true);
2534 Asm->EOL("Compilation Unit Length");
2535
2536 StringMap<DIE*> &Globals = Unit->getGlobals();
2537 for (StringMap<DIE*>::const_iterator
2538 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2539 const char *Name = GI->getKeyData();
2540 DIE * Entity = GI->second;
2541
2542 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2543 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2544 }
2545
2546 Asm->EmitInt32(0); Asm->EOL("End Mark");
2547 EmitLabel("pubnames_end", Unit->getID());
2548
2549 Asm->EOL();
2550}
2551
2552/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2553///
2554void DwarfDebug::EmitDebugPubNames() {
2555 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002556 Asm->OutStreamer.SwitchSection(
2557 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002558
Devang Patel1dbc7712009-06-29 20:45:18 +00002559 EmitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002560}
2561
2562/// EmitDebugStr - Emit visible names into a debug str section.
2563///
2564void DwarfDebug::EmitDebugStr() {
2565 // Check to see if it is worth the effort.
2566 if (!StringPool.empty()) {
2567 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002568 Asm->OutStreamer.SwitchSection(
2569 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002570
2571 // For each of strings in the string pool.
2572 for (unsigned StringID = 1, N = StringPool.size();
2573 StringID <= N; ++StringID) {
2574 // Emit a label for reference from debug information entries.
2575 EmitLabel("string", StringID);
2576
2577 // Emit the string itself.
2578 const std::string &String = StringPool[StringID];
2579 Asm->EmitString(String); Asm->EOL();
2580 }
2581
2582 Asm->EOL();
2583 }
2584}
2585
2586/// EmitDebugLoc - Emit visible names into a debug loc section.
2587///
2588void DwarfDebug::EmitDebugLoc() {
2589 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002590 Asm->OutStreamer.SwitchSection(
2591 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002592 Asm->EOL();
2593}
2594
2595/// EmitDebugARanges - Emit visible names into a debug aranges section.
2596///
2597void DwarfDebug::EmitDebugARanges() {
2598 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002599 Asm->OutStreamer.SwitchSection(
2600 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002601
2602 // FIXME - Mock up
2603#if 0
2604 CompileUnit *Unit = GetBaseCompileUnit();
2605
2606 // Don't include size of length
2607 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2608
2609 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2610
2611 EmitReference("info_begin", Unit->getID());
2612 Asm->EOL("Offset of Compilation Unit Info");
2613
2614 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2615
2616 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2617
2618 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2619 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2620
2621 // Range 1
2622 EmitReference("text_begin", 0); Asm->EOL("Address");
2623 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2624
2625 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2626 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2627#endif
2628
2629 Asm->EOL();
2630}
2631
2632/// EmitDebugRanges - Emit visible names into a debug ranges section.
2633///
2634void DwarfDebug::EmitDebugRanges() {
2635 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002636 Asm->OutStreamer.SwitchSection(
2637 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002638 Asm->EOL();
2639}
2640
2641/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2642///
2643void DwarfDebug::EmitDebugMacInfo() {
Chris Lattner18a4c162009-08-02 07:24:22 +00002644 if (const MCSection *LineInfo =
2645 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002646 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002647 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002648 Asm->EOL();
2649 }
2650}
2651
2652/// EmitDebugInlineInfo - Emit inline info using following format.
2653/// Section Header:
2654/// 1. length of section
2655/// 2. Dwarf version number
2656/// 3. address size.
2657///
2658/// Entries (one "entry" for each function that was inlined):
2659///
2660/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2661/// otherwise offset into __debug_str for regular function name.
2662/// 2. offset into __debug_str section for regular function name.
2663/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2664/// instances for the function.
2665///
2666/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2667/// inlined instance; the die_offset points to the inlined_subroutine die in the
2668/// __debug_info section, and the low_pc is the starting address for the
2669/// inlining instance.
2670void DwarfDebug::EmitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002671 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002672 return;
2673
Devang Patel1dbc7712009-06-29 20:45:18 +00002674 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002675 return;
2676
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002677 Asm->OutStreamer.SwitchSection(
2678 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002679 Asm->EOL();
2680 EmitDifference("debug_inlined_end", 1,
2681 "debug_inlined_begin", 1, true);
2682 Asm->EOL("Length of Debug Inlined Information Entry");
2683
2684 EmitLabel("debug_inlined_begin", 1);
2685
2686 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2687 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2688
Devang Patele4b27562009-08-28 23:24:31 +00002689 for (DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
Bill Wendling94d04b82009-05-20 23:21:38 +00002690 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
Devang Patele4b27562009-08-28 23:24:31 +00002691 MDNode *Node = I->first;
Bill Wendling94d04b82009-05-20 23:21:38 +00002692 SmallVector<unsigned, 4> &Labels = I->second;
Devang Patele4b27562009-08-28 23:24:31 +00002693 DISubprogram SP(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00002694 std::string Name;
2695 std::string LName;
2696
2697 SP.getLinkageName(LName);
2698 SP.getName(Name);
2699
Devang Patel53cb17d2009-07-16 01:01:22 +00002700 if (LName.empty())
2701 Asm->EmitString(Name);
2702 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002703 // Skip special LLVM prefix that is used to inform the asm printer to not
2704 // emit usual symbol prefix before the symbol name. This happens for
2705 // Objective-C symbol names and symbol whose name is replaced using GCC's
2706 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002707 if (LName[0] == 1)
2708 LName = &LName[1];
2709 Asm->EmitString(LName);
2710 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002711 Asm->EOL("MIPS linkage name");
2712
2713 Asm->EmitString(Name); Asm->EOL("Function name");
2714
2715 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2716
2717 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2718 LE = Labels.end(); LI != LE; ++LI) {
Devang Patele4b27562009-08-28 23:24:31 +00002719 DIE *SP = ModuleCU->getDieMapSlotFor(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00002720 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2721
2722 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002723 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002724 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002725 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002726
2727 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2728 }
2729 }
2730
2731 EmitLabel("debug_inlined_end", 1);
2732 Asm->EOL();
2733}