blob: 98423dd4c37e0c38bf7c167728d082eb035efaf1 [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Devang Patele4b27562009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner334fd1f2009-09-16 00:08:41 +000026#include "llvm/Support/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000027#include "llvm/Support/Timer.h"
Devang Patele4b27562009-08-28 23:24:31 +000028#include "llvm/Support/Debug.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000030using namespace llvm;
31
32static TimerGroup &getDwarfTimerGroup() {
33 static TimerGroup DwarfTimerGroup("Dwarf Debugging");
34 return DwarfTimerGroup;
35}
36
37//===----------------------------------------------------------------------===//
38
39/// Configuration values for initial hash set sizes (log2).
40///
41static const unsigned InitDiesSetSize = 9; // log2(512)
42static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
43static const unsigned InitValuesSetSize = 9; // log2(512)
44
45namespace llvm {
46
47//===----------------------------------------------------------------------===//
48/// CompileUnit - This dwarf writer support class manages information associate
49/// with a source file.
50class VISIBILITY_HIDDEN CompileUnit {
51 /// ID - File identifier for source.
52 ///
53 unsigned ID;
54
55 /// Die - Compile unit debug information entry.
56 ///
57 DIE *Die;
58
59 /// GVToDieMap - Tracks the mapping of unit level debug informaton
60 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000061 /// FIXME : Rename GVToDieMap -> NodeToDieMap
62 std::map<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000063
64 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
65 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000066 /// FIXME : Rename
67 std::map<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000068
69 /// Globals - A map of globally visible named entities for this unit.
70 ///
71 StringMap<DIE*> Globals;
72
73 /// DiesSet - Used to uniquely define dies within the compile unit.
74 ///
75 FoldingSet<DIE> DiesSet;
76public:
77 CompileUnit(unsigned I, DIE *D)
Bill Wendling39dd6962009-05-20 23:31:45 +000078 : ID(I), Die(D), DiesSet(InitDiesSetSize) {}
79 ~CompileUnit() { delete Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000080
81 // Accessors.
Bill Wendling39dd6962009-05-20 23:31:45 +000082 unsigned getID() const { return ID; }
83 DIE* getDie() const { return Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000084 StringMap<DIE*> &getGlobals() { return Globals; }
85
86 /// hasContent - Return true if this compile unit has something to write out.
87 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000088 bool hasContent() const { return !Die->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +000089
90 /// AddGlobal - Add a new global entity to the compile unit.
91 ///
Bill Wendling39dd6962009-05-20 23:31:45 +000092 void AddGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000093
94 /// getDieMapSlotFor - Returns the debug information entry map slot for the
95 /// specified debug variable.
Devang Patele4b27562009-08-28 23:24:31 +000096 DIE *&getDieMapSlotFor(MDNode *N) { return GVToDieMap[N]; }
Bill Wendling0310d762009-05-15 09:23:25 +000097
Chris Lattner1cda87c2009-07-14 04:50:12 +000098 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for
99 /// the specified debug variable.
Devang Patele4b27562009-08-28 23:24:31 +0000100 DIEEntry *&getDIEEntrySlotFor(MDNode *N) {
101 return GVToDIEEntryMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +0000102 }
103
104 /// AddDie - Adds or interns the DIE to the compile unit.
105 ///
106 DIE *AddDie(DIE &Buffer) {
107 FoldingSetNodeID ID;
108 Buffer.Profile(ID);
109 void *Where;
110 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
111
112 if (!Die) {
113 Die = new DIE(Buffer);
114 DiesSet.InsertNode(Die, Where);
115 this->Die->AddChild(Die);
116 Buffer.Detach();
117 }
118
119 return Die;
120 }
121};
122
123//===----------------------------------------------------------------------===//
124/// DbgVariable - This class is used to track local variable information.
125///
126class VISIBILITY_HIDDEN DbgVariable {
127 DIVariable Var; // Variable Descriptor.
128 unsigned FrameIndex; // Variable frame index.
Bill Wendling1180c782009-05-18 23:08:55 +0000129 bool InlinedFnVar; // Variable for an inlined function.
Bill Wendling0310d762009-05-15 09:23:25 +0000130public:
Bill Wendling1180c782009-05-18 23:08:55 +0000131 DbgVariable(DIVariable V, unsigned I, bool IFV)
132 : Var(V), FrameIndex(I), InlinedFnVar(IFV) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000133
134 // Accessors.
Bill Wendling1180c782009-05-18 23:08:55 +0000135 DIVariable getVariable() const { return Var; }
Bill Wendling0310d762009-05-15 09:23:25 +0000136 unsigned getFrameIndex() const { return FrameIndex; }
Bill Wendling1180c782009-05-18 23:08:55 +0000137 bool isInlinedFnVar() const { return InlinedFnVar; }
Bill Wendling0310d762009-05-15 09:23:25 +0000138};
139
140//===----------------------------------------------------------------------===//
141/// DbgScope - This class is used to track scope information.
142///
143class DbgConcreteScope;
144class VISIBILITY_HIDDEN DbgScope {
145 DbgScope *Parent; // Parent to this scope.
146 DIDescriptor Desc; // Debug info descriptor for scope.
147 // Either subprogram or block.
148 unsigned StartLabelID; // Label ID of the beginning of scope.
149 unsigned EndLabelID; // Label ID of the end of scope.
150 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
151 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
152 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000153
Owen Anderson04c05f72009-06-24 22:53:20 +0000154 // Private state for dump()
155 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000156public:
157 DbgScope(DbgScope *P, DIDescriptor D)
Owen Anderson04c05f72009-06-24 22:53:20 +0000158 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000159 virtual ~DbgScope();
160
161 // Accessors.
162 DbgScope *getParent() const { return Parent; }
163 DIDescriptor getDesc() const { return Desc; }
164 unsigned getStartLabelID() const { return StartLabelID; }
165 unsigned getEndLabelID() const { return EndLabelID; }
166 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
167 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
168 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; }
169 void setStartLabelID(unsigned S) { StartLabelID = S; }
170 void setEndLabelID(unsigned E) { EndLabelID = E; }
171
172 /// AddScope - Add a scope to the scope.
173 ///
174 void AddScope(DbgScope *S) { Scopes.push_back(S); }
175
176 /// AddVariable - Add a variable to the scope.
177 ///
178 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
179
180 /// AddConcreteInst - Add a concrete instance to the scope.
181 ///
182 void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); }
183
184#ifndef NDEBUG
185 void dump() const;
186#endif
187};
188
189#ifndef NDEBUG
190void DbgScope::dump() const {
Chris Lattnerc281de12009-08-23 00:51:00 +0000191 raw_ostream &err = errs();
192 err.indent(IndentLevel);
193 Desc.dump();
194 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000195
196 IndentLevel += 2;
197
198 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
199 if (Scopes[i] != this)
200 Scopes[i]->dump();
201
202 IndentLevel -= 2;
203}
204#endif
205
206//===----------------------------------------------------------------------===//
207/// DbgConcreteScope - This class is used to track a scope that holds concrete
208/// instance information.
209///
210class VISIBILITY_HIDDEN DbgConcreteScope : public DbgScope {
211 CompileUnit *Unit;
212 DIE *Die; // Debug info for this concrete scope.
213public:
214 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
215
216 // Accessors.
217 DIE *getDie() const { return Die; }
218 void setDie(DIE *D) { Die = D; }
219};
220
221DbgScope::~DbgScope() {
222 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
223 delete Scopes[i];
224 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
225 delete Variables[j];
226 for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k)
227 delete ConcreteInsts[k];
228}
229
230} // end llvm namespace
231
Chris Lattneraf76e592009-08-22 20:48:53 +0000232DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000233 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000234 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000235 ValuesSet(InitValuesSetSize), Values(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000236 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel43da8fb2009-07-13 21:26:33 +0000237 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000238 if (TimePassesIsEnabled)
239 DebugTimer = new Timer("Dwarf Debug Writer",
240 getDwarfTimerGroup());
241}
242DwarfDebug::~DwarfDebug() {
243 for (unsigned j = 0, M = Values.size(); j < M; ++j)
244 delete Values[j];
245
Devang Patele4b27562009-08-28 23:24:31 +0000246 for (DenseMap<const MDNode *, DbgScope *>::iterator
Bill Wendling0310d762009-05-15 09:23:25 +0000247 I = AbstractInstanceRootMap.begin(),
248 E = AbstractInstanceRootMap.end(); I != E;++I)
249 delete I->second;
250
251 delete DebugTimer;
252}
253
254/// AssignAbbrevNumber - Define a unique number for the abbreviation.
255///
256void DwarfDebug::AssignAbbrevNumber(DIEAbbrev &Abbrev) {
257 // Profile the node so that we can make it unique.
258 FoldingSetNodeID ID;
259 Abbrev.Profile(ID);
260
261 // Check the set for priors.
262 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
263
264 // If it's newly added.
265 if (InSet == &Abbrev) {
266 // Add to abbreviation list.
267 Abbreviations.push_back(&Abbrev);
268
269 // Assign the vector position + 1 as its number.
270 Abbrev.setNumber(Abbreviations.size());
271 } else {
272 // Assign existing abbreviation number.
273 Abbrev.setNumber(InSet->getNumber());
274 }
275}
276
Bill Wendling995f80a2009-05-20 23:24:48 +0000277/// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
278/// information entry.
279DIEEntry *DwarfDebug::CreateDIEEntry(DIE *Entry) {
Bill Wendling0310d762009-05-15 09:23:25 +0000280 DIEEntry *Value;
281
282 if (Entry) {
283 FoldingSetNodeID ID;
284 DIEEntry::Profile(ID, Entry);
285 void *Where;
286 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
287
288 if (Value) return Value;
289
290 Value = new DIEEntry(Entry);
291 ValuesSet.InsertNode(Value, Where);
292 } else {
293 Value = new DIEEntry(Entry);
294 }
295
296 Values.push_back(Value);
297 return Value;
298}
299
300/// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
301///
302void DwarfDebug::SetDIEEntry(DIEEntry *Value, DIE *Entry) {
303 Value->setEntry(Entry);
304
305 // Add to values set if not already there. If it is, we merely have a
306 // duplicate in the values list (no harm.)
307 ValuesSet.GetOrInsertNode(Value);
308}
309
310/// AddUInt - Add an unsigned integer attribute data and value.
311///
312void DwarfDebug::AddUInt(DIE *Die, unsigned Attribute,
313 unsigned Form, uint64_t Integer) {
314 if (!Form) Form = DIEInteger::BestForm(false, Integer);
315
316 FoldingSetNodeID ID;
317 DIEInteger::Profile(ID, Integer);
318 void *Where;
319 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
320
321 if (!Value) {
322 Value = new DIEInteger(Integer);
323 ValuesSet.InsertNode(Value, Where);
324 Values.push_back(Value);
325 }
326
327 Die->AddValue(Attribute, Form, Value);
328}
329
330/// AddSInt - Add an signed integer attribute data and value.
331///
332void DwarfDebug::AddSInt(DIE *Die, unsigned Attribute,
333 unsigned Form, int64_t Integer) {
334 if (!Form) Form = DIEInteger::BestForm(true, Integer);
335
336 FoldingSetNodeID ID;
337 DIEInteger::Profile(ID, (uint64_t)Integer);
338 void *Where;
339 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
340
341 if (!Value) {
342 Value = new DIEInteger(Integer);
343 ValuesSet.InsertNode(Value, Where);
344 Values.push_back(Value);
345 }
346
347 Die->AddValue(Attribute, Form, Value);
348}
349
350/// AddString - Add a string attribute data and value.
351///
352void DwarfDebug::AddString(DIE *Die, unsigned Attribute, unsigned Form,
353 const std::string &String) {
354 FoldingSetNodeID ID;
355 DIEString::Profile(ID, String);
356 void *Where;
357 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
358
359 if (!Value) {
360 Value = new DIEString(String);
361 ValuesSet.InsertNode(Value, Where);
362 Values.push_back(Value);
363 }
364
365 Die->AddValue(Attribute, Form, Value);
366}
367
368/// AddLabel - Add a Dwarf label attribute data and value.
369///
370void DwarfDebug::AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
371 const DWLabel &Label) {
372 FoldingSetNodeID ID;
373 DIEDwarfLabel::Profile(ID, Label);
374 void *Where;
375 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
376
377 if (!Value) {
378 Value = new DIEDwarfLabel(Label);
379 ValuesSet.InsertNode(Value, Where);
380 Values.push_back(Value);
381 }
382
383 Die->AddValue(Attribute, Form, Value);
384}
385
386/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
387///
388void DwarfDebug::AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
389 const std::string &Label) {
390 FoldingSetNodeID ID;
391 DIEObjectLabel::Profile(ID, Label);
392 void *Where;
393 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
394
395 if (!Value) {
396 Value = new DIEObjectLabel(Label);
397 ValuesSet.InsertNode(Value, Where);
398 Values.push_back(Value);
399 }
400
401 Die->AddValue(Attribute, Form, Value);
402}
403
404/// AddSectionOffset - Add a section offset label attribute data and value.
405///
406void DwarfDebug::AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
407 const DWLabel &Label, const DWLabel &Section,
408 bool isEH, bool useSet) {
409 FoldingSetNodeID ID;
410 DIESectionOffset::Profile(ID, Label, Section);
411 void *Where;
412 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
413
414 if (!Value) {
415 Value = new DIESectionOffset(Label, Section, isEH, useSet);
416 ValuesSet.InsertNode(Value, Where);
417 Values.push_back(Value);
418 }
419
420 Die->AddValue(Attribute, Form, Value);
421}
422
423/// AddDelta - Add a label delta attribute data and value.
424///
425void DwarfDebug::AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
426 const DWLabel &Hi, const DWLabel &Lo) {
427 FoldingSetNodeID ID;
428 DIEDelta::Profile(ID, Hi, Lo);
429 void *Where;
430 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
431
432 if (!Value) {
433 Value = new DIEDelta(Hi, Lo);
434 ValuesSet.InsertNode(Value, Where);
435 Values.push_back(Value);
436 }
437
438 Die->AddValue(Attribute, Form, Value);
439}
440
441/// AddBlock - Add block data.
442///
443void DwarfDebug::AddBlock(DIE *Die, unsigned Attribute, unsigned Form,
444 DIEBlock *Block) {
445 Block->ComputeSize(TD);
446 FoldingSetNodeID ID;
447 Block->Profile(ID);
448 void *Where;
449 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
450
451 if (!Value) {
452 Value = Block;
453 ValuesSet.InsertNode(Value, Where);
454 Values.push_back(Value);
455 } else {
456 // Already exists, reuse the previous one.
457 delete Block;
458 Block = cast<DIEBlock>(Value);
459 }
460
461 Die->AddValue(Attribute, Block->BestForm(), Value);
462}
463
464/// AddSourceLine - Add location information to specified debug information
465/// entry.
466void DwarfDebug::AddSourceLine(DIE *Die, const DIVariable *V) {
467 // If there is no compile unit specified, don't add a line #.
468 if (V->getCompileUnit().isNull())
469 return;
470
471 unsigned Line = V->getLineNumber();
472 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
473 assert(FileID && "Invalid file id");
474 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
475 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
476}
477
478/// AddSourceLine - Add location information to specified debug information
479/// entry.
480void DwarfDebug::AddSourceLine(DIE *Die, const DIGlobal *G) {
481 // If there is no compile unit specified, don't add a line #.
482 if (G->getCompileUnit().isNull())
483 return;
484
485 unsigned Line = G->getLineNumber();
486 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
487 assert(FileID && "Invalid file id");
488 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
489 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
490}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000491
492/// AddSourceLine - Add location information to specified debug information
493/// entry.
494void DwarfDebug::AddSourceLine(DIE *Die, const DISubprogram *SP) {
495 // If there is no compile unit specified, don't add a line #.
496 if (SP->getCompileUnit().isNull())
497 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000498 // If the line number is 0, don't add it.
499 if (SP->getLineNumber() == 0)
500 return;
501
Devang Patel82dfc0c2009-08-31 22:47:13 +0000502
503 unsigned Line = SP->getLineNumber();
504 unsigned FileID = FindCompileUnit(SP->getCompileUnit()).getID();
505 assert(FileID && "Invalid file id");
506 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
507 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
508}
509
510/// AddSourceLine - Add location information to specified debug information
511/// entry.
Bill Wendling0310d762009-05-15 09:23:25 +0000512void DwarfDebug::AddSourceLine(DIE *Die, const DIType *Ty) {
513 // If there is no compile unit specified, don't add a line #.
514 DICompileUnit CU = Ty->getCompileUnit();
515 if (CU.isNull())
516 return;
517
518 unsigned Line = Ty->getLineNumber();
519 unsigned FileID = FindCompileUnit(CU).getID();
520 assert(FileID && "Invalid file id");
521 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
522 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
523}
524
Caroline Ticedc8f6042009-08-31 21:19:37 +0000525/* Byref variables, in Blocks, are declared by the programmer as
526 "SomeType VarName;", but the compiler creates a
527 __Block_byref_x_VarName struct, and gives the variable VarName
528 either the struct, or a pointer to the struct, as its type. This
529 is necessary for various behind-the-scenes things the compiler
530 needs to do with by-reference variables in blocks.
531
532 However, as far as the original *programmer* is concerned, the
533 variable should still have type 'SomeType', as originally declared.
534
535 The following function dives into the __Block_byref_x_VarName
536 struct to find the original type of the variable. This will be
537 passed back to the code generating the type for the Debug
538 Information Entry for the variable 'VarName'. 'VarName' will then
539 have the original type 'SomeType' in its debug information.
540
541 The original type 'SomeType' will be the type of the field named
542 'VarName' inside the __Block_byref_x_VarName struct.
543
544 NOTE: In order for this to not completely fail on the debugger
545 side, the Debug Information Entry for the variable VarName needs to
546 have a DW_AT_location that tells the debugger how to unwind through
547 the pointers and __Block_byref_x_VarName struct to find the actual
548 value of the variable. The function AddBlockByrefType does this. */
549
550/// Find the type the programmer originally declared the variable to be
551/// and return that type.
552///
553DIType DwarfDebug::GetBlockByrefType(DIType Ty, std::string Name) {
554
555 DIType subType = Ty;
556 unsigned tag = Ty.getTag();
557
558 if (tag == dwarf::DW_TAG_pointer_type) {
559 DIDerivedType DTy = DIDerivedType (Ty.getNode());
560 subType = DTy.getTypeDerivedFrom();
561 }
562
563 DICompositeType blockStruct = DICompositeType(subType.getNode());
564
565 DIArray Elements = blockStruct.getTypeArray();
566
567 if (Elements.isNull())
568 return Ty;
569
570 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
571 DIDescriptor Element = Elements.getElement(i);
572 DIDerivedType DT = DIDerivedType(Element.getNode());
573 std::string Name2;
574 DT.getName(Name2);
575 if (Name == Name2)
576 return (DT.getTypeDerivedFrom());
577 }
578
579 return Ty;
580}
581
582/* Byref variables, in Blocks, are declared by the programmer as "SomeType
583 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
584 gives the variable VarName either the struct, or a pointer to the struct, as
585 its type. This is necessary for various behind-the-scenes things the
586 compiler needs to do with by-reference variables in Blocks.
587
588 However, as far as the original *programmer* is concerned, the variable
589 should still have type 'SomeType', as originally declared.
590
591 The function GetBlockByrefType dives into the __Block_byref_x_VarName
592 struct to find the original type of the variable, which is then assigned to
593 the variable's Debug Information Entry as its real type. So far, so good.
594 However now the debugger will expect the variable VarName to have the type
595 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000596 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000597 pointers and struct to find the actual variable of type SomeType.
598
599 The following function does just that. We start by getting
600 the "normal" location for the variable. This will be the location
601 of either the struct __Block_byref_x_VarName or the pointer to the
602 struct __Block_byref_x_VarName.
603
604 The struct will look something like:
605
606 struct __Block_byref_x_VarName {
607 ... <various fields>
608 struct __Block_byref_x_VarName *forwarding;
609 ... <various other fields>
610 SomeType VarName;
611 ... <maybe more fields>
612 };
613
614 If we are given the struct directly (as our starting point) we
615 need to tell the debugger to:
616
617 1). Add the offset of the forwarding field.
618
619 2). Follow that pointer to get the the real __Block_byref_x_VarName
620 struct to use (the real one may have been copied onto the heap).
621
622 3). Add the offset for the field VarName, to find the actual variable.
623
624 If we started with a pointer to the struct, then we need to
625 dereference that pointer first, before the other steps.
626 Translating this into DWARF ops, we will need to append the following
627 to the current location description for the variable:
628
629 DW_OP_deref -- optional, if we start with a pointer
630 DW_OP_plus_uconst <forward_fld_offset>
631 DW_OP_deref
632 DW_OP_plus_uconst <varName_fld_offset>
633
634 That is what this function does. */
635
636/// AddBlockByrefAddress - Start with the address based on the location
637/// provided, and generate the DWARF information necessary to find the
638/// actual Block variable (navigating the Block struct) based on the
639/// starting location. Add the DWARF information to the die. For
640/// more information, read large comment just above here.
641///
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000642void DwarfDebug::AddBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Caroline Ticedc8f6042009-08-31 21:19:37 +0000643 unsigned Attribute,
644 const MachineLocation &Location) {
645 const DIVariable &VD = DV->getVariable();
646 DIType Ty = VD.getType();
647 DIType TmpTy = Ty;
648 unsigned Tag = Ty.getTag();
649 bool isPointer = false;
650
651 std::string varName;
652 VD.getName(varName);
653
654 if (Tag == dwarf::DW_TAG_pointer_type) {
655 DIDerivedType DTy = DIDerivedType (Ty.getNode());
656 TmpTy = DTy.getTypeDerivedFrom();
657 isPointer = true;
658 }
659
660 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
661
662 std::string typeName;
663 blockStruct.getName(typeName);
664
665 assert(typeName.find ("__Block_byref_") == 0
666 && "Attempting to get Block location of non-Block variable!");
667
668 // Find the __forwarding field and the variable field in the __Block_byref
669 // struct.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000670
Caroline Ticedc8f6042009-08-31 21:19:37 +0000671 DIArray Fields = blockStruct.getTypeArray();
672 DIDescriptor varField = DIDescriptor();
673 DIDescriptor forwardingField = DIDescriptor();
674
675
676 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
677 DIDescriptor Element = Fields.getElement(i);
678 DIDerivedType DT = DIDerivedType(Element.getNode());
679 std::string fieldName;
680 DT.getName(fieldName);
681 if (fieldName == "__forwarding")
682 forwardingField = Element;
683 else if (fieldName == varName)
684 varField = Element;
685 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000686
Caroline Ticedc8f6042009-08-31 21:19:37 +0000687 assert (!varField.isNull() && "Can't find byref variable in Block struct");
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000688 assert (!forwardingField.isNull()
Caroline Ticedc8f6042009-08-31 21:19:37 +0000689 && "Can't find forwarding field in Block struct");
690
691 // Get the offsets for the forwarding field and the variable field.
692
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000693 unsigned int forwardingFieldOffset =
Caroline Ticedc8f6042009-08-31 21:19:37 +0000694 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000695 unsigned int varFieldOffset =
Caroline Ticedc8f6042009-08-31 21:19:37 +0000696 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
697
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000698 // Decode the original location, and use that as the start of the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000699 // byref variable's location.
700
701 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
702 DIEBlock *Block = new DIEBlock();
703
704 if (Location.isReg()) {
705 if (Reg < 32)
706 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
707 else {
708 Reg = Reg - dwarf::DW_OP_reg0;
709 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
710 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
711 }
712 } else {
713 if (Reg < 32)
714 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
715 else {
716 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
717 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
718 }
719
720 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
721 }
722
723 // If we started with a pointer to the__Block_byref... struct, then
724 // the first thing we need to do is dereference the pointer (DW_OP_deref).
725
726 if (isPointer)
727 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
728
729 // Next add the offset for the '__forwarding' field:
730 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
731 // adding the offset if it's 0.
732
733 if (forwardingFieldOffset > 0) {
734 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
735 AddUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
736 }
737
738 // Now dereference the __forwarding field to get to the real __Block_byref
739 // struct: DW_OP_deref.
740
741 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
742
743 // Now that we've got the real __Block_byref... struct, add the offset
744 // for the variable's field to get to the location of the actual variable:
745 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
746
747 if (varFieldOffset > 0) {
748 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
749 AddUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
750 }
751
752 // Now attach the location information to the DIE.
753
754 AddBlock(Die, Attribute, 0, Block);
755}
756
Bill Wendling0310d762009-05-15 09:23:25 +0000757/// AddAddress - Add an address attribute to a die based on the location
758/// provided.
759void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute,
760 const MachineLocation &Location) {
761 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
762 DIEBlock *Block = new DIEBlock();
763
764 if (Location.isReg()) {
765 if (Reg < 32) {
766 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
767 } else {
768 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
769 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
770 }
771 } else {
772 if (Reg < 32) {
773 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
774 } else {
775 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
776 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
777 }
778
779 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
780 }
781
782 AddBlock(Die, Attribute, 0, Block);
783}
784
785/// AddType - Add a new type attribute to the specified entity.
786void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
787 if (Ty.isNull())
788 return;
789
790 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +0000791 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000792
793 // If it exists then use the existing value.
794 if (Slot) {
795 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
796 return;
797 }
798
799 // Set up proxy.
Bill Wendling995f80a2009-05-20 23:24:48 +0000800 Slot = CreateDIEEntry();
Bill Wendling0310d762009-05-15 09:23:25 +0000801
802 // Construct type.
803 DIE Buffer(dwarf::DW_TAG_base_type);
Devang Patel6ceea332009-08-31 18:49:10 +0000804 if (Ty.isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000805 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getNode()));
Devang Patel6ceea332009-08-31 18:49:10 +0000806 else if (Ty.isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000807 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000808 else {
Devang Patel6ceea332009-08-31 18:49:10 +0000809 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patele4b27562009-08-28 23:24:31 +0000810 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getNode()));
811
Bill Wendling0310d762009-05-15 09:23:25 +0000812 }
813
814 // Add debug information entry to entity and appropriate context.
815 DIE *Die = NULL;
816 DIDescriptor Context = Ty.getContext();
817 if (!Context.isNull())
Devang Patele4b27562009-08-28 23:24:31 +0000818 Die = DW_Unit->getDieMapSlotFor(Context.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000819
820 if (Die) {
821 DIE *Child = new DIE(Buffer);
822 Die->AddChild(Child);
823 Buffer.Detach();
824 SetDIEEntry(Slot, Child);
825 } else {
826 Die = DW_Unit->AddDie(Buffer);
827 SetDIEEntry(Slot, Die);
828 }
829
830 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
831}
832
833/// ConstructTypeDIE - Construct basic type die from DIBasicType.
834void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
835 DIBasicType BTy) {
836 // Get core information.
837 std::string Name;
838 BTy.getName(Name);
839 Buffer.setTag(dwarf::DW_TAG_base_type);
840 AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
841 BTy.getEncoding());
842
843 // Add name if not anonymous or intermediate type.
844 if (!Name.empty())
845 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
846 uint64_t Size = BTy.getSizeInBits() >> 3;
847 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
848}
849
850/// ConstructTypeDIE - Construct derived type die from DIDerivedType.
851void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
852 DIDerivedType DTy) {
853 // Get core information.
854 std::string Name;
855 DTy.getName(Name);
856 uint64_t Size = DTy.getSizeInBits() >> 3;
857 unsigned Tag = DTy.getTag();
858
859 // FIXME - Workaround for templates.
860 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
861
862 Buffer.setTag(Tag);
863
864 // Map to main type, void will not have a type.
865 DIType FromTy = DTy.getTypeDerivedFrom();
866 AddType(DW_Unit, &Buffer, FromTy);
867
868 // Add name if not anonymous or intermediate type.
869 if (!Name.empty())
870 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
871
872 // Add size if non-zero (derived types might be zero-sized.)
873 if (Size)
874 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
875
876 // Add source line info if available and TyDesc is not a forward declaration.
877 if (!DTy.isForwardDecl())
878 AddSourceLine(&Buffer, &DTy);
879}
880
881/// ConstructTypeDIE - Construct type DIE from DICompositeType.
882void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
883 DICompositeType CTy) {
884 // Get core information.
885 std::string Name;
886 CTy.getName(Name);
887
888 uint64_t Size = CTy.getSizeInBits() >> 3;
889 unsigned Tag = CTy.getTag();
890 Buffer.setTag(Tag);
891
892 switch (Tag) {
893 case dwarf::DW_TAG_vector_type:
894 case dwarf::DW_TAG_array_type:
895 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
896 break;
897 case dwarf::DW_TAG_enumeration_type: {
898 DIArray Elements = CTy.getTypeArray();
899
900 // Add enumerators to enumeration type.
901 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
902 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000903 DIEnumerator Enum(Elements.getElement(i).getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000904 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
905 Buffer.AddChild(ElemDie);
906 }
907 }
908 break;
909 case dwarf::DW_TAG_subroutine_type: {
910 // Add return type.
911 DIArray Elements = CTy.getTypeArray();
912 DIDescriptor RTy = Elements.getElement(0);
Devang Patele4b27562009-08-28 23:24:31 +0000913 AddType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000914
915 // Add prototype flag.
916 AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
917
918 // Add arguments.
919 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
920 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
921 DIDescriptor Ty = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000922 AddType(DW_Unit, Arg, DIType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000923 Buffer.AddChild(Arg);
924 }
925 }
926 break;
927 case dwarf::DW_TAG_structure_type:
928 case dwarf::DW_TAG_union_type:
929 case dwarf::DW_TAG_class_type: {
930 // Add elements to structure type.
931 DIArray Elements = CTy.getTypeArray();
932
933 // A forward struct declared type may not have elements available.
934 if (Elements.isNull())
935 break;
936
937 // Add elements to structure type.
938 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
939 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000940 if (Element.isNull())
941 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000942 DIE *ElemDie = NULL;
943 if (Element.getTag() == dwarf::DW_TAG_subprogram)
944 ElemDie = CreateSubprogramDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000945 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000946 else
947 ElemDie = CreateMemberDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000948 DIDerivedType(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000949 Buffer.AddChild(ElemDie);
950 }
951
Devang Patela1ba2692009-08-27 23:51:51 +0000952 if (CTy.isAppleBlockExtension())
Bill Wendling0310d762009-05-15 09:23:25 +0000953 AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
954
955 unsigned RLang = CTy.getRunTimeLang();
956 if (RLang)
957 AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
958 dwarf::DW_FORM_data1, RLang);
959 break;
960 }
961 default:
962 break;
963 }
964
965 // Add name if not anonymous or intermediate type.
966 if (!Name.empty())
967 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
968
969 if (Tag == dwarf::DW_TAG_enumeration_type ||
970 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
971 // Add size if non-zero (derived types might be zero-sized.)
972 if (Size)
973 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
974 else {
975 // Add zero size if it is not a forward declaration.
976 if (CTy.isForwardDecl())
977 AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
978 else
979 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
980 }
981
982 // Add source line info if available.
983 if (!CTy.isForwardDecl())
984 AddSourceLine(&Buffer, &CTy);
985 }
986}
987
988/// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
989void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
990 int64_t L = SR.getLo();
991 int64_t H = SR.getHi();
992 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
993
Devang Patel6325a532009-08-14 20:59:16 +0000994 AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
995 if (L)
996 AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
997 if (H)
998 AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000999
1000 Buffer.AddChild(DW_Subrange);
1001}
1002
1003/// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1004void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1005 DICompositeType *CTy) {
1006 Buffer.setTag(dwarf::DW_TAG_array_type);
1007 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1008 AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
1009
1010 // Emit derived type.
1011 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
1012 DIArray Elements = CTy->getTypeArray();
1013
1014 // Construct an anonymous type for index type.
1015 DIE IdxBuffer(dwarf::DW_TAG_base_type);
1016 AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1017 AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1018 dwarf::DW_ATE_signed);
1019 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1020
1021 // Add subranges to array type.
1022 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1023 DIDescriptor Element = Elements.getElement(i);
1024 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patele4b27562009-08-28 23:24:31 +00001025 ConstructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IndexTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001026 }
1027}
1028
1029/// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1030DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
1031 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1032 std::string Name;
1033 ETy->getName(Name);
1034 AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1035 int64_t Value = ETy->getEnumValue();
1036 AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1037 return Enumerator;
1038}
1039
1040/// CreateGlobalVariableDIE - Create new DIE using GV.
1041DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit,
1042 const DIGlobalVariable &GV) {
1043 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
1044 std::string Name;
1045 GV.getDisplayName(Name);
1046 AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1047 std::string LinkageName;
1048 GV.getLinkageName(LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001049 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001050 // Skip special LLVM prefix that is used to inform the asm printer to not
1051 // emit usual symbol prefix before the symbol name. This happens for
1052 // Objective-C symbol names and symbol whose name is replaced using GCC's
1053 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001054 if (LinkageName[0] == 1)
1055 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001056 AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001057 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001058 }
1059 AddType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001060 if (!GV.isLocalToUnit())
1061 AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1062 AddSourceLine(GVDie, &GV);
1063 return GVDie;
1064}
1065
1066/// CreateMemberDIE - Create new member DIE.
1067DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
1068 DIE *MemberDie = new DIE(DT.getTag());
1069 std::string Name;
1070 DT.getName(Name);
1071 if (!Name.empty())
1072 AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1073
1074 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1075
1076 AddSourceLine(MemberDie, &DT);
1077
1078 uint64_t Size = DT.getSizeInBits();
1079 uint64_t FieldSize = DT.getOriginalTypeSize();
1080
1081 if (Size != FieldSize) {
1082 // Handle bitfield.
1083 AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1084 AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1085
1086 uint64_t Offset = DT.getOffsetInBits();
1087 uint64_t FieldOffset = Offset;
1088 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1089 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1090 FieldOffset = (HiMark - FieldSize);
1091 Offset -= FieldOffset;
1092
1093 // Maybe we need to work from the other end.
1094 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1095 AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1096 }
1097
1098 DIEBlock *Block = new DIEBlock();
1099 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1100 AddUInt(Block, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1101 AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, Block);
1102
1103 if (DT.isProtected())
1104 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1105 dwarf::DW_ACCESS_protected);
1106 else if (DT.isPrivate())
1107 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1108 dwarf::DW_ACCESS_private);
1109
1110 return MemberDie;
1111}
1112
1113/// CreateSubprogramDIE - Create new DIE using SP.
1114DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit,
1115 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +00001116 bool IsConstructor,
1117 bool IsInlined) {
Bill Wendling0310d762009-05-15 09:23:25 +00001118 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1119
1120 std::string Name;
1121 SP.getName(Name);
1122 AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1123
1124 std::string LinkageName;
1125 SP.getLinkageName(LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001126 if (!LinkageName.empty()) {
1127 // Skip special LLVM prefix that is used to inform the asm printer to not emit
1128 // usual symbol prefix before the symbol name. This happens for Objective-C
1129 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
1130 if (LinkageName[0] == 1)
1131 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001132 AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001133 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001134 }
Bill Wendling0310d762009-05-15 09:23:25 +00001135 AddSourceLine(SPDie, &SP);
1136
1137 DICompositeType SPTy = SP.getType();
1138 DIArray Args = SPTy.getTypeArray();
1139
1140 // Add prototyped tag, if C or ObjC.
1141 unsigned Lang = SP.getCompileUnit().getLanguage();
1142 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1143 Lang == dwarf::DW_LANG_ObjC)
1144 AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
1145
1146 // Add Return Type.
1147 unsigned SPTag = SPTy.getTag();
1148 if (!IsConstructor) {
1149 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1150 AddType(DW_Unit, SPDie, SPTy);
1151 else
Devang Patele4b27562009-08-28 23:24:31 +00001152 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001153 }
1154
1155 if (!SP.isDefinition()) {
1156 AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1157
1158 // Add arguments. Do not add arguments for subprogram definition. They will
1159 // be handled through RecordVariable.
1160 if (SPTag == dwarf::DW_TAG_subroutine_type)
1161 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1162 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patele4b27562009-08-28 23:24:31 +00001163 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001164 AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1165 SPDie->AddChild(Arg);
1166 }
1167 }
1168
Bill Wendling6679ee42009-05-18 22:02:36 +00001169 if (!SP.isLocalToUnit() && !IsInlined)
Bill Wendling0310d762009-05-15 09:23:25 +00001170 AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1171
1172 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patele4b27562009-08-28 23:24:31 +00001173 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001174 Slot = SPDie;
1175 return SPDie;
1176}
1177
1178/// FindCompileUnit - Get the compile unit for the given descriptor.
1179///
1180CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const {
1181 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001182 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001183 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1184 return *I->second;
1185}
1186
Bill Wendling995f80a2009-05-20 23:24:48 +00001187/// CreateDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +00001188///
Bill Wendling995f80a2009-05-20 23:24:48 +00001189DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001190 // Get the descriptor.
1191 const DIVariable &VD = DV->getVariable();
1192
1193 // Translate tag to proper Dwarf tag. The result variable is dropped for
1194 // now.
1195 unsigned Tag;
1196 switch (VD.getTag()) {
1197 case dwarf::DW_TAG_return_variable:
1198 return NULL;
1199 case dwarf::DW_TAG_arg_variable:
1200 Tag = dwarf::DW_TAG_formal_parameter;
1201 break;
1202 case dwarf::DW_TAG_auto_variable: // fall thru
1203 default:
1204 Tag = dwarf::DW_TAG_variable;
1205 break;
1206 }
1207
1208 // Define variable debug information entry.
1209 DIE *VariableDie = new DIE(Tag);
1210 std::string Name;
1211 VD.getName(Name);
1212 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1213
1214 // Add source line info if available.
1215 AddSourceLine(VariableDie, &VD);
1216
1217 // Add variable type.
Caroline Ticedc8f6042009-08-31 21:19:37 +00001218 if (VD.isBlockByrefVariable())
1219 AddType(Unit, VariableDie, GetBlockByrefType(VD.getType(), Name));
1220 else
1221 AddType(Unit, VariableDie, VD.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001222
1223 // Add variable address.
Bill Wendling1180c782009-05-18 23:08:55 +00001224 if (!DV->isInlinedFnVar()) {
1225 // Variables for abstract instances of inlined functions don't get a
1226 // location.
1227 MachineLocation Location;
1228 Location.set(RI->getFrameRegister(*MF),
1229 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Caroline Ticedc8f6042009-08-31 21:19:37 +00001230
1231 if (VD.isBlockByrefVariable())
1232 AddBlockByrefAddress (DV, VariableDie, dwarf::DW_AT_location, Location);
1233 else
1234 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling1180c782009-05-18 23:08:55 +00001235 }
Bill Wendling0310d762009-05-15 09:23:25 +00001236
1237 return VariableDie;
1238}
1239
1240/// getOrCreateScope - Returns the scope associated with the given descriptor.
1241///
Devang Patele4b27562009-08-28 23:24:31 +00001242DbgScope *DwarfDebug::getOrCreateScope(MDNode *N) {
1243 DbgScope *&Slot = DbgScopeMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +00001244 if (Slot) return Slot;
1245
1246 DbgScope *Parent = NULL;
Devang Patel5e005d82009-08-31 22:00:15 +00001247 DILexicalBlock Block(N);
Bill Wendling0310d762009-05-15 09:23:25 +00001248
Bill Wendling8fff19b2009-06-01 20:18:46 +00001249 // Don't create a new scope if we already created one for an inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001250 DenseMap<const MDNode *, DbgScope *>::iterator
1251 II = AbstractInstanceRootMap.find(N);
Bill Wendling8fff19b2009-06-01 20:18:46 +00001252 if (II != AbstractInstanceRootMap.end())
1253 return LexicalScopeStack.back();
1254
Bill Wendling0310d762009-05-15 09:23:25 +00001255 if (!Block.isNull()) {
1256 DIDescriptor ParentDesc = Block.getContext();
1257 Parent =
Devang Patele4b27562009-08-28 23:24:31 +00001258 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001259 }
1260
Devang Patele4b27562009-08-28 23:24:31 +00001261 Slot = new DbgScope(Parent, DIDescriptor(N));
Bill Wendling0310d762009-05-15 09:23:25 +00001262
1263 if (Parent)
1264 Parent->AddScope(Slot);
1265 else
1266 // First function is top level function.
1267 FunctionDbgScope = Slot;
1268
1269 return Slot;
1270}
1271
1272/// ConstructDbgScope - Construct the components of a scope.
1273///
1274void DwarfDebug::ConstructDbgScope(DbgScope *ParentScope,
1275 unsigned ParentStartID,
1276 unsigned ParentEndID,
1277 DIE *ParentDie, CompileUnit *Unit) {
1278 // Add variables to scope.
1279 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
1280 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Bill Wendling995f80a2009-05-20 23:24:48 +00001281 DIE *VariableDie = CreateDbgScopeVariable(Variables[i], Unit);
Bill Wendling0310d762009-05-15 09:23:25 +00001282 if (VariableDie) ParentDie->AddChild(VariableDie);
1283 }
1284
1285 // Add concrete instances to scope.
1286 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts =
1287 ParentScope->getConcreteInsts();
1288 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
1289 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
1290 DIE *Die = ConcreteInst->getDie();
1291
1292 unsigned StartID = ConcreteInst->getStartLabelID();
1293 unsigned EndID = ConcreteInst->getEndLabelID();
1294
1295 // Add the scope bounds.
1296 if (StartID)
1297 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1298 DWLabel("label", StartID));
1299 else
1300 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1301 DWLabel("func_begin", SubprogramCount));
1302
1303 if (EndID)
1304 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1305 DWLabel("label", EndID));
1306 else
1307 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1308 DWLabel("func_end", SubprogramCount));
1309
1310 ParentDie->AddChild(Die);
1311 }
1312
1313 // Add nested scopes.
1314 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
1315 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1316 // Define the Scope debug information entry.
1317 DbgScope *Scope = Scopes[j];
1318
1319 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1320 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1321
1322 // Ignore empty scopes.
1323 if (StartID == EndID && StartID != 0) continue;
1324
1325 // Do not ignore inlined scopes even if they don't have any variables or
1326 // scopes.
1327 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
1328 Scope->getConcreteInsts().empty())
1329 continue;
1330
1331 if (StartID == ParentStartID && EndID == ParentEndID) {
1332 // Just add stuff to the parent scope.
1333 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1334 } else {
1335 DIE *ScopeDie = new DIE(dwarf::DW_TAG_lexical_block);
1336
1337 // Add the scope bounds.
1338 if (StartID)
1339 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1340 DWLabel("label", StartID));
1341 else
1342 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1343 DWLabel("func_begin", SubprogramCount));
1344
1345 if (EndID)
1346 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1347 DWLabel("label", EndID));
1348 else
1349 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1350 DWLabel("func_end", SubprogramCount));
1351
1352 // Add the scope's contents.
1353 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1354 ParentDie->AddChild(ScopeDie);
1355 }
1356 }
1357}
1358
1359/// ConstructFunctionDbgScope - Construct the scope for the subprogram.
1360///
Bill Wendling17956162009-05-20 23:28:48 +00001361void DwarfDebug::ConstructFunctionDbgScope(DbgScope *RootScope,
1362 bool AbstractScope) {
Bill Wendling0310d762009-05-15 09:23:25 +00001363 // Exit if there is no root scope.
1364 if (!RootScope) return;
1365 DIDescriptor Desc = RootScope->getDesc();
1366 if (Desc.isNull())
1367 return;
1368
1369 // Get the subprogram debug information entry.
Devang Patele4b27562009-08-28 23:24:31 +00001370 DISubprogram SPD(Desc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001371
Bill Wendling0310d762009-05-15 09:23:25 +00001372 // Get the subprogram die.
Devang Patele4b27562009-08-28 23:24:31 +00001373 DIE *SPDie = ModuleCU->getDieMapSlotFor(SPD.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001374 assert(SPDie && "Missing subprogram descriptor");
1375
Bill Wendling17956162009-05-20 23:28:48 +00001376 if (!AbstractScope) {
1377 // Add the function bounds.
1378 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1379 DWLabel("func_begin", SubprogramCount));
1380 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1381 DWLabel("func_end", SubprogramCount));
1382 MachineLocation Location(RI->getFrameRegister(*MF));
1383 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
1384 }
Bill Wendling0310d762009-05-15 09:23:25 +00001385
Devang Patel1dbc7712009-06-29 20:45:18 +00001386 ConstructDbgScope(RootScope, 0, 0, SPDie, ModuleCU);
Bill Wendling0310d762009-05-15 09:23:25 +00001387}
1388
Bill Wendling0310d762009-05-15 09:23:25 +00001389/// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1390///
1391void DwarfDebug::ConstructDefaultDbgScope(MachineFunction *MF) {
Devang Patel1dbc7712009-06-29 20:45:18 +00001392 StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Daniel Dunbar460f6562009-07-26 09:48:23 +00001393 StringMap<DIE*>::iterator GI = Globals.find(MF->getFunction()->getName());
Devang Patel70f44262009-06-29 20:38:13 +00001394 if (GI != Globals.end()) {
1395 DIE *SPDie = GI->second;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001396
Devang Patel70f44262009-06-29 20:38:13 +00001397 // Add the function bounds.
1398 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1399 DWLabel("func_begin", SubprogramCount));
1400 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1401 DWLabel("func_end", SubprogramCount));
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001402
Devang Patel70f44262009-06-29 20:38:13 +00001403 MachineLocation Location(RI->getFrameRegister(*MF));
1404 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001405 }
Bill Wendling0310d762009-05-15 09:23:25 +00001406}
1407
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001408/// GetOrCreateSourceID - Look up the source id with the given directory and
1409/// source file names. If none currently exists, create a new id and insert it
1410/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1411/// maps as well.
1412unsigned DwarfDebug::GetOrCreateSourceID(const std::string &DirName,
1413 const std::string &FileName) {
1414 unsigned DId;
1415 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1416 if (DI != DirectoryIdMap.end()) {
1417 DId = DI->getValue();
1418 } else {
1419 DId = DirectoryNames.size() + 1;
1420 DirectoryIdMap[DirName] = DId;
1421 DirectoryNames.push_back(DirName);
1422 }
1423
1424 unsigned FId;
1425 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1426 if (FI != SourceFileIdMap.end()) {
1427 FId = FI->getValue();
1428 } else {
1429 FId = SourceFileNames.size() + 1;
1430 SourceFileIdMap[FileName] = FId;
1431 SourceFileNames.push_back(FileName);
1432 }
1433
1434 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1435 SourceIdMap.find(std::make_pair(DId, FId));
1436 if (SI != SourceIdMap.end())
1437 return SI->second;
1438
1439 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1440 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1441 SourceIds.push_back(std::make_pair(DId, FId));
1442
1443 return SrcId;
1444}
1445
Devang Patele4b27562009-08-28 23:24:31 +00001446void DwarfDebug::ConstructCompileUnit(MDNode *N) {
1447 DICompileUnit DIUnit(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001448 std::string Dir, FN, Prod;
1449 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
1450 DIUnit.getFilename(FN));
1451
1452 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
1453 AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
1454 DWLabel("section_line", 0), DWLabel("section_line", 0),
1455 false);
1456 AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
1457 DIUnit.getProducer(Prod));
1458 AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
1459 DIUnit.getLanguage());
1460 AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
1461
1462 if (!Dir.empty())
1463 AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
1464 if (DIUnit.isOptimized())
1465 AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1466
1467 std::string Flags;
1468 DIUnit.getFlags(Flags);
1469 if (!Flags.empty())
1470 AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
1471
1472 unsigned RVer = DIUnit.getRunTimeVersion();
1473 if (RVer)
1474 AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1475 dwarf::DW_FORM_data1, RVer);
1476
1477 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001478 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001479 // Use first compile unit marked as isMain as the compile unit
1480 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001481 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001482 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001483
Devang Patele4b27562009-08-28 23:24:31 +00001484 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001485 CompileUnits.push_back(Unit);
1486}
1487
Devang Patele4b27562009-08-28 23:24:31 +00001488void DwarfDebug::ConstructGlobalVariableDIE(MDNode *N) {
1489 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001490
Devang Patel905cf5e2009-09-04 23:59:07 +00001491 // If debug information is malformed then ignore it.
1492 if (DI_GV.Verify() == false)
1493 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001494
1495 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001496 DIE *&Slot = ModuleCU->getDieMapSlotFor(DI_GV.getNode());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001497 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001498 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001499
Devang Patel1dbc7712009-06-29 20:45:18 +00001500 DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001501
1502 // Add address.
1503 DIEBlock *Block = new DIEBlock();
1504 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001505 AddObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner334fd1f2009-09-16 00:08:41 +00001506 Asm->Mang->getMangledName(DI_GV.getGlobal()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001507 AddBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1508
1509 // Add to map.
1510 Slot = VariableDie;
1511
1512 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001513 ModuleCU->getDie()->AddChild(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001514
1515 // Expose as global. FIXME - need to check external flag.
1516 std::string Name;
Devang Patel1dbc7712009-06-29 20:45:18 +00001517 ModuleCU->AddGlobal(DI_GV.getName(Name), VariableDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001518 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001519}
1520
Devang Patele4b27562009-08-28 23:24:31 +00001521void DwarfDebug::ConstructSubprogram(MDNode *N) {
1522 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001523
1524 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001525 DIE *&Slot = ModuleCU->getDieMapSlotFor(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001526 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001527 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001528
1529 if (!SP.isDefinition())
1530 // This is a method declaration which will be handled while constructing
1531 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001532 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001533
Devang Patel1dbc7712009-06-29 20:45:18 +00001534 DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001535
1536 // Add to map.
1537 Slot = SubprogramDie;
1538
1539 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001540 ModuleCU->getDie()->AddChild(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001541
1542 // Expose as global.
1543 std::string Name;
Devang Patel1dbc7712009-06-29 20:45:18 +00001544 ModuleCU->AddGlobal(SP.getName(Name), SubprogramDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001545 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001546}
1547
Devang Patel208622d2009-06-25 22:36:02 +00001548 /// BeginModule - Emit all Dwarf sections that should come prior to the
1549 /// content. Create global DIEs and emit initial debug info sections.
1550 /// This is inovked by the target AsmPrinter.
1551void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) {
1552 this->M = M;
1553
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001554 if (TimePassesIsEnabled)
1555 DebugTimer->startTimer();
1556
Devang Patel78ab9e22009-07-30 18:56:46 +00001557 DebugInfoFinder DbgFinder;
1558 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001559
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001560 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001561 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1562 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001563 ConstructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001564
1565 if (CompileUnits.empty()) {
1566 if (TimePassesIsEnabled)
1567 DebugTimer->stopTimer();
1568
1569 return;
1570 }
1571
Devang Patel70f44262009-06-29 20:38:13 +00001572 // If main compile unit for this module is not seen than randomly
1573 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001574 if (!ModuleCU)
1575 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001576
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001577 // If there is not any debug info available for any global variables and any
1578 // subprograms then there is not any debug info to emit.
Devang Patel78ab9e22009-07-30 18:56:46 +00001579 if (DbgFinder.global_variable_count() == 0
1580 && DbgFinder.subprogram_count() == 0) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001581 if (TimePassesIsEnabled)
1582 DebugTimer->stopTimer();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001583 return;
1584 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001585
Devang Patel13e16b62009-06-26 01:49:18 +00001586 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001587 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1588 E = DbgFinder.global_variable_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001589 ConstructGlobalVariableDIE(*I);
1590
1591 // Create DIEs for each of the externally visible subprograms.
Devang Patel78ab9e22009-07-30 18:56:46 +00001592 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1593 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001594 ConstructSubprogram(*I);
1595
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001596 MMI = mmi;
1597 shouldEmit = true;
1598 MMI->setDebugInfoAvailability(true);
1599
1600 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001601 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001602
1603 // Print out .file directives to specify files for .loc directives. These are
1604 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001605 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001606 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1607 // Remember source id starts at 1.
1608 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1609 sys::Path FullPath(getSourceDirectoryName(Id.first));
1610 bool AppendOk =
1611 FullPath.appendComponent(getSourceFileName(Id.second));
1612 assert(AppendOk && "Could not append filename to directory!");
1613 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001614 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001615 Asm->EOL();
1616 }
1617 }
1618
1619 // Emit initial sections
1620 EmitInitial();
1621
1622 if (TimePassesIsEnabled)
1623 DebugTimer->stopTimer();
1624}
1625
1626/// EndModule - Emit all Dwarf sections that should come after the content.
1627///
1628void DwarfDebug::EndModule() {
1629 if (!ShouldEmitDwarfDebug())
1630 return;
1631
1632 if (TimePassesIsEnabled)
1633 DebugTimer->startTimer();
1634
1635 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001636 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001637 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001638 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001639 EmitLabel("data_end", 0);
1640
1641 // End text sections.
1642 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001643 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001644 EmitLabel("section_end", i);
1645 }
1646
1647 // Emit common frame information.
1648 EmitCommonDebugFrame();
1649
1650 // Emit function debug frame information
1651 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1652 E = DebugFrames.end(); I != E; ++I)
1653 EmitFunctionDebugFrame(*I);
1654
1655 // Compute DIE offsets and sizes.
1656 SizeAndOffsets();
1657
1658 // Emit all the DIEs into a debug info section
1659 EmitDebugInfo();
1660
1661 // Corresponding abbreviations into a abbrev section.
1662 EmitAbbreviations();
1663
1664 // Emit source line correspondence into a debug line section.
1665 EmitDebugLines();
1666
1667 // Emit info into a debug pubnames section.
1668 EmitDebugPubNames();
1669
1670 // Emit info into a debug str section.
1671 EmitDebugStr();
1672
1673 // Emit info into a debug loc section.
1674 EmitDebugLoc();
1675
1676 // Emit info into a debug aranges section.
1677 EmitDebugARanges();
1678
1679 // Emit info into a debug ranges section.
1680 EmitDebugRanges();
1681
1682 // Emit info into a debug macinfo section.
1683 EmitDebugMacInfo();
1684
1685 // Emit inline info.
1686 EmitDebugInlineInfo();
1687
1688 if (TimePassesIsEnabled)
1689 DebugTimer->stopTimer();
1690}
1691
1692/// BeginFunction - Gather pre-function debug information. Assumes being
1693/// emitted immediately after the function entry point.
1694void DwarfDebug::BeginFunction(MachineFunction *MF) {
1695 this->MF = MF;
1696
1697 if (!ShouldEmitDwarfDebug()) return;
1698
1699 if (TimePassesIsEnabled)
1700 DebugTimer->startTimer();
1701
1702 // Begin accumulating function debug information.
1703 MMI->BeginFunction(MF);
1704
1705 // Assumes in correct section after the entry point.
1706 EmitLabel("func_begin", ++SubprogramCount);
1707
1708 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1709 // function.
1710 DebugLoc FDL = MF->getDefaultDebugLoc();
1711 if (!FDL.isUnknown()) {
1712 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
1713 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
1714 DICompileUnit(DLT.CompileUnit));
1715 Asm->printLabel(LabelID);
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001716 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001717 }
1718
1719 if (TimePassesIsEnabled)
1720 DebugTimer->stopTimer();
1721}
1722
1723/// EndFunction - Gather and emit post-function debug information.
1724///
1725void DwarfDebug::EndFunction(MachineFunction *MF) {
1726 if (!ShouldEmitDwarfDebug()) return;
1727
1728 if (TimePassesIsEnabled)
1729 DebugTimer->startTimer();
1730
1731 // Define end label for subprogram.
1732 EmitLabel("func_end", SubprogramCount);
1733
1734 // Get function line info.
1735 if (!Lines.empty()) {
1736 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00001737 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001738 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
1739 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
1740 // Append the function info to section info.
1741 SectionLineInfos.insert(SectionLineInfos.end(),
1742 Lines.begin(), Lines.end());
1743 }
1744
1745 // Construct the DbgScope for abstract instances.
1746 for (SmallVector<DbgScope *, 32>::iterator
1747 I = AbstractInstanceRootList.begin(),
1748 E = AbstractInstanceRootList.end(); I != E; ++I)
Bill Wendling17956162009-05-20 23:28:48 +00001749 ConstructFunctionDbgScope(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001750
1751 // Construct scopes for subprogram.
1752 if (FunctionDbgScope)
1753 ConstructFunctionDbgScope(FunctionDbgScope);
1754 else
1755 // FIXME: This is wrong. We are essentially getting past a problem with
1756 // debug information not being able to handle unreachable blocks that have
1757 // debug information in them. In particular, those unreachable blocks that
1758 // have "region end" info in them. That situation results in the "root
1759 // scope" not being created. If that's the case, then emit a "default"
1760 // scope, i.e., one that encompasses the whole function. This isn't
1761 // desirable. And a better way of handling this (and all of the debugging
1762 // information) needs to be explored.
1763 ConstructDefaultDbgScope(MF);
1764
1765 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
1766 MMI->getFrameMoves()));
1767
1768 // Clear debug info
1769 if (FunctionDbgScope) {
1770 delete FunctionDbgScope;
1771 DbgScopeMap.clear();
1772 DbgAbstractScopeMap.clear();
1773 DbgConcreteScopeMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001774 FunctionDbgScope = NULL;
1775 LexicalScopeStack.clear();
1776 AbstractInstanceRootList.clear();
Devang Patel9217f792009-06-12 19:24:05 +00001777 AbstractInstanceRootMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001778 }
1779
1780 Lines.clear();
1781
1782 if (TimePassesIsEnabled)
1783 DebugTimer->stopTimer();
1784}
1785
1786/// RecordSourceLine - Records location information and associates it with a
1787/// label. Returns a unique label ID used to generate a label and provide
1788/// correspondence to the source line list.
1789unsigned DwarfDebug::RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
1790 if (TimePassesIsEnabled)
1791 DebugTimer->startTimer();
1792
1793 CompileUnit *Unit = CompileUnitMap[V];
1794 assert(Unit && "Unable to find CompileUnit");
1795 unsigned ID = MMI->NextLabelID();
1796 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
1797
1798 if (TimePassesIsEnabled)
1799 DebugTimer->stopTimer();
1800
1801 return ID;
1802}
1803
1804/// RecordSourceLine - Records location information and associates it with a
1805/// label. Returns a unique label ID used to generate a label and provide
1806/// correspondence to the source line list.
1807unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col,
1808 DICompileUnit CU) {
Devang Patele4b27562009-08-28 23:24:31 +00001809 if (!MMI)
1810 return 0;
1811
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001812 if (TimePassesIsEnabled)
1813 DebugTimer->startTimer();
1814
1815 std::string Dir, Fn;
1816 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
1817 CU.getFilename(Fn));
1818 unsigned ID = MMI->NextLabelID();
1819 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
1820
1821 if (TimePassesIsEnabled)
1822 DebugTimer->stopTimer();
1823
1824 return ID;
1825}
1826
1827/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
1828/// timed. Look up the source id with the given directory and source file
1829/// names. If none currently exists, create a new id and insert it in the
1830/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
1831/// well.
1832unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
1833 const std::string &FileName) {
1834 if (TimePassesIsEnabled)
1835 DebugTimer->startTimer();
1836
1837 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
1838
1839 if (TimePassesIsEnabled)
1840 DebugTimer->stopTimer();
1841
1842 return SrcId;
1843}
1844
1845/// RecordRegionStart - Indicate the start of a region.
Devang Patele4b27562009-08-28 23:24:31 +00001846unsigned DwarfDebug::RecordRegionStart(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001847 if (TimePassesIsEnabled)
1848 DebugTimer->startTimer();
1849
Devang Patele4b27562009-08-28 23:24:31 +00001850 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001851 unsigned ID = MMI->NextLabelID();
1852 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1853 LexicalScopeStack.push_back(Scope);
1854
1855 if (TimePassesIsEnabled)
1856 DebugTimer->stopTimer();
1857
1858 return ID;
1859}
1860
1861/// RecordRegionEnd - Indicate the end of a region.
Devang Patele4b27562009-08-28 23:24:31 +00001862unsigned DwarfDebug::RecordRegionEnd(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001863 if (TimePassesIsEnabled)
1864 DebugTimer->startTimer();
1865
Devang Patele4b27562009-08-28 23:24:31 +00001866 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001867 unsigned ID = MMI->NextLabelID();
1868 Scope->setEndLabelID(ID);
Devang Pateldaf9e022009-06-13 02:16:18 +00001869 // FIXME : region.end() may not be in the last basic block.
1870 // For now, do not pop last lexical scope because next basic
1871 // block may start new inlined function's body.
1872 unsigned LSSize = LexicalScopeStack.size();
1873 if (LSSize != 0 && LSSize != 1)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001874 LexicalScopeStack.pop_back();
1875
1876 if (TimePassesIsEnabled)
1877 DebugTimer->stopTimer();
1878
1879 return ID;
1880}
1881
1882/// RecordVariable - Indicate the declaration of a local variable.
Devang Patele4b27562009-08-28 23:24:31 +00001883void DwarfDebug::RecordVariable(MDNode *N, unsigned FrameIndex) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001884 if (TimePassesIsEnabled)
1885 DebugTimer->startTimer();
1886
Devang Patele4b27562009-08-28 23:24:31 +00001887 DIDescriptor Desc(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001888 DbgScope *Scope = NULL;
1889 bool InlinedFnVar = false;
1890
Devang Patele4b27562009-08-28 23:24:31 +00001891 if (Desc.getTag() == dwarf::DW_TAG_variable)
1892 Scope = getOrCreateScope(DIGlobalVariable(N).getContext().getNode());
1893 else {
Devang Patel24f20e02009-08-22 17:12:53 +00001894 bool InlinedVar = false;
Devang Patele4b27562009-08-28 23:24:31 +00001895 MDNode *Context = DIVariable(N).getContext().getNode();
1896 DISubprogram SP(Context);
Devang Patel24f20e02009-08-22 17:12:53 +00001897 if (!SP.isNull()) {
1898 // SP is inserted into DbgAbstractScopeMap when inlined function
1899 // start was recorded by RecordInlineFnStart.
Devang Patele4b27562009-08-28 23:24:31 +00001900 DenseMap<MDNode *, DbgScope *>::iterator
1901 I = DbgAbstractScopeMap.find(SP.getNode());
Devang Patel24f20e02009-08-22 17:12:53 +00001902 if (I != DbgAbstractScopeMap.end()) {
1903 InlinedVar = true;
1904 Scope = I->second;
1905 }
1906 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001907 if (!InlinedVar)
Devang Patele4b27562009-08-28 23:24:31 +00001908 Scope = getOrCreateScope(Context);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001909 }
1910
1911 assert(Scope && "Unable to find the variable's scope");
Devang Patele4b27562009-08-28 23:24:31 +00001912 DbgVariable *DV = new DbgVariable(DIVariable(N), FrameIndex, InlinedFnVar);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001913 Scope->AddVariable(DV);
1914
1915 if (TimePassesIsEnabled)
1916 DebugTimer->stopTimer();
1917}
1918
1919//// RecordInlinedFnStart - Indicate the start of inlined subroutine.
1920unsigned DwarfDebug::RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
1921 unsigned Line, unsigned Col) {
1922 unsigned LabelID = MMI->NextLabelID();
1923
Chris Lattner33adcfb2009-08-22 21:43:10 +00001924 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001925 return LabelID;
1926
1927 if (TimePassesIsEnabled)
1928 DebugTimer->startTimer();
1929
Devang Patele4b27562009-08-28 23:24:31 +00001930 MDNode *Node = SP.getNode();
1931 DenseMap<const MDNode *, DbgScope *>::iterator
1932 II = AbstractInstanceRootMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001933
1934 if (II == AbstractInstanceRootMap.end()) {
1935 // Create an abstract instance entry for this inlined function if it doesn't
1936 // already exist.
Devang Patele4b27562009-08-28 23:24:31 +00001937 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001938
1939 // Get the compile unit context.
Devang Patele4b27562009-08-28 23:24:31 +00001940 DIE *SPDie = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001941 if (!SPDie)
Devang Patel1dbc7712009-06-29 20:45:18 +00001942 SPDie = CreateSubprogramDIE(ModuleCU, SP, false, true);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001943
1944 // Mark as being inlined. This makes this subprogram entry an abstract
1945 // instance root.
1946 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
1947 // that it's defined. That probably won't change in the future. However,
1948 // this could be more elegant.
1949 AddUInt(SPDie, dwarf::DW_AT_inline, 0, dwarf::DW_INL_declared_not_inlined);
1950
1951 // Keep track of the abstract scope for this function.
Devang Patele4b27562009-08-28 23:24:31 +00001952 DbgAbstractScopeMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001953
Devang Patele4b27562009-08-28 23:24:31 +00001954 AbstractInstanceRootMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001955 AbstractInstanceRootList.push_back(Scope);
1956 }
1957
1958 // Create a concrete inlined instance for this inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001959 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001960 DIE *ScopeDie = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel1dbc7712009-06-29 20:45:18 +00001961 ScopeDie->setAbstractCompileUnit(ModuleCU);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001962
Devang Patele4b27562009-08-28 23:24:31 +00001963 DIE *Origin = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001964 AddDIEEntry(ScopeDie, dwarf::DW_AT_abstract_origin,
1965 dwarf::DW_FORM_ref4, Origin);
Devang Patel1dbc7712009-06-29 20:45:18 +00001966 AddUInt(ScopeDie, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001967 AddUInt(ScopeDie, dwarf::DW_AT_call_line, 0, Line);
1968 AddUInt(ScopeDie, dwarf::DW_AT_call_column, 0, Col);
1969
1970 ConcreteScope->setDie(ScopeDie);
1971 ConcreteScope->setStartLabelID(LabelID);
1972 MMI->RecordUsedDbgLabel(LabelID);
1973
1974 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
1975
1976 // Keep track of the concrete scope that's inlined into this function.
Devang Patele4b27562009-08-28 23:24:31 +00001977 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
1978 SI = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001979
1980 if (SI == DbgConcreteScopeMap.end())
Devang Patele4b27562009-08-28 23:24:31 +00001981 DbgConcreteScopeMap[Node].push_back(ConcreteScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001982 else
1983 SI->second.push_back(ConcreteScope);
1984
1985 // Track the start label for this inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001986 DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
1987 I = InlineInfo.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001988
1989 if (I == InlineInfo.end())
Devang Patele4b27562009-08-28 23:24:31 +00001990 InlineInfo[Node].push_back(LabelID);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001991 else
1992 I->second.push_back(LabelID);
1993
1994 if (TimePassesIsEnabled)
1995 DebugTimer->stopTimer();
1996
1997 return LabelID;
1998}
1999
2000/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
2001unsigned DwarfDebug::RecordInlinedFnEnd(DISubprogram &SP) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002002 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002003 return 0;
2004
2005 if (TimePassesIsEnabled)
2006 DebugTimer->startTimer();
2007
Devang Patele4b27562009-08-28 23:24:31 +00002008 MDNode *Node = SP.getNode();
2009 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
2010 I = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002011
2012 if (I == DbgConcreteScopeMap.end()) {
2013 // FIXME: Can this situation actually happen? And if so, should it?
2014 if (TimePassesIsEnabled)
2015 DebugTimer->stopTimer();
2016
2017 return 0;
2018 }
2019
2020 SmallVector<DbgScope *, 8> &Scopes = I->second;
Devang Patel11a407f2009-06-15 21:45:50 +00002021 if (Scopes.empty()) {
2022 // Returned ID is 0 if this is unbalanced "end of inlined
2023 // scope". This could happen if optimizer eats dbg intrinsics
2024 // or "beginning of inlined scope" is not recoginized due to
2025 // missing location info. In such cases, ignore this region.end.
2026 return 0;
2027 }
2028
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002029 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
2030 unsigned ID = MMI->NextLabelID();
2031 MMI->RecordUsedDbgLabel(ID);
2032 Scope->setEndLabelID(ID);
2033
2034 if (TimePassesIsEnabled)
2035 DebugTimer->stopTimer();
2036
2037 return ID;
2038}
2039
Bill Wendling829e67b2009-05-20 23:22:40 +00002040//===----------------------------------------------------------------------===//
2041// Emit Methods
2042//===----------------------------------------------------------------------===//
2043
Bill Wendling94d04b82009-05-20 23:21:38 +00002044/// SizeAndOffsetDie - Compute the size and offset of a DIE.
2045///
2046unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2047 // Get the children.
2048 const std::vector<DIE *> &Children = Die->getChildren();
2049
2050 // If not last sibling and has children then add sibling offset attribute.
2051 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2052
2053 // Record the abbreviation.
2054 AssignAbbrevNumber(Die->getAbbrev());
2055
2056 // Get the abbreviation for this DIE.
2057 unsigned AbbrevNumber = Die->getAbbrevNumber();
2058 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2059
2060 // Set DIE offset
2061 Die->setOffset(Offset);
2062
2063 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002064 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002065
2066 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2067 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2068
2069 // Size the DIE attribute values.
2070 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2071 // Size attribute value.
2072 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2073
2074 // Size the DIE children if any.
2075 if (!Children.empty()) {
2076 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2077 "Children flag not set");
2078
2079 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2080 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2081
2082 // End of children marker.
2083 Offset += sizeof(int8_t);
2084 }
2085
2086 Die->setSize(Offset - Die->getOffset());
2087 return Offset;
2088}
2089
2090/// SizeAndOffsets - Compute the size and offset of all the DIEs.
2091///
2092void DwarfDebug::SizeAndOffsets() {
2093 // Compute size of compile unit header.
2094 static unsigned Offset =
2095 sizeof(int32_t) + // Length of Compilation Unit Info
2096 sizeof(int16_t) + // DWARF version number
2097 sizeof(int32_t) + // Offset Into Abbrev. Section
2098 sizeof(int8_t); // Pointer Size (in bytes)
2099
Devang Patel1dbc7712009-06-29 20:45:18 +00002100 SizeAndOffsetDie(ModuleCU->getDie(), Offset, true);
2101 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002102}
2103
2104/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2105/// tools to recognize the object file contains Dwarf information.
2106void DwarfDebug::EmitInitial() {
2107 // Check to see if we already emitted intial headers.
2108 if (didInitial) return;
2109 didInitial = true;
2110
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002111 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002112
Bill Wendling94d04b82009-05-20 23:21:38 +00002113 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002114 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002115 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002116 EmitLabel("section_debug_frame", 0);
2117 }
2118
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002119 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002120 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002121 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002122 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002123 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002124 EmitLabel("section_aranges", 0);
2125
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002126 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2127 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002128 EmitLabel("section_macinfo", 0);
2129 }
2130
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002131 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002132 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002133 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002134 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002135 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002136 EmitLabel("section_pubnames", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002137 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002138 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002139 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002140 EmitLabel("section_ranges", 0);
2141
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002142 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002143 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002144 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002145 EmitLabel("data_begin", 0);
2146}
2147
2148/// EmitDIE - Recusively Emits a debug information entry.
2149///
2150void DwarfDebug::EmitDIE(DIE *Die) {
2151 // Get the abbreviation for this DIE.
2152 unsigned AbbrevNumber = Die->getAbbrevNumber();
2153 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2154
2155 Asm->EOL();
2156
2157 // Emit the code (index) for the abbreviation.
2158 Asm->EmitULEB128Bytes(AbbrevNumber);
2159
2160 if (Asm->isVerbose())
2161 Asm->EOL(std::string("Abbrev [" +
2162 utostr(AbbrevNumber) +
2163 "] 0x" + utohexstr(Die->getOffset()) +
2164 ":0x" + utohexstr(Die->getSize()) + " " +
2165 dwarf::TagString(Abbrev->getTag())));
2166 else
2167 Asm->EOL();
2168
2169 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2170 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2171
2172 // Emit the DIE attribute values.
2173 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2174 unsigned Attr = AbbrevData[i].getAttribute();
2175 unsigned Form = AbbrevData[i].getForm();
2176 assert(Form && "Too many attributes for DIE (check abbreviation)");
2177
2178 switch (Attr) {
2179 case dwarf::DW_AT_sibling:
2180 Asm->EmitInt32(Die->SiblingOffset());
2181 break;
2182 case dwarf::DW_AT_abstract_origin: {
2183 DIEEntry *E = cast<DIEEntry>(Values[i]);
2184 DIE *Origin = E->getEntry();
2185 unsigned Addr =
2186 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
2187 Origin->getOffset();
2188
2189 Asm->EmitInt32(Addr);
2190 break;
2191 }
2192 default:
2193 // Emit an attribute using the defined form.
2194 Values[i]->EmitValue(this, Form);
2195 break;
2196 }
2197
2198 Asm->EOL(dwarf::AttributeString(Attr));
2199 }
2200
2201 // Emit the DIE children if any.
2202 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2203 const std::vector<DIE *> &Children = Die->getChildren();
2204
2205 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2206 EmitDIE(Children[j]);
2207
2208 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2209 }
2210}
2211
2212/// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
2213///
2214void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) {
2215 DIE *Die = Unit->getDie();
2216
2217 // Emit the compile units header.
2218 EmitLabel("info_begin", Unit->getID());
2219
2220 // Emit size of content not including length itself
2221 unsigned ContentSize = Die->getSize() +
2222 sizeof(int16_t) + // DWARF version number
2223 sizeof(int32_t) + // Offset Into Abbrev. Section
2224 sizeof(int8_t) + // Pointer Size (in bytes)
2225 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2226
2227 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2228 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2229 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2230 Asm->EOL("Offset Into Abbrev. Section");
2231 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2232
2233 EmitDIE(Die);
2234 // FIXME - extra padding for gdb bug.
2235 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2236 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2237 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2238 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2239 EmitLabel("info_end", Unit->getID());
2240
2241 Asm->EOL();
2242}
2243
2244void DwarfDebug::EmitDebugInfo() {
2245 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002246 Asm->OutStreamer.SwitchSection(
2247 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002248
Devang Patel1dbc7712009-06-29 20:45:18 +00002249 EmitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002250}
2251
2252/// EmitAbbreviations - Emit the abbreviation section.
2253///
2254void DwarfDebug::EmitAbbreviations() const {
2255 // Check to see if it is worth the effort.
2256 if (!Abbreviations.empty()) {
2257 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002258 Asm->OutStreamer.SwitchSection(
2259 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002260
2261 EmitLabel("abbrev_begin", 0);
2262
2263 // For each abbrevation.
2264 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2265 // Get abbreviation data
2266 const DIEAbbrev *Abbrev = Abbreviations[i];
2267
2268 // Emit the abbrevations code (base 1 index.)
2269 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2270 Asm->EOL("Abbreviation Code");
2271
2272 // Emit the abbreviations data.
2273 Abbrev->Emit(Asm);
2274
2275 Asm->EOL();
2276 }
2277
2278 // Mark end of abbreviations.
2279 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2280
2281 EmitLabel("abbrev_end", 0);
2282 Asm->EOL();
2283 }
2284}
2285
2286/// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2287/// the line matrix.
2288///
2289void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) {
2290 // Define last address of section.
2291 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2292 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2293 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2294 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2295
2296 // Mark end of matrix.
2297 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2298 Asm->EmitULEB128Bytes(1); Asm->EOL();
2299 Asm->EmitInt8(1); Asm->EOL();
2300}
2301
2302/// EmitDebugLines - Emit source line information.
2303///
2304void DwarfDebug::EmitDebugLines() {
2305 // If the target is using .loc/.file, the assembler will be emitting the
2306 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002307 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002308 return;
2309
2310 // Minimum line delta, thus ranging from -10..(255-10).
2311 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2312 // Maximum line delta, thus ranging from -10..(255-10).
2313 const int MaxLineDelta = 255 + MinLineDelta;
2314
2315 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002316 Asm->OutStreamer.SwitchSection(
2317 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002318
2319 // Construct the section header.
2320 EmitDifference("line_end", 0, "line_begin", 0, true);
2321 Asm->EOL("Length of Source Line Info");
2322 EmitLabel("line_begin", 0);
2323
2324 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2325
2326 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2327 Asm->EOL("Prolog Length");
2328 EmitLabel("line_prolog_begin", 0);
2329
2330 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2331
2332 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2333
2334 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2335
2336 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2337
2338 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2339
2340 // Line number standard opcode encodings argument count
2341 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2342 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2343 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2344 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2345 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2346 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2347 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2348 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2349 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2350
2351 // Emit directories.
2352 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2353 Asm->EmitString(getSourceDirectoryName(DI));
2354 Asm->EOL("Directory");
2355 }
2356
2357 Asm->EmitInt8(0); Asm->EOL("End of directories");
2358
2359 // Emit files.
2360 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2361 // Remember source id starts at 1.
2362 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2363 Asm->EmitString(getSourceFileName(Id.second));
2364 Asm->EOL("Source");
2365 Asm->EmitULEB128Bytes(Id.first);
2366 Asm->EOL("Directory #");
2367 Asm->EmitULEB128Bytes(0);
2368 Asm->EOL("Mod date");
2369 Asm->EmitULEB128Bytes(0);
2370 Asm->EOL("File size");
2371 }
2372
2373 Asm->EmitInt8(0); Asm->EOL("End of files");
2374
2375 EmitLabel("line_prolog_end", 0);
2376
2377 // A sequence for each text section.
2378 unsigned SecSrcLinesSize = SectionSourceLines.size();
2379
2380 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2381 // Isolate current sections line info.
2382 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2383
Chris Lattner93b6db32009-08-08 23:39:42 +00002384 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002385 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002386 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002387 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002388 }*/
2389 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002390
2391 // Dwarf assumes we start with first line of first source file.
2392 unsigned Source = 1;
2393 unsigned Line = 1;
2394
2395 // Construct rows of the address, source, line, column matrix.
2396 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2397 const SrcLineInfo &LineInfo = LineInfos[i];
2398 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2399 if (!LabelID) continue;
2400
Caroline Ticec6f9d622009-09-11 18:25:54 +00002401 if (LineInfo.getLine() == 0) continue;
2402
Bill Wendling94d04b82009-05-20 23:21:38 +00002403 if (!Asm->isVerbose())
2404 Asm->EOL();
2405 else {
2406 std::pair<unsigned, unsigned> SourceID =
2407 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002408 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002409 << getSourceDirectoryName(SourceID.first) << ' '
2410 << getSourceFileName(SourceID.second)
2411 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2412 }
2413
2414 // Define the line address.
2415 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2416 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2417 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2418 EmitReference("label", LabelID); Asm->EOL("Location label");
2419
2420 // If change of source, then switch to the new source.
2421 if (Source != LineInfo.getSourceID()) {
2422 Source = LineInfo.getSourceID();
2423 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2424 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2425 }
2426
2427 // If change of line.
2428 if (Line != LineInfo.getLine()) {
2429 // Determine offset.
2430 int Offset = LineInfo.getLine() - Line;
2431 int Delta = Offset - MinLineDelta;
2432
2433 // Update line.
2434 Line = LineInfo.getLine();
2435
2436 // If delta is small enough and in range...
2437 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2438 // ... then use fast opcode.
2439 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2440 } else {
2441 // ... otherwise use long hand.
2442 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2443 Asm->EOL("DW_LNS_advance_line");
2444 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2445 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2446 }
2447 } else {
2448 // Copy the previous row (different address or source)
2449 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2450 }
2451 }
2452
2453 EmitEndOfLineMatrix(j + 1);
2454 }
2455
2456 if (SecSrcLinesSize == 0)
2457 // Because we're emitting a debug_line section, we still need a line
2458 // table. The linker and friends expect it to exist. If there's nothing to
2459 // put into it, emit an empty table.
2460 EmitEndOfLineMatrix(1);
2461
2462 EmitLabel("line_end", 0);
2463 Asm->EOL();
2464}
2465
2466/// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2467///
2468void DwarfDebug::EmitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002469 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002470 return;
2471
2472 int stackGrowth =
2473 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2474 TargetFrameInfo::StackGrowsUp ?
2475 TD->getPointerSize() : -TD->getPointerSize();
2476
2477 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002478 Asm->OutStreamer.SwitchSection(
2479 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002480
2481 EmitLabel("debug_frame_common", 0);
2482 EmitDifference("debug_frame_common_end", 0,
2483 "debug_frame_common_begin", 0, true);
2484 Asm->EOL("Length of Common Information Entry");
2485
2486 EmitLabel("debug_frame_common_begin", 0);
2487 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2488 Asm->EOL("CIE Identifier Tag");
2489 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2490 Asm->EOL("CIE Version");
2491 Asm->EmitString("");
2492 Asm->EOL("CIE Augmentation");
2493 Asm->EmitULEB128Bytes(1);
2494 Asm->EOL("CIE Code Alignment Factor");
2495 Asm->EmitSLEB128Bytes(stackGrowth);
2496 Asm->EOL("CIE Data Alignment Factor");
2497 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2498 Asm->EOL("CIE RA Column");
2499
2500 std::vector<MachineMove> Moves;
2501 RI->getInitialFrameState(Moves);
2502
2503 EmitFrameMoves(NULL, 0, Moves, false);
2504
2505 Asm->EmitAlignment(2, 0, 0, false);
2506 EmitLabel("debug_frame_common_end", 0);
2507
2508 Asm->EOL();
2509}
2510
2511/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2512/// section.
2513void
2514DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002515 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002516 return;
2517
2518 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002519 Asm->OutStreamer.SwitchSection(
2520 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002521
2522 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2523 "debug_frame_begin", DebugFrameInfo.Number, true);
2524 Asm->EOL("Length of Frame Information Entry");
2525
2526 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2527
2528 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2529 0, 0, true, false);
2530 Asm->EOL("FDE CIE offset");
2531
2532 EmitReference("func_begin", DebugFrameInfo.Number);
2533 Asm->EOL("FDE initial location");
2534 EmitDifference("func_end", DebugFrameInfo.Number,
2535 "func_begin", DebugFrameInfo.Number);
2536 Asm->EOL("FDE address range");
2537
2538 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2539 false);
2540
2541 Asm->EmitAlignment(2, 0, 0, false);
2542 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2543
2544 Asm->EOL();
2545}
2546
2547void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2548 EmitDifference("pubnames_end", Unit->getID(),
2549 "pubnames_begin", Unit->getID(), true);
2550 Asm->EOL("Length of Public Names Info");
2551
2552 EmitLabel("pubnames_begin", Unit->getID());
2553
2554 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2555
2556 EmitSectionOffset("info_begin", "section_info",
2557 Unit->getID(), 0, true, false);
2558 Asm->EOL("Offset of Compilation Unit Info");
2559
2560 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2561 true);
2562 Asm->EOL("Compilation Unit Length");
2563
2564 StringMap<DIE*> &Globals = Unit->getGlobals();
2565 for (StringMap<DIE*>::const_iterator
2566 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2567 const char *Name = GI->getKeyData();
2568 DIE * Entity = GI->second;
2569
2570 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2571 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2572 }
2573
2574 Asm->EmitInt32(0); Asm->EOL("End Mark");
2575 EmitLabel("pubnames_end", Unit->getID());
2576
2577 Asm->EOL();
2578}
2579
2580/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2581///
2582void DwarfDebug::EmitDebugPubNames() {
2583 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002584 Asm->OutStreamer.SwitchSection(
2585 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002586
Devang Patel1dbc7712009-06-29 20:45:18 +00002587 EmitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002588}
2589
2590/// EmitDebugStr - Emit visible names into a debug str section.
2591///
2592void DwarfDebug::EmitDebugStr() {
2593 // Check to see if it is worth the effort.
2594 if (!StringPool.empty()) {
2595 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002596 Asm->OutStreamer.SwitchSection(
2597 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002598
2599 // For each of strings in the string pool.
2600 for (unsigned StringID = 1, N = StringPool.size();
2601 StringID <= N; ++StringID) {
2602 // Emit a label for reference from debug information entries.
2603 EmitLabel("string", StringID);
2604
2605 // Emit the string itself.
2606 const std::string &String = StringPool[StringID];
2607 Asm->EmitString(String); Asm->EOL();
2608 }
2609
2610 Asm->EOL();
2611 }
2612}
2613
2614/// EmitDebugLoc - Emit visible names into a debug loc section.
2615///
2616void DwarfDebug::EmitDebugLoc() {
2617 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002618 Asm->OutStreamer.SwitchSection(
2619 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002620 Asm->EOL();
2621}
2622
2623/// EmitDebugARanges - Emit visible names into a debug aranges section.
2624///
2625void DwarfDebug::EmitDebugARanges() {
2626 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002627 Asm->OutStreamer.SwitchSection(
2628 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002629
2630 // FIXME - Mock up
2631#if 0
2632 CompileUnit *Unit = GetBaseCompileUnit();
2633
2634 // Don't include size of length
2635 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2636
2637 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2638
2639 EmitReference("info_begin", Unit->getID());
2640 Asm->EOL("Offset of Compilation Unit Info");
2641
2642 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2643
2644 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2645
2646 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2647 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2648
2649 // Range 1
2650 EmitReference("text_begin", 0); Asm->EOL("Address");
2651 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2652
2653 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2654 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2655#endif
2656
2657 Asm->EOL();
2658}
2659
2660/// EmitDebugRanges - Emit visible names into a debug ranges section.
2661///
2662void DwarfDebug::EmitDebugRanges() {
2663 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002664 Asm->OutStreamer.SwitchSection(
2665 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002666 Asm->EOL();
2667}
2668
2669/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2670///
2671void DwarfDebug::EmitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002672 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002673 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002674 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002675 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002676 Asm->EOL();
2677 }
2678}
2679
2680/// EmitDebugInlineInfo - Emit inline info using following format.
2681/// Section Header:
2682/// 1. length of section
2683/// 2. Dwarf version number
2684/// 3. address size.
2685///
2686/// Entries (one "entry" for each function that was inlined):
2687///
2688/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2689/// otherwise offset into __debug_str for regular function name.
2690/// 2. offset into __debug_str section for regular function name.
2691/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2692/// instances for the function.
2693///
2694/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2695/// inlined instance; the die_offset points to the inlined_subroutine die in the
2696/// __debug_info section, and the low_pc is the starting address for the
2697/// inlining instance.
2698void DwarfDebug::EmitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002699 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002700 return;
2701
Devang Patel1dbc7712009-06-29 20:45:18 +00002702 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002703 return;
2704
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002705 Asm->OutStreamer.SwitchSection(
2706 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002707 Asm->EOL();
2708 EmitDifference("debug_inlined_end", 1,
2709 "debug_inlined_begin", 1, true);
2710 Asm->EOL("Length of Debug Inlined Information Entry");
2711
2712 EmitLabel("debug_inlined_begin", 1);
2713
2714 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2715 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2716
Devang Patele4b27562009-08-28 23:24:31 +00002717 for (DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
Bill Wendling94d04b82009-05-20 23:21:38 +00002718 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
Devang Patele4b27562009-08-28 23:24:31 +00002719 MDNode *Node = I->first;
Bill Wendling94d04b82009-05-20 23:21:38 +00002720 SmallVector<unsigned, 4> &Labels = I->second;
Devang Patele4b27562009-08-28 23:24:31 +00002721 DISubprogram SP(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00002722 std::string Name;
2723 std::string LName;
2724
2725 SP.getLinkageName(LName);
2726 SP.getName(Name);
2727
Devang Patel53cb17d2009-07-16 01:01:22 +00002728 if (LName.empty())
2729 Asm->EmitString(Name);
2730 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002731 // Skip special LLVM prefix that is used to inform the asm printer to not
2732 // emit usual symbol prefix before the symbol name. This happens for
2733 // Objective-C symbol names and symbol whose name is replaced using GCC's
2734 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002735 if (LName[0] == 1)
2736 LName = &LName[1];
2737 Asm->EmitString(LName);
2738 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002739 Asm->EOL("MIPS linkage name");
2740
2741 Asm->EmitString(Name); Asm->EOL("Function name");
2742
2743 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2744
2745 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2746 LE = Labels.end(); LI != LE; ++LI) {
Devang Patele4b27562009-08-28 23:24:31 +00002747 DIE *SP = ModuleCU->getDieMapSlotFor(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00002748 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2749
2750 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002751 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002752 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002753 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002754
2755 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2756 }
2757 }
2758
2759 EmitLabel("debug_inlined_end", 1);
2760 Asm->EOL();
2761}