blob: 06ee6272a24ab1b672b4ed0bbc9d724468ea7153 [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,
Daniel Dunbar00564992009-09-19 20:40:14 +0000643 unsigned Attribute,
644 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000645 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
Daniel Dunbar00564992009-09-19 20:40:14 +0000665 // Find the __forwarding field and the variable field in the __Block_byref
666 // struct.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000667
Daniel Dunbar00564992009-09-19 20:40:14 +0000668 DIArray Fields = blockStruct.getTypeArray();
669 DIDescriptor varField = DIDescriptor();
670 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000671
672
Daniel Dunbar00564992009-09-19 20:40:14 +0000673 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
674 DIDescriptor Element = Fields.getElement(i);
675 DIDerivedType DT = DIDerivedType(Element.getNode());
676 std::string fieldName;
677 DT.getName(fieldName);
678 if (fieldName == "__forwarding")
679 forwardingField = Element;
680 else if (fieldName == varName)
681 varField = Element;
682 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000683
Daniel Dunbar00564992009-09-19 20:40:14 +0000684 assert (!varField.isNull() && "Can't find byref variable in Block struct");
685 assert (!forwardingField.isNull()
686 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000687
Daniel Dunbar00564992009-09-19 20:40:14 +0000688 // Get the offsets for the forwarding field and the variable field.
Caroline Ticedc8f6042009-08-31 21:19:37 +0000689
Daniel Dunbar00564992009-09-19 20:40:14 +0000690 unsigned int forwardingFieldOffset =
691 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
692 unsigned int varFieldOffset =
693 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000694
Daniel Dunbar00564992009-09-19 20:40:14 +0000695 // Decode the original location, and use that as the start of the
696 // byref variable's location.
Caroline Ticedc8f6042009-08-31 21:19:37 +0000697
Daniel Dunbar00564992009-09-19 20:40:14 +0000698 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
699 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000700
Daniel Dunbar00564992009-09-19 20:40:14 +0000701 if (Location.isReg()) {
702 if (Reg < 32)
703 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
704 else {
705 Reg = Reg - dwarf::DW_OP_reg0;
706 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
707 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
708 }
709 } else {
710 if (Reg < 32)
711 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
712 else {
713 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
714 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
715 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000716
Daniel Dunbar00564992009-09-19 20:40:14 +0000717 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
718 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000719
Daniel Dunbar00564992009-09-19 20:40:14 +0000720 // If we started with a pointer to the__Block_byref... struct, then
721 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Caroline Ticedc8f6042009-08-31 21:19:37 +0000722
Daniel Dunbar00564992009-09-19 20:40:14 +0000723 if (isPointer)
724 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000725
Daniel Dunbar00564992009-09-19 20:40:14 +0000726 // Next add the offset for the '__forwarding' field:
727 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
728 // adding the offset if it's 0.
Caroline Ticedc8f6042009-08-31 21:19:37 +0000729
Daniel Dunbar00564992009-09-19 20:40:14 +0000730 if (forwardingFieldOffset > 0) {
731 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
732 AddUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
733 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000734
Daniel Dunbar00564992009-09-19 20:40:14 +0000735 // Now dereference the __forwarding field to get to the real __Block_byref
736 // struct: DW_OP_deref.
Caroline Ticedc8f6042009-08-31 21:19:37 +0000737
Daniel Dunbar00564992009-09-19 20:40:14 +0000738 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000739
Daniel Dunbar00564992009-09-19 20:40:14 +0000740 // Now that we've got the real __Block_byref... struct, add the offset
741 // for the variable's field to get to the location of the actual variable:
742 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Caroline Ticedc8f6042009-08-31 21:19:37 +0000743
Daniel Dunbar00564992009-09-19 20:40:14 +0000744 if (varFieldOffset > 0) {
745 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
746 AddUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
747 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000748
Daniel Dunbar00564992009-09-19 20:40:14 +0000749 // Now attach the location information to the DIE.
Caroline Ticedc8f6042009-08-31 21:19:37 +0000750
Daniel Dunbar00564992009-09-19 20:40:14 +0000751 AddBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000752}
753
Bill Wendling0310d762009-05-15 09:23:25 +0000754/// AddAddress - Add an address attribute to a die based on the location
755/// provided.
756void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute,
757 const MachineLocation &Location) {
758 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
759 DIEBlock *Block = new DIEBlock();
760
761 if (Location.isReg()) {
762 if (Reg < 32) {
763 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
764 } else {
765 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
766 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
767 }
768 } else {
769 if (Reg < 32) {
770 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
771 } else {
772 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
773 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
774 }
775
776 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
777 }
778
779 AddBlock(Die, Attribute, 0, Block);
780}
781
782/// AddType - Add a new type attribute to the specified entity.
783void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
784 if (Ty.isNull())
785 return;
786
787 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +0000788 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000789
790 // If it exists then use the existing value.
791 if (Slot) {
792 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
793 return;
794 }
795
796 // Set up proxy.
Bill Wendling995f80a2009-05-20 23:24:48 +0000797 Slot = CreateDIEEntry();
Bill Wendling0310d762009-05-15 09:23:25 +0000798
799 // Construct type.
800 DIE Buffer(dwarf::DW_TAG_base_type);
Devang Patel6ceea332009-08-31 18:49:10 +0000801 if (Ty.isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000802 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getNode()));
Devang Patel6ceea332009-08-31 18:49:10 +0000803 else if (Ty.isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000804 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000805 else {
Devang Patel6ceea332009-08-31 18:49:10 +0000806 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patele4b27562009-08-28 23:24:31 +0000807 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getNode()));
808
Bill Wendling0310d762009-05-15 09:23:25 +0000809 }
810
811 // Add debug information entry to entity and appropriate context.
812 DIE *Die = NULL;
813 DIDescriptor Context = Ty.getContext();
814 if (!Context.isNull())
Devang Patele4b27562009-08-28 23:24:31 +0000815 Die = DW_Unit->getDieMapSlotFor(Context.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000816
817 if (Die) {
818 DIE *Child = new DIE(Buffer);
819 Die->AddChild(Child);
820 Buffer.Detach();
821 SetDIEEntry(Slot, Child);
822 } else {
823 Die = DW_Unit->AddDie(Buffer);
824 SetDIEEntry(Slot, Die);
825 }
826
827 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot);
828}
829
830/// ConstructTypeDIE - Construct basic type die from DIBasicType.
831void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
832 DIBasicType BTy) {
833 // Get core information.
834 std::string Name;
835 BTy.getName(Name);
836 Buffer.setTag(dwarf::DW_TAG_base_type);
837 AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
838 BTy.getEncoding());
839
840 // Add name if not anonymous or intermediate type.
841 if (!Name.empty())
842 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
843 uint64_t Size = BTy.getSizeInBits() >> 3;
844 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
845}
846
847/// ConstructTypeDIE - Construct derived type die from DIDerivedType.
848void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
849 DIDerivedType DTy) {
850 // Get core information.
851 std::string Name;
852 DTy.getName(Name);
853 uint64_t Size = DTy.getSizeInBits() >> 3;
854 unsigned Tag = DTy.getTag();
855
856 // FIXME - Workaround for templates.
857 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
858
859 Buffer.setTag(Tag);
860
861 // Map to main type, void will not have a type.
862 DIType FromTy = DTy.getTypeDerivedFrom();
863 AddType(DW_Unit, &Buffer, FromTy);
864
865 // Add name if not anonymous or intermediate type.
866 if (!Name.empty())
867 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
868
869 // Add size if non-zero (derived types might be zero-sized.)
870 if (Size)
871 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
872
873 // Add source line info if available and TyDesc is not a forward declaration.
874 if (!DTy.isForwardDecl())
875 AddSourceLine(&Buffer, &DTy);
876}
877
878/// ConstructTypeDIE - Construct type DIE from DICompositeType.
879void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
880 DICompositeType CTy) {
881 // Get core information.
882 std::string Name;
883 CTy.getName(Name);
884
885 uint64_t Size = CTy.getSizeInBits() >> 3;
886 unsigned Tag = CTy.getTag();
887 Buffer.setTag(Tag);
888
889 switch (Tag) {
890 case dwarf::DW_TAG_vector_type:
891 case dwarf::DW_TAG_array_type:
892 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
893 break;
894 case dwarf::DW_TAG_enumeration_type: {
895 DIArray Elements = CTy.getTypeArray();
896
897 // Add enumerators to enumeration type.
898 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
899 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000900 DIEnumerator Enum(Elements.getElement(i).getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000901 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
902 Buffer.AddChild(ElemDie);
903 }
904 }
905 break;
906 case dwarf::DW_TAG_subroutine_type: {
907 // Add return type.
908 DIArray Elements = CTy.getTypeArray();
909 DIDescriptor RTy = Elements.getElement(0);
Devang Patele4b27562009-08-28 23:24:31 +0000910 AddType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000911
912 // Add prototype flag.
913 AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
914
915 // Add arguments.
916 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
917 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
918 DIDescriptor Ty = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000919 AddType(DW_Unit, Arg, DIType(Ty.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000920 Buffer.AddChild(Arg);
921 }
922 }
923 break;
924 case dwarf::DW_TAG_structure_type:
925 case dwarf::DW_TAG_union_type:
926 case dwarf::DW_TAG_class_type: {
927 // Add elements to structure type.
928 DIArray Elements = CTy.getTypeArray();
929
930 // A forward struct declared type may not have elements available.
931 if (Elements.isNull())
932 break;
933
934 // Add elements to structure type.
935 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
936 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000937 if (Element.isNull())
938 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000939 DIE *ElemDie = NULL;
940 if (Element.getTag() == dwarf::DW_TAG_subprogram)
941 ElemDie = CreateSubprogramDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000942 DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000943 else
944 ElemDie = CreateMemberDIE(DW_Unit,
Devang Patele4b27562009-08-28 23:24:31 +0000945 DIDerivedType(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000946 Buffer.AddChild(ElemDie);
947 }
948
Devang Patela1ba2692009-08-27 23:51:51 +0000949 if (CTy.isAppleBlockExtension())
Bill Wendling0310d762009-05-15 09:23:25 +0000950 AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
951
952 unsigned RLang = CTy.getRunTimeLang();
953 if (RLang)
954 AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
955 dwarf::DW_FORM_data1, RLang);
956 break;
957 }
958 default:
959 break;
960 }
961
962 // Add name if not anonymous or intermediate type.
963 if (!Name.empty())
964 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
965
966 if (Tag == dwarf::DW_TAG_enumeration_type ||
967 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
968 // Add size if non-zero (derived types might be zero-sized.)
969 if (Size)
970 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
971 else {
972 // Add zero size if it is not a forward declaration.
973 if (CTy.isForwardDecl())
974 AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
975 else
976 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
977 }
978
979 // Add source line info if available.
980 if (!CTy.isForwardDecl())
981 AddSourceLine(&Buffer, &CTy);
982 }
983}
984
985/// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
986void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
987 int64_t L = SR.getLo();
988 int64_t H = SR.getHi();
989 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
990
Devang Patel6325a532009-08-14 20:59:16 +0000991 AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
992 if (L)
993 AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
994 if (H)
Daniel Dunbar00564992009-09-19 20:40:14 +0000995 AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000996
997 Buffer.AddChild(DW_Subrange);
998}
999
1000/// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1001void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1002 DICompositeType *CTy) {
1003 Buffer.setTag(dwarf::DW_TAG_array_type);
1004 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1005 AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
1006
1007 // Emit derived type.
1008 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
1009 DIArray Elements = CTy->getTypeArray();
1010
1011 // Construct an anonymous type for index type.
1012 DIE IdxBuffer(dwarf::DW_TAG_base_type);
1013 AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1014 AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1015 dwarf::DW_ATE_signed);
1016 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1017
1018 // Add subranges to array type.
1019 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1020 DIDescriptor Element = Elements.getElement(i);
1021 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patele4b27562009-08-28 23:24:31 +00001022 ConstructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IndexTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001023 }
1024}
1025
1026/// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1027DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
1028 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1029 std::string Name;
1030 ETy->getName(Name);
1031 AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1032 int64_t Value = ETy->getEnumValue();
1033 AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1034 return Enumerator;
1035}
1036
1037/// CreateGlobalVariableDIE - Create new DIE using GV.
1038DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit,
1039 const DIGlobalVariable &GV) {
1040 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
1041 std::string Name;
1042 GV.getDisplayName(Name);
1043 AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1044 std::string LinkageName;
1045 GV.getLinkageName(LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001046 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001047 // Skip special LLVM prefix that is used to inform the asm printer to not
1048 // emit usual symbol prefix before the symbol name. This happens for
1049 // Objective-C symbol names and symbol whose name is replaced using GCC's
1050 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001051 if (LinkageName[0] == 1)
1052 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001053 AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001054 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001055 }
Daniel Dunbar00564992009-09-19 20:40:14 +00001056 AddType(DW_Unit, GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001057 if (!GV.isLocalToUnit())
1058 AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1059 AddSourceLine(GVDie, &GV);
1060 return GVDie;
1061}
1062
1063/// CreateMemberDIE - Create new member DIE.
1064DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
1065 DIE *MemberDie = new DIE(DT.getTag());
1066 std::string Name;
1067 DT.getName(Name);
1068 if (!Name.empty())
1069 AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1070
1071 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1072
1073 AddSourceLine(MemberDie, &DT);
1074
1075 uint64_t Size = DT.getSizeInBits();
1076 uint64_t FieldSize = DT.getOriginalTypeSize();
1077
1078 if (Size != FieldSize) {
1079 // Handle bitfield.
1080 AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1081 AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1082
1083 uint64_t Offset = DT.getOffsetInBits();
1084 uint64_t FieldOffset = Offset;
1085 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1086 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1087 FieldOffset = (HiMark - FieldSize);
1088 Offset -= FieldOffset;
1089
1090 // Maybe we need to work from the other end.
1091 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1092 AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1093 }
1094
1095 DIEBlock *Block = new DIEBlock();
1096 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1097 AddUInt(Block, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1098 AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, Block);
1099
1100 if (DT.isProtected())
1101 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1102 dwarf::DW_ACCESS_protected);
1103 else if (DT.isPrivate())
1104 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
1105 dwarf::DW_ACCESS_private);
1106
1107 return MemberDie;
1108}
1109
1110/// CreateSubprogramDIE - Create new DIE using SP.
1111DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit,
1112 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +00001113 bool IsConstructor,
1114 bool IsInlined) {
Bill Wendling0310d762009-05-15 09:23:25 +00001115 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1116
1117 std::string Name;
1118 SP.getName(Name);
1119 AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1120
1121 std::string LinkageName;
1122 SP.getLinkageName(LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001123 if (!LinkageName.empty()) {
1124 // Skip special LLVM prefix that is used to inform the asm printer to not emit
1125 // usual symbol prefix before the symbol name. This happens for Objective-C
1126 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
1127 if (LinkageName[0] == 1)
1128 LinkageName = &LinkageName[1];
Bill Wendling0310d762009-05-15 09:23:25 +00001129 AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001130 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001131 }
Bill Wendling0310d762009-05-15 09:23:25 +00001132 AddSourceLine(SPDie, &SP);
1133
1134 DICompositeType SPTy = SP.getType();
1135 DIArray Args = SPTy.getTypeArray();
1136
1137 // Add prototyped tag, if C or ObjC.
1138 unsigned Lang = SP.getCompileUnit().getLanguage();
1139 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1140 Lang == dwarf::DW_LANG_ObjC)
1141 AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
1142
1143 // Add Return Type.
1144 unsigned SPTag = SPTy.getTag();
1145 if (!IsConstructor) {
1146 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1147 AddType(DW_Unit, SPDie, SPTy);
1148 else
Devang Patele4b27562009-08-28 23:24:31 +00001149 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001150 }
1151
1152 if (!SP.isDefinition()) {
1153 AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1154
1155 // Add arguments. Do not add arguments for subprogram definition. They will
1156 // be handled through RecordVariable.
1157 if (SPTag == dwarf::DW_TAG_subroutine_type)
1158 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1159 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patele4b27562009-08-28 23:24:31 +00001160 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +00001161 AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1162 SPDie->AddChild(Arg);
1163 }
1164 }
1165
Bill Wendling6679ee42009-05-18 22:02:36 +00001166 if (!SP.isLocalToUnit() && !IsInlined)
Bill Wendling0310d762009-05-15 09:23:25 +00001167 AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1168
1169 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patele4b27562009-08-28 23:24:31 +00001170 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001171 Slot = SPDie;
1172 return SPDie;
1173}
1174
1175/// FindCompileUnit - Get the compile unit for the given descriptor.
1176///
1177CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const {
1178 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001179 CompileUnitMap.find(Unit.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001180 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1181 return *I->second;
1182}
1183
Bill Wendling995f80a2009-05-20 23:24:48 +00001184/// CreateDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +00001185///
Bill Wendling995f80a2009-05-20 23:24:48 +00001186DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001187 // Get the descriptor.
1188 const DIVariable &VD = DV->getVariable();
1189
1190 // Translate tag to proper Dwarf tag. The result variable is dropped for
1191 // now.
1192 unsigned Tag;
1193 switch (VD.getTag()) {
1194 case dwarf::DW_TAG_return_variable:
1195 return NULL;
1196 case dwarf::DW_TAG_arg_variable:
1197 Tag = dwarf::DW_TAG_formal_parameter;
1198 break;
1199 case dwarf::DW_TAG_auto_variable: // fall thru
1200 default:
1201 Tag = dwarf::DW_TAG_variable;
1202 break;
1203 }
1204
1205 // Define variable debug information entry.
1206 DIE *VariableDie = new DIE(Tag);
1207 std::string Name;
1208 VD.getName(Name);
1209 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1210
1211 // Add source line info if available.
1212 AddSourceLine(VariableDie, &VD);
1213
1214 // Add variable type.
Caroline Ticedc8f6042009-08-31 21:19:37 +00001215 if (VD.isBlockByrefVariable())
1216 AddType(Unit, VariableDie, GetBlockByrefType(VD.getType(), Name));
1217 else
1218 AddType(Unit, VariableDie, VD.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001219
1220 // Add variable address.
Bill Wendling1180c782009-05-18 23:08:55 +00001221 if (!DV->isInlinedFnVar()) {
1222 // Variables for abstract instances of inlined functions don't get a
1223 // location.
1224 MachineLocation Location;
1225 Location.set(RI->getFrameRegister(*MF),
1226 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Caroline Ticedc8f6042009-08-31 21:19:37 +00001227
1228 if (VD.isBlockByrefVariable())
Mike Stumpee4b8a72009-09-24 23:11:08 +00001229 AddBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Caroline Ticedc8f6042009-08-31 21:19:37 +00001230 else
1231 AddAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling1180c782009-05-18 23:08:55 +00001232 }
Bill Wendling0310d762009-05-15 09:23:25 +00001233
1234 return VariableDie;
1235}
1236
1237/// getOrCreateScope - Returns the scope associated with the given descriptor.
1238///
Devang Patele4b27562009-08-28 23:24:31 +00001239DbgScope *DwarfDebug::getOrCreateScope(MDNode *N) {
1240 DbgScope *&Slot = DbgScopeMap[N];
Bill Wendling0310d762009-05-15 09:23:25 +00001241 if (Slot) return Slot;
1242
1243 DbgScope *Parent = NULL;
Devang Patel5e005d82009-08-31 22:00:15 +00001244 DILexicalBlock Block(N);
Bill Wendling0310d762009-05-15 09:23:25 +00001245
Bill Wendling8fff19b2009-06-01 20:18:46 +00001246 // Don't create a new scope if we already created one for an inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001247 DenseMap<const MDNode *, DbgScope *>::iterator
1248 II = AbstractInstanceRootMap.find(N);
Bill Wendling8fff19b2009-06-01 20:18:46 +00001249 if (II != AbstractInstanceRootMap.end())
1250 return LexicalScopeStack.back();
1251
Bill Wendling0310d762009-05-15 09:23:25 +00001252 if (!Block.isNull()) {
1253 DIDescriptor ParentDesc = Block.getContext();
1254 Parent =
Devang Patele4b27562009-08-28 23:24:31 +00001255 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001256 }
1257
Devang Patele4b27562009-08-28 23:24:31 +00001258 Slot = new DbgScope(Parent, DIDescriptor(N));
Bill Wendling0310d762009-05-15 09:23:25 +00001259
1260 if (Parent)
1261 Parent->AddScope(Slot);
1262 else
1263 // First function is top level function.
1264 FunctionDbgScope = Slot;
1265
1266 return Slot;
1267}
1268
1269/// ConstructDbgScope - Construct the components of a scope.
1270///
1271void DwarfDebug::ConstructDbgScope(DbgScope *ParentScope,
1272 unsigned ParentStartID,
1273 unsigned ParentEndID,
1274 DIE *ParentDie, CompileUnit *Unit) {
1275 // Add variables to scope.
1276 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
1277 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Bill Wendling995f80a2009-05-20 23:24:48 +00001278 DIE *VariableDie = CreateDbgScopeVariable(Variables[i], Unit);
Bill Wendling0310d762009-05-15 09:23:25 +00001279 if (VariableDie) ParentDie->AddChild(VariableDie);
1280 }
1281
1282 // Add concrete instances to scope.
1283 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts =
1284 ParentScope->getConcreteInsts();
1285 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
1286 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
1287 DIE *Die = ConcreteInst->getDie();
1288
1289 unsigned StartID = ConcreteInst->getStartLabelID();
1290 unsigned EndID = ConcreteInst->getEndLabelID();
1291
1292 // Add the scope bounds.
1293 if (StartID)
1294 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1295 DWLabel("label", StartID));
1296 else
1297 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1298 DWLabel("func_begin", SubprogramCount));
1299
1300 if (EndID)
1301 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1302 DWLabel("label", EndID));
1303 else
1304 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1305 DWLabel("func_end", SubprogramCount));
1306
1307 ParentDie->AddChild(Die);
1308 }
1309
1310 // Add nested scopes.
1311 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
1312 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1313 // Define the Scope debug information entry.
1314 DbgScope *Scope = Scopes[j];
1315
1316 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1317 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1318
1319 // Ignore empty scopes.
1320 if (StartID == EndID && StartID != 0) continue;
1321
1322 // Do not ignore inlined scopes even if they don't have any variables or
1323 // scopes.
1324 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
1325 Scope->getConcreteInsts().empty())
1326 continue;
1327
1328 if (StartID == ParentStartID && EndID == ParentEndID) {
1329 // Just add stuff to the parent scope.
1330 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1331 } else {
1332 DIE *ScopeDie = new DIE(dwarf::DW_TAG_lexical_block);
1333
1334 // Add the scope bounds.
1335 if (StartID)
1336 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1337 DWLabel("label", StartID));
1338 else
1339 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1340 DWLabel("func_begin", SubprogramCount));
1341
1342 if (EndID)
1343 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1344 DWLabel("label", EndID));
1345 else
1346 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1347 DWLabel("func_end", SubprogramCount));
1348
1349 // Add the scope's contents.
1350 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1351 ParentDie->AddChild(ScopeDie);
1352 }
1353 }
1354}
1355
1356/// ConstructFunctionDbgScope - Construct the scope for the subprogram.
1357///
Bill Wendling17956162009-05-20 23:28:48 +00001358void DwarfDebug::ConstructFunctionDbgScope(DbgScope *RootScope,
1359 bool AbstractScope) {
Bill Wendling0310d762009-05-15 09:23:25 +00001360 // Exit if there is no root scope.
1361 if (!RootScope) return;
1362 DIDescriptor Desc = RootScope->getDesc();
1363 if (Desc.isNull())
1364 return;
1365
1366 // Get the subprogram debug information entry.
Devang Patele4b27562009-08-28 23:24:31 +00001367 DISubprogram SPD(Desc.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001368
Bill Wendling0310d762009-05-15 09:23:25 +00001369 // Get the subprogram die.
Devang Patele4b27562009-08-28 23:24:31 +00001370 DIE *SPDie = ModuleCU->getDieMapSlotFor(SPD.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +00001371 assert(SPDie && "Missing subprogram descriptor");
1372
Bill Wendling17956162009-05-20 23:28:48 +00001373 if (!AbstractScope) {
1374 // Add the function bounds.
1375 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1376 DWLabel("func_begin", SubprogramCount));
1377 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1378 DWLabel("func_end", SubprogramCount));
1379 MachineLocation Location(RI->getFrameRegister(*MF));
1380 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
1381 }
Bill Wendling0310d762009-05-15 09:23:25 +00001382
Devang Patel1dbc7712009-06-29 20:45:18 +00001383 ConstructDbgScope(RootScope, 0, 0, SPDie, ModuleCU);
Bill Wendling0310d762009-05-15 09:23:25 +00001384}
1385
Bill Wendling0310d762009-05-15 09:23:25 +00001386/// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1387///
1388void DwarfDebug::ConstructDefaultDbgScope(MachineFunction *MF) {
Devang Patel1dbc7712009-06-29 20:45:18 +00001389 StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Daniel Dunbar460f6562009-07-26 09:48:23 +00001390 StringMap<DIE*>::iterator GI = Globals.find(MF->getFunction()->getName());
Devang Patel70f44262009-06-29 20:38:13 +00001391 if (GI != Globals.end()) {
1392 DIE *SPDie = GI->second;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001393
Devang Patel70f44262009-06-29 20:38:13 +00001394 // Add the function bounds.
1395 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1396 DWLabel("func_begin", SubprogramCount));
1397 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1398 DWLabel("func_end", SubprogramCount));
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001399
Devang Patel70f44262009-06-29 20:38:13 +00001400 MachineLocation Location(RI->getFrameRegister(*MF));
1401 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Bill Wendling0310d762009-05-15 09:23:25 +00001402 }
Bill Wendling0310d762009-05-15 09:23:25 +00001403}
1404
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001405/// GetOrCreateSourceID - Look up the source id with the given directory and
1406/// source file names. If none currently exists, create a new id and insert it
1407/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1408/// maps as well.
1409unsigned DwarfDebug::GetOrCreateSourceID(const std::string &DirName,
1410 const std::string &FileName) {
1411 unsigned DId;
1412 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1413 if (DI != DirectoryIdMap.end()) {
1414 DId = DI->getValue();
1415 } else {
1416 DId = DirectoryNames.size() + 1;
1417 DirectoryIdMap[DirName] = DId;
1418 DirectoryNames.push_back(DirName);
1419 }
1420
1421 unsigned FId;
1422 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1423 if (FI != SourceFileIdMap.end()) {
1424 FId = FI->getValue();
1425 } else {
1426 FId = SourceFileNames.size() + 1;
1427 SourceFileIdMap[FileName] = FId;
1428 SourceFileNames.push_back(FileName);
1429 }
1430
1431 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1432 SourceIdMap.find(std::make_pair(DId, FId));
1433 if (SI != SourceIdMap.end())
1434 return SI->second;
1435
1436 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1437 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1438 SourceIds.push_back(std::make_pair(DId, FId));
1439
1440 return SrcId;
1441}
1442
Devang Patele4b27562009-08-28 23:24:31 +00001443void DwarfDebug::ConstructCompileUnit(MDNode *N) {
1444 DICompileUnit DIUnit(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001445 std::string Dir, FN, Prod;
1446 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
1447 DIUnit.getFilename(FN));
1448
1449 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
1450 AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
1451 DWLabel("section_line", 0), DWLabel("section_line", 0),
1452 false);
1453 AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
1454 DIUnit.getProducer(Prod));
1455 AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
1456 DIUnit.getLanguage());
1457 AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
1458
1459 if (!Dir.empty())
1460 AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
1461 if (DIUnit.isOptimized())
1462 AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1463
1464 std::string Flags;
1465 DIUnit.getFlags(Flags);
1466 if (!Flags.empty())
1467 AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
1468
1469 unsigned RVer = DIUnit.getRunTimeVersion();
1470 if (RVer)
1471 AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1472 dwarf::DW_FORM_data1, RVer);
1473
1474 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001475 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001476 // Use first compile unit marked as isMain as the compile unit
1477 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001478 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001479 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001480
Devang Patele4b27562009-08-28 23:24:31 +00001481 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001482 CompileUnits.push_back(Unit);
1483}
1484
Devang Patele4b27562009-08-28 23:24:31 +00001485void DwarfDebug::ConstructGlobalVariableDIE(MDNode *N) {
1486 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001487
Devang Patel905cf5e2009-09-04 23:59:07 +00001488 // If debug information is malformed then ignore it.
1489 if (DI_GV.Verify() == false)
1490 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001491
1492 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001493 DIE *&Slot = ModuleCU->getDieMapSlotFor(DI_GV.getNode());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001494 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001495 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001496
Devang Patel1dbc7712009-06-29 20:45:18 +00001497 DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001498
1499 // Add address.
1500 DIEBlock *Block = new DIEBlock();
1501 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001502 AddObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner334fd1f2009-09-16 00:08:41 +00001503 Asm->Mang->getMangledName(DI_GV.getGlobal()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001504 AddBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1505
1506 // Add to map.
1507 Slot = VariableDie;
1508
1509 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001510 ModuleCU->getDie()->AddChild(VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001511
1512 // Expose as global. FIXME - need to check external flag.
1513 std::string Name;
Devang Patel1dbc7712009-06-29 20:45:18 +00001514 ModuleCU->AddGlobal(DI_GV.getName(Name), VariableDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001515 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001516}
1517
Devang Patele4b27562009-08-28 23:24:31 +00001518void DwarfDebug::ConstructSubprogram(MDNode *N) {
1519 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001520
1521 // Check for pre-existence.
Devang Patele4b27562009-08-28 23:24:31 +00001522 DIE *&Slot = ModuleCU->getDieMapSlotFor(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001523 if (Slot)
Devang Patel13e16b62009-06-26 01:49:18 +00001524 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001525
1526 if (!SP.isDefinition())
1527 // This is a method declaration which will be handled while constructing
1528 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001529 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001530
Devang Patel1dbc7712009-06-29 20:45:18 +00001531 DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001532
1533 // Add to map.
1534 Slot = SubprogramDie;
1535
1536 // Add to context owner.
Devang Patel1dbc7712009-06-29 20:45:18 +00001537 ModuleCU->getDie()->AddChild(SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001538
1539 // Expose as global.
1540 std::string Name;
Devang Patel1dbc7712009-06-29 20:45:18 +00001541 ModuleCU->AddGlobal(SP.getName(Name), SubprogramDie);
Devang Patel13e16b62009-06-26 01:49:18 +00001542 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001543}
1544
Daniel Dunbar00564992009-09-19 20:40:14 +00001545/// BeginModule - Emit all Dwarf sections that should come prior to the
1546/// content. Create global DIEs and emit initial debug info sections.
1547/// This is inovked by the target AsmPrinter.
Devang Patel208622d2009-06-25 22:36:02 +00001548void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) {
1549 this->M = M;
1550
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001551 if (TimePassesIsEnabled)
1552 DebugTimer->startTimer();
1553
Devang Patel78ab9e22009-07-30 18:56:46 +00001554 DebugInfoFinder DbgFinder;
1555 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001556
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001557 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001558 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1559 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001560 ConstructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001561
1562 if (CompileUnits.empty()) {
1563 if (TimePassesIsEnabled)
1564 DebugTimer->stopTimer();
1565
1566 return;
1567 }
1568
Devang Patel70f44262009-06-29 20:38:13 +00001569 // If main compile unit for this module is not seen than randomly
1570 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001571 if (!ModuleCU)
1572 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001573
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001574 // If there is not any debug info available for any global variables and any
1575 // subprograms then there is not any debug info to emit.
Devang Patel78ab9e22009-07-30 18:56:46 +00001576 if (DbgFinder.global_variable_count() == 0
1577 && DbgFinder.subprogram_count() == 0) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001578 if (TimePassesIsEnabled)
1579 DebugTimer->stopTimer();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001580 return;
1581 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001582
Devang Patel13e16b62009-06-26 01:49:18 +00001583 // Create DIEs for each of the externally visible global variables.
Devang Patel78ab9e22009-07-30 18:56:46 +00001584 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1585 E = DbgFinder.global_variable_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001586 ConstructGlobalVariableDIE(*I);
1587
1588 // Create DIEs for each of the externally visible subprograms.
Devang Patel78ab9e22009-07-30 18:56:46 +00001589 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1590 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +00001591 ConstructSubprogram(*I);
1592
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001593 MMI = mmi;
1594 shouldEmit = true;
1595 MMI->setDebugInfoAvailability(true);
1596
1597 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001598 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001599
1600 // Print out .file directives to specify files for .loc directives. These are
1601 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001602 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001603 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1604 // Remember source id starts at 1.
1605 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1606 sys::Path FullPath(getSourceDirectoryName(Id.first));
1607 bool AppendOk =
1608 FullPath.appendComponent(getSourceFileName(Id.second));
1609 assert(AppendOk && "Could not append filename to directory!");
1610 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001611 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001612 Asm->EOL();
1613 }
1614 }
1615
1616 // Emit initial sections
1617 EmitInitial();
1618
1619 if (TimePassesIsEnabled)
1620 DebugTimer->stopTimer();
1621}
1622
1623/// EndModule - Emit all Dwarf sections that should come after the content.
1624///
1625void DwarfDebug::EndModule() {
1626 if (!ShouldEmitDwarfDebug())
1627 return;
1628
1629 if (TimePassesIsEnabled)
1630 DebugTimer->startTimer();
1631
1632 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001633 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001634 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001635 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001636 EmitLabel("data_end", 0);
1637
1638 // End text sections.
1639 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001640 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001641 EmitLabel("section_end", i);
1642 }
1643
1644 // Emit common frame information.
1645 EmitCommonDebugFrame();
1646
1647 // Emit function debug frame information
1648 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1649 E = DebugFrames.end(); I != E; ++I)
1650 EmitFunctionDebugFrame(*I);
1651
1652 // Compute DIE offsets and sizes.
1653 SizeAndOffsets();
1654
1655 // Emit all the DIEs into a debug info section
1656 EmitDebugInfo();
1657
1658 // Corresponding abbreviations into a abbrev section.
1659 EmitAbbreviations();
1660
1661 // Emit source line correspondence into a debug line section.
1662 EmitDebugLines();
1663
1664 // Emit info into a debug pubnames section.
1665 EmitDebugPubNames();
1666
1667 // Emit info into a debug str section.
1668 EmitDebugStr();
1669
1670 // Emit info into a debug loc section.
1671 EmitDebugLoc();
1672
1673 // Emit info into a debug aranges section.
1674 EmitDebugARanges();
1675
1676 // Emit info into a debug ranges section.
1677 EmitDebugRanges();
1678
1679 // Emit info into a debug macinfo section.
1680 EmitDebugMacInfo();
1681
1682 // Emit inline info.
1683 EmitDebugInlineInfo();
1684
1685 if (TimePassesIsEnabled)
1686 DebugTimer->stopTimer();
1687}
1688
1689/// BeginFunction - Gather pre-function debug information. Assumes being
1690/// emitted immediately after the function entry point.
1691void DwarfDebug::BeginFunction(MachineFunction *MF) {
1692 this->MF = MF;
1693
1694 if (!ShouldEmitDwarfDebug()) return;
1695
1696 if (TimePassesIsEnabled)
1697 DebugTimer->startTimer();
1698
1699 // Begin accumulating function debug information.
1700 MMI->BeginFunction(MF);
1701
1702 // Assumes in correct section after the entry point.
1703 EmitLabel("func_begin", ++SubprogramCount);
1704
1705 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1706 // function.
1707 DebugLoc FDL = MF->getDefaultDebugLoc();
1708 if (!FDL.isUnknown()) {
1709 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
1710 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
1711 DICompileUnit(DLT.CompileUnit));
1712 Asm->printLabel(LabelID);
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001713 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001714 }
1715
1716 if (TimePassesIsEnabled)
1717 DebugTimer->stopTimer();
1718}
1719
1720/// EndFunction - Gather and emit post-function debug information.
1721///
1722void DwarfDebug::EndFunction(MachineFunction *MF) {
1723 if (!ShouldEmitDwarfDebug()) return;
1724
1725 if (TimePassesIsEnabled)
1726 DebugTimer->startTimer();
1727
1728 // Define end label for subprogram.
1729 EmitLabel("func_end", SubprogramCount);
1730
1731 // Get function line info.
1732 if (!Lines.empty()) {
1733 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00001734 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001735 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
1736 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
1737 // Append the function info to section info.
1738 SectionLineInfos.insert(SectionLineInfos.end(),
1739 Lines.begin(), Lines.end());
1740 }
1741
1742 // Construct the DbgScope for abstract instances.
1743 for (SmallVector<DbgScope *, 32>::iterator
1744 I = AbstractInstanceRootList.begin(),
1745 E = AbstractInstanceRootList.end(); I != E; ++I)
Bill Wendling17956162009-05-20 23:28:48 +00001746 ConstructFunctionDbgScope(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001747
1748 // Construct scopes for subprogram.
1749 if (FunctionDbgScope)
1750 ConstructFunctionDbgScope(FunctionDbgScope);
1751 else
1752 // FIXME: This is wrong. We are essentially getting past a problem with
1753 // debug information not being able to handle unreachable blocks that have
1754 // debug information in them. In particular, those unreachable blocks that
1755 // have "region end" info in them. That situation results in the "root
1756 // scope" not being created. If that's the case, then emit a "default"
1757 // scope, i.e., one that encompasses the whole function. This isn't
1758 // desirable. And a better way of handling this (and all of the debugging
1759 // information) needs to be explored.
1760 ConstructDefaultDbgScope(MF);
1761
1762 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
1763 MMI->getFrameMoves()));
1764
1765 // Clear debug info
1766 if (FunctionDbgScope) {
1767 delete FunctionDbgScope;
1768 DbgScopeMap.clear();
1769 DbgAbstractScopeMap.clear();
1770 DbgConcreteScopeMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001771 FunctionDbgScope = NULL;
1772 LexicalScopeStack.clear();
1773 AbstractInstanceRootList.clear();
Devang Patel9217f792009-06-12 19:24:05 +00001774 AbstractInstanceRootMap.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001775 }
1776
1777 Lines.clear();
1778
1779 if (TimePassesIsEnabled)
1780 DebugTimer->stopTimer();
1781}
1782
1783/// RecordSourceLine - Records location information and associates it with a
1784/// label. Returns a unique label ID used to generate a label and provide
1785/// correspondence to the source line list.
1786unsigned DwarfDebug::RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
1787 if (TimePassesIsEnabled)
1788 DebugTimer->startTimer();
1789
1790 CompileUnit *Unit = CompileUnitMap[V];
1791 assert(Unit && "Unable to find CompileUnit");
1792 unsigned ID = MMI->NextLabelID();
1793 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
1794
1795 if (TimePassesIsEnabled)
1796 DebugTimer->stopTimer();
1797
1798 return ID;
1799}
1800
1801/// RecordSourceLine - Records location information and associates it with a
1802/// label. Returns a unique label ID used to generate a label and provide
1803/// correspondence to the source line list.
1804unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col,
1805 DICompileUnit CU) {
Devang Patele4b27562009-08-28 23:24:31 +00001806 if (!MMI)
1807 return 0;
1808
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001809 if (TimePassesIsEnabled)
1810 DebugTimer->startTimer();
1811
1812 std::string Dir, Fn;
1813 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
1814 CU.getFilename(Fn));
1815 unsigned ID = MMI->NextLabelID();
1816 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
1817
1818 if (TimePassesIsEnabled)
1819 DebugTimer->stopTimer();
1820
1821 return ID;
1822}
1823
1824/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
1825/// timed. Look up the source id with the given directory and source file
1826/// names. If none currently exists, create a new id and insert it in the
1827/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
1828/// well.
1829unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
1830 const std::string &FileName) {
1831 if (TimePassesIsEnabled)
1832 DebugTimer->startTimer();
1833
1834 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
1835
1836 if (TimePassesIsEnabled)
1837 DebugTimer->stopTimer();
1838
1839 return SrcId;
1840}
1841
1842/// RecordRegionStart - Indicate the start of a region.
Devang Patele4b27562009-08-28 23:24:31 +00001843unsigned DwarfDebug::RecordRegionStart(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001844 if (TimePassesIsEnabled)
1845 DebugTimer->startTimer();
1846
Devang Patele4b27562009-08-28 23:24:31 +00001847 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001848 unsigned ID = MMI->NextLabelID();
1849 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1850 LexicalScopeStack.push_back(Scope);
1851
1852 if (TimePassesIsEnabled)
1853 DebugTimer->stopTimer();
1854
1855 return ID;
1856}
1857
1858/// RecordRegionEnd - Indicate the end of a region.
Devang Patele4b27562009-08-28 23:24:31 +00001859unsigned DwarfDebug::RecordRegionEnd(MDNode *N) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001860 if (TimePassesIsEnabled)
1861 DebugTimer->startTimer();
1862
Devang Patele4b27562009-08-28 23:24:31 +00001863 DbgScope *Scope = getOrCreateScope(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001864 unsigned ID = MMI->NextLabelID();
1865 Scope->setEndLabelID(ID);
Devang Pateldaf9e022009-06-13 02:16:18 +00001866 // FIXME : region.end() may not be in the last basic block.
1867 // For now, do not pop last lexical scope because next basic
1868 // block may start new inlined function's body.
1869 unsigned LSSize = LexicalScopeStack.size();
1870 if (LSSize != 0 && LSSize != 1)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001871 LexicalScopeStack.pop_back();
1872
1873 if (TimePassesIsEnabled)
1874 DebugTimer->stopTimer();
1875
1876 return ID;
1877}
1878
1879/// RecordVariable - Indicate the declaration of a local variable.
Devang Patele4b27562009-08-28 23:24:31 +00001880void DwarfDebug::RecordVariable(MDNode *N, unsigned FrameIndex) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001881 if (TimePassesIsEnabled)
1882 DebugTimer->startTimer();
1883
Devang Patele4b27562009-08-28 23:24:31 +00001884 DIDescriptor Desc(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001885 DbgScope *Scope = NULL;
1886 bool InlinedFnVar = false;
1887
Devang Patele4b27562009-08-28 23:24:31 +00001888 if (Desc.getTag() == dwarf::DW_TAG_variable)
1889 Scope = getOrCreateScope(DIGlobalVariable(N).getContext().getNode());
1890 else {
Devang Patel24f20e02009-08-22 17:12:53 +00001891 bool InlinedVar = false;
Devang Patele4b27562009-08-28 23:24:31 +00001892 MDNode *Context = DIVariable(N).getContext().getNode();
1893 DISubprogram SP(Context);
Devang Patel24f20e02009-08-22 17:12:53 +00001894 if (!SP.isNull()) {
1895 // SP is inserted into DbgAbstractScopeMap when inlined function
1896 // start was recorded by RecordInlineFnStart.
Devang Patele4b27562009-08-28 23:24:31 +00001897 DenseMap<MDNode *, DbgScope *>::iterator
1898 I = DbgAbstractScopeMap.find(SP.getNode());
Devang Patel24f20e02009-08-22 17:12:53 +00001899 if (I != DbgAbstractScopeMap.end()) {
1900 InlinedVar = true;
1901 Scope = I->second;
1902 }
1903 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001904 if (!InlinedVar)
Devang Patele4b27562009-08-28 23:24:31 +00001905 Scope = getOrCreateScope(Context);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001906 }
1907
1908 assert(Scope && "Unable to find the variable's scope");
Devang Patele4b27562009-08-28 23:24:31 +00001909 DbgVariable *DV = new DbgVariable(DIVariable(N), FrameIndex, InlinedFnVar);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001910 Scope->AddVariable(DV);
1911
1912 if (TimePassesIsEnabled)
1913 DebugTimer->stopTimer();
1914}
1915
1916//// RecordInlinedFnStart - Indicate the start of inlined subroutine.
1917unsigned DwarfDebug::RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
1918 unsigned Line, unsigned Col) {
1919 unsigned LabelID = MMI->NextLabelID();
1920
Chris Lattner33adcfb2009-08-22 21:43:10 +00001921 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001922 return LabelID;
1923
1924 if (TimePassesIsEnabled)
1925 DebugTimer->startTimer();
1926
Devang Patele4b27562009-08-28 23:24:31 +00001927 MDNode *Node = SP.getNode();
1928 DenseMap<const MDNode *, DbgScope *>::iterator
1929 II = AbstractInstanceRootMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001930
1931 if (II == AbstractInstanceRootMap.end()) {
1932 // Create an abstract instance entry for this inlined function if it doesn't
1933 // already exist.
Devang Patele4b27562009-08-28 23:24:31 +00001934 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001935
1936 // Get the compile unit context.
Devang Patele4b27562009-08-28 23:24:31 +00001937 DIE *SPDie = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001938 if (!SPDie)
Devang Patel1dbc7712009-06-29 20:45:18 +00001939 SPDie = CreateSubprogramDIE(ModuleCU, SP, false, true);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001940
1941 // Mark as being inlined. This makes this subprogram entry an abstract
1942 // instance root.
1943 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
1944 // that it's defined. That probably won't change in the future. However,
1945 // this could be more elegant.
1946 AddUInt(SPDie, dwarf::DW_AT_inline, 0, dwarf::DW_INL_declared_not_inlined);
1947
1948 // Keep track of the abstract scope for this function.
Devang Patele4b27562009-08-28 23:24:31 +00001949 DbgAbstractScopeMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001950
Devang Patele4b27562009-08-28 23:24:31 +00001951 AbstractInstanceRootMap[Node] = Scope;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001952 AbstractInstanceRootList.push_back(Scope);
1953 }
1954
1955 // Create a concrete inlined instance for this inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001956 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(Node));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001957 DIE *ScopeDie = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel1dbc7712009-06-29 20:45:18 +00001958 ScopeDie->setAbstractCompileUnit(ModuleCU);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001959
Devang Patele4b27562009-08-28 23:24:31 +00001960 DIE *Origin = ModuleCU->getDieMapSlotFor(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001961 AddDIEEntry(ScopeDie, dwarf::DW_AT_abstract_origin,
1962 dwarf::DW_FORM_ref4, Origin);
Devang Patel1dbc7712009-06-29 20:45:18 +00001963 AddUInt(ScopeDie, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001964 AddUInt(ScopeDie, dwarf::DW_AT_call_line, 0, Line);
1965 AddUInt(ScopeDie, dwarf::DW_AT_call_column, 0, Col);
1966
1967 ConcreteScope->setDie(ScopeDie);
1968 ConcreteScope->setStartLabelID(LabelID);
1969 MMI->RecordUsedDbgLabel(LabelID);
1970
1971 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
1972
1973 // Keep track of the concrete scope that's inlined into this function.
Devang Patele4b27562009-08-28 23:24:31 +00001974 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
1975 SI = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001976
1977 if (SI == DbgConcreteScopeMap.end())
Devang Patele4b27562009-08-28 23:24:31 +00001978 DbgConcreteScopeMap[Node].push_back(ConcreteScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001979 else
1980 SI->second.push_back(ConcreteScope);
1981
1982 // Track the start label for this inlined function.
Devang Patele4b27562009-08-28 23:24:31 +00001983 DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
1984 I = InlineInfo.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001985
1986 if (I == InlineInfo.end())
Devang Patele4b27562009-08-28 23:24:31 +00001987 InlineInfo[Node].push_back(LabelID);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001988 else
1989 I->second.push_back(LabelID);
1990
1991 if (TimePassesIsEnabled)
1992 DebugTimer->stopTimer();
1993
1994 return LabelID;
1995}
1996
1997/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
1998unsigned DwarfDebug::RecordInlinedFnEnd(DISubprogram &SP) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001999 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002000 return 0;
2001
2002 if (TimePassesIsEnabled)
2003 DebugTimer->startTimer();
2004
Devang Patele4b27562009-08-28 23:24:31 +00002005 MDNode *Node = SP.getNode();
2006 DenseMap<MDNode *, SmallVector<DbgScope *, 8> >::iterator
2007 I = DbgConcreteScopeMap.find(Node);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002008
2009 if (I == DbgConcreteScopeMap.end()) {
2010 // FIXME: Can this situation actually happen? And if so, should it?
2011 if (TimePassesIsEnabled)
2012 DebugTimer->stopTimer();
2013
2014 return 0;
2015 }
2016
2017 SmallVector<DbgScope *, 8> &Scopes = I->second;
Devang Patel11a407f2009-06-15 21:45:50 +00002018 if (Scopes.empty()) {
2019 // Returned ID is 0 if this is unbalanced "end of inlined
2020 // scope". This could happen if optimizer eats dbg intrinsics
2021 // or "beginning of inlined scope" is not recoginized due to
2022 // missing location info. In such cases, ignore this region.end.
2023 return 0;
2024 }
2025
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002026 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
2027 unsigned ID = MMI->NextLabelID();
2028 MMI->RecordUsedDbgLabel(ID);
2029 Scope->setEndLabelID(ID);
2030
2031 if (TimePassesIsEnabled)
2032 DebugTimer->stopTimer();
2033
2034 return ID;
2035}
2036
Bill Wendling829e67b2009-05-20 23:22:40 +00002037//===----------------------------------------------------------------------===//
2038// Emit Methods
2039//===----------------------------------------------------------------------===//
2040
Bill Wendling94d04b82009-05-20 23:21:38 +00002041/// SizeAndOffsetDie - Compute the size and offset of a DIE.
2042///
2043unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2044 // Get the children.
2045 const std::vector<DIE *> &Children = Die->getChildren();
2046
2047 // If not last sibling and has children then add sibling offset attribute.
2048 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2049
2050 // Record the abbreviation.
2051 AssignAbbrevNumber(Die->getAbbrev());
2052
2053 // Get the abbreviation for this DIE.
2054 unsigned AbbrevNumber = Die->getAbbrevNumber();
2055 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2056
2057 // Set DIE offset
2058 Die->setOffset(Offset);
2059
2060 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002061 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002062
2063 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2064 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2065
2066 // Size the DIE attribute values.
2067 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2068 // Size attribute value.
2069 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2070
2071 // Size the DIE children if any.
2072 if (!Children.empty()) {
2073 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2074 "Children flag not set");
2075
2076 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2077 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2078
2079 // End of children marker.
2080 Offset += sizeof(int8_t);
2081 }
2082
2083 Die->setSize(Offset - Die->getOffset());
2084 return Offset;
2085}
2086
2087/// SizeAndOffsets - Compute the size and offset of all the DIEs.
2088///
2089void DwarfDebug::SizeAndOffsets() {
2090 // Compute size of compile unit header.
2091 static unsigned Offset =
2092 sizeof(int32_t) + // Length of Compilation Unit Info
2093 sizeof(int16_t) + // DWARF version number
2094 sizeof(int32_t) + // Offset Into Abbrev. Section
2095 sizeof(int8_t); // Pointer Size (in bytes)
2096
Devang Patel1dbc7712009-06-29 20:45:18 +00002097 SizeAndOffsetDie(ModuleCU->getDie(), Offset, true);
2098 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002099}
2100
2101/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2102/// tools to recognize the object file contains Dwarf information.
2103void DwarfDebug::EmitInitial() {
2104 // Check to see if we already emitted intial headers.
2105 if (didInitial) return;
2106 didInitial = true;
2107
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002108 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002109
Bill Wendling94d04b82009-05-20 23:21:38 +00002110 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002111 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002112 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002113 EmitLabel("section_debug_frame", 0);
2114 }
2115
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002116 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002117 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002118 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002119 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002120 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002121 EmitLabel("section_aranges", 0);
2122
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002123 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2124 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002125 EmitLabel("section_macinfo", 0);
2126 }
2127
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002128 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002129 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002130 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002131 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002132 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002133 EmitLabel("section_pubnames", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002134 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002135 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002136 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002137 EmitLabel("section_ranges", 0);
2138
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002139 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002140 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002141 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002142 EmitLabel("data_begin", 0);
2143}
2144
2145/// EmitDIE - Recusively Emits a debug information entry.
2146///
2147void DwarfDebug::EmitDIE(DIE *Die) {
2148 // Get the abbreviation for this DIE.
2149 unsigned AbbrevNumber = Die->getAbbrevNumber();
2150 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2151
2152 Asm->EOL();
2153
2154 // Emit the code (index) for the abbreviation.
2155 Asm->EmitULEB128Bytes(AbbrevNumber);
2156
2157 if (Asm->isVerbose())
2158 Asm->EOL(std::string("Abbrev [" +
2159 utostr(AbbrevNumber) +
2160 "] 0x" + utohexstr(Die->getOffset()) +
2161 ":0x" + utohexstr(Die->getSize()) + " " +
2162 dwarf::TagString(Abbrev->getTag())));
2163 else
2164 Asm->EOL();
2165
2166 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2167 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2168
2169 // Emit the DIE attribute values.
2170 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2171 unsigned Attr = AbbrevData[i].getAttribute();
2172 unsigned Form = AbbrevData[i].getForm();
2173 assert(Form && "Too many attributes for DIE (check abbreviation)");
2174
2175 switch (Attr) {
2176 case dwarf::DW_AT_sibling:
2177 Asm->EmitInt32(Die->SiblingOffset());
2178 break;
2179 case dwarf::DW_AT_abstract_origin: {
2180 DIEEntry *E = cast<DIEEntry>(Values[i]);
2181 DIE *Origin = E->getEntry();
2182 unsigned Addr =
2183 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
2184 Origin->getOffset();
2185
2186 Asm->EmitInt32(Addr);
2187 break;
2188 }
2189 default:
2190 // Emit an attribute using the defined form.
2191 Values[i]->EmitValue(this, Form);
2192 break;
2193 }
2194
2195 Asm->EOL(dwarf::AttributeString(Attr));
2196 }
2197
2198 // Emit the DIE children if any.
2199 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2200 const std::vector<DIE *> &Children = Die->getChildren();
2201
2202 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2203 EmitDIE(Children[j]);
2204
2205 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2206 }
2207}
2208
2209/// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
2210///
2211void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) {
2212 DIE *Die = Unit->getDie();
2213
2214 // Emit the compile units header.
2215 EmitLabel("info_begin", Unit->getID());
2216
2217 // Emit size of content not including length itself
2218 unsigned ContentSize = Die->getSize() +
2219 sizeof(int16_t) + // DWARF version number
2220 sizeof(int32_t) + // Offset Into Abbrev. Section
2221 sizeof(int8_t) + // Pointer Size (in bytes)
2222 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2223
2224 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2225 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2226 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2227 Asm->EOL("Offset Into Abbrev. Section");
2228 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2229
2230 EmitDIE(Die);
2231 // FIXME - extra padding for gdb bug.
2232 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2233 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2234 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2235 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2236 EmitLabel("info_end", Unit->getID());
2237
2238 Asm->EOL();
2239}
2240
2241void DwarfDebug::EmitDebugInfo() {
2242 // Start debug info section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002243 Asm->OutStreamer.SwitchSection(
2244 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002245
Devang Patel1dbc7712009-06-29 20:45:18 +00002246 EmitDebugInfoPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002247}
2248
2249/// EmitAbbreviations - Emit the abbreviation section.
2250///
2251void DwarfDebug::EmitAbbreviations() const {
2252 // Check to see if it is worth the effort.
2253 if (!Abbreviations.empty()) {
2254 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002255 Asm->OutStreamer.SwitchSection(
2256 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002257
2258 EmitLabel("abbrev_begin", 0);
2259
2260 // For each abbrevation.
2261 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2262 // Get abbreviation data
2263 const DIEAbbrev *Abbrev = Abbreviations[i];
2264
2265 // Emit the abbrevations code (base 1 index.)
2266 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2267 Asm->EOL("Abbreviation Code");
2268
2269 // Emit the abbreviations data.
2270 Abbrev->Emit(Asm);
2271
2272 Asm->EOL();
2273 }
2274
2275 // Mark end of abbreviations.
2276 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2277
2278 EmitLabel("abbrev_end", 0);
2279 Asm->EOL();
2280 }
2281}
2282
2283/// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2284/// the line matrix.
2285///
2286void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) {
2287 // Define last address of section.
2288 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2289 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2290 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2291 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2292
2293 // Mark end of matrix.
2294 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2295 Asm->EmitULEB128Bytes(1); Asm->EOL();
2296 Asm->EmitInt8(1); Asm->EOL();
2297}
2298
2299/// EmitDebugLines - Emit source line information.
2300///
2301void DwarfDebug::EmitDebugLines() {
2302 // If the target is using .loc/.file, the assembler will be emitting the
2303 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002304 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002305 return;
2306
2307 // Minimum line delta, thus ranging from -10..(255-10).
2308 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2309 // Maximum line delta, thus ranging from -10..(255-10).
2310 const int MaxLineDelta = 255 + MinLineDelta;
2311
2312 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002313 Asm->OutStreamer.SwitchSection(
2314 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002315
2316 // Construct the section header.
2317 EmitDifference("line_end", 0, "line_begin", 0, true);
2318 Asm->EOL("Length of Source Line Info");
2319 EmitLabel("line_begin", 0);
2320
2321 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2322
2323 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2324 Asm->EOL("Prolog Length");
2325 EmitLabel("line_prolog_begin", 0);
2326
2327 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2328
2329 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2330
2331 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2332
2333 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2334
2335 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2336
2337 // Line number standard opcode encodings argument count
2338 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2339 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2340 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2341 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2342 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2343 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2344 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2345 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2346 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2347
2348 // Emit directories.
2349 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2350 Asm->EmitString(getSourceDirectoryName(DI));
2351 Asm->EOL("Directory");
2352 }
2353
2354 Asm->EmitInt8(0); Asm->EOL("End of directories");
2355
2356 // Emit files.
2357 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2358 // Remember source id starts at 1.
2359 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2360 Asm->EmitString(getSourceFileName(Id.second));
2361 Asm->EOL("Source");
2362 Asm->EmitULEB128Bytes(Id.first);
2363 Asm->EOL("Directory #");
2364 Asm->EmitULEB128Bytes(0);
2365 Asm->EOL("Mod date");
2366 Asm->EmitULEB128Bytes(0);
2367 Asm->EOL("File size");
2368 }
2369
2370 Asm->EmitInt8(0); Asm->EOL("End of files");
2371
2372 EmitLabel("line_prolog_end", 0);
2373
2374 // A sequence for each text section.
2375 unsigned SecSrcLinesSize = SectionSourceLines.size();
2376
2377 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2378 // Isolate current sections line info.
2379 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2380
Chris Lattner93b6db32009-08-08 23:39:42 +00002381 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002382 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002383 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002384 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002385 }*/
2386 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002387
2388 // Dwarf assumes we start with first line of first source file.
2389 unsigned Source = 1;
2390 unsigned Line = 1;
2391
2392 // Construct rows of the address, source, line, column matrix.
2393 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2394 const SrcLineInfo &LineInfo = LineInfos[i];
2395 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2396 if (!LabelID) continue;
2397
Caroline Ticec6f9d622009-09-11 18:25:54 +00002398 if (LineInfo.getLine() == 0) continue;
2399
Bill Wendling94d04b82009-05-20 23:21:38 +00002400 if (!Asm->isVerbose())
2401 Asm->EOL();
2402 else {
2403 std::pair<unsigned, unsigned> SourceID =
2404 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002405 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling94d04b82009-05-20 23:21:38 +00002406 << getSourceDirectoryName(SourceID.first) << ' '
2407 << getSourceFileName(SourceID.second)
2408 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2409 }
2410
2411 // Define the line address.
2412 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2413 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2414 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2415 EmitReference("label", LabelID); Asm->EOL("Location label");
2416
2417 // If change of source, then switch to the new source.
2418 if (Source != LineInfo.getSourceID()) {
2419 Source = LineInfo.getSourceID();
2420 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2421 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2422 }
2423
2424 // If change of line.
2425 if (Line != LineInfo.getLine()) {
2426 // Determine offset.
2427 int Offset = LineInfo.getLine() - Line;
2428 int Delta = Offset - MinLineDelta;
2429
2430 // Update line.
2431 Line = LineInfo.getLine();
2432
2433 // If delta is small enough and in range...
2434 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2435 // ... then use fast opcode.
2436 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2437 } else {
2438 // ... otherwise use long hand.
2439 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2440 Asm->EOL("DW_LNS_advance_line");
2441 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2442 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2443 }
2444 } else {
2445 // Copy the previous row (different address or source)
2446 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2447 }
2448 }
2449
2450 EmitEndOfLineMatrix(j + 1);
2451 }
2452
2453 if (SecSrcLinesSize == 0)
2454 // Because we're emitting a debug_line section, we still need a line
2455 // table. The linker and friends expect it to exist. If there's nothing to
2456 // put into it, emit an empty table.
2457 EmitEndOfLineMatrix(1);
2458
2459 EmitLabel("line_end", 0);
2460 Asm->EOL();
2461}
2462
2463/// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2464///
2465void DwarfDebug::EmitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002466 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002467 return;
2468
2469 int stackGrowth =
2470 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2471 TargetFrameInfo::StackGrowsUp ?
2472 TD->getPointerSize() : -TD->getPointerSize();
2473
2474 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002475 Asm->OutStreamer.SwitchSection(
2476 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002477
2478 EmitLabel("debug_frame_common", 0);
2479 EmitDifference("debug_frame_common_end", 0,
2480 "debug_frame_common_begin", 0, true);
2481 Asm->EOL("Length of Common Information Entry");
2482
2483 EmitLabel("debug_frame_common_begin", 0);
2484 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2485 Asm->EOL("CIE Identifier Tag");
2486 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2487 Asm->EOL("CIE Version");
2488 Asm->EmitString("");
2489 Asm->EOL("CIE Augmentation");
2490 Asm->EmitULEB128Bytes(1);
2491 Asm->EOL("CIE Code Alignment Factor");
2492 Asm->EmitSLEB128Bytes(stackGrowth);
2493 Asm->EOL("CIE Data Alignment Factor");
2494 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2495 Asm->EOL("CIE RA Column");
2496
2497 std::vector<MachineMove> Moves;
2498 RI->getInitialFrameState(Moves);
2499
2500 EmitFrameMoves(NULL, 0, Moves, false);
2501
2502 Asm->EmitAlignment(2, 0, 0, false);
2503 EmitLabel("debug_frame_common_end", 0);
2504
2505 Asm->EOL();
2506}
2507
2508/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2509/// section.
2510void
2511DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002512 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002513 return;
2514
2515 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002516 Asm->OutStreamer.SwitchSection(
2517 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002518
2519 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2520 "debug_frame_begin", DebugFrameInfo.Number, true);
2521 Asm->EOL("Length of Frame Information Entry");
2522
2523 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2524
2525 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2526 0, 0, true, false);
2527 Asm->EOL("FDE CIE offset");
2528
2529 EmitReference("func_begin", DebugFrameInfo.Number);
2530 Asm->EOL("FDE initial location");
2531 EmitDifference("func_end", DebugFrameInfo.Number,
2532 "func_begin", DebugFrameInfo.Number);
2533 Asm->EOL("FDE address range");
2534
2535 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2536 false);
2537
2538 Asm->EmitAlignment(2, 0, 0, false);
2539 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2540
2541 Asm->EOL();
2542}
2543
2544void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2545 EmitDifference("pubnames_end", Unit->getID(),
2546 "pubnames_begin", Unit->getID(), true);
2547 Asm->EOL("Length of Public Names Info");
2548
2549 EmitLabel("pubnames_begin", Unit->getID());
2550
2551 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2552
2553 EmitSectionOffset("info_begin", "section_info",
2554 Unit->getID(), 0, true, false);
2555 Asm->EOL("Offset of Compilation Unit Info");
2556
2557 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2558 true);
2559 Asm->EOL("Compilation Unit Length");
2560
2561 StringMap<DIE*> &Globals = Unit->getGlobals();
2562 for (StringMap<DIE*>::const_iterator
2563 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2564 const char *Name = GI->getKeyData();
2565 DIE * Entity = GI->second;
2566
2567 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2568 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2569 }
2570
2571 Asm->EmitInt32(0); Asm->EOL("End Mark");
2572 EmitLabel("pubnames_end", Unit->getID());
2573
2574 Asm->EOL();
2575}
2576
2577/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2578///
2579void DwarfDebug::EmitDebugPubNames() {
2580 // Start the dwarf pubnames section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002581 Asm->OutStreamer.SwitchSection(
2582 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002583
Devang Patel1dbc7712009-06-29 20:45:18 +00002584 EmitDebugPubNamesPerCU(ModuleCU);
Bill Wendling94d04b82009-05-20 23:21:38 +00002585}
2586
2587/// EmitDebugStr - Emit visible names into a debug str section.
2588///
2589void DwarfDebug::EmitDebugStr() {
2590 // Check to see if it is worth the effort.
2591 if (!StringPool.empty()) {
2592 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002593 Asm->OutStreamer.SwitchSection(
2594 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002595
2596 // For each of strings in the string pool.
2597 for (unsigned StringID = 1, N = StringPool.size();
2598 StringID <= N; ++StringID) {
2599 // Emit a label for reference from debug information entries.
2600 EmitLabel("string", StringID);
2601
2602 // Emit the string itself.
2603 const std::string &String = StringPool[StringID];
2604 Asm->EmitString(String); Asm->EOL();
2605 }
2606
2607 Asm->EOL();
2608 }
2609}
2610
2611/// EmitDebugLoc - Emit visible names into a debug loc section.
2612///
2613void DwarfDebug::EmitDebugLoc() {
2614 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002615 Asm->OutStreamer.SwitchSection(
2616 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002617 Asm->EOL();
2618}
2619
2620/// EmitDebugARanges - Emit visible names into a debug aranges section.
2621///
2622void DwarfDebug::EmitDebugARanges() {
2623 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002624 Asm->OutStreamer.SwitchSection(
2625 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002626
2627 // FIXME - Mock up
2628#if 0
2629 CompileUnit *Unit = GetBaseCompileUnit();
2630
2631 // Don't include size of length
2632 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2633
2634 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2635
2636 EmitReference("info_begin", Unit->getID());
2637 Asm->EOL("Offset of Compilation Unit Info");
2638
2639 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2640
2641 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2642
2643 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2644 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2645
2646 // Range 1
2647 EmitReference("text_begin", 0); Asm->EOL("Address");
2648 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2649
2650 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2651 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2652#endif
2653
2654 Asm->EOL();
2655}
2656
2657/// EmitDebugRanges - Emit visible names into a debug ranges section.
2658///
2659void DwarfDebug::EmitDebugRanges() {
2660 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002661 Asm->OutStreamer.SwitchSection(
2662 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002663 Asm->EOL();
2664}
2665
2666/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2667///
2668void DwarfDebug::EmitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002669 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002670 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002671 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002672 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002673 Asm->EOL();
2674 }
2675}
2676
2677/// EmitDebugInlineInfo - Emit inline info using following format.
2678/// Section Header:
2679/// 1. length of section
2680/// 2. Dwarf version number
2681/// 3. address size.
2682///
2683/// Entries (one "entry" for each function that was inlined):
2684///
2685/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2686/// otherwise offset into __debug_str for regular function name.
2687/// 2. offset into __debug_str section for regular function name.
2688/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2689/// instances for the function.
2690///
2691/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2692/// inlined instance; the die_offset points to the inlined_subroutine die in the
2693/// __debug_info section, and the low_pc is the starting address for the
2694/// inlining instance.
2695void DwarfDebug::EmitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002696 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002697 return;
2698
Devang Patel1dbc7712009-06-29 20:45:18 +00002699 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002700 return;
2701
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002702 Asm->OutStreamer.SwitchSection(
2703 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002704 Asm->EOL();
2705 EmitDifference("debug_inlined_end", 1,
2706 "debug_inlined_begin", 1, true);
2707 Asm->EOL("Length of Debug Inlined Information Entry");
2708
2709 EmitLabel("debug_inlined_begin", 1);
2710
2711 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2712 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2713
Devang Patele4b27562009-08-28 23:24:31 +00002714 for (DenseMap<MDNode *, SmallVector<unsigned, 4> >::iterator
Bill Wendling94d04b82009-05-20 23:21:38 +00002715 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
Devang Patele4b27562009-08-28 23:24:31 +00002716 MDNode *Node = I->first;
Bill Wendling94d04b82009-05-20 23:21:38 +00002717 SmallVector<unsigned, 4> &Labels = I->second;
Devang Patele4b27562009-08-28 23:24:31 +00002718 DISubprogram SP(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00002719 std::string Name;
2720 std::string LName;
2721
2722 SP.getLinkageName(LName);
2723 SP.getName(Name);
2724
Devang Patel53cb17d2009-07-16 01:01:22 +00002725 if (LName.empty())
2726 Asm->EmitString(Name);
2727 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002728 // Skip special LLVM prefix that is used to inform the asm printer to not
2729 // emit usual symbol prefix before the symbol name. This happens for
2730 // Objective-C symbol names and symbol whose name is replaced using GCC's
2731 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002732 if (LName[0] == 1)
2733 LName = &LName[1];
2734 Asm->EmitString(LName);
2735 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002736 Asm->EOL("MIPS linkage name");
2737
2738 Asm->EmitString(Name); Asm->EOL("Function name");
2739
2740 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2741
2742 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2743 LE = Labels.end(); LI != LE; ++LI) {
Devang Patele4b27562009-08-28 23:24:31 +00002744 DIE *SP = ModuleCU->getDieMapSlotFor(Node);
Bill Wendling94d04b82009-05-20 23:21:38 +00002745 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2746
2747 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002748 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002749 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002750 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002751
2752 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2753 }
2754 }
2755
2756 EmitLabel("debug_inlined_end", 1);
2757 Asm->EOL();
2758}